PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/phpexcel/PHPExcel/Shared/OLE/ChainedBlockStream.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 237 lines | 113 code | 19 blank | 105 comment | 25 complexity | e036ffe05c0d651469fcfd7e3a3caea5 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (C) 2006 - 2011 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.7.6, 2011-02-27
  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. * @param string resource name including scheme, e.g.
  63. * ole-chainedblockstream://oleInstanceId=1
  64. * @param string only "r" is supported
  65. * @param int mask of STREAM_REPORT_ERRORS and STREAM_USE_PATH
  66. * @param string absolute path of the opened stream (out parameter)
  67. * @return bool true on success
  68. */
  69. public function stream_open($path, $mode, $options, &$openedPath)
  70. {
  71. if ($mode != 'r')
  72. {
  73. if ($options & STREAM_REPORT_ERRORS)
  74. {
  75. trigger_error('Only reading is supported', E_USER_WARNING);
  76. }
  77. return false;
  78. }
  79. // 25 is length of "ole-chainedblockstream://"
  80. parse_str(substr($path, 25), $this->params);
  81. if (! isset($this->params['oleInstanceId'], $this->params['blockId'], $GLOBALS['_OLE_INSTANCES'][$this->params['oleInstanceId']]))
  82. {
  83. if ($options & STREAM_REPORT_ERRORS)
  84. {
  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']) && $this->params['size'] < $this->ole->bigBlockThreshold && $blockId != $this->ole->root->_StartBlock)
  93. {
  94. // Block id refers to small blocks
  95. $rootPos = $this->ole->_getBlockOffset($this->ole->root->_StartBlock);
  96. while ($blockId != - 2)
  97. {
  98. $pos = $rootPos + $blockId * $this->ole->bigBlockSize;
  99. $blockId = $this->ole->sbat[$blockId];
  100. fseek($this->ole->_file_handle, $pos);
  101. $this->data .= fread($this->ole->_file_handle, $this->ole->bigBlockSize);
  102. }
  103. }
  104. else
  105. {
  106. // Block id refers to big blocks
  107. while ($blockId != - 2)
  108. {
  109. $pos = $this->ole->_getBlockOffset($blockId);
  110. fseek($this->ole->_file_handle, $pos);
  111. $this->data .= fread($this->ole->_file_handle, $this->ole->bigBlockSize);
  112. $blockId = $this->ole->bbat[$blockId];
  113. }
  114. }
  115. if (isset($this->params['size']))
  116. {
  117. $this->data = substr($this->data, 0, $this->params['size']);
  118. }
  119. if ($options & STREAM_USE_PATH)
  120. {
  121. $openedPath = $path;
  122. }
  123. return true;
  124. }
  125. /**
  126. * Implements support for fclose().
  127. * @return string
  128. */
  129. public function stream_close()
  130. {
  131. $this->ole = null;
  132. unset($GLOBALS['_OLE_INSTANCES']);
  133. }
  134. /**
  135. * Implements support for fread(), fgets() etc.
  136. * @param int maximum number of bytes to read
  137. * @return string
  138. */
  139. public function stream_read($count)
  140. {
  141. if ($this->stream_eof())
  142. {
  143. return false;
  144. }
  145. $s = substr($this->data, $this->pos, $count);
  146. $this->pos += $count;
  147. return $s;
  148. }
  149. /**
  150. * Implements support for feof().
  151. * @return bool TRUE if the file pointer is at EOF; otherwise FALSE
  152. */
  153. public function stream_eof()
  154. {
  155. $eof = $this->pos >= strlen($this->data);
  156. // Workaround for bug in PHP 5.0.x: http://bugs.php.net/27508
  157. if (version_compare(PHP_VERSION, '5.0', '>=') && version_compare(PHP_VERSION, '5.1', '<'))
  158. {
  159. $eof = ! $eof;
  160. }
  161. return $eof;
  162. }
  163. /**
  164. * Returns the position of the file pointer, i.e. its offset into the file
  165. * stream. Implements support for ftell().
  166. * @return int
  167. */
  168. public function stream_tell()
  169. {
  170. return $this->pos;
  171. }
  172. /**
  173. * Implements support for fseek().
  174. * @param int byte offset
  175. * @param int SEEK_SET, SEEK_CUR or SEEK_END
  176. * @return bool
  177. */
  178. public function stream_seek($offset, $whence)
  179. {
  180. if ($whence == SEEK_SET && $offset >= 0)
  181. {
  182. $this->pos = $offset;
  183. }
  184. elseif ($whence == SEEK_CUR && - $offset <= $this->pos)
  185. {
  186. $this->pos += $offset;
  187. }
  188. elseif ($whence == SEEK_END && - $offset <= sizeof($this->data))
  189. {
  190. $this->pos = strlen($this->data) + $offset;
  191. }
  192. else
  193. {
  194. return false;
  195. }
  196. return true;
  197. }
  198. /**
  199. * Implements support for fstat(). Currently the only supported field is
  200. * "size".
  201. * @return array
  202. */
  203. public function stream_stat()
  204. {
  205. return array('size' => strlen($this->data));
  206. }
  207. // Methods used by stream_wrapper_register() that are not implemented:
  208. // bool stream_flush ( void )
  209. // int stream_write ( string data )
  210. // bool rename ( string path_from, string path_to )
  211. // bool mkdir ( string path, int mode, int options )
  212. // bool rmdir ( string path, int options )
  213. // bool dir_opendir ( string path, int options )
  214. // array url_stat ( string path, int flags )
  215. // string dir_readdir ( void )
  216. // bool dir_rewinddir ( void )
  217. // bool dir_closedir ( void )
  218. }