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

Source for file OLE.php

Documentation is available at OLE.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2002 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Xavier Noguer <xnoguer@php.net>                              |
  17. // | Based on OLE::Storage_Lite by Kawai, Takanori                        |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: OLE.php,v 1.13 2007/03/07 14:38:25 schmidt Exp $
  21.  
  22. require_once 'PHPExcel/Shared/OLE.php';
  23. require_once 'PHPExcel/Shared/OLE/OLE_PPS.php';
  24. require_once 'PHPExcel/Shared/OLE/OLE_File.php';
  25. require_once 'PHPExcel/Shared/OLE/OLE_Root.php';
  26. require_once 'PHPExcel/Shared/OLE/ChainedBlockStream.php';
  27.  
  28. /**
  29. * Array for storing OLE instances that are accessed from
  30. * OLE_ChainedBlockStream::stream_open().
  31. * @var  array 
  32. */
  33. $GLOBALS['_OLE_INSTANCES'] = array();
  34.  
  35. /**
  36. * OLE package base class.
  37. *
  38. * @author   Xavier Noguer <xnoguer@php.net>
  39. * @author   Christian Schmidt <schmidt@php.net>
  40. * @category   PHPExcel
  41. * @package    PHPExcel_Shared_OLE
  42. */
  43. {
  44.     const OLE_PPS_TYPE_ROOT   =      5;
  45.     const OLE_PPS_TYPE_DIR    =      1;
  46.     const OLE_PPS_TYPE_FILE   =      2;
  47.     const OLE_DATA_SIZE_SMALL = 0x1000;
  48.     const OLE_LONG_INT_SIZE   =      4;
  49.     const OLE_PPS_SIZE        =   0x80;
  50.  
  51.     /**
  52.      * The file handle for reading an OLE container
  53.      * @var resource 
  54.     */
  55.     public $_file_handle;
  56.  
  57.     /**
  58.     * Array of PPS's found on the OLE container
  59.     * @var array 
  60.     */
  61.     public $_list = array();
  62.  
  63.     /**
  64.      * Root directory of OLE container
  65.      * @var OLE_PPS_Root 
  66.     */
  67.     public $root;
  68.  
  69.     /**
  70.      * Big Block Allocation Table
  71.      * @var array  (blockId => nextBlockId)
  72.     */
  73.     public $bbat;
  74.  
  75.     /**
  76.      * Short Block Allocation Table
  77.      * @var array  (blockId => nextBlockId)
  78.     */
  79.     public $sbat;
  80.  
  81.     /**
  82.      * Size of big blocks. This is usually 512.
  83.      * @var  int  number of octets per block.
  84.     */
  85.     public $bigBlockSize;
  86.  
  87.     /**
  88.      * Size of small blocks. This is usually 64.
  89.      * @var  int  number of octets per block
  90.     */
  91.     public $smallBlockSize;
  92.  
  93.     /**
  94.      * Reads an OLE container from the contents of the file given.
  95.      *
  96.      * @acces public
  97.      * @param string $file 
  98.      * @return mixed true on success, PEAR_Error on failure
  99.     */
  100.     public function read($file)
  101.     {
  102.         $fh = fopen($file, "r");
  103.         if (!$fh) {
  104.             throw new Exception("Can't open file $file");
  105.         }
  106.         $this->_file_handle = $fh;
  107.  
  108.         $signature = fread($fh, 8);
  109.         if ("\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1" != $signature) {
  110.             throw new Exception("File doesn't seem to be an OLE container.");
  111.         }
  112.         fseek($fh, 28);
  113.         if (fread($fh, 2) != "\xFE\xFF") {
  114.             // This shouldn't be a problem in practice
  115.             throw new Exception("Only Little-Endian encoding is supported.");
  116.         }
  117.         // Size of blocks and short blocks in bytes
  118.         $this->bigBlockSize = pow(2, $this->_readInt2($fh));
  119.         $this->smallBlockSize  = pow(2, $this->_readInt2($fh));
  120.  
  121.         // Skip UID, revision number and version number
  122.         fseek($fh, 44);
  123.         // Number of blocks in Big Block Allocation Table
  124.         $bbatBlockCount = $this->_readInt4($fh);
  125.  
  126.         // Root chain 1st block
  127.         $directoryFirstBlockId = $this->_readInt4($fh);
  128.  
  129.         // Skip unused bytes
  130.         fseek($fh, 56);
  131.         // Streams shorter than this are stored using small blocks
  132.         $this->bigBlockThreshold = $this->_readInt4($fh);
  133.         // Block id of first sector in Short Block Allocation Table
  134.         $sbatFirstBlockId = $this->_readInt4($fh);
  135.         // Number of blocks in Short Block Allocation Table
  136.         $sbbatBlockCount = $this->_readInt4($fh);
  137.         // Block id of first sector in Master Block Allocation Table
  138.         $mbatFirstBlockId = $this->_readInt4($fh);
  139.         // Number of blocks in Master Block Allocation Table
  140.         $mbbatBlockCount = $this->_readInt4($fh);
  141.         $this->bbat = array();
  142.  
  143.         // Remaining 4 * 109 bytes of current block is beginning of Master
  144.         // Block Allocation Table
  145.         $mbatBlocks = array();
  146.         for ($i = 0; $i < 109; ++$i) {
  147.             $mbatBlocks[] = $this->_readInt4($fh);
  148.         }
  149.  
  150.         // Read rest of Master Block Allocation Table (if any is left)
  151.         $pos = $this->_getBlockOffset($mbatFirstBlockId);
  152.         for ($i = 0; $i < $mbbatBlockCount; ++$i) {
  153.             fseek($fh, $pos);
  154.             for ($j = 0; $j < $this->bigBlockSize / 4 - 1; ++$j) {
  155.                 $mbatBlocks[] = $this->_readInt4($fh);
  156.             }
  157.             // Last block id in each block points to next block
  158.             $pos = $this->_getBlockOffset($this->_readInt4($fh));
  159.         }
  160.  
  161.         // Read Big Block Allocation Table according to chain specified by
  162.         // $mbatBlocks
  163.         for ($i = 0; $i < $bbatBlockCount; ++$i) {
  164.             $pos = $this->_getBlockOffset($mbatBlocks[$i]);
  165.             fseek($fh, $pos);
  166.             for ($j = 0 ; $j < $this->bigBlockSize / 4; ++$j) {
  167.                 $this->bbat[] = $this->_readInt4($fh);
  168.             }
  169.         }
  170.  
  171.         // Read short block allocation table (SBAT)
  172.         $this->sbat = array();
  173.         $shortBlockCount = $sbbatBlockCount * $this->bigBlockSize / 4;
  174.         $sbatFh = $this->getStream($sbatFirstBlockId);
  175.         for ($blockId = 0; $blockId < $shortBlockCount; ++$blockId) {
  176.             $this->sbat[$blockId] = $this->_readInt4($sbatFh);
  177.         }
  178.         fclose($sbatFh);
  179.  
  180.         $this->_readPpsWks($directoryFirstBlockId);
  181.  
  182.         return true;
  183.     }
  184.  
  185.     /**
  186.      * @param  int  block id
  187.      * @param  int  byte offset from beginning of file
  188.      * @access public
  189.      */
  190.     public function _getBlockOffset($blockId)
  191.     {
  192.         return 512 + $blockId * $this->bigBlockSize;
  193.     }
  194.  
  195.     /**
  196.     * Returns a stream for use with fread() etc. External callers should
  197.     * use PHPExcel_Shared_OLE_PPS_File::getStream().
  198.     * @param   int|PPS  block id or PPS
  199.     * @return  resource  read-only stream
  200.     */
  201.     public function getStream($blockIdOrPps)
  202.     {
  203.         static $isRegistered = false;
  204.         if (!$isRegistered) {
  205.             stream_wrapper_register('ole-chainedblockstream',
  206.                 'PHPExcel_Shared_OLE_ChainedBlockStream');
  207.             $isRegistered = true;
  208.         }
  209.  
  210.         // Store current instance in global array, so that it can be accessed
  211.         // in OLE_ChainedBlockStream::stream_open().
  212.         // Object is removed from self::$instances in OLE_Stream::close().
  213.         $GLOBALS['_OLE_INSTANCES'][] = $this;
  214.         $instanceId = end(array_keys($GLOBALS['_OLE_INSTANCES']));
  215.  
  216.         $path = 'ole-chainedblockstream://oleInstanceId=' . $instanceId;
  217.         if ($blockIdOrPps instanceof PHPExcel_Shared_OLE_PPS) {
  218.             $path .= '&blockId=' . $blockIdOrPps->_StartBlock;
  219.             $path .= '&size=' . $blockIdOrPps->Size;
  220.         } else {
  221.             $path .= '&blockId=' . $blockIdOrPps;
  222.         }
  223.         return fopen($path, 'r');
  224.     }
  225.  
  226.     /**
  227.      * Reads a signed char.
  228.      * @param   resource  file handle
  229.      * @return  int 
  230.      * @access public
  231.      */
  232.     public function _readInt1($fh)
  233.     {
  234.         list(, $tmp) = unpack("c", fread($fh, 1));
  235.         return $tmp;
  236.     }
  237.  
  238.     /**
  239.      * Reads an unsigned short (2 octets).
  240.      * @param   resource  file handle
  241.      * @return  int 
  242.      * @access public
  243.      */
  244.     public function _readInt2($fh)
  245.     {
  246.         list(, $tmp) = unpack("v", fread($fh, 2));
  247.         return $tmp;
  248.     }
  249.  
  250.     /**
  251.      * Reads an unsigned long (4 octets).
  252.      * @param   resource  file handle
  253.      * @return  int 
  254.      * @access public
  255.      */
  256.     public function _readInt4($fh)
  257.     {
  258.         list(, $tmp) = unpack("V", fread($fh, 4));
  259.         return $tmp;
  260.     }
  261.  
  262.     /**
  263.     * Gets information about all PPS's on the OLE container from the PPS WK's
  264.     * creates an OLE_PPS object for each one.
  265.     *
  266.     * @access public
  267.     * @param  integer  the block id of the first block
  268.     * @return mixed true on success, PEAR_Error on failure
  269.     */
  270.     public function _readPpsWks($blockId)
  271.     {
  272.         $fh = $this->getStream($blockId);
  273.         for ($pos = 0; ; $pos += 128) {
  274.             fseek($fh, $pos, SEEK_SET);
  275.             $nameUtf16 = fread($fh, 64);
  276.             $nameLength = $this->_readInt2($fh);
  277.             $nameUtf16 = substr($nameUtf16, 0, $nameLength - 2);
  278.             // Simple conversion from UTF-16LE to ISO-8859-1
  279.             $name = str_replace("\x00", "", $nameUtf16);
  280.             $type = $this->_readInt1($fh);
  281.             switch ($type) {
  282.             case self::OLE_PPS_TYPE_ROOT:
  283.                 $pps = new PHPExcel_Shared_OLE_PPS_Root(null, null, array());
  284.                 $this->root = $pps;
  285.                 break;
  286.             case self::OLE_PPS_TYPE_DIR:
  287.                 $pps = new PHPExcel_Shared_OLE_PPS(null, null, null, null, null,
  288.                                    null, null, null, null, array());
  289.                 break;
  290.             case self::OLE_PPS_TYPE_FILE:
  291.                 $pps = new PHPExcel_Shared_OLE_PPS_File($name);
  292.                 break;
  293.             default:
  294.                 continue;
  295.             }
  296.             fseek($fh, 1, SEEK_CUR);
  297.             $pps->Type    = $type;
  298.             $pps->Name    = $name;
  299.             $pps->PrevPps = $this->_readInt4($fh);
  300.             $pps->NextPps = $this->_readInt4($fh);
  301.             $pps->DirPps  = $this->_readInt4($fh);
  302.             fseek($fh, 20, SEEK_CUR);
  303.             $pps->Time1st = self::OLE2LocalDate(fread($fh, 8));
  304.             $pps->Time2nd = self::OLE2LocalDate(fread($fh, 8));
  305.             $pps->_StartBlock = $this->_readInt4($fh);
  306.             $pps->Size = $this->_readInt4($fh);
  307.             $pps->No = count($this->_list);
  308.             $this->_list[] = $pps;
  309.  
  310.             // check if the PPS tree (starting from root) is complete
  311.             if (isset($this->root) &&
  312.                 $this->_ppsTreeComplete($this->root->No)) {
  313.  
  314.                 break;
  315.             }
  316.         }
  317.         fclose($fh);
  318.  
  319.         // Initialize $pps->children on directories
  320.         foreach ($this->_list as $pps) {
  321.             if ($pps->Type == self::OLE_PPS_TYPE_DIR || $pps->Type == self::OLE_PPS_TYPE_ROOT) {
  322.                 $nos = array($pps->DirPps);
  323.                 $pps->children = array();
  324.                 while ($nos) {
  325.                     $no = array_pop($nos);
  326.                     if ($no != -1) {
  327.                         $childPps = $this->_list[$no];
  328.                         $nos[] = $childPps->PrevPps;
  329.                         $nos[] = $childPps->NextPps;
  330.                         $pps->children[] = $childPps;
  331.                     }
  332.                 }
  333.             }
  334.         }
  335.  
  336.         return true;
  337.     }
  338.  
  339.     /**
  340.     * It checks whether the PPS tree is complete (all PPS's read)
  341.     * starting with the given PPS (not necessarily root)
  342.     *
  343.     * @access public
  344.     * @param integer $index The index of the PPS from which we are checking
  345.     * @return boolean Whether the PPS tree for the given PPS is complete
  346.     */
  347.     public function _ppsTreeComplete($index)
  348.     {
  349.         return isset($this->_list[$index]) &&
  350.                ($pps = $this->_list[$index]) &&
  351.                ($pps->PrevPps == -1 ||
  352.                 $this->_ppsTreeComplete($pps->PrevPps)) &&
  353.                ($pps->NextPps == -1 ||
  354.                 $this->_ppsTreeComplete($pps->NextPps)) &&
  355.                ($pps->DirPps == -1 ||
  356.                 $this->_ppsTreeComplete($pps->DirPps));
  357.     }
  358.  
  359.     /**
  360.     * Checks whether a PPS is a File PPS or not.
  361.     * If there is no PPS for the index given, it will return false.
  362.     *
  363.     * @access public
  364.     * @param integer $index The index for the PPS
  365.     * @return bool true if it's a File PPS, false otherwise
  366.     */
  367.     public function isFile($index)
  368.     {
  369.         if (isset($this->_list[$index])) {
  370.             return ($this->_list[$index]->Type == self::OLE_PPS_TYPE_FILE);
  371.         }
  372.         return false;
  373.     }
  374.  
  375.     /**
  376.     * Checks whether a PPS is a Root PPS or not.
  377.     * If there is no PPS for the index given, it will return false.
  378.     *
  379.     * @access public
  380.     * @param integer $index The index for the PPS.
  381.     * @return bool true if it's a Root PPS, false otherwise
  382.     */
  383.     public function isRoot($index)
  384.     {
  385.         if (isset($this->_list[$index])) {
  386.             return ($this->_list[$index]->Type == self::OLE_PPS_TYPE_ROOT);
  387.         }
  388.         return false;
  389.     }
  390.  
  391.     /**
  392.     * Gives the total number of PPS's found in the OLE container.
  393.     *
  394.     * @access public
  395.     * @return integer The total number of PPS's found in the OLE container
  396.     */
  397.     public function ppsTotal()
  398.     {
  399.         return count($this->_list);
  400.     }
  401.  
  402.     /**
  403.     * Gets data from a PPS
  404.     * If there is no PPS for the index given, it will return an empty string.
  405.     *
  406.     * @access public
  407.     * @param integer $index    The index for the PPS
  408.     * @param integer $position The position from which to start reading
  409.     *                           (relative to the PPS)
  410.     * @param integer $length   The amount of bytes to read (at most)
  411.     * @return string The binary string containing the data requested
  412.     * @see OLE_PPS_File::getStream()
  413.     */
  414.     public function getData($index, $position, $length)
  415.     {
  416.         // if position is not valid return empty string
  417.         if (!isset($this->_list[$index]) || ($position >= $this->_list[$index]->Size) || ($position < 0)) {
  418.             return '';
  419.         }
  420.         $fh = $this->getStream($this->_list[$index]);
  421.         $data = stream_get_contents($fh, $length, $position);
  422.         fclose($fh);
  423.         return $data;
  424.     }
  425.  
  426.     /**
  427.     * Gets the data length from a PPS
  428.     * If there is no PPS for the index given, it will return 0.
  429.     *
  430.     * @access public
  431.     * @param integer $index    The index for the PPS
  432.     * @return integer The amount of bytes in data the PPS has
  433.     */
  434.     public function getDataLength($index)
  435.     {
  436.         if (isset($this->_list[$index])) {
  437.             return $this->_list[$index]->Size;
  438.         }
  439.         return 0;
  440.     }
  441.  
  442.     /**
  443.     * Utility function to transform ASCII text to Unicode
  444.     *
  445.     * @access public
  446.     * @static
  447.     * @param string $ascii The ASCII string to transform
  448.     * @return string The string in Unicode
  449.     */
  450.     public static function Asc2Ucs($ascii)
  451.     {
  452.         $rawname = '';
  453.         for ($i = 0; $i < strlen($ascii); ++$i) {
  454.             $rawname .= $ascii{$i} . "\x00";
  455.         }
  456.         return $rawname;
  457.     }
  458.  
  459.     /**
  460.     * Utility function
  461.     * Returns a string for the OLE container with the date given
  462.     *
  463.     * @access public
  464.     * @static
  465.     * @param integer $date A timestamp
  466.     * @return string The string for the OLE container
  467.     */
  468.     public static function LocalDate2OLE($date = null)
  469.     {
  470.         if (!isset($date)) {
  471.             return "\x00\x00\x00\x00\x00\x00\x00\x00";
  472.         }
  473.  
  474.         // factor used for separating numbers into 4 bytes parts
  475.         $factor = pow(2, 32);
  476.  
  477.         // days from 1-1-1601 until the beggining of UNIX era
  478.         $days = 134774;
  479.         // calculate seconds
  480.         $big_date = $days*24*3600 + gmmktime(date("H",$date),date("i",$date),date("s",$date),
  481.                                              date("m",$date),date("d",$date),date("Y",$date));
  482.         // multiply just to make MS happy
  483.         $big_date *= 10000000;
  484.  
  485.         $high_part = floor($big_date / $factor);
  486.         // lower 4 bytes
  487.         $low_part = floor((($big_date / $factor) - $high_part) * $factor);
  488.  
  489.         // Make HEX string
  490.         $res = '';
  491.  
  492.         for ($i = 0; $i < 4; ++$i) {
  493.             $hex = $low_part % 0x100;
  494.             $res .= pack('c', $hex);
  495.             $low_part /= 0x100;
  496.         }
  497.         for ($i = 0; $i < 4; ++$i) {
  498.             $hex = $high_part % 0x100;
  499.             $res .= pack('c', $hex);
  500.             $high_part /= 0x100;
  501.         }
  502.         return $res;
  503.     }
  504.  
  505.     /**
  506.     * Returns a timestamp from an OLE container's date
  507.     *
  508.     * @access public
  509.     * @static
  510.     * @param integer $string A binary string with the encoded date
  511.     * @return string The timestamp corresponding to the string
  512.     */
  513.     public static function OLE2LocalDate($string)
  514.     {
  515.         if (strlen($string) != 8) {
  516.             return new PEAR_Error("Expecting 8 byte string");
  517.         }
  518.  
  519.         // factor used for separating numbers into 4 bytes parts
  520.         $factor = pow(2,32);
  521.         $high_part = 0;
  522.         for ($i = 0; $i < 4; ++$i) {
  523.             list(, $high_part) = unpack('C', $string{(7 - $i)});
  524.             if ($i < 3) {
  525.                 $high_part *= 0x100;
  526.             }
  527.         }
  528.         $low_part = 0;
  529.         for ($i = 4; $i < 8; ++$i) {
  530.             list(, $low_part) = unpack('C', $string{(7 - $i)});
  531.             if ($i < 7) {
  532.                 $low_part *= 0x100;
  533.             }
  534.         }
  535.         $big_date = ($high_part * $factor) + $low_part;
  536.         // translate to seconds
  537.         $big_date /= 10000000;
  538.  
  539.         // days from 1-1-1601 until the beggining of UNIX era
  540.         $days = 134774;
  541.  
  542.         // translate to seconds from beggining of UNIX era
  543.         $big_date -= $days * 24 * 3600;
  544.         return floor($big_date);
  545.     }
  546. }

Documentation generated on Mon, 05 Jan 2009 20:38:09 +0100 by phpDocumentor 1.4.1