PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/phpoffice/phpexcel/Classes/PHPExcel/Shared/OLE/ChainedBlockStream.php

https://gitlab.com/techniconline/kmc
PHP | 224 lines | 97 code | 18 blank | 109 comment | 23 complexity | caf26317ae5064c453b14c4aba82354f 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 ##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. ) {
  84. if ($options & STREAM_REPORT_ERRORS) {
  85. trigger_error('OLE stream not found', E_USER_WARNING);
  86. }
  87. return false;
  88. }
  89. $this->ole = $GLOBALS['_OLE_INSTANCES'][$this->params['oleInstanceId']];
  90. $blockId = $this->params['blockId'];
  91. $this->data = '';
  92. if (isset($this->params['size']) &&
  93. $this->params['size'] < $this->ole->bigBlockThreshold &&
  94. $blockId != $this->ole->root->_StartBlock
  95. ) {
  96. // Block id refers to small blocks
  97. $rootPos = $this->ole->_getBlockOffset($this->ole->root->_StartBlock);
  98. while ($blockId != -2) {
  99. $pos = $rootPos + $blockId * $this->ole->bigBlockSize;
  100. $blockId = $this->ole->sbat[$blockId];
  101. fseek($this->ole->_file_handle, $pos);
  102. $this->data .= fread($this->ole->_file_handle, $this->ole->bigBlockSize);
  103. }
  104. } else {
  105. // Block id refers to big blocks
  106. while ($blockId != -2) {
  107. $pos = $this->ole->_getBlockOffset($blockId);
  108. fseek($this->ole->_file_handle, $pos);
  109. $this->data .= fread($this->ole->_file_handle, $this->ole->bigBlockSize);
  110. $blockId = $this->ole->bbat[$blockId];
  111. }
  112. }
  113. if (isset($this->params['size'])) {
  114. $this->data = substr($this->data, 0, $this->params['size']);
  115. }
  116. if ($options & STREAM_USE_PATH) {
  117. $openedPath = $path;
  118. }
  119. return true;
  120. }
  121. /**
  122. * Implements support for fclose().
  123. *
  124. */
  125. public function stream_close()
  126. {
  127. $this->ole = null;
  128. unset($GLOBALS['_OLE_INSTANCES']);
  129. }
  130. /**
  131. * Implements support for fread(), fgets() etc.
  132. *
  133. * @param int $count maximum number of bytes to read
  134. * @return string
  135. */
  136. public function stream_read($count)
  137. {
  138. if ($this->stream_eof()) {
  139. return false;
  140. }
  141. $s = substr($this->data, $this->pos, $count);
  142. $this->pos += $count;
  143. return $s;
  144. }
  145. /**
  146. * Implements support for feof().
  147. *
  148. * @return bool TRUE if the file pointer is at EOF; otherwise FALSE
  149. */
  150. public function stream_eof()
  151. {
  152. return $this->pos >= strlen($this->data);
  153. }
  154. /**
  155. * Returns the position of the file pointer, i.e. its offset into the file
  156. * stream. Implements support for ftell().
  157. *
  158. * @return int
  159. */
  160. public function stream_tell()
  161. {
  162. return $this->pos;
  163. }
  164. /**
  165. * Implements support for fseek().
  166. *
  167. * @param int $offset byte offset
  168. * @param int $whence SEEK_SET, SEEK_CUR or SEEK_END
  169. * @return bool
  170. */
  171. public function stream_seek($offset, $whence)
  172. {
  173. if ($whence == SEEK_SET && $offset >= 0) {
  174. $this->pos = $offset;
  175. } elseif ($whence == SEEK_CUR && -$offset <= $this->pos) {
  176. $this->pos += $offset;
  177. } elseif ($whence == SEEK_END && -$offset <= sizeof($this->data)) {
  178. $this->pos = strlen($this->data) + $offset;
  179. } else {
  180. return false;
  181. }
  182. return true;
  183. }
  184. /**
  185. * Implements support for fstat(). Currently the only supported field is
  186. * "size".
  187. * @return array
  188. */
  189. public function stream_stat()
  190. {
  191. return array(
  192. 'size' => strlen($this->data),
  193. );
  194. }
  195. // Methods used by stream_wrapper_register() that are not implemented:
  196. // bool stream_flush ( void )
  197. // int stream_write ( string data )
  198. // bool rename ( string path_from, string path_to )
  199. // bool mkdir ( string path, int mode, int options )
  200. // bool rmdir ( string path, int options )
  201. // bool dir_opendir ( string path, int options )
  202. // array url_stat ( string path, int flags )
  203. // string dir_readdir ( void )
  204. // bool dir_rewinddir ( void )
  205. // bool dir_closedir ( void )
  206. }