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 - 2009 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.0, 2009-08-10
  26.  */
  27.  
  28. /** PHPExcel root directory */
  29. if (!defined('PHPEXCEL_ROOT')) {
  30.     /**
  31.      * @ignore
  32.      */
  33.     define('PHPEXCEL_ROOT'dirname(__FILE__'/../../../');
  34. }
  35.  
  36. require_once PHPEXCEL_ROOT 'PHPExcel/Shared/OLE.php';
  37.  
  38. /**
  39.  * PHPExcel_Shared_OLE_ChainedBlockStream
  40.  *
  41.  * Stream wrapper for reading data stored in an OLE file. Implements methods
  42.  * for PHP's stream_wrapper_register(). For creating streams using this
  43.  * wrapper, use PHPExcel_Shared_OLE_PPS_File::getStream().
  44.  *
  45.  * @category   PHPExcel
  46.  * @package    PHPExcel_Shared_OLE
  47.  */
  48. {
  49.     /**
  50.      * The OLE container of the file that is being read.
  51.      * @var OLE 
  52.      */
  53.     public $ole;
  54.  
  55.     /**
  56.      * Parameters specified by fopen().
  57.      * @var array 
  58.      */
  59.     public $params;
  60.  
  61.     /**
  62.      * The binary data of the file.
  63.      * @var  string 
  64.      */
  65.     public $data;
  66.  
  67.     /**
  68.      * The file pointer.
  69.      * @var  int  byte offset
  70.      */
  71.     public $pos;
  72.  
  73.     /**
  74.      * Implements support for fopen().
  75.      * For creating streams using this wrapper, use OLE_PPS_File::getStream().
  76.      * @param  string  resource name including scheme, e.g.
  77.      *                  ole-chainedblockstream://oleInstanceId=1
  78.      * @param  string  only "r" is supported
  79.      * @param  int     mask of STREAM_REPORT_ERRORS and STREAM_USE_PATH
  80.      * @param  string  absolute path of the opened stream (out parameter)
  81.      * @return bool    true on success
  82.      */
  83.     public function stream_open($path$mode$options&$openedPath)
  84.     {
  85.         if ($mode != 'r'{
  86.             if ($options STREAM_REPORT_ERRORS{
  87.                 trigger_error('Only reading is supported'E_USER_WARNING);
  88.             }
  89.             return false;
  90.         }
  91.  
  92.         // 25 is length of "ole-chainedblockstream://"
  93.         parse_str(substr($path25)$this->params);
  94.         if (!isset($this->params['oleInstanceId'],
  95.                    $this->params['blockId'],
  96.                    $GLOBALS['_OLE_INSTANCES'][$this->params['oleInstanceId']])) {
  97.  
  98.             if ($options STREAM_REPORT_ERRORS{
  99.                 trigger_error('OLE stream not found'E_USER_WARNING);
  100.             }
  101.             return false;
  102.         }
  103.         $this->ole = $GLOBALS['_OLE_INSTANCES'][$this->params['oleInstanceId']];
  104.  
  105.         $blockId $this->params['blockId'];
  106.         $this->data = '';
  107.         if (isset($this->params['size']&&
  108.             $this->params['size'$this->ole->bigBlockThreshold &&
  109.             $blockId != $this->ole->root->_StartBlock{
  110.  
  111.             // Block id refers to small blocks
  112.             $rootPos $this->ole->_getBlockOffset($this->ole->root->_StartBlock);
  113.             while ($blockId != -2{
  114.                 $pos $rootPos $blockId $this->ole->bigBlockSize;
  115.                 $blockId $this->ole->sbat[$blockId];
  116.                 fseek($this->ole->_file_handle$pos);
  117.                 $this->data .= fread($this->ole->_file_handle$this->ole->bigBlockSize);
  118.             }
  119.         else {
  120.             // Block id refers to big blocks
  121.             while ($blockId != -2{
  122.                 $pos $this->ole->_getBlockOffset($blockId);
  123.                 fseek($this->ole->_file_handle$pos);
  124.                 $this->data .= fread($this->ole->_file_handle$this->ole->bigBlockSize);
  125.                 $blockId $this->ole->bbat[$blockId];
  126.             }
  127.         }
  128.         if (isset($this->params['size'])) {
  129.             $this->data = substr($this->data0$this->params['size']);
  130.         }
  131.  
  132.         if ($options STREAM_USE_PATH{
  133.             $openedPath $path;
  134.         }
  135.  
  136.         return true;
  137.     }
  138.  
  139.     /**
  140.      * Implements support for fclose().
  141.      * @return  string 
  142.      */
  143.     public function stream_close()
  144.     {
  145.         $this->ole = null;
  146.         unset($GLOBALS['_OLE_INSTANCES']);
  147.     }
  148.  
  149.     /**
  150.      * Implements support for fread(), fgets() etc.
  151.      * @param   int  maximum number of bytes to read
  152.      * @return  string 
  153.      */
  154.     public function stream_read($count)
  155.     {
  156.         if ($this->stream_eof()) {
  157.             return false;
  158.         }
  159.         $s substr($this->data$this->pos$count);
  160.         $this->pos += $count;
  161.         return $s;
  162.     }
  163.  
  164.     /**
  165.      * Implements support for feof().
  166.      * @return  bool  TRUE if the file pointer is at EOF; otherwise FALSE
  167.      */
  168.     public function stream_eof()
  169.     {
  170.         $eof $this->pos >= strlen($this->data);
  171.         // Workaround for bug in PHP 5.0.x: http://bugs.php.net/27508
  172.         if (version_compare(PHP_VERSION'5.0''>='&&
  173.             version_compare(PHP_VERSION'5.1''<')) {
  174.  
  175.            $eof !$eof;
  176.         }
  177.         return $eof;
  178.     }
  179.  
  180.     /**
  181.      * Returns the position of the file pointer, i.e. its offset into the file
  182.      * stream. Implements support for ftell().
  183.      * @return  int 
  184.      */
  185.     public function stream_tell()
  186.     {
  187.         return $this->pos;
  188.     }
  189.  
  190.     /**
  191.      * Implements support for fseek().
  192.      * @param   int  byte offset
  193.      * @param   int  SEEK_SET, SEEK_CUR or SEEK_END
  194.      * @return  bool 
  195.      */
  196.     public function stream_seek($offset$whence)
  197.     {
  198.         if ($whence == SEEK_SET && $offset >= 0{
  199.             $this->pos = $offset;
  200.         elseif ($whence == SEEK_CUR && -$offset <= $this->pos{
  201.             $this->pos += $offset;
  202.         elseif ($whence == SEEK_END && -$offset <= sizeof($this->data)) {
  203.             $this->pos = strlen($this->data$offset;
  204.         else {
  205.             return false;
  206.         }
  207.         return true;
  208.     }
  209.  
  210.     /**
  211.      * Implements support for fstat(). Currently the only supported field is
  212.      * "size".
  213.      * @return  array 
  214.      */
  215.     public function stream_stat()
  216.     {
  217.         return array(
  218.             'size' => strlen($this->data),
  219.             );
  220.     }
  221.  
  222.     // Methods used by stream_wrapper_register() that are not implemented:
  223.     // bool stream_flush ( void )
  224.     // int stream_write ( string data )
  225.     // bool rename ( string path_from, string path_to )
  226.     // bool mkdir ( string path, int mode, int options )
  227.     // bool rmdir ( string path, int options )
  228.     // bool dir_opendir ( string path, int options )
  229.     // array url_stat ( string path, int flags )
  230.     // string dir_readdir ( void )
  231.     // bool dir_rewinddir ( void )
  232.     // bool dir_closedir ( void )
  233. }

Documentation generated on Mon, 10 Aug 2009 08:02:40 +0200 by phpDocumentor 1.4.1