Back to index

d-push  2.0
stringstreamwrapper.php
Go to the documentation of this file.
00001 <?php
00002 /***********************************************
00003 * File      :   stringstreamwrapper.php
00004 * Project   :   Z-Push
00005 * Descr     :   Wraps a string as a standard php stream
00006 *               The used method names are predefined and can not be altered.
00007 *
00008 * Created   :   24.11.2011
00009 *
00010 * Copyright 2007 - 2011 Zarafa Deutschland GmbH
00011 *
00012 * This program is free software: you can redistribute it and/or modify
00013 * it under the terms of the GNU Affero General Public License, version 3,
00014 * as published by the Free Software Foundation with the following additional
00015 * term according to sec. 7:
00016 *
00017 * According to sec. 7 of the GNU Affero General Public License, version 3,
00018 * the terms of the AGPL are supplemented with the following terms:
00019 *
00020 * "Zarafa" is a registered trademark of Zarafa B.V.
00021 * "Z-Push" is a registered trademark of Zarafa Deutschland GmbH
00022 * The licensing of the Program under the AGPL does not imply a trademark license.
00023 * Therefore any rights, title and interest in our trademarks remain entirely with us.
00024 *
00025 * However, if you propagate an unmodified version of the Program you are
00026 * allowed to use the term "Z-Push" to indicate that you distribute the Program.
00027 * Furthermore you may use our trademarks where it is necessary to indicate
00028 * the intended purpose of a product or service provided you use it in accordance
00029 * with honest practices in industrial or commercial matters.
00030 * If you want to propagate modified versions of the Program under the name "Z-Push",
00031 * you may only do so if you have a written permission by Zarafa Deutschland GmbH
00032 * (to acquire a permission please contact Zarafa at trademark@zarafa.com).
00033 *
00034 * This program is distributed in the hope that it will be useful,
00035 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00036 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00037 * GNU Affero General Public License for more details.
00038 *
00039 * You should have received a copy of the GNU Affero General Public License
00040 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00041 *
00042 * Consult LICENSE file for details
00043 ************************************************/
00044 
00045 class StringStreamWrapper {
00046     const PROTOCOL = "stringstream";
00047 
00048     private $stringstream;
00049     private $position;
00050     private $stringlength;
00051 
00065     public function stream_open($path, $mode, $options, &$opened_path) {
00066         $contextOptions = stream_context_get_options($this->context);
00067         if (!isset($contextOptions[self::PROTOCOL]['string']))
00068             return false;
00069 
00070         $this->position = 0;
00071 
00072         // this is our stream!
00073         $this->stringstream = $contextOptions[self::PROTOCOL]['string'];
00074 
00075         $this->stringlength = strlen($this->stringstream);
00076         ZLog::Write(LOGLEVEL_DEBUG, sprintf("StringStreamWrapper::stream_open(): initialized stream length: %d", $this->stringlength));
00077 
00078         return true;
00079     }
00080 
00089     public function stream_read($len) {
00090         $data = substr($this->stringstream, $this->position, $len);
00091         $this->position += strlen($data);
00092         return $data;
00093     }
00094 
00101     public function stream_tell() {
00102         return $this->position;
00103     }
00104 
00111     public function stream_eof() {
00112         return ($this->position >= $this->stringlength);
00113     }
00114 
00123      static public function Open($string) {
00124         $context = stream_context_create(array(self::PROTOCOL => array('string' => &$string)));
00125         return fopen(self::PROTOCOL . "://",'r', false, $context);
00126     }
00127 }
00128 
00129 stream_wrapper_register(StringStreamWrapper::PROTOCOL, "StringStreamWrapper")
00130 
00131 ?>