blob: be616f62d94715f96be44ed244bcdd8f2ce87300 [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
Derek Jonesc37f0e12009-04-21 13:33:40 +000035 * @link http://codeigniter.com/user_guide/helpers/file_helpers.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 *
Timothy Warren01b129a2012-04-27 11:36:50 -040050 * @param string path to file
51 * @return string
52 */
Derek Allard2067d1a2008-11-13 22:59:24 +000053 function read_file($file)
54 {
Andrey Andreev596c51c2012-06-07 15:51:58 +030055 return @file_get_contents($file);
Derek Allard2067d1a2008-11-13 22:59:24 +000056 }
57}
Barry Mienydd671972010-10-04 16:33:58 +020058
Derek Allard2067d1a2008-11-13 22:59:24 +000059// ------------------------------------------------------------------------
60
Derek Allard2067d1a2008-11-13 22:59:24 +000061if ( ! function_exists('write_file'))
62{
Timothy Warren01b129a2012-04-27 11:36:50 -040063 /**
64 * Write File
65 *
66 * Writes data to the file specified in the path.
67 * Creates a new file if non-existent.
68 *
69 * @param string path to file
70 * @param string file data
71 * @param int
72 * @return bool
73 */
Derek Allard2067d1a2008-11-13 22:59:24 +000074 function write_file($path, $data, $mode = FOPEN_WRITE_CREATE_DESTRUCTIVE)
75 {
76 if ( ! $fp = @fopen($path, $mode))
77 {
78 return FALSE;
79 }
Barry Mienydd671972010-10-04 16:33:58 +020080
Derek Allard2067d1a2008-11-13 22:59:24 +000081 flock($fp, LOCK_EX);
82 fwrite($fp, $data);
83 flock($fp, LOCK_UN);
Barry Mienydd671972010-10-04 16:33:58 +020084 fclose($fp);
Derek Allard2067d1a2008-11-13 22:59:24 +000085
86 return TRUE;
87 }
88}
Barry Mienydd671972010-10-04 16:33:58 +020089
Derek Allard2067d1a2008-11-13 22:59:24 +000090// ------------------------------------------------------------------------
91
Derek Allard2067d1a2008-11-13 22:59:24 +000092if ( ! function_exists('delete_files'))
93{
Timothy Warren01b129a2012-04-27 11:36:50 -040094 /**
95 * Delete Files
96 *
97 * Deletes all files contained in the supplied directory path.
98 * Files must be writable or owned by the system in order to be deleted.
99 * If the second parameter is set to TRUE, any directories contained
100 * within the supplied base directory will be nuked as well.
101 *
102 * @param string path to file
103 * @param bool whether to delete any directories found in the path
104 * @param int
105 * @param bool whether to skip deleting .htaccess and index page files
106 * @return bool
107 */
Andrey Andreevfd6c2bc2012-04-03 16:21:48 +0300108 function delete_files($path, $del_dir = FALSE, $level = 0, $htdocs = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200109 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 // Trim the trailing slash
Derek Jonesc37f0e12009-04-21 13:33:40 +0000111 $path = rtrim($path, DIRECTORY_SEPARATOR);
Barry Mienydd671972010-10-04 16:33:58 +0200112
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 if ( ! $current_dir = @opendir($path))
Greg Aker3e9519e2010-01-15 00:09:34 +0000114 {
Barry Mienydd671972010-10-04 16:33:58 +0200115 return FALSE;
Greg Aker3e9519e2010-01-15 00:09:34 +0000116 }
Barry Mienydd671972010-10-04 16:33:58 +0200117
Pascal Kriete45e3cdf2011-02-14 13:26:20 -0500118 while (FALSE !== ($filename = @readdir($current_dir)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 {
Andrey Andreevb2518642012-03-26 21:44:08 +0300120 if ($filename !== '.' && $filename !== '..')
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200122 if (is_dir($path.DIRECTORY_SEPARATOR.$filename) && $filename[0] !== '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 {
Andrey Andreevfd6c2bc2012-04-03 16:21:48 +0300124 delete_files($path.DIRECTORY_SEPARATOR.$filename, $del_dir, $level + 1, $htdocs);
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 }
st214c350c2012-04-25 02:48:49 +0800126 elseif ($htdocs === TRUE && ! preg_match('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i', $filename))
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 {
Andrey Andreevb2518642012-03-26 21:44:08 +0300128 @unlink($path.DIRECTORY_SEPARATOR.$filename);
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 }
130 }
131 }
132 @closedir($current_dir);
Barry Mienydd671972010-10-04 16:33:58 +0200133
Alex Bilbie773ccc32012-06-02 11:11:08 +0100134 if ($del_dir === TRUE && $level > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 {
Greg Aker3e9519e2010-01-15 00:09:34 +0000136 return @rmdir($path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 }
Barry Mienydd671972010-10-04 16:33:58 +0200138
Greg Aker3e9519e2010-01-15 00:09:34 +0000139 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 }
141}
142
143// ------------------------------------------------------------------------
144
Derek Allard2067d1a2008-11-13 22:59:24 +0000145if ( ! function_exists('get_filenames'))
146{
Timothy Warren01b129a2012-04-27 11:36:50 -0400147 /**
148 * Get Filenames
149 *
150 * Reads the specified directory and builds an array containing the filenames.
151 * Any sub-folders contained within the specified path are read as well.
152 *
153 * @param string path to source
154 * @param bool whether to include the path as part of the filename
155 * @param bool internal variable to determine recursion status - do not use in calls
156 * @return array
157 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 function get_filenames($source_dir, $include_path = FALSE, $_recursion = FALSE)
159 {
160 static $_filedata = array();
Barry Mienydd671972010-10-04 16:33:58 +0200161
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 if ($fp = @opendir($source_dir))
163 {
164 // reset the array and make sure $source_dir has a trailing slash on the initial call
165 if ($_recursion === FALSE)
166 {
167 $_filedata = array();
168 $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
169 }
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 while (FALSE !== ($file = readdir($fp)))
172 {
Andrey Andreev3b8ad8f2012-01-10 18:09:07 +0200173 if (@is_dir($source_dir.$file) && $file[0] !== '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 {
Barry Mienydd671972010-10-04 16:33:58 +0200175 get_filenames($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 }
Andrey Andreev3b8ad8f2012-01-10 18:09:07 +0200177 elseif ($file[0] !== '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100179 $_filedata[] = ($include_path === TRUE) ? $source_dir.$file : $file;
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 }
181 }
Andrey Andreev3b8ad8f2012-01-10 18:09:07 +0200182 closedir($fp);
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200183
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 return $_filedata;
185 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200186
187 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 }
189}
190
191// --------------------------------------------------------------------
192
Derek Allard2067d1a2008-11-13 22:59:24 +0000193if ( ! function_exists('get_dir_file_info'))
194{
Timothy Warren01b129a2012-04-27 11:36:50 -0400195 /**
196 * Get Directory File Information
197 *
198 * Reads the specified directory and builds an array containing the filenames,
199 * filesize, dates, and permissions
200 *
201 * Any sub-folders contained within the specified path are read as well.
202 *
203 * @param string path to source
204 * @param bool Look only at the top level directory specified?
205 * @param bool internal variable to determine recursion status - do not use in calls
206 * @return array
207 */
Derek Jones788b00f2009-11-27 18:00:20 +0000208 function get_dir_file_info($source_dir, $top_level_only = TRUE, $_recursion = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 {
Derek Jones63dc27f2009-02-10 21:59:20 +0000210 static $_filedata = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 $relative_path = $source_dir;
Derek Jonesc37f0e12009-04-21 13:33:40 +0000212
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 if ($fp = @opendir($source_dir))
214 {
215 // reset the array and make sure $source_dir has a trailing slash on the initial call
216 if ($_recursion === FALSE)
217 {
218 $_filedata = array();
219 $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
220 }
221
Derek Jones788b00f2009-11-27 18:00:20 +0000222 // 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 +0000223 while (FALSE !== ($file = readdir($fp)))
224 {
Andrey Andreev3b8ad8f2012-01-10 18:09:07 +0200225 if (@is_dir($source_dir.$file) && $file[0] !== '.' && $top_level_only === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 {
Barry Mienydd671972010-10-04 16:33:58 +0200227 get_dir_file_info($source_dir.$file.DIRECTORY_SEPARATOR, $top_level_only, TRUE);
228 }
Andrey Andreev3b8ad8f2012-01-10 18:09:07 +0200229 elseif ($file[0] !== '.')
Barry Mienydd671972010-10-04 16:33:58 +0200230 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 $_filedata[$file] = get_file_info($source_dir.$file);
232 $_filedata[$file]['relative_path'] = $relative_path;
233 }
234 }
Andrey Andreev3b8ad8f2012-01-10 18:09:07 +0200235 closedir($fp);
Derek Jones788b00f2009-11-27 18:00:20 +0000236
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 return $_filedata;
238 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200239
240 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 }
242}
243
244// --------------------------------------------------------------------
245
Derek Allard2067d1a2008-11-13 22:59:24 +0000246if ( ! function_exists('get_file_info'))
247{
Timothy Warren01b129a2012-04-27 11:36:50 -0400248 /**
249 * Get File Info
250 *
251 * Given a file and path, returns the name, path, size, date modified
252 * Second parameter allows you to explicitly declare what information you want returned
253 * Options are: name, server_path, size, date, readable, writable, executable, fileperms
254 * Returns FALSE if the file cannot be found.
255 *
256 * @param string path to file
257 * @param mixed array or comma separated string of information returned
258 * @return array
259 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 function get_file_info($file, $returned_values = array('name', 'server_path', 'size', 'date'))
261 {
262
263 if ( ! file_exists($file))
264 {
265 return FALSE;
266 }
267
268 if (is_string($returned_values))
269 {
270 $returned_values = explode(',', $returned_values);
271 }
272
273 foreach ($returned_values as $key)
274 {
275 switch ($key)
276 {
277 case 'name':
Derek Jonesc37f0e12009-04-21 13:33:40 +0000278 $fileinfo['name'] = substr(strrchr($file, DIRECTORY_SEPARATOR), 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 break;
280 case 'server_path':
281 $fileinfo['server_path'] = $file;
282 break;
283 case 'size':
284 $fileinfo['size'] = filesize($file);
285 break;
286 case 'date':
Robin Sowell73c19fc2010-04-09 11:18:26 -0400287 $fileinfo['date'] = filemtime($file);
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 break;
289 case 'readable':
290 $fileinfo['readable'] = is_readable($file);
291 break;
292 case 'writable':
Derek Jones37f4b9c2011-07-01 17:56:50 -0500293 // There are known problems using is_weritable on IIS. It may not be reliable - consider fileperms()
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 $fileinfo['writable'] = is_writable($file);
295 break;
296 case 'executable':
297 $fileinfo['executable'] = is_executable($file);
298 break;
299 case 'fileperms':
300 $fileinfo['fileperms'] = fileperms($file);
301 break;
302 }
303 }
304
305 return $fileinfo;
306 }
307}
308
309// --------------------------------------------------------------------
310
Derek Allard2067d1a2008-11-13 22:59:24 +0000311if ( ! function_exists('get_mime_by_extension'))
312{
Timothy Warren01b129a2012-04-27 11:36:50 -0400313 /**
314 * Get Mime by Extension
315 *
316 * Translates a file extension into a mime type based on config/mimes.php.
317 * Returns FALSE if it can't determine the type, or open the mime config file
318 *
319 * Note: this is NOT an accurate way of determining file mime types, and is here strictly as a convenience
320 * It should NOT be trusted, and should certainly NOT be used for security
321 *
322 * @param string path to file
323 * @return mixed
324 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 function get_mime_by_extension($file)
326 {
Derek Allard95023112010-01-17 07:51:21 +0000327 $extension = strtolower(substr(strrchr($file, '.'), 1));
Derek Jonesc37f0e12009-04-21 13:33:40 +0000328
Phil Sturgeon39b1c112012-06-04 16:51:14 -0500329 static $mimes;
Derek Jonesc37f0e12009-04-21 13:33:40 +0000330
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 if ( ! is_array($mimes))
332 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300333 $mimes =& get_mimes();
Eric Barnes92808342011-03-18 09:02:37 -0400334
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300335 if (empty($mimes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 {
337 return FALSE;
338 }
339 }
340
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300341 if (isset($mimes[$extension]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300343 return is_array($mimes[$extension])
344 ? current($mimes[$extension]) // Multiple mime types, just give the first one
345 : $mimes[$extension];
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200347
348 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 }
350}
351
352// --------------------------------------------------------------------
353
Derek Allard2067d1a2008-11-13 22:59:24 +0000354if ( ! function_exists('symbolic_permissions'))
355{
Timothy Warren01b129a2012-04-27 11:36:50 -0400356 /**
357 * Symbolic Permissions
358 *
359 * Takes a numeric value representing a file's permissions and returns
360 * standard symbolic notation representing that value
361 *
362 * @param int
363 * @return string
364 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 function symbolic_permissions($perms)
Barry Mienydd671972010-10-04 16:33:58 +0200366 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200367 if (($perms & 0xC000) === 0xC000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 {
369 $symbolic = 's'; // Socket
370 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200371 elseif (($perms & 0xA000) === 0xA000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 {
373 $symbolic = 'l'; // Symbolic Link
374 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200375 elseif (($perms & 0x8000) === 0x8000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 {
377 $symbolic = '-'; // Regular
378 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200379 elseif (($perms & 0x6000) === 0x6000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 {
381 $symbolic = 'b'; // Block special
382 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200383 elseif (($perms & 0x4000) === 0x4000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 {
385 $symbolic = 'd'; // Directory
386 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200387 elseif (($perms & 0x2000) === 0x2000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 {
389 $symbolic = 'c'; // Character special
390 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200391 elseif (($perms & 0x1000) === 0x1000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 {
393 $symbolic = 'p'; // FIFO pipe
394 }
395 else
396 {
397 $symbolic = 'u'; // Unknown
398 }
399
400 // Owner
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200401 $symbolic .= (($perms & 0x0100) ? 'r' : '-')
402 . (($perms & 0x0080) ? 'w' : '-')
403 . (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x' ) : (($perms & 0x0800) ? 'S' : '-'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000404
405 // Group
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200406 $symbolic .= (($perms & 0x0020) ? 'r' : '-')
407 . (($perms & 0x0010) ? 'w' : '-')
408 . (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x' ) : (($perms & 0x0400) ? 'S' : '-'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000409
410 // World
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200411 $symbolic .= (($perms & 0x0004) ? 'r' : '-')
412 . (($perms & 0x0002) ? 'w' : '-')
413 . (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x' ) : (($perms & 0x0200) ? 'T' : '-'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000414
Barry Mienydd671972010-10-04 16:33:58 +0200415 return $symbolic;
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 }
417}
418
419// --------------------------------------------------------------------
420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421if ( ! function_exists('octal_permissions'))
422{
Timothy Warren01b129a2012-04-27 11:36:50 -0400423 /**
424 * Octal Permissions
425 *
426 * Takes a numeric value representing a file's permissions and returns
427 * a three character string representing the file's octal permissions
428 *
429 * @param int
430 * @return string
431 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 function octal_permissions($perms)
433 {
434 return substr(sprintf('%o', $perms), -3);
435 }
436}
437
Derek Allard2067d1a2008-11-13 22:59:24 +0000438/* End of file file_helper.php */
Andrey Andreevb2518642012-03-26 21:44:08 +0300439/* Location: ./system/helpers/file_helper.php */