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

Documentation generated on Mon, 10 Aug 2009 08:06:59 +0200 by phpDocumentor 1.4.1