| 68 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* CodeIgniter
|
|
|
4 |
*
|
|
|
5 |
* An open source application development framework for PHP
|
|
|
6 |
*
|
|
|
7 |
* This content is released under the MIT License (MIT)
|
|
|
8 |
*
|
| 2257 |
lars |
9 |
* Copyright (c) 2014 - 2018, British Columbia Institute of Technology
|
| 68 |
lars |
10 |
*
|
|
|
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:
|
|
|
17 |
*
|
|
|
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
|
|
|
31 |
* @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
|
| 2257 |
lars |
32 |
* @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
|
| 68 |
lars |
33 |
* @license http://opensource.org/licenses/MIT MIT License
|
|
|
34 |
* @link https://codeigniter.com
|
|
|
35 |
* @since Version 1.0.0
|
|
|
36 |
* @filesource
|
|
|
37 |
*/
|
|
|
38 |
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* CodeIgniter File Helpers
|
|
|
42 |
*
|
|
|
43 |
* @package CodeIgniter
|
|
|
44 |
* @subpackage Helpers
|
|
|
45 |
* @category Helpers
|
|
|
46 |
* @author EllisLab Dev Team
|
|
|
47 |
* @link https://codeigniter.com/user_guide/helpers/file_helper.html
|
|
|
48 |
*/
|
|
|
49 |
|
|
|
50 |
// ------------------------------------------------------------------------
|
|
|
51 |
|
|
|
52 |
if ( ! function_exists('read_file'))
|
|
|
53 |
{
|
|
|
54 |
/**
|
|
|
55 |
* Read File
|
|
|
56 |
*
|
|
|
57 |
* Opens the file specified in the path and returns it as a string.
|
|
|
58 |
*
|
|
|
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
|
|
|
63 |
*/
|
|
|
64 |
function read_file($file)
|
|
|
65 |
{
|
|
|
66 |
return @file_get_contents($file);
|
|
|
67 |
}
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
// ------------------------------------------------------------------------
|
|
|
71 |
|
|
|
72 |
if ( ! function_exists('write_file'))
|
|
|
73 |
{
|
|
|
74 |
/**
|
|
|
75 |
* Write File
|
|
|
76 |
*
|
|
|
77 |
* Writes data to the file specified in the path.
|
|
|
78 |
* Creates a new file if non-existent.
|
|
|
79 |
*
|
|
|
80 |
* @param string $path File path
|
|
|
81 |
* @param string $data Data to write
|
|
|
82 |
* @param string $mode fopen() mode (default: 'wb')
|
|
|
83 |
* @return bool
|
|
|
84 |
*/
|
|
|
85 |
function write_file($path, $data, $mode = 'wb')
|
|
|
86 |
{
|
|
|
87 |
if ( ! $fp = @fopen($path, $mode))
|
|
|
88 |
{
|
|
|
89 |
return FALSE;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
flock($fp, LOCK_EX);
|
|
|
93 |
|
|
|
94 |
for ($result = $written = 0, $length = strlen($data); $written < $length; $written += $result)
|
|
|
95 |
{
|
|
|
96 |
if (($result = fwrite($fp, substr($data, $written))) === FALSE)
|
|
|
97 |
{
|
|
|
98 |
break;
|
|
|
99 |
}
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
flock($fp, LOCK_UN);
|
|
|
103 |
fclose($fp);
|
|
|
104 |
|
|
|
105 |
return is_int($result);
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
// ------------------------------------------------------------------------
|
|
|
110 |
|
|
|
111 |
if ( ! function_exists('delete_files'))
|
|
|
112 |
{
|
|
|
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 |
*
|
|
|
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)
|
|
|
125 |
* @return bool
|
|
|
126 |
*/
|
|
|
127 |
function delete_files($path, $del_dir = FALSE, $htdocs = FALSE, $_level = 0)
|
|
|
128 |
{
|
|
|
129 |
// Trim the trailing slash
|
|
|
130 |
$path = rtrim($path, '/\\');
|
|
|
131 |
|
|
|
132 |
if ( ! $current_dir = @opendir($path))
|
|
|
133 |
{
|
|
|
134 |
return FALSE;
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
while (FALSE !== ($filename = @readdir($current_dir)))
|
|
|
138 |
{
|
|
|
139 |
if ($filename !== '.' && $filename !== '..')
|
|
|
140 |
{
|
|
|
141 |
$filepath = $path.DIRECTORY_SEPARATOR.$filename;
|
|
|
142 |
|
|
|
143 |
if (is_dir($filepath) && $filename[0] !== '.' && ! is_link($filepath))
|
|
|
144 |
{
|
|
|
145 |
delete_files($filepath, $del_dir, $htdocs, $_level + 1);
|
|
|
146 |
}
|
|
|
147 |
elseif ($htdocs !== TRUE OR ! preg_match('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i', $filename))
|
|
|
148 |
{
|
|
|
149 |
@unlink($filepath);
|
|
|
150 |
}
|
|
|
151 |
}
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
closedir($current_dir);
|
|
|
155 |
|
|
|
156 |
return ($del_dir === TRUE && $_level > 0)
|
|
|
157 |
? @rmdir($path)
|
|
|
158 |
: TRUE;
|
|
|
159 |
}
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
// ------------------------------------------------------------------------
|
|
|
163 |
|
|
|
164 |
if ( ! function_exists('get_filenames'))
|
|
|
165 |
{
|
|
|
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 |
*/
|
|
|
177 |
function get_filenames($source_dir, $include_path = FALSE, $_recursion = FALSE)
|
|
|
178 |
{
|
|
|
179 |
static $_filedata = array();
|
|
|
180 |
|
|
|
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 |
}
|
|
|
189 |
|
|
|
190 |
while (FALSE !== ($file = readdir($fp)))
|
|
|
191 |
{
|
|
|
192 |
if (is_dir($source_dir.$file) && $file[0] !== '.')
|
|
|
193 |
{
|
|
|
194 |
get_filenames($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE);
|
|
|
195 |
}
|
|
|
196 |
elseif ($file[0] !== '.')
|
|
|
197 |
{
|
|
|
198 |
$_filedata[] = ($include_path === TRUE) ? $source_dir.$file : $file;
|
|
|
199 |
}
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
closedir($fp);
|
|
|
203 |
return $_filedata;
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
return FALSE;
|
|
|
207 |
}
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
// --------------------------------------------------------------------
|
|
|
211 |
|
|
|
212 |
if ( ! function_exists('get_dir_file_info'))
|
|
|
213 |
{
|
|
|
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 |
*/
|
|
|
227 |
function get_dir_file_info($source_dir, $top_level_only = TRUE, $_recursion = FALSE)
|
|
|
228 |
{
|
|
|
229 |
static $_filedata = array();
|
|
|
230 |
$relative_path = $source_dir;
|
|
|
231 |
|
|
|
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 |
|
|
|
241 |
// Used to be foreach (scandir($source_dir, 1) as $file), but scandir() is simply not as fast
|
|
|
242 |
while (FALSE !== ($file = readdir($fp)))
|
|
|
243 |
{
|
|
|
244 |
if (is_dir($source_dir.$file) && $file[0] !== '.' && $top_level_only === FALSE)
|
|
|
245 |
{
|
|
|
246 |
get_dir_file_info($source_dir.$file.DIRECTORY_SEPARATOR, $top_level_only, TRUE);
|
|
|
247 |
}
|
|
|
248 |
elseif ($file[0] !== '.')
|
|
|
249 |
{
|
|
|
250 |
$_filedata[$file] = get_file_info($source_dir.$file);
|
|
|
251 |
$_filedata[$file]['relative_path'] = $relative_path;
|
|
|
252 |
}
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
closedir($fp);
|
|
|
256 |
return $_filedata;
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
return FALSE;
|
|
|
260 |
}
|
|
|
261 |
}
|
|
|
262 |
|
|
|
263 |
// --------------------------------------------------------------------
|
|
|
264 |
|
|
|
265 |
if ( ! function_exists('get_file_info'))
|
|
|
266 |
{
|
|
|
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 |
*/
|
|
|
279 |
function get_file_info($file, $returned_values = array('name', 'server_path', 'size', 'date'))
|
|
|
280 |
{
|
|
|
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':
|
|
|
296 |
$fileinfo['name'] = basename($file);
|
|
|
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':
|
|
|
305 |
$fileinfo['date'] = filemtime($file);
|
|
|
306 |
break;
|
|
|
307 |
case 'readable':
|
|
|
308 |
$fileinfo['readable'] = is_readable($file);
|
|
|
309 |
break;
|
|
|
310 |
case 'writable':
|
|
|
311 |
$fileinfo['writable'] = is_really_writable($file);
|
|
|
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 |
|
|
|
328 |
if ( ! function_exists('get_mime_by_extension'))
|
|
|
329 |
{
|
|
|
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 |
*
|
|
|
339 |
* @param string $filename File name
|
|
|
340 |
* @return string
|
|
|
341 |
*/
|
|
|
342 |
function get_mime_by_extension($filename)
|
|
|
343 |
{
|
|
|
344 |
static $mimes;
|
|
|
345 |
|
|
|
346 |
if ( ! is_array($mimes))
|
|
|
347 |
{
|
|
|
348 |
$mimes = get_mimes();
|
|
|
349 |
|
|
|
350 |
if (empty($mimes))
|
|
|
351 |
{
|
|
|
352 |
return FALSE;
|
|
|
353 |
}
|
|
|
354 |
}
|
|
|
355 |
|
|
|
356 |
$extension = strtolower(substr(strrchr($filename, '.'), 1));
|
|
|
357 |
|
|
|
358 |
if (isset($mimes[$extension]))
|
|
|
359 |
{
|
|
|
360 |
return is_array($mimes[$extension])
|
|
|
361 |
? current($mimes[$extension]) // Multiple mime types, just give the first one
|
|
|
362 |
: $mimes[$extension];
|
|
|
363 |
}
|
|
|
364 |
|
|
|
365 |
return FALSE;
|
|
|
366 |
}
|
|
|
367 |
}
|
|
|
368 |
|
|
|
369 |
// --------------------------------------------------------------------
|
|
|
370 |
|
|
|
371 |
if ( ! function_exists('symbolic_permissions'))
|
|
|
372 |
{
|
|
|
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 |
*
|
|
|
379 |
* @param int $perms Permissions
|
|
|
380 |
* @return string
|
|
|
381 |
*/
|
|
|
382 |
function symbolic_permissions($perms)
|
|
|
383 |
{
|
|
|
384 |
if (($perms & 0xC000) === 0xC000)
|
|
|
385 |
{
|
|
|
386 |
$symbolic = 's'; // Socket
|
|
|
387 |
}
|
|
|
388 |
elseif (($perms & 0xA000) === 0xA000)
|
|
|
389 |
{
|
|
|
390 |
$symbolic = 'l'; // Symbolic Link
|
|
|
391 |
}
|
|
|
392 |
elseif (($perms & 0x8000) === 0x8000)
|
|
|
393 |
{
|
|
|
394 |
$symbolic = '-'; // Regular
|
|
|
395 |
}
|
|
|
396 |
elseif (($perms & 0x6000) === 0x6000)
|
|
|
397 |
{
|
|
|
398 |
$symbolic = 'b'; // Block special
|
|
|
399 |
}
|
|
|
400 |
elseif (($perms & 0x4000) === 0x4000)
|
|
|
401 |
{
|
|
|
402 |
$symbolic = 'd'; // Directory
|
|
|
403 |
}
|
|
|
404 |
elseif (($perms & 0x2000) === 0x2000)
|
|
|
405 |
{
|
|
|
406 |
$symbolic = 'c'; // Character special
|
|
|
407 |
}
|
|
|
408 |
elseif (($perms & 0x1000) === 0x1000)
|
|
|
409 |
{
|
|
|
410 |
$symbolic = 'p'; // FIFO pipe
|
|
|
411 |
}
|
|
|
412 |
else
|
|
|
413 |
{
|
|
|
414 |
$symbolic = 'u'; // Unknown
|
|
|
415 |
}
|
|
|
416 |
|
|
|
417 |
// Owner
|
|
|
418 |
$symbolic .= (($perms & 0x0100) ? 'r' : '-')
|
|
|
419 |
.(($perms & 0x0080) ? 'w' : '-')
|
|
|
420 |
.(($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x' ) : (($perms & 0x0800) ? 'S' : '-'));
|
|
|
421 |
|
|
|
422 |
// Group
|
|
|
423 |
$symbolic .= (($perms & 0x0020) ? 'r' : '-')
|
|
|
424 |
.(($perms & 0x0010) ? 'w' : '-')
|
|
|
425 |
.(($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x' ) : (($perms & 0x0400) ? 'S' : '-'));
|
|
|
426 |
|
|
|
427 |
// World
|
|
|
428 |
$symbolic .= (($perms & 0x0004) ? 'r' : '-')
|
|
|
429 |
.(($perms & 0x0002) ? 'w' : '-')
|
|
|
430 |
.(($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x' ) : (($perms & 0x0200) ? 'T' : '-'));
|
|
|
431 |
|
|
|
432 |
return $symbolic;
|
|
|
433 |
}
|
|
|
434 |
}
|
|
|
435 |
|
|
|
436 |
// --------------------------------------------------------------------
|
|
|
437 |
|
|
|
438 |
if ( ! function_exists('octal_permissions'))
|
|
|
439 |
{
|
|
|
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 |
*
|
|
|
446 |
* @param int $perms Permissions
|
|
|
447 |
* @return string
|
|
|
448 |
*/
|
|
|
449 |
function octal_permissions($perms)
|
|
|
450 |
{
|
|
|
451 |
return substr(sprintf('%o', $perms), -3);
|
|
|
452 |
}
|
|
|
453 |
}
|