|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00026 function fullPath($path,$dirsep=DIRECTORY_SEPARATOR) { 00027 $token = '$IMS-CC-FILEBASE$'; 00028 $path = str_replace($token,'',$path); 00029 if ( is_string($path) && ($path != '') ) { 00030 $sep = $dirsep; 00031 $dotDir= '.'; 00032 $upDir = '..'; 00033 $length= strlen($path); 00034 $rtemp= trim($path); 00035 $start = strrpos($path, $sep); 00036 $canContinue = ($start !== false); 00037 $result= $canContinue ? '': $path; 00038 $rcount=0; 00039 while ($canContinue) { 00040 $dirPart = ($start !== false) ? substr($rtemp,$start+1,$length-$start) : $rtemp; 00041 $canContinue = ($dirPart !== false); 00042 if ($canContinue) { 00043 if ($dirPart != $dotDir) { 00044 if ($dirPart == $upDir) { 00045 $rcount++; 00046 } else { 00047 if ($rcount > 0) { 00048 $rcount--; 00049 } else { 00050 $result = ($result == '') ? $dirPart : $dirPart.$sep.$result; 00051 } 00052 } 00053 } 00054 $rtemp = substr($path,0,$start); 00055 $start = strrpos($rtemp, $sep); 00056 $canContinue = (($start !== false) || (strlen($rtemp) > 0)); 00057 } 00058 } //end while 00059 } 00060 return $result; 00061 } 00062 00063 00064 00072 function stripUrl($path, $rootDir='') { 00073 $result = $path; 00074 if ( is_string($path) && ($path != '') ) { 00075 $start=strpos($path,'(')+1; 00076 $length=strpos($path,')')-$start; 00077 $rut = $rootDir.substr($path,$start,$length); 00078 $result=fullPath($rut,'/'); 00079 } 00080 return $result; 00081 } 00082 00089 function toNativePath(&$path) { 00090 for ($count = 0 ; $count < strlen($path); ++$count) { 00091 $chr = $path{$count}; 00092 if (($chr == '\\') || ($chr == '/')) { 00093 $path{$count} = '/'; 00094 } 00095 } 00096 } 00097 00098 00105 function toNativePath2(&$path) { 00106 for ($count = 0 ; $count < strlen($path); ++$count) { 00107 $chr = $path{$count}; 00108 if (($chr == '\\') || ($chr == '/')) { 00109 $path{$count} = DIRECTORY_SEPARATOR; 00110 } 00111 } 00112 } 00113 00119 function toUrlPath(&$path) { 00120 for ($count = 0 ; $count < strlen($path); ++$count) { 00121 $chr = $path{$count}; 00122 if (($chr == '\\')) { 00123 $path{$count} = '/'; 00124 } 00125 } 00126 } 00127 00135 function pathDiff($path1, $path2) { 00136 toUrlPath($path1); 00137 toUrlPath($path2); 00138 $result = ""; 00139 $bl2 = strlen($path2); 00140 $a = strpos($path1,$path2); 00141 if ($a !== false) { 00142 $result = trim(substr($path1,$bl2+$a),'/'); 00143 } 00144 return $result; 00145 } 00146 00157 function copyr($source, $dest) 00158 { 00159 // Simple copy for a file 00160 if (is_file($source)) { 00161 return copy($source, $dest); 00162 } 00163 00164 // Make destination directory 00165 if (!is_dir($dest)) { 00166 mkdir($dest); 00167 } 00168 00169 // Loop through the folder 00170 $dir = dir($source); 00171 while (false !== $entry = $dir->read()) { 00172 // Skip pointers 00173 if ($entry == '.' || $entry == '..') { 00174 continue; 00175 } 00176 00177 // Deep copy directories 00178 if ($dest !== "$source/$entry") { 00179 copyr("$source/$entry", "$dest/$entry"); 00180 } 00181 } 00182 00183 // Clean up 00184 $dir->close(); 00185 return true; 00186 } 00187 00199 function getDirectories($rootDir, $contains, $excludeitems = null, $startswith = true) { 00200 $result = is_dir($rootDir); 00201 if ($result) { 00202 $dirlist = dir($rootDir); 00203 $entry = null; 00204 $result = array(); 00205 while(false !== ($entry = $dirlist->read())) { 00206 $currdir = $rootDir.$entry; 00207 if (is_dir($currdir)) { 00208 $bret = strpos($entry,$contains); 00209 if (($bret !== false)) { 00210 if (($startswith && ($bret == 0)) || !$startswith) { 00211 if (!( is_array($excludeitems) && in_array($entry,$excludeitems) )) { 00212 $result[] = $entry; 00213 } 00214 } 00215 } 00216 } 00217 } 00218 } 00219 return $result; 00220 } 00221 00222 function getFilesOnly($rootDir, $contains, $excludeitems = null, $startswith = true,$extension=null) { 00223 $result = is_dir($rootDir); 00224 if ($result) { 00225 $filelist = dir($rootDir); 00226 $entry = null; 00227 $result = array(); 00228 while(false !== ($entry = $filelist->read())) { 00229 $curritem = $rootDir.$entry; 00230 $pinfo = pathinfo($entry); 00231 $ext = array_key_exists('extension',$pinfo) ? $pinfo['extension'] : null; 00232 if (is_file($curritem) && (is_null($extension) || ($ext == $extension) )) { 00233 $bret = strpos($entry,$contains); 00234 if (($bret !== false)) { 00235 if (($startswith && ($bret == 0)) || !$startswith) { 00236 if (!( is_array($excludeitems) && in_array($entry,$excludeitems) )) { 00237 $result[] = $entry; 00238 } 00239 } 00240 } 00241 } 00242 } 00243 } 00244 natcasesort($result); 00245 return $result; 00246 } 00247 00248 00249 00258 function search_ident_by_name($array,$name){ 00259 if (empty($array)){ 00260 throw new Exception('The array given is null'); 00261 } 00262 $ident = null; 00263 foreach ($array as $k => $v){ 00264 ($k); 00265 if ($v[1] == $name){ 00266 $ident = $v[0]; 00267 break; 00268 } 00269 } 00270 return $ident; 00271 } 00272 00273 00274 00275 00276 00286 function getRawFiles($startDir, &$fhandle, $rootDir='', $excludedirs = null, $excludefileext = null) { 00287 $result = is_dir($startDir); 00288 if ($result) { 00289 $dirlist = dir($startDir); 00290 $entry = null; 00291 while(false !== ($entry = $dirlist->read())) { 00292 $curritem = $startDir.$entry; 00293 if (($entry=='.') || ($entry =='..')) { 00294 continue; 00295 } 00296 if (is_dir($curritem)) { 00297 if (!( is_array($excludedirs) && in_array($entry,$excludedirs) )) { 00298 getRawFiles($startDir.$entry."/",$fhandle,$rootDir.$entry."/",$excludedirs,$excludefileext); 00299 } 00300 continue; 00301 } 00302 if (is_file($curritem)){ 00303 $pinfo = pathinfo($entry); 00304 $ext = array_key_exists('extension',$pinfo) ? $pinfo['extension'] : ''; 00305 if (!is_array($excludefileext) || 00306 (is_array($excludefileext) && !in_array($ext,$excludefileext))) { 00307 fwrite($fhandle,$rootDir.$entry."\n"); 00308 } 00309 } 00310 } 00311 } 00312 return $result; 00313 } 00314 00315 00316 function getRawFiles2($startDir,&$arr, $rootDir='', $excludedirs = null, $excludefileext = null) { 00317 00318 $result = is_dir($startDir); 00319 if ($result) { 00320 $dirlist = dir($startDir); 00321 $entry = null; 00322 while(false !== ($entry = $dirlist->read())) { 00323 $curritem = $startDir.$entry; 00324 if (($entry=='.') || ($entry =='..')) { 00325 continue; 00326 } 00327 if (is_dir($curritem)) { 00328 if (!( is_array($excludedirs) && in_array($entry,$excludedirs) )) { 00329 getRawFiles2($startDir.$entry."/",$arr,$rootDir.$entry."/",$excludedirs,$excludefileext); 00330 } 00331 continue; 00332 } 00333 if (is_file($curritem)){ 00334 $pinfo = pathinfo($entry); 00335 $ext = array_key_exists('extension',$pinfo) ? $pinfo['extension'] : ''; 00336 if (!is_array($excludefileext) || 00337 (is_array($excludefileext) && !in_array($ext,$excludefileext))) { 00338 array_push($arr,$rootDir.$entry); 00339 // fwrite($fhandle,$rootDir.$entry."\n"); 00340 } 00341 } 00342 } 00343 } 00344 return $result; 00345 } 00346 00347 00348 function GetFiles($startDir, $outfile, $rootDir='', $excludedirs = null, $excludefileext = null) { 00349 $fh = @fopen($outfile,"w+"); 00350 if ($fh !== FALSE) { 00351 getRawFiles($startDir,$fh,$rootDir,$excludedirs,$excludefileext); 00352 @fclose($fh); 00353 @chmod($outfile,0777); 00354 } 00355 } 00356 00357 00368 function GetFilesArray($startDir, $rootDir='', $excludedirs = null, $excludefileext = null) { 00369 $arr = array(); 00370 getRawFiles2($startDir,$arr,$rootDir,$excludedirs,$excludefileext); 00371 return $arr; 00372 } 00373 00374 00375 00386 function getCourseDirs ($rootDir, $contains, $excludeitems=null, $startswith=true) { 00387 $result = getDirectories($rootDir,$contains,$excludeitems,$startswith); 00388 if ($result !== false) { 00389 natcasesort($result); 00390 $result = array_values($result); 00391 } 00392 return $result; 00393 } 00394 00395 00402 function rmdirr($dirname) 00403 { 00404 if (!file_exists($dirname)) { 00405 return false; 00406 } 00407 if (is_file($dirname) || is_link($dirname)) { 00408 return unlink($dirname); 00409 } 00410 $dir = dir($dirname); 00411 while (false !== $entry = $dir->read()) { 00412 if ($entry == '.' || $entry == '..') { 00413 continue; 00414 } 00415 rmdirr($dirname . DIRECTORY_SEPARATOR . $entry); 00416 } 00417 $dir->close(); 00418 return rmdir($dirname); 00419 }