File Source for ChainedBlockStream.php

PHPExcel_Shared_OLE

# Source for file ChainedBlockStream.php

Documentation is available at ChainedBlockStream.php

1. <?php
2. /**
3. * PHPExcel
4. *
5. * Copyright (C) 2006 - 2011 PHPExcel
6. *
7. * This library is free software; you can redistribute it and/or
8. * modify it under the terms of the GNU Lesser General Public
9. * License as published by the Free Software Foundation; either
10. * version 2.1 of the License, or (at your option) any later version.
11. *
12. * This library is distributed in the hope that it will be useful,
13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15. * Lesser General Public License for more details.
16. *
17. * You should have received a copy of the GNU Lesser General Public
18. * License along with this library; if not, write to the Free Software
19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20. *
21. * @category PHPExcel
22. * @package PHPExcel_Shared_OLE
23. * @copyright Copyright (c) 2006 - 2007 Christian Schmidt
24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25. * @version 1.7.6, 2011-02-27
26. */
28. /**
29. * PHPExcel_Shared_OLE_ChainedBlockStream
30. *
31. * Stream wrapper for reading data stored in an OLE file. Implements methods
32. * for PHP's stream_wrapper_register(). For creating streams using this
33. * wrapper, use PHPExcel_Shared_OLE_PPS_File::getStream().
34. *
35. * @category PHPExcel
36. * @package PHPExcel_Shared_OLE
37. */
38. class PHPExcel_Shared_OLE_ChainedBlockStream
39. {
40. /**
41. * The OLE container of the file that is being read.
42. * @var OLE
43. */
44. public $ole;
46. /**
47. * Parameters specified by fopen().
48. * @var array
49. */
50. public $params;
52. /**
53. * The binary data of the file.
54. * @var string
55. */
56. public $data;
58. /**
59. * The file pointer.
60. * @var int byte offset
61. */
62. public $pos;
64. /**
65. * Implements support for fopen().
66. * For creating streams using this wrapper, use OLE_PPS_File::getStream().
67. * @param string resource name including scheme, e.g.
68. * ole-chainedblockstream://oleInstanceId=1
69. * @param string only "r" is supported
70. * @param int mask of STREAM_REPORT_ERRORS and STREAM_USE_PATH
71. * @param string absolute path of the opened stream (out parameter)
72. * @return bool true on success
73. */
74. public function stream_open($path, $mode, $options, &$openedPath)
75. {
76. if ($mode != 'r') {
77. if ($options & STREAM_REPORT_ERRORS) {
78. trigger_error('Only reading is supported', E_USER_WARNING);
79. }
80. return false;
81. }
83. // 25 is length of "ole-chainedblockstream://"
84. parse_str( substr($path, 25), $this-> params);
85. if (!isset($this-> params['oleInstanceId'],
86. $this-> params['blockId'],
87. $GLOBALS['_OLE_INSTANCES'][$this-> params['oleInstanceId']])) {
89. if ($options & STREAM_REPORT_ERRORS) {
90. trigger_error('OLE stream not found', E_USER_WARNING);
91. }
92. return false;
93. }
94. $this-> ole = $GLOBALS['_OLE_INSTANCES'][$this-> params['oleInstanceId']];
96. $blockId = $this-> params['blockId'];
97. $this-> data = '';
98. if (isset($this-> params['size']) &&
99. $this-> params['size'] < $this-> ole->bigBlockThreshold &&
100. $blockId != $this-> ole->root->_StartBlock) {
102. // Block id refers to small blocks
103. $rootPos = $this-> ole->_getBlockOffset($this-> ole->root->_StartBlock);
104. while ($blockId != -2) {
105. $pos = $rootPos + $blockId * $this-> ole->bigBlockSize;
106. $blockId = $this-> ole->sbat[$blockId];
107. fseek($this-> ole->_file_handle, $pos);
108. $this-> data .= fread($this-> ole->_file_handle, $this-> ole->bigBlockSize);
109. }
110. } else {
111. // Block id refers to big blocks
112. while ($blockId != -2) {
113. $pos = $this-> ole->_getBlockOffset($blockId);
114. fseek($this-> ole->_file_handle, $pos);
115. $this-> data .= fread($this-> ole->_file_handle, $this-> ole->bigBlockSize);
116. $blockId = $this-> ole->bbat[$blockId];
117. }
118. }
119. if (isset($this-> params['size'])) {
120. $this-> data = substr($this-> data, 0, $this-> params['size']);
121. }
123. if ($options & STREAM_USE_PATH) {
124. $openedPath = $path;
125. }
127. return true;
128. }
130. /**
131. * Implements support for fclose().
132. * @return string
133. */
134. public function stream_close()
135. {
136. $this-> ole = null;
137. unset($GLOBALS['_OLE_INSTANCES']);
138. }
140. /**
141. * Implements support for fread(), fgets() etc.
142. * @param int maximum number of bytes to read
143. * @return string
144. */
145. public function stream_read($count)
146. {
147. if ($this-> stream_eof()) {
148. return false;
149. }
150. $s = substr($this-> data, $this-> pos, $count);
151. $this-> pos += $count;
152. return $s;
153. }
155. /**
156. * Implements support for feof().
157. * @return bool TRUE if the file pointer is at EOF; otherwise FALSE
158. */
159. public function stream_eof()
160. {
161. $eof = $this-> pos >= strlen($this-> data);
162. // Workaround for bug in PHP 5.0.x: http://bugs.php.net/27508
163. if ( version_compare(PHP_VERSION, '5.0', '>=') &&
164. version_compare(PHP_VERSION, '5.1', '<')) {
166. $eof = !$eof;
167. }
168. return $eof;
169. }
171. /**
172. * Returns the position of the file pointer, i.e. its offset into the file
173. * stream. Implements support for ftell().
174. * @return int
175. */
176. public function stream_tell()
177. {
178. return $this-> pos;
179. }
181. /**
182. * Implements support for fseek().
183. * @param int byte offset
184. * @param int SEEK_SET, SEEK_CUR or SEEK_END
185. * @return bool
186. */
187. public function stream_seek($offset, $whence)
188. {
189. if ($whence == SEEK_SET && $offset >= 0) {
190. $this-> pos = $offset;
191. } elseif ($whence == SEEK_CUR && -$offset <= $this-> pos) {
192. $this-> pos += $offset;
193. } elseif ($whence == SEEK_END && -$offset <= sizeof($this-> data)) {
194. $this-> pos = strlen($this-> data) + $offset;
195. } else {
196. return false;
197. }
198. return true;
199. }
201. /**
202. * Implements support for fstat(). Currently the only supported field is
203. * "size".
204. * @return array
205. */
206. public function stream_stat()
207. {
208. return array(
209. 'size' => strlen($this-> data),
210. );
211. }
213. // Methods used by stream_wrapper_register() that are not implemented:
214. // bool stream_flush ( void )
215. // int stream_write ( string data )
216. // bool rename ( string path_from, string path_to )
217. // bool mkdir ( string path, int mode, int options )
218. // bool rmdir ( string path, int options )
219. // bool dir_opendir ( string path, int options )
220. // array url_stat ( string path, int flags )
221. // string dir_readdir ( void )
222. // bool dir_rewinddir ( void )
223. // bool dir_closedir ( void )
224. }

Documentation generated on Sun, 27 Feb 2011 16:29:00 -0800 by phpDocumentor 1.4.3