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