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