ChainedBlockStream.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. class PHPExcel_Shared_OLE_ChainedBlockStream
  8. {
  9. /**
  10. * The OLE container of the file that is being read.
  11. * @var OLE
  12. */
  13. public $ole;
  14. /**
  15. * Parameters specified by fopen().
  16. * @var array
  17. */
  18. public $params;
  19. /**
  20. * The binary data of the file.
  21. * @var string
  22. */
  23. public $data;
  24. /**
  25. * The file pointer.
  26. * @var int byte offset
  27. */
  28. public $pos;
  29. /**
  30. * Implements support for fopen().
  31. * For creating streams using this wrapper, use OLE_PPS_File::getStream().
  32. *
  33. * @param string $path resource name including scheme, e.g.
  34. * ole-chainedblockstream://oleInstanceId=1
  35. * @param string $mode only "r" is supported
  36. * @param int $options mask of STREAM_REPORT_ERRORS and STREAM_USE_PATH
  37. * @param string &$openedPath absolute path of the opened stream (out parameter)
  38. * @return bool true on success
  39. */
  40. public function stream_open($path, $mode, $options, &$openedPath)
  41. {
  42. if ($mode != 'r') {
  43. if ($options & STREAM_REPORT_ERRORS) {
  44. trigger_error('Only reading is supported', E_USER_WARNING);
  45. }
  46. return false;
  47. }
  48. // 25 is length of "ole-chainedblockstream://"
  49. parse_str(substr($path, 25), $this->params);
  50. if (!isset($this->params['oleInstanceId'], $this->params['blockId'], $GLOBALS['_OLE_INSTANCES'][$this->params['oleInstanceId']])) {
  51. if ($options & STREAM_REPORT_ERRORS) {
  52. trigger_error('OLE stream not found', E_USER_WARNING);
  53. }
  54. return false;
  55. }
  56. $this->ole = $GLOBALS['_OLE_INSTANCES'][$this->params['oleInstanceId']];
  57. $blockId = $this->params['blockId'];
  58. $this->data = '';
  59. if (isset($this->params['size']) && $this->params['size'] < $this->ole->bigBlockThreshold && $blockId != $this->ole->root->_StartBlock) {
  60. // Block id refers to small blocks
  61. $rootPos = $this->ole->_getBlockOffset($this->ole->root->_StartBlock);
  62. while ($blockId != -2) {
  63. $pos = $rootPos + $blockId * $this->ole->bigBlockSize;
  64. $blockId = $this->ole->sbat[$blockId];
  65. fseek($this->ole->_file_handle, $pos);
  66. $this->data .= fread($this->ole->_file_handle, $this->ole->bigBlockSize);
  67. }
  68. } else {
  69. // Block id refers to big blocks
  70. while ($blockId != -2) {
  71. $pos = $this->ole->_getBlockOffset($blockId);
  72. fseek($this->ole->_file_handle, $pos);
  73. $this->data .= fread($this->ole->_file_handle, $this->ole->bigBlockSize);
  74. $blockId = $this->ole->bbat[$blockId];
  75. }
  76. }
  77. if (isset($this->params['size'])) {
  78. $this->data = substr($this->data, 0, $this->params['size']);
  79. }
  80. if ($options & STREAM_USE_PATH) {
  81. $openedPath = $path;
  82. }
  83. return true;
  84. }
  85. /**
  86. * Implements support for fclose().
  87. *
  88. */
  89. public function stream_close()
  90. {
  91. $this->ole = null;
  92. unset($GLOBALS['_OLE_INSTANCES']);
  93. }
  94. /**
  95. * Implements support for fread(), fgets() etc.
  96. *
  97. * @param int $count maximum number of bytes to read
  98. * @return string
  99. */
  100. public function stream_read($count)
  101. {
  102. if ($this->stream_eof()) {
  103. return false;
  104. }
  105. $s = substr($this->data, $this->pos, $count);
  106. $this->pos += $count;
  107. return $s;
  108. }
  109. /**
  110. * Implements support for feof().
  111. *
  112. * @return bool TRUE if the file pointer is at EOF; otherwise FALSE
  113. */
  114. public function stream_eof()
  115. {
  116. return $this->pos >= strlen($this->data);
  117. }
  118. /**
  119. * Returns the position of the file pointer, i.e. its offset into the file
  120. * stream. Implements support for ftell().
  121. *
  122. * @return int
  123. */
  124. public function stream_tell()
  125. {
  126. return $this->pos;
  127. }
  128. /**
  129. * Implements support for fseek().
  130. *
  131. * @param int $offset byte offset
  132. * @param int $whence SEEK_SET, SEEK_CUR or SEEK_END
  133. * @return bool
  134. */
  135. public function stream_seek($offset, $whence)
  136. {
  137. if ($whence == SEEK_SET && $offset >= 0) {
  138. $this->pos = $offset;
  139. } elseif ($whence == SEEK_CUR && -$offset <= $this->pos) {
  140. $this->pos += $offset;
  141. } elseif ($whence == SEEK_END && -$offset <= sizeof($this->data)) {
  142. $this->pos = strlen($this->data) + $offset;
  143. } else {
  144. return false;
  145. }
  146. return true;
  147. }
  148. /**
  149. * Implements support for fstat(). Currently the only supported field is
  150. * "size".
  151. * @return array
  152. */
  153. public function stream_stat()
  154. {
  155. return array(
  156. 'size' => strlen($this->data),
  157. );
  158. }
  159. // Methods used by stream_wrapper_register() that are not implemented:
  160. // bool stream_flush ( void )
  161. // int stream_write ( string data )
  162. // bool rename ( string path_from, string path_to )
  163. // bool mkdir ( string path, int mode, int options )
  164. // bool rmdir ( string path, int options )
  165. // bool dir_opendir ( string path, int options )
  166. // array url_stat ( string path, int flags )
  167. // string dir_readdir ( void )
  168. // bool dir_rewinddir ( void )
  169. // bool dir_closedir ( void )
  170. }