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

Source for file Root.php

Documentation is available at Root.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: Root.php,v 1.10 2008/02/02 21:00:37 schmidt Exp $
  21.  
  22.  
  23. require_once 'OLE/PPS.php';
  24. require_once 'System.php';
  25.  
  26. /**
  27. * Class for creating Root PPS's for OLE containers
  28. *
  29. @author   Xavier Noguer <xnoguer@php.net>
  30. @category Structures
  31. @package  OLE
  32. */
  33. class OLE_PPS_Root extends OLE_PPS
  34. {
  35.     /**
  36.     * The temporary dir for storing the OLE file
  37.     * @var string 
  38.     */
  39.     var $_tmp_dir;
  40.     
  41.     /**
  42.     * Constructor
  43.     *
  44.     * @access public
  45.     * @param integer $time_1st A timestamp
  46.     * @param integer $time_2nd A timestamp
  47.     */
  48.     function OLE_PPS_Root($time_1st$time_2nd$raChild)
  49.     {
  50.         $this->_tmp_dir = System::tmpdir();
  51.         $this->OLE_PPS(
  52.            null
  53.            OLE::Asc2Ucs('Root Entry'),
  54.            OLE_PPS_TYPE_ROOT,
  55.            null,
  56.            null,
  57.            null,
  58.            $time_1st,
  59.            $time_2nd,
  60.            null,
  61.            $raChild);
  62.     }
  63.  
  64.     /**
  65.     * Sets the temp dir used for storing the OLE file
  66.     *
  67.     * @access public
  68.     * @param string $dir The dir to be used as temp dir
  69.     * @return true if given dir is valid, false otherwise
  70.     */
  71.     function setTempDir($dir)
  72.     {
  73.         if (is_dir($dir)) {
  74.             $this->_tmp_dir $dir;
  75.             return true;
  76.         }
  77.         return false;
  78.     }
  79.  
  80.     /**
  81.     * Method for saving the whole OLE container (including files).
  82.     * In fact, if called with an empty argument (or '-'), it saves to a
  83.     * temporary file and then outputs it's contents to stdout.
  84.     *
  85.     * @param string $filename The name of the file where to save the OLE container
  86.     * @access public
  87.     * @return mixed true on success, PEAR_Error on failure
  88.     */
  89.     function save($filename)
  90.     {
  91.         // Initial Setting for saving
  92.         $this->_BIG_BLOCK_SIZE  pow(2,
  93.                       ((isset($this->_BIG_BLOCK_SIZE))$this->_adjust2($this->_BIG_BLOCK_SIZE)  : 9));
  94.         $this->_SMALL_BLOCK_SIZEpow(2
  95.                       ((isset($this->_SMALL_BLOCK_SIZE))?  $this->_adjust2($this->_SMALL_BLOCK_SIZE): 6));
  96.  
  97.         // Open temp file if we are sending output to stdout
  98.         if (($filename == '-'|| ($filename == '')) {
  99.             $this->_tmp_filename tempnam($this->_tmp_dir"OLE_PPS_Root");
  100.             $this->_FILEH_ @fopen($this->_tmp_filename,"w+b");
  101.             if ($this->_FILEH_ == false{
  102.                 return $this->raiseError("Can't create temporary file.");
  103.             }
  104.         else {
  105.             $this->_FILEH_ @fopen($filename"wb");
  106.             if ($this->_FILEH_ == false{
  107.                 return $this->raiseError("Can't open $filename. It may be in use or protected.");
  108.             }
  109.         }
  110.         // Make an array of PPS's (for Save)
  111.         $aList = array();
  112.         $this->_savePpsSetPnt($aList);
  113.         // calculate values for header
  114.         list($iSBDcnt$iBBcnt$iPPScnt$this->_calcSize($aList)//, $rhInfo);
  115.         // Save Header
  116.         $this->_saveHeader($iSBDcnt$iBBcnt$iPPScnt);
  117.   
  118.         // Make Small Data string (write SBD)
  119.         $this->_data $this->_makeSmallData($aList);
  120.   
  121.         // Write BB
  122.         $this->_saveBigData($iSBDcnt$aList);
  123.         // Write PPS
  124.         $this->_savePps($aList);
  125.         // Write Big Block Depot and BDList and Adding Header informations
  126.         $this->_saveBbd($iSBDcnt$iBBcnt$iPPScnt);
  127.         // Close File, send it to stdout if necessary
  128.         if (($filename == '-'|| ($filename == '')) {
  129.             fseek($this->_FILEH_0);
  130.             fpassthru($this->_FILEH_);
  131.             @fclose($this->_FILEH_);
  132.             // Delete the temporary file.
  133.             @unlink($this->_tmp_filename);
  134.         else {
  135.             @fclose($this->_FILEH_);
  136.         }
  137.  
  138.         return true;
  139.     }
  140.  
  141.     /**
  142.     * Calculate some numbers
  143.     *
  144.     * @access private
  145.     * @param array $raList Reference to an array of PPS's
  146.     * @return array The array of numbers
  147.     */
  148.     function _calcSize(&$raList
  149.     {
  150.         // Calculate Basic Setting
  151.         list($iSBDcnt$iBBcnt$iPPScnt= array(0,0,0);
  152.         $iSmallLen = 0;
  153.         $iSBcnt = 0;
  154.         for ($i = 0; $i count($raList)$i++{
  155.             if ($raList[$i]->Type == OLE_PPS_TYPE_FILE{
  156.                 $raList[$i]->Size = $raList[$i]->_DataLen();
  157.                 if ($raList[$i]->Size < OLE_DATA_SIZE_SMALL{
  158.                     $iSBcnt += floor($raList[$i]->Size / $this->_SMALL_BLOCK_SIZE)
  159.                                   + (($raList[$i]->Size % $this->_SMALL_BLOCK_SIZE)? 1: 0);
  160.                 else {
  161.                     $iBBcnt += (floor($raList[$i]->Size / $this->_BIG_BLOCK_SIZE+
  162.                         (($raList[$i]->Size % $this->_BIG_BLOCK_SIZE)? 1: 0));
  163.                 }
  164.             }
  165.         }
  166.         $iSmallLen $iSBcnt $this->_SMALL_BLOCK_SIZE;
  167.         $iSlCnt floor($this->_BIG_BLOCK_SIZE OLE_LONG_INT_SIZE);
  168.         $iSBDcnt floor($iSBcnt $iSlCnt(($iSBcnt $iSlCnt)? 1:0);
  169.         $iBBcnt +=  (floor($iSmallLen $this->_BIG_BLOCK_SIZE+
  170.                       (( $iSmallLen $this->_BIG_BLOCK_SIZE)? 1: 0));
  171.         $iCnt count($raList);
  172.         $iBdCnt $this->_BIG_BLOCK_SIZE OLE_PPS_SIZE;
  173.         $iPPScnt (floor($iCnt/$iBdCnt(($iCnt $iBdCnt)? 1: 0));
  174.    
  175.         return array($iSBDcnt$iBBcnt$iPPScnt);
  176.     }
  177.  
  178.     /**
  179.     * Helper function for caculating a magic value for block sizes
  180.     *
  181.     * @access private
  182.     * @param integer $i2 The argument
  183.     * @see save()
  184.     * @return integer 
  185.     */
  186.     function _adjust2($i2)
  187.     {
  188.         $iWk log($i2)/log(2);
  189.         return ($iWk floor($iWk))floor($iWk)+1:$iWk;
  190.     }
  191.  
  192.     /**
  193.     * Save OLE header
  194.     *
  195.     * @access private
  196.     * @param integer $iSBDcnt 
  197.     * @param integer $iBBcnt 
  198.     * @param integer $iPPScnt 
  199.     */
  200.     function _saveHeader($iSBDcnt$iBBcnt$iPPScnt)
  201.     {
  202.         $FILE $this->_FILEH_;
  203.   
  204.         // Calculate Basic Setting
  205.         $iBlCnt $this->_BIG_BLOCK_SIZE OLE_LONG_INT_SIZE;
  206.         $i1stBdL ($this->_BIG_BLOCK_SIZE - 0x4COLE_LONG_INT_SIZE;
  207.   
  208.         $iBdExL = 0;
  209.         $iAll $iBBcnt $iPPScnt $iSBDcnt;
  210.         $iAllW $iAll;
  211.         $iBdCntW floor($iAllW $iBlCnt(($iAllW $iBlCnt)? 1: 0);
  212.         $iBdCnt floor(($iAll $iBdCntW$iBlCnt((($iAllW+$iBdCntW$iBlCnt)? 1: 0);
  213.   
  214.         // Calculate BD count
  215.         if ($iBdCnt $i1stBdL{
  216.             while (1{
  217.                 $iBdExL++;
  218.                 $iAllW++;
  219.                 $iBdCntW floor($iAllW $iBlCnt(($iAllW $iBlCnt)? 1: 0);
  220.                 $iBdCnt floor(($iAllW $iBdCntW$iBlCnt((($iAllW+$iBdCntW$iBlCnt)? 1: 0);
  221.                 if ($iBdCnt <= ($iBdExL*$iBlCnt$i1stBdL)) {
  222.                     break;
  223.                 }
  224.             }
  225.         }
  226.   
  227.         // Save Header
  228.         fwrite($FILE,
  229.                   "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1"
  230.                   . "\x00\x00\x00\x00"
  231.                   . "\x00\x00\x00\x00"
  232.                   . "\x00\x00\x00\x00"
  233.                   . "\x00\x00\x00\x00"
  234.                   . pack("v"0x3b)
  235.                   . pack("v"0x03)
  236.                   . pack("v"-2)
  237.                   . pack("v"9)
  238.                   . pack("v"6)
  239.                   . pack("v"0)
  240.                   . "\x00\x00\x00\x00"
  241.                   . "\x00\x00\x00\x00"
  242.                   . pack("V"$iBdCnt
  243.                   . pack("V"$iBBcnt+$iSBDcnt//ROOT START
  244.                   . pack("V"0)
  245.                   . pack("V"0x1000)
  246.                   . pack("V"0)                  //Small Block Depot
  247.                   . pack("V"1)
  248.           );
  249.         // Extra BDList Start, Count
  250.         if ($iBdCnt $i1stBdL{
  251.             fwrite($FILE,
  252.                       pack("V"-2).      // Extra BDList Start
  253.                       pack("V"0)        // Extra BDList Count
  254.                   );
  255.         else {
  256.             fwrite($FILEpack("V"$iAll+$iBdCntpack("V"$iBdExL));
  257.         }
  258.  
  259.         // BDList
  260.         for ($i = 0; $i $i1stBdL && $i $iBdCnt$i++{
  261.             fwrite($FILEpack("V"$iAll+$i));
  262.         }
  263.         if ($i $i1stBdL{
  264.             for ($j = 0; $j ($i1stBdL-$i)$j++{
  265.                 fwrite($FILE(pack("V"-1)));
  266.             }
  267.         }
  268.     }
  269.  
  270.     /**
  271.     * Saving big data (PPS's with data bigger than OLE_DATA_SIZE_SMALL)
  272.     *
  273.     * @access private
  274.     * @param integer $iStBlk 
  275.     * @param array &$raList Reference to array of PPS's
  276.     */
  277.     function _saveBigData($iStBlk&$raList)
  278.     {
  279.         $FILE $this->_FILEH_;
  280.    
  281.         // cycle through PPS's
  282.         for ($i = 0; $i count($raList)$i++{
  283.             if ($raList[$i]->Type != OLE_PPS_TYPE_DIR{
  284.                 $raList[$i]->Size = $raList[$i]->_DataLen();
  285.                 if (($raList[$i]->Size >= OLE_DATA_SIZE_SMALL||
  286.                     (($raList[$i]->Type == OLE_PPS_TYPE_ROOT&& isset($raList[$i]->_data)))
  287.                 {
  288.                     // Write Data
  289.                     if (isset($raList[$i]->_PPS_FILE)) {
  290.                         $iLen = 0;
  291.                         fseek($raList[$i]->_PPS_FILE0)// To The Top
  292.                         while($sBuff fread($raList[$i]->_PPS_FILE4096)) {
  293.                             $iLen += strlen($sBuff);
  294.                             fwrite($FILE$sBuff);
  295.                         }
  296.                     else {
  297.                         fwrite($FILE$raList[$i]->_data);
  298.                     }
  299.            
  300.                     if ($raList[$i]->Size % $this->_BIG_BLOCK_SIZE{
  301.                         for ($j = 0; $j ($this->_BIG_BLOCK_SIZE ($raList[$i]->Size % $this->_BIG_BLOCK_SIZE))$j++{
  302.                             fwrite($FILE"\x00");
  303.                         }
  304.                     }
  305.                     // Set For PPS
  306.                     $raList[$i]->_StartBlock = $iStBlk;
  307.                     $iStBlk += 
  308.                             (floor($raList[$i]->Size / $this->_BIG_BLOCK_SIZE+
  309.                                 (($raList[$i]->Size % $this->_BIG_BLOCK_SIZE)? 1: 0));
  310.                 }
  311.                 // Close file for each PPS, and unlink it
  312.                 if (isset($raList[$i]->_PPS_FILE)) {
  313.                     @fclose($raList[$i]->_PPS_FILE);
  314.                     $raList[$i]->_PPS_FILE = null;
  315.                     @unlink($raList[$i]->_tmp_filename);
  316.                 }
  317.             }
  318.         }
  319.     }
  320.  
  321.     /**
  322.     * get small data (PPS's with data smaller than OLE_DATA_SIZE_SMALL)
  323.     *
  324.     * @access private
  325.     * @param array &$raList Reference to array of PPS's
  326.     */
  327.     function _makeSmallData(&$raList)
  328.     {
  329.         $sRes '';
  330.         $FILE $this->_FILEH_;
  331.         $iSmBlk = 0;
  332.    
  333.         for ($i = 0; $i count($raList)$i++{
  334.             // Make SBD, small data string
  335.             if ($raList[$i]->Type == OLE_PPS_TYPE_FILE{
  336.                 if ($raList[$i]->Size <= 0{
  337.                     continue;
  338.                 }
  339.                 if ($raList[$i]->Size < OLE_DATA_SIZE_SMALL{
  340.                     $iSmbCnt floor($raList[$i]->Size / $this->_SMALL_BLOCK_SIZE)
  341.                                   + (($raList[$i]->Size % $this->_SMALL_BLOCK_SIZE)? 1: 0);
  342.                     // Add to SBD
  343.                     for ($j = 0; $j ($iSmbCnt-1)$j++{
  344.                         fwrite($FILEpack("V"$j+$iSmBlk+1));
  345.                     }
  346.                     fwrite($FILEpack("V"-2));
  347.                    
  348.                     // Add to Data String(this will be written for RootEntry)
  349.                     if ($raList[$i]->_PPS_FILE{
  350.                         fseek($raList[$i]->_PPS_FILE0)// To The Top
  351.                         while ($sBuff fread($raList[$i]->_PPS_FILE4096)) {
  352.                             $sRes .= $sBuff;
  353.                         }
  354.                     else {
  355.                         $sRes .= $raList[$i]->_data;
  356.                     }
  357.                     if ($raList[$i]->Size % $this->_SMALL_BLOCK_SIZE{
  358.                         for ($j = 0; $j ($this->_SMALL_BLOCK_SIZE ($raList[$i]->Size % $this->_SMALL_BLOCK_SIZE))$j++{
  359.                             $sRes .= "\x00";
  360.                         }
  361.                     }
  362.                     // Set for PPS
  363.                     $raList[$i]->_StartBlock = $iSmBlk;
  364.                     $iSmBlk += $iSmbCnt;
  365.                 }
  366.             }
  367.         }
  368.         $iSbCnt floor($this->_BIG_BLOCK_SIZE OLE_LONG_INT_SIZE);
  369.         if ($iSmBlk $iSbCnt{
  370.             for ($i = 0; $i ($iSbCnt ($iSmBlk $iSbCnt))$i++{
  371.                 fwrite($FILEpack("V"-1));
  372.             }
  373.         }
  374.         return $sRes;
  375.     }
  376.  
  377.     /**
  378.     * Saves all the PPS's WKs
  379.     *
  380.     * @access private
  381.     * @param array $raList Reference to an array with all PPS's
  382.     */
  383.     function _savePps(&$raList
  384.     {
  385.         // Save each PPS WK
  386.         for ($i = 0; $i count($raList)$i++{
  387.             fwrite($this->_FILEH_$raList[$i]->_getPpsWk());
  388.         }
  389.         // Adjust for Block
  390.         $iCnt count($raList);
  391.         $iBCnt $this->_BIG_BLOCK_SIZE OLE_PPS_SIZE;
  392.         if ($iCnt $iBCnt{
  393.             for ($i = 0; $i (($iBCnt ($iCnt $iBCnt)) OLE_PPS_SIZE)$i++{
  394.                 fwrite($this->_FILEH_"\x00");
  395.             }
  396.         }
  397.     }
  398.  
  399.     /**
  400.     * Saving Big Block Depot
  401.     *
  402.     * @access private
  403.     * @param integer $iSbdSize 
  404.     * @param integer $iBsize 
  405.     * @param integer $iPpsCnt 
  406.     */
  407.     function _saveBbd($iSbdSize$iBsize$iPpsCnt
  408.     {
  409.         $FILE $this->_FILEH_;
  410.         // Calculate Basic Setting
  411.         $iBbCnt $this->_BIG_BLOCK_SIZE OLE_LONG_INT_SIZE;
  412.         $i1stBdL ($this->_BIG_BLOCK_SIZE - 0x4COLE_LONG_INT_SIZE;
  413.       
  414.         $iBdExL = 0;
  415.         $iAll $iBsize $iPpsCnt $iSbdSize;
  416.         $iAllW $iAll;
  417.         $iBdCntW floor($iAllW $iBbCnt(($iAllW $iBbCnt)? 1: 0);
  418.         $iBdCnt floor(($iAll $iBdCntW$iBbCnt((($iAllW+$iBdCntW$iBbCnt)? 1: 0);
  419.         // Calculate BD count
  420.         if ($iBdCnt >$i1stBdL{
  421.             while (1{
  422.                 $iBdExL++;
  423.                 $iAllW++;
  424.                 $iBdCntW floor($iAllW $iBbCnt(($iAllW $iBbCnt)? 1: 0);
  425.                 $iBdCnt floor(($iAllW $iBdCntW$iBbCnt((($iAllW+$iBdCntW$iBbCnt)? 1: 0);
  426.                 if ($iBdCnt <= ($iBdExL*$iBbCnt$i1stBdL)) {
  427.                     break;
  428.                 }
  429.             }
  430.         }
  431.       
  432.         // Making BD
  433.         // Set for SBD
  434.         if ($iSbdSize > 0{
  435.             for ($i = 0; $i ($iSbdSize - 1)$i++{
  436.                 fwrite($FILEpack("V"$i+1));
  437.             }
  438.             fwrite($FILEpack("V"-2));
  439.         }
  440.         // Set for B
  441.         for ($i = 0; $i ($iBsize - 1)$i++{
  442.             fwrite($FILEpack("V"$i+$iSbdSize+1));
  443.         }
  444.         fwrite($FILEpack("V"-2));
  445.       
  446.         // Set for PPS
  447.         for ($i = 0; $i ($iPpsCnt - 1)$i++{
  448.             fwrite($FILEpack("V"$i+$iSbdSize+$iBsize+1));
  449.         }
  450.         fwrite($FILEpack("V"-2));
  451.         // Set for BBD itself ( 0xFFFFFFFD : BBD)
  452.         for ($i = 0; $i $iBdCnt$i++{
  453.             fwrite($FILEpack("V"0xFFFFFFFD));
  454.         }
  455.         // Set for ExtraBDList
  456.         for ($i = 0; $i $iBdExL$i++{
  457.             fwrite($FILEpack("V"0xFFFFFFFC));
  458.         }
  459.         // Adjust for Block
  460.         if (($iAllW $iBdCnt$iBbCnt{
  461.             for ($i = 0; $i ($iBbCnt (($iAllW $iBdCnt$iBbCnt))$i++{
  462.                 fwrite($FILEpack("V"-1));
  463.             }
  464.         }
  465.         // Extra BDList
  466.         if ($iBdCnt $i1stBdL{
  467.             $iN=0;
  468.             $iNb=0;
  469.             for ($i $i1stBdL;$i $iBdCnt$i++$iN++{
  470.                 if ($iN >= ($iBbCnt - 1)) {
  471.                     $iN = 0;
  472.                     $iNb++;
  473.                     fwrite($FILEpack("V"$iAll+$iBdCnt+$iNb));
  474.                 }
  475.                 fwrite($FILEpack("V"$iBsize+$iSbdSize+$iPpsCnt+$i));
  476.             }
  477.             if (($iBdCnt-$i1stBdL($iBbCnt-1)) {
  478.                 for ($i = 0; $i (($iBbCnt - 1(($iBdCnt $i1stBdL($iBbCnt - 1)))$i++{
  479.                     fwrite($FILEpack("V"-1))
  480.                 }
  481.             }
  482.             fwrite($FILEpack("V"-2));
  483.         }
  484.     }
  485. }
  486. ?>

Documentation generated on Mon, 11 Mar 2019 15:22:19 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.