PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/system/helpers/file_helper.php

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