PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/system/helpers/file_helper.php

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