blob: 0c2ae5af6e6ec342bb9b5f0be53b5059b33b3430 [file] [log] [blame]
Derek Allardd2df9bc2007-04-15 17:41:17 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allardd2df9bc2007-04-15 17:41:17 +000012 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter File Helpers
20 *
21 * @package CodeIgniter
22 * @subpackage Helpers
23 * @category Helpers
Derek Allard3d879d52008-01-18 19:41:32 +000024 * @author ExpressionEngine Dev Team
Derek Jonesfd93d222008-05-06 15:18:50 +000025 * @link http://codeigniter.com/user_guide/helpers/file_helper.html
Derek Allardd2df9bc2007-04-15 17:41:17 +000026 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Read File
32 *
33 * Opens the file specfied in the path and returns it as a string.
34 *
35 * @access public
36 * @param string path to file
37 * @return string
38 */
Derek Jones269b9422008-01-28 21:00:20 +000039if (! function_exists('read_file'))
Derek Allardd2df9bc2007-04-15 17:41:17 +000040{
Derek Jones269b9422008-01-28 21:00:20 +000041 function read_file($file)
Derek Allardd2df9bc2007-04-15 17:41:17 +000042 {
Derek Allard73274992008-05-05 16:39:18 +000043 if (! file_exists($file))
Derek Jones269b9422008-01-28 21:00:20 +000044 {
45 return FALSE;
46 }
Derek Allardd2df9bc2007-04-15 17:41:17 +000047
Derek Jones269b9422008-01-28 21:00:20 +000048 if (function_exists('file_get_contents'))
49 {
50 return file_get_contents($file);
51 }
Derek Allardd2df9bc2007-04-15 17:41:17 +000052
Derek Jones3be20e22008-05-05 20:07:09 +000053 if (! $fp = @fopen($file, FOPEN_READ))
Derek Jones269b9422008-01-28 21:00:20 +000054 {
55 return FALSE;
56 }
Derek Allardd2df9bc2007-04-15 17:41:17 +000057
Derek Jones269b9422008-01-28 21:00:20 +000058 flock($fp, LOCK_SH);
Derek Allardd2df9bc2007-04-15 17:41:17 +000059
Derek Jones269b9422008-01-28 21:00:20 +000060 $data = '';
61 if (filesize($file) > 0)
62 {
63 $data =& fread($fp, filesize($file));
64 }
65
66 flock($fp, LOCK_UN);
67 fclose($fp);
68
69 return $data;
Derek Allardd2df9bc2007-04-15 17:41:17 +000070 }
Derek Allardd2df9bc2007-04-15 17:41:17 +000071}
72
73// ------------------------------------------------------------------------
74
75/**
76 * Write File
77 *
78 * Writes data to the file specified in the path.
79 * Creates a new file if non-existent.
80 *
81 * @access public
82 * @param string path to file
83 * @param string file data
84 * @return bool
85 */
Derek Jones269b9422008-01-28 21:00:20 +000086if (! function_exists('write_file'))
Derek Allardd2df9bc2007-04-15 17:41:17 +000087{
Derek Jones3be20e22008-05-05 20:07:09 +000088 function write_file($path, $data, $mode = FOPEN_WRITE_CREATE_DESTRUCTIVE)
Derek Allardd2df9bc2007-04-15 17:41:17 +000089 {
Derek Allard73274992008-05-05 16:39:18 +000090 if (! $fp = @fopen($path, $mode))
Derek Jones269b9422008-01-28 21:00:20 +000091 {
92 return FALSE;
93 }
Derek Allardd2df9bc2007-04-15 17:41:17 +000094
Derek Jones269b9422008-01-28 21:00:20 +000095 flock($fp, LOCK_EX);
96 fwrite($fp, $data);
97 flock($fp, LOCK_UN);
98 fclose($fp);
Derek Allardd2df9bc2007-04-15 17:41:17 +000099
Derek Jones269b9422008-01-28 21:00:20 +0000100 return TRUE;
101 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000102}
103
104// ------------------------------------------------------------------------
105
106/**
107 * Delete Files
108 *
109 * Deletes all files contained in the supplied directory path.
110 * Files must be writable or owned by the system in order to be deleted.
111 * If the second parameter is set to TRUE, any directories contained
112 * within the supplied base directory will be nuked as well.
113 *
114 * @access public
115 * @param string path to file
116 * @param bool whether to delete any directories found in the path
117 * @return bool
118 */
Derek Jones269b9422008-01-28 21:00:20 +0000119if (! function_exists('delete_files'))
120{
121 function delete_files($path, $del_dir = FALSE, $level = 0)
122 {
123 // Trim the trailing slash
124 $path = preg_replace("|^(.+?)/*$|", "\\1", $path);
Derek Jones1c8dd3a2008-01-29 20:20:00 +0000125
Derek Allard73274992008-05-05 16:39:18 +0000126 if (! $current_dir = @opendir($path))
Derek Jones269b9422008-01-28 21:00:20 +0000127 return;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000128
Derek Jones269b9422008-01-28 21:00:20 +0000129 while(FALSE !== ($filename = @readdir($current_dir)))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000130 {
Derek Jones269b9422008-01-28 21:00:20 +0000131 if ($filename != "." and $filename != "..")
Derek Allardd2df9bc2007-04-15 17:41:17 +0000132 {
Derek Jones269b9422008-01-28 21:00:20 +0000133 if (is_dir($path.'/'.$filename))
134 {
Derek Jones1c8dd3a2008-01-29 20:20:00 +0000135 delete_files($path.'/'.$filename, $del_dir, $level + 1);
Derek Jones269b9422008-01-28 21:00:20 +0000136 }
137 else
138 {
139 unlink($path.'/'.$filename);
140 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000141 }
142 }
Derek Jones269b9422008-01-28 21:00:20 +0000143 @closedir($current_dir);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000144
Derek Jones269b9422008-01-28 21:00:20 +0000145 if ($del_dir == TRUE AND $level > 0)
146 {
147 @rmdir($path);
148 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000149 }
150}
151
152// ------------------------------------------------------------------------
153
154/**
155 * Get Filenames
156 *
157 * Reads the specified directory and builds an array containing the filenames.
158 * Any sub-folders contained within the specified path are read as well.
159 *
160 * @access public
161 * @param string path to source
162 * @param bool whether to include the path as part of the filename
Derek Jones560efb32008-03-27 16:07:00 +0000163 * @param bool internal variable to determine recursion status - do not use in calls
Derek Allardd2df9bc2007-04-15 17:41:17 +0000164 * @return array
165 */
Derek Jones269b9422008-01-28 21:00:20 +0000166if (! function_exists('get_filenames'))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000167{
Derek Jones560efb32008-03-27 16:07:00 +0000168 function get_filenames($source_dir, $include_path = FALSE, $_recursion = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000169 {
Derek Jones14031d12008-05-05 18:29:43 +0000170 static $_filedata = array();
Derek Jonesf00992d2008-03-28 14:13:14 +0000171
Derek Jones269b9422008-01-28 21:00:20 +0000172 if ($fp = @opendir($source_dir))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000173 {
Derek Jonesf00992d2008-03-28 14:13:14 +0000174 // reset the array and make sure $source_dir has a trailing slash on the initial call
175 if ($_recursion === FALSE)
176 {
177 $_filedata = array();
Derek Jonesac7c81e2008-04-01 13:59:49 +0000178 $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
Derek Jonesf00992d2008-03-28 14:13:14 +0000179 }
180
Derek Jones269b9422008-01-28 21:00:20 +0000181 while (FALSE !== ($file = readdir($fp)))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000182 {
Derek Allard73274992008-05-05 16:39:18 +0000183 if (@is_dir($source_dir.$file) && strncmp($file, '.', 1) !== 0)
Derek Jones269b9422008-01-28 21:00:20 +0000184 {
Derek Jonesac7c81e2008-04-01 13:59:49 +0000185 get_filenames($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE);
Derek Jones269b9422008-01-28 21:00:20 +0000186 }
Derek Allard73274992008-05-05 16:39:18 +0000187 elseif (strncmp($file, '.', 1) !== 0)
Derek Jones269b9422008-01-28 21:00:20 +0000188 {
Derek Allardd2df9bc2007-04-15 17:41:17 +0000189
Derek Jones269b9422008-01-28 21:00:20 +0000190 $_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file;
191 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000192 }
Derek Jones269b9422008-01-28 21:00:20 +0000193 return $_filedata;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000194 }
Derek Jonesfd971782008-02-20 18:39:18 +0000195 else
196 {
Derek Allardbca400f2008-02-21 22:19:55 +0000197 return FALSE;
Derek Jonesfd971782008-02-20 18:39:18 +0000198 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000199 }
200}
201
Derek Allarde4e603b2008-01-09 14:18:14 +0000202// --------------------------------------------------------------------
203
Derek Allard73274992008-05-05 16:39:18 +0000204/**
205 * Get Directory File Information
206 *
207 * Reads the specified directory and builds an array containing the filenames,
208 * filesize, dates, and permissions
209 *
210 * Any sub-folders contained within the specified path are read as well.
211 *
212 * @access public
213 * @param string path to source
214 * @param bool whether to include the path as part of the filename
215 * @param bool internal variable to determine recursion status - do not use in calls
216 * @return array
217 */
218if (! function_exists('get_dir_file_info'))
219{
220 function get_dir_file_info($source_dir, $include_path = FALSE, $_recursion = FALSE)
221 {
222 $_filedata = array();
223 $relative_path = $source_dir;
224
225 if ($fp = @opendir($source_dir))
226 {
227 // reset the array and make sure $source_dir has a trailing slash on the initial call
228 if ($_recursion === FALSE)
229 {
230 $_filedata = array();
231 $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
232 }
233
234 while (FALSE !== ($file = readdir($fp)))
235 {
236 if (@is_dir($source_dir.$file) && strncmp($file, '.', 1) !== 0)
237 {
238 get_dir_file_info($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE);
239 }
240 elseif (strncmp($file, '.', 1) !== 0)
241 {
242 $_filedata[$file] = get_file_info($source_dir.$file);
243 $_filedata[$file]['relative_path'] = $relative_path;
244 }
245 }
246 return $_filedata;
247 }
248 else
249 {
250 return FALSE;
251 }
252 }
253}
254
255// --------------------------------------------------------------------
256
257/**
258* Get File Info
259*
260* Given a file and path, returns the name, path, size, date modified
261* Second parameter allows you to explicitly declare what information you want returned
262* Options are: name, server_path, size, date, readable, writable, executable, fileperms
263* Returns FALSE if the file cannot be found.
264*
265* @access public
266* @param string path to file
267* @param mixed array or comma separated string of information returned
268* @return array
269*/
270if (! function_exists('get_file_info'))
271{
272 function get_file_info($file, $returned_values = array('name', 'server_path', 'size', 'date'))
273 {
274
275 if (! file_exists($file))
276 {
277 return FALSE;
278 }
279
280 if (is_string($returned_values))
281 {
282 $returned_values = explode(',', $returned_values);
283 }
284
285 foreach ($returned_values as $key)
286 {
287 switch ($key)
288 {
289 case 'name':
290 $fileinfo['name'] = substr(strrchr($file, '/'), 1);
291 break;
292 case 'server_path':
293 $fileinfo['server_path'] = $file;
294 break;
295 case 'size':
296 $fileinfo['size'] = filesize($file);
297 break;
298 case 'date':
299 $fileinfo['date'] = filectime($file);
300 break;
301 case 'readable':
302 $fileinfo['readable'] = is_readable($file);
303 break;
304 case 'writable':
305 // There are known problems using is_weritable on IIS. It may not be reliable - consider fileperms()
306 $fileinfo['writable'] = is_writable($file);
307 break;
308 case 'executable':
309 $fileinfo['executable'] = is_executable($file);
310 break;
311 case 'fileperms':
312 $fileinfo['fileperms'] = fileperms($file);
313 break;
314 }
315 }
316
317 return $fileinfo;
318 }
319}
320
321// --------------------------------------------------------------------
322
323/**
324 * Get Mime by Extension
325 *
326 * Translates a file extension into a mime type based on config/mimes.php.
327 * Returns FALSE if it can't determine the type, or open the mime config file
328 *
329 * Note: this is NOT an accurate way of determining file mime types, and is here strictly as a convenience
330 * It should NOT be trusted, and should certainly NOT be used for security
331 *
332 * @access public
333 * @param string path to file
334 * @return mixed
335 */
336if (! function_exists('get_mime_by_extension'))
337{
338 function get_mime_by_extension($file)
339 {
340 $extension = substr(strrchr($file, '.'), 1);
341
342 global $mimes;
343
344 if (! is_array($mimes))
345 {
346 if (! require_once(APPPATH.'config/mimes.php'))
347 {
348 return FALSE;
349 }
350 }
351
352 if (array_key_exists($extension, $mimes))
353 {
354 if (is_array($mimes[$extension]))
355 {
356 // Multiple mime types, just give the first one
357 return current($mimes[$extension]);
358 }
359 else
360 {
361 return $mimes[$extension];
362 }
363 }
364 else
365 {
366 return FALSE;
367 }
368 }
369}
370
Derek Jones94a21822008-05-08 15:00:28 +0000371// --------------------------------------------------------------------
372
373/**
374 * Symbolic Permissions
375 *
376 * Takes a numeric value representing a file's permissions and returns
377 * standard symbolic notation representing that value
378 *
379 * @access public
380 * @param int
381 * @return string
382 */
383if (! function_exists('symbolic_permissions'))
384{
385 function symbolic_permissions($perms)
386 {
387 if (($perms & 0xC000) == 0xC000)
388 {
389 $symbolic = 's'; // Socket
390 }
391 elseif (($perms & 0xA000) == 0xA000)
392 {
393 $symbolic = 'l'; // Symbolic Link
394 }
395 elseif (($perms & 0x8000) == 0x8000)
396 {
397 $symbolic = '-'; // Regular
398 }
399 elseif (($perms & 0x6000) == 0x6000)
400 {
401 $symbolic = 'b'; // Block special
402 }
403 elseif (($perms & 0x4000) == 0x4000)
404 {
405 $symbolic = 'd'; // Directory
406 }
407 elseif (($perms & 0x2000) == 0x2000)
408 {
409 $symbolic = 'c'; // Character special
410 }
411 elseif (($perms & 0x1000) == 0x1000)
412 {
413 $symbolic = 'p'; // FIFO pipe
414 }
415 else
416 {
417 $symbolic = 'u'; // Unknown
418 }
419
420 // Owner
421 $symbolic .= (($perms & 0x0100) ? 'r' : '-');
422 $symbolic .= (($perms & 0x0080) ? 'w' : '-');
423 $symbolic .= (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x' ) : (($perms & 0x0800) ? 'S' : '-'));
424
425 // Group
426 $symbolic .= (($perms & 0x0020) ? 'r' : '-');
427 $symbolic .= (($perms & 0x0010) ? 'w' : '-');
428 $symbolic .= (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x' ) : (($perms & 0x0400) ? 'S' : '-'));
429
430 // World
431 $symbolic .= (($perms & 0x0004) ? 'r' : '-');
432 $symbolic .= (($perms & 0x0002) ? 'w' : '-');
433 $symbolic .= (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x' ) : (($perms & 0x0200) ? 'T' : '-'));
434
435 return $symbolic;
436 }
437}
438
439// --------------------------------------------------------------------
440
441/**
442 * Octal Permissions
443 *
444 * Takes a numeric value representing a file's permissions and returns
445 * a three character string representing the file's octal permissions
446 *
447 * @access public
448 * @param int
449 * @return string
450 */
451if (! function_exists('octal_permissions'))
452{
453 function octal_permissions($perms)
454 {
455 return substr(sprintf('%o', $perms), -3);
456 }
457}
458