Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 29 | * CodeIgniter File Helpers |
| 30 | * |
| 31 | * @package CodeIgniter |
| 32 | * @subpackage Helpers |
| 33 | * @category Helpers |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 34 | * @author EllisLab Dev Team |
Derek Jones | c37f0e1 | 2009-04-21 13:33:40 +0000 | [diff] [blame] | 35 | * @link http://codeigniter.com/user_guide/helpers/file_helpers.html |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | */ |
| 37 | |
| 38 | // ------------------------------------------------------------------------ |
| 39 | |
| 40 | /** |
| 41 | * Read File |
| 42 | * |
| 43 | * Opens the file specfied in the path and returns it as a string. |
| 44 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 45 | * @param string path to file |
| 46 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 47 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 48 | if ( ! function_exists('read_file')) |
| 49 | { |
| 50 | function read_file($file) |
| 51 | { |
| 52 | if ( ! file_exists($file)) |
| 53 | { |
| 54 | return FALSE; |
| 55 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 56 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 57 | if (function_exists('file_get_contents')) |
| 58 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 59 | return file_get_contents($file); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | if ( ! $fp = @fopen($file, FOPEN_READ)) |
| 63 | { |
| 64 | return FALSE; |
| 65 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 66 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 67 | flock($fp, LOCK_SH); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 68 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 69 | $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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 81 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 82 | // ------------------------------------------------------------------------ |
| 83 | |
| 84 | /** |
| 85 | * Write File |
| 86 | * |
| 87 | * Writes data to the file specified in the path. |
| 88 | * Creates a new file if non-existent. |
| 89 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 90 | * @param string path to file |
| 91 | * @param string file data |
| 92 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 93 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 94 | if ( ! function_exists('write_file')) |
| 95 | { |
| 96 | function write_file($path, $data, $mode = FOPEN_WRITE_CREATE_DESTRUCTIVE) |
| 97 | { |
| 98 | if ( ! $fp = @fopen($path, $mode)) |
| 99 | { |
| 100 | return FALSE; |
| 101 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 102 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 103 | flock($fp, LOCK_EX); |
| 104 | fwrite($fp, $data); |
| 105 | flock($fp, LOCK_UN); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 106 | fclose($fp); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 107 | |
| 108 | return TRUE; |
| 109 | } |
| 110 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 111 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 112 | // ------------------------------------------------------------------------ |
| 113 | |
| 114 | /** |
| 115 | * Delete Files |
| 116 | * |
| 117 | * Deletes all files contained in the supplied directory path. |
| 118 | * Files must be writable or owned by the system in order to be deleted. |
| 119 | * If the second parameter is set to TRUE, any directories contained |
| 120 | * within the supplied base directory will be nuked as well. |
| 121 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 122 | * @param string path to file |
| 123 | * @param bool whether to delete any directories found in the path |
| 124 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 125 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 126 | if ( ! function_exists('delete_files')) |
| 127 | { |
| 128 | function delete_files($path, $del_dir = FALSE, $level = 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 129 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 130 | // Trim the trailing slash |
Derek Jones | c37f0e1 | 2009-04-21 13:33:40 +0000 | [diff] [blame] | 131 | $path = rtrim($path, DIRECTORY_SEPARATOR); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 132 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 133 | if ( ! $current_dir = @opendir($path)) |
Greg Aker | 3e9519e | 2010-01-15 00:09:34 +0000 | [diff] [blame] | 134 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 135 | return FALSE; |
Greg Aker | 3e9519e | 2010-01-15 00:09:34 +0000 | [diff] [blame] | 136 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 137 | |
Pascal Kriete | 45e3cdf | 2011-02-14 13:26:20 -0500 | [diff] [blame] | 138 | while (FALSE !== ($filename = @readdir($current_dir))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 139 | { |
Andrey Andreev | b251864 | 2012-03-26 21:44:08 +0300 | [diff] [blame^] | 140 | if ($filename !== '.' && $filename !== '..') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 141 | { |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 142 | if (is_dir($path.DIRECTORY_SEPARATOR.$filename) && $filename[0] !== '.') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 143 | { |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 144 | delete_files($path.DIRECTORY_SEPARATOR.$filename, $del_dir, $level + 1); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 145 | } |
| 146 | else |
| 147 | { |
Andrey Andreev | b251864 | 2012-03-26 21:44:08 +0300 | [diff] [blame^] | 148 | @unlink($path.DIRECTORY_SEPARATOR.$filename); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | } |
| 152 | @closedir($current_dir); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 153 | |
Andrey Andreev | b251864 | 2012-03-26 21:44:08 +0300 | [diff] [blame^] | 154 | if ($del_dir == TRUE && $level > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 155 | { |
Greg Aker | 3e9519e | 2010-01-15 00:09:34 +0000 | [diff] [blame] | 156 | return @rmdir($path); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 158 | |
Greg Aker | 3e9519e | 2010-01-15 00:09:34 +0000 | [diff] [blame] | 159 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | // ------------------------------------------------------------------------ |
| 164 | |
| 165 | /** |
| 166 | * Get Filenames |
| 167 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 168 | * Reads the specified directory and builds an array containing the filenames. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 169 | * Any sub-folders contained within the specified path are read as well. |
| 170 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 171 | * @param string path to source |
| 172 | * @param bool whether to include the path as part of the filename |
| 173 | * @param bool internal variable to determine recursion status - do not use in calls |
| 174 | * @return array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 175 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 176 | if ( ! function_exists('get_filenames')) |
| 177 | { |
| 178 | function get_filenames($source_dir, $include_path = FALSE, $_recursion = FALSE) |
| 179 | { |
| 180 | static $_filedata = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 181 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | if ($fp = @opendir($source_dir)) |
| 183 | { |
| 184 | // reset the array and make sure $source_dir has a trailing slash on the initial call |
| 185 | if ($_recursion === FALSE) |
| 186 | { |
| 187 | $_filedata = array(); |
| 188 | $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; |
| 189 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 190 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 191 | while (FALSE !== ($file = readdir($fp))) |
| 192 | { |
Andrey Andreev | 3b8ad8f | 2012-01-10 18:09:07 +0200 | [diff] [blame] | 193 | if (@is_dir($source_dir.$file) && $file[0] !== '.') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 194 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 195 | get_filenames($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 196 | } |
Andrey Andreev | 3b8ad8f | 2012-01-10 18:09:07 +0200 | [diff] [blame] | 197 | elseif ($file[0] !== '.') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 198 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 199 | $_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file; |
| 200 | } |
| 201 | } |
Andrey Andreev | 3b8ad8f | 2012-01-10 18:09:07 +0200 | [diff] [blame] | 202 | closedir($fp); |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 203 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 204 | return $_filedata; |
| 205 | } |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 206 | |
| 207 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
| 211 | // -------------------------------------------------------------------- |
| 212 | |
| 213 | /** |
| 214 | * Get Directory File Information |
| 215 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 216 | * Reads the specified directory and builds an array containing the filenames, |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 217 | * filesize, dates, and permissions |
| 218 | * |
| 219 | * Any sub-folders contained within the specified path are read as well. |
| 220 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 221 | * @param string path to source |
Derek Jones | 626d39f | 2010-03-02 23:14:56 -0600 | [diff] [blame] | 222 | * @param bool Look only at the top level directory specified? |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 223 | * @param bool internal variable to determine recursion status - do not use in calls |
| 224 | * @return array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 225 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 226 | if ( ! function_exists('get_dir_file_info')) |
| 227 | { |
Derek Jones | 788b00f | 2009-11-27 18:00:20 +0000 | [diff] [blame] | 228 | function get_dir_file_info($source_dir, $top_level_only = TRUE, $_recursion = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 229 | { |
Derek Jones | 63dc27f | 2009-02-10 21:59:20 +0000 | [diff] [blame] | 230 | static $_filedata = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 231 | $relative_path = $source_dir; |
Derek Jones | c37f0e1 | 2009-04-21 13:33:40 +0000 | [diff] [blame] | 232 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 233 | if ($fp = @opendir($source_dir)) |
| 234 | { |
| 235 | // reset the array and make sure $source_dir has a trailing slash on the initial call |
| 236 | if ($_recursion === FALSE) |
| 237 | { |
| 238 | $_filedata = array(); |
| 239 | $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; |
| 240 | } |
| 241 | |
Derek Jones | 788b00f | 2009-11-27 18:00:20 +0000 | [diff] [blame] | 242 | // foreach (scandir($source_dir, 1) as $file) // In addition to being PHP5+, scandir() is simply not as fast |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 243 | while (FALSE !== ($file = readdir($fp))) |
| 244 | { |
Andrey Andreev | 3b8ad8f | 2012-01-10 18:09:07 +0200 | [diff] [blame] | 245 | if (@is_dir($source_dir.$file) && $file[0] !== '.' && $top_level_only === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 246 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 247 | get_dir_file_info($source_dir.$file.DIRECTORY_SEPARATOR, $top_level_only, TRUE); |
| 248 | } |
Andrey Andreev | 3b8ad8f | 2012-01-10 18:09:07 +0200 | [diff] [blame] | 249 | elseif ($file[0] !== '.') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 250 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 251 | $_filedata[$file] = get_file_info($source_dir.$file); |
| 252 | $_filedata[$file]['relative_path'] = $relative_path; |
| 253 | } |
| 254 | } |
Andrey Andreev | 3b8ad8f | 2012-01-10 18:09:07 +0200 | [diff] [blame] | 255 | closedir($fp); |
Derek Jones | 788b00f | 2009-11-27 18:00:20 +0000 | [diff] [blame] | 256 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 257 | return $_filedata; |
| 258 | } |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 259 | |
| 260 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
| 264 | // -------------------------------------------------------------------- |
| 265 | |
| 266 | /** |
| 267 | * Get File Info |
| 268 | * |
| 269 | * Given a file and path, returns the name, path, size, date modified |
| 270 | * Second parameter allows you to explicitly declare what information you want returned |
| 271 | * Options are: name, server_path, size, date, readable, writable, executable, fileperms |
| 272 | * Returns FALSE if the file cannot be found. |
| 273 | * |
Derek Jones | c37f0e1 | 2009-04-21 13:33:40 +0000 | [diff] [blame] | 274 | * @param string path to file |
| 275 | * @param mixed array or comma separated string of information returned |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | * @return array |
| 277 | */ |
| 278 | if ( ! function_exists('get_file_info')) |
| 279 | { |
| 280 | function get_file_info($file, $returned_values = array('name', 'server_path', 'size', 'date')) |
| 281 | { |
| 282 | |
| 283 | if ( ! file_exists($file)) |
| 284 | { |
| 285 | return FALSE; |
| 286 | } |
| 287 | |
| 288 | if (is_string($returned_values)) |
| 289 | { |
| 290 | $returned_values = explode(',', $returned_values); |
| 291 | } |
| 292 | |
| 293 | foreach ($returned_values as $key) |
| 294 | { |
| 295 | switch ($key) |
| 296 | { |
| 297 | case 'name': |
Derek Jones | c37f0e1 | 2009-04-21 13:33:40 +0000 | [diff] [blame] | 298 | $fileinfo['name'] = substr(strrchr($file, DIRECTORY_SEPARATOR), 1); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 299 | break; |
| 300 | case 'server_path': |
| 301 | $fileinfo['server_path'] = $file; |
| 302 | break; |
| 303 | case 'size': |
| 304 | $fileinfo['size'] = filesize($file); |
| 305 | break; |
| 306 | case 'date': |
Robin Sowell | 73c19fc | 2010-04-09 11:18:26 -0400 | [diff] [blame] | 307 | $fileinfo['date'] = filemtime($file); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 308 | break; |
| 309 | case 'readable': |
| 310 | $fileinfo['readable'] = is_readable($file); |
| 311 | break; |
| 312 | case 'writable': |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 313 | // There are known problems using is_weritable on IIS. It may not be reliable - consider fileperms() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 314 | $fileinfo['writable'] = is_writable($file); |
| 315 | break; |
| 316 | case 'executable': |
| 317 | $fileinfo['executable'] = is_executable($file); |
| 318 | break; |
| 319 | case 'fileperms': |
| 320 | $fileinfo['fileperms'] = fileperms($file); |
| 321 | break; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | return $fileinfo; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | // -------------------------------------------------------------------- |
| 330 | |
| 331 | /** |
| 332 | * Get Mime by Extension |
| 333 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 334 | * Translates a file extension into a mime type based on config/mimes.php. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 335 | * Returns FALSE if it can't determine the type, or open the mime config file |
| 336 | * |
| 337 | * Note: this is NOT an accurate way of determining file mime types, and is here strictly as a convenience |
| 338 | * It should NOT be trusted, and should certainly NOT be used for security |
| 339 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 340 | * @param string path to file |
| 341 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 342 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 343 | if ( ! function_exists('get_mime_by_extension')) |
| 344 | { |
| 345 | function get_mime_by_extension($file) |
| 346 | { |
Derek Allard | 9502311 | 2010-01-17 07:51:21 +0000 | [diff] [blame] | 347 | $extension = strtolower(substr(strrchr($file, '.'), 1)); |
Derek Jones | c37f0e1 | 2009-04-21 13:33:40 +0000 | [diff] [blame] | 348 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 349 | global $mimes; |
Derek Jones | c37f0e1 | 2009-04-21 13:33:40 +0000 | [diff] [blame] | 350 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 351 | if ( ! is_array($mimes)) |
| 352 | { |
Andrey Andreev | 3b8ad8f | 2012-01-10 18:09:07 +0200 | [diff] [blame] | 353 | if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php')) |
Greg Aker | d96f882 | 2011-12-27 16:23:47 -0600 | [diff] [blame] | 354 | { |
| 355 | include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'); |
| 356 | } |
| 357 | elseif (is_file(APPPATH.'config/mimes.php')) |
| 358 | { |
| 359 | include(APPPATH.'config/mimes.php'); |
| 360 | } |
Eric Barnes | 9280834 | 2011-03-18 09:02:37 -0400 | [diff] [blame] | 361 | |
bubbafoley | 0ea0414 | 2011-03-17 14:55:41 -0500 | [diff] [blame] | 362 | if ( ! is_array($mimes)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 363 | { |
| 364 | return FALSE; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | if (array_key_exists($extension, $mimes)) |
| 369 | { |
| 370 | if (is_array($mimes[$extension])) |
| 371 | { |
| 372 | // Multiple mime types, just give the first one |
| 373 | return current($mimes[$extension]); |
| 374 | } |
| 375 | else |
| 376 | { |
| 377 | return $mimes[$extension]; |
| 378 | } |
| 379 | } |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 380 | |
| 381 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | |
| 385 | // -------------------------------------------------------------------- |
| 386 | |
| 387 | /** |
| 388 | * Symbolic Permissions |
| 389 | * |
| 390 | * Takes a numeric value representing a file's permissions and returns |
| 391 | * standard symbolic notation representing that value |
| 392 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 393 | * @param int |
| 394 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 395 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 396 | if ( ! function_exists('symbolic_permissions')) |
| 397 | { |
| 398 | function symbolic_permissions($perms) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 399 | { |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 400 | if (($perms & 0xC000) === 0xC000) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 401 | { |
| 402 | $symbolic = 's'; // Socket |
| 403 | } |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 404 | elseif (($perms & 0xA000) === 0xA000) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 405 | { |
| 406 | $symbolic = 'l'; // Symbolic Link |
| 407 | } |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 408 | elseif (($perms & 0x8000) === 0x8000) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 409 | { |
| 410 | $symbolic = '-'; // Regular |
| 411 | } |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 412 | elseif (($perms & 0x6000) === 0x6000) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 413 | { |
| 414 | $symbolic = 'b'; // Block special |
| 415 | } |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 416 | elseif (($perms & 0x4000) === 0x4000) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 417 | { |
| 418 | $symbolic = 'd'; // Directory |
| 419 | } |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 420 | elseif (($perms & 0x2000) === 0x2000) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 421 | { |
| 422 | $symbolic = 'c'; // Character special |
| 423 | } |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 424 | elseif (($perms & 0x1000) === 0x1000) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 425 | { |
| 426 | $symbolic = 'p'; // FIFO pipe |
| 427 | } |
| 428 | else |
| 429 | { |
| 430 | $symbolic = 'u'; // Unknown |
| 431 | } |
| 432 | |
| 433 | // Owner |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 434 | $symbolic .= (($perms & 0x0100) ? 'r' : '-') |
| 435 | . (($perms & 0x0080) ? 'w' : '-') |
| 436 | . (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x' ) : (($perms & 0x0800) ? 'S' : '-')); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 437 | |
| 438 | // Group |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 439 | $symbolic .= (($perms & 0x0020) ? 'r' : '-') |
| 440 | . (($perms & 0x0010) ? 'w' : '-') |
| 441 | . (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x' ) : (($perms & 0x0400) ? 'S' : '-')); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 442 | |
| 443 | // World |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 444 | $symbolic .= (($perms & 0x0004) ? 'r' : '-') |
| 445 | . (($perms & 0x0002) ? 'w' : '-') |
| 446 | . (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x' ) : (($perms & 0x0200) ? 'T' : '-')); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 447 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 448 | return $symbolic; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
| 452 | // -------------------------------------------------------------------- |
| 453 | |
| 454 | /** |
| 455 | * Octal Permissions |
| 456 | * |
| 457 | * Takes a numeric value representing a file's permissions and returns |
| 458 | * a three character string representing the file's octal permissions |
| 459 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 460 | * @param int |
| 461 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 462 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 463 | if ( ! function_exists('octal_permissions')) |
| 464 | { |
| 465 | function octal_permissions($perms) |
| 466 | { |
| 467 | return substr(sprintf('%o', $perms), -3); |
| 468 | } |
| 469 | } |
| 470 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 471 | /* End of file file_helper.php */ |
Andrey Andreev | b251864 | 2012-03-26 21:44:08 +0300 | [diff] [blame^] | 472 | /* Location: ./system/helpers/file_helper.php */ |