PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/cake/libs/file.php

http://github.com/mfriesen/kaching-php
PHP | 544 lines | 370 code | 23 blank | 151 comment | 35 complexity | a8c1b6f116fe4519a15547803440406c MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Convenience class for reading, writing and appending to files.
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package cake
  16. * @subpackage cake.cake.libs
  17. * @since CakePHP(tm) v 0.2.9
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. /**
  21. * Included libraries.
  22. *
  23. */
  24. if (!class_exists('Object')) {
  25. require LIBS . 'object.php';
  26. }
  27. if (!class_exists('Folder')) {
  28. require LIBS . 'folder.php';
  29. }
  30. /**
  31. * Convenience class for reading, writing and appending to files.
  32. *
  33. * @package cake
  34. * @subpackage cake.cake.libs
  35. */
  36. class File extends Object {
  37. /**
  38. * Folder object of the File
  39. *
  40. * @var Folder
  41. * @access public
  42. */
  43. var $Folder = null;
  44. /**
  45. * Filename
  46. *
  47. * @var string
  48. * @access public
  49. */
  50. var $name = null;
  51. /**
  52. * file info
  53. *
  54. * @var string
  55. * @access public
  56. */
  57. var $info = array();
  58. /**
  59. * Holds the file handler resource if the file is opened
  60. *
  61. * @var resource
  62. * @access public
  63. */
  64. var $handle = null;
  65. /**
  66. * enable locking for file reading and writing
  67. *
  68. * @var boolean
  69. * @access public
  70. */
  71. var $lock = null;
  72. /**
  73. * path property
  74. *
  75. * Current file's absolute path
  76. *
  77. * @var mixed null
  78. * @access public
  79. */
  80. var $path = null;
  81. /**
  82. * Constructor
  83. *
  84. * @param string $path Path to file
  85. * @param boolean $create Create file if it does not exist (if true)
  86. * @param integer $mode Mode to apply to the folder holding the file
  87. * @access private
  88. */
  89. function __construct($path, $create = false, $mode = 0755) {
  90. parent::__construct();
  91. $this->Folder =& new Folder(dirname($path), $create, $mode);
  92. if (!is_dir($path)) {
  93. $this->name = basename($path);
  94. }
  95. $this->pwd();
  96. !$this->exists() && $create && $this->safe($path) && $this->create();
  97. }
  98. /**
  99. * Closes the current file if it is opened
  100. *
  101. * @access private
  102. */
  103. function __destruct() {
  104. $this->close();
  105. }
  106. /**
  107. * Creates the File.
  108. *
  109. * @return boolean Success
  110. * @access public
  111. */
  112. function create() {
  113. $dir = $this->Folder->pwd();
  114. if (is_dir($dir) && is_writable($dir) && !$this->exists()) {
  115. $old = umask(0);
  116. if (touch($this->path)) {
  117. umask($old);
  118. return true;
  119. }
  120. }
  121. return false;
  122. }
  123. /**
  124. * Opens the current file with a given $mode
  125. *
  126. * @param string $mode A valid 'fopen' mode string (r|w|a ...)
  127. * @param boolean $force If true then the file will be re-opened even if its already opened, otherwise it won't
  128. * @return boolean True on success, false on failure
  129. * @access public
  130. */
  131. function open($mode = 'r', $force = false) {
  132. if (!$force && is_resource($this->handle)) {
  133. return true;
  134. }
  135. clearstatcache();
  136. if ($this->exists() === false) {
  137. if ($this->create() === false) {
  138. return false;
  139. }
  140. }
  141. $this->handle = fopen($this->path, $mode);
  142. if (is_resource($this->handle)) {
  143. return true;
  144. }
  145. return false;
  146. }
  147. /**
  148. * Return the contents of this File as a string.
  149. *
  150. * @param string $bytes where to start
  151. * @param string $mode A `fread` compatible mode.
  152. * @param boolean $force If true then the file will be re-opened even if its already opened, otherwise it won't
  153. * @return mixed string on success, false on failure
  154. * @access public
  155. */
  156. function read($bytes = false, $mode = 'rb', $force = false) {
  157. if ($bytes === false && $this->lock === null) {
  158. return file_get_contents($this->path);
  159. }
  160. if ($this->open($mode, $force) === false) {
  161. return false;
  162. }
  163. if ($this->lock !== null && flock($this->handle, LOCK_SH) === false) {
  164. return false;
  165. }
  166. if (is_int($bytes)) {
  167. return fread($this->handle, $bytes);
  168. }
  169. $data = '';
  170. while (!feof($this->handle)) {
  171. $data .= fgets($this->handle, 4096);
  172. }
  173. if ($this->lock !== null) {
  174. flock($this->handle, LOCK_UN);
  175. }
  176. if ($bytes === false) {
  177. $this->close();
  178. }
  179. return trim($data);
  180. }
  181. /**
  182. * Sets or gets the offset for the currently opened file.
  183. *
  184. * @param mixed $offset The $offset in bytes to seek. If set to false then the current offset is returned.
  185. * @param integer $seek PHP Constant SEEK_SET | SEEK_CUR | SEEK_END determining what the $offset is relative to
  186. * @return mixed True on success, false on failure (set mode), false on failure or integer offset on success (get mode)
  187. * @access public
  188. */
  189. function offset($offset = false, $seek = SEEK_SET) {
  190. if ($offset === false) {
  191. if (is_resource($this->handle)) {
  192. return ftell($this->handle);
  193. }
  194. } elseif ($this->open() === true) {
  195. return fseek($this->handle, $offset, $seek) === 0;
  196. }
  197. return false;
  198. }
  199. /**
  200. * Prepares a ascii string for writing. Converts line endings to the
  201. * correct terminator for the current platform. If windows "\r\n" will be used
  202. * all other platforms will use "\n"
  203. *
  204. * @param string $data Data to prepare for writing.
  205. * @return string The with converted line endings.
  206. * @access public
  207. */
  208. function prepare($data, $forceWindows = false) {
  209. $lineBreak = "\n";
  210. if (DIRECTORY_SEPARATOR == '\\' || $forceWindows === true) {
  211. $lineBreak = "\r\n";
  212. }
  213. return strtr($data, array("\r\n" => $lineBreak, "\n" => $lineBreak, "\r" => $lineBreak));
  214. }
  215. /**
  216. * Write given data to this File.
  217. *
  218. * @param string $data Data to write to this File.
  219. * @param string $mode Mode of writing. {@link http://php.net/fwrite See fwrite()}.
  220. * @param string $force force the file to open
  221. * @return boolean Success
  222. * @access public
  223. */
  224. function write($data, $mode = 'w', $force = false) {
  225. $success = false;
  226. if ($this->open($mode, $force) === true) {
  227. if ($this->lock !== null) {
  228. if (flock($this->handle, LOCK_EX) === false) {
  229. return false;
  230. }
  231. }
  232. if (fwrite($this->handle, $data) !== false) {
  233. $success = true;
  234. }
  235. if ($this->lock !== null) {
  236. flock($this->handle, LOCK_UN);
  237. }
  238. }
  239. return $success;
  240. }
  241. /**
  242. * Append given data string to this File.
  243. *
  244. * @param string $data Data to write
  245. * @param string $force force the file to open
  246. * @return boolean Success
  247. * @access public
  248. */
  249. function append($data, $force = false) {
  250. return $this->write($data, 'a', $force);
  251. }
  252. /**
  253. * Closes the current file if it is opened.
  254. *
  255. * @return boolean True if closing was successful or file was already closed, otherwise false
  256. * @access public
  257. */
  258. function close() {
  259. if (!is_resource($this->handle)) {
  260. return true;
  261. }
  262. return fclose($this->handle);
  263. }
  264. /**
  265. * Deletes the File.
  266. *
  267. * @return boolean Success
  268. * @access public
  269. */
  270. function delete() {
  271. clearstatcache();
  272. if ($this->exists()) {
  273. return unlink($this->path);
  274. }
  275. return false;
  276. }
  277. /**
  278. * Returns the File info.
  279. *
  280. * @return string The File extension
  281. * @access public
  282. */
  283. function info() {
  284. if ($this->info == null) {
  285. $this->info = pathinfo($this->path);
  286. }
  287. if (!isset($this->info['filename'])) {
  288. $this->info['filename'] = $this->name();
  289. }
  290. return $this->info;
  291. }
  292. /**
  293. * Returns the File extension.
  294. *
  295. * @return string The File extension
  296. * @access public
  297. */
  298. function ext() {
  299. if ($this->info == null) {
  300. $this->info();
  301. }
  302. if (isset($this->info['extension'])) {
  303. return $this->info['extension'];
  304. }
  305. return false;
  306. }
  307. /**
  308. * Returns the File name without extension.
  309. *
  310. * @return string The File name without extension.
  311. * @access public
  312. */
  313. function name() {
  314. if ($this->info == null) {
  315. $this->info();
  316. }
  317. if (isset($this->info['extension'])) {
  318. return basename($this->name, '.'.$this->info['extension']);
  319. } elseif ($this->name) {
  320. return $this->name;
  321. }
  322. return false;
  323. }
  324. /**
  325. * makes filename safe for saving
  326. *
  327. * @param string $name The name of the file to make safe if different from $this->name
  328. * @param strin $ext The name of the extension to make safe if different from $this->ext
  329. * @return string $ext the extension of the file
  330. * @access public
  331. */
  332. function safe($name = null, $ext = null) {
  333. if (!$name) {
  334. $name = $this->name;
  335. }
  336. if (!$ext) {
  337. $ext = $this->ext();
  338. }
  339. return preg_replace( "/(?:[^\w\.-]+)/", "_", basename($name, $ext));
  340. }
  341. /**
  342. * Get md5 Checksum of file with previous check of Filesize
  343. *
  344. * @param mixed $maxsize in MB or true to force
  345. * @return string md5 Checksum {@link http://php.net/md5_file See md5_file()}
  346. * @access public
  347. */
  348. function md5($maxsize = 5) {
  349. if ($maxsize === true) {
  350. return md5_file($this->path);
  351. }
  352. $size = $this->size();
  353. if ($size && $size < ($maxsize * 1024) * 1024) {
  354. return md5_file($this->path);
  355. }
  356. return false;
  357. }
  358. /**
  359. * Returns the full path of the File.
  360. *
  361. * @return string Full path to file
  362. * @access public
  363. */
  364. function pwd() {
  365. if (is_null($this->path)) {
  366. $this->path = $this->Folder->slashTerm($this->Folder->pwd()) . $this->name;
  367. }
  368. return $this->path;
  369. }
  370. /**
  371. * Returns true if the File exists.
  372. *
  373. * @return boolean true if it exists, false otherwise
  374. * @access public
  375. */
  376. function exists() {
  377. return (file_exists($this->path) && is_file($this->path));
  378. }
  379. /**
  380. * Returns the "chmod" (permissions) of the File.
  381. *
  382. * @return string Permissions for the file
  383. * @access public
  384. */
  385. function perms() {
  386. if ($this->exists()) {
  387. return substr(sprintf('%o', fileperms($this->path)), -4);
  388. }
  389. return false;
  390. }
  391. /**
  392. * Returns the Filesize
  393. *
  394. * @return integer size of the file in bytes, or false in case of an error
  395. * @access public
  396. */
  397. function size() {
  398. if ($this->exists()) {
  399. return filesize($this->path);
  400. }
  401. return false;
  402. }
  403. /**
  404. * Returns true if the File is writable.
  405. *
  406. * @return boolean true if its writable, false otherwise
  407. * @access public
  408. */
  409. function writable() {
  410. return is_writable($this->path);
  411. }
  412. /**
  413. * Returns true if the File is executable.
  414. *
  415. * @return boolean true if its executable, false otherwise
  416. * @access public
  417. */
  418. function executable() {
  419. return is_executable($this->path);
  420. }
  421. /**
  422. * Returns true if the File is readable.
  423. *
  424. * @return boolean true if file is readable, false otherwise
  425. * @access public
  426. */
  427. function readable() {
  428. return is_readable($this->path);
  429. }
  430. /**
  431. * Returns the File's owner.
  432. *
  433. * @return integer the Fileowner
  434. * @access public
  435. */
  436. function owner() {
  437. if ($this->exists()) {
  438. return fileowner($this->path);
  439. }
  440. return false;
  441. }
  442. /**
  443. * Returns the File's group.
  444. *
  445. * @return integer the Filegroup
  446. * @access public
  447. */
  448. function group() {
  449. if ($this->exists()) {
  450. return filegroup($this->path);
  451. }
  452. return false;
  453. }
  454. /**
  455. * Returns last access time.
  456. *
  457. * @return integer timestamp Timestamp of last access time
  458. * @access public
  459. */
  460. function lastAccess() {
  461. if ($this->exists()) {
  462. return fileatime($this->path);
  463. }
  464. return false;
  465. }
  466. /**
  467. * Returns last modified time.
  468. *
  469. * @return integer timestamp Timestamp of last modification
  470. * @access public
  471. */
  472. function lastChange() {
  473. if ($this->exists()) {
  474. return filemtime($this->path);
  475. }
  476. return false;
  477. }
  478. /**
  479. * Returns the current folder.
  480. *
  481. * @return Folder Current folder
  482. * @access public
  483. */
  484. function &Folder() {
  485. return $this->Folder;
  486. }
  487. /**
  488. * Copy the File to $dest
  489. *
  490. * @param string $dest destination for the copy
  491. * @param boolean $overwrite Overwrite $dest if exists
  492. * @return boolean Succes
  493. * @access public
  494. */
  495. function copy($dest, $overwrite = true) {
  496. if (!$this->exists() || is_file($dest) && !$overwrite) {
  497. return false;
  498. }
  499. return copy($this->path, $dest);
  500. }
  501. }