ChainedBlockStream.php 5.7 KB

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