No Description

ChainedBlockStream.php 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (C) 2006 - 2014 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 ##VERSION##, ##DATE##
  26. */
  27. /**
  28. * PHPExcel_Shared_OLE_ChainedBlockStream
  29. *
  30. * Stream wrapper for reading data stored in an OLE file. Implements methods
  31. * for PHP's stream_wrapper_register(). For creating streams using this
  32. * wrapper, use PHPExcel_Shared_OLE_PPS_File::getStream().
  33. *
  34. * @category PHPExcel
  35. * @package PHPExcel_Shared_OLE
  36. */
  37. class PHPExcel_Shared_OLE_ChainedBlockStream
  38. {
  39. /**
  40. * The OLE container of the file that is being read.
  41. * @var OLE
  42. */
  43. public $ole;
  44. /**
  45. * Parameters specified by fopen().
  46. * @var array
  47. */
  48. public $params;
  49. /**
  50. * The binary data of the file.
  51. * @var string
  52. */
  53. public $data;
  54. /**
  55. * The file pointer.
  56. * @var int byte offset
  57. */
  58. public $pos;
  59. /**
  60. * Implements support for fopen().
  61. * For creating streams using this wrapper, use OLE_PPS_File::getStream().
  62. *
  63. * @param string $path resource name including scheme, e.g.
  64. * ole-chainedblockstream://oleInstanceId=1
  65. * @param string $mode only "r" is supported
  66. * @param int $options mask of STREAM_REPORT_ERRORS and STREAM_USE_PATH
  67. * @param string &$openedPath absolute path of the opened stream (out parameter)
  68. * @return bool true on success
  69. */
  70. public function stream_open($path, $mode, $options, &$openedPath)
  71. {
  72. if ($mode != 'r') {
  73. if ($options & STREAM_REPORT_ERRORS) {
  74. trigger_error('Only reading is supported', E_USER_WARNING);
  75. }
  76. return false;
  77. }
  78. // 25 is length of "ole-chainedblockstream://"
  79. parse_str(substr($path, 25), $this->params);
  80. if (!isset($this->params['oleInstanceId'],
  81. $this->params['blockId'],
  82. $GLOBALS['_OLE_INSTANCES'][$this->params['oleInstanceId']])) {
  83. if ($options & STREAM_REPORT_ERRORS) {
  84. trigger_error('OLE stream not found', E_USER_WARNING);
  85. }
  86. return false;
  87. }
  88. $this->ole = $GLOBALS['_OLE_INSTANCES'][$this->params['oleInstanceId']];
  89. $blockId = $this->params['blockId'];
  90. $this->data = '';
  91. if (isset($this->params['size']) &&
  92. $this->params['size'] < $this->ole->bigBlockThreshold &&
  93. $blockId != $this->ole->root->_StartBlock) {
  94. // Block id refers to small blocks
  95. $rootPos = $this->ole->_getBlockOffset($this->ole->root->_StartBlock);
  96. while ($blockId != -2) {
  97. $pos = $rootPos + $blockId * $this->ole->bigBlockSize;
  98. $blockId = $this->ole->sbat[$blockId];
  99. fseek($this->ole->_file_handle, $pos);
  100. $this->data .= fread($this->ole->_file_handle, $this->ole->bigBlockSize);
  101. }
  102. } else {
  103. // Block id refers to big blocks
  104. while ($blockId != -2) {
  105. $pos = $this->ole->_getBlockOffset($blockId);
  106. fseek($this->ole->_file_handle, $pos);
  107. $this->data .= fread($this->ole->_file_handle, $this->ole->bigBlockSize);
  108. $blockId = $this->ole->bbat[$blockId];
  109. }
  110. }
  111. if (isset($this->params['size'])) {
  112. $this->data = substr($this->data, 0, $this->params['size']);
  113. }
  114. if ($options & STREAM_USE_PATH) {
  115. $openedPath = $path;
  116. }
  117. return true;
  118. }
  119. /**
  120. * Implements support for fclose().
  121. *
  122. */
  123. public function stream_close()
  124. {
  125. $this->ole = null;
  126. unset($GLOBALS['_OLE_INSTANCES']);
  127. }
  128. /**
  129. * Implements support for fread(), fgets() etc.
  130. *
  131. * @param int $count maximum number of bytes to read
  132. * @return string
  133. */
  134. public function stream_read($count)
  135. {
  136. if ($this->stream_eof()) {
  137. return false;
  138. }
  139. $s = substr($this->data, $this->pos, $count);
  140. $this->pos += $count;
  141. return $s;
  142. }
  143. /**
  144. * Implements support for feof().
  145. *
  146. * @return bool TRUE if the file pointer is at EOF; otherwise FALSE
  147. */
  148. public function stream_eof()
  149. {
  150. return $this->pos >= strlen($this->data);
  151. }
  152. /**
  153. * Returns the position of the file pointer, i.e. its offset into the file
  154. * stream. Implements support for ftell().
  155. *
  156. * @return int
  157. */
  158. public function stream_tell()
  159. {
  160. return $this->pos;
  161. }
  162. /**
  163. * Implements support for fseek().
  164. *
  165. * @param int $offset byte offset
  166. * @param int $whence SEEK_SET, SEEK_CUR or SEEK_END
  167. * @return bool
  168. */
  169. public function stream_seek($offset, $whence)
  170. {
  171. if ($whence == SEEK_SET && $offset >= 0) {
  172. $this->pos = $offset;
  173. } elseif ($whence == SEEK_CUR && -$offset <= $this->pos) {
  174. $this->pos += $offset;
  175. } elseif ($whence == SEEK_END && -$offset <= sizeof($this->data)) {
  176. $this->pos = strlen($this->data) + $offset;
  177. } else {
  178. return false;
  179. }
  180. return true;
  181. }
  182. /**
  183. * Implements support for fstat(). Currently the only supported field is
  184. * "size".
  185. * @return array
  186. */
  187. public function stream_stat()
  188. {
  189. return array(
  190. 'size' => strlen($this->data),
  191. );
  192. }
  193. // Methods used by stream_wrapper_register() that are not implemented:
  194. // bool stream_flush ( void )
  195. // int stream_write ( string data )
  196. // bool rename ( string path_from, string path_to )
  197. // bool mkdir ( string path, int mode, int options )
  198. // bool rmdir ( string path, int options )
  199. // bool dir_opendir ( string path, int options )
  200. // array url_stat ( string path, int flags )
  201. // string dir_readdir ( void )
  202. // bool dir_rewinddir ( void )
  203. // bool dir_closedir ( void )
  204. }