|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 /* 00003 * $Id: Utility.php,v 1.3 2010/12/14 17:36:05 moodlerobot Exp $ 00004 * 00005 * Copyright(c) 2004-2006, SpikeSource Inc. All Rights Reserved. 00006 * Licensed under the Open Software License version 2.1 00007 * (See http://www.spikesource.com/license.html) 00008 */ 00009 ?> 00010 <?php 00011 00012 if(!defined("__PHPCOVERAGE_HOME")) { 00013 define("__PHPCOVERAGE_HOME", dirname(dirname(__FILE__))); 00014 } 00015 require_once __PHPCOVERAGE_HOME . "/conf/phpcoverage.conf.php"; 00016 00017 // include our dummy implementation 00018 require_once 'CoverageLogger.php'; 00019 00020 00028 class Utility { 00029 00030 public static $logger; 00031 00032 /*{{{ public function getTimeStamp() */ 00033 00041 public function getTimeStamp() { 00042 $ts = getdate(); 00043 return $ts["weekday"] . " " . $ts["month"] . " " . $ts["mday"] 00044 . ", " . $ts["year"] . " " . sprintf("%02d:%02d:%02d", $ts["hours"], $ts["minutes"], $ts["seconds"]); 00045 } 00046 00047 /*}}}*/ 00048 /*{{{ public function shortenFilename() */ 00049 00059 public function shortenFilename($filename, $maxlength=80) { 00060 $length = strlen($filename); 00061 if($length < $maxlength) { 00062 return $filename; 00063 } 00064 00065 // trim the first few characters 00066 $filename = substr($filename, $length-$maxlength); 00067 // If there is a path separator slash in first n characters, 00068 // trim upto that point. 00069 $n = 20; 00070 $firstSlash = strpos($filename, "/"); 00071 if($firstSlash === false || $firstSlash > $n) { 00072 $firstSlash = strpos($filename, "\\"); 00073 if($firstSlash === false || $firstSlash > $n) { 00074 return "..." . $filename; 00075 } 00076 return "..." . substr($filename, $firstSlash); 00077 } 00078 return "..." . substr($filename, $firstSlash); 00079 } 00080 00081 /*}}}*/ 00082 /*{{{ public function writeError() */ 00083 00090 public function writeError($str) { 00091 if(__PHPCOVERAGE_DEBUG) { 00092 error_log($str); 00093 } 00094 } 00095 /*}}}*/ 00096 /*{{{ public function unixifyPath() */ 00097 00105 public function unixifyPath($path) { 00106 // Remove the drive-letter: 00107 if(strpos($path, ":") == 1) { 00108 $path = substr($path, 2); 00109 } 00110 $path = $this->replaceBackslashes($path); 00111 return $path; 00112 } 00113 00114 /*}}}*/ 00115 /*{{{ public function replaceBackslashes() */ 00116 00124 public function replaceBackslashes($path) { 00125 $path = str_replace("\\", "/", $path); 00126 return $this->capitalizeDriveLetter($path); 00127 } 00128 /*}}}*/ 00129 /*{{{ public function capitalizeDriveLetter() */ 00130 00138 public function capitalizeDriveLetter($path) { 00139 if(strpos($path, ":") === 1) { 00140 $path = strtoupper(substr($path, 0, 1)) . substr($path, 1); 00141 } 00142 return $path; 00143 } 00144 00145 /*}}}*/ 00146 /*{{{ public function makeDirRecursive() */ 00156 public function makeDirRecursive($dir, $mode=0755) { 00157 // Check if directory already exists 00158 if (is_dir($dir) || empty($dir)) { 00159 return true; 00160 } 00161 00162 // Ensure a file does not already exist with the same name 00163 if (is_file($dir)) { 00164 $this->getLogger()->debug("File already exists: " . $dir, 00165 __FILE__, __LINE__); 00166 return false; 00167 } 00168 00169 $dir = $this->replaceBackslashes($dir); 00170 00171 // Crawl up the directory tree 00172 $next_pathname = substr($dir, 0, strrpos($dir, "/")); 00173 if ($this->makeDirRecursive($next_pathname, $mode)) { 00174 if (!file_exists($dir)) { 00175 return mkdir($dir, $mode); 00176 } 00177 } 00178 00179 return false; 00180 } 00181 /*}}}*/ 00182 /*{{{ public function getOS() */ 00191 public function getOS() { 00192 return strtoupper(substr(PHP_OS, 0, 3)); 00193 } 00194 /*}}}*/ 00195 /*{{{ public function getTmpDir() */ 00196 00197 public function getTmpDir() { 00198 global $spc_config; 00199 $OS = $this->getOS(); 00200 switch($OS) { 00201 case "WIN": 00202 return $spc_config['windows_tmpdir']; 00203 default: 00204 return $spc_config['tmpdir']; 00205 } 00206 } 00207 00208 /*}}}*/ 00209 /*{{{ public function getLogger() */ 00210 00211 public function getLogger($package=false) { 00212 global $spc_config; 00213 if(!isset($this->logger) || $this->logger == NULL) { 00214 $this->logger = new CoverageLogger(); 00215 $this->logger->setLevel($spc_config["log_level"]); 00216 } 00217 return $this->logger; 00218 } 00219 00220 /*}}}*/ 00221 } 00222 00223 $util = new Utility(); 00224 global $util; 00225 ?>