123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834 |
- <?php
- define('STREAM_WRAPPERS_ALL', 0x0000);
- define('STREAM_WRAPPERS_LOCAL', 0x0001);
- define('STREAM_WRAPPERS_READ', 0x0004);
- define('STREAM_WRAPPERS_WRITE', 0x0008);
- define('STREAM_WRAPPERS_VISIBLE', 0x0010);
- define('STREAM_WRAPPERS_HIDDEN', STREAM_WRAPPERS_READ | STREAM_WRAPPERS_WRITE);
- define('STREAM_WRAPPERS_LOCAL_HIDDEN', STREAM_WRAPPERS_LOCAL | STREAM_WRAPPERS_HIDDEN);
- define('STREAM_WRAPPERS_WRITE_VISIBLE', STREAM_WRAPPERS_READ | STREAM_WRAPPERS_WRITE | STREAM_WRAPPERS_VISIBLE);
- define('STREAM_WRAPPERS_READ_VISIBLE', STREAM_WRAPPERS_READ | STREAM_WRAPPERS_VISIBLE);
- define('STREAM_WRAPPERS_NORMAL', STREAM_WRAPPERS_WRITE_VISIBLE);
- define('STREAM_WRAPPERS_LOCAL_NORMAL', STREAM_WRAPPERS_LOCAL | STREAM_WRAPPERS_NORMAL);
- interface StreamWrapperInterface {
- public function stream_open($uri, $mode, $options, &$opened_url);
- public function stream_close();
- public function stream_lock($operation);
- public function stream_read($count);
- public function stream_write($data);
- public function stream_eof();
- public function stream_seek($offset, $whence);
- public function stream_flush();
- public function stream_tell();
- public function stream_stat();
- public function unlink($uri);
- public function rename($from_uri, $to_uri);
- public function mkdir($uri, $mode, $options);
- public function rmdir($uri, $options);
- public function url_stat($uri, $flags);
- public function dir_opendir($uri, $options);
- public function dir_readdir();
- public function dir_rewinddir();
- public function dir_closedir();
- }
- interface DrupalStreamWrapperInterface extends StreamWrapperInterface {
-
- function setUri($uri);
-
- public function getUri();
-
- public function getExternalUrl();
-
- public static function getMimeType($uri, $mapping = NULL);
-
- public function chmod($mode);
-
- public function realpath();
-
- public function dirname($uri = NULL);
- }
- abstract class DrupalLocalStreamWrapper implements DrupalStreamWrapperInterface {
-
- 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 DRUPAL_ROOT . '/includes/file.mimetypes.inc';
- $mapping = file_mimetype_mapping();
- }
- $extension = '';
- $file_parts = explode('.', drupal_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)) . '/' . drupal_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();
- $this->handle = ($options & STREAM_REPORT_ERRORS) ? fopen($path, $mode) : @fopen($path, $mode);
- 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 unlink($uri) {
- $this->uri = $uri;
- return drupal_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) {
-
-
- $localpath = $this->getDirectoryPath() . '/' . $this->getTarget($uri);
- }
- else {
- $localpath = $this->getLocalPath($uri);
- }
- if ($options & STREAM_REPORT_ERRORS) {
- return mkdir($localpath, $mode, $recursive);
- }
- else {
- return @mkdir($localpath, $mode, $recursive);
- }
- }
-
- public function rmdir($uri, $options) {
- $this->uri = $uri;
- if ($options & STREAM_REPORT_ERRORS) {
- return drupal_rmdir($this->getLocalPath());
- }
- else {
- return @drupal_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;
- }
- }
- class DrupalPublicStreamWrapper extends DrupalLocalStreamWrapper {
-
- public function getDirectoryPath() {
- return variable_get('file_public_path', conf_path() . '/files');
- }
-
- function getExternalUrl() {
- $path = str_replace('\\', '/', $this->getTarget());
- return $GLOBALS['base_url'] . '/' . self::getDirectoryPath() . '/' . drupal_encode_path($path);
- }
- }
- class DrupalPrivateStreamWrapper extends DrupalLocalStreamWrapper {
-
- public function getDirectoryPath() {
- return variable_get('file_private_path', '');
- }
-
- function getExternalUrl() {
- $path = str_replace('\\', '/', $this->getTarget());
- return url('system/files/' . $path, array('absolute' => TRUE));
- }
- }
- class DrupalTemporaryStreamWrapper extends DrupalLocalStreamWrapper {
-
- public function getDirectoryPath() {
- return variable_get('file_temporary_path', file_directory_temp());
- }
-
- public function getExternalUrl() {
- $path = str_replace('\\', '/', $this->getTarget());
- return url('system/temporary/' . $path, array('absolute' => TRUE));
- }
- }
|