PHPExcel_Shared_OLE
[ class tree: PHPExcel_Shared_OLE ] [ index: PHPExcel_Shared_OLE ] [ all elements ]

Source for file ChainedBlockStream.php

Documentation is available at ChainedBlockStream.php

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

Documentation generated on Sun, 27 Feb 2011 16:29:00 -0800 by phpDocumentor 1.4.3