ChainedBlockStream.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Stream wrapper for reading data stored in an OLE file.
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: This source file is subject to version 3.0 of the PHP license
  9. * that is available through the world-wide-web at the following URI:
  10. * http://www.php.net/license/3_0.txt. If you did not receive a copy of
  11. * the PHP License and are unable to obtain it through the web, please
  12. * send a note to license@php.net so we can mail you a copy immediately.
  13. *
  14. * @category Structures
  15. * @package OLE
  16. * @author Christian Schmidt <schmidt@php.net>
  17. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  18. * @version CVS: $Id: ChainedBlockStream.php,v 1.1 2007/02/13 21:00:42 schmidt Exp $
  19. * @link http://pear.php.net/package/OLE
  20. * @since File available since Release 0.6.0
  21. */
  22. /**
  23. * Stream wrapper for reading data stored in an OLE file. Implements methods
  24. * for PHP's stream_wrapper_register(). For creating streams using this
  25. * wrapper, use OLE_PPS_File::getStream().
  26. *
  27. * @category Structures
  28. * @package OLE
  29. * @author Christian Schmidt <schmidt@php.net>
  30. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  31. * @version Release: @package_version@
  32. * @link http://pear.php.net/package/OLE
  33. * @since Class available since Release 0.6.0
  34. */
  35. class OLE_ChainedBlockStream extends PEAR
  36. {
  37. /**
  38. * The OLE container of the file that is being read.
  39. * @var OLE
  40. */
  41. var $ole;
  42. /**
  43. * Parameters specified by fopen().
  44. * @var array
  45. */
  46. var $params;
  47. /**
  48. * The binary data of the file.
  49. * @var string
  50. */
  51. var $data;
  52. /**
  53. * The file pointer.
  54. * @var int byte offset
  55. */
  56. var $pos;
  57. /**
  58. * Implements support for fopen().
  59. * For creating streams using this wrapper, use OLE_PPS_File::getStream().
  60. * @param string resource name including scheme, e.g.
  61. * ole-chainedblockstream://oleInstanceId=1
  62. * @param string only "r" is supported
  63. * @param int mask of STREAM_REPORT_ERRORS and STREAM_USE_PATH
  64. * @param string absolute path of the opened stream (out parameter)
  65. * @return bool true on success
  66. */
  67. function stream_open($path, $mode, $options, &$openedPath)
  68. {
  69. if ($mode != 'r') {
  70. if ($options & STREAM_REPORT_ERRORS) {
  71. trigger_error('Only reading is supported', E_USER_WARNING);
  72. }
  73. return false;
  74. }
  75. // 25 is length of "ole-chainedblockstream://"
  76. parse_str(substr($path, 25), $this->params);
  77. if (!isset($this->params['oleInstanceId'],
  78. $this->params['blockId'],
  79. $GLOBALS['_OLE_INSTANCES'][$this->params['oleInstanceId']])) {
  80. if ($options & STREAM_REPORT_ERRORS) {
  81. trigger_error('OLE stream not found', E_USER_WARNING);
  82. }
  83. return false;
  84. }
  85. $this->ole = $GLOBALS['_OLE_INSTANCES'][$this->params['oleInstanceId']];
  86. $blockId = $this->params['blockId'];
  87. $this->data = '';
  88. if (isset($this->params['size']) &&
  89. $this->params['size'] < $this->ole->bigBlockThreshold &&
  90. $blockId != $this->ole->root->_StartBlock) {
  91. // Block id refers to small blocks
  92. $rootPos = $this->ole->_getBlockOffset($this->ole->root->_StartBlock);
  93. while ($blockId != -2) {
  94. $pos = $rootPos + $blockId * $this->ole->bigBlockSize;
  95. $blockId = $this->ole->sbat[$blockId];
  96. fseek($this->ole->_file_handle, $pos);
  97. $this->data .= fread($this->ole->_file_handle, $this->ole->bigBlockSize);
  98. }
  99. } else {
  100. // Block id refers to big blocks
  101. while ($blockId != -2) {
  102. $pos = $this->ole->_getBlockOffset($blockId);
  103. fseek($this->ole->_file_handle, $pos);
  104. $this->data .= fread($this->ole->_file_handle, $this->ole->bigBlockSize);
  105. $blockId = $this->ole->bbat[$blockId];
  106. }
  107. }
  108. if (isset($this->params['size'])) {
  109. $this->data = substr($this->data, 0, $this->params['size']);
  110. }
  111. if ($options & STREAM_USE_PATH) {
  112. $openedPath = $path;
  113. }
  114. return true;
  115. }
  116. /**
  117. * Implements support for fclose().
  118. * @return string
  119. */
  120. function stream_close()
  121. {
  122. $this->ole = null;
  123. unset($GLOBALS['_OLE_INSTANCES']);
  124. }
  125. /**
  126. * Implements support for fread(), fgets() etc.
  127. * @param int maximum number of bytes to read
  128. * @return string
  129. */
  130. function stream_read($count)
  131. {
  132. if ($this->stream_eof()) {
  133. return false;
  134. }
  135. $s = substr($this->data, $this->pos, $count);
  136. $this->pos += $count;
  137. return $s;
  138. }
  139. /**
  140. * Implements support for feof().
  141. * @return bool TRUE if the file pointer is at EOF; otherwise FALSE
  142. */
  143. function stream_eof()
  144. {
  145. $eof = $this->pos >= strlen($this->data);
  146. // Workaround for bug in PHP 5.0.x: http://bugs.php.net/27508
  147. if (version_compare(PHP_VERSION, '5.0', '>=') &&
  148. version_compare(PHP_VERSION, '5.1', '<')) {
  149. $eof = !$eof;
  150. }
  151. return $eof;
  152. }
  153. /**
  154. * Returns the position of the file pointer, i.e. its offset into the file
  155. * stream. Implements support for ftell().
  156. * @return int
  157. */
  158. function stream_tell()
  159. {
  160. return $this->pos;
  161. }
  162. /**
  163. * Implements support for fseek().
  164. * @param int byte offset
  165. * @param int SEEK_SET, SEEK_CUR or SEEK_END
  166. * @return bool
  167. */
  168. function stream_seek($offset, $whence)
  169. {
  170. if ($whence == SEEK_SET && $offset >= 0) {
  171. $this->pos = $offset;
  172. } elseif ($whence == SEEK_CUR && -$offset <= $this->pos) {
  173. $this->pos += $offset;
  174. } elseif ($whence == SEEK_END && -$offset <= sizeof($this->data)) {
  175. $this->pos = strlen($this->data) + $offset;
  176. } else {
  177. return false;
  178. }
  179. return true;
  180. }
  181. /**
  182. * Implements support for fstat(). Currently the only supported field is
  183. * "size".
  184. * @return array
  185. */
  186. function stream_stat()
  187. {
  188. return array(
  189. 'size' => strlen($this->data),
  190. );
  191. }
  192. // Methods used by stream_wrapper_register() that are not implemented:
  193. // bool stream_flush ( void )
  194. // int stream_write ( string data )
  195. // bool rename ( string path_from, string path_to )
  196. // bool mkdir ( string path, int mode, int options )
  197. // bool rmdir ( string path, int options )
  198. // bool dir_opendir ( string path, int options )
  199. // array url_stat ( string path, int flags )
  200. // string dir_readdir ( void )
  201. // bool dir_rewinddir ( void )
  202. // bool dir_closedir ( void )
  203. }
  204. ?>