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

/core/file.php

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