ChainedBlockStream.php 6.9 KB

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