PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/obullo/helpers/file.php

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