FileUtils.class.php (3428B)
1 <?php 2 3 namespace util; 4 use cms\base\Configuration; 5 use LogicException; 6 use Pfad; 7 use RuntimeException; 8 9 /** 10 * Werkzeugklasse f�r Datei-Operationen. 11 * 12 */ 13 class FileUtils 14 { 15 /** 16 * Adds a trailing slash ('/') if not existing. 17 * 18 * @param String $pfad path 19 * @return string path with trailing slash. 20 */ 21 public static function slashify($pfad) 22 { 23 if (substr($pfad, -1, 1) == '/') 24 return $pfad; 25 else 26 return $pfad . '/'; 27 } 28 29 30 /** 31 * Gets a directory for temporary files. 32 */ 33 public static function getTempDir() 34 { 35 $cacheConfig = Configuration::subset('cache'); 36 37 $tmpdirs = [ 38 $cacheConfig->get('tmp_dir',''), // temporary cache dir from configuration 39 sys_get_temp_dir(), 40 // do not try /var/cache here as it is mostly not writable 41 '/var/tmp', // FHS, survives restarts 42 '/tmp', // FHS 43 null 44 ]; 45 46 foreach( $tmpdirs as $tmpdir ) { 47 if ( $tmpdir && @is_dir($tmpdir) && is_writable($tmpdir) ) 48 break; 49 } 50 51 if ( ! $tmpdir ) 52 throw new LogicException('Could not find a directory for temporary files. Hint: You may config this with cache/tmp_dir'); 53 54 $tmpdir = FileUtils::slashify($tmpdir).$cacheConfig->get('name','openrat-cache'); 55 $tmpdir = FileUtils::slashify($tmpdir); 56 57 if ( ! is_dir($tmpdir ) ) { 58 mkdir( $tmpdir ); 59 60 file_put_contents($tmpdir.'CACHEDIR.TAG', <<<CACHETAG 61 Signature: 8a477f597d28d172789f06886806bc55 62 # This file is a cache directory tag created by OpenRat CMS. 63 # For information about cache directory tags, see: 64 # http://www.brynosaurus.com/cachedir/ 65 CACHETAG 66 ); 67 68 } 69 70 return $tmpdir; 71 } 72 73 74 /** 75 * Gets all files from a directory. 76 * 77 * @param $dir string directory to read 78 * @param null $extension only files with this extension (default: all files) 79 * @return array List of all files in this directory 80 */ 81 public static function readDir($dir,$extension=null) 82 { 83 $dir = FileUtils::slashify($dir); 84 $dateien = array(); 85 86 if (!is_dir($dir)) { 87 throw new RuntimeException('not a directory: ' . $dir); 88 } 89 90 if ($dh = opendir($dir)) { 91 while (($verzEintrag = readdir($dh)) !== false) { 92 if (substr($verzEintrag, 0, 1) != '.') { 93 if ( !$extension || substr($verzEintrag,(strlen($extension)+1)*-1) == '.'.$extension ) { 94 $dateien[] = $verzEintrag; 95 } 96 } 97 } 98 closedir($dh); 99 100 // Sorting the files, because the order should be identically on all platforms. 101 sort($dateien ); 102 103 return $dateien; 104 } else { 105 throw new RuntimeException('unable to open directory: ' . $dir); 106 } 107 108 } 109 110 111 /** 112 * Is the given path an absolute path? 113 * 114 * @param $path 115 * @return bool 116 */ 117 public static function isAbsolutePath($path) 118 { 119 return $path && 120 @$path[0] == '/' || // beginning with '/' (absolute file path) 121 substr($path,0,4)=='php:' || // beginning with 'php:' (PHP-Streams) 122 strpos($path,'://') !== FALSE; // containing '://' (URLs) 123 } 124 125 126 public static function toAbsolutePath($pathElements) 127 { 128 $pathElements = array_map(function ($path) { 129 return trim($path, '/'); 130 }, $pathElements); 131 132 return array_reduce($pathElements, function ($path, $item) { 133 return $path . ($item ? '/' . $item : ''); 134 }, ''); 135 } 136 137 138 public static function toRelativePath($pathElements) 139 { 140 return '.' . self::toAbsolutePath($pathElements); 141 } 142 143 144 /** 145 * @param $path 146 * @return bool 147 */ 148 public static function isRelativePath($path ) { 149 return ! self::isAbsolutePath($path); 150 } 151 }