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 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | if ( ! function_exists('read_file')) |
| 41 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame^] | 42 | /** |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 84 | if ( ! function_exists('write_file')) |
| 85 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame^] | 86 | /** |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 97 | function write_file($path, $data, $mode = FOPEN_WRITE_CREATE_DESTRUCTIVE) |
| 98 | { |
| 99 | if ( ! $fp = @fopen($path, $mode)) |
| 100 | { |
| 101 | return FALSE; |
| 102 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 103 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 104 | flock($fp, LOCK_EX); |
| 105 | fwrite($fp, $data); |
| 106 | flock($fp, LOCK_UN); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 107 | fclose($fp); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 108 | |
| 109 | return TRUE; |
| 110 | } |
| 111 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 112 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 113 | // ------------------------------------------------------------------------ |
| 114 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 115 | if ( ! function_exists('delete_files')) |
| 116 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame^] | 117 | /** |
| 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 Andreev | fd6c2bc | 2012-04-03 16:21:48 +0300 | [diff] [blame] | 131 | function delete_files($path, $del_dir = FALSE, $level = 0, $htdocs = FALSE) |
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 | // Trim the trailing slash |
Derek Jones | c37f0e1 | 2009-04-21 13:33:40 +0000 | [diff] [blame] | 134 | $path = rtrim($path, DIRECTORY_SEPARATOR); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 135 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 136 | if ( ! $current_dir = @opendir($path)) |
Greg Aker | 3e9519e | 2010-01-15 00:09:34 +0000 | [diff] [blame] | 137 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 138 | return FALSE; |
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 | |
Pascal Kriete | 45e3cdf | 2011-02-14 13:26:20 -0500 | [diff] [blame] | 141 | while (FALSE !== ($filename = @readdir($current_dir))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 142 | { |
Andrey Andreev | b251864 | 2012-03-26 21:44:08 +0300 | [diff] [blame] | 143 | if ($filename !== '.' && $filename !== '..') |
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 (is_dir($path.DIRECTORY_SEPARATOR.$filename) && $filename[0] !== '.') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 146 | { |
Andrey Andreev | fd6c2bc | 2012-04-03 16:21:48 +0300 | [diff] [blame] | 147 | delete_files($path.DIRECTORY_SEPARATOR.$filename, $del_dir, $level + 1, $htdocs); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 148 | } |
st2 | 14c350c | 2012-04-25 02:48:49 +0800 | [diff] [blame] | 149 | elseif ($htdocs === TRUE && ! preg_match('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i', $filename)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 150 | { |
Andrey Andreev | b251864 | 2012-03-26 21:44:08 +0300 | [diff] [blame] | 151 | @unlink($path.DIRECTORY_SEPARATOR.$filename); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | } |
| 155 | @closedir($current_dir); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 156 | |
Andrey Andreev | b251864 | 2012-03-26 21:44:08 +0300 | [diff] [blame] | 157 | if ($del_dir == TRUE && $level > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 158 | { |
Greg Aker | 3e9519e | 2010-01-15 00:09:34 +0000 | [diff] [blame] | 159 | return @rmdir($path); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 160 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 161 | |
Greg Aker | 3e9519e | 2010-01-15 00:09:34 +0000 | [diff] [blame] | 162 | return TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | // ------------------------------------------------------------------------ |
| 167 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | if ( ! function_exists('get_filenames')) |
| 169 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame^] | 170 | /** |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 181 | function get_filenames($source_dir, $include_path = FALSE, $_recursion = FALSE) |
| 182 | { |
| 183 | static $_filedata = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 184 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 185 | 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 193 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 194 | while (FALSE !== ($file = readdir($fp))) |
| 195 | { |
Andrey Andreev | 3b8ad8f | 2012-01-10 18:09:07 +0200 | [diff] [blame] | 196 | if (@is_dir($source_dir.$file) && $file[0] !== '.') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 197 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 198 | get_filenames($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 199 | } |
Andrey Andreev | 3b8ad8f | 2012-01-10 18:09:07 +0200 | [diff] [blame] | 200 | elseif ($file[0] !== '.') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 201 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 202 | $_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file; |
| 203 | } |
| 204 | } |
Andrey Andreev | 3b8ad8f | 2012-01-10 18:09:07 +0200 | [diff] [blame] | 205 | closedir($fp); |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 206 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 207 | return $_filedata; |
| 208 | } |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 209 | |
| 210 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
| 214 | // -------------------------------------------------------------------- |
| 215 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 216 | if ( ! function_exists('get_dir_file_info')) |
| 217 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame^] | 218 | /** |
| 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 Jones | 788b00f | 2009-11-27 18:00:20 +0000 | [diff] [blame] | 231 | 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] | 232 | { |
Derek Jones | 63dc27f | 2009-02-10 21:59:20 +0000 | [diff] [blame] | 233 | static $_filedata = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 234 | $relative_path = $source_dir; |
Derek Jones | c37f0e1 | 2009-04-21 13:33:40 +0000 | [diff] [blame] | 235 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 236 | 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 Jones | 788b00f | 2009-11-27 18:00:20 +0000 | [diff] [blame] | 245 | // 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] | 246 | while (FALSE !== ($file = readdir($fp))) |
| 247 | { |
Andrey Andreev | 3b8ad8f | 2012-01-10 18:09:07 +0200 | [diff] [blame] | 248 | if (@is_dir($source_dir.$file) && $file[0] !== '.' && $top_level_only === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 249 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 250 | get_dir_file_info($source_dir.$file.DIRECTORY_SEPARATOR, $top_level_only, TRUE); |
| 251 | } |
Andrey Andreev | 3b8ad8f | 2012-01-10 18:09:07 +0200 | [diff] [blame] | 252 | elseif ($file[0] !== '.') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 253 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 254 | $_filedata[$file] = get_file_info($source_dir.$file); |
| 255 | $_filedata[$file]['relative_path'] = $relative_path; |
| 256 | } |
| 257 | } |
Andrey Andreev | 3b8ad8f | 2012-01-10 18:09:07 +0200 | [diff] [blame] | 258 | closedir($fp); |
Derek Jones | 788b00f | 2009-11-27 18:00:20 +0000 | [diff] [blame] | 259 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 260 | return $_filedata; |
| 261 | } |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 262 | |
| 263 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
| 267 | // -------------------------------------------------------------------- |
| 268 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 269 | if ( ! function_exists('get_file_info')) |
| 270 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame^] | 271 | /** |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 283 | 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 Jones | c37f0e1 | 2009-04-21 13:33:40 +0000 | [diff] [blame] | 301 | $fileinfo['name'] = substr(strrchr($file, DIRECTORY_SEPARATOR), 1); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 302 | 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 Sowell | 73c19fc | 2010-04-09 11:18:26 -0400 | [diff] [blame] | 310 | $fileinfo['date'] = filemtime($file); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 311 | break; |
| 312 | case 'readable': |
| 313 | $fileinfo['readable'] = is_readable($file); |
| 314 | break; |
| 315 | case 'writable': |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 316 | // 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] | 317 | $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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 334 | if ( ! function_exists('get_mime_by_extension')) |
| 335 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame^] | 336 | /** |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 348 | function get_mime_by_extension($file) |
| 349 | { |
Derek Allard | 9502311 | 2010-01-17 07:51:21 +0000 | [diff] [blame] | 350 | $extension = strtolower(substr(strrchr($file, '.'), 1)); |
Derek Jones | c37f0e1 | 2009-04-21 13:33:40 +0000 | [diff] [blame] | 351 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 352 | global $mimes; |
Derek Jones | c37f0e1 | 2009-04-21 13:33:40 +0000 | [diff] [blame] | 353 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 354 | if ( ! is_array($mimes)) |
| 355 | { |
Andrey Andreev | 3b8ad8f | 2012-01-10 18:09:07 +0200 | [diff] [blame] | 356 | if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php')) |
Greg Aker | d96f882 | 2011-12-27 16:23:47 -0600 | [diff] [blame] | 357 | { |
| 358 | include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'); |
| 359 | } |
| 360 | elseif (is_file(APPPATH.'config/mimes.php')) |
| 361 | { |
| 362 | include(APPPATH.'config/mimes.php'); |
| 363 | } |
Eric Barnes | 9280834 | 2011-03-18 09:02:37 -0400 | [diff] [blame] | 364 | |
bubbafoley | 0ea0414 | 2011-03-17 14:55:41 -0500 | [diff] [blame] | 365 | if ( ! is_array($mimes)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 366 | { |
| 367 | return FALSE; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | if (array_key_exists($extension, $mimes)) |
| 372 | { |
| 373 | if (is_array($mimes[$extension])) |
| 374 | { |
| 375 | // Multiple mime types, just give the first one |
| 376 | return current($mimes[$extension]); |
| 377 | } |
| 378 | else |
| 379 | { |
| 380 | return $mimes[$extension]; |
| 381 | } |
| 382 | } |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 383 | |
| 384 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
| 388 | // -------------------------------------------------------------------- |
| 389 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 390 | if ( ! function_exists('symbolic_permissions')) |
| 391 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame^] | 392 | /** |
| 393 | * Symbolic Permissions |
| 394 | * |
| 395 | * Takes a numeric value representing a file's permissions and returns |
| 396 | * standard symbolic notation representing that value |
| 397 | * |
| 398 | * @param int |
| 399 | * @return string |
| 400 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 401 | function symbolic_permissions($perms) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 402 | { |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 403 | if (($perms & 0xC000) === 0xC000) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 404 | { |
| 405 | $symbolic = 's'; // Socket |
| 406 | } |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 407 | elseif (($perms & 0xA000) === 0xA000) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 408 | { |
| 409 | $symbolic = 'l'; // Symbolic Link |
| 410 | } |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 411 | elseif (($perms & 0x8000) === 0x8000) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 412 | { |
| 413 | $symbolic = '-'; // Regular |
| 414 | } |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 415 | elseif (($perms & 0x6000) === 0x6000) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 416 | { |
| 417 | $symbolic = 'b'; // Block special |
| 418 | } |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 419 | elseif (($perms & 0x4000) === 0x4000) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 420 | { |
| 421 | $symbolic = 'd'; // Directory |
| 422 | } |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 423 | elseif (($perms & 0x2000) === 0x2000) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 424 | { |
| 425 | $symbolic = 'c'; // Character special |
| 426 | } |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 427 | elseif (($perms & 0x1000) === 0x1000) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 428 | { |
| 429 | $symbolic = 'p'; // FIFO pipe |
| 430 | } |
| 431 | else |
| 432 | { |
| 433 | $symbolic = 'u'; // Unknown |
| 434 | } |
| 435 | |
| 436 | // Owner |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 437 | $symbolic .= (($perms & 0x0100) ? 'r' : '-') |
| 438 | . (($perms & 0x0080) ? 'w' : '-') |
| 439 | . (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x' ) : (($perms & 0x0800) ? 'S' : '-')); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 440 | |
| 441 | // Group |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 442 | $symbolic .= (($perms & 0x0020) ? 'r' : '-') |
| 443 | . (($perms & 0x0010) ? 'w' : '-') |
| 444 | . (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x' ) : (($perms & 0x0400) ? 'S' : '-')); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 445 | |
| 446 | // World |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 447 | $symbolic .= (($perms & 0x0004) ? 'r' : '-') |
| 448 | . (($perms & 0x0002) ? 'w' : '-') |
| 449 | . (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x' ) : (($perms & 0x0200) ? 'T' : '-')); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 450 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 451 | return $symbolic; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
| 455 | // -------------------------------------------------------------------- |
| 456 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 457 | if ( ! function_exists('octal_permissions')) |
| 458 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame^] | 459 | /** |
| 460 | * Octal Permissions |
| 461 | * |
| 462 | * Takes a numeric value representing a file's permissions and returns |
| 463 | * a three character string representing the file's octal permissions |
| 464 | * |
| 465 | * @param int |
| 466 | * @return string |
| 467 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 468 | function octal_permissions($perms) |
| 469 | { |
| 470 | return substr(sprintf('%o', $perms), -3); |
| 471 | } |
| 472 | } |
| 473 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 474 | /* End of file file_helper.php */ |
Andrey Andreev | b251864 | 2012-03-26 21:44:08 +0300 | [diff] [blame] | 475 | /* Location: ./system/helpers/file_helper.php */ |