PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/system/helpers/file_helper.php

https://gitlab.com/RikaPM/manik
PHP | 480 lines | 279 code | 51 blank | 150 comment | 51 complexity | e9bd7f50af39df7a3f50dde574910c96 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author EllisLab Dev Team
  9. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc.
  10. * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
  11. * @license http://codeigniter.com/user_guide/license.html
  12. * @link http://codeigniter.com
  13. * @since Version 1.0
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. /**
  18. * CodeIgniter File Helpers
  19. *
  20. * @package CodeIgniter
  21. * @subpackage Helpers
  22. * @category Helpers
  23. * @author EllisLab Dev Team
  24. * @link http://codeigniter.com/user_guide/helpers/file_helpers.html
  25. */
  26. // ------------------------------------------------------------------------
  27. /**
  28. * Read File
  29. *
  30. * Opens the file specfied in the path and returns it as a string.
  31. *
  32. * @access public
  33. * @param string path to file
  34. * @return string
  35. */
  36. if ( ! function_exists('read_file'))
  37. {
  38. function read_file($file)
  39. {
  40. if ( ! file_exists($file))
  41. {
  42. return FALSE;
  43. }
  44. if (function_exists('file_get_contents'))
  45. {
  46. return file_get_contents($file);
  47. }
  48. if ( ! $fp = @fopen($file, FOPEN_READ))
  49. {
  50. return FALSE;
  51. }
  52. flock($fp, LOCK_SH);
  53. $data = '';
  54. if (filesize($file) > 0)
  55. {
  56. $data =& fread($fp, filesize($file));
  57. }
  58. flock($fp, LOCK_UN);
  59. fclose($fp);
  60. return $data;
  61. }
  62. }
  63. // ------------------------------------------------------------------------
  64. /**
  65. * Write File
  66. *
  67. * Writes data to the file specified in the path.
  68. * Creates a new file if non-existent.
  69. *
  70. * @access public
  71. * @param string path to file
  72. * @param string file data
  73. * @return bool
  74. */
  75. if ( ! function_exists('write_file'))
  76. {
  77. function write_file($path, $data, $mode = FOPEN_WRITE_CREATE_DESTRUCTIVE)
  78. {
  79. if ( ! $fp = @fopen($path, $mode))
  80. {
  81. return FALSE;
  82. }
  83. flock($fp, LOCK_EX);
  84. fwrite($fp, $data);
  85. flock($fp, LOCK_UN);
  86. fclose($fp);
  87. return TRUE;
  88. }
  89. }
  90. // ------------------------------------------------------------------------
  91. /**
  92. * Delete Files
  93. *
  94. * Deletes all files contained in the supplied directory path.
  95. * Files must be writable or owned by the system in order to be deleted.
  96. * If the second parameter is set to TRUE, any directories contained
  97. * within the supplied base directory will be nuked as well.
  98. *
  99. * @access public
  100. * @param string path to file
  101. * @param bool whether to delete any directories found in the path
  102. * @return bool
  103. */
  104. if ( ! function_exists('delete_files'))
  105. {
  106. function delete_files($path, $del_dir = FALSE, $level = 0)
  107. {
  108. // Trim the trailing slash
  109. $path = rtrim($path, DIRECTORY_SEPARATOR);
  110. if ( ! $current_dir = @opendir($path))
  111. {
  112. return FALSE;
  113. }
  114. while (FALSE !== ($filename = @readdir($current_dir)))
  115. {
  116. if ($filename != "." and $filename != "..")
  117. {
  118. if (is_dir($path.DIRECTORY_SEPARATOR.$filename))
  119. {
  120. // Ignore empty folders
  121. if (substr($filename, 0, 1) != '.')
  122. {
  123. delete_files($path.DIRECTORY_SEPARATOR.$filename, $del_dir, $level + 1);
  124. }
  125. }
  126. else
  127. {
  128. unlink($path.DIRECTORY_SEPARATOR.$filename);
  129. }
  130. }
  131. }
  132. @closedir($current_dir);
  133. if ($del_dir == TRUE AND $level > 0)
  134. {
  135. return @rmdir($path);
  136. }
  137. return TRUE;
  138. }
  139. }
  140. // ------------------------------------------------------------------------
  141. /**
  142. * Get Filenames
  143. *
  144. * Reads the specified directory and builds an array containing the filenames.
  145. * Any sub-folders contained within the specified path are read as well.
  146. *
  147. * @access public
  148. * @param string path to source
  149. * @param bool whether to include the path as part of the filename
  150. * @param bool internal variable to determine recursion status - do not use in calls
  151. * @return array
  152. */
  153. if ( ! function_exists('get_filenames'))
  154. {
  155. function get_filenames($source_dir, $include_path = FALSE, $_recursion = FALSE)
  156. {
  157. static $_filedata = array();
  158. if ($fp = @opendir($source_dir))
  159. {
  160. // reset the array and make sure $source_dir has a trailing slash on the initial call
  161. if ($_recursion === FALSE)
  162. {
  163. $_filedata = array();
  164. $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
  165. }
  166. while (FALSE !== ($file = readdir($fp)))
  167. {
  168. if (@is_dir($source_dir.$file) && strncmp($file, '.', 1) !== 0)
  169. {
  170. get_filenames($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE);
  171. }
  172. elseif (strncmp($file, '.', 1) !== 0)
  173. {
  174. $_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file;
  175. }
  176. }
  177. return $_filedata;
  178. }
  179. else
  180. {
  181. return FALSE;
  182. }
  183. }
  184. }
  185. // --------------------------------------------------------------------
  186. /**
  187. * Get Directory File Information
  188. *
  189. * Reads the specified directory and builds an array containing the filenames,
  190. * filesize, dates, and permissions
  191. *
  192. * Any sub-folders contained within the specified path are read as well.
  193. *
  194. * @access public
  195. * @param string path to source
  196. * @param bool Look only at the top level directory specified?
  197. * @param bool internal variable to determine recursion status - do not use in calls
  198. * @return array
  199. */
  200. if ( ! function_exists('get_dir_file_info'))
  201. {
  202. function get_dir_file_info($source_dir, $top_level_only = TRUE, $_recursion = FALSE)
  203. {
  204. static $_filedata = array();
  205. $relative_path = $source_dir;
  206. if ($fp = @opendir($source_dir))
  207. {
  208. // reset the array and make sure $source_dir has a trailing slash on the initial call
  209. if ($_recursion === FALSE)
  210. {
  211. $_filedata = array();
  212. $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
  213. }
  214. // Used to be foreach (scandir($source_dir, 1) as $file), but scandir() is simply not as fast
  215. while (FALSE !== ($file = readdir($fp)))
  216. {
  217. if (@is_dir($source_dir.$file) AND strncmp($file, '.', 1) !== 0 AND $top_level_only === FALSE)
  218. {
  219. get_dir_file_info($source_dir.$file.DIRECTORY_SEPARATOR, $top_level_only, TRUE);
  220. }
  221. elseif (strncmp($file, '.', 1) !== 0)
  222. {
  223. $_filedata[$file] = get_file_info($source_dir.$file);
  224. $_filedata[$file]['relative_path'] = $relative_path;
  225. }
  226. }
  227. return $_filedata;
  228. }
  229. else
  230. {
  231. return FALSE;
  232. }
  233. }
  234. }
  235. // --------------------------------------------------------------------
  236. /**
  237. * Get File Info
  238. *
  239. * Given a file and path, returns the name, path, size, date modified
  240. * Second parameter allows you to explicitly declare what information you want returned
  241. * Options are: name, server_path, size, date, readable, writable, executable, fileperms
  242. * Returns FALSE if the file cannot be found.
  243. *
  244. * @access public
  245. * @param string path to file
  246. * @param mixed array or comma separated string of information returned
  247. * @return array
  248. */
  249. if ( ! function_exists('get_file_info'))
  250. {
  251. function get_file_info($file, $returned_values = array('name', 'server_path', 'size', 'date'))
  252. {
  253. if ( ! file_exists($file))
  254. {
  255. return FALSE;
  256. }
  257. if (is_string($returned_values))
  258. {
  259. $returned_values = explode(',', $returned_values);
  260. }
  261. foreach ($returned_values as $key)
  262. {
  263. switch ($key)
  264. {
  265. case 'name':
  266. $fileinfo['name'] = substr(strrchr($file, DIRECTORY_SEPARATOR), 1);
  267. break;
  268. case 'server_path':
  269. $fileinfo['server_path'] = $file;
  270. break;
  271. case 'size':
  272. $fileinfo['size'] = filesize($file);
  273. break;
  274. case 'date':
  275. $fileinfo['date'] = filemtime($file);
  276. break;
  277. case 'readable':
  278. $fileinfo['readable'] = is_readable($file);
  279. break;
  280. case 'writable':
  281. // There are known problems using is_weritable on IIS. It may not be reliable - consider fileperms()
  282. $fileinfo['writable'] = is_writable($file);
  283. break;
  284. case 'executable':
  285. $fileinfo['executable'] = is_executable($file);
  286. break;
  287. case 'fileperms':
  288. $fileinfo['fileperms'] = fileperms($file);
  289. break;
  290. }
  291. }
  292. return $fileinfo;
  293. }
  294. }
  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. * @access public
  306. * @param string path to file
  307. * @return mixed
  308. */
  309. if ( ! function_exists('get_mime_by_extension'))
  310. {
  311. function get_mime_by_extension($file)
  312. {
  313. $extension = strtolower(substr(strrchr($file, '.'), 1));
  314. global $mimes;
  315. if ( ! is_array($mimes))
  316. {
  317. if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
  318. {
  319. include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
  320. }
  321. elseif (is_file(APPPATH.'config/mimes.php'))
  322. {
  323. include(APPPATH.'config/mimes.php');
  324. }
  325. if ( ! is_array($mimes))
  326. {
  327. return FALSE;
  328. }
  329. }
  330. if (array_key_exists($extension, $mimes))
  331. {
  332. if (is_array($mimes[$extension]))
  333. {
  334. // Multiple mime types, just give the first one
  335. return current($mimes[$extension]);
  336. }
  337. else
  338. {
  339. return $mimes[$extension];
  340. }
  341. }
  342. else
  343. {
  344. return FALSE;
  345. }
  346. }
  347. }
  348. // --------------------------------------------------------------------
  349. /**
  350. * Symbolic Permissions
  351. *
  352. * Takes a numeric value representing a file's permissions and returns
  353. * standard symbolic notation representing that value
  354. *
  355. * @access public
  356. * @param int
  357. * @return string
  358. */
  359. if ( ! function_exists('symbolic_permissions'))
  360. {
  361. function symbolic_permissions($perms)
  362. {
  363. if (($perms & 0xC000) == 0xC000)
  364. {
  365. $symbolic = 's'; // Socket
  366. }
  367. elseif (($perms & 0xA000) == 0xA000)
  368. {
  369. $symbolic = 'l'; // Symbolic Link
  370. }
  371. elseif (($perms & 0x8000) == 0x8000)
  372. {
  373. $symbolic = '-'; // Regular
  374. }
  375. elseif (($perms & 0x6000) == 0x6000)
  376. {
  377. $symbolic = 'b'; // Block special
  378. }
  379. elseif (($perms & 0x4000) == 0x4000)
  380. {
  381. $symbolic = 'd'; // Directory
  382. }
  383. elseif (($perms & 0x2000) == 0x2000)
  384. {
  385. $symbolic = 'c'; // Character special
  386. }
  387. elseif (($perms & 0x1000) == 0x1000)
  388. {
  389. $symbolic = 'p'; // FIFO pipe
  390. }
  391. else
  392. {
  393. $symbolic = 'u'; // Unknown
  394. }
  395. // Owner
  396. $symbolic .= (($perms & 0x0100) ? 'r' : '-');
  397. $symbolic .= (($perms & 0x0080) ? 'w' : '-');
  398. $symbolic .= (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x' ) : (($perms & 0x0800) ? 'S' : '-'));
  399. // Group
  400. $symbolic .= (($perms & 0x0020) ? 'r' : '-');
  401. $symbolic .= (($perms & 0x0010) ? 'w' : '-');
  402. $symbolic .= (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x' ) : (($perms & 0x0400) ? 'S' : '-'));
  403. // World
  404. $symbolic .= (($perms & 0x0004) ? 'r' : '-');
  405. $symbolic .= (($perms & 0x0002) ? 'w' : '-');
  406. $symbolic .= (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x' ) : (($perms & 0x0200) ? 'T' : '-'));
  407. return $symbolic;
  408. }
  409. }
  410. // --------------------------------------------------------------------
  411. /**
  412. * Octal Permissions
  413. *
  414. * Takes a numeric value representing a file's permissions and returns
  415. * a three character string representing the file's octal permissions
  416. *
  417. * @access public
  418. * @param int
  419. * @return string
  420. */
  421. if ( ! function_exists('octal_permissions'))
  422. {
  423. function octal_permissions($perms)
  424. {
  425. return substr(sprintf('%o', $perms), -3);
  426. }
  427. }
  428. /* End of file file_helper.php */
  429. /* Location: ./system/helpers/file_helper.php */