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

Source for file OLE_Root.php

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

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