2
/***********************************************
3
* File : stringstreamwrapper.php
5
* Descr : Wraps a string as a standard php stream
6
* The used method names are predefined and can not be altered.
10
* Copyright 2007 - 2013, 2015 Zarafa Deutschland GmbH
12
* This program is free software: you can redistribute it and/or modify
13
* it under the terms of the GNU Affero General Public License, version 3,
14
* as published by the Free Software Foundation with the following additional
15
* term according to sec. 7:
17
* According to sec. 7 of the GNU Affero General Public License, version 3,
18
* the terms of the AGPL are supplemented with the following terms:
20
* "Zarafa" is a registered trademark of Zarafa B.V.
21
* "Z-Push" is a registered trademark of Zarafa Deutschland GmbH
22
* The licensing of the Program under the AGPL does not imply a trademark license.
23
* Therefore any rights, title and interest in our trademarks remain entirely with us.
25
* However, if you propagate an unmodified version of the Program you are
26
* allowed to use the term "Z-Push" to indicate that you distribute the Program.
27
* Furthermore you may use our trademarks where it is necessary to indicate
28
* the intended purpose of a product or service provided you use it in accordance
29
* with honest practices in industrial or commercial matters.
30
* If you want to propagate modified versions of the Program under the name "Z-Push",
31
* you may only do so if you have a written permission by Zarafa Deutschland GmbH
32
* (to acquire a permission please contact Zarafa at trademark@zarafa.com).
34
* This program is distributed in the hope that it will be useful,
35
* but WITHOUT ANY WARRANTY; without even the implied warranty of
36
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37
* GNU Affero General Public License for more details.
39
* You should have received a copy of the GNU Affero General Public License
40
* along with this program. If not, see <http://www.gnu.org/licenses/>.
42
* Consult LICENSE file for details
43
************************************************/
45
class StringStreamWrapper {
46
const PROTOCOL = "stringstream";
48
private $stringstream;
50
private $stringlength;
54
* The string to be streamed is passed over the context
56
* @param string $path Specifies the URL that was passed to the original function
57
* @param string $mode The mode used to open the file, as detailed for fopen()
58
* @param int $options Holds additional flags set by the streams API
59
* @param string $opened_path If the path is opened successfully, and STREAM_USE_PATH is set in options,
60
* opened_path should be set to the full path of the file/resource that was actually opened.
65
public function stream_open($path, $mode, $options, &$opened_path) {
66
$contextOptions = stream_context_get_options($this->context);
67
if (!isset($contextOptions[self::PROTOCOL]['string']))
72
// this is our stream!
73
$this->stringstream = $contextOptions[self::PROTOCOL]['string'];
75
$this->stringlength = strlen($this->stringstream);
76
ZLog::Write(LOGLEVEL_DEBUG, sprintf("StringStreamWrapper::stream_open(): initialized stream length: %d", $this->stringlength));
84
* @param int $len amount of bytes to be read
89
public function stream_read($len) {
90
$data = substr($this->stringstream, $this->position, $len);
91
$this->position += strlen($data);
96
* Writes data to the stream.
101
public function stream_write($data){
103
$this->stringstream = substr($this->stringstream, 0, $this->position) . $data . substr($this->stringstream, $this->position += $l);
104
$this->stringlength = strlen($this->stringstream);
109
* Stream "seek" functionality.
115
public function stream_seek($offset, $whence = SEEK_SET) {
116
if ($whence == SEEK_CUR) {
117
$this->position += $offset;
119
else if ($whence == SEEK_END) {
120
$this->position = $this->stringlength + $offset;
123
$this->position = $offset;
129
* Returns the current position on stream
134
public function stream_tell() {
135
return $this->position;
139
* Indicates if 'end of file' is reached
144
public function stream_eof() {
145
return ($this->position >= $this->stringlength);
149
* Retrieves information about a stream
154
public function stream_stat() {
156
7 => $this->stringlength,
157
'size' => $this->stringlength,
162
* Instantiates a StringStreamWrapper
164
* @param string $string The string to be wrapped
167
* @return StringStreamWrapper
169
static public function Open($string) {
170
$context = stream_context_create(array(self::PROTOCOL => array('string' => &$string)));
171
return fopen(self::PROTOCOL . "://",'r', false, $context);
175
stream_wrapper_register(StringStreamWrapper::PROTOCOL, "StringStreamWrapper")
b'\\ No newline at end of file'