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