abstract class BackdropLocalStreamWrapper implements BackdropStreamWrapperInterface {
public $context;
public $handle = NULL;
protected $uri;
abstract function getDirectoryPath();
function setUri($uri) {
$this->uri = $uri;
}
function getUri() {
return $this->uri;
}
protected function getTarget($uri = NULL) {
if (!isset($uri)) {
$uri = $this->uri;
}
list($scheme, $target) = explode('://', $uri, 2);
return trim($target, '\/');
}
static function getMimeType($uri, $mapping = NULL) {
if (!isset($mapping)) {
include_once BACKDROP_ROOT . '/core/includes/file.mimetypes.inc';
$mapping = file_mimetype_mapping();
}
$extension = '';
$file_parts = explode('.', backdrop_basename($uri));
array_shift($file_parts);
while ($additional_part = array_pop($file_parts)) {
$extension = strtolower($additional_part . ($extension ? '.' . $extension : ''));
if (isset($mapping['extensions'][$extension])) {
return $mapping['mimetypes'][$mapping['extensions'][$extension]];
}
}
return 'application/octet-stream';
}
function chmod($mode) {
$output = @chmod($this->getLocalPath(), $mode);
clearstatcache();
return $output;
}
function realpath() {
return $this->getLocalPath();
}
protected function getLocalPath($uri = NULL) {
if (!isset($uri)) {
$uri = $this->uri;
}
$path = $this->getDirectoryPath() . '/' . $this->getTarget($uri);
$realpath = realpath($path);
if (!$realpath) {
$realpath = realpath(dirname($path)) . '/' . backdrop_basename($path);
}
$directory = realpath($this->getDirectoryPath());
if (!$realpath || !$directory || strpos($realpath, $directory) !== 0) {
return FALSE;
}
return $realpath;
}
public function stream_open($uri, $mode, $options, &$opened_path) {
$this->uri = $uri;
$path = $this->getLocalPath();
if ($path) {
$this->handle = ($options & STREAM_REPORT_ERRORS) ? fopen($path, $mode) : @fopen($path, $mode);
}
else {
$this->handle = NULL;
}
if ((bool) $this->handle && $options & STREAM_USE_PATH) {
$opened_path = $path;
}
return (bool) $this->handle;
}
public function stream_lock($operation) {
if (in_array($operation, array(LOCK_SH, LOCK_EX, LOCK_UN, LOCK_NB))) {
return flock($this->handle, $operation);
}
return TRUE;
}
public function stream_read($count) {
return fread($this->handle, $count);
}
public function stream_write($data) {
return fwrite($this->handle, $data);
}
public function stream_eof() {
return feof($this->handle);
}
public function stream_seek($offset, $whence) {
return !fseek($this->handle, $offset, $whence);
}
public function stream_flush() {
return fflush($this->handle);
}
public function stream_tell() {
return ftell($this->handle);
}
public function stream_stat() {
return fstat($this->handle);
}
public function stream_close() {
return fclose($this->handle);
}
public function stream_metadata($uri, $option, $value) {
$target = $this->getLocalPath($uri);
$return = FALSE;
switch ($option) {
case STREAM_META_TOUCH:
if (!empty($value)) {
$return = touch($target, $value[0], $value[1]);
}
else {
$return = touch($target);
}
break;
case STREAM_META_OWNER_NAME:
case STREAM_META_OWNER:
$return = chown($target, $value);
break;
case STREAM_META_GROUP_NAME:
case STREAM_META_GROUP:
$return = chgrp($target, $value);
break;
case STREAM_META_ACCESS:
$return = chmod($target, $value);
break;
}
if ($return) {
clearstatcache(TRUE, $target);
}
return $return;
}
public function stream_truncate($new_size) {
return ftruncate($this->handle, $new_size);
}
public function stream_cast($cast_as) {
return $this->handle ? $this->handle : FALSE;
}
public function stream_set_option($option, $arg1, $arg2) {
trigger_error('stream_set_option() not supported for local file based stream wrappers', E_USER_WARNING);
return FALSE;
}
public function unlink($uri) {
$this->uri = $uri;
return backdrop_unlink($this->getLocalPath());
}
public function rename($from_uri, $to_uri) {
return rename($this->getLocalPath($from_uri), $this->getLocalPath($to_uri));
}
public function dirname($uri = NULL) {
list($scheme, $target) = explode('://', $uri, 2);
$target = $this->getTarget($uri);
$dirname = dirname($target);
if ($dirname == '.') {
$dirname = '';
}
return $scheme . '://' . $dirname;
}
public function mkdir($uri, $mode, $options) {
$this->uri = $uri;
$recursive = (bool) ($options & STREAM_MKDIR_RECURSIVE);
if ($recursive) {
$local_path = $this->getDirectoryPath() . '/' . $this->getTarget($uri);
}
else {
$local_path = $this->getLocalPath($uri);
}
if ($options & STREAM_REPORT_ERRORS) {
return mkdir($local_path, $mode, $recursive);
}
else {
return @mkdir($local_path, $mode, $recursive);
}
}
public function rmdir($uri, $options) {
$this->uri = $uri;
if ($options & STREAM_REPORT_ERRORS) {
return backdrop_rmdir($this->getLocalPath());
}
else {
return @backdrop_rmdir($this->getLocalPath());
}
}
public function url_stat($uri, $flags) {
$this->uri = $uri;
$path = $this->getLocalPath();
if ($flags & STREAM_URL_STAT_QUIET || !file_exists($path)) {
return @stat($path);
}
else {
return stat($path);
}
}
public function dir_opendir($uri, $options) {
$this->uri = $uri;
$this->handle = opendir($this->getLocalPath());
return (bool) $this->handle;
}
public function dir_readdir() {
return readdir($this->handle);
}
public function dir_rewinddir() {
rewinddir($this->handle);
return TRUE;
}
public function dir_closedir() {
closedir($this->handle);
return TRUE;
}
}