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