00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 class aliroFileManager {
00050
00051 private static $instance = __CLASS__;
00052
00053 private function __construct () {
00054
00055 }
00056
00057 private function __clone () {
00058
00059 }
00060
00061 public static function getInstance () {
00062 return is_object(self::$instance) ? self::$instance : (self::$instance = new self::$instance());
00063 }
00064
00065 function deleteFile ($file) {
00066 if (file_exists($file)) {
00067 @chmod($file, 0644);
00068 return unlink($file);
00069 }
00070 return true;
00071 }
00072
00073 function deleteDirectory ($dir) {
00074 if (file_exists($dir)) {
00075 if (is_dir($dir)) {
00076 @chmod($dir, 0755);
00077 return rmdir($dir);
00078 }
00079 return false;
00080 }
00081 return true;
00082 }
00083
00084 function setPermissions ($fileSysObject, $mode=null) {
00085 $result = true;
00086 if (file_exists($fileSysObject)) {
00087 if ($mode);
00088 elseif (is_dir($fileSysObject)) $mode = octdec(aliroCore::get('mosConfig_dirperms'));
00089 else $mode = octdec(aliroCore::get('mosConfig_fileperms'));
00090 if ($mode) {
00091 $origmask = @umask(0);
00092 $result = @chmod($fileSysObject, $mode);
00093 @umask($origmask);
00094 }
00095 }
00096 return $result;
00097 }
00098
00099 function makeDirectory ($dir) {
00100 $result = @mkdir($dir, 0755);
00101 if ($result) $this->setPermissions($dir);
00102 return $result;
00103 }
00104
00105 public function createDirectory ($dir, $onlyCheck=false) {
00106 if (file_exists($dir)) {
00107 if (is_dir($dir) AND is_writable($dir)) return true;
00108 else return false;
00109 }
00110 list($upDirectory, $count) = $this->containingDirectory($dir);
00111 if ($count > 1 AND !file_exists($upDirectory) AND !($result = $this->createDirectory($upDirectory, $onlyCheck))) return false;
00112 if ($onlyCheck AND isset($result)) return true;
00113 if (!is_dir($upDirectory) OR !is_writable($upDirectory)) return false;
00114 if ($onlyCheck) return true;
00115 else return $this->makeDirectory($dir);
00116 }
00117
00118 private function containingDirectory ($dir) {
00119 $dirs = preg_split('*[/|\\\]*', $dir);
00120 for ($i = count($dirs)-1; $i >= 0; $i--) {
00121 $text = trim($dirs[$i]);
00122 unset($dirs[$i]);
00123 if ($text) break;
00124 }
00125 $result2 = count($dirs);
00126 $result1 = implode('/',$dirs).($result2 > 1 ? '' : '/');
00127 return array($result1, $result2);
00128 }
00129
00130 function simpleCopy ($from, $to) {
00131 if (@copy($from, $to)) {
00132 $this->setPermissions($to);
00133 return true;
00134 }
00135 else return false;
00136 }
00137
00138 function forceCopy ($from, $to, $reportErrors=false) {
00139 if (!file_exists($from)) {
00140 if ($reportErrors) aliroRequest::getInstance()->setErrorMessage (sprintf(T_('Copy requested for %s, but source file not found'), $from), _ALIRO_ERROR_WARN);
00141 return false;
00142 }
00143 $todir = dirname($to);
00144 if (!file_exists($todir)) $this->createDirectory($todir);
00145 if (!file_exists($todir)) {
00146 if ($reportErrors) aliroRequest::getInstance()->setErrorMessage (sprintf(T_('Copy requested for %s, but could not create destination directory'), $from), _ALIRO_ERROR_WARN);
00147 return false;
00148 }
00149 $name = basename($from);
00150 $this->deleteFile($to.$name);
00151 if ($this->simpleCopy ($from, $to)) return true;
00152 elseif ($reportErrors) aliroRequest::getInstance()->setErrorMessage (sprintf(T_('Copy requested for %s, source found but could not copy to %s'), $from, $to), _ALIRO_ERROR_WARN);
00153 return false;
00154 }
00155
00156 function lightCopy ($from, $to) {
00157 $name = basename($from);
00158 if (file_exists($to.$name)) return false;
00159 $todir = dirname($to);
00160 if (!file_exists($todir)) $this->createDirectory($todir);
00161 if (!file_exists($todir)) return false;
00162 return $this->simpleCopy ($from, $to);
00163 }
00164
00165 function acceptCopy ($to) {
00166 $todir = dirname($to);
00167 return $this->createDirectory($todir, true);
00168 }
00169
00170
00171
00172
00173 function mosPathName($p_path, $p_addtrailingslash=true) {
00174 if (substr(PHP_OS, 0, 3) == 'WIN') {
00175 $retval = str_replace( '/', '\\', $p_path );
00176 if ($p_addtrailingslash AND substr( $retval, -1 ) != '\\') $retval .= '\\';
00177
00178 $retval = str_replace( '\\\\', '\\', $retval );
00179 }
00180 else {
00181 $retval = str_replace( '\\', '/', $p_path );
00182 if ($p_addtrailingslash AND substr( $retval, -1 ) != '/') $retval .= '/';
00183
00184 $retval = str_replace('//','/',$retval);
00185 }
00186 return $retval;
00187 }
00188
00196 function mosChmod($path)
00197 {
00198 $filemode = octdec(aliroCore::get('mosConfig_fileperms'));
00199 $dirmode = octdec(aliroCore::get('mosConfig_dirperms'));
00200 if ($filemode OR $dirmode) return $this->mosChmodRecursive($path, $filemode, $dirmode);
00201 return true;
00202 }
00203
00211 function mosChmodRecursive($path, $filemode=NULL, $dirmode=NULL) {
00212 $ret = true;
00213 if (is_dir($path)) {
00214 $topdir = new aliroDirectory($path);
00215 $files = $topdir->listFiles ('', 'file', true, true);
00216 $dirs = $topdir->listFiles ('', 'dir', true, true);
00217 }
00218 else {
00219 $files = array($path);
00220 $dirs = array();
00221 }
00222 if (isset($filemode)) foreach ($files as $file) $ret = @chmod($file, $filemode) ? $ret : false;
00223 if (isset($dirmode)) foreach ($dirs as $dir) $ret = @chmod($dir, $dirmode) ? $ret : false;
00224 return $ret;
00225 }
00226
00227 public function makeUploadSafe ($name, $useRealName=false) {
00228 $tempdir = $this->makeTemp();
00229 $files = array();
00230 $tempfiles = (array) $_FILES[$name]['tmp_name'];
00231 $filenames = (array) $_FILES[$name]['name'];
00232 foreach ($tempfiles as $key=>$temp) {
00233 if ($useRealName) $filename = $filenames[$key];
00234 else $filename = basename($temp);
00235 if (move_uploaded_file($temp, $tempdir.$filename)) {
00236 $files[$filenames[$key]] = $tempdir.$filename;
00237 $this->setPermissions($tempdir.$filename);
00238 }
00239 }
00240 return $files;
00241 }
00242
00243 public function extractArchive ($filename, $extractDir='') {
00244 if (!$extractDir) $extractDir = $this->makeTemp();
00245 if (preg_match( '/\.zip$/i', $filename )) {
00246 $zipfile = new PclZip( $filename );
00247 $ret = $zipfile->extract( PCLZIP_OPT_PATH, $extractDir );
00248 if($ret == 0) {
00249 aliroRequest::getInstance()->setErrorMessage (sprintf(T_('Installer unrecoverable ZIP error %s in %s'), $zipfile->errorName(true), $filename), _ALIRO_ERROR_FATAL);
00250 return false;
00251 }
00252 } else {
00253 error_reporting(E_ALL);
00254 $archive = new Archive_Tar( $filename );
00255 $archive->setErrorHandling( PEAR_ERROR_PRINT );
00256
00257 if (!$archive->extractModify( $extractDir, '' )) {
00258 aliroRequest::getInstance()->setErrorMessage (sprintf(T_('Installer unrecoverable TAR error in %s'), $filename), _ALIRO_ERROR_FATAL);
00259 return false;
00260 }
00261 error_reporting(E_ALL|E_STRICT);
00262 }
00263 $this->mosChmodRecursive($extractDir);
00264 return $extractDir;
00265 }
00266
00267 public function makeTemp () {
00268 $tempDir = criticalInfo::getInstance()->class_base.'/media/'.uniqid('_aliro_temp_').'/';
00269 $this->createDirectory($tempDir);
00270 return $tempDir;
00271 }
00272
00273 }
00274
00275 class aliroDirectory {
00276 private $path = '';
00277
00278 public function __construct ($path) {
00279 $path = str_replace('\\', '/', $path);
00280 $this->path = ('/' == substr($path,-1)) ? $path : $path.'/';
00281 }
00282
00283 public function listAll ($type='file', $recurse=false, $fullpath=false) {
00284 $results = array();
00285 if ($dir = @opendir($this->path)) {
00286 while ($file = readdir($dir)) {
00287 if ($file == '.' OR $file == '..') continue;
00288 if (is_dir($this->path.$file)) {
00289 if ($recurse) {
00290 $subdir = new aliroDirectory($this->path.$file);
00291 $results = array_merge($results, $subdir->listAll($type, $recurse, $fullpath));
00292 unset($subdir);
00293 }
00294 if ($type == 'file') continue;
00295 }
00296 elseif ($type == 'dir') continue;
00297 if ($fullpath) $results[] = $this->path.$file;
00298 else $results[] = $file;
00299 }
00300 closedir($dir);
00301 }
00302 return $results;
00303 }
00304
00305 public function soleDir () {
00306 $found = '';
00307 if ($dir = @opendir($this->path)) {
00308 while ($file = readdir($dir)) {
00309 if ($file == '.' OR $file == '..') continue;
00310 if (is_dir($this->path.$file)) {
00311 if ($found) return '';
00312 else $found = $file;
00313 }
00314 else return '';
00315 }
00316 closedir($dir);
00317 }
00318 return $found;
00319 }
00320
00321 public function deleteAll () {
00322 if (!file_exists($this->path)) return;
00323 $subdirs = $this->listAll ('dir', false, true);
00324 foreach ($subdirs as $subdir) {
00325 $subdirectory = new aliroDirectory($subdir);
00326 $subdirectory->deleteAll();
00327 unset($subdirectory);
00328 }
00329 $filemanager = aliroFileManager::getInstance();
00330 $files = $this->listAll ('file', false, true);
00331 foreach ($files as $file) $filemanager->deleteFile($file);
00332 $filemanager->deleteDirectory($this->path);
00333 }
00334
00335 public function createFresh () {
00336 $this->deleteAll();
00337 $filemanager = aliroFileManager::getInstance();
00338 $filemanager->createDirectory($this->path);
00339 return true;
00340 }
00341
00342 public function createIfNeeded () {
00343 if (!file_exists($this->path)) {
00344 $filemanager = aliroFileManager::getInstance();
00345 $filemanager->createDirectory($this->path);
00346 }
00347 }
00348
00349 public function listFiles ($pattern='', $type='file', $recurse=false, $fullpath=false) {
00350 $results = array();
00351 $all = $this->listAll($type, $recurse, $fullpath);
00352 foreach ($all as $file) {
00353 $name = basename($file);
00354 if ($pattern AND !preg_match( "/$pattern/", $name )) continue;
00355 if (($name != 'index.html') AND ($name[0] != '.')) $results[] = $file;
00356 }
00357 return $results;
00358 }
00359
00360 public function getSize () {
00361 $totalsize = 0;
00362 $files = $this->listFiles();
00363 foreach ($files as $file) $totalsize += filesize($this->path.$file);
00364 return $totalsize;
00365 }
00366
00367 }
00368
00369 ?>