ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
ChainedBlockStream.php
Go to the documentation of this file.
1 <?php
29 if (!defined('PHPEXCEL_ROOT')) {
33  define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../../');
34 }
35 
36 require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/OLE.php';
37 
49 {
54  public $ole;
55 
60  public $params;
61 
66  public $data;
67 
72  public $pos;
73 
84  public function stream_open($path, $mode, $options, &$openedPath)
85  {
86  if ($mode != 'r') {
87  if ($options & STREAM_REPORT_ERRORS) {
88  trigger_error('Only reading is supported', E_USER_WARNING);
89  }
90  return false;
91  }
92 
93  // 25 is length of "ole-chainedblockstream://"
94  parse_str(substr($path, 25), $this->params);
95  if (!isset($this->params['oleInstanceId'],
96  $this->params['blockId'],
97  $GLOBALS['_OLE_INSTANCES'][$this->params['oleInstanceId']])) {
98 
99  if ($options & STREAM_REPORT_ERRORS) {
100  trigger_error('OLE stream not found', E_USER_WARNING);
101  }
102  return false;
103  }
104  $this->ole = $GLOBALS['_OLE_INSTANCES'][$this->params['oleInstanceId']];
105 
106  $blockId = $this->params['blockId'];
107  $this->data = '';
108  if (isset($this->params['size']) &&
109  $this->params['size'] < $this->ole->bigBlockThreshold &&
110  $blockId != $this->ole->root->_StartBlock) {
111 
112  // Block id refers to small blocks
113  $rootPos = $this->ole->_getBlockOffset($this->ole->root->_StartBlock);
114  while ($blockId != -2) {
115  $pos = $rootPos + $blockId * $this->ole->bigBlockSize;
116  $blockId = $this->ole->sbat[$blockId];
117  fseek($this->ole->_file_handle, $pos);
118  $this->data .= fread($this->ole->_file_handle, $this->ole->bigBlockSize);
119  }
120  } else {
121  // Block id refers to big blocks
122  while ($blockId != -2) {
123  $pos = $this->ole->_getBlockOffset($blockId);
124  fseek($this->ole->_file_handle, $pos);
125  $this->data .= fread($this->ole->_file_handle, $this->ole->bigBlockSize);
126  $blockId = $this->ole->bbat[$blockId];
127  }
128  }
129  if (isset($this->params['size'])) {
130  $this->data = substr($this->data, 0, $this->params['size']);
131  }
132 
133  if ($options & STREAM_USE_PATH) {
134  $openedPath = $path;
135  }
136 
137  return true;
138  }
139 
144  public function stream_close()
145  {
146  $this->ole = null;
147  unset($GLOBALS['_OLE_INSTANCES']);
148  }
149 
155  public function stream_read($count)
156  {
157  if ($this->stream_eof()) {
158  return false;
159  }
160  $s = substr($this->data, $this->pos, $count);
161  $this->pos += $count;
162  return $s;
163  }
164 
169  public function stream_eof()
170  {
171  $eof = $this->pos >= strlen($this->data);
172  // Workaround for bug in PHP 5.0.x: http://bugs.php.net/27508
173  if (version_compare(PHP_VERSION, '5.0', '>=') &&
174  version_compare(PHP_VERSION, '5.1', '<')) {
175 
176  $eof = !$eof;
177  }
178  return $eof;
179  }
180 
186  public function stream_tell()
187  {
188  return $this->pos;
189  }
190 
197  public function stream_seek($offset, $whence)
198  {
199  if ($whence == SEEK_SET && $offset >= 0) {
200  $this->pos = $offset;
201  } elseif ($whence == SEEK_CUR && -$offset <= $this->pos) {
202  $this->pos += $offset;
203  } elseif ($whence == SEEK_END && -$offset <= sizeof($this->data)) {
204  $this->pos = strlen($this->data) + $offset;
205  } else {
206  return false;
207  }
208  return true;
209  }
210 
216  public function stream_stat()
217  {
218  return array(
219  'size' => strlen($this->data),
220  );
221  }
222 
223  // Methods used by stream_wrapper_register() that are not implemented:
224  // bool stream_flush ( void )
225  // int stream_write ( string data )
226  // bool rename ( string path_from, string path_to )
227  // bool mkdir ( string path, int mode, int options )
228  // bool rmdir ( string path, int options )
229  // bool dir_opendir ( string path, int options )
230  // array url_stat ( string path, int flags )
231  // string dir_readdir ( void )
232  // bool dir_rewinddir ( void )
233  // bool dir_closedir ( void )
234 }