[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/phpexcel/PHPExcel/Shared/OLE/ -> ChainedBlockStream.php (source)

   1  <?php
   2  
   3  /**
   4   * PHPExcel_Shared_OLE_ChainedBlockStream
   5   *
   6   * Copyright (c) 2006 - 2015 PHPExcel
   7   *
   8   * This library is free software; you can redistribute it and/or
   9   * modify it under the terms of the GNU Lesser General Public
  10   * License as published by the Free Software Foundation; either
  11   * version 2.1 of the License, or (at your option) any later version.
  12   *
  13   * This library is distributed in the hope that it will be useful,
  14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16   * Lesser General Public License for more details.
  17   *
  18   * You should have received a copy of the GNU Lesser General Public
  19   * License along with this library; if not, write to the Free Software
  20   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  21   *
  22   * @category   PHPExcel
  23   * @package    PHPExcel_Shared_OLE
  24   * @copyright  Copyright (c) 2006 - 2007 Christian Schmidt
  25   * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  26   * @version ##VERSION##, ##DATE##
  27   */
  28  class PHPExcel_Shared_OLE_ChainedBlockStream
  29  {
  30      /**
  31       * The OLE container of the file that is being read.
  32       * @var OLE
  33       */
  34      public $ole;
  35  
  36      /**
  37       * Parameters specified by fopen().
  38       * @var array
  39       */
  40      public $params;
  41  
  42      /**
  43       * The binary data of the file.
  44       * @var  string
  45       */
  46      public $data;
  47  
  48      /**
  49       * The file pointer.
  50       * @var  int  byte offset
  51       */
  52      public $pos;
  53  
  54      /**
  55       * Implements support for fopen().
  56       * For creating streams using this wrapper, use OLE_PPS_File::getStream().
  57       *
  58       * @param    string    $path            resource name including scheme, e.g.
  59       *                                    ole-chainedblockstream://oleInstanceId=1
  60       * @param    string    $mode            only "r" is supported
  61       * @param    int        $options        mask of STREAM_REPORT_ERRORS and STREAM_USE_PATH
  62       * @param    string  &$openedPath    absolute path of the opened stream (out parameter)
  63       * @return    bool    true on success
  64       */
  65      public function stream_open($path, $mode, $options, &$openedPath)
  66      {
  67          if ($mode != 'r') {
  68              if ($options & STREAM_REPORT_ERRORS) {
  69                  trigger_error('Only reading is supported', E_USER_WARNING);
  70              }
  71              return false;
  72          }
  73  
  74          // 25 is length of "ole-chainedblockstream://"
  75          parse_str(substr($path, 25), $this->params);
  76          if (!isset($this->params['oleInstanceId'], $this->params['blockId'], $GLOBALS['_OLE_INSTANCES'][$this->params['oleInstanceId']])) {
  77              if ($options & STREAM_REPORT_ERRORS) {
  78                  trigger_error('OLE stream not found', E_USER_WARNING);
  79              }
  80              return false;
  81          }
  82          $this->ole = $GLOBALS['_OLE_INSTANCES'][$this->params['oleInstanceId']];
  83  
  84          $blockId = $this->params['blockId'];
  85          $this->data = '';
  86          if (isset($this->params['size']) && $this->params['size'] < $this->ole->bigBlockThreshold && $blockId != $this->ole->root->_StartBlock) {
  87              // Block id refers to small blocks
  88              $rootPos = $this->ole->_getBlockOffset($this->ole->root->_StartBlock);
  89              while ($blockId != -2) {
  90                  $pos = $rootPos + $blockId * $this->ole->bigBlockSize;
  91                  $blockId = $this->ole->sbat[$blockId];
  92                  fseek($this->ole->_file_handle, $pos);
  93                  $this->data .= fread($this->ole->_file_handle, $this->ole->bigBlockSize);
  94              }
  95          } else {
  96              // Block id refers to big blocks
  97              while ($blockId != -2) {
  98                  $pos = $this->ole->_getBlockOffset($blockId);
  99                  fseek($this->ole->_file_handle, $pos);
 100                  $this->data .= fread($this->ole->_file_handle, $this->ole->bigBlockSize);
 101                  $blockId = $this->ole->bbat[$blockId];
 102              }
 103          }
 104          if (isset($this->params['size'])) {
 105              $this->data = substr($this->data, 0, $this->params['size']);
 106          }
 107  
 108          if ($options & STREAM_USE_PATH) {
 109              $openedPath = $path;
 110          }
 111  
 112          return true;
 113      }
 114  
 115      /**
 116       * Implements support for fclose().
 117       *
 118       */
 119      public function stream_close()
 120      {
 121          $this->ole = null;
 122          unset($GLOBALS['_OLE_INSTANCES']);
 123      }
 124  
 125      /**
 126       * Implements support for fread(), fgets() etc.
 127       *
 128       * @param   int        $count    maximum number of bytes to read
 129       * @return  string
 130       */
 131      public function stream_read($count)
 132      {
 133          if ($this->stream_eof()) {
 134              return false;
 135          }
 136          $s = substr($this->data, $this->pos, $count);
 137          $this->pos += $count;
 138          return $s;
 139      }
 140  
 141      /**
 142       * Implements support for feof().
 143       *
 144       * @return  bool  TRUE if the file pointer is at EOF; otherwise FALSE
 145       */
 146      public function stream_eof()
 147      {
 148          return $this->pos >= strlen($this->data);
 149      }
 150  
 151      /**
 152       * Returns the position of the file pointer, i.e. its offset into the file
 153       * stream. Implements support for ftell().
 154       *
 155       * @return  int
 156       */
 157      public function stream_tell()
 158      {
 159          return $this->pos;
 160      }
 161  
 162      /**
 163       * Implements support for fseek().
 164       *
 165       * @param    int        $offset    byte offset
 166       * @param    int        $whence    SEEK_SET, SEEK_CUR or SEEK_END
 167       * @return    bool
 168       */
 169      public function stream_seek($offset, $whence)
 170      {
 171          if ($whence == SEEK_SET && $offset >= 0) {
 172              $this->pos = $offset;
 173          } elseif ($whence == SEEK_CUR && -$offset <= $this->pos) {
 174              $this->pos += $offset;
 175          } elseif ($whence == SEEK_END && -$offset <= sizeof($this->data)) {
 176              $this->pos = strlen($this->data) + $offset;
 177          } else {
 178              return false;
 179          }
 180          return true;
 181      }
 182  
 183      /**
 184       * Implements support for fstat(). Currently the only supported field is
 185       * "size".
 186       * @return  array
 187       */
 188      public function stream_stat()
 189      {
 190          return array(
 191              'size' => strlen($this->data),
 192              );
 193      }
 194  
 195      // Methods used by stream_wrapper_register() that are not implemented:
 196      // bool stream_flush ( void )
 197      // int stream_write ( string data )
 198      // bool rename ( string path_from, string path_to )
 199      // bool mkdir ( string path, int mode, int options )
 200      // bool rmdir ( string path, int options )
 201      // bool dir_opendir ( string path, int options )
 202      // array url_stat ( string path, int flags )
 203      // string dir_readdir ( void )
 204      // bool dir_rewinddir ( void )
 205      // bool dir_closedir ( void )
 206  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1