ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
ChainedBlockStream.php
Go to the documentation of this file.
1 <?php
2 
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4 
25 require_once 'PEAR.php';
26 require_once 'OLE.php';
27 
42 {
47  var $ole;
48 
53  var $params;
54 
59  var $data;
60 
65  var $pos;
66 
77  function stream_open($path, $mode, $options, &$openedPath)
78  {
79  if ($mode != 'r') {
80  if ($options & STREAM_REPORT_ERRORS) {
81  trigger_error('Only reading is supported', E_USER_WARNING);
82  }
83  return false;
84  }
85 
86  // 25 is length of "ole-chainedblockstream://"
87  parse_str(substr($path, 25), $this->params);
88  if (!isset($this->params['oleInstanceId'],
89  $this->params['blockId'],
90  $GLOBALS['_OLE_INSTANCES'][$this->params['oleInstanceId']])) {
91 
92  if ($options & STREAM_REPORT_ERRORS) {
93  trigger_error('OLE stream not found', E_USER_WARNING);
94  }
95  return false;
96  }
97  $this->ole = $GLOBALS['_OLE_INSTANCES'][$this->params['oleInstanceId']];
98 
99  $blockId = $this->params['blockId'];
100  $this->data = '';
101  if (isset($this->params['size']) &&
102  $this->params['size'] < $this->ole->bigBlockThreshold &&
103  $blockId != $this->ole->root->_StartBlock) {
104 
105  // Block id refers to small blocks
106  $rootPos = $this->ole->_getBlockOffset($this->ole->root->_StartBlock);
107  while ($blockId != -2) {
108  $pos = $rootPos + $blockId * $this->ole->bigBlockSize;
109  $blockId = $this->ole->sbat[$blockId];
110  fseek($this->ole->_file_handle, $pos);
111  $this->data .= fread($this->ole->_file_handle, $this->ole->bigBlockSize);
112  }
113  } else {
114  // Block id refers to big blocks
115  while ($blockId != -2) {
116  $pos = $this->ole->_getBlockOffset($blockId);
117  fseek($this->ole->_file_handle, $pos);
118  $this->data .= fread($this->ole->_file_handle, $this->ole->bigBlockSize);
119  $blockId = $this->ole->bbat[$blockId];
120  }
121  }
122  if (isset($this->params['size'])) {
123  $this->data = substr($this->data, 0, $this->params['size']);
124  }
125 
126  if ($options & STREAM_USE_PATH) {
127  $openedPath = $path;
128  }
129 
130  return true;
131  }
132 
137  function stream_close()
138  {
139  $this->ole = null;
140  unset($GLOBALS['_OLE_INSTANCES']);
141  }
142 
148  function stream_read($count)
149  {
150  if ($this->stream_eof()) {
151  return false;
152  }
153  $s = substr($this->data, $this->pos, $count);
154  $this->pos += $count;
155  return $s;
156  }
157 
162  function stream_eof()
163  {
164  $eof = $this->pos >= strlen($this->data);
165  // Workaround for bug in PHP 5.0.x: http://bugs.php.net/27508
166  if (version_compare(PHP_VERSION, '5.0', '>=') &&
167  version_compare(PHP_VERSION, '5.1', '<')) {
168 
169  $eof = !$eof;
170  }
171  return $eof;
172  }
173 
179  function stream_tell()
180  {
181  return $this->pos;
182  }
183 
190  function stream_seek($offset, $whence)
191  {
192  if ($whence == SEEK_SET && $offset >= 0) {
193  $this->pos = $offset;
194  } elseif ($whence == SEEK_CUR && -$offset <= $this->pos) {
195  $this->pos += $offset;
196  } elseif ($whence == SEEK_END && -$offset <= sizeof($this->data)) {
197  $this->pos = strlen($this->data) + $offset;
198  } else {
199  return false;
200  }
201  return true;
202  }
203 
209  function stream_stat()
210  {
211  return array(
212  'size' => strlen($this->data),
213  );
214  }
215 
216  // Methods used by stream_wrapper_register() that are not implemented:
217  // bool stream_flush ( void )
218  // int stream_write ( string data )
219  // bool rename ( string path_from, string path_to )
220  // bool mkdir ( string path, int mode, int options )
221  // bool rmdir ( string path, int options )
222  // bool dir_opendir ( string path, int options )
223  // array url_stat ( string path, int flags )
224  // string dir_readdir ( void )
225  // bool dir_rewinddir ( void )
226  // bool dir_closedir ( void )
227 }
228 
229 ?>