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