blob: 8f23a3d548f297dea9a66f4512124e563d5d58b5 [file] [log] [blame]
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev8bf6bb62012-01-06 16:11:04 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev8bf6bb62012-01-06 16:11:04 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * CodeIgniter File Helpers
30 *
31 * @package CodeIgniter
32 * @subpackage Helpers
33 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
vkeranov50ec6a62012-07-12 11:00:06 +030035 * @link http://codeigniter.com/user_guide/helpers/file_helper.html
Derek Allard2067d1a2008-11-13 22:59:24 +000036 */
37
38// ------------------------------------------------------------------------
39
Derek Allard2067d1a2008-11-13 22:59:24 +000040if ( ! function_exists('read_file'))
41{
Timothy Warren01b129a2012-04-27 11:36:50 -040042 /**
43 * Read File
44 *
45 * Opens the file specfied in the path and returns it as a string.
46 *
Andrey Andreev29d909d2012-10-27 01:05:09 +030047 * @todo Remove in version 3.1+.
48 * @deprecated 3.0.0 It is now just an alias for PHP's native file_get_contents().
49 * @param string $file Path to file
50 * @return string File contents
Timothy Warren01b129a2012-04-27 11:36:50 -040051 */
Derek Allard2067d1a2008-11-13 22:59:24 +000052 function read_file($file)
53 {
Andrey Andreev596c51c2012-06-07 15:51:58 +030054 return @file_get_contents($file);
Derek Allard2067d1a2008-11-13 22:59:24 +000055 }
56}
Barry Mienydd671972010-10-04 16:33:58 +020057
Derek Allard2067d1a2008-11-13 22:59:24 +000058// ------------------------------------------------------------------------
59
Derek Allard2067d1a2008-11-13 22:59:24 +000060if ( ! function_exists('write_file'))
61{
Timothy Warren01b129a2012-04-27 11:36:50 -040062 /**
63 * Write File
64 *
65 * Writes data to the file specified in the path.
66 * Creates a new file if non-existent.
67 *
68 * @param string path to file
69 * @param string file data
70 * @param int
71 * @return bool
72 */
Derek Allard2067d1a2008-11-13 22:59:24 +000073 function write_file($path, $data, $mode = FOPEN_WRITE_CREATE_DESTRUCTIVE)
74 {
75 if ( ! $fp = @fopen($path, $mode))
76 {
77 return FALSE;
78 }
Barry Mienydd671972010-10-04 16:33:58 +020079
Derek Allard2067d1a2008-11-13 22:59:24 +000080 flock($fp, LOCK_EX);
81 fwrite($fp, $data);
82 flock($fp, LOCK_UN);
Barry Mienydd671972010-10-04 16:33:58 +020083 fclose($fp);
Derek Allard2067d1a2008-11-13 22:59:24 +000084
85 return TRUE;
86 }
87}
Barry Mienydd671972010-10-04 16:33:58 +020088
Derek Allard2067d1a2008-11-13 22:59:24 +000089// ------------------------------------------------------------------------
90
Derek Allard2067d1a2008-11-13 22:59:24 +000091if ( ! function_exists('delete_files'))
92{
Timothy Warren01b129a2012-04-27 11:36:50 -040093 /**
94 * Delete Files
95 *
96 * Deletes all files contained in the supplied directory path.
97 * Files must be writable or owned by the system in order to be deleted.
98 * If the second parameter is set to TRUE, any directories contained
99 * within the supplied base directory will be nuked as well.
100 *
101 * @param string path to file
102 * @param bool whether to delete any directories found in the path
103 * @param int
104 * @param bool whether to skip deleting .htaccess and index page files
105 * @return bool
106 */
Andrey Andreevfd6c2bc2012-04-03 16:21:48 +0300107 function delete_files($path, $del_dir = FALSE, $level = 0, $htdocs = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200108 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 // Trim the trailing slash
Andrey Andreev256a18c2012-10-23 12:18:32 +0300110 $path = rtrim($path, '/\\');
Barry Mienydd671972010-10-04 16:33:58 +0200111
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 if ( ! $current_dir = @opendir($path))
Greg Aker3e9519e2010-01-15 00:09:34 +0000113 {
Barry Mienydd671972010-10-04 16:33:58 +0200114 return FALSE;
Greg Aker3e9519e2010-01-15 00:09:34 +0000115 }
Barry Mienydd671972010-10-04 16:33:58 +0200116
Pascal Kriete45e3cdf2011-02-14 13:26:20 -0500117 while (FALSE !== ($filename = @readdir($current_dir)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 {
Andrey Andreevb2518642012-03-26 21:44:08 +0300119 if ($filename !== '.' && $filename !== '..')
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200121 if (is_dir($path.DIRECTORY_SEPARATOR.$filename) && $filename[0] !== '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 {
Andrey Andreevfd6c2bc2012-04-03 16:21:48 +0300123 delete_files($path.DIRECTORY_SEPARATOR.$filename, $del_dir, $level + 1, $htdocs);
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 }
Andrey Andreev27228c92012-07-27 10:36:29 +0300125 elseif ($htdocs !== TRUE OR ! preg_match('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i', $filename))
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 {
Andrey Andreevb2518642012-03-26 21:44:08 +0300127 @unlink($path.DIRECTORY_SEPARATOR.$filename);
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 }
129 }
130 }
131 @closedir($current_dir);
Barry Mienydd671972010-10-04 16:33:58 +0200132
Alex Bilbie773ccc32012-06-02 11:11:08 +0100133 if ($del_dir === TRUE && $level > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 {
Greg Aker3e9519e2010-01-15 00:09:34 +0000135 return @rmdir($path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 }
Barry Mienydd671972010-10-04 16:33:58 +0200137
Greg Aker3e9519e2010-01-15 00:09:34 +0000138 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 }
140}
141
142// ------------------------------------------------------------------------
143
Derek Allard2067d1a2008-11-13 22:59:24 +0000144if ( ! function_exists('get_filenames'))
145{
Timothy Warren01b129a2012-04-27 11:36:50 -0400146 /**
147 * Get Filenames
148 *
149 * Reads the specified directory and builds an array containing the filenames.
150 * Any sub-folders contained within the specified path are read as well.
151 *
152 * @param string path to source
153 * @param bool whether to include the path as part of the filename
154 * @param bool internal variable to determine recursion status - do not use in calls
155 * @return array
156 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 function get_filenames($source_dir, $include_path = FALSE, $_recursion = FALSE)
158 {
159 static $_filedata = array();
Barry Mienydd671972010-10-04 16:33:58 +0200160
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 if ($fp = @opendir($source_dir))
162 {
163 // reset the array and make sure $source_dir has a trailing slash on the initial call
164 if ($_recursion === FALSE)
165 {
166 $_filedata = array();
167 $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
168 }
Barry Mienydd671972010-10-04 16:33:58 +0200169
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 while (FALSE !== ($file = readdir($fp)))
171 {
Andrey Andreev3b8ad8f2012-01-10 18:09:07 +0200172 if (@is_dir($source_dir.$file) && $file[0] !== '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 {
Barry Mienydd671972010-10-04 16:33:58 +0200174 get_filenames($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 }
Andrey Andreev3b8ad8f2012-01-10 18:09:07 +0200176 elseif ($file[0] !== '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100178 $_filedata[] = ($include_path === TRUE) ? $source_dir.$file : $file;
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 }
180 }
Andrey Andreev3b8ad8f2012-01-10 18:09:07 +0200181 closedir($fp);
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 return $_filedata;
184 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200185
186 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 }
188}
189
190// --------------------------------------------------------------------
191
Derek Allard2067d1a2008-11-13 22:59:24 +0000192if ( ! function_exists('get_dir_file_info'))
193{
Timothy Warren01b129a2012-04-27 11:36:50 -0400194 /**
195 * Get Directory File Information
196 *
197 * Reads the specified directory and builds an array containing the filenames,
198 * filesize, dates, and permissions
199 *
200 * Any sub-folders contained within the specified path are read as well.
201 *
202 * @param string path to source
203 * @param bool Look only at the top level directory specified?
204 * @param bool internal variable to determine recursion status - do not use in calls
205 * @return array
206 */
Derek Jones788b00f2009-11-27 18:00:20 +0000207 function get_dir_file_info($source_dir, $top_level_only = TRUE, $_recursion = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 {
Derek Jones63dc27f2009-02-10 21:59:20 +0000209 static $_filedata = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 $relative_path = $source_dir;
Derek Jonesc37f0e12009-04-21 13:33:40 +0000211
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 if ($fp = @opendir($source_dir))
213 {
214 // reset the array and make sure $source_dir has a trailing slash on the initial call
215 if ($_recursion === FALSE)
216 {
217 $_filedata = array();
218 $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
219 }
220
Derek Jones788b00f2009-11-27 18:00:20 +0000221 // foreach (scandir($source_dir, 1) as $file) // In addition to being PHP5+, scandir() is simply not as fast
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 while (FALSE !== ($file = readdir($fp)))
223 {
Andrey Andreev3b8ad8f2012-01-10 18:09:07 +0200224 if (@is_dir($source_dir.$file) && $file[0] !== '.' && $top_level_only === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 {
Barry Mienydd671972010-10-04 16:33:58 +0200226 get_dir_file_info($source_dir.$file.DIRECTORY_SEPARATOR, $top_level_only, TRUE);
227 }
Andrey Andreev3b8ad8f2012-01-10 18:09:07 +0200228 elseif ($file[0] !== '.')
Barry Mienydd671972010-10-04 16:33:58 +0200229 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 $_filedata[$file] = get_file_info($source_dir.$file);
231 $_filedata[$file]['relative_path'] = $relative_path;
232 }
233 }
Andrey Andreev3b8ad8f2012-01-10 18:09:07 +0200234 closedir($fp);
Derek Jones788b00f2009-11-27 18:00:20 +0000235
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 return $_filedata;
237 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200238
239 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 }
241}
242
243// --------------------------------------------------------------------
244
Derek Allard2067d1a2008-11-13 22:59:24 +0000245if ( ! function_exists('get_file_info'))
246{
Timothy Warren01b129a2012-04-27 11:36:50 -0400247 /**
248 * Get File Info
249 *
250 * Given a file and path, returns the name, path, size, date modified
251 * Second parameter allows you to explicitly declare what information you want returned
252 * Options are: name, server_path, size, date, readable, writable, executable, fileperms
253 * Returns FALSE if the file cannot be found.
254 *
255 * @param string path to file
256 * @param mixed array or comma separated string of information returned
257 * @return array
258 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 function get_file_info($file, $returned_values = array('name', 'server_path', 'size', 'date'))
260 {
261
262 if ( ! file_exists($file))
263 {
264 return FALSE;
265 }
266
267 if (is_string($returned_values))
268 {
269 $returned_values = explode(',', $returned_values);
270 }
271
272 foreach ($returned_values as $key)
273 {
274 switch ($key)
275 {
276 case 'name':
Derek Jonesc37f0e12009-04-21 13:33:40 +0000277 $fileinfo['name'] = substr(strrchr($file, DIRECTORY_SEPARATOR), 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 break;
279 case 'server_path':
280 $fileinfo['server_path'] = $file;
281 break;
282 case 'size':
283 $fileinfo['size'] = filesize($file);
284 break;
285 case 'date':
Robin Sowell73c19fc2010-04-09 11:18:26 -0400286 $fileinfo['date'] = filemtime($file);
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 break;
288 case 'readable':
289 $fileinfo['readable'] = is_readable($file);
290 break;
291 case 'writable':
Derek Jones37f4b9c2011-07-01 17:56:50 -0500292 // There are known problems using is_weritable on IIS. It may not be reliable - consider fileperms()
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 $fileinfo['writable'] = is_writable($file);
294 break;
295 case 'executable':
296 $fileinfo['executable'] = is_executable($file);
297 break;
298 case 'fileperms':
299 $fileinfo['fileperms'] = fileperms($file);
300 break;
301 }
302 }
303
304 return $fileinfo;
305 }
306}
307
308// --------------------------------------------------------------------
309
Derek Allard2067d1a2008-11-13 22:59:24 +0000310if ( ! function_exists('get_mime_by_extension'))
311{
Timothy Warren01b129a2012-04-27 11:36:50 -0400312 /**
313 * Get Mime by Extension
314 *
315 * Translates a file extension into a mime type based on config/mimes.php.
316 * Returns FALSE if it can't determine the type, or open the mime config file
317 *
318 * Note: this is NOT an accurate way of determining file mime types, and is here strictly as a convenience
319 * It should NOT be trusted, and should certainly NOT be used for security
320 *
321 * @param string path to file
322 * @return mixed
323 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 function get_mime_by_extension($file)
325 {
Derek Allard95023112010-01-17 07:51:21 +0000326 $extension = strtolower(substr(strrchr($file, '.'), 1));
Derek Jonesc37f0e12009-04-21 13:33:40 +0000327
Phil Sturgeon39b1c112012-06-04 16:51:14 -0500328 static $mimes;
Derek Jonesc37f0e12009-04-21 13:33:40 +0000329
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 if ( ! is_array($mimes))
331 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300332 $mimes =& get_mimes();
Eric Barnes92808342011-03-18 09:02:37 -0400333
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300334 if (empty($mimes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 {
336 return FALSE;
337 }
338 }
339
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300340 if (isset($mimes[$extension]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300342 return is_array($mimes[$extension])
343 ? current($mimes[$extension]) // Multiple mime types, just give the first one
344 : $mimes[$extension];
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200346
347 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 }
349}
350
351// --------------------------------------------------------------------
352
Derek Allard2067d1a2008-11-13 22:59:24 +0000353if ( ! function_exists('symbolic_permissions'))
354{
Timothy Warren01b129a2012-04-27 11:36:50 -0400355 /**
356 * Symbolic Permissions
357 *
358 * Takes a numeric value representing a file's permissions and returns
359 * standard symbolic notation representing that value
360 *
361 * @param int
362 * @return string
363 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 function symbolic_permissions($perms)
Barry Mienydd671972010-10-04 16:33:58 +0200365 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200366 if (($perms & 0xC000) === 0xC000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 {
368 $symbolic = 's'; // Socket
369 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200370 elseif (($perms & 0xA000) === 0xA000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 {
372 $symbolic = 'l'; // Symbolic Link
373 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200374 elseif (($perms & 0x8000) === 0x8000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 {
376 $symbolic = '-'; // Regular
377 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200378 elseif (($perms & 0x6000) === 0x6000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 {
380 $symbolic = 'b'; // Block special
381 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200382 elseif (($perms & 0x4000) === 0x4000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 {
384 $symbolic = 'd'; // Directory
385 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200386 elseif (($perms & 0x2000) === 0x2000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 {
388 $symbolic = 'c'; // Character special
389 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200390 elseif (($perms & 0x1000) === 0x1000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 {
392 $symbolic = 'p'; // FIFO pipe
393 }
394 else
395 {
396 $symbolic = 'u'; // Unknown
397 }
398
399 // Owner
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200400 $symbolic .= (($perms & 0x0100) ? 'r' : '-')
401 . (($perms & 0x0080) ? 'w' : '-')
402 . (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x' ) : (($perms & 0x0800) ? 'S' : '-'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000403
404 // Group
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200405 $symbolic .= (($perms & 0x0020) ? 'r' : '-')
406 . (($perms & 0x0010) ? 'w' : '-')
407 . (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x' ) : (($perms & 0x0400) ? 'S' : '-'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000408
409 // World
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200410 $symbolic .= (($perms & 0x0004) ? 'r' : '-')
411 . (($perms & 0x0002) ? 'w' : '-')
412 . (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x' ) : (($perms & 0x0200) ? 'T' : '-'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000413
Barry Mienydd671972010-10-04 16:33:58 +0200414 return $symbolic;
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 }
416}
417
418// --------------------------------------------------------------------
419
Derek Allard2067d1a2008-11-13 22:59:24 +0000420if ( ! function_exists('octal_permissions'))
421{
Timothy Warren01b129a2012-04-27 11:36:50 -0400422 /**
423 * Octal Permissions
424 *
425 * Takes a numeric value representing a file's permissions and returns
426 * a three character string representing the file's octal permissions
427 *
428 * @param int
429 * @return string
430 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 function octal_permissions($perms)
432 {
433 return substr(sprintf('%o', $perms), -3);
434 }
435}
436
Derek Allard2067d1a2008-11-13 22:59:24 +0000437/* End of file file_helper.php */
Andrey Andreevb2518642012-03-26 21:44:08 +0300438/* Location: ./system/helpers/file_helper.php */