123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477 |
- <?php
- namespace TYPO3\PharStreamWrapper;
- /*
- * This file is part of the TYPO3 project.
- *
- * It is free software; you can redistribute it and/or modify it under the terms
- * of the MIT License (MIT). For the full copyright and license information,
- * please read the LICENSE file that was distributed with this source code.
- *
- * The TYPO3 project - inspiring people to share!
- */
- class PharStreamWrapper
- {
- /**
- * Internal stream constants that are not exposed to PHP, but used...
- * @see https://github.com/php/php-src/blob/e17fc0d73c611ad0207cac8a4a01ded38251a7dc/main/php_streams.h
- */
- const STREAM_OPEN_FOR_INCLUDE = 128;
- /**
- * @var resource
- */
- public $context;
- /**
- * @var resource
- */
- protected $internalResource;
- /**
- * @return bool
- */
- public function dir_closedir()
- {
- if (!is_resource($this->internalResource)) {
- return false;
- }
- $this->invokeInternalStreamWrapper(
- 'closedir',
- $this->internalResource
- );
- return !is_resource($this->internalResource);
- }
- /**
- * @param string $path
- * @param int $options
- * @return bool
- */
- public function dir_opendir($path, $options)
- {
- $this->assert($path, Behavior::COMMAND_DIR_OPENDIR);
- $this->internalResource = $this->invokeInternalStreamWrapper(
- 'opendir',
- $path,
- $this->context
- );
- return is_resource($this->internalResource);
- }
- /**
- * @return string|false
- */
- public function dir_readdir()
- {
- return $this->invokeInternalStreamWrapper(
- 'readdir',
- $this->internalResource
- );
- }
- /**
- * @return bool
- */
- public function dir_rewinddir()
- {
- if (!is_resource($this->internalResource)) {
- return false;
- }
- $this->invokeInternalStreamWrapper(
- 'rewinddir',
- $this->internalResource
- );
- return is_resource($this->internalResource);
- }
- /**
- * @param string $path
- * @param int $mode
- * @param int $options
- * @return bool
- */
- public function mkdir($path, $mode, $options)
- {
- $this->assert($path, Behavior::COMMAND_MKDIR);
- return $this->invokeInternalStreamWrapper(
- 'mkdir',
- $path,
- $mode,
- (bool) ($options & STREAM_MKDIR_RECURSIVE),
- $this->context
- );
- }
- /**
- * @param string $path_from
- * @param string $path_to
- * @return bool
- */
- public function rename($path_from, $path_to)
- {
- $this->assert($path_from, Behavior::COMMAND_RENAME);
- $this->assert($path_to, Behavior::COMMAND_RENAME);
- return $this->invokeInternalStreamWrapper(
- 'rename',
- $path_from,
- $path_to,
- $this->context
- );
- }
- /**
- * @param string $path
- * @param int $options
- * @return bool
- */
- public function rmdir($path, $options)
- {
- $this->assert($path, Behavior::COMMAND_RMDIR);
- return $this->invokeInternalStreamWrapper(
- 'rmdir',
- $path,
- $this->context
- );
- }
- /**
- * @param int $cast_as
- */
- public function stream_cast($cast_as)
- {
- throw new Exception(
- 'Method stream_select() cannot be used',
- 1530103999
- );
- }
- public function stream_close()
- {
- $this->invokeInternalStreamWrapper(
- 'fclose',
- $this->internalResource
- );
- }
- /**
- * @return bool
- */
- public function stream_eof()
- {
- return $this->invokeInternalStreamWrapper(
- 'feof',
- $this->internalResource
- );
- }
- /**
- * @return bool
- */
- public function stream_flush()
- {
- return $this->invokeInternalStreamWrapper(
- 'fflush',
- $this->internalResource
- );
- }
- /**
- * @param int $operation
- * @return bool
- */
- public function stream_lock($operation)
- {
- return $this->invokeInternalStreamWrapper(
- 'flock',
- $this->internalResource,
- $operation
- );
- }
- /**
- * @param string $path
- * @param int $option
- * @param string|int $value
- * @return bool
- */
- public function stream_metadata($path, $option, $value)
- {
- $this->assert($path, Behavior::COMMAND_STEAM_METADATA);
- if ($option === STREAM_META_TOUCH) {
- return call_user_func_array(
- array($this, 'invokeInternalStreamWrapper'),
- array_merge(array('touch', $path), (array) $value)
- );
- }
- if ($option === STREAM_META_OWNER_NAME || $option === STREAM_META_OWNER) {
- return $this->invokeInternalStreamWrapper(
- 'chown',
- $path,
- $value
- );
- }
- if ($option === STREAM_META_GROUP_NAME || $option === STREAM_META_GROUP) {
- return $this->invokeInternalStreamWrapper(
- 'chgrp',
- $path,
- $value
- );
- }
- if ($option === STREAM_META_ACCESS) {
- return $this->invokeInternalStreamWrapper(
- 'chmod',
- $path,
- $value
- );
- }
- return false;
- }
- /**
- * @param string $path
- * @param string $mode
- * @param int $options
- * @param string|null $opened_path
- * @return bool
- */
- public function stream_open(
- $path,
- $mode,
- $options,
- &$opened_path = null
- ) {
- $this->assert($path, Behavior::COMMAND_STREAM_OPEN);
- $arguments = array($path, $mode, (bool) ($options & STREAM_USE_PATH));
- // only add stream context for non include/require calls
- if (!($options & static::STREAM_OPEN_FOR_INCLUDE)) {
- $arguments[] = $this->context;
- // work around https://bugs.php.net/bug.php?id=66569
- // for including files from Phar stream with OPcache enabled
- } else {
- Helper::resetOpCache();
- }
- $this->internalResource = call_user_func_array(
- array($this, 'invokeInternalStreamWrapper'),
- array_merge(array('fopen'), $arguments)
- );
- if (!is_resource($this->internalResource)) {
- return false;
- }
- if ($opened_path !== null) {
- $metaData = stream_get_meta_data($this->internalResource);
- $opened_path = $metaData['uri'];
- }
- return true;
- }
- /**
- * @param int $count
- * @return string
- */
- public function stream_read($count)
- {
- return $this->invokeInternalStreamWrapper(
- 'fread',
- $this->internalResource,
- $count
- );
- }
- /**
- * @param int $offset
- * @param int $whence
- * @return bool
- */
- public function stream_seek($offset, $whence = SEEK_SET)
- {
- return $this->invokeInternalStreamWrapper(
- 'fseek',
- $this->internalResource,
- $offset,
- $whence
- ) !== -1;
- }
- /**
- * @param int $option
- * @param int $arg1
- * @param int $arg2
- * @return bool
- */
- public function stream_set_option($option, $arg1, $arg2)
- {
- if ($option === STREAM_OPTION_BLOCKING) {
- return $this->invokeInternalStreamWrapper(
- 'stream_set_blocking',
- $this->internalResource,
- $arg1
- );
- }
- if ($option === STREAM_OPTION_READ_TIMEOUT) {
- return $this->invokeInternalStreamWrapper(
- 'stream_set_timeout',
- $this->internalResource,
- $arg1,
- $arg2
- );
- }
- if ($option === STREAM_OPTION_WRITE_BUFFER) {
- return $this->invokeInternalStreamWrapper(
- 'stream_set_write_buffer',
- $this->internalResource,
- $arg2
- ) === 0;
- }
- return false;
- }
- /**
- * @return array
- */
- public function stream_stat()
- {
- return $this->invokeInternalStreamWrapper(
- 'fstat',
- $this->internalResource
- );
- }
- /**
- * @return int
- */
- public function stream_tell()
- {
- return $this->invokeInternalStreamWrapper(
- 'ftell',
- $this->internalResource
- );
- }
- /**
- * @param int $new_size
- * @return bool
- */
- public function stream_truncate($new_size)
- {
- return $this->invokeInternalStreamWrapper(
- 'ftruncate',
- $this->internalResource,
- $new_size
- );
- }
- /**
- * @param string $data
- * @return int
- */
- public function stream_write($data)
- {
- return $this->invokeInternalStreamWrapper(
- 'fwrite',
- $this->internalResource,
- $data
- );
- }
- /**
- * @param string $path
- * @return bool
- */
- public function unlink($path)
- {
- $this->assert($path, Behavior::COMMAND_UNLINK);
- return $this->invokeInternalStreamWrapper(
- 'unlink',
- $path,
- $this->context
- );
- }
- /**
- * @param string $path
- * @param int $flags
- * @return array|false
- */
- public function url_stat($path, $flags)
- {
- $this->assert($path, Behavior::COMMAND_URL_STAT);
- $functionName = $flags & STREAM_URL_STAT_QUIET ? '@stat' : 'stat';
- return $this->invokeInternalStreamWrapper($functionName, $path);
- }
- /**
- * @param string $path
- * @param string $command
- */
- protected function assert($path, $command)
- {
- if ($this->resolveAssertable()->assert($path, $command) === true) {
- return;
- }
- throw new Exception(
- sprintf(
- 'Denied invocation of "%s" for command "%s"',
- $path,
- $command
- ),
- 1535189880
- );
- }
- /**
- * @return Assertable
- */
- protected function resolveAssertable()
- {
- return Manager::instance();
- }
- /**
- * Invokes commands on the native PHP Phar stream wrapper.
- *
- * @param string $functionName
- * @param mixed ...$arguments
- * @return mixed
- */
- private function invokeInternalStreamWrapper($functionName)
- {
- $arguments = func_get_args();
- array_shift($arguments);
- $silentExecution = $functionName{0} === '@';
- $functionName = ltrim($functionName, '@');
- $this->restoreInternalSteamWrapper();
- try {
- if ($silentExecution) {
- $result = @call_user_func_array($functionName, $arguments);
- } else {
- $result = call_user_func_array($functionName, $arguments);
- }
- } catch (\Exception $exception) {
- $this->registerStreamWrapper();
- throw $exception;
- } catch (\Throwable $throwable) {
- $this->registerStreamWrapper();
- throw $throwable;
- }
- $this->registerStreamWrapper();
- return $result;
- }
- private function restoreInternalSteamWrapper()
- {
- stream_wrapper_restore('phar');
- }
- private function registerStreamWrapper()
- {
- stream_wrapper_unregister('phar');
- stream_wrapper_register('phar', get_class($this));
- }
- }
|