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

Documentation generated on Thu, 12 Jun 2008 14:08:04 -0500 by phpDocumentor 1.4.1