| 1604 | raphael | 1 | <?php
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 | /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
 | 
        
           |  |  | 4 |   | 
        
           |  |  | 5 | /**
 | 
        
           |  |  | 6 |  * Stream wrapper for reading data stored in an OLE file.
 | 
        
           |  |  | 7 |  *
 | 
        
           |  |  | 8 |  * PHP versions 4 and 5
 | 
        
           |  |  | 9 |  *
 | 
        
           |  |  | 10 |  * LICENSE: This source file is subject to version 3.0 of the PHP license
 | 
        
           |  |  | 11 |  * that is available through the world-wide-web at the following URI:
 | 
        
           |  |  | 12 |  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
 | 
        
           |  |  | 13 |  * the PHP License and are unable to obtain it through the web, please
 | 
        
           |  |  | 14 |  * send a note to license@php.net so we can mail you a copy immediately.
 | 
        
           |  |  | 15 |  *
 | 
        
           |  |  | 16 |  * @category   Structures
 | 
        
           |  |  | 17 |  * @package    OLE
 | 
        
           |  |  | 18 |  * @author     Christian Schmidt <schmidt@php.net>
 | 
        
           |  |  | 19 |  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
 | 
        
           |  |  | 20 |  * @version    CVS: $Id: ChainedBlockStream.php 322771 2012-01-26 01:24:20Z clockwerx $
 | 
        
           |  |  | 21 |  * @link       http://pear.php.net/package/OLE
 | 
        
           |  |  | 22 |  * @since      File available since Release 0.6.0
 | 
        
           |  |  | 23 |  */
 | 
        
           |  |  | 24 |   | 
        
           |  |  | 25 | require_once 'PEAR.php';
 | 
        
           |  |  | 26 | require_once 'OLE.php';
 | 
        
           |  |  | 27 |   | 
        
           |  |  | 28 | /**
 | 
        
           |  |  | 29 |  * Stream wrapper for reading data stored in an OLE file. Implements methods
 | 
        
           |  |  | 30 |  * for PHP's stream_wrapper_register(). For creating streams using this
 | 
        
           |  |  | 31 |  * wrapper, use OLE_PPS_File::getStream().
 | 
        
           |  |  | 32 |  *
 | 
        
           |  |  | 33 |  * @category   Structures
 | 
        
           |  |  | 34 |  * @package    OLE
 | 
        
           |  |  | 35 |  * @author     Christian Schmidt <schmidt@php.net>
 | 
        
           |  |  | 36 |  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
 | 
        
           |  |  | 37 |  * @version    Release: @package_version@
 | 
        
           |  |  | 38 |  * @link       http://pear.php.net/package/OLE
 | 
        
           |  |  | 39 |  * @since      Class available since Release 0.6.0
 | 
        
           |  |  | 40 |  */
 | 
        
           |  |  | 41 | class OLE_ChainedBlockStream extends PEAR
 | 
        
           |  |  | 42 | {
 | 
        
           |  |  | 43 |     /**
 | 
        
           |  |  | 44 |      * The OLE container of the file that is being read.
 | 
        
           |  |  | 45 |      * @var OLE
 | 
        
           |  |  | 46 |      */
 | 
        
           |  |  | 47 |     var $ole;
 | 
        
           |  |  | 48 |   | 
        
           |  |  | 49 |     /**
 | 
        
           |  |  | 50 |      * Parameters specified by fopen().
 | 
        
           |  |  | 51 |      * @var array
 | 
        
           |  |  | 52 |      */
 | 
        
           |  |  | 53 |     var $params;
 | 
        
           |  |  | 54 |   | 
        
           |  |  | 55 |     /**
 | 
        
           |  |  | 56 |      * The binary data of the file.
 | 
        
           |  |  | 57 |      * @var  string
 | 
        
           |  |  | 58 |      */
 | 
        
           |  |  | 59 |     var $data;
 | 
        
           |  |  | 60 |   | 
        
           |  |  | 61 |     /**
 | 
        
           |  |  | 62 |      * The file pointer.
 | 
        
           |  |  | 63 |      * @var  int  byte offset
 | 
        
           |  |  | 64 |      */
 | 
        
           |  |  | 65 |     var $pos;
 | 
        
           |  |  | 66 |   | 
        
           |  |  | 67 |     /**
 | 
        
           |  |  | 68 |      * Implements support for fopen().
 | 
        
           |  |  | 69 |      * For creating streams using this wrapper, use OLE_PPS_File::getStream().
 | 
        
           |  |  | 70 |      * @param  string  resource name including scheme, e.g.
 | 
        
           |  |  | 71 |      *                 ole-chainedblockstream://oleInstanceId=1
 | 
        
           |  |  | 72 |      * @param  string  only "r" is supported
 | 
        
           |  |  | 73 |      * @param  int     mask of STREAM_REPORT_ERRORS and STREAM_USE_PATH
 | 
        
           |  |  | 74 |      * @param  string  absolute path of the opened stream (out parameter)
 | 
        
           |  |  | 75 |      * @return bool    true on success
 | 
        
           |  |  | 76 |      */
 | 
        
           |  |  | 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 |   | 
        
           |  |  | 133 |     /**
 | 
        
           |  |  | 134 |      * Implements support for fclose().
 | 
        
           |  |  | 135 |      * @return  string
 | 
        
           |  |  | 136 |      */
 | 
        
           |  |  | 137 |     function stream_close()
 | 
        
           |  |  | 138 |     {
 | 
        
           |  |  | 139 |         $this->ole = null;
 | 
        
           |  |  | 140 |         unset($GLOBALS['_OLE_INSTANCES']);
 | 
        
           |  |  | 141 |     }
 | 
        
           |  |  | 142 |   | 
        
           |  |  | 143 |     /**
 | 
        
           |  |  | 144 |      * Implements support for fread(), fgets() etc.
 | 
        
           |  |  | 145 |      * @param   int  maximum number of bytes to read
 | 
        
           |  |  | 146 |      * @return  string
 | 
        
           |  |  | 147 |      */
 | 
        
           |  |  | 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 |   | 
        
           |  |  | 158 |     /**
 | 
        
           |  |  | 159 |      * Implements support for feof().
 | 
        
           |  |  | 160 |      * @return  bool  TRUE if the file pointer is at EOF; otherwise FALSE
 | 
        
           |  |  | 161 |      */
 | 
        
           |  |  | 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 |   | 
        
           |  |  | 174 |     /**
 | 
        
           |  |  | 175 |      * Returns the position of the file pointer, i.e. its offset into the file
 | 
        
           |  |  | 176 |      * stream. Implements support for ftell().
 | 
        
           |  |  | 177 |      * @return  int
 | 
        
           |  |  | 178 |      */
 | 
        
           |  |  | 179 |     function stream_tell()
 | 
        
           |  |  | 180 |     {
 | 
        
           |  |  | 181 |         return $this->pos;
 | 
        
           |  |  | 182 |     }
 | 
        
           |  |  | 183 |   | 
        
           |  |  | 184 |     /**
 | 
        
           |  |  | 185 |      * Implements support for fseek().
 | 
        
           |  |  | 186 |      * @param   int  byte offset
 | 
        
           |  |  | 187 |      * @param   int  SEEK_SET, SEEK_CUR or SEEK_END
 | 
        
           |  |  | 188 |      * @return  bool
 | 
        
           |  |  | 189 |      */
 | 
        
           |  |  | 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 |   | 
        
           |  |  | 204 |     /**
 | 
        
           |  |  | 205 |      * Implements support for fstat(). Currently the only supported field is
 | 
        
           |  |  | 206 |      * "size".
 | 
        
           |  |  | 207 |      * @return  array
 | 
        
           |  |  | 208 |      */
 | 
        
           |  |  | 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 | ?>
 |