10 defined(
'JPATH_PLATFORM') or die;
38 protected $filemode = 0644;
46 protected $dirmode = 0755;
54 protected $chunksize = 8192;
87 protected $processingmethod =
'f';
95 protected $filters = array();
119 protected $context = null;
146 public function __construct($writeprefix =
'', $readprefix =
'', $context = array())
148 $this->writeprefix = $writeprefix;
149 $this->readprefix = $readprefix;
150 $this->contextOptions = $context;
151 $this->_buildContext();
159 public function __destruct()
186 public function open($filename, $mode =
'r', $use_include_path =
false, $context = null,
187 $use_prefix =
false, $relative =
false, $detectprocessingmode =
false)
189 $filename = $this->_getFilename($filename, $mode, $use_prefix, $relative);
193 $this->setError(
JText::_(
'JLIB_FILESYSTEM_ERROR_STREAMS_FILENAME'));
198 $this->filename = $filename;
199 $this->openmode = $mode;
201 $url = parse_url($filename);
204 if (isset($url[
'scheme']))
209 require_once __DIR__ .
'/streams/' . $url[
'scheme'] .
'.php';
213 $this->processingmethod =
'f';
215 elseif ($detectprocessingmode)
224 $this->processingmethod =
'gz';
230 $this->processingmethod =
'bz';
234 $this->processingmethod =
'f';
240 $php_errormsg =
'Error Unknown whilst opening a file';
241 $track_errors = ini_get(
'track_errors');
242 ini_set(
'track_errors',
true);
245 switch ($this->processingmethod)
249 $this->fh = gzopen($filename, $mode, $use_include_path);
254 $this->fh = bzopen($filename, $mode);
263 $this->fh = fopen($filename, $mode, $use_include_path, $context);
266 elseif ($this->context)
268 $this->fh = fopen($filename, $mode, $use_include_path, $this->context);
273 $this->fh = fopen($filename, $mode, $use_include_path);
281 $this->setError($php_errormsg);
289 ini_set(
'track_errors', $track_errors);
305 public function close()
309 $this->setError(
JText::_(
'JLIB_FILESYSTEM_ERROR_STREAMS_FILE_NOT_OPEN'));
317 $php_errormsg =
'Error Unknown';
318 $track_errors = ini_get(
'track_errors');
319 ini_set(
'track_errors',
true);
321 switch ($this->processingmethod)
324 $res = gzclose($this->fh);
328 $res = bzclose($this->fh);
333 $res = fclose($this->fh);
339 $this->setError($php_errormsg);
349 if ($this->openmode[0] ==
'w')
355 ini_set(
'track_errors', $track_errors);
368 public function eof()
372 $this->setError(
JText::_(
'JLIB_FILESYSTEM_ERROR_STREAMS_FILE_NOT_OPEN'));
379 $track_errors = ini_get(
'track_errors');
380 ini_set(
'track_errors',
true);
382 switch ($this->processingmethod)
385 $res = gzeof($this->fh);
391 $res = feof($this->fh);
397 $this->setError($php_errormsg);
401 ini_set(
'track_errors', $track_errors);
414 public function filesize()
416 if (!$this->filename)
418 $this->setError(
JText::_(
'JLIB_FILESYSTEM_ERROR_STREAMS_FILE_NOT_OPEN'));
427 $track_errors = ini_get(
'track_errors');
428 ini_set(
'track_errors',
true);
429 $res = @filesize($this->filename);
439 $tmp_error = $php_errormsg;
449 $this->setError($tmp_error);
454 $this->setError(
JText::_(
'JLIB_FILESYSTEM_ERROR_STREAMS_FILE_SIZE'));
459 $this->filesize = $res;
465 $this->filesize = $res;
470 ini_set(
'track_errors', $track_errors);
485 public function gets($length = 0)
489 $this->setError(
JText::_(
'JLIB_FILESYSTEM_ERROR_STREAMS_FILE_NOT_OPEN'));
497 $php_errormsg =
'Error Unknown';
498 $track_errors = ini_get(
'track_errors');
499 ini_set(
'track_errors',
true);
501 switch ($this->processingmethod)
504 $res = $length ? gzgets($this->fh, $length) : gzgets($this->fh);
510 $res = $length ? fgets($this->fh, $length) : fgets($this->fh);
516 $this->setError($php_errormsg);
524 ini_set(
'track_errors', $track_errors);
542 public function read($length = 0)
544 if (!$this->filesize && !$length)
549 if (!$this->filesize)
556 $length = $this->filesize;
562 $this->setError(
JText::_(
'JLIB_FILESYSTEM_ERROR_STREAMS_FILE_NOT_OPEN'));
570 $php_errormsg =
'Error Unknown';
571 $track_errors = ini_get(
'track_errors');
572 ini_set(
'track_errors',
true);
573 $remaining = $length;
578 switch ($this->processingmethod)
581 $res = ($remaining > 0) ? bzread($this->fh, $remaining) : bzread($this->fh, $this->chunksize);
585 $res = ($remaining > 0) ? gzread($this->fh, $remaining) : gzread($this->fh, $this->chunksize);
590 $res = ($remaining > 0) ? fread($this->fh, $remaining) : fread($this->fh, $this->chunksize);
596 $this->setError($php_errormsg);
619 $length = strlen($retval);
623 while ($remaining || !$length);
626 ini_set(
'track_errors', $track_errors);
645 public function seek($offset, $whence = SEEK_SET)
649 $this->setError(
JText::_(
'JLIB_FILESYSTEM_ERROR_STREAMS_FILE_NOT_OPEN'));
658 $track_errors = ini_get(
'track_errors');
659 ini_set(
'track_errors',
true);
661 switch ($this->processingmethod)
664 $res = gzseek($this->fh, $offset, $whence);
670 $res = fseek($this->fh, $offset, $whence);
677 $this->setError($php_errormsg);
685 ini_set(
'track_errors', $track_errors);
698 public function tell()
702 $this->setError(
JText::_(
'JLIB_FILESYSTEM_ERROR_STREAMS_FILE_NOT_OPEN'));
709 $track_errors = ini_get(
'track_errors');
710 ini_set(
'track_errors',
true);
712 switch ($this->processingmethod)
715 $res = gztell($this->fh);
721 $res = ftell($this->fh);
728 $this->setError($php_errormsg);
732 ini_set(
'track_errors', $track_errors);
758 public function write(&$string, $length = 0, $chunk = 0)
762 $this->setError(
JText::_(
'JLIB_FILESYSTEM_ERROR_STREAMS_FILE_NOT_OPEN'));
770 $length = strlen($string);
776 $chunk = $this->chunksize;
783 $track_errors = ini_get(
'track_errors');
784 ini_set(
'track_errors',
true);
785 $remaining = $length;
791 $amount = ($remaining > $chunk) ? $chunk : $remaining;
792 $res = fwrite($this->fh, substr($string, $start), $amount);
798 $this->setError($php_errormsg);
806 $this->setError(
JText::_(
'JLIB_FILESYSTEM_ERROR_NO_DATA_WRITTEN'));
818 ini_set(
'track_errors', $track_errors);
834 public function chmod($filename =
'', $mode = 0)
838 if (!isset($this->filename) || !$this->filename)
840 $this->setError(
JText::_(
'JLIB_FILESYSTEM_ERROR_STREAMS_FILENAME'));
845 $filename = $this->filename;
851 $mode = $this->filemode;
858 $track_errors = ini_get(
'track_errors');
859 ini_set(
'track_errors',
true);
860 $sch = parse_url($filename, PHP_URL_SCHEME);
871 $res = chmod($filename, $mode);
878 $this->setError($php_errormsg);
886 ini_set(
'track_errors', $track_errors);
900 public function get_meta_data()
904 $this->setError(
JText::_(
'JLIB_FILESYSTEM_ERROR_STREAMS_FILE_NOT_OPEN'));
909 return stream_get_meta_data($this->fh);
920 public function _buildContext()
923 if (count($this->contextOptions))
925 $this->context = @stream_context_create($this->contextOptions);
929 $this->context = null;
945 public function setContextOptions($context)
947 $this->contextOptions = $context;
948 $this->_buildContext();
964 public function addContextEntry($wrapper, $name, $value)
966 $this->contextOptions[$wrapper][$name] = $value;
967 $this->_buildContext();
981 public function deleteContextEntry($wrapper, $name)
984 if (isset($this->contextOptions[$wrapper]))
987 if (isset($this->contextOptions[$wrapper][$name]))
990 unset($this->contextOptions[$wrapper][$name]);
993 if (!count($this->contextOptions[$wrapper]))
996 unset($this->contextOptions[$wrapper]);
1002 $this->_buildContext();
1014 public function applyContextToStream()
1021 $php_errormsg =
'Unknown error setting context option';
1022 $track_errors = ini_get(
'track_errors');
1023 ini_set(
'track_errors',
true);
1024 $retval = @stream_context_set_option($this->fh, $this->contextOptions);
1028 $this->setError($php_errormsg);
1032 ini_set(
'track_errors', $track_errors);
1051 public function appendFilter($filtername, $read_write = STREAM_FILTER_READ, $params = array())
1059 $track_errors = ini_get(
'track_errors');
1060 ini_set(
'track_errors',
true);
1062 $res = @stream_filter_append($this->fh, $filtername, $read_write, $params);
1064 if (!$res && $php_errormsg)
1066 $this->setError($php_errormsg);
1070 $this->filters[] = &$res;
1074 ini_set(
'track_errors', $track_errors);
1092 public function prependFilter($filtername, $read_write = STREAM_FILTER_READ, $params = array())
1100 $track_errors = ini_get(
'track_errors');
1101 ini_set(
'track_errors',
true);
1102 $res = @stream_filter_prepend($this->fh, $filtername, $read_write, $params);
1104 if (!$res && $php_errormsg)
1107 $this->setError($php_errormsg);
1111 array_unshift($res,
'');
1112 $res[0] = &$this->filters;
1116 ini_set(
'track_errors', $track_errors);
1133 public function removeFilter(&$resource, $byindex =
false)
1137 $track_errors = ini_get(
'track_errors');
1138 ini_set(
'track_errors',
true);
1142 $res = stream_filter_remove($this->filters[$resource]);
1146 $res = stream_filter_remove($resource);
1149 if ($res && $php_errormsg)
1151 $this->setError($php_errormsg);
1155 ini_set(
'track_errors', $track_errors);
1173 public function copy($src, $dest, $context = null, $use_prefix =
true, $relative =
false)
1177 $track_errors = ini_get(
'track_errors');
1178 ini_set(
'track_errors',
true);
1180 $chmodDest = $this->_getFilename($dest,
'w', $use_prefix, $relative);
1184 $src = $this->_getFilename($src,
'w', $use_prefix, $relative);
1185 $dest = $this->_getFilename($dest,
'w', $use_prefix, $relative);
1190 $res = @copy($src, $dest, $context);
1192 elseif ($this->context)
1195 $res = @copy($src, $dest, $this->context);
1200 $res = @copy($src, $dest);
1203 if (!$res && $php_errormsg)
1205 $this->setError($php_errormsg);
1209 $this->chmod($chmodDest);
1213 ini_set(
'track_errors', $track_errors);
1231 public function move($src, $dest, $context = null, $use_prefix =
true, $relative =
false)
1235 $track_errors = ini_get(
'track_errors');
1236 ini_set(
'track_errors',
true);
1238 $src = $this->_getFilename($src,
'w', $use_prefix, $relative);
1239 $dest = $this->_getFilename($dest,
'w', $use_prefix, $relative);
1244 $res = @rename($src, $dest, $context);
1246 elseif ($this->context)
1249 $res = @rename($src, $dest, $this->context);
1254 $res = @rename($src, $dest);
1257 if (!$res && $php_errormsg)
1259 $this->setError($php_errormsg());
1262 $this->chmod($dest);
1265 ini_set(
'track_errors', $track_errors);
1282 public function delete($filename, $context = null, $use_prefix =
true, $relative =
false)
1286 $track_errors = ini_get(
'track_errors');
1287 ini_set(
'track_errors',
true);
1289 $filename = $this->_getFilename($filename,
'w', $use_prefix, $relative);
1294 $res = @unlink($filename, $context);
1296 elseif ($this->context)
1299 $res = @unlink($filename, $this->context);
1304 $res = @unlink($filename);
1307 if (!$res && $php_errormsg)
1309 $this->setError($php_errormsg());
1313 ini_set(
'track_errors', $track_errors);
1331 public function upload($src, $dest, $context = null, $use_prefix =
true, $relative =
false)
1333 if (is_uploaded_file($src))
1336 return $this->copy($src, $dest, $context, $use_prefix, $relative);
1340 $this->setError(
JText::_(
'JLIB_FILESYSTEM_ERROR_STREAMS_NOT_UPLOADED_FILE'));
1356 public function writeFile($filename, &$buffer)
1358 if ($this->open($filename,
'w'))
1360 $result = $this->write($buffer);
1382 public function _getFilename($filename, $mode, $use_prefix, $relative)
1387 $tmode = trim($mode,
'btf123456789');
1393 if (!$relative && $this->writeprefix)
1395 $filename = str_replace(JPATH_ROOT,
'', $filename);
1398 $filename = $this->writeprefix . $filename;
1402 if (!$relative && $this->readprefix)
1404 $filename = str_replace(JPATH_ROOT,
'', $filename);
1407 $filename = $this->readprefix . $filename;
1421 public function getFileHandle()