Linux multi-monitor fullscreen support
[ryzomcore.git] / web / public_php / webtt / cake / libs / file.php
blob86755a513f360ecf28ef485dab9c35a48f23c988
1 <?php
2 /**
3 * Convenience class for reading, writing and appending to files.
5 * PHP versions 4 and 5
7 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
10 * Licensed under The MIT License
11 * Redistributions of files must retain the above copyright notice.
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)
21 /**
22 * Included libraries.
25 if (!class_exists('Object')) {
26 require LIBS . 'object.php';
28 if (!class_exists('Folder')) {
29 require LIBS . 'folder.php';
32 /**
33 * Convenience class for reading, writing and appending to files.
35 * @package cake
36 * @subpackage cake.cake.libs
38 class File extends Object {
40 /**
41 * Folder object of the File
43 * @var Folder
44 * @access public
46 var $Folder = null;
48 /**
49 * Filename
51 * @var string
52 * @access public
54 var $name = null;
56 /**
57 * file info
59 * @var string
60 * @access public
62 var $info = array();
64 /**
65 * Holds the file handler resource if the file is opened
67 * @var resource
68 * @access public
70 var $handle = null;
72 /**
73 * enable locking for file reading and writing
75 * @var boolean
76 * @access public
78 var $lock = null;
80 /**
81 * path property
83 * Current file's absolute path
85 * @var mixed null
86 * @access public
88 var $path = null;
90 /**
91 * Constructor
93 * @param string $path Path to file
94 * @param boolean $create Create file if it does not exist (if true)
95 * @param integer $mode Mode to apply to the folder holding the file
97 function __construct($path, $create = false, $mode = 0755) {
98 parent::__construct();
99 $this->Folder =& new Folder(dirname($path), $create, $mode);
100 if (!is_dir($path)) {
101 $this->name = basename($path);
103 $this->pwd();
104 $create && !$this->exists() && $this->safe($path) && $this->create();
108 * Closes the current file if it is opened
111 function __destruct() {
112 $this->close();
116 * Creates the File.
118 * @return boolean Success
119 * @access public
121 function create() {
122 $dir = $this->Folder->pwd();
123 if (is_dir($dir) && is_writable($dir) && !$this->exists()) {
124 $old = umask(0);
125 if (touch($this->path)) {
126 umask($old);
127 return true;
130 return false;
134 * Opens the current file with a given $mode
136 * @param string $mode A valid 'fopen' mode string (r|w|a ...)
137 * @param boolean $force If true then the file will be re-opened even if its already opened, otherwise it won't
138 * @return boolean True on success, false on failure
139 * @access public
141 function open($mode = 'r', $force = false) {
142 if (!$force && is_resource($this->handle)) {
143 return true;
145 clearstatcache();
146 if ($this->exists() === false) {
147 if ($this->create() === false) {
148 return false;
152 $this->handle = fopen($this->path, $mode);
153 if (is_resource($this->handle)) {
154 return true;
156 return false;
160 * Return the contents of this File as a string.
162 * @param string $bytes where to start
163 * @param string $mode A `fread` compatible mode.
164 * @param boolean $force If true then the file will be re-opened even if its already opened, otherwise it won't
165 * @return mixed string on success, false on failure
166 * @access public
168 function read($bytes = false, $mode = 'rb', $force = false) {
169 if ($bytes === false && $this->lock === null) {
170 return file_get_contents($this->path);
172 if ($this->open($mode, $force) === false) {
173 return false;
175 if ($this->lock !== null && flock($this->handle, LOCK_SH) === false) {
176 return false;
178 if (is_int($bytes)) {
179 return fread($this->handle, $bytes);
182 $data = '';
183 while (!feof($this->handle)) {
184 $data .= fgets($this->handle, 4096);
187 if ($this->lock !== null) {
188 flock($this->handle, LOCK_UN);
190 if ($bytes === false) {
191 $this->close();
193 return trim($data);
197 * Sets or gets the offset for the currently opened file.
199 * @param mixed $offset The $offset in bytes to seek. If set to false then the current offset is returned.
200 * @param integer $seek PHP Constant SEEK_SET | SEEK_CUR | SEEK_END determining what the $offset is relative to
201 * @return mixed True on success, false on failure (set mode), false on failure or integer offset on success (get mode)
202 * @access public
204 function offset($offset = false, $seek = SEEK_SET) {
205 if ($offset === false) {
206 if (is_resource($this->handle)) {
207 return ftell($this->handle);
209 } elseif ($this->open() === true) {
210 return fseek($this->handle, $offset, $seek) === 0;
212 return false;
216 * Prepares a ascii string for writing. Converts line endings to the
217 * correct terminator for the current platform. If windows "\r\n" will be used
218 * all other platforms will use "\n"
220 * @param string $data Data to prepare for writing.
221 * @return string The with converted line endings.
222 * @access public
224 function prepare($data, $forceWindows = false) {
225 $lineBreak = "\n";
226 if (DIRECTORY_SEPARATOR == '\\' || $forceWindows === true) {
227 $lineBreak = "\r\n";
229 return strtr($data, array("\r\n" => $lineBreak, "\n" => $lineBreak, "\r" => $lineBreak));
233 * Write given data to this File.
235 * @param string $data Data to write to this File.
236 * @param string $mode Mode of writing. {@link http://php.net/fwrite See fwrite()}.
237 * @param string $force force the file to open
238 * @return boolean Success
239 * @access public
241 function write($data, $mode = 'w', $force = false) {
242 $success = false;
243 if ($this->open($mode, $force) === true) {
244 if ($this->lock !== null) {
245 if (flock($this->handle, LOCK_EX) === false) {
246 return false;
250 if (fwrite($this->handle, $data) !== false) {
251 $success = true;
253 if ($this->lock !== null) {
254 flock($this->handle, LOCK_UN);
257 return $success;
261 * Append given data string to this File.
263 * @param string $data Data to write
264 * @param string $force force the file to open
265 * @return boolean Success
266 * @access public
268 function append($data, $force = false) {
269 return $this->write($data, 'a', $force);
273 * Closes the current file if it is opened.
275 * @return boolean True if closing was successful or file was already closed, otherwise false
276 * @access public
278 function close() {
279 if (!is_resource($this->handle)) {
280 return true;
282 return fclose($this->handle);
286 * Deletes the File.
288 * @return boolean Success
289 * @access public
291 function delete() {
292 clearstatcache();
293 if ($this->exists()) {
294 return unlink($this->path);
296 return false;
300 * Returns the File info.
302 * @return string The File extension
303 * @access public
305 function info() {
306 if ($this->info == null) {
307 $this->info = pathinfo($this->path);
309 if (!isset($this->info['filename'])) {
310 $this->info['filename'] = $this->name();
312 return $this->info;
316 * Returns the File extension.
318 * @return string The File extension
319 * @access public
321 function ext() {
322 if ($this->info == null) {
323 $this->info();
325 if (isset($this->info['extension'])) {
326 return $this->info['extension'];
328 return false;
332 * Returns the File name without extension.
334 * @return string The File name without extension.
335 * @access public
337 function name() {
338 if ($this->info == null) {
339 $this->info();
341 if (isset($this->info['extension'])) {
342 return basename($this->name, '.'.$this->info['extension']);
343 } elseif ($this->name) {
344 return $this->name;
346 return false;
350 * makes filename safe for saving
352 * @param string $name The name of the file to make safe if different from $this->name
353 * @param strin $ext The name of the extension to make safe if different from $this->ext
354 * @return string $ext the extension of the file
355 * @access public
357 function safe($name = null, $ext = null) {
358 if (!$name) {
359 $name = $this->name;
361 if (!$ext) {
362 $ext = $this->ext();
364 return preg_replace( "/(?:[^\w\.-]+)/", "_", basename($name, $ext));
368 * Get md5 Checksum of file with previous check of Filesize
370 * @param mixed $maxsize in MB or true to force
371 * @return string md5 Checksum {@link http://php.net/md5_file See md5_file()}
372 * @access public
374 function md5($maxsize = 5) {
375 if ($maxsize === true) {
376 return md5_file($this->path);
379 $size = $this->size();
380 if ($size && $size < ($maxsize * 1024) * 1024) {
381 return md5_file($this->path);
384 return false;
388 * Returns the full path of the File.
390 * @return string Full path to file
391 * @access public
393 function pwd() {
394 if (is_null($this->path)) {
395 $this->path = $this->Folder->slashTerm($this->Folder->pwd()) . $this->name;
397 return $this->path;
401 * Returns true if the File exists.
403 * @return boolean true if it exists, false otherwise
404 * @access public
406 function exists() {
407 return (file_exists($this->path) && is_file($this->path));
411 * Returns the "chmod" (permissions) of the File.
413 * @return string Permissions for the file
414 * @access public
416 function perms() {
417 if ($this->exists()) {
418 return substr(sprintf('%o', fileperms($this->path)), -4);
420 return false;
424 * Returns the Filesize
426 * @return integer size of the file in bytes, or false in case of an error
427 * @access public
429 function size() {
430 if ($this->exists()) {
431 return filesize($this->path);
433 return false;
437 * Returns true if the File is writable.
439 * @return boolean true if its writable, false otherwise
440 * @access public
442 function writable() {
443 return is_writable($this->path);
447 * Returns true if the File is executable.
449 * @return boolean true if its executable, false otherwise
450 * @access public
452 function executable() {
453 return is_executable($this->path);
457 * Returns true if the File is readable.
459 * @return boolean true if file is readable, false otherwise
460 * @access public
462 function readable() {
463 return is_readable($this->path);
467 * Returns the File's owner.
469 * @return integer the Fileowner
470 * @access public
472 function owner() {
473 if ($this->exists()) {
474 return fileowner($this->path);
476 return false;
480 * Returns the File's group.
482 * @return integer the Filegroup
483 * @access public
485 function group() {
486 if ($this->exists()) {
487 return filegroup($this->path);
489 return false;
493 * Returns last access time.
495 * @return integer timestamp Timestamp of last access time
496 * @access public
498 function lastAccess() {
499 if ($this->exists()) {
500 return fileatime($this->path);
502 return false;
506 * Returns last modified time.
508 * @return integer timestamp Timestamp of last modification
509 * @access public
511 function lastChange() {
512 if ($this->exists()) {
513 return filemtime($this->path);
515 return false;
519 * Returns the current folder.
521 * @return Folder Current folder
522 * @access public
524 function &Folder() {
525 return $this->Folder;
529 * Copy the File to $dest
531 * @param string $dest destination for the copy
532 * @param boolean $overwrite Overwrite $dest if exists
533 * @return boolean Succes
534 * @access public
536 function copy($dest, $overwrite = true) {
537 if (!$this->exists() || is_file($dest) && !$overwrite) {
538 return false;
540 return copy($this->path, $dest);