Take two:
[www-register-wizard.git] / helpers / file_helper.php
1 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');\r
2 /**\r
3  * CodeIgniter\r
4  *\r
5  * An open source application development framework for PHP 4.3.2 or newer\r
6  *\r
7  * @package             CodeIgniter\r
8  * @author              ExpressionEngine Dev Team\r
9  * @copyright   Copyright (c) 2008, EllisLab, Inc.\r
10  * @license             http://codeigniter.com/user_guide/license.html\r
11  * @link                http://codeigniter.com\r
12  * @since               Version 1.0\r
13  * @filesource\r
14  */\r
15 \r
16 // ------------------------------------------------------------------------\r
17 \r
18 /**\r
19  * CodeIgniter File Helpers\r
20  *\r
21  * @package             CodeIgniter\r
22  * @subpackage  Helpers\r
23  * @category    Helpers\r
24  * @author              ExpressionEngine Dev Team\r
25  * @link                http://codeigniter.com/user_guide/helpers/file_helper.html\r
26  */\r
27 \r
28 // ------------------------------------------------------------------------\r
29 \r
30 /**\r
31  * Read File\r
32  *\r
33  * Opens the file specfied in the path and returns it as a string.\r
34  *\r
35  * @access      public\r
36  * @param       string  path to file\r
37  * @return      string\r
38  */     \r
39 if ( ! function_exists('read_file'))\r
40 {\r
41         function read_file($file)\r
42         {\r
43                 if ( ! file_exists($file))\r
44                 {\r
45                         return FALSE;\r
46                 }\r
47         \r
48                 if (function_exists('file_get_contents'))\r
49                 {\r
50                         return file_get_contents($file);                \r
51                 }\r
52 \r
53                 if ( ! $fp = @fopen($file, FOPEN_READ))\r
54                 {\r
55                         return FALSE;\r
56                 }\r
57                 \r
58                 flock($fp, LOCK_SH);\r
59         \r
60                 $data = '';\r
61                 if (filesize($file) > 0)\r
62                 {\r
63                         $data =& fread($fp, filesize($file));\r
64                 }\r
65 \r
66                 flock($fp, LOCK_UN);\r
67                 fclose($fp);\r
68 \r
69                 return $data;\r
70         }\r
71 }\r
72         \r
73 // ------------------------------------------------------------------------\r
74 \r
75 /**\r
76  * Write File\r
77  *\r
78  * Writes data to the file specified in the path.\r
79  * Creates a new file if non-existent.\r
80  *\r
81  * @access      public\r
82  * @param       string  path to file\r
83  * @param       string  file data\r
84  * @return      bool\r
85  */     \r
86 if ( ! function_exists('write_file'))\r
87 {\r
88         function write_file($path, $data, $mode = FOPEN_WRITE_CREATE_DESTRUCTIVE)\r
89         {\r
90                 if ( ! $fp = @fopen($path, $mode))\r
91                 {\r
92                         return FALSE;\r
93                 }\r
94                 \r
95                 flock($fp, LOCK_EX);\r
96                 fwrite($fp, $data);\r
97                 flock($fp, LOCK_UN);\r
98                 fclose($fp);    \r
99 \r
100                 return TRUE;\r
101         }\r
102 }\r
103         \r
104 // ------------------------------------------------------------------------\r
105 \r
106 /**\r
107  * Delete Files\r
108  *\r
109  * Deletes all files contained in the supplied directory path.\r
110  * Files must be writable or owned by the system in order to be deleted.\r
111  * If the second parameter is set to TRUE, any directories contained\r
112  * within the supplied base directory will be nuked as well.\r
113  *\r
114  * @access      public\r
115  * @param       string  path to file\r
116  * @param       bool    whether to delete any directories found in the path\r
117  * @return      bool\r
118  */     \r
119 if ( ! function_exists('delete_files'))\r
120 {\r
121         function delete_files($path, $del_dir = FALSE, $level = 0)\r
122         {       \r
123                 // Trim the trailing slash\r
124                 $path = preg_replace("|^(.+?)/*$|", "\\1", $path);\r
125                 \r
126                 if ( ! $current_dir = @opendir($path))\r
127                         return;\r
128         \r
129                 while(FALSE !== ($filename = @readdir($current_dir)))\r
130                 {\r
131                         if ($filename != "." and $filename != "..")\r
132                         {\r
133                                 if (is_dir($path.'/'.$filename))\r
134                                 {\r
135                                         // Ignore empty folders\r
136                                         if (substr($filename, 0, 1) != '.')\r
137                                         {\r
138                                                 delete_files($path.'/'.$filename, $del_dir, $level + 1);\r
139                                         }\r
140                                 }\r
141                                 else\r
142                                 {\r
143                                         unlink($path.'/'.$filename);\r
144                                 }\r
145                         }\r
146                 }\r
147                 @closedir($current_dir);\r
148         \r
149                 if ($del_dir == TRUE AND $level > 0)\r
150                 {\r
151                         @rmdir($path);\r
152                 }\r
153         }\r
154 }\r
155 \r
156 // ------------------------------------------------------------------------\r
157 \r
158 /**\r
159  * Get Filenames\r
160  *\r
161  * Reads the specified directory and builds an array containing the filenames.  \r
162  * Any sub-folders contained within the specified path are read as well.\r
163  *\r
164  * @access      public\r
165  * @param       string  path to source\r
166  * @param       bool    whether to include the path as part of the filename\r
167  * @param       bool    internal variable to determine recursion status - do not use in calls\r
168  * @return      array\r
169  */     \r
170 if ( ! function_exists('get_filenames'))\r
171 {\r
172         function get_filenames($source_dir, $include_path = FALSE, $_recursion = FALSE)\r
173         {\r
174                 static $_filedata = array();\r
175                                 \r
176                 if ($fp = @opendir($source_dir))\r
177                 {\r
178                         // reset the array and make sure $source_dir has a trailing slash on the initial call\r
179                         if ($_recursion === FALSE)\r
180                         {\r
181                                 $_filedata = array();\r
182                                 $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;\r
183                         }\r
184                         \r
185                         while (FALSE !== ($file = readdir($fp)))\r
186                         {\r
187                                 if (@is_dir($source_dir.$file) && strncmp($file, '.', 1) !== 0)\r
188                                 {\r
189                                          get_filenames($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE);\r
190                                 }\r
191                                 elseif (strncmp($file, '.', 1) !== 0)\r
192                                 {\r
193                         \r
194                                         $_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file;\r
195                                 }\r
196                         }\r
197                         return $_filedata;\r
198                 }\r
199                 else\r
200                 {\r
201                         return FALSE;\r
202                 }\r
203         }\r
204 }\r
205 \r
206 // --------------------------------------------------------------------\r
207 \r
208 /**\r
209  * Get Directory File Information\r
210  *\r
211  * Reads the specified directory and builds an array containing the filenames,  \r
212  * filesize, dates, and permissions\r
213  *\r
214  * Any sub-folders contained within the specified path are read as well.\r
215  *\r
216  * @access      public\r
217  * @param       string  path to source\r
218  * @param       bool    whether to include the path as part of the filename\r
219  * @param       bool    internal variable to determine recursion status - do not use in calls\r
220  * @return      array\r
221  */     \r
222 if ( ! function_exists('get_dir_file_info'))\r
223 {\r
224         function get_dir_file_info($source_dir, $include_path = FALSE, $_recursion = FALSE)\r
225         {\r
226                 $_filedata = array();\r
227                 $relative_path = $source_dir;\r
228                                 \r
229                 if ($fp = @opendir($source_dir))\r
230                 {\r
231                         // reset the array and make sure $source_dir has a trailing slash on the initial call\r
232                         if ($_recursion === FALSE)\r
233                         {\r
234                                 $_filedata = array();\r
235                                 $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;\r
236                         }\r
237 \r
238                         while (FALSE !== ($file = readdir($fp)))\r
239                         {\r
240                                 if (@is_dir($source_dir.$file) && strncmp($file, '.', 1) !== 0)\r
241                                 {\r
242                                          get_dir_file_info($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE);\r
243                                 }\r
244                                 elseif (strncmp($file, '.', 1) !== 0)\r
245                                 {\r
246                                         $_filedata[$file] = get_file_info($source_dir.$file);\r
247                                         $_filedata[$file]['relative_path'] = $relative_path;\r
248                                 }\r
249                         }\r
250                         return $_filedata;\r
251                 }\r
252                 else\r
253                 {\r
254                         return FALSE;\r
255                 }\r
256         }\r
257 }\r
258 \r
259 // --------------------------------------------------------------------\r
260 \r
261 /**\r
262 * Get File Info\r
263 *\r
264 * Given a file and path, returns the name, path, size, date modified\r
265 * Second parameter allows you to explicitly declare what information you want returned\r
266 * Options are: name, server_path, size, date, readable, writable, executable, fileperms\r
267 * Returns FALSE if the file cannot be found.\r
268 *\r
269 * @access       public\r
270 * @param        string          path to file\r
271 * @param        mixed           array or comma separated string of information returned\r
272 * @return       array\r
273 */\r
274 if ( ! function_exists('get_file_info'))\r
275 {\r
276         function get_file_info($file, $returned_values = array('name', 'server_path', 'size', 'date'))\r
277         {\r
278 \r
279                 if ( ! file_exists($file))\r
280                 {\r
281                         return FALSE;\r
282                 }\r
283 \r
284                 if (is_string($returned_values))\r
285                 {\r
286                         $returned_values = explode(',', $returned_values);\r
287                 }\r
288 \r
289                 foreach ($returned_values as $key)\r
290                 {\r
291                         switch ($key)\r
292                         {\r
293                                 case 'name':\r
294                                         $fileinfo['name'] = substr(strrchr($file, '/'), 1);\r
295                                         break;\r
296                                 case 'server_path':\r
297                                         $fileinfo['server_path'] = $file;\r
298                                         break;\r
299                                 case 'size':\r
300                                         $fileinfo['size'] = filesize($file);\r
301                                         break;\r
302                                 case 'date':\r
303                                         $fileinfo['date'] = filectime($file);\r
304                                         break;\r
305                                 case 'readable':\r
306                                         $fileinfo['readable'] = is_readable($file);\r
307                                         break;\r
308                                 case 'writable':\r
309                                         // There are known problems using is_weritable on IIS.  It may not be reliable - consider fileperms()\r
310                                         $fileinfo['writable'] = is_writable($file);\r
311                                         break;\r
312                                 case 'executable':\r
313                                         $fileinfo['executable'] = is_executable($file);\r
314                                         break;\r
315                                 case 'fileperms':\r
316                                         $fileinfo['fileperms'] = fileperms($file);\r
317                                         break;\r
318                         }\r
319                 }\r
320 \r
321                 return $fileinfo;\r
322         }\r
323 }\r
324 \r
325 // --------------------------------------------------------------------\r
326 \r
327 /**\r
328  * Get Mime by Extension\r
329  *\r
330  * Translates a file extension into a mime type based on config/mimes.php. \r
331  * Returns FALSE if it can't determine the type, or open the mime config file\r
332  *\r
333  * Note: this is NOT an accurate way of determining file mime types, and is here strictly as a convenience\r
334  * It should NOT be trusted, and should certainly NOT be used for security\r
335  *\r
336  * @access      public\r
337  * @param       string  path to file\r
338  * @return      mixed\r
339  */     \r
340 if ( ! function_exists('get_mime_by_extension'))\r
341 {\r
342         function get_mime_by_extension($file)\r
343         {\r
344                 $extension = substr(strrchr($file, '.'), 1);\r
345         \r
346                 global $mimes;\r
347         \r
348                 if ( ! is_array($mimes))\r
349                 {\r
350                         if ( ! require_once(APPPATH.'config/mimes.php'))\r
351                         {\r
352                                 return FALSE;\r
353                         }\r
354                 }\r
355 \r
356                 if (array_key_exists($extension, $mimes))\r
357                 {\r
358                         if (is_array($mimes[$extension]))\r
359                         {\r
360                                 // Multiple mime types, just give the first one\r
361                                 return current($mimes[$extension]);\r
362                         }\r
363                         else\r
364                         {\r
365                                 return $mimes[$extension];\r
366                         }\r
367                 }\r
368                 else\r
369                 {\r
370                         return FALSE;\r
371                 }\r
372         }\r
373 }\r
374 \r
375 // --------------------------------------------------------------------\r
376 \r
377 /**\r
378  * Symbolic Permissions\r
379  *\r
380  * Takes a numeric value representing a file's permissions and returns\r
381  * standard symbolic notation representing that value\r
382  *\r
383  * @access      public\r
384  * @param       int\r
385  * @return      string\r
386  */     \r
387 if ( ! function_exists('symbolic_permissions'))\r
388 {\r
389         function symbolic_permissions($perms)\r
390         {       \r
391                 if (($perms & 0xC000) == 0xC000)\r
392                 {\r
393                         $symbolic = 's'; // Socket\r
394                 }\r
395                 elseif (($perms & 0xA000) == 0xA000)\r
396                 {\r
397                         $symbolic = 'l'; // Symbolic Link\r
398                 }\r
399                 elseif (($perms & 0x8000) == 0x8000)\r
400                 {\r
401                         $symbolic = '-'; // Regular\r
402                 }\r
403                 elseif (($perms & 0x6000) == 0x6000)\r
404                 {\r
405                         $symbolic = 'b'; // Block special\r
406                 }\r
407                 elseif (($perms & 0x4000) == 0x4000)\r
408                 {\r
409                         $symbolic = 'd'; // Directory\r
410                 }\r
411                 elseif (($perms & 0x2000) == 0x2000)\r
412                 {\r
413                         $symbolic = 'c'; // Character special\r
414                 }\r
415                 elseif (($perms & 0x1000) == 0x1000)\r
416                 {\r
417                         $symbolic = 'p'; // FIFO pipe\r
418                 }\r
419                 else\r
420                 {\r
421                         $symbolic = 'u'; // Unknown\r
422                 }\r
423 \r
424                 // Owner\r
425                 $symbolic .= (($perms & 0x0100) ? 'r' : '-');\r
426                 $symbolic .= (($perms & 0x0080) ? 'w' : '-');\r
427                 $symbolic .= (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x' ) : (($perms & 0x0800) ? 'S' : '-'));\r
428 \r
429                 // Group\r
430                 $symbolic .= (($perms & 0x0020) ? 'r' : '-');\r
431                 $symbolic .= (($perms & 0x0010) ? 'w' : '-');\r
432                 $symbolic .= (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x' ) : (($perms & 0x0400) ? 'S' : '-'));\r
433 \r
434                 // World\r
435                 $symbolic .= (($perms & 0x0004) ? 'r' : '-');\r
436                 $symbolic .= (($perms & 0x0002) ? 'w' : '-');\r
437                 $symbolic .= (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x' ) : (($perms & 0x0200) ? 'T' : '-'));\r
438 \r
439                 return $symbolic;               \r
440         }\r
441 }\r
442 \r
443 // --------------------------------------------------------------------\r
444 \r
445 /**\r
446  * Octal Permissions\r
447  *\r
448  * Takes a numeric value representing a file's permissions and returns\r
449  * a three character string representing the file's octal permissions\r
450  *\r
451  * @access      public\r
452  * @param       int\r
453  * @return      string\r
454  */     \r
455 if ( ! function_exists('octal_permissions'))\r
456 {\r
457         function octal_permissions($perms)\r
458         {\r
459                 return substr(sprintf('%o', $perms), -3);\r
460         }\r
461 }\r
462 \r
463 \r
464 /* End of file file_helper.php */\r
465 /* Location: ./system/helpers/file_helper.php */