Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [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 |
| 8 | * |
| 9 | * Licensed under the Open Software License version 3.0 |
| 10 | * |
| 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 |
| 21 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/) |
| 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 | { |
| 145 | if ($filename != "." and $filename != "..") |
| 146 | { |
Derek Jones | c37f0e1 | 2009-04-21 13:33:40 +0000 | [diff] [blame] | 147 | if (is_dir($path.DIRECTORY_SEPARATOR.$filename)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 148 | { |
| 149 | // Ignore empty folders |
| 150 | if (substr($filename, 0, 1) != '.') |
| 151 | { |
Derek Jones | c37f0e1 | 2009-04-21 13:33:40 +0000 | [diff] [blame] | 152 | delete_files($path.DIRECTORY_SEPARATOR.$filename, $del_dir, $level + 1); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 153 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 154 | } |
| 155 | else |
| 156 | { |
Derek Jones | c37f0e1 | 2009-04-21 13:33:40 +0000 | [diff] [blame] | 157 | unlink($path.DIRECTORY_SEPARATOR.$filename); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | } |
| 161 | @closedir($current_dir); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 162 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 163 | if ($del_dir == TRUE AND $level > 0) |
| 164 | { |
Greg Aker | 3e9519e | 2010-01-15 00:09:34 +0000 | [diff] [blame] | 165 | return @rmdir($path); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 166 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 167 | |
Greg Aker | 3e9519e | 2010-01-15 00:09:34 +0000 | [diff] [blame] | 168 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
| 172 | // ------------------------------------------------------------------------ |
| 173 | |
| 174 | /** |
| 175 | * Get Filenames |
| 176 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 177 | * Reads the specified directory and builds an array containing the filenames. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 178 | * Any sub-folders contained within the specified path are read as well. |
| 179 | * |
| 180 | * @access public |
| 181 | * @param string path to source |
| 182 | * @param bool whether to include the path as part of the filename |
| 183 | * @param bool internal variable to determine recursion status - do not use in calls |
| 184 | * @return array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 185 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 186 | if ( ! function_exists('get_filenames')) |
| 187 | { |
| 188 | function get_filenames($source_dir, $include_path = FALSE, $_recursion = FALSE) |
| 189 | { |
| 190 | static $_filedata = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 191 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 192 | if ($fp = @opendir($source_dir)) |
| 193 | { |
| 194 | // reset the array and make sure $source_dir has a trailing slash on the initial call |
| 195 | if ($_recursion === FALSE) |
| 196 | { |
| 197 | $_filedata = array(); |
| 198 | $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; |
| 199 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 200 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 201 | while (FALSE !== ($file = readdir($fp))) |
| 202 | { |
| 203 | if (@is_dir($source_dir.$file) && strncmp($file, '.', 1) !== 0) |
| 204 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 205 | get_filenames($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 206 | } |
| 207 | elseif (strncmp($file, '.', 1) !== 0) |
| 208 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 209 | $_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file; |
| 210 | } |
| 211 | } |
| 212 | return $_filedata; |
| 213 | } |
| 214 | else |
| 215 | { |
| 216 | return FALSE; |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | // -------------------------------------------------------------------- |
| 222 | |
| 223 | /** |
| 224 | * Get Directory File Information |
| 225 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 226 | * Reads the specified directory and builds an array containing the filenames, |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 227 | * filesize, dates, and permissions |
| 228 | * |
| 229 | * Any sub-folders contained within the specified path are read as well. |
| 230 | * |
| 231 | * @access public |
| 232 | * @param string path to source |
Derek Jones | 626d39f | 2010-03-02 23:14:56 -0600 | [diff] [blame] | 233 | * @param bool Look only at the top level directory specified? |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 234 | * @param bool internal variable to determine recursion status - do not use in calls |
| 235 | * @return array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 236 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 237 | if ( ! function_exists('get_dir_file_info')) |
| 238 | { |
Derek Jones | 788b00f | 2009-11-27 18:00:20 +0000 | [diff] [blame] | 239 | 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] | 240 | { |
Derek Jones | 63dc27f | 2009-02-10 21:59:20 +0000 | [diff] [blame] | 241 | static $_filedata = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 242 | $relative_path = $source_dir; |
Derek Jones | c37f0e1 | 2009-04-21 13:33:40 +0000 | [diff] [blame] | 243 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 244 | if ($fp = @opendir($source_dir)) |
| 245 | { |
| 246 | // reset the array and make sure $source_dir has a trailing slash on the initial call |
| 247 | if ($_recursion === FALSE) |
| 248 | { |
| 249 | $_filedata = array(); |
| 250 | $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; |
| 251 | } |
| 252 | |
Derek Jones | 788b00f | 2009-11-27 18:00:20 +0000 | [diff] [blame] | 253 | // 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] | 254 | while (FALSE !== ($file = readdir($fp))) |
| 255 | { |
Derek Jones | 788b00f | 2009-11-27 18:00:20 +0000 | [diff] [blame] | 256 | if (@is_dir($source_dir.$file) AND strncmp($file, '.', 1) !== 0 AND $top_level_only === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 257 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 258 | get_dir_file_info($source_dir.$file.DIRECTORY_SEPARATOR, $top_level_only, TRUE); |
| 259 | } |
| 260 | elseif (strncmp($file, '.', 1) !== 0) |
| 261 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 262 | $_filedata[$file] = get_file_info($source_dir.$file); |
| 263 | $_filedata[$file]['relative_path'] = $relative_path; |
| 264 | } |
| 265 | } |
Derek Jones | 788b00f | 2009-11-27 18:00:20 +0000 | [diff] [blame] | 266 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 267 | return $_filedata; |
| 268 | } |
| 269 | else |
| 270 | { |
| 271 | return FALSE; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // -------------------------------------------------------------------- |
| 277 | |
| 278 | /** |
| 279 | * Get File Info |
| 280 | * |
| 281 | * Given a file and path, returns the name, path, size, date modified |
| 282 | * Second parameter allows you to explicitly declare what information you want returned |
| 283 | * Options are: name, server_path, size, date, readable, writable, executable, fileperms |
| 284 | * Returns FALSE if the file cannot be found. |
| 285 | * |
| 286 | * @access public |
Derek Jones | c37f0e1 | 2009-04-21 13:33:40 +0000 | [diff] [blame] | 287 | * @param string path to file |
| 288 | * @param mixed array or comma separated string of information returned |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 289 | * @return array |
| 290 | */ |
| 291 | if ( ! function_exists('get_file_info')) |
| 292 | { |
| 293 | function get_file_info($file, $returned_values = array('name', 'server_path', 'size', 'date')) |
| 294 | { |
| 295 | |
| 296 | if ( ! file_exists($file)) |
| 297 | { |
| 298 | return FALSE; |
| 299 | } |
| 300 | |
| 301 | if (is_string($returned_values)) |
| 302 | { |
| 303 | $returned_values = explode(',', $returned_values); |
| 304 | } |
| 305 | |
| 306 | foreach ($returned_values as $key) |
| 307 | { |
| 308 | switch ($key) |
| 309 | { |
| 310 | case 'name': |
Derek Jones | c37f0e1 | 2009-04-21 13:33:40 +0000 | [diff] [blame] | 311 | $fileinfo['name'] = substr(strrchr($file, DIRECTORY_SEPARATOR), 1); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 312 | break; |
| 313 | case 'server_path': |
| 314 | $fileinfo['server_path'] = $file; |
| 315 | break; |
| 316 | case 'size': |
| 317 | $fileinfo['size'] = filesize($file); |
| 318 | break; |
| 319 | case 'date': |
Robin Sowell | 73c19fc | 2010-04-09 11:18:26 -0400 | [diff] [blame] | 320 | $fileinfo['date'] = filemtime($file); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 321 | break; |
| 322 | case 'readable': |
| 323 | $fileinfo['readable'] = is_readable($file); |
| 324 | break; |
| 325 | case 'writable': |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 326 | // 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] | 327 | $fileinfo['writable'] = is_writable($file); |
| 328 | break; |
| 329 | case 'executable': |
| 330 | $fileinfo['executable'] = is_executable($file); |
| 331 | break; |
| 332 | case 'fileperms': |
| 333 | $fileinfo['fileperms'] = fileperms($file); |
| 334 | break; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | return $fileinfo; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | // -------------------------------------------------------------------- |
| 343 | |
| 344 | /** |
| 345 | * Get Mime by Extension |
| 346 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 347 | * 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] | 348 | * Returns FALSE if it can't determine the type, or open the mime config file |
| 349 | * |
| 350 | * Note: this is NOT an accurate way of determining file mime types, and is here strictly as a convenience |
| 351 | * It should NOT be trusted, and should certainly NOT be used for security |
| 352 | * |
| 353 | * @access public |
| 354 | * @param string path to file |
| 355 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 356 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 357 | if ( ! function_exists('get_mime_by_extension')) |
| 358 | { |
| 359 | function get_mime_by_extension($file) |
| 360 | { |
Derek Allard | 9502311 | 2010-01-17 07:51:21 +0000 | [diff] [blame] | 361 | $extension = strtolower(substr(strrchr($file, '.'), 1)); |
Derek Jones | c37f0e1 | 2009-04-21 13:33:40 +0000 | [diff] [blame] | 362 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 363 | global $mimes; |
Derek Jones | c37f0e1 | 2009-04-21 13:33:40 +0000 | [diff] [blame] | 364 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 365 | if ( ! is_array($mimes)) |
| 366 | { |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 367 | if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php')) |
bubbafoley | 0ea0414 | 2011-03-17 14:55:41 -0500 | [diff] [blame] | 368 | { |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 369 | include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'); |
bubbafoley | 0ea0414 | 2011-03-17 14:55:41 -0500 | [diff] [blame] | 370 | } |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 371 | elseif (is_file(APPPATH.'config/mimes.php')) |
bubbafoley | 0ea0414 | 2011-03-17 14:55:41 -0500 | [diff] [blame] | 372 | { |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 373 | include(APPPATH.'config/mimes.php'); |
bubbafoley | 0ea0414 | 2011-03-17 14:55:41 -0500 | [diff] [blame] | 374 | } |
Eric Barnes | 9280834 | 2011-03-18 09:02:37 -0400 | [diff] [blame] | 375 | |
bubbafoley | 0ea0414 | 2011-03-17 14:55:41 -0500 | [diff] [blame] | 376 | if ( ! is_array($mimes)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 377 | { |
| 378 | return FALSE; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | if (array_key_exists($extension, $mimes)) |
| 383 | { |
| 384 | if (is_array($mimes[$extension])) |
| 385 | { |
| 386 | // Multiple mime types, just give the first one |
| 387 | return current($mimes[$extension]); |
| 388 | } |
| 389 | else |
| 390 | { |
| 391 | return $mimes[$extension]; |
| 392 | } |
| 393 | } |
| 394 | else |
| 395 | { |
| 396 | return FALSE; |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | // -------------------------------------------------------------------- |
| 402 | |
| 403 | /** |
| 404 | * Symbolic Permissions |
| 405 | * |
| 406 | * Takes a numeric value representing a file's permissions and returns |
| 407 | * standard symbolic notation representing that value |
| 408 | * |
| 409 | * @access public |
| 410 | * @param int |
| 411 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 412 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 413 | if ( ! function_exists('symbolic_permissions')) |
| 414 | { |
| 415 | function symbolic_permissions($perms) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 416 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 417 | if (($perms & 0xC000) == 0xC000) |
| 418 | { |
| 419 | $symbolic = 's'; // Socket |
| 420 | } |
| 421 | elseif (($perms & 0xA000) == 0xA000) |
| 422 | { |
| 423 | $symbolic = 'l'; // Symbolic Link |
| 424 | } |
| 425 | elseif (($perms & 0x8000) == 0x8000) |
| 426 | { |
| 427 | $symbolic = '-'; // Regular |
| 428 | } |
| 429 | elseif (($perms & 0x6000) == 0x6000) |
| 430 | { |
| 431 | $symbolic = 'b'; // Block special |
| 432 | } |
| 433 | elseif (($perms & 0x4000) == 0x4000) |
| 434 | { |
| 435 | $symbolic = 'd'; // Directory |
| 436 | } |
| 437 | elseif (($perms & 0x2000) == 0x2000) |
| 438 | { |
| 439 | $symbolic = 'c'; // Character special |
| 440 | } |
| 441 | elseif (($perms & 0x1000) == 0x1000) |
| 442 | { |
| 443 | $symbolic = 'p'; // FIFO pipe |
| 444 | } |
| 445 | else |
| 446 | { |
| 447 | $symbolic = 'u'; // Unknown |
| 448 | } |
| 449 | |
| 450 | // Owner |
| 451 | $symbolic .= (($perms & 0x0100) ? 'r' : '-'); |
| 452 | $symbolic .= (($perms & 0x0080) ? 'w' : '-'); |
| 453 | $symbolic .= (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x' ) : (($perms & 0x0800) ? 'S' : '-')); |
| 454 | |
| 455 | // Group |
| 456 | $symbolic .= (($perms & 0x0020) ? 'r' : '-'); |
| 457 | $symbolic .= (($perms & 0x0010) ? 'w' : '-'); |
| 458 | $symbolic .= (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x' ) : (($perms & 0x0400) ? 'S' : '-')); |
| 459 | |
| 460 | // World |
| 461 | $symbolic .= (($perms & 0x0004) ? 'r' : '-'); |
| 462 | $symbolic .= (($perms & 0x0002) ? 'w' : '-'); |
| 463 | $symbolic .= (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x' ) : (($perms & 0x0200) ? 'T' : '-')); |
| 464 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 465 | return $symbolic; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
| 469 | // -------------------------------------------------------------------- |
| 470 | |
| 471 | /** |
| 472 | * Octal Permissions |
| 473 | * |
| 474 | * Takes a numeric value representing a file's permissions and returns |
| 475 | * a three character string representing the file's octal permissions |
| 476 | * |
| 477 | * @access public |
| 478 | * @param int |
| 479 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 480 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 481 | if ( ! function_exists('octal_permissions')) |
| 482 | { |
| 483 | function octal_permissions($perms) |
| 484 | { |
| 485 | return substr(sprintf('%o', $perms), -3); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | |
| 490 | /* End of file file_helper.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 491 | /* Location: ./system/helpers/file_helper.php */ |