- md5
- e12dfaae88284bf845d714d15d1b2e1d
- sha1
- a4508c442bc34211fac8498633c721f4e164f568
- sha256
- 69fd192f48f20460705fd92c8d589cbb766e7c9e5b599621b30b4814b3fa7b46
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Utility;
defined('JPATH_PLATFORM') or die;
// Workaround for B/C. Will be removed with 4.0
BufferStreamHandler::stream_register();
/**
* Generic Buffer stream handler
*
* This class provides a generic buffer stream. It can be used to store/retrieve/manipulate
* string buffers with the standard PHP filesystem I/O methods.
*
* @since 1.7.0
*/
class BufferStreamHandler
{
/**
* Stream position
*
* @var integer
* @since 1.7.0
*/
public $position = 0;
/**
* Buffer name
*
* @var string
* @since 1.7.0
*/
public $name = null;
/**
* Buffer hash
*
* @var array
* @since 3.0.0
*/
public $buffers = array();
/**
* Status of registering the wrapper
*
* @var boolean
* @since 3.8.2
*/
static private $registered = false;
/**
* Function to register the stream wrapper
*
* @return void
*
* @since 3.8.2
*/
public static function stream_register()
{
if (!self::$registered)
{
stream_wrapper_register('buffer', '\\Joomla\\CMS\\Utility\\BufferStreamHandler');
self::$registered = true;
}
return;
}
/**
* Function to open file or url
*
* @param string $path The URL that was passed
* @param string $mode Mode used to open the file @see fopen
* @param integer $options Flags used by the API, may be STREAM_USE_PATH and
* STREAM_REPORT_ERRORS
* @param string &$opened_path Full path of the resource. Used with STREAN_USE_PATH option
*
* @return boolean
*
* @since 1.7.0
* @see streamWrapper::stream_open
*/
public function stream_open($path, $mode, $options, &$opened_path)
{
$url = parse_url($path);
$this->name = $url['host'];
$this->buffers[$this->name] = null;
$this->position = 0;
return true;
}
/**
* Read stream
*
* @param integer $count How many bytes of data from the current position should be returned.
*
* @return mixed The data from the stream up to the specified number of bytes (all data if
* the total number of bytes in the stream is less than $count. Null if
* the stream is empty.
*
* @see streamWrapper::stream_read
* @since 1.7.0
*/
public function stream_read($count)
{
$ret = substr($this->buffers[$this->name], $this->position, $count);
$this->position += strlen($ret);
return $ret;
}
/**
* Write stream
*
* @param string $data The data to write to the stream.
*
* @return integer
*
* @see streamWrapper::stream_write
* @since 1.7.0
*/
public function stream_write($data)
{
$left = substr($this->buffers[$this->name], 0, $this->position);
$right = substr($this->buffers[$this->name], $this->position + strlen($data));
$this->buffers[$this->name] = $left . $data . $right;
$this->position += strlen($data);
return strlen($data);
}
/**
* Function to get the current position of the stream
*
* @return integer
*
* @see streamWrapper::stream_tell
* @since 1.7.0
*/
public function stream_tell()
{
return $this->position;
}
/**
* Function to test for end of file pointer
*
* @return boolean True if the pointer is at the end of the stream
*
* @see streamWrapper::stream_eof
* @since 1.7.0
*/
public function stream_eof()
{
return $this->position >= strlen($this->buffers[$this->name]);
}
/**
* The read write position updates in response to $offset and $whence
*
* @param integer $offset The offset in bytes
* @param integer $whence Position the offset is added to
* Options are SEEK_SET, SEEK_CUR, and SEEK_END
*
* @return boolean True if updated
*
* @see streamWrapper::stream_seek
* @since 1.7.0
*/
public function stream_seek($offset, $whence)
{
switch ($whence)
{
case SEEK_SET :
return $this->seek_set($offset);
case SEEK_CUR :
return $this->seek_cur($offset);
case SEEK_END :
return $this->seek_end($offset);
}
return false;
}
/**
* Set the position to the offset
*
* @param integer $offset The offset in bytes
*
* @return boolean
*/
protected function seek_set($offset)
{
if ($offset < 0 || $offset > strlen($this->buffers[$this->name]))
{
return false;
}
$this->position = $offset;
return true;
}
/**
* Adds the offset to current position
*
* @param integer $offset The offset in bytes
*
* @return boolean
*/
protected function seek_cur($offset)
{
if ($offset < 0)
{
return false;
}
$this->position += $offset;
return true;
}
/**
* Sets the position to the end of the current buffer + offset
*
* @param integer $offset The offset in bytes
*
* @return boolean
*/
protected function seek_end($offset)
{
$offset += strlen($this->buffers[$this->name]);
if ($offset < 0)
{
return false;
}
$this->position = $offset;
return true;
}
}
The same content is in 16 releases, Joomla 3.9.15-rc to 3.10.0-alpha1. See them all, or every version of this file.