PageRenderTime 33ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/fsn-site-central/library/PHPExcel/Shared/OLE/ChainedBlockStream.php

https://gitlab.com/team_fsn/fsn-php
PHP | 230 lines | 95 code | 18 blank | 117 comment | 23 complexity | d4bb1993b2f702946acfa46e97aa4160 MD5 | raw file
  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 1.8.0, 2014-03-02
  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. // As we don't support below 5.2 anymore, this is simply redundancy and overhead
  151. // $eof = $this->pos >= strlen($this->data);
  152. // // Workaround for bug in PHP 5.0.x: http://bugs.php.net/27508
  153. // if (version_compare(PHP_VERSION, '5.0', '>=') &&
  154. // version_compare(PHP_VERSION, '5.1', '<')) {
  155. // $eof = !$eof;
  156. // }
  157. // return $eof;
  158. return $this->pos >= strlen($this->data);
  159. }
  160. /**
  161. * Returns the position of the file pointer, i.e. its offset into the file
  162. * stream. Implements support for ftell().
  163. *
  164. * @return int
  165. */
  166. public function stream_tell()
  167. {
  168. return $this->pos;
  169. }
  170. /**
  171. * Implements support for fseek().
  172. *
  173. * @param int $offset byte offset
  174. * @param int $whence SEEK_SET, SEEK_CUR or SEEK_END
  175. * @return bool
  176. */
  177. public function stream_seek($offset, $whence)
  178. {
  179. if ($whence == SEEK_SET && $offset >= 0) {
  180. $this->pos = $offset;
  181. } elseif ($whence == SEEK_CUR && -$offset <= $this->pos) {
  182. $this->pos += $offset;
  183. } elseif ($whence == SEEK_END && -$offset <= sizeof($this->data)) {
  184. $this->pos = strlen($this->data) + $offset;
  185. } else {
  186. return false;
  187. }
  188. return true;
  189. }
  190. /**
  191. * Implements support for fstat(). Currently the only supported field is
  192. * "size".
  193. * @return array
  194. */
  195. public function stream_stat()
  196. {
  197. return array(
  198. 'size' => strlen($this->data),
  199. );
  200. }
  201. // Methods used by stream_wrapper_register() that are not implemented:
  202. // bool stream_flush ( void )
  203. // int stream_write ( string data )
  204. // bool rename ( string path_from, string path_to )
  205. // bool mkdir ( string path, int mode, int options )
  206. // bool rmdir ( string path, int options )
  207. // bool dir_opendir ( string path, int options )
  208. // array url_stat ( string path, int flags )
  209. // string dir_readdir ( void )
  210. // bool dir_rewinddir ( void )
  211. // bool dir_closedir ( void )
  212. }