|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00017 class Solar_Dir { 00018 00026 protected static $_tmp; 00027 00049 public static function exists($dir) 00050 { 00051 // no file requested? 00052 $dir = trim($dir); 00053 if (! $dir) { 00054 return false; 00055 } 00056 00057 // using an absolute path for the file? 00058 // dual check for Unix '/' and Windows '\', 00059 // or Windows drive letter and a ':'. 00060 $abs = ($dir[0] == '/' || $dir[0] == '\\' || $dir[1] == ':'); 00061 if ($abs && is_dir($dir)) { 00062 return $dir; 00063 } 00064 00065 // using a relative path on the file 00066 $path = explode(PATH_SEPARATOR, ini_get('include_path')); 00067 foreach ($path as $base) { 00068 // strip Unix '/' and Windows '\' 00069 $target = rtrim($base, '\\/') . DIRECTORY_SEPARATOR . $dir; 00070 if (is_dir($target)) { 00071 return $target; 00072 } 00073 } 00074 00075 // never found it 00076 return false; 00077 } 00078 00094 public static function fix($dir) 00095 { 00096 $dir = str_replace('/', DIRECTORY_SEPARATOR, $dir); 00097 return rtrim($dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; 00098 } 00099 00112 public static function name($file, $up = 0) 00113 { 00114 $dir = dirname($file); 00115 while ($up --) { 00116 $dir = dirname($dir); 00117 } 00118 return $dir; 00119 } 00120 00131 public static function tmp($sub = '') 00132 { 00133 // find the tmp dir if needed 00134 if (! Solar_Dir::$_tmp) { 00135 00136 // use the system if we can 00137 if (function_exists('sys_get_temp_dir')) { 00138 $tmp = sys_get_temp_dir(); 00139 } else { 00140 $tmp = Solar_Dir::_tmp(); 00141 } 00142 00143 // remove trailing separator and save 00144 Solar_Dir::$_tmp = rtrim($tmp, DIRECTORY_SEPARATOR); 00145 } 00146 00147 // do we have a subdirectory request? 00148 $sub = trim($sub); 00149 if ($sub) { 00150 // remove leading and trailing separators, and force exactly 00151 // one trailing separator 00152 $sub = trim($sub, DIRECTORY_SEPARATOR) 00153 . DIRECTORY_SEPARATOR; 00154 } 00155 00156 return Solar_Dir::$_tmp . DIRECTORY_SEPARATOR . $sub; 00157 } 00158 00166 protected static function _tmp() 00167 { 00168 // non-Windows system? 00169 if (strtolower(substr(PHP_OS, 0, 3)) != 'win') { 00170 $tmp = empty($_ENV['TMPDIR']) ? getenv('TMPDIR') : $_ENV['TMPDIR']; 00171 if ($tmp) { 00172 return $tmp; 00173 } else { 00174 return '/tmp'; 00175 } 00176 } 00177 00178 // Windows 'TEMP' 00179 $tmp = empty($_ENV['TEMP']) ? getenv('TEMP') : $_ENV['TEMP']; 00180 if ($tmp) { 00181 return $tmp; 00182 } 00183 00184 // Windows 'TMP' 00185 $tmp = empty($_ENV['TMP']) ? getenv('TMP') : $_ENV['TMP']; 00186 if ($tmp) { 00187 return $tmp; 00188 } 00189 00190 // Windows 'windir' 00191 $tmp = empty($_ENV['windir']) ? getenv('windir') : $_ENV['windir']; 00192 if ($tmp) { 00193 return $tmp; 00194 } 00195 00196 // final fallback for Windows 00197 return getenv('SystemRoot') . '\\temp'; 00198 } 00199 }