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 | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014 - 2018, 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 |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014 - 2018, 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 | f7b028b | 2016-06-22 12:42:44 +0300 | [diff] [blame] | 141 | $filepath = $path.DIRECTORY_SEPARATOR.$filename; |
| 142 | |
| 143 | if (is_dir($filepath) && $filename[0] !== '.' && ! is_link($filepath)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 144 | { |
Andrey Andreev | f7b028b | 2016-06-22 12:42:44 +0300 | [diff] [blame] | 145 | delete_files($filepath, $del_dir, $htdocs, $_level + 1); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 146 | } |
Andrey Andreev | 27228c9 | 2012-07-27 10:36:29 +0300 | [diff] [blame] | 147 | 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] | 148 | { |
Andrey Andreev | f7b028b | 2016-06-22 12:42:44 +0300 | [diff] [blame] | 149 | @unlink($filepath); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | } |
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 | closedir($current_dir); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 155 | |
Andrey Andreev | ea41c8a | 2014-02-26 18:31:02 +0200 | [diff] [blame] | 156 | return ($del_dir === TRUE && $_level > 0) |
| 157 | ? @rmdir($path) |
| 158 | : TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
| 162 | // ------------------------------------------------------------------------ |
| 163 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 164 | if ( ! function_exists('get_filenames')) |
| 165 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 166 | /** |
| 167 | * Get Filenames |
| 168 | * |
| 169 | * Reads the specified directory and builds an array containing the filenames. |
| 170 | * Any sub-folders contained within the specified path are read as well. |
| 171 | * |
| 172 | * @param string path to source |
| 173 | * @param bool whether to include the path as part of the filename |
| 174 | * @param bool internal variable to determine recursion status - do not use in calls |
| 175 | * @return array |
| 176 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 177 | function get_filenames($source_dir, $include_path = FALSE, $_recursion = FALSE) |
| 178 | { |
| 179 | static $_filedata = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 180 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 181 | if ($fp = @opendir($source_dir)) |
| 182 | { |
| 183 | // reset the array and make sure $source_dir has a trailing slash on the initial call |
| 184 | if ($_recursion === FALSE) |
| 185 | { |
| 186 | $_filedata = array(); |
| 187 | $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; |
| 188 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 189 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | while (FALSE !== ($file = readdir($fp))) |
| 191 | { |
Andrey Andreev | ea41c8a | 2014-02-26 18:31:02 +0200 | [diff] [blame] | 192 | if (is_dir($source_dir.$file) && $file[0] !== '.') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 193 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 194 | get_filenames($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 195 | } |
Andrey Andreev | 3b8ad8f | 2012-01-10 18:09:07 +0200 | [diff] [blame] | 196 | elseif ($file[0] !== '.') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 197 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 198 | $_filedata[] = ($include_path === TRUE) ? $source_dir.$file : $file; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 199 | } |
| 200 | } |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 201 | |
Andrey Andreev | ea41c8a | 2014-02-26 18:31:02 +0200 | [diff] [blame] | 202 | closedir($fp); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 203 | return $_filedata; |
| 204 | } |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 205 | |
| 206 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
| 210 | // -------------------------------------------------------------------- |
| 211 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 212 | if ( ! function_exists('get_dir_file_info')) |
| 213 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 214 | /** |
| 215 | * Get Directory File Information |
| 216 | * |
| 217 | * Reads the specified directory and builds an array containing the filenames, |
| 218 | * filesize, dates, and permissions |
| 219 | * |
| 220 | * Any sub-folders contained within the specified path are read as well. |
| 221 | * |
| 222 | * @param string path to source |
| 223 | * @param bool Look only at the top level directory specified? |
| 224 | * @param bool internal variable to determine recursion status - do not use in calls |
| 225 | * @return array |
| 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 | |
Andrey Andreev | f9a615a | 2014-09-28 20:24:06 +0300 | [diff] [blame] | 241 | // 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] | 242 | while (FALSE !== ($file = readdir($fp))) |
| 243 | { |
Andrey Andreev | ea41c8a | 2014-02-26 18:31:02 +0200 | [diff] [blame] | 244 | if (is_dir($source_dir.$file) && $file[0] !== '.' && $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 | } |
Andrey Andreev | 3b8ad8f | 2012-01-10 18:09:07 +0200 | [diff] [blame] | 248 | elseif ($file[0] !== '.') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 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 | |
Andrey Andreev | ea41c8a | 2014-02-26 18:31:02 +0200 | [diff] [blame] | 255 | closedir($fp); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 256 | return $_filedata; |
| 257 | } |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 258 | |
| 259 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
| 263 | // -------------------------------------------------------------------- |
| 264 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 265 | if ( ! function_exists('get_file_info')) |
| 266 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 267 | /** |
| 268 | * Get File Info |
| 269 | * |
| 270 | * Given a file and path, returns the name, path, size, date modified |
| 271 | * Second parameter allows you to explicitly declare what information you want returned |
| 272 | * Options are: name, server_path, size, date, readable, writable, executable, fileperms |
| 273 | * Returns FALSE if the file cannot be found. |
| 274 | * |
| 275 | * @param string path to file |
| 276 | * @param mixed array or comma separated string of information returned |
| 277 | * @return array |
| 278 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 279 | function get_file_info($file, $returned_values = array('name', 'server_path', 'size', 'date')) |
| 280 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 281 | if ( ! file_exists($file)) |
| 282 | { |
| 283 | return FALSE; |
| 284 | } |
| 285 | |
| 286 | if (is_string($returned_values)) |
| 287 | { |
| 288 | $returned_values = explode(',', $returned_values); |
| 289 | } |
| 290 | |
| 291 | foreach ($returned_values as $key) |
| 292 | { |
| 293 | switch ($key) |
| 294 | { |
| 295 | case 'name': |
Andrey Andreev | 12526cd | 2013-07-19 13:06:51 +0300 | [diff] [blame] | 296 | $fileinfo['name'] = basename($file); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 297 | break; |
| 298 | case 'server_path': |
| 299 | $fileinfo['server_path'] = $file; |
| 300 | break; |
| 301 | case 'size': |
| 302 | $fileinfo['size'] = filesize($file); |
| 303 | break; |
| 304 | case 'date': |
Robin Sowell | 73c19fc | 2010-04-09 11:18:26 -0400 | [diff] [blame] | 305 | $fileinfo['date'] = filemtime($file); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 306 | break; |
| 307 | case 'readable': |
| 308 | $fileinfo['readable'] = is_readable($file); |
| 309 | break; |
| 310 | case 'writable': |
Andrey Andreev | 03077f9 | 2014-02-07 14:53:39 +0200 | [diff] [blame] | 311 | $fileinfo['writable'] = is_really_writable($file); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 312 | break; |
| 313 | case 'executable': |
| 314 | $fileinfo['executable'] = is_executable($file); |
| 315 | break; |
| 316 | case 'fileperms': |
| 317 | $fileinfo['fileperms'] = fileperms($file); |
| 318 | break; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | return $fileinfo; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | // -------------------------------------------------------------------- |
| 327 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 328 | if ( ! function_exists('get_mime_by_extension')) |
| 329 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 330 | /** |
| 331 | * Get Mime by Extension |
| 332 | * |
| 333 | * Translates a file extension into a mime type based on config/mimes.php. |
| 334 | * Returns FALSE if it can't determine the type, or open the mime config file |
| 335 | * |
| 336 | * Note: this is NOT an accurate way of determining file mime types, and is here strictly as a convenience |
| 337 | * It should NOT be trusted, and should certainly NOT be used for security |
| 338 | * |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 339 | * @param string $filename File name |
| 340 | * @return string |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 341 | */ |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 342 | function get_mime_by_extension($filename) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 343 | { |
Phil Sturgeon | 39b1c11 | 2012-06-04 16:51:14 -0500 | [diff] [blame] | 344 | static $mimes; |
Derek Jones | c37f0e1 | 2009-04-21 13:33:40 +0000 | [diff] [blame] | 345 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 346 | if ( ! is_array($mimes)) |
| 347 | { |
Andrey Andreev | 48844d1 | 2015-10-05 10:52:04 +0300 | [diff] [blame] | 348 | $mimes = get_mimes(); |
Eric Barnes | 9280834 | 2011-03-18 09:02:37 -0400 | [diff] [blame] | 349 | |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 350 | if (empty($mimes)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 351 | { |
| 352 | return FALSE; |
| 353 | } |
| 354 | } |
| 355 | |
Andrey Andreev | ea41c8a | 2014-02-26 18:31:02 +0200 | [diff] [blame] | 356 | $extension = strtolower(substr(strrchr($filename, '.'), 1)); |
| 357 | |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 358 | if (isset($mimes[$extension])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 359 | { |
Andrey Andreev | 6ef498b | 2012-06-05 22:01:58 +0300 | [diff] [blame] | 360 | return is_array($mimes[$extension]) |
| 361 | ? current($mimes[$extension]) // Multiple mime types, just give the first one |
| 362 | : $mimes[$extension]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 363 | } |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 364 | |
| 365 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
| 369 | // -------------------------------------------------------------------- |
| 370 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 371 | if ( ! function_exists('symbolic_permissions')) |
| 372 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 373 | /** |
| 374 | * Symbolic Permissions |
| 375 | * |
| 376 | * Takes a numeric value representing a file's permissions and returns |
| 377 | * standard symbolic notation representing that value |
| 378 | * |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 379 | * @param int $perms Permissions |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 380 | * @return string |
| 381 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 382 | function symbolic_permissions($perms) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 383 | { |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 384 | if (($perms & 0xC000) === 0xC000) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 385 | { |
| 386 | $symbolic = 's'; // Socket |
| 387 | } |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 388 | elseif (($perms & 0xA000) === 0xA000) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 389 | { |
| 390 | $symbolic = 'l'; // Symbolic Link |
| 391 | } |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 392 | elseif (($perms & 0x8000) === 0x8000) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 393 | { |
| 394 | $symbolic = '-'; // Regular |
| 395 | } |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 396 | elseif (($perms & 0x6000) === 0x6000) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 397 | { |
| 398 | $symbolic = 'b'; // Block special |
| 399 | } |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 400 | elseif (($perms & 0x4000) === 0x4000) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 401 | { |
| 402 | $symbolic = 'd'; // Directory |
| 403 | } |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 404 | elseif (($perms & 0x2000) === 0x2000) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 405 | { |
| 406 | $symbolic = 'c'; // Character special |
| 407 | } |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 408 | elseif (($perms & 0x1000) === 0x1000) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 409 | { |
| 410 | $symbolic = 'p'; // FIFO pipe |
| 411 | } |
| 412 | else |
| 413 | { |
| 414 | $symbolic = 'u'; // Unknown |
| 415 | } |
| 416 | |
| 417 | // Owner |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 418 | $symbolic .= (($perms & 0x0100) ? 'r' : '-') |
Andrey Andreev | ea41c8a | 2014-02-26 18:31:02 +0200 | [diff] [blame] | 419 | .(($perms & 0x0080) ? 'w' : '-') |
| 420 | .(($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x' ) : (($perms & 0x0800) ? 'S' : '-')); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 421 | |
| 422 | // Group |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 423 | $symbolic .= (($perms & 0x0020) ? 'r' : '-') |
Andrey Andreev | ea41c8a | 2014-02-26 18:31:02 +0200 | [diff] [blame] | 424 | .(($perms & 0x0010) ? 'w' : '-') |
| 425 | .(($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x' ) : (($perms & 0x0400) ? 'S' : '-')); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 426 | |
| 427 | // World |
Andrey Andreev | 8bf6bb6 | 2012-01-06 16:11:04 +0200 | [diff] [blame] | 428 | $symbolic .= (($perms & 0x0004) ? 'r' : '-') |
Andrey Andreev | ea41c8a | 2014-02-26 18:31:02 +0200 | [diff] [blame] | 429 | .(($perms & 0x0002) ? 'w' : '-') |
| 430 | .(($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x' ) : (($perms & 0x0200) ? 'T' : '-')); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 431 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 432 | return $symbolic; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
| 436 | // -------------------------------------------------------------------- |
| 437 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 438 | if ( ! function_exists('octal_permissions')) |
| 439 | { |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 440 | /** |
| 441 | * Octal Permissions |
| 442 | * |
| 443 | * Takes a numeric value representing a file's permissions and returns |
| 444 | * a three character string representing the file's octal permissions |
| 445 | * |
Andrey Andreev | f6d9a7c | 2012-11-08 17:27:57 +0200 | [diff] [blame] | 446 | * @param int $perms Permissions |
Timothy Warren | 01b129a | 2012-04-27 11:36:50 -0400 | [diff] [blame] | 447 | * @return string |
| 448 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 449 | function octal_permissions($perms) |
| 450 | { |
| 451 | return substr(sprintf('%o', $perms), -3); |
| 452 | } |
| 453 | } |