XOOPS  2.6.0
file.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 */
11 
12 /*
13  * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
14  * Copyright 2005-2008, Cake Software Foundation, Inc.
15  * 1785 E. Sahara Avenue, Suite 490-204
16  * Las Vegas, Nevada 89104
17  *
18  * Licensed under The MIT License
19  * Redistributions of files must retain the above copyright notice.
20  *
21  */
22 
38 {
45  public $folder = null;
46 
53  public $name = null;
54 
61  public $info = array();
62 
69  public $handle = null;
70 
77  public $lock = null;
78 
88  public function __construct($path, $create = false, $mode = 0755)
89  {
90  $this->folder = XoopsFile::getHandler('folder', dirname($path), $create, $mode);
91  if (!is_dir($path)) {
92  $this->name = basename($path);
93  } else {
94  return false;
95  }
96  if (!$this->exists()) {
97  if ($create === true) {
98  if ($this->safe($path) && $this->create() === false) {
99  return false;
100  }
101  } else {
102  return false;
103  }
104  }
105  return true;
106  }
107 
108 
112  public function __destruct()
113  {
114  $this->close();
115  }
116 
123  public function create()
124  {
125  $dir = $this->folder->pwd();
126  if (is_dir($dir) && is_writable($dir) && !$this->exists()) {
127  if (touch($this->pwd())) {
128  return true;
129  }
130  }
131  return false;
132  }
133 
144  public function open($mode = 'r', $force = false)
145  {
146  if (!$force && is_resource($this->handle)) {
147  return true;
148  }
149  if ($this->exists() === false) {
150  if ($this->create() === false) {
151  return false;
152  }
153  }
154  $this->handle = fopen($this->pwd(), $mode);
155  if (is_resource($this->handle)) {
156  return true;
157  }
158  return false;
159  }
160 
171  public function read($bytes = false, $mode = 'rb', $force = false)
172  {
173  $success = false;
174  if ($this->lock !== null) {
175  if (flock($this->handle, LOCK_SH) === false) {
176  return false;
177  }
178  }
179  if ($bytes === false) {
180  $success = file_get_contents($this->pwd());
181  } else {
182  if ($this->open($mode, $force) === true) {
183  if (is_int($bytes)) {
184  $success = fread($this->handle, $bytes);
185  } else {
186  $data = '';
187  while (!feof($this->handle)) {
188  $data .= fgets($this->handle, 4096);
189  }
190  $success = trim($data);
191  }
192  }
193  }
194  if ($this->lock !== null) {
195  flock($this->handle, LOCK_UN);
196  }
197  return $success;
198  }
199 
210  public function offset($offset = false, $seek = SEEK_SET)
211  {
212  if ($offset === false) {
213  if (is_resource($this->handle)) {
214  return ftell($this->handle);
215  }
216  } else {
217  if ($this->open() === true) {
218  return fseek($this->handle, $offset, $seek) === 0;
219  }
220  }
221  return false;
222  }
223 
233  public function prepare($data)
234  {
235  $lineBreak = "\n";
236  if (substr(PHP_OS, 0, 3) == 'WIN') {
237  $lineBreak = "\r\n";
238  }
239  return strtr($data, array("\r\n" => $lineBreak, "\n" => $lineBreak, "\r" => $lineBreak));
240  }
241 
252  public function write($data, $mode = 'w', $force = false)
253  {
254  $success = false;
255  if ($this->open($mode, $force) === true) {
256  if ($this->lock !== null) {
257  if (flock($this->handle, LOCK_EX) === false) {
258  return false;
259  }
260  }
261  if (fwrite($this->handle, $data) !== false) {
262  $success = true;
263  }
264  if ($this->lock !== null) {
265  flock($this->handle, LOCK_UN);
266  }
267  }
268  return $success;
269  }
270 
280  public function append($data, $force = false)
281  {
282  return $this->write($data, 'a', $force);
283  }
284 
291  public function close()
292  {
293  if (!is_resource($this->handle)) {
294  return true;
295  }
296  return fclose($this->handle);
297  }
298 
305  public function delete()
306  {
307  if ($this->exists()) {
308  return unlink($this->pwd());
309  }
310  return false;
311  }
312 
319  public function info()
320  {
321  if ($this->info == null) {
322  $this->info = pathinfo($this->pwd());
323  }
324  if (!isset($this->info['filename'])) {
325  $this->info['filename'] = $this->name();
326  }
327  return $this->info;
328  }
329 
336  public function ext()
337  {
338  if ($this->info == null) {
339  $this->info();
340  }
341  if (isset($this->info['extension'])) {
342  return $this->info['extension'];
343  }
344  return false;
345  }
346 
353  public function name()
354  {
355  if ($this->info == null) {
356  $this->info();
357  }
358  if (isset($this->info['extension'])) {
359  return basename($this->name, '.' . $this->info['extension']);
360  } elseif ($this->name) {
361  return $this->name;
362  }
363  return false;
364  }
365 
374  public function safe($name = null, $ext = null)
375  {
376  if (!$name) {
377  $name = $this->name;
378  }
379  if (!$ext) {
380  $ext = $this->ext();
381  }
382  return preg_replace('/[^\w\.-]+/', '_', basename($name, $ext));
383  }
384 
393  public function md5($maxsize = 5)
394  {
395  if ($maxsize === true) {
396  return md5_file($this->pwd());
397  } else {
398  $size = $this->size();
399  if ($size && $size < ($maxsize * 1024) * 1024) {
400  return md5_file($this->pwd());
401  }
402  }
403  return false;
404  }
405 
412  public function pwd()
413  {
414  return $this->folder->slashTerm($this->folder->pwd()) . $this->name;
415  }
416 
423  public function exists()
424  {
425  $exists = is_file($this->pwd());
426  return $exists;
427  }
428 
435  public function perms()
436  {
437  if ($this->exists()) {
438  return substr(sprintf('%o', fileperms($this->pwd())), -4);
439  }
440  return false;
441  }
442 
449  public function size()
450  {
451  if ($this->exists()) {
452  return filesize($this->pwd());
453  }
454  return false;
455  }
456 
463  public function writable()
464  {
465  return is_writable($this->pwd());
466  }
467 
474  public function executable()
475  {
476  return is_executable($this->pwd());
477  }
478 
485  public function readable()
486  {
487  return is_readable($this->pwd());
488  }
489 
495  public function owner()
496  {
497  if ($this->exists()) {
498  return fileowner($this->pwd());
499  }
500  return false;
501  }
502 
509  public function group()
510  {
511  if ($this->exists()) {
512  return filegroup($this->pwd());
513  }
514  return false;
515  }
516 
523  public function lastAccess()
524  {
525  if ($this->exists()) {
526  return fileatime($this->pwd());
527  }
528  return false;
529  }
530 
537  public function lastChange()
538  {
539  if ($this->exists()) {
540  return filemtime($this->pwd());
541  }
542  return false;
543  }
544 
551  public function folder()
552  {
553  return $this->folder;
554  }
555 }
$path
Definition: execute.php:31
static getHandler($name= 'file', $path=false, $create=false, $mode=null)
Definition: xoopsfile.php:40
read($bytes=false, $mode= 'rb', $force=false)
Definition: file.php:171
append($data, $force=false)
Definition: file.php:280
offset($offset=false, $seek=SEEK_SET)
Definition: file.php:210
open($mode= 'r', $force=false)
Definition: file.php:144
safe($name=null, $ext=null)
Definition: file.php:374
$ext
Definition: browse.php:87
__construct($path, $create=false, $mode=0755)
Definition: file.php:88
md5($maxsize=5)
Definition: file.php:393
prepare($data)
Definition: file.php:233
$dir
Definition: browse.php:56
write($data, $mode= 'w', $force=false)
Definition: file.php:252