PageRenderTime 28ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/system/helpers/file_helper.php

http://github.com/EllisLab/CodeIgniter
PHP | 433 lines | 315 code | 22 blank | 96 comment | 12 complexity | 1e49299e39c4924f7b1c1c24331d4de8 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  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. *
  9. * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
  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/)
  32. * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
  33. * @license https://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. * CodeIgniter File Helpers
  41. *
  42. * @package CodeIgniter
  43. * @subpackage Helpers
  44. * @category Helpers
  45. * @author EllisLab Dev Team
  46. * @link https://codeigniter.com/userguide3/helpers/file_helper.html
  47. */
  48. // ------------------------------------------------------------------------
  49. if ( ! function_exists('write_file'))
  50. {
  51. /**
  52. * Write File
  53. *
  54. * Writes data to the file specified in the path.
  55. * Creates a new file if non-existent.
  56. *
  57. * @param string $path File path
  58. * @param string $data Data to write
  59. * @param string $mode fopen() mode (default: 'wb')
  60. * @return bool
  61. */
  62. function write_file($path, $data, $mode = 'wb')
  63. {
  64. if ( ! $fp = @fopen($path, $mode))
  65. {
  66. return FALSE;
  67. }
  68. flock($fp, LOCK_EX);
  69. for ($result = $written = 0, $length = strlen($data); $written < $length; $written += $result)
  70. {
  71. if (($result = fwrite($fp, substr($data, $written))) === FALSE)
  72. {
  73. break;
  74. }
  75. }
  76. flock($fp, LOCK_UN);
  77. fclose($fp);
  78. return is_int($result);
  79. }
  80. }
  81. // ------------------------------------------------------------------------
  82. if ( ! function_exists('delete_files'))
  83. {
  84. /**
  85. * Delete Files
  86. *
  87. * Deletes all files contained in the supplied directory path.
  88. * Files must be writable or owned by the system in order to be deleted.
  89. * If the second parameter is set to TRUE, any directories contained
  90. * within the supplied base directory will be nuked as well.
  91. *
  92. * @param string $path File path
  93. * @param bool $del_dir Whether to delete any directories found in the path
  94. * @param bool $htdocs Whether to skip deleting .htaccess and index page files
  95. * @param int $_level Current directory depth level (default: 0; internal use only)
  96. * @return bool
  97. */
  98. function delete_files($path, $del_dir = FALSE, $htdocs = FALSE, $_level = 0)
  99. {
  100. // Trim the trailing slash
  101. $path = rtrim($path, '/\\');
  102. if ( ! $current_dir = @opendir($path))
  103. {
  104. return FALSE;
  105. }
  106. while (FALSE !== ($filename = @readdir($current_dir)))
  107. {
  108. if ($filename !== '.' && $filename !== '..')
  109. {
  110. $filepath = $path.DIRECTORY_SEPARATOR.$filename;
  111. if (is_dir($filepath) && $filename[0] !== '.' && ! is_link($filepath))
  112. {
  113. delete_files($filepath, $del_dir, $htdocs, $_level + 1);
  114. }
  115. elseif ($htdocs !== TRUE OR ! preg_match('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i', $filename))
  116. {
  117. @unlink($filepath);
  118. }
  119. }
  120. }
  121. closedir($current_dir);
  122. return ($del_dir === TRUE && $_level > 0)
  123. ? @rmdir($path)
  124. : TRUE;
  125. }
  126. }
  127. // ------------------------------------------------------------------------
  128. if ( ! function_exists('get_filenames'))
  129. {
  130. /**
  131. * Get Filenames
  132. *
  133. * Reads the specified directory and builds an array containing the filenames.
  134. * Any sub-folders contained within the specified path are read as well.
  135. *
  136. * @param string path to source
  137. * @param bool whether to include the path as part of the filename
  138. * @param bool internal variable to determine recursion status - do not use in calls
  139. * @return array
  140. */
  141. function get_filenames($source_dir, $include_path = FALSE, $_recursion = FALSE)
  142. {
  143. static $_filedata = array();
  144. if ($fp = @opendir($source_dir))
  145. {
  146. // reset the array and make sure $source_dir has a trailing slash on the initial call
  147. if ($_recursion === FALSE)
  148. {
  149. $_filedata = array();
  150. $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
  151. }
  152. while (FALSE !== ($file = readdir($fp)))
  153. {
  154. if (is_dir($source_dir.$file) && $file[0] !== '.')
  155. {
  156. get_filenames($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE);
  157. }
  158. elseif ($file[0] !== '.')
  159. {
  160. $_filedata[] = ($include_path === TRUE) ? $source_dir.$file : $file;
  161. }
  162. }
  163. closedir($fp);
  164. return $_filedata;
  165. }
  166. return FALSE;
  167. }
  168. }
  169. // --------------------------------------------------------------------
  170. if ( ! function_exists('get_dir_file_info'))
  171. {
  172. /**
  173. * Get Directory File Information
  174. *
  175. * Reads the specified directory and builds an array containing the filenames,
  176. * filesize, dates, and permissions
  177. *
  178. * Any sub-folders contained within the specified path are read as well.
  179. *
  180. * @param string path to source
  181. * @param bool Look only at the top level directory specified?
  182. * @param bool internal variable to determine recursion status - do not use in calls
  183. * @return array
  184. */
  185. function get_dir_file_info($source_dir, $top_level_only = TRUE, $_recursion = FALSE)
  186. {
  187. static $_filedata = array();
  188. $relative_path = $source_dir;
  189. if ($fp = @opendir($source_dir))
  190. {
  191. // reset the array and make sure $source_dir has a trailing slash on the initial call
  192. if ($_recursion === FALSE)
  193. {
  194. $_filedata = array();
  195. $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
  196. }
  197. // Used to be foreach (scandir($source_dir, 1) as $file), but scandir() is simply not as fast
  198. while (FALSE !== ($file = readdir($fp)))
  199. {
  200. if (is_dir($source_dir.$file) && $file[0] !== '.' && $top_level_only === FALSE)
  201. {
  202. get_dir_file_info($source_dir.$file.DIRECTORY_SEPARATOR, $top_level_only, TRUE);
  203. }
  204. elseif ($file[0] !== '.')
  205. {
  206. $_filedata[$file] = get_file_info($source_dir.$file);
  207. $_filedata[$file]['relative_path'] = $relative_path;
  208. }
  209. }
  210. closedir($fp);
  211. return $_filedata;
  212. }
  213. return FALSE;
  214. }
  215. }
  216. // --------------------------------------------------------------------
  217. if ( ! function_exists('get_file_info'))
  218. {
  219. /**
  220. * Get File Info
  221. *
  222. * Given a file and path, returns the name, path, size, date modified
  223. * Second parameter allows you to explicitly declare what information you want returned
  224. * Options are: name, server_path, size, date, readable, writable, executable, fileperms
  225. * Returns FALSE if the file cannot be found.
  226. *
  227. * @param string path to file
  228. * @param mixed array or comma separated string of information returned
  229. * @return array
  230. */
  231. function get_file_info($file, $returned_values = array('name', 'server_path', 'size', 'date'))
  232. {
  233. if ( ! file_exists($file))
  234. {
  235. return FALSE;
  236. }
  237. if (is_string($returned_values))
  238. {
  239. $returned_values = explode(',', $returned_values);
  240. }
  241. foreach ($returned_values as $key)
  242. {
  243. switch ($key)
  244. {
  245. case 'name':
  246. $fileinfo['name'] = basename($file);
  247. break;
  248. case 'server_path':
  249. $fileinfo['server_path'] = $file;
  250. break;
  251. case 'size':
  252. $fileinfo['size'] = filesize($file);
  253. break;
  254. case 'date':
  255. $fileinfo['date'] = filemtime($file);
  256. break;
  257. case 'readable':
  258. $fileinfo['readable'] = is_readable($file);
  259. break;
  260. case 'writable':
  261. $fileinfo['writable'] = is_really_writable($file);
  262. break;
  263. case 'executable':
  264. $fileinfo['executable'] = is_executable($file);
  265. break;
  266. case 'fileperms':
  267. $fileinfo['fileperms'] = fileperms($file);
  268. break;
  269. }
  270. }
  271. return $fileinfo;
  272. }
  273. }
  274. // --------------------------------------------------------------------
  275. if ( ! function_exists('get_mime_by_extension'))
  276. {
  277. /**
  278. * Get Mime by Extension
  279. *
  280. * Translates a file extension into a mime type based on config/mimes.php.
  281. * Returns FALSE if it can't determine the type, or open the mime config file
  282. *
  283. * Note: this is NOT an accurate way of determining file mime types, and is here strictly as a convenience
  284. * It should NOT be trusted, and should certainly NOT be used for security
  285. *
  286. * @param string $filename File name
  287. * @return string
  288. */
  289. function get_mime_by_extension($filename)
  290. {
  291. static $mimes;
  292. if ( ! is_array($mimes))
  293. {
  294. $mimes = get_mimes();
  295. if (empty($mimes))
  296. {
  297. return FALSE;
  298. }
  299. }
  300. $extension = strtolower(substr(strrchr($filename, '.'), 1));
  301. if (isset($mimes[$extension]))
  302. {
  303. return is_array($mimes[$extension])
  304. ? current($mimes[$extension]) // Multiple mime types, just give the first one
  305. : $mimes[$extension];
  306. }
  307. return FALSE;
  308. }
  309. }
  310. // --------------------------------------------------------------------
  311. if ( ! function_exists('symbolic_permissions'))
  312. {
  313. /**
  314. * Symbolic Permissions
  315. *
  316. * Takes a numeric value representing a file's permissions and returns
  317. * standard symbolic notation representing that value
  318. *
  319. * @param int $perms Permissions
  320. * @return string
  321. */
  322. function symbolic_permissions($perms)
  323. {
  324. if (($perms & 0xC000) === 0xC000)
  325. {
  326. $symbolic = 's'; // Socket
  327. }
  328. elseif (($perms & 0xA000) === 0xA000)
  329. {
  330. $symbolic = 'l'; // Symbolic Link
  331. }
  332. elseif (($perms & 0x8000) === 0x8000)
  333. {
  334. $symbolic = '-'; // Regular
  335. }
  336. elseif (($perms & 0x6000) === 0x6000)
  337. {
  338. $symbolic = 'b'; // Block special
  339. }
  340. elseif (($perms & 0x4000) === 0x4000)
  341. {
  342. $symbolic = 'd'; // Directory
  343. }
  344. elseif (($perms & 0x2000) === 0x2000)
  345. {
  346. $symbolic = 'c'; // Character special
  347. }
  348. elseif (($perms & 0x1000) === 0x1000)
  349. {
  350. $symbolic = 'p'; // FIFO pipe
  351. }
  352. else
  353. {
  354. $symbolic = 'u'; // Unknown
  355. }
  356. // Owner
  357. $symbolic .= (($perms & 0x0100) ? 'r' : '-')
  358. .(($perms & 0x0080) ? 'w' : '-')
  359. .(($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x' ) : (($perms & 0x0800) ? 'S' : '-'));
  360. // Group
  361. $symbolic .= (($perms & 0x0020) ? 'r' : '-')
  362. .(($perms & 0x0010) ? 'w' : '-')
  363. .(($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x' ) : (($perms & 0x0400) ? 'S' : '-'));
  364. // World
  365. $symbolic .= (($perms & 0x0004) ? 'r' : '-')
  366. .(($perms & 0x0002) ? 'w' : '-')
  367. .(($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x' ) : (($perms & 0x0200) ? 'T' : '-'));
  368. return $symbolic;
  369. }
  370. }
  371. // --------------------------------------------------------------------
  372. if ( ! function_exists('octal_permissions'))
  373. {
  374. /**
  375. * Octal Permissions
  376. *
  377. * Takes a numeric value representing a file's permissions and returns
  378. * a three character string representing the file's octal permissions
  379. *
  380. * @param int $perms Permissions
  381. * @return string
  382. */
  383. function octal_permissions($perms)
  384. {
  385. return substr(sprintf('%o', $perms), -3);
  386. }
  387. }