blob: d53d986f93b489efabfecb5e3cfa27d916f0f78b [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 *
47 * @param string path to file
48 * @return string
49 */
Derek Allard2067d1a2008-11-13 22:59:24 +000050 function read_file($file)
51 {
52 if ( ! file_exists($file))
53 {
54 return FALSE;
55 }
Barry Mienydd671972010-10-04 16:33:58 +020056
Derek Allard2067d1a2008-11-13 22:59:24 +000057 if (function_exists('file_get_contents'))
58 {
Barry Mienydd671972010-10-04 16:33:58 +020059 return file_get_contents($file);
Derek Allard2067d1a2008-11-13 22:59:24 +000060 }
61
62 if ( ! $fp = @fopen($file, FOPEN_READ))
63 {
64 return FALSE;
65 }
Barry Mienydd671972010-10-04 16:33:58 +020066
Derek Allard2067d1a2008-11-13 22:59:24 +000067 flock($fp, LOCK_SH);
Barry Mienydd671972010-10-04 16:33:58 +020068
Derek Allard2067d1a2008-11-13 22:59:24 +000069 $data = '';
70 if (filesize($file) > 0)
71 {
72 $data =& fread($fp, filesize($file));
73 }
74
75 flock($fp, LOCK_UN);
76 fclose($fp);
77
78 return $data;
79 }
80}
Barry Mienydd671972010-10-04 16:33:58 +020081
Derek Allard2067d1a2008-11-13 22:59:24 +000082// ------------------------------------------------------------------------
83
Derek Allard2067d1a2008-11-13 22:59:24 +000084if ( ! function_exists('write_file'))
85{
Timothy Warren01b129a2012-04-27 11:36:50 -040086 /**
87 * Write File
88 *
89 * Writes data to the file specified in the path.
90 * Creates a new file if non-existent.
91 *
92 * @param string path to file
93 * @param string file data
94 * @param int
95 * @return bool
96 */
Derek Allard2067d1a2008-11-13 22:59:24 +000097 function write_file($path, $data, $mode = FOPEN_WRITE_CREATE_DESTRUCTIVE)
98 {
99 if ( ! $fp = @fopen($path, $mode))
100 {
101 return FALSE;
102 }
Barry Mienydd671972010-10-04 16:33:58 +0200103
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 flock($fp, LOCK_EX);
105 fwrite($fp, $data);
106 flock($fp, LOCK_UN);
Barry Mienydd671972010-10-04 16:33:58 +0200107 fclose($fp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000108
109 return TRUE;
110 }
111}
Barry Mienydd671972010-10-04 16:33:58 +0200112
Derek Allard2067d1a2008-11-13 22:59:24 +0000113// ------------------------------------------------------------------------
114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115if ( ! function_exists('delete_files'))
116{
Timothy Warren01b129a2012-04-27 11:36:50 -0400117 /**
118 * Delete Files
119 *
120 * Deletes all files contained in the supplied directory path.
121 * Files must be writable or owned by the system in order to be deleted.
122 * If the second parameter is set to TRUE, any directories contained
123 * within the supplied base directory will be nuked as well.
124 *
125 * @param string path to file
126 * @param bool whether to delete any directories found in the path
127 * @param int
128 * @param bool whether to skip deleting .htaccess and index page files
129 * @return bool
130 */
Andrey Andreevfd6c2bc2012-04-03 16:21:48 +0300131 function delete_files($path, $del_dir = FALSE, $level = 0, $htdocs = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200132 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 // Trim the trailing slash
Derek Jonesc37f0e12009-04-21 13:33:40 +0000134 $path = rtrim($path, DIRECTORY_SEPARATOR);
Barry Mienydd671972010-10-04 16:33:58 +0200135
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 if ( ! $current_dir = @opendir($path))
Greg Aker3e9519e2010-01-15 00:09:34 +0000137 {
Barry Mienydd671972010-10-04 16:33:58 +0200138 return FALSE;
Greg Aker3e9519e2010-01-15 00:09:34 +0000139 }
Barry Mienydd671972010-10-04 16:33:58 +0200140
Pascal Kriete45e3cdf2011-02-14 13:26:20 -0500141 while (FALSE !== ($filename = @readdir($current_dir)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 {
Andrey Andreevb2518642012-03-26 21:44:08 +0300143 if ($filename !== '.' && $filename !== '..')
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200145 if (is_dir($path.DIRECTORY_SEPARATOR.$filename) && $filename[0] !== '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 {
Andrey Andreevfd6c2bc2012-04-03 16:21:48 +0300147 delete_files($path.DIRECTORY_SEPARATOR.$filename, $del_dir, $level + 1, $htdocs);
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 }
st214c350c2012-04-25 02:48:49 +0800149 elseif ($htdocs === TRUE && ! preg_match('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i', $filename))
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 {
Andrey Andreevb2518642012-03-26 21:44:08 +0300151 @unlink($path.DIRECTORY_SEPARATOR.$filename);
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 }
153 }
154 }
155 @closedir($current_dir);
Barry Mienydd671972010-10-04 16:33:58 +0200156
Alex Bilbie773ccc32012-06-02 11:11:08 +0100157 if ($del_dir === TRUE && $level > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 {
Greg Aker3e9519e2010-01-15 00:09:34 +0000159 return @rmdir($path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 }
Barry Mienydd671972010-10-04 16:33:58 +0200161
Greg Aker3e9519e2010-01-15 00:09:34 +0000162 return TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 }
164}
165
166// ------------------------------------------------------------------------
167
Derek Allard2067d1a2008-11-13 22:59:24 +0000168if ( ! function_exists('get_filenames'))
169{
Timothy Warren01b129a2012-04-27 11:36:50 -0400170 /**
171 * Get Filenames
172 *
173 * Reads the specified directory and builds an array containing the filenames.
174 * Any sub-folders contained within the specified path are read as well.
175 *
176 * @param string path to source
177 * @param bool whether to include the path as part of the filename
178 * @param bool internal variable to determine recursion status - do not use in calls
179 * @return array
180 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 function get_filenames($source_dir, $include_path = FALSE, $_recursion = FALSE)
182 {
183 static $_filedata = array();
Barry Mienydd671972010-10-04 16:33:58 +0200184
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 if ($fp = @opendir($source_dir))
186 {
187 // reset the array and make sure $source_dir has a trailing slash on the initial call
188 if ($_recursion === FALSE)
189 {
190 $_filedata = array();
191 $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
192 }
Barry Mienydd671972010-10-04 16:33:58 +0200193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 while (FALSE !== ($file = readdir($fp)))
195 {
Andrey Andreev3b8ad8f2012-01-10 18:09:07 +0200196 if (@is_dir($source_dir.$file) && $file[0] !== '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 {
Barry Mienydd671972010-10-04 16:33:58 +0200198 get_filenames($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 }
Andrey Andreev3b8ad8f2012-01-10 18:09:07 +0200200 elseif ($file[0] !== '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100202 $_filedata[] = ($include_path === TRUE) ? $source_dir.$file : $file;
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 }
204 }
Andrey Andreev3b8ad8f2012-01-10 18:09:07 +0200205 closedir($fp);
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200206
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 return $_filedata;
208 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200209
210 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 }
212}
213
214// --------------------------------------------------------------------
215
Derek Allard2067d1a2008-11-13 22:59:24 +0000216if ( ! function_exists('get_dir_file_info'))
217{
Timothy Warren01b129a2012-04-27 11:36:50 -0400218 /**
219 * Get Directory File Information
220 *
221 * Reads the specified directory and builds an array containing the filenames,
222 * filesize, dates, and permissions
223 *
224 * Any sub-folders contained within the specified path are read as well.
225 *
226 * @param string path to source
227 * @param bool Look only at the top level directory specified?
228 * @param bool internal variable to determine recursion status - do not use in calls
229 * @return array
230 */
Derek Jones788b00f2009-11-27 18:00:20 +0000231 function get_dir_file_info($source_dir, $top_level_only = TRUE, $_recursion = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 {
Derek Jones63dc27f2009-02-10 21:59:20 +0000233 static $_filedata = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 $relative_path = $source_dir;
Derek Jonesc37f0e12009-04-21 13:33:40 +0000235
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 if ($fp = @opendir($source_dir))
237 {
238 // reset the array and make sure $source_dir has a trailing slash on the initial call
239 if ($_recursion === FALSE)
240 {
241 $_filedata = array();
242 $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
243 }
244
Derek Jones788b00f2009-11-27 18:00:20 +0000245 // 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 +0000246 while (FALSE !== ($file = readdir($fp)))
247 {
Andrey Andreev3b8ad8f2012-01-10 18:09:07 +0200248 if (@is_dir($source_dir.$file) && $file[0] !== '.' && $top_level_only === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 {
Barry Mienydd671972010-10-04 16:33:58 +0200250 get_dir_file_info($source_dir.$file.DIRECTORY_SEPARATOR, $top_level_only, TRUE);
251 }
Andrey Andreev3b8ad8f2012-01-10 18:09:07 +0200252 elseif ($file[0] !== '.')
Barry Mienydd671972010-10-04 16:33:58 +0200253 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 $_filedata[$file] = get_file_info($source_dir.$file);
255 $_filedata[$file]['relative_path'] = $relative_path;
256 }
257 }
Andrey Andreev3b8ad8f2012-01-10 18:09:07 +0200258 closedir($fp);
Derek Jones788b00f2009-11-27 18:00:20 +0000259
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 return $_filedata;
261 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200262
263 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 }
265}
266
267// --------------------------------------------------------------------
268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269if ( ! function_exists('get_file_info'))
270{
Timothy Warren01b129a2012-04-27 11:36:50 -0400271 /**
272 * Get File Info
273 *
274 * Given a file and path, returns the name, path, size, date modified
275 * Second parameter allows you to explicitly declare what information you want returned
276 * Options are: name, server_path, size, date, readable, writable, executable, fileperms
277 * Returns FALSE if the file cannot be found.
278 *
279 * @param string path to file
280 * @param mixed array or comma separated string of information returned
281 * @return array
282 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 function get_file_info($file, $returned_values = array('name', 'server_path', 'size', 'date'))
284 {
285
286 if ( ! file_exists($file))
287 {
288 return FALSE;
289 }
290
291 if (is_string($returned_values))
292 {
293 $returned_values = explode(',', $returned_values);
294 }
295
296 foreach ($returned_values as $key)
297 {
298 switch ($key)
299 {
300 case 'name':
Derek Jonesc37f0e12009-04-21 13:33:40 +0000301 $fileinfo['name'] = substr(strrchr($file, DIRECTORY_SEPARATOR), 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 break;
303 case 'server_path':
304 $fileinfo['server_path'] = $file;
305 break;
306 case 'size':
307 $fileinfo['size'] = filesize($file);
308 break;
309 case 'date':
Robin Sowell73c19fc2010-04-09 11:18:26 -0400310 $fileinfo['date'] = filemtime($file);
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 break;
312 case 'readable':
313 $fileinfo['readable'] = is_readable($file);
314 break;
315 case 'writable':
Derek Jones37f4b9c2011-07-01 17:56:50 -0500316 // There are known problems using is_weritable on IIS. It may not be reliable - consider fileperms()
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 $fileinfo['writable'] = is_writable($file);
318 break;
319 case 'executable':
320 $fileinfo['executable'] = is_executable($file);
321 break;
322 case 'fileperms':
323 $fileinfo['fileperms'] = fileperms($file);
324 break;
325 }
326 }
327
328 return $fileinfo;
329 }
330}
331
332// --------------------------------------------------------------------
333
Derek Allard2067d1a2008-11-13 22:59:24 +0000334if ( ! function_exists('get_mime_by_extension'))
335{
Timothy Warren01b129a2012-04-27 11:36:50 -0400336 /**
337 * Get Mime by Extension
338 *
339 * Translates a file extension into a mime type based on config/mimes.php.
340 * Returns FALSE if it can't determine the type, or open the mime config file
341 *
342 * Note: this is NOT an accurate way of determining file mime types, and is here strictly as a convenience
343 * It should NOT be trusted, and should certainly NOT be used for security
344 *
345 * @param string path to file
346 * @return mixed
347 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 function get_mime_by_extension($file)
349 {
Derek Allard95023112010-01-17 07:51:21 +0000350 $extension = strtolower(substr(strrchr($file, '.'), 1));
Derek Jonesc37f0e12009-04-21 13:33:40 +0000351
Phil Sturgeon39b1c112012-06-04 16:51:14 -0500352 static $mimes;
Derek Jonesc37f0e12009-04-21 13:33:40 +0000353
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 if ( ! is_array($mimes))
355 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300356 $mimes =& get_mimes();
Eric Barnes92808342011-03-18 09:02:37 -0400357
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300358 if (empty($mimes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 {
360 return FALSE;
361 }
362 }
363
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300364 if (isset($mimes[$extension]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 {
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300366 return is_array($mimes[$extension])
367 ? current($mimes[$extension]) // Multiple mime types, just give the first one
368 : $mimes[$extension];
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200370
371 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 }
373}
374
375// --------------------------------------------------------------------
376
Derek Allard2067d1a2008-11-13 22:59:24 +0000377if ( ! function_exists('symbolic_permissions'))
378{
Timothy Warren01b129a2012-04-27 11:36:50 -0400379 /**
380 * Symbolic Permissions
381 *
382 * Takes a numeric value representing a file's permissions and returns
383 * standard symbolic notation representing that value
384 *
385 * @param int
386 * @return string
387 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 function symbolic_permissions($perms)
Barry Mienydd671972010-10-04 16:33:58 +0200389 {
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200390 if (($perms & 0xC000) === 0xC000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 {
392 $symbolic = 's'; // Socket
393 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200394 elseif (($perms & 0xA000) === 0xA000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 {
396 $symbolic = 'l'; // Symbolic Link
397 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200398 elseif (($perms & 0x8000) === 0x8000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 {
400 $symbolic = '-'; // Regular
401 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200402 elseif (($perms & 0x6000) === 0x6000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 {
404 $symbolic = 'b'; // Block special
405 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200406 elseif (($perms & 0x4000) === 0x4000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 {
408 $symbolic = 'd'; // Directory
409 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200410 elseif (($perms & 0x2000) === 0x2000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 {
412 $symbolic = 'c'; // Character special
413 }
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200414 elseif (($perms & 0x1000) === 0x1000)
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 {
416 $symbolic = 'p'; // FIFO pipe
417 }
418 else
419 {
420 $symbolic = 'u'; // Unknown
421 }
422
423 // Owner
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200424 $symbolic .= (($perms & 0x0100) ? 'r' : '-')
425 . (($perms & 0x0080) ? 'w' : '-')
426 . (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x' ) : (($perms & 0x0800) ? 'S' : '-'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000427
428 // Group
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200429 $symbolic .= (($perms & 0x0020) ? 'r' : '-')
430 . (($perms & 0x0010) ? 'w' : '-')
431 . (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x' ) : (($perms & 0x0400) ? 'S' : '-'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000432
433 // World
Andrey Andreev8bf6bb62012-01-06 16:11:04 +0200434 $symbolic .= (($perms & 0x0004) ? 'r' : '-')
435 . (($perms & 0x0002) ? 'w' : '-')
436 . (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x' ) : (($perms & 0x0200) ? 'T' : '-'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000437
Barry Mienydd671972010-10-04 16:33:58 +0200438 return $symbolic;
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 }
440}
441
442// --------------------------------------------------------------------
443
Derek Allard2067d1a2008-11-13 22:59:24 +0000444if ( ! function_exists('octal_permissions'))
445{
Timothy Warren01b129a2012-04-27 11:36:50 -0400446 /**
447 * Octal Permissions
448 *
449 * Takes a numeric value representing a file's permissions and returns
450 * a three character string representing the file's octal permissions
451 *
452 * @param int
453 * @return string
454 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 function octal_permissions($perms)
456 {
457 return substr(sprintf('%o', $perms), -3);
458 }
459}
460
Derek Allard2067d1a2008-11-13 22:59:24 +0000461/* End of file file_helper.php */
Andrey Andreevb2518642012-03-26 21:44:08 +0300462/* Location: ./system/helpers/file_helper.php */