blob: e68bb7f7a3b6012b8de7d6caff17596bb2edd976 [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 Andreev0f0b7692012-06-07 14:57:04 +030047 * This function is DEPRECATED and should be removed in
48 * CodeIgniter 3.1+. Use file_get_contents() instead.
49 *
Andrey Andreevd1cace72012-06-17 03:01:00 +030050 * @deprecated
Timothy Warren01b129a2012-04-27 11:36:50 -040051 * @param string path to file
52 * @return string
53 */
Derek Allard2067d1a2008-11-13 22:59:24 +000054 function read_file($file)
55 {
Andrey Andreev596c51c2012-06-07 15:51:58 +030056 return @file_get_contents($file);
Derek Allard2067d1a2008-11-13 22:59:24 +000057 }
58}
Barry Mienydd671972010-10-04 16:33:58 +020059
Derek Allard2067d1a2008-11-13 22:59:24 +000060// ------------------------------------------------------------------------
61
Derek Allard2067d1a2008-11-13 22:59:24 +000062if ( ! function_exists('write_file'))
63{
Timothy Warren01b129a2012-04-27 11:36:50 -040064 /**
65 * Write File
66 *
67 * Writes data to the file specified in the path.
68 * Creates a new file if non-existent.
69 *
70 * @param string path to file
71 * @param string file data
72 * @param int
73 * @return bool
74 */
Derek Allard2067d1a2008-11-13 22:59:24 +000075 function write_file($path, $data, $mode = FOPEN_WRITE_CREATE_DESTRUCTIVE)
76 {
77 if ( ! $fp = @fopen($path, $mode))
78 {
79 return FALSE;
80 }
Barry Mienydd671972010-10-04 16:33:58 +020081
Derek Allard2067d1a2008-11-13 22:59:24 +000082 flock($fp, LOCK_EX);
83 fwrite($fp, $data);
84 flock($fp, LOCK_UN);
Barry Mienydd671972010-10-04 16:33:58 +020085 fclose($fp);
Derek Allard2067d1a2008-11-13 22:59:24 +000086
87 return TRUE;
88 }
89}
Barry Mienydd671972010-10-04 16:33:58 +020090
Derek Allard2067d1a2008-11-13 22:59:24 +000091// ------------------------------------------------------------------------
92
Derek Allard2067d1a2008-11-13 22:59:24 +000093if ( ! function_exists('delete_files'))
94{
Timothy Warren01b129a2012-04-27 11:36:50 -040095 /**
96 * Delete Files
97 *
98 * Deletes all files contained in the supplied directory path.
99 * Files must be writable or owned by the system in order to be deleted.
100 * If the second parameter is set to TRUE, any directories contained
101 * within the supplied base directory will be nuked as well.
102 *
103 * @param string path to file
104 * @param bool whether to delete any directories found in the path
105 * @param int
106 * @param bool whether to skip deleting .htaccess and index page files
107 * @return bool
108 */
Andrey Andreevfd6c2bc2012-04-03 16:21:48 +0300109 function delete_files($path, $del_dir = FALSE, $level = 0, $htdocs = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200110 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 // Trim the trailing slash
Derek Jonesc37f0e12009-04-21 13:33:40 +0000112 $path = rtrim($path, DIRECTORY_SEPARATOR);
Barry Mienydd671972010-10-04 16:33:58 +0200113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 if ( ! $current_dir = @opendir($path))
Greg Aker3e9519e2010-01-15 00:09:34 +0000115 {
Barry Mienydd671972010-10-04 16:33:58 +0200116 return FALSE;
Greg Aker3e9519e2010-01-15 00:09:34 +0000117 }
Barry Mienydd671972010-10-04 16:33:58 +0200118
Pascal Kriete45e3cdf2011-02-14 13:26:20 -0500119 while (FALSE !== ($filename = @readdir($current_dir)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 {
Andrey Andreevb2518642012-03-26 21:44:08 +0300121 if ($filename !== '.' && $filename !== '..')
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200123 if (is_dir($path.DIRECTORY_SEPARATOR.$filename) && $filename[0] !== '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 {
Andrey Andreevfd6c2bc2012-04-03 16:21:48 +0300125 delete_files($path.DIRECTORY_SEPARATOR.$filename, $del_dir, $level + 1, $htdocs);
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 }
Andrey Andreev27228c92012-07-27 10:36:29 +0300127 elseif ($htdocs !== TRUE OR ! preg_match('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i', $filename))
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 {
Andrey Andreevb2518642012-03-26 21:44:08 +0300129 @unlink($path.DIRECTORY_SEPARATOR.$filename);
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 }
131 }
132 }
133 @closedir($current_dir);
Barry Mienydd671972010-10-04 16:33:58 +0200134
Alex Bilbie773ccc32012-06-02 11:11:08 +0100135 if ($del_dir === TRUE && $level > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 {
Greg Aker3e9519e2010-01-15 00:09:34 +0000137 return @rmdir($path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 }
Barry Mienydd671972010-10-04 16:33:58 +0200139
Greg Aker3e9519e2010-01-15 00:09:34 +0000140 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 }
142}
143
144// ------------------------------------------------------------------------
145
Derek Allard2067d1a2008-11-13 22:59:24 +0000146if ( ! function_exists('get_filenames'))
147{
Timothy Warren01b129a2012-04-27 11:36:50 -0400148 /**
149 * Get Filenames
150 *
151 * Reads the specified directory and builds an array containing the filenames.
152 * Any sub-folders contained within the specified path are read as well.
153 *
154 * @param string path to source
155 * @param bool whether to include the path as part of the filename
156 * @param bool internal variable to determine recursion status - do not use in calls
157 * @return array
158 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 function get_filenames($source_dir, $include_path = FALSE, $_recursion = FALSE)
160 {
161 static $_filedata = array();
Barry Mienydd671972010-10-04 16:33:58 +0200162
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 if ($fp = @opendir($source_dir))
164 {
165 // reset the array and make sure $source_dir has a trailing slash on the initial call
166 if ($_recursion === FALSE)
167 {
168 $_filedata = array();
169 $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
170 }
Barry Mienydd671972010-10-04 16:33:58 +0200171
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 while (FALSE !== ($file = readdir($fp)))
173 {
Andrey Andreev3b8ad8f2012-01-10 18:09:07 +0200174 if (@is_dir($source_dir.$file) && $file[0] !== '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 {
Barry Mienydd671972010-10-04 16:33:58 +0200176 get_filenames($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 }
Andrey Andreev3b8ad8f2012-01-10 18:09:07 +0200178 elseif ($file[0] !== '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100180 $_filedata[] = ($include_path === TRUE) ? $source_dir.$file : $file;
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 }
182 }
Andrey Andreev3b8ad8f2012-01-10 18:09:07 +0200183 closedir($fp);
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200184
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 return $_filedata;
186 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200187
188 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 }
190}
191
192// --------------------------------------------------------------------
193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194if ( ! function_exists('get_dir_file_info'))
195{
Timothy Warren01b129a2012-04-27 11:36:50 -0400196 /**
197 * Get Directory File Information
198 *
199 * Reads the specified directory and builds an array containing the filenames,
200 * filesize, dates, and permissions
201 *
202 * Any sub-folders contained within the specified path are read as well.
203 *
204 * @param string path to source
205 * @param bool Look only at the top level directory specified?
206 * @param bool internal variable to determine recursion status - do not use in calls
207 * @return array
208 */
Derek Jones788b00f2009-11-27 18:00:20 +0000209 function get_dir_file_info($source_dir, $top_level_only = TRUE, $_recursion = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 {
Derek Jones63dc27f2009-02-10 21:59:20 +0000211 static $_filedata = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 $relative_path = $source_dir;
Derek Jonesc37f0e12009-04-21 13:33:40 +0000213
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 if ($fp = @opendir($source_dir))
215 {
216 // reset the array and make sure $source_dir has a trailing slash on the initial call
217 if ($_recursion === FALSE)
218 {
219 $_filedata = array();
220 $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
221 }
222
Derek Jones788b00f2009-11-27 18:00:20 +0000223 // 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 +0000224 while (FALSE !== ($file = readdir($fp)))
225 {
Andrey Andreev3b8ad8f2012-01-10 18:09:07 +0200226 if (@is_dir($source_dir.$file) && $file[0] !== '.' && $top_level_only === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 {
Barry Mienydd671972010-10-04 16:33:58 +0200228 get_dir_file_info($source_dir.$file.DIRECTORY_SEPARATOR, $top_level_only, TRUE);
229 }
Andrey Andreev3b8ad8f2012-01-10 18:09:07 +0200230 elseif ($file[0] !== '.')
Barry Mienydd671972010-10-04 16:33:58 +0200231 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 $_filedata[$file] = get_file_info($source_dir.$file);
233 $_filedata[$file]['relative_path'] = $relative_path;
234 }
235 }
Andrey Andreev3b8ad8f2012-01-10 18:09:07 +0200236 closedir($fp);
Derek Jones788b00f2009-11-27 18:00:20 +0000237
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 return $_filedata;
239 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200240
241 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 }
243}
244
245// --------------------------------------------------------------------
246
Derek Allard2067d1a2008-11-13 22:59:24 +0000247if ( ! function_exists('get_file_info'))
248{
Timothy Warren01b129a2012-04-27 11:36:50 -0400249 /**
250 * Get File Info
251 *
252 * Given a file and path, returns the name, path, size, date modified
253 * Second parameter allows you to explicitly declare what information you want returned
254 * Options are: name, server_path, size, date, readable, writable, executable, fileperms
255 * Returns FALSE if the file cannot be found.
256 *
257 * @param string path to file
258 * @param mixed array or comma separated string of information returned
259 * @return array
260 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 function get_file_info($file, $returned_values = array('name', 'server_path', 'size', 'date'))
262 {
263
264 if ( ! file_exists($file))
265 {
266 return FALSE;
267 }
268
269 if (is_string($returned_values))
270 {
271 $returned_values = explode(',', $returned_values);
272 }
273
274 foreach ($returned_values as $key)
275 {
276 switch ($key)
277 {
278 case 'name':
Derek Jonesc37f0e12009-04-21 13:33:40 +0000279 $fileinfo['name'] = substr(strrchr($file, DIRECTORY_SEPARATOR), 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 break;
281 case 'server_path':
282 $fileinfo['server_path'] = $file;
283 break;
284 case 'size':
285 $fileinfo['size'] = filesize($file);
286 break;
287 case 'date':
Robin Sowell73c19fc2010-04-09 11:18:26 -0400288 $fileinfo['date'] = filemtime($file);
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 break;
290 case 'readable':
291 $fileinfo['readable'] = is_readable($file);
292 break;
293 case 'writable':
Derek Jones37f4b9c2011-07-01 17:56:50 -0500294 // There are known problems using is_weritable on IIS. It may not be reliable - consider fileperms()
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 $fileinfo['writable'] = is_writable($file);
296 break;
297 case 'executable':
298 $fileinfo['executable'] = is_executable($file);
299 break;
300 case 'fileperms':
301 $fileinfo['fileperms'] = fileperms($file);
302 break;
303 }
304 }
305
306 return $fileinfo;
307 }
308}
309
310// --------------------------------------------------------------------
311
Derek Allard2067d1a2008-11-13 22:59:24 +0000312if ( ! function_exists('get_mime_by_extension'))
313{
Timothy Warren01b129a2012-04-27 11:36:50 -0400314 /**
315 * Get Mime by Extension
316 *
317 * Translates a file extension into a mime type based on config/mimes.php.
318 * Returns FALSE if it can't determine the type, or open the mime config file
319 *
320 * Note: this is NOT an accurate way of determining file mime types, and is here strictly as a convenience
321 * It should NOT be trusted, and should certainly NOT be used for security
322 *
323 * @param string path to file
324 * @return mixed
325 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 function get_mime_by_extension($file)
327 {
Derek Allard95023112010-01-17 07:51:21 +0000328 $extension = strtolower(substr(strrchr($file, '.'), 1));
Derek Jonesc37f0e12009-04-21 13:33:40 +0000329
Phil Sturgeon39b1c112012-06-04 16:51:14 -0500330 static $mimes;
Derek Jonesc37f0e12009-04-21 13:33:40 +0000331
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 if ( ! is_array($mimes))
333 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300334 $mimes =& get_mimes();
Eric Barnes92808342011-03-18 09:02:37 -0400335
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300336 if (empty($mimes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
338 return FALSE;
339 }
340 }
341
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300342 if (isset($mimes[$extension]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300344 return is_array($mimes[$extension])
345 ? current($mimes[$extension]) // Multiple mime types, just give the first one
346 : $mimes[$extension];
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200348
349 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 }
351}
352
353// --------------------------------------------------------------------
354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355if ( ! function_exists('symbolic_permissions'))
356{
Timothy Warren01b129a2012-04-27 11:36:50 -0400357 /**
358 * Symbolic Permissions
359 *
360 * Takes a numeric value representing a file's permissions and returns
361 * standard symbolic notation representing that value
362 *
363 * @param int
364 * @return string
365 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 function symbolic_permissions($perms)
Barry Mienydd671972010-10-04 16:33:58 +0200367 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200368 if (($perms & 0xC000) === 0xC000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 {
370 $symbolic = 's'; // Socket
371 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200372 elseif (($perms & 0xA000) === 0xA000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 {
374 $symbolic = 'l'; // Symbolic Link
375 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200376 elseif (($perms & 0x8000) === 0x8000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
378 $symbolic = '-'; // Regular
379 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200380 elseif (($perms & 0x6000) === 0x6000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 {
382 $symbolic = 'b'; // Block special
383 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200384 elseif (($perms & 0x4000) === 0x4000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 {
386 $symbolic = 'd'; // Directory
387 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200388 elseif (($perms & 0x2000) === 0x2000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 {
390 $symbolic = 'c'; // Character special
391 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200392 elseif (($perms & 0x1000) === 0x1000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 {
394 $symbolic = 'p'; // FIFO pipe
395 }
396 else
397 {
398 $symbolic = 'u'; // Unknown
399 }
400
401 // Owner
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200402 $symbolic .= (($perms & 0x0100) ? 'r' : '-')
403 . (($perms & 0x0080) ? 'w' : '-')
404 . (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x' ) : (($perms & 0x0800) ? 'S' : '-'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000405
406 // Group
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200407 $symbolic .= (($perms & 0x0020) ? 'r' : '-')
408 . (($perms & 0x0010) ? 'w' : '-')
409 . (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x' ) : (($perms & 0x0400) ? 'S' : '-'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000410
411 // World
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200412 $symbolic .= (($perms & 0x0004) ? 'r' : '-')
413 . (($perms & 0x0002) ? 'w' : '-')
414 . (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x' ) : (($perms & 0x0200) ? 'T' : '-'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000415
Barry Mienydd671972010-10-04 16:33:58 +0200416 return $symbolic;
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 }
418}
419
420// --------------------------------------------------------------------
421
Derek Allard2067d1a2008-11-13 22:59:24 +0000422if ( ! function_exists('octal_permissions'))
423{
Timothy Warren01b129a2012-04-27 11:36:50 -0400424 /**
425 * Octal Permissions
426 *
427 * Takes a numeric value representing a file's permissions and returns
428 * a three character string representing the file's octal permissions
429 *
430 * @param int
431 * @return string
432 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 function octal_permissions($perms)
434 {
435 return substr(sprintf('%o', $perms), -3);
436 }
437}
438
Derek Allard2067d1a2008-11-13 22:59:24 +0000439/* End of file file_helper.php */
Andrey Andreevb2518642012-03-26 21:44:08 +0300440/* Location: ./system/helpers/file_helper.php */