[7589] | 1 | <?php |
---|
| 2 | /*********************************************** |
---|
| 3 | * File : mapistreamwrapper.php |
---|
| 4 | * Project : Z-Push |
---|
| 5 | * Descr : Wraps a mapi stream as a standard php stream |
---|
| 6 | * The used method names are predefined and can not be altered. |
---|
| 7 | * |
---|
| 8 | * Created : 24.11.2011 |
---|
| 9 | * |
---|
| 10 | * Copyright 2007 - 2012 Zarafa Deutschland GmbH |
---|
| 11 | * |
---|
| 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: |
---|
| 16 | * |
---|
| 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: |
---|
| 19 | * |
---|
| 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. |
---|
| 24 | * |
---|
| 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). |
---|
| 33 | * |
---|
| 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. |
---|
| 38 | * |
---|
| 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/>. |
---|
| 41 | * |
---|
| 42 | * Consult LICENSE file for details |
---|
| 43 | ************************************************/ |
---|
| 44 | |
---|
| 45 | class MAPIStreamWrapper { |
---|
| 46 | const PROTOCOL = "mapistream"; |
---|
| 47 | |
---|
| 48 | private $mapistream; |
---|
| 49 | private $position; |
---|
| 50 | private $streamlength; |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | * Opens the stream |
---|
| 54 | * The mapistream reference is passed over the context |
---|
| 55 | * |
---|
| 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. |
---|
| 61 | * |
---|
| 62 | * @access public |
---|
| 63 | * @return boolean |
---|
| 64 | */ |
---|
| 65 | public function stream_open($path, $mode, $options, &$opened_path) { |
---|
| 66 | $contextOptions = stream_context_get_options($this->context); |
---|
| 67 | if (!isset($contextOptions[self::PROTOCOL]['stream'])) |
---|
| 68 | return false; |
---|
| 69 | |
---|
| 70 | $this->position = 0; |
---|
| 71 | |
---|
| 72 | // this is our stream! |
---|
| 73 | $this->mapistream = $contextOptions[self::PROTOCOL]['stream']; |
---|
| 74 | |
---|
| 75 | // get the data length from mapi |
---|
| 76 | $stat = mapi_stream_stat($this->mapistream); |
---|
| 77 | $this->streamlength = $stat["cb"]; |
---|
| 78 | |
---|
| 79 | ZLog::Write(LOGLEVEL_DEBUG, sprintf("MAPIStreamWrapper::stream_open(): initialized mapistream: %s streamlength: %d", $this->mapistream, $this->streamlength)); |
---|
| 80 | |
---|
| 81 | return true; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | /** |
---|
| 85 | * Reads from stream |
---|
| 86 | * |
---|
| 87 | * @param int $len amount of bytes to be read |
---|
| 88 | * |
---|
| 89 | * @access public |
---|
| 90 | * @return string |
---|
| 91 | */ |
---|
| 92 | public function stream_read($len) { |
---|
| 93 | $len = ($this->position + $len > $this->streamlength) ? ($this->streamlength - $this->position) : $len; |
---|
| 94 | $data = mapi_stream_read($this->mapistream, $len); |
---|
| 95 | $this->position += strlen($data); |
---|
| 96 | return $data; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /** |
---|
| 100 | * Returns the current position on stream |
---|
| 101 | * |
---|
| 102 | * @access public |
---|
| 103 | * @return int |
---|
| 104 | */ |
---|
| 105 | public function stream_tell() { |
---|
| 106 | return $this->position; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | /** |
---|
| 110 | * Indicates if 'end of file' is reached |
---|
| 111 | * |
---|
| 112 | * @access public |
---|
| 113 | * @return boolean |
---|
| 114 | */ |
---|
| 115 | public function stream_eof() { |
---|
| 116 | return ($this->position >= $this->streamlength); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | /** |
---|
| 120 | * Retrieves information about a stream |
---|
| 121 | * |
---|
| 122 | * @access public |
---|
| 123 | * @return array |
---|
| 124 | */ |
---|
| 125 | public function stream_stat() { |
---|
| 126 | return array( |
---|
| 127 | 7 => $this->streamlength, |
---|
| 128 | 'size' => $this->streamlength, |
---|
| 129 | ); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | /** |
---|
| 133 | * Instantiates a MAPIStreamWrapper |
---|
| 134 | * |
---|
| 135 | * @param mapistream $mapistream The stream to be wrapped |
---|
| 136 | * |
---|
| 137 | * @access public |
---|
| 138 | * @return MAPIStreamWrapper |
---|
| 139 | */ |
---|
| 140 | static public function Open($mapistream) { |
---|
| 141 | $context = stream_context_create(array(self::PROTOCOL => array('stream' => &$mapistream))); |
---|
| 142 | return fopen(self::PROTOCOL . "://",'r', false, $context); |
---|
| 143 | } |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | stream_wrapper_register(MAPIStreamWrapper::PROTOCOL, "MAPIStreamWrapper") |
---|
| 147 | |
---|
| 148 | ?> |
---|