20 defined(
'XOOPS_ROOT_PATH') or die('Restricted access');
103 function __construct(
$path, $create =
false, $mode = 0755)
107 if (!is_dir(
$path)) {
108 $this->name = basename(
$path);
110 if (!$this->exists()) {
111 if ($create ===
true) {
112 if ($this->safe(
$path) && $this->create() ===
false) {
128 function XoopsFileHandler(
$path, $create =
false, $mode = 0755)
130 $this->__construct(
$path, $create, $mode);
138 function __destruct()
151 $dir = $this->folder->pwd();
152 if (is_dir(
$dir) && is_writable(
$dir) && ! $this->exists()) {
153 if (touch($this->pwd())) {
168 function open($mode =
'r', $force =
false)
170 if (!$force && is_resource($this->handle)) {
173 if ($this->exists() ===
false) {
174 if ($this->create() ===
false) {
178 $this->handle = fopen($this->pwd(), $mode);
179 if (is_resource($this->handle)) {
194 function read($bytes =
false, $mode =
'rb', $force =
false)
197 if ($this->lock !== null) {
198 if (flock($this->handle, LOCK_SH) ===
false) {
202 if ($bytes ===
false) {
203 $success = file_get_contents($this->pwd());
204 }
else if ($this->open($mode, $force) ===
true) {
205 if (is_int($bytes)) {
206 $success = fread($this->handle, $bytes);
209 while (! feof($this->handle)) {
210 $data .= fgets($this->handle, 4096);
212 $success = trim($data);
215 if ($this->lock !== null) {
216 flock($this->handle, LOCK_UN);
229 function offset($offset =
false, $seek = SEEK_SET)
231 if ($offset ===
false) {
232 if (is_resource($this->handle)) {
233 return ftell($this->handle);
235 }
else if ($this->open() ===
true) {
236 return fseek($this->handle, $offset, $seek) === 0;
249 function prepare($data)
252 if (substr(PHP_OS, 0, 3) ==
'WIN') {
255 return strtr($data, array(
256 "\r\n" => $lineBreak ,
258 "\r" => $lineBreak));
270 function write($data, $mode =
'w', $force =
false)
273 if ($this->open($mode, $force) ===
true) {
274 if ($this->lock !== null) {
275 if (flock($this->handle, LOCK_EX) ===
false) {
279 if (fwrite($this->handle, $data) !==
false) {
282 if ($this->lock !== null) {
283 flock($this->handle, LOCK_UN);
297 function append($data, $force =
false)
299 return $this->write($data,
'a', $force);
310 if (!is_resource($this->handle)) {
313 return fclose($this->handle);
324 if ($this->exists()) {
325 return unlink($this->pwd());
338 if ($this->info == null) {
339 $this->info = pathinfo($this->pwd());
341 if (!isset($this->info[
'filename'])) {
342 $this->info[
'filename'] = $this->name();
355 if ($this->info == null) {
358 if (isset($this->info[
'extension'])) {
359 return $this->info[
'extension'];
372 if ($this->info == null) {
375 if (isset($this->info[
'extension'])) {
376 return basename($this->name,
'.' . $this->info[
'extension']);
377 } elseif ($this->name) {
390 function safe($name = null,
$ext = null)
398 return preg_replace(
'/[^\w\.-]+/',
'_', basename($name,
$ext));
408 function md5($maxsize = 5)
410 if ($maxsize ===
true) {
411 return md5_file($this->pwd());
413 $size = $this->size();
414 if ($size && $size < ($maxsize * 1024) * 1024) {
415 return md5_file($this->pwd());
429 return $this->folder->slashTerm($this->folder->pwd()) . $this->name;
440 $exists = (file_exists($this->pwd()) && is_file($this->pwd()));
452 if ($this->exists()) {
453 return substr(sprintf(
'%o', fileperms($this->pwd())), - 4);
467 if ($this->exists()) {
468 return filesize($this->pwd());
481 return is_writable($this->pwd());
490 function executable()
492 return is_executable($this->pwd());
503 return is_readable($this->pwd());
513 if ($this->exists()) {
514 return fileowner($this->pwd());
527 if ($this->exists()) {
528 return filegroup($this->pwd());
539 function lastAccess()
541 if ($this->exists()) {
542 return fileatime($this->pwd());
553 function lastChange()
555 if ($this->exists()) {
556 return filemtime($this->pwd());
569 return $this->folder;