1 <?php
  2 
  3 /**
  4  * Class for handling files.
  5  *
  6  * @package redaxo\core
  7  */
  8 class rex_file
  9 {
 10     /**
 11      * Returns the content of a file.
 12      *
 13      * @param string $file    Path to the file
 14      * @param mixed  $default Default value
 15      *
 16      * @return mixed Content of the file or default value if the file isn't readable
 17      */
 18     public static function get($file, $default = null)
 19     {
 20         return rex_timer::measure(__METHOD__, function () use ($file, $default) {
 21             $content = @file_get_contents($file);
 22             return $content !== false ? $content : $default;
 23         });
 24     }
 25 
 26     /**
 27      * Returns the content of a config file.
 28      *
 29      * @param string $file    Path to the file
 30      * @param mixed  $default Default value
 31      *
 32      * @return mixed Content of the file or default value if the file isn't readable
 33      */
 34     public static function getConfig($file, $default = [])
 35     {
 36         $content = self::get($file);
 37         return $content === null ? $default : rex_string::yamlDecode($content);
 38     }
 39 
 40     /**
 41      * Returns the content of a cache file.
 42      *
 43      * @param string $file    Path to the file
 44      * @param mixed  $default Default value
 45      *
 46      * @return mixed Content of the file or default value if the file isn't readable
 47      */
 48     public static function getCache($file, $default = [])
 49     {
 50         $content = self::get($file);
 51         return $content === null ? $default : json_decode($content, true);
 52     }
 53 
 54     /**
 55      * Puts content in a file.
 56      *
 57      * @param string $file    Path to the file
 58      * @param string $content Content for the file
 59      *
 60      * @return bool TRUE on success, FALSE on failure
 61      */
 62     public static function put($file, $content)
 63     {
 64         return rex_timer::measure(__METHOD__, function () use ($file, $content) {
 65             if (!rex_dir::create(dirname($file)) || file_exists($file) && !is_writable($file)) {
 66                 return false;
 67             }
 68 
 69             // mimic a atomic write
 70             $tmpFile = @tempnam(dirname($file), basename($file));
 71             if (file_put_contents($tmpFile, $content) !== false && rename($tmpFile, $file)) {
 72                 @chmod($file, rex::getFilePerm());
 73                 return true;
 74             }
 75             @unlink($tmpFile);
 76 
 77             return false;
 78         });
 79     }
 80 
 81     /**
 82      * Puts content in a config file.
 83      *
 84      * @param string $file    Path to the file
 85      * @param mixed  $content Content for the file
 86      * @param int    $inline  The level where you switch to inline YAML
 87      *
 88      * @return bool TRUE on success, FALSE on failure
 89      */
 90     public static function putConfig($file, $content, $inline = 3)
 91     {
 92         return self::put($file, rex_string::yamlEncode($content, $inline));
 93     }
 94 
 95     /**
 96      * Puts content in a cache file.
 97      *
 98      * @param string $file    Path to the file
 99      * @param mixed  $content Content for the file
100      *
101      * @return bool TRUE on success, FALSE on failure
102      */
103     public static function putCache($file, $content)
104     {
105         return self::put($file, json_encode($content));
106     }
107 
108     /**
109      * Copies a file.
110      *
111      * @param string $srcfile Path of the source file
112      * @param string $dstfile Path of the destination file or directory
113      *
114      * @return bool TRUE on success, FALSE on failure
115      */
116     public static function copy($srcfile, $dstfile)
117     {
118         return rex_timer::measure(__METHOD__, function () use ($srcfile, $dstfile) {
119             if (is_file($srcfile)) {
120                 if (is_dir($dstfile)) {
121                     $dstdir = rtrim($dstfile, DIRECTORY_SEPARATOR);
122                     $dstfile = $dstdir . DIRECTORY_SEPARATOR . basename($srcfile);
123                 } else {
124                     $dstdir = dirname($dstfile);
125                     rex_dir::create($dstdir);
126                 }
127 
128                 if (rex_dir::isWritable($dstdir) && (!file_exists($dstfile) || is_writable($dstfile)) && copy($srcfile, $dstfile)) {
129                     @chmod($dstfile, rex::getFilePerm());
130                     touch($dstfile, filemtime($srcfile), fileatime($srcfile));
131                     return true;
132                 }
133             }
134             return false;
135         });
136     }
137 
138     /**
139      * Deletes a file.
140      *
141      * @param string $file Path of the file
142      *
143      * @return bool TRUE on success, FALSE on failure
144      */
145     public static function delete($file)
146     {
147         return rex_timer::measure(__METHOD__, function () use ($file) {
148             if (file_exists($file)) {
149                 return unlink($file);
150             }
151             return true;
152         });
153     }
154 
155     /**
156      * Extracts the extension of the given filename.
157      *
158      * @param string $filename Filename
159      *
160      * @return string Extension of $filename
161      */
162     public static function extension($filename)
163     {
164         return pathinfo($filename, PATHINFO_EXTENSION);
165     }
166 
167     /**
168      * Formates the filesize of the given file into a userfriendly form.
169      *
170      * @param string $file   Path to the file
171      * @param array  $format
172      *
173      * @return string Formatted filesize
174      */
175     public static function formattedSize($file, $format = [])
176     {
177         return rex_formatter::bytes(filesize($file), $format);
178     }
179 
180     /**
181      * Gets executed content of given file.
182      *
183      * @param string $file Path of the file
184      *
185      * @return string executed Content
186      */
187     public static function getOutput($file)
188     {
189         ob_start();
190         require $file;
191         return ob_get_clean();
192     }
193 }
194