File modules/util/FileUtils.class.php

Last commit: Sat Mar 6 20:39:30 2021 +0100	Jan Dankert	Fix: Sorting the files, because the order should be identically on all platforms.
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 }
Download modules/util/FileUtils.class.php
History Sat, 6 Mar 2021 20:39:30 +0100 Jan Dankert Fix: Sorting the files, because the order should be identically on all platforms. Fri, 26 Feb 2021 17:09:29 +0100 Jan Dankert Fix: Disable warning if openbasedir-restriction is enabled. Fri, 8 Jan 2021 22:01:28 +0100 Jan Dankert New: Implementing cache directory tag standard (CACHEDIR.TAG) and using a cleaner logic to detect the directory for temporary files. Thu, 19 Nov 2020 19:51:26 +0100 Jan Dankert Fix: Using a stream for log output (like php://stdout) Fri, 23 Oct 2020 23:09:52 +0200 Jan Dankert Refactoring: Using the new config classes. Sun, 4 Oct 2020 21:24:40 +0200 Jan Dankert Fix: Throw correct DatabaseException Sat, 26 Sep 2020 10:32:02 +0200 Jan Dankert Refactoring: No global $conf array any more. Sat, 22 Feb 2020 23:58:02 +0100 Jan Dankert Refactoring: Namespacing for module 'util'. Fri, 22 Nov 2019 00:35:51 +0100 Jan Dankert New: More helper function for filesystem utils. Sat, 11 May 2019 02:50:43 +0200 Jan Dankert Einbau eines dedizierten Caches. Es ist fraglich, ob dieser Cache überhaupt sinnvoll ist, da beim Anzeigen von BLOBs das HTTP-Caching zum Zuge kommt. Thu, 30 Aug 2018 00:34:00 +0200 Jan Dankert Fix: Anzeige der Berechtigungen vereinheitlicht, weil alle Objekttypen die gleichen Berechtigungseinstellungen haben; Fix: Die 2. Action nach einem POST wird als GET ausgeführt. Wed, 27 Jun 2018 22:02:09 +0200 Jan Dankert Exceptions werfen, wenn ein Ordner nicht gefunden wird. Das ist besser als ein die(). Sat, 16 Dec 2017 23:21:31 +0100 Jan Dankert Eigenes Modul für alle Util-Klassen.