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