Advertisement
dMedia

ExcelReader

Dec 15th, 2017
680
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 58.92 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Utility\Classes;
  4.  
  5. /**
  6.  * A class for reading Microsoft Excel (97/2003) Spreadsheets.
  7.  *
  8.  * Version 2.21
  9.  *
  10.  * Enhanced and maintained by Matt Kruse < http://mattkruse.com >
  11.  * Maintained at http://code.google.com/p/php-excel-reader/
  12.  *
  13.  * Format parsing and MUCH more contributed by:
  14.  *    Matt Roxburgh < http://www.roxburgh.me.uk >
  15.  *
  16.  * DOCUMENTATION
  17.  * =============
  18.  *   http://code.google.com/p/php-excel-reader/wiki/Documentation
  19.  *
  20.  * CHANGE LOG
  21.  * ==========
  22.  *   http://code.google.com/p/php-excel-reader/wiki/ChangeHistory
  23.  *
  24.  * DISCUSSION/SUPPORT
  25.  * ==================
  26.  *   http://groups.google.com/group/php-excel-reader-discuss/topics
  27.  *
  28.  * FIXIES
  29.  * ==================
  30.  * PHP Excel Reader PHP5 fix
  31.  * Lars Gyrup Brink Nielsen
  32.  * https://pastebin.com/YNUZANcs
  33.  *
  34.  * PHP Excel Reader PHP7 fix
  35.  * MisterX
  36.  * antradienio@gmail.com
  37.  * --------------------------------------------------------------------------
  38.  *
  39.  * Originally developed by Vadim Tkachenko under the name PHPExcelReader.
  40.  * (http://sourceforge.net/projects/phpexcelreader)
  41.  * Based on the Java version by Andy Khan (http://www.andykhan.com).  Now
  42.  * maintained by David Sanders.  Reads only Biff 7 and Biff 8 formats.
  43.  *
  44.  * PHP versions 4 and 5
  45.  *
  46.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  47.  * that is available through the world-wide-web at the following URI:
  48.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  49.  * the PHP License and are unable to obtain it through the web, please
  50.  * send a note to license@php.net so we can mail you a copy immediately.
  51.  *
  52.  * @category   Spreadsheet
  53.  * @package Spreadsheet_Excel_Reader
  54.  * @author   Vadim Tkachenko <vt@apachephp.com>
  55.  * @license http://www.php.net/license/3_0.txt  PHP License 3.0
  56.  * @version CVS: $Id: reader.php 19 2007-03-13 12:42:41Z shangxiao $
  57.  * @link       http://pear.php.net/package/Spreadsheet_Excel_Reader
  58.  * @see     OLE, Spreadsheet_Excel_Writer
  59.  * --------------------------------------------------------------------------
  60.  */
  61.  
  62. define('NUM_BIG_BLOCK_DEPOT_BLOCKS_POS', 0x2c);
  63. define('SMALL_BLOCK_DEPOT_BLOCK_POS', 0x3c);
  64. define('ROOT_START_BLOCK_POS', 0x30);
  65. define('BIG_BLOCK_SIZE', 0x200);
  66. define('SMALL_BLOCK_SIZE', 0x40);
  67. define('EXTENSION_BLOCK_POS', 0x44);
  68. define('NUM_EXTENSION_BLOCK_POS', 0x48);
  69. define('PROPERTY_STORAGE_BLOCK_SIZE', 0x80);
  70. define('BIG_BLOCK_DEPOT_BLOCKS_POS', 0x4c);
  71. define('SMALL_BLOCK_THRESHOLD', 0x1000);
  72. // property storage offsets
  73. define('SIZE_OF_NAME_POS', 0x40);
  74. define('TYPE_POS', 0x42);
  75. define('START_BLOCK_POS', 0x74);
  76. define('SIZE_POS', 0x78);
  77. define('IDENTIFIER_OLE', pack("CCCCCCCC",0xd0,0xcf,0x11,0xe0,0xa1,0xb1,0x1a,0xe1));
  78.  
  79.  
  80. function GetInt4d($data, $pos) {
  81.     $value = ord($data[$pos]) | (ord($data[$pos+1]) << 8) | (ord($data[$pos+2]) << 16) | (ord($data[$pos+3]) << 24);
  82.     if ($value>=4294967294) {
  83.         $value=-2;
  84.     }
  85.     return $value;
  86. }
  87.  
  88. // http://uk.php.net/manual/en/function.getdate.php
  89. function gmgetdate($ts = null){
  90.     $k = array('seconds','minutes','hours','mday','wday','mon','year','yday','weekday','month',0);
  91.  
  92.     //return(array_comb($k,split(":",gmdate('s:i:G:j:w:n:Y:z:l:F:U',is_null($ts)?time():$ts)))); LGBN
  93.     return array_comb($k, explode(':', gmdate('s:i:G:j:w:n:Y:z:l:F:U', is_null($ts) ? time() : $ts)));
  94.     }
  95.  
  96. // Added for PHP4 compatibility
  97. function array_comb($array1, $array2) {
  98.     /*$out = array();
  99.     foreach ($array1 as $key => $value) {
  100.         $out[$value] = $array2[$key];
  101.     }
  102.     return $out;*/ // Updated to PHP5, LGBN
  103.  
  104.     return array_combine($array2, $array1);
  105. }
  106.  
  107. function v($data,$pos) {
  108.     return ord($data[$pos]) | ord($data[$pos+1])<<8;
  109. }
  110.  
  111. class OLERead {
  112.     var $data = '';
  113.     function OLERead(){ }
  114.  
  115.     function read($sFileName){
  116.         // check if file exist and is readable (Darko Miljanovic)
  117.         if(!is_readable($sFileName)) {
  118.             $this->error = 1;
  119.             return false;
  120.         }
  121.         $this->data = @file_get_contents($sFileName);
  122.         if (!$this->data) {
  123.             $this->error = 1;
  124.             return false;
  125.         }
  126.         if (substr($this->data, 0, 8) != IDENTIFIER_OLE) {
  127.             $this->error = 1;
  128.             return false;
  129.         }
  130.         $this->numBigBlockDepotBlocks = GetInt4d($this->data, NUM_BIG_BLOCK_DEPOT_BLOCKS_POS);
  131.         $this->sbdStartBlock = GetInt4d($this->data, SMALL_BLOCK_DEPOT_BLOCK_POS);
  132.         $this->rootStartBlock = GetInt4d($this->data, ROOT_START_BLOCK_POS);
  133.         $this->extensionBlock = GetInt4d($this->data, EXTENSION_BLOCK_POS);
  134.         $this->numExtensionBlocks = GetInt4d($this->data, NUM_EXTENSION_BLOCK_POS);
  135.  
  136.         $bigBlockDepotBlocks = array();
  137.         $pos = BIG_BLOCK_DEPOT_BLOCKS_POS;
  138.         $bbdBlocks = $this->numBigBlockDepotBlocks;
  139.         if ($this->numExtensionBlocks != 0) {
  140.             $bbdBlocks = (BIG_BLOCK_SIZE - BIG_BLOCK_DEPOT_BLOCKS_POS)/4;
  141.         }
  142.  
  143.         for ($i = 0; $i < $bbdBlocks; $i++) {
  144.             $bigBlockDepotBlocks[$i] = GetInt4d($this->data, $pos);
  145.             $pos += 4;
  146.         }
  147.  
  148.  
  149.         for ($j = 0; $j < $this->numExtensionBlocks; $j++) {
  150.             $pos = ($this->extensionBlock + 1) * BIG_BLOCK_SIZE;
  151.             $blocksToRead = min($this->numBigBlockDepotBlocks - $bbdBlocks, BIG_BLOCK_SIZE / 4 - 1);
  152.  
  153.             for ($i = $bbdBlocks; $i < $bbdBlocks + $blocksToRead; $i++) {
  154.                 $bigBlockDepotBlocks[$i] = GetInt4d($this->data, $pos);
  155.                 $pos += 4;
  156.             }
  157.  
  158.             $bbdBlocks += $blocksToRead;
  159.             if ($bbdBlocks < $this->numBigBlockDepotBlocks) {
  160.                 $this->extensionBlock = GetInt4d($this->data, $pos);
  161.             }
  162.         }
  163.  
  164.         // readBigBlockDepot
  165.         $pos = 0;
  166.         $index = 0;
  167.         $this->bigBlockChain = array();
  168.  
  169.         for ($i = 0; $i < $this->numBigBlockDepotBlocks; $i++) {
  170.             $pos = ($bigBlockDepotBlocks[$i] + 1) * BIG_BLOCK_SIZE;
  171.             //echo "pos = $pos";
  172.             for ($j = 0 ; $j < BIG_BLOCK_SIZE / 4; $j++) {
  173.                 $this->bigBlockChain[$index] = GetInt4d($this->data, $pos);
  174.                 $pos += 4 ;
  175.                 $index++;
  176.             }
  177.         }
  178.  
  179.         // readSmallBlockDepot();
  180.         $pos = 0;
  181.         $index = 0;
  182.         $sbdBlock = $this->sbdStartBlock;
  183.         $this->smallBlockChain = array();
  184.  
  185.         while ($sbdBlock != -2) {
  186.           $pos = ($sbdBlock + 1) * BIG_BLOCK_SIZE;
  187.           for ($j = 0; $j < BIG_BLOCK_SIZE / 4; $j++) {
  188.             $this->smallBlockChain[$index] = GetInt4d($this->data, $pos);
  189.             $pos += 4;
  190.             $index++;
  191.           }
  192.           $sbdBlock = $this->bigBlockChain[$sbdBlock];
  193.         }
  194.  
  195.  
  196.         // readData(rootStartBlock)
  197.         $block = $this->rootStartBlock;
  198.         $pos = 0;
  199.         $this->entry = $this->__readData($block);
  200.         $this->__readPropertySets();
  201.     }
  202.  
  203.     function __readData($bl) {
  204.         $block = $bl;
  205.         $pos = 0;
  206.         $data = '';
  207.         while ($block != -2)  {
  208.             $pos = ($block + 1) * BIG_BLOCK_SIZE;
  209.             $data = $data.substr($this->data, $pos, BIG_BLOCK_SIZE);
  210.             $block = $this->bigBlockChain[$block];
  211.         }
  212.         return $data;
  213.      }
  214.  
  215.     function __readPropertySets(){
  216.         $offset = 0;
  217.         while ($offset < strlen($this->entry)) {
  218.             $d = substr($this->entry, $offset, PROPERTY_STORAGE_BLOCK_SIZE);
  219.             $nameSize = ord($d[SIZE_OF_NAME_POS]) | (ord($d[SIZE_OF_NAME_POS+1]) << 8);
  220.             $type = ord($d[TYPE_POS]);
  221.             $startBlock = GetInt4d($d, START_BLOCK_POS);
  222.             $size = GetInt4d($d, SIZE_POS);
  223.             $name = '';
  224.             for ($i = 0; $i < $nameSize ; $i++) {
  225.                 $name .= $d[$i];
  226.             }
  227.             $name = str_replace("\x00", "", $name);
  228.             $this->props[] = array (
  229.                 'name' => $name,
  230.                 'type' => $type,
  231.                 'startBlock' => $startBlock,
  232.                 'size' => $size);
  233.             if ((strtolower($name) == "workbook") || ( strtolower($name) == "book")) {
  234.                 $this->wrkbook = count($this->props) - 1;
  235.             }
  236.             if ($name == "Root Entry") {
  237.                 $this->rootentry = count($this->props) - 1;
  238.             }
  239.             $offset += PROPERTY_STORAGE_BLOCK_SIZE;
  240.         }
  241.  
  242.     }
  243.  
  244.  
  245.     function getWorkBook(){
  246.         if ($this->props[$this->wrkbook]['size'] < SMALL_BLOCK_THRESHOLD){
  247.             $rootdata = $this->__readData($this->props[$this->rootentry]['startBlock']);
  248.             $streamData = '';
  249.             $block = $this->props[$this->wrkbook]['startBlock'];
  250.             $pos = 0;
  251.             while ($block != -2) {
  252.                   $pos = $block * SMALL_BLOCK_SIZE;
  253.                   $streamData .= substr($rootdata, $pos, SMALL_BLOCK_SIZE);
  254.                   $block = $this->smallBlockChain[$block];
  255.             }
  256.             return $streamData;
  257.         }else{
  258.             $numBlocks = $this->props[$this->wrkbook]['size'] / BIG_BLOCK_SIZE;
  259.             if ($this->props[$this->wrkbook]['size'] % BIG_BLOCK_SIZE != 0) {
  260.                 $numBlocks++;
  261.             }
  262.  
  263.             if ($numBlocks == 0) return '';
  264.             $streamData = '';
  265.             $block = $this->props[$this->wrkbook]['startBlock'];
  266.             $pos = 0;
  267.             while ($block != -2) {
  268.               $pos = ($block + 1) * BIG_BLOCK_SIZE;
  269.               $streamData .= substr($this->data, $pos, BIG_BLOCK_SIZE);
  270.               $block = $this->bigBlockChain[$block];
  271.             }
  272.             return $streamData;
  273.         }
  274.     }
  275.  
  276. }
  277.  
  278. define('SPREADSHEET_EXCEL_READER_BIFF8',             0x600);
  279. define('SPREADSHEET_EXCEL_READER_BIFF7',             0x500);
  280. define('SPREADSHEET_EXCEL_READER_WORKBOOKGLOBALS',   0x5);
  281. define('SPREADSHEET_EXCEL_READER_WORKSHEET',         0x10);
  282. define('SPREADSHEET_EXCEL_READER_TYPE_BOF',       0x809);
  283. define('SPREADSHEET_EXCEL_READER_TYPE_EOF',       0x0a);
  284. define('SPREADSHEET_EXCEL_READER_TYPE_BOUNDSHEET',   0x85);
  285. define('SPREADSHEET_EXCEL_READER_TYPE_DIMENSION',   0x200);
  286. define('SPREADSHEET_EXCEL_READER_TYPE_ROW',       0x208);
  287. define('SPREADSHEET_EXCEL_READER_TYPE_DBCELL',     0xd7);
  288. define('SPREADSHEET_EXCEL_READER_TYPE_FILEPASS',     0x2f);
  289. define('SPREADSHEET_EXCEL_READER_TYPE_NOTE',         0x1c);
  290. define('SPREADSHEET_EXCEL_READER_TYPE_TXO',       0x1b6);
  291. define('SPREADSHEET_EXCEL_READER_TYPE_RK',         0x7e);
  292. define('SPREADSHEET_EXCEL_READER_TYPE_RK2',       0x27e);
  293. define('SPREADSHEET_EXCEL_READER_TYPE_MULRK',       0xbd);
  294. define('SPREADSHEET_EXCEL_READER_TYPE_MULBLANK',     0xbe);
  295. define('SPREADSHEET_EXCEL_READER_TYPE_INDEX',       0x20b);
  296. define('SPREADSHEET_EXCEL_READER_TYPE_SST',       0xfc);
  297. define('SPREADSHEET_EXCEL_READER_TYPE_EXTSST',     0xff);
  298. define('SPREADSHEET_EXCEL_READER_TYPE_CONTINUE',     0x3c);
  299. define('SPREADSHEET_EXCEL_READER_TYPE_LABEL',       0x204);
  300. define('SPREADSHEET_EXCEL_READER_TYPE_LABELSST',     0xfd);
  301. define('SPREADSHEET_EXCEL_READER_TYPE_NUMBER',     0x203);
  302. define('SPREADSHEET_EXCEL_READER_TYPE_NAME',         0x18);
  303. define('SPREADSHEET_EXCEL_READER_TYPE_ARRAY',       0x221);
  304. define('SPREADSHEET_EXCEL_READER_TYPE_STRING',     0x207);
  305. define('SPREADSHEET_EXCEL_READER_TYPE_FORMULA',   0x406);
  306. define('SPREADSHEET_EXCEL_READER_TYPE_FORMULA2',     0x6);
  307. define('SPREADSHEET_EXCEL_READER_TYPE_FORMAT',     0x41e);
  308. define('SPREADSHEET_EXCEL_READER_TYPE_XF',         0xe0);
  309. define('SPREADSHEET_EXCEL_READER_TYPE_BOOLERR',   0x205);
  310. define('SPREADSHEET_EXCEL_READER_TYPE_FONT',      0x0031);
  311. define('SPREADSHEET_EXCEL_READER_TYPE_PALETTE',   0x0092);
  312. define('SPREADSHEET_EXCEL_READER_TYPE_UNKNOWN',   0xffff);
  313. define('SPREADSHEET_EXCEL_READER_TYPE_NINETEENFOUR', 0x22);
  314. define('SPREADSHEET_EXCEL_READER_TYPE_MERGEDCELLS',  0xE5);
  315. define('SPREADSHEET_EXCEL_READER_UTCOFFSETDAYS' ,   25569);
  316. define('SPREADSHEET_EXCEL_READER_UTCOFFSETDAYS1904', 24107);
  317. define('SPREADSHEET_EXCEL_READER_MSINADAY',       86400);
  318. define('SPREADSHEET_EXCEL_READER_TYPE_HYPER',        0x01b8);
  319. define('SPREADSHEET_EXCEL_READER_TYPE_COLINFO',      0x7d);
  320. define('SPREADSHEET_EXCEL_READER_TYPE_DEFCOLWIDTH',  0x55);
  321. define('SPREADSHEET_EXCEL_READER_TYPE_STANDARDWIDTH', 0x99);
  322. define('SPREADSHEET_EXCEL_READER_DEF_NUM_FORMAT',   "%s");
  323.  
  324.  
  325. /*
  326. * Main Class
  327. */
  328. class ExcelReader {
  329.  
  330.     // MK: Added to make data retrieval easier
  331.     var $colnames = array();
  332.     var $colindexes = array();
  333.     var $standardColWidth = 0;
  334.     var $defaultColWidth = 0;
  335.  
  336.     function myHex($d) {
  337.         if ($d < 16) return "0" . dechex($d);
  338.         return dechex($d);
  339.     }
  340.  
  341.     function dumpHexData($data, $pos, $length) {
  342.         $info = "";
  343.         for ($i = 0; $i <= $length; $i++) {
  344.             $info .= ($i==0?"":" ") . $this->myHex(ord($data[$pos + $i])) . (ord($data[$pos + $i])>31? "[" . $data[$pos + $i] . "]":'');
  345.         }
  346.         return $info;
  347.     }
  348.  
  349.     function getCol($col) {
  350.         if (is_string($col)) {
  351.             $col = strtolower($col);
  352.             if (array_key_exists($col,$this->colnames)) {
  353.                 $col = $this->colnames[$col];
  354.             }
  355.         }
  356.         return $col;
  357.     }
  358.  
  359.     // PUBLIC API FUNCTIONS
  360.     // --------------------
  361.  
  362.     function val($row,$col,$sheet=0) {
  363.         $col = $this->getCol($col);
  364.         if (array_key_exists($row,$this->sheets[$sheet]['cells']) && array_key_exists($col,$this->sheets[$sheet]['cells'][$row])) {
  365.             return $this->sheets[$sheet]['cells'][$row][$col];
  366.         }
  367.         return "";
  368.     }
  369.     function value($row,$col,$sheet=0) {
  370.         return $this->val($row,$col,$sheet);
  371.     }
  372.     function info($row,$col,$type='',$sheet=0) {
  373.         $col = $this->getCol($col);
  374.         if (array_key_exists('cellsInfo',$this->sheets[$sheet])
  375.                 && array_key_exists($row,$this->sheets[$sheet]['cellsInfo'])
  376.                 && array_key_exists($col,$this->sheets[$sheet]['cellsInfo'][$row])
  377.                 && array_key_exists($type,$this->sheets[$sheet]['cellsInfo'][$row][$col])) {
  378.             return $this->sheets[$sheet]['cellsInfo'][$row][$col][$type];
  379.         }
  380.         return "";
  381.     }
  382.     function type($row,$col,$sheet=0) {
  383.         return $this->info($row,$col,'type',$sheet);
  384.     }
  385.     function raw($row,$col,$sheet=0) {
  386.         return $this->info($row,$col,'raw',$sheet);
  387.     }
  388.     function rowspan($row,$col,$sheet=0) {
  389.         $val = $this->info($row,$col,'rowspan',$sheet);
  390.         if ($val=="") { return 1; }
  391.         return $val;
  392.     }
  393.     function colspan($row,$col,$sheet=0) {
  394.         $val = $this->info($row,$col,'colspan',$sheet);
  395.         if ($val=="") { return 1; }
  396.         return $val;
  397.     }
  398.     function hyperlink($row,$col,$sheet=0) {
  399.         $link = $this->sheets[$sheet]['cellsInfo'][$row][$col]['hyperlink'];
  400.         if ($link) {
  401.             return $link['link'];
  402.         }
  403.         return '';
  404.     }
  405.     function rowcount($sheet=0) {
  406.         return $this->sheets[$sheet]['numRows'];
  407.     }
  408.     function colcount($sheet=0) {
  409.         return $this->sheets[$sheet]['numCols'];
  410.     }
  411.     function colwidth($col,$sheet=0) {
  412.         // Col width is actually the width of the number 0. So we have to estimate and come close
  413.         return $this->colInfo[$sheet][$col]['width']/9142*200;
  414.     }
  415.     function colhidden($col,$sheet=0) {
  416.         return !!$this->colInfo[$sheet][$col]['hidden'];
  417.     }
  418.     function rowheight($row,$sheet=0) {
  419.         return $this->rowInfo[$sheet][$row]['height'];
  420.     }
  421.     function rowhidden($row,$sheet=0) {
  422.         return !!$this->rowInfo[$sheet][$row]['hidden'];
  423.     }
  424.  
  425.     // GET THE CSS FOR FORMATTING
  426.     // ==========================
  427.     function style($row,$col,$sheet=0,$properties='') {
  428.         $css = "";
  429.         $font=$this->font($row,$col,$sheet);
  430.         if ($font!="") {
  431.             $css .= "font-family:$font;";
  432.         }
  433.         $align=$this->align($row,$col,$sheet);
  434.         if ($align!="") {
  435.             $css .= "text-align:$align;";
  436.         }
  437.         $height=$this->height($row,$col,$sheet);
  438.         if ($height!="") {
  439.             $css .= "font-size:$height"."px;";
  440.         }
  441.         $bgcolor=$this->bgColor($row,$col,$sheet);
  442.         if ($bgcolor!="") {
  443.             $bgcolor = $this->colors[$bgcolor];
  444.             $css .= "background-color:$bgcolor;";
  445.         }
  446.         $color=$this->color($row,$col,$sheet);
  447.         if ($color!="") {
  448.             $css .= "color:$color;";
  449.         }
  450.         $bold=$this->bold($row,$col,$sheet);
  451.         if ($bold) {
  452.             $css .= "font-weight:bold;";
  453.         }
  454.         $italic=$this->italic($row,$col,$sheet);
  455.         if ($italic) {
  456.             $css .= "font-style:italic;";
  457.         }
  458.         $underline=$this->underline($row,$col,$sheet);
  459.         if ($underline) {
  460.             $css .= "text-decoration:underline;";
  461.         }
  462.         // Borders
  463.         $bLeft = $this->borderLeft($row,$col,$sheet);
  464.         $bRight = $this->borderRight($row,$col,$sheet);
  465.         $bTop = $this->borderTop($row,$col,$sheet);
  466.         $bBottom = $this->borderBottom($row,$col,$sheet);
  467.         $bLeftCol = $this->borderLeftColor($row,$col,$sheet);
  468.         $bRightCol = $this->borderRightColor($row,$col,$sheet);
  469.         $bTopCol = $this->borderTopColor($row,$col,$sheet);
  470.         $bBottomCol = $this->borderBottomColor($row,$col,$sheet);
  471.         // Try to output the minimal required style
  472.         if ($bLeft!="" && $bLeft==$bRight && $bRight==$bTop && $bTop==$bBottom) {
  473.             $css .= "border:" . $this->lineStylesCss[$bLeft] .";";
  474.         }
  475.         else {
  476.             if ($bLeft!="") { $css .= "border-left:" . $this->lineStylesCss[$bLeft] .";"; }
  477.             if ($bRight!="") { $css .= "border-right:" . $this->lineStylesCss[$bRight] .";"; }
  478.             if ($bTop!="") { $css .= "border-top:" . $this->lineStylesCss[$bTop] .";"; }
  479.             if ($bBottom!="") { $css .= "border-bottom:" . $this->lineStylesCss[$bBottom] .";"; }
  480.         }
  481.         // Only output border colors if there is an actual border specified
  482.         if ($bLeft!="" && $bLeftCol!="") { $css .= "border-left-color:" . $bLeftCol .";"; }
  483.         if ($bRight!="" && $bRightCol!="") { $css .= "border-right-color:" . $bRightCol .";"; }
  484.         if ($bTop!="" && $bTopCol!="") { $css .= "border-top-color:" . $bTopCol . ";"; }
  485.         if ($bBottom!="" && $bBottomCol!="") { $css .= "border-bottom-color:" . $bBottomCol .";"; }
  486.  
  487.         return $css;
  488.     }
  489.  
  490.     // FORMAT PROPERTIES
  491.     // =================
  492.     function format($row,$col,$sheet=0) {
  493.         return $this->info($row,$col,'format',$sheet);
  494.     }
  495.     function formatIndex($row,$col,$sheet=0) {
  496.         return $this->info($row,$col,'formatIndex',$sheet);
  497.     }
  498.     function formatColor($row,$col,$sheet=0) {
  499.         return $this->info($row,$col,'formatColor',$sheet);
  500.     }
  501.  
  502.     // CELL (XF) PROPERTIES
  503.     // ====================
  504.     function xfRecord($row,$col,$sheet=0) {
  505.         $xfIndex = $this->info($row,$col,'xfIndex',$sheet);
  506.         if ($xfIndex!="") {
  507.             return $this->xfRecords[$xfIndex];
  508.         }
  509.         return null;
  510.     }
  511.     function xfProperty($row,$col,$sheet,$prop) {
  512.         $xfRecord = $this->xfRecord($row,$col,$sheet);
  513.         if ($xfRecord!=null) {
  514.             return $xfRecord[$prop];
  515.         }
  516.         return "";
  517.     }
  518.     function align($row,$col,$sheet=0) {
  519.         return $this->xfProperty($row,$col,$sheet,'align');
  520.     }
  521.     function bgColor($row,$col,$sheet=0) {
  522.         return $this->xfProperty($row,$col,$sheet,'bgColor');
  523.     }
  524.     function borderLeft($row,$col,$sheet=0) {
  525.         return $this->xfProperty($row,$col,$sheet,'borderLeft');
  526.     }
  527.     function borderRight($row,$col,$sheet=0) {
  528.         return $this->xfProperty($row,$col,$sheet,'borderRight');
  529.     }
  530.     function borderTop($row,$col,$sheet=0) {
  531.         return $this->xfProperty($row,$col,$sheet,'borderTop');
  532.     }
  533.     function borderBottom($row,$col,$sheet=0) {
  534.         return $this->xfProperty($row,$col,$sheet,'borderBottom');
  535.     }
  536.     function borderLeftColor($row,$col,$sheet=0) {
  537.         return $this->colors[$this->xfProperty($row,$col,$sheet,'borderLeftColor')];
  538.     }
  539.     function borderRightColor($row,$col,$sheet=0) {
  540.         return $this->colors[$this->xfProperty($row,$col,$sheet,'borderRightColor')];
  541.     }
  542.     function borderTopColor($row,$col,$sheet=0) {
  543.         return $this->colors[$this->xfProperty($row,$col,$sheet,'borderTopColor')];
  544.     }
  545.     function borderBottomColor($row,$col,$sheet=0) {
  546.         return $this->colors[$this->xfProperty($row,$col,$sheet,'borderBottomColor')];
  547.     }
  548.  
  549.     // FONT PROPERTIES
  550.     // ===============
  551.     function fontRecord($row,$col,$sheet=0) {
  552.         $xfRecord = $this->xfRecord($row,$col,$sheet);
  553.         if ($xfRecord!=null) {
  554.             $font = $xfRecord['fontIndex'];
  555.             if ($font!=null) {
  556.                 return $this->fontRecords[$font];
  557.             }
  558.         }
  559.         return null;
  560.     }
  561.     function fontProperty($row,$col,$sheet=0,$prop) {
  562.         $font = $this->fontRecord($row,$col,$sheet);
  563.         if ($font!=null) {
  564.             return $font[$prop];
  565.         }
  566.         return false;
  567.     }
  568.     function fontIndex($row,$col,$sheet=0) {
  569.         return $this->xfProperty($row,$col,$sheet,'fontIndex');
  570.     }
  571.     function color($row,$col,$sheet=0) {
  572.         $formatColor = $this->formatColor($row,$col,$sheet);
  573.         if ($formatColor!="") {
  574.             return $formatColor;
  575.         }
  576.         $ci = $this->fontProperty($row,$col,$sheet,'color');
  577.                 return $this->rawColor($ci);
  578.         }
  579.         function rawColor($ci) {
  580.         if (($ci <> 0x7FFF) && ($ci <> '')) {
  581.             return $this->colors[$ci];
  582.         }
  583.         return "";
  584.     }
  585.     function bold($row,$col,$sheet=0) {
  586.         return $this->fontProperty($row,$col,$sheet,'bold');
  587.     }
  588.     function italic($row,$col,$sheet=0) {
  589.         return $this->fontProperty($row,$col,$sheet,'italic');
  590.     }
  591.     function underline($row,$col,$sheet=0) {
  592.         return $this->fontProperty($row,$col,$sheet,'under');
  593.     }
  594.     function height($row,$col,$sheet=0) {
  595.         return $this->fontProperty($row,$col,$sheet,'height');
  596.     }
  597.     function font($row,$col,$sheet=0) {
  598.         return $this->fontProperty($row,$col,$sheet,'font');
  599.     }
  600.  
  601.     // DUMP AN HTML TABLE OF THE ENTIRE XLS DATA
  602.     // =========================================
  603.     function dump($row_numbers=false,$col_letters=false,$sheet=0,$table_class='excel') {
  604.         $out = "<table class=\"$table_class\" cellspacing=0>";
  605.         if ($col_letters) {
  606.             $out .= "<thead>\n\t<tr>";
  607.             if ($row_numbers) {
  608.                 $out .= "\n\t\t<th>&nbsp</th>";
  609.             }
  610.             for($i=1;$i<=$this->colcount($sheet);$i++) {
  611.                 $style = "width:" . ($this->colwidth($i,$sheet)*1) . "px;";
  612.                 if ($this->colhidden($i,$sheet)) {
  613.                     $style .= "display:none;";
  614.                 }
  615.                 $out .= "\n\t\t<th style=\"$style\">" . strtoupper($this->colindexes[$i]) . "</th>";
  616.             }
  617.             $out .= "</tr></thead>\n";
  618.         }
  619.  
  620.         $out .= "<tbody>\n";
  621.         for($row=1;$row<=$this->rowcount($sheet);$row++) {
  622.             $rowheight = $this->rowheight($row,$sheet);
  623.             $style = "height:" . ($rowheight*(4/3)) . "px;";
  624.             if ($this->rowhidden($row,$sheet)) {
  625.                 $style .= "display:none;";
  626.             }
  627.             $out .= "\n\t<tr style=\"$style\">";
  628.             if ($row_numbers) {
  629.                 $out .= "\n\t\t<th>$row</th>";
  630.             }
  631.             for($col=1;$col<=$this->colcount($sheet);$col++) {
  632.                 // Account for Rowspans/Colspans
  633.                 $rowspan = $this->rowspan($row,$col,$sheet);
  634.                 $colspan = $this->colspan($row,$col,$sheet);
  635.                 for($i=0;$i<$rowspan;$i++) {
  636.                     for($j=0;$j<$colspan;$j++) {
  637.                         if ($i>0 || $j>0) {
  638.                             $this->sheets[$sheet]['cellsInfo'][$row+$i][$col+$j]['dontprint']=1;
  639.                         }
  640.                     }
  641.                 }
  642.                 if(!$this->sheets[$sheet]['cellsInfo'][$row][$col]['dontprint']) {
  643.                     $style = $this->style($row,$col,$sheet);
  644.                     if ($this->colhidden($col,$sheet)) {
  645.                         $style .= "display:none;";
  646.                     }
  647.                     $out .= "\n\t\t<td style=\"$style\"" . ($colspan > 1?" colspan=$colspan":"") . ($rowspan > 1?" rowspan=$rowspan":"") . ">";
  648.                     $val = $this->val($row,$col,$sheet);
  649.                     if ($val=='') { $val="&nbsp;"; }
  650.                     else {
  651.                         $val = htmlentities($val);
  652.                         $link = $this->hyperlink($row,$col,$sheet);
  653.                         if ($link!='') {
  654.                             $val = "<a href=\"$link\">$val</a>";
  655.                         }
  656.                     }
  657.                     $out .= "<nobr>".nl2br($val)."</nobr>";
  658.                     $out .= "</td>";
  659.                 }
  660.             }
  661.             $out .= "</tr>\n";
  662.         }
  663.         $out .= "</tbody></table>";
  664.         return $out;
  665.     }
  666.  
  667.     // --------------
  668.     // END PUBLIC API
  669.  
  670.  
  671.     var $boundsheets = array();
  672.     var $formatRecords = array();
  673.     var $fontRecords = array();
  674.     var $xfRecords = array();
  675.     var $colInfo = array();
  676.     var $rowInfo = array();
  677.  
  678.     var $sst = array();
  679.     var $sheets = array();
  680.  
  681.     var $data;
  682.     var $_ole;
  683.     var $_defaultEncoding = "UTF-8";
  684.     var $_defaultFormat = SPREADSHEET_EXCEL_READER_DEF_NUM_FORMAT;
  685.     var $_columnsFormat = array();
  686.     var $_rowoffset = 1;
  687.     var $_coloffset = 1;
  688.  
  689.     /**
  690.      * List of default date formats used by Excel
  691.      */
  692.     var $dateFormats = array (
  693.         0xe => "m/d/Y",
  694.         0xf => "M-d-Y",
  695.         0x10 => "d-M",
  696.         0x11 => "M-Y",
  697.         0x12 => "h:i a",
  698.         0x13 => "h:i:s a",
  699.         0x14 => "H:i",
  700.         0x15 => "H:i:s",
  701.         0x16 => "d/m/Y H:i",
  702.         0x2d => "i:s",
  703.         0x2e => "H:i:s",
  704.         0x2f => "i:s.S"
  705.     );
  706.  
  707.     /**
  708.      * Default number formats used by Excel
  709.      */
  710.     var $numberFormats = array(
  711.         0x1 => "0",
  712.         0x2 => "0.00",
  713.         0x3 => "#,##0",
  714.         0x4 => "#,##0.00",
  715.         0x5 => "\$#,##0;(\$#,##0)",
  716.         0x6 => "\$#,##0;[Red](\$#,##0)",
  717.         0x7 => "\$#,##0.00;(\$#,##0.00)",
  718.         0x8 => "\$#,##0.00;[Red](\$#,##0.00)",
  719.         0x9 => "0%",
  720.         0xa => "0.00%",
  721.         0xb => "0.00E+00",
  722.         0x25 => "#,##0;(#,##0)",
  723.         0x26 => "#,##0;[Red](#,##0)",
  724.         0x27 => "#,##0.00;(#,##0.00)",
  725.         0x28 => "#,##0.00;[Red](#,##0.00)",
  726.         0x29 => "#,##0;(#,##0)",  // Not exactly
  727.         0x2a => "\$#,##0;(\$#,##0)",  // Not exactly
  728.         0x2b => "#,##0.00;(#,##0.00)",  // Not exactly
  729.         0x2c => "\$#,##0.00;(\$#,##0.00)",  // Not exactly
  730.         0x30 => "##0.0E+0"
  731.     );
  732.  
  733.     var $colors = Array(
  734.         0x00 => "#000000",
  735.         0x01 => "#FFFFFF",
  736.         0x02 => "#FF0000",
  737.         0x03 => "#00FF00",
  738.         0x04 => "#0000FF",
  739.         0x05 => "#FFFF00",
  740.         0x06 => "#FF00FF",
  741.         0x07 => "#00FFFF",
  742.         0x08 => "#000000",
  743.         0x09 => "#FFFFFF",
  744.         0x0A => "#FF0000",
  745.         0x0B => "#00FF00",
  746.         0x0C => "#0000FF",
  747.         0x0D => "#FFFF00",
  748.         0x0E => "#FF00FF",
  749.         0x0F => "#00FFFF",
  750.         0x10 => "#800000",
  751.         0x11 => "#008000",
  752.         0x12 => "#000080",
  753.         0x13 => "#808000",
  754.         0x14 => "#800080",
  755.         0x15 => "#008080",
  756.         0x16 => "#C0C0C0",
  757.         0x17 => "#808080",
  758.         0x18 => "#9999FF",
  759.         0x19 => "#993366",
  760.         0x1A => "#FFFFCC",
  761.         0x1B => "#CCFFFF",
  762.         0x1C => "#660066",
  763.         0x1D => "#FF8080",
  764.         0x1E => "#0066CC",
  765.         0x1F => "#CCCCFF",
  766.         0x20 => "#000080",
  767.         0x21 => "#FF00FF",
  768.         0x22 => "#FFFF00",
  769.         0x23 => "#00FFFF",
  770.         0x24 => "#800080",
  771.         0x25 => "#800000",
  772.         0x26 => "#008080",
  773.         0x27 => "#0000FF",
  774.         0x28 => "#00CCFF",
  775.         0x29 => "#CCFFFF",
  776.         0x2A => "#CCFFCC",
  777.         0x2B => "#FFFF99",
  778.         0x2C => "#99CCFF",
  779.         0x2D => "#FF99CC",
  780.         0x2E => "#CC99FF",
  781.         0x2F => "#FFCC99",
  782.         0x30 => "#3366FF",
  783.         0x31 => "#33CCCC",
  784.         0x32 => "#99CC00",
  785.         0x33 => "#FFCC00",
  786.         0x34 => "#FF9900",
  787.         0x35 => "#FF6600",
  788.         0x36 => "#666699",
  789.         0x37 => "#969696",
  790.         0x38 => "#003366",
  791.         0x39 => "#339966",
  792.         0x3A => "#003300",
  793.         0x3B => "#333300",
  794.         0x3C => "#993300",
  795.         0x3D => "#993366",
  796.         0x3E => "#333399",
  797.         0x3F => "#333333",
  798.         0x40 => "#000000",
  799.         0x41 => "#FFFFFF",
  800.  
  801.         0x43 => "#000000",
  802.         0x4D => "#000000",
  803.         0x4E => "#FFFFFF",
  804.         0x4F => "#000000",
  805.         0x50 => "#FFFFFF",
  806.         0x51 => "#000000",
  807.  
  808.         0x7FFF => "#000000"
  809.     );
  810.  
  811.     var $lineStyles = array(
  812.         0x00 => "",
  813.         0x01 => "Thin",
  814.         0x02 => "Medium",
  815.         0x03 => "Dashed",
  816.         0x04 => "Dotted",
  817.         0x05 => "Thick",
  818.         0x06 => "Double",
  819.         0x07 => "Hair",
  820.         0x08 => "Medium dashed",
  821.         0x09 => "Thin dash-dotted",
  822.         0x0A => "Medium dash-dotted",
  823.         0x0B => "Thin dash-dot-dotted",
  824.         0x0C => "Medium dash-dot-dotted",
  825.         0x0D => "Slanted medium dash-dotted"
  826.     );
  827.  
  828.     var $lineStylesCss = array(
  829.         "Thin" => "1px solid",
  830.         "Medium" => "2px solid",
  831.         "Dashed" => "1px dashed",
  832.         "Dotted" => "1px dotted",
  833.         "Thick" => "3px solid",
  834.         "Double" => "double",
  835.         "Hair" => "1px solid",
  836.         "Medium dashed" => "2px dashed",
  837.         "Thin dash-dotted" => "1px dashed",
  838.         "Medium dash-dotted" => "2px dashed",
  839.         "Thin dash-dot-dotted" => "1px dashed",
  840.         "Medium dash-dot-dotted" => "2px dashed",
  841.         "Slanted medium dash-dotte" => "2px dashed"
  842.     );
  843.  
  844.     function read16bitstring($data, $start) {
  845.         $len = 0;
  846.         while (ord($data[$start + $len]) + ord($data[$start + $len + 1]) > 0) $len++;
  847.         return substr($data, $start, $len);
  848.     }
  849.  
  850.     // ADDED by Matt Kruse for better formatting
  851.     function _format_value($format,$num,$f) {
  852.         // 49==TEXT format
  853.         // http://code.google.com/p/php-excel-reader/issues/detail?id=7
  854.         if ( (!$f && $format=="%s") || ($f==49) || ($format=="GENERAL") ) {
  855.             return array('string'=>$num, 'formatColor'=>null);
  856.         }
  857.  
  858.         // Custom pattern can be POSITIVE;NEGATIVE;ZERO
  859.         // The "text" option as 4th parameter is not handled
  860.         //$parts = split(";",$format); LGBN
  861.         $parts = explode(';', $format);
  862.         $pattern = $parts[0];
  863.         // Negative pattern
  864.         if (count($parts)>2 && $num==0) {
  865.             $pattern = $parts[2];
  866.         }
  867.         // Zero pattern
  868.         if (count($parts)>1 && $num<0) {
  869.             $pattern = $parts[1];
  870.             $num = abs($num);
  871.         }
  872.  
  873.         $color = "";
  874.         $matches = array();
  875.         $color_regex = "/^\[(BLACK|BLUE|CYAN|GREEN|MAGENTA|RED|WHITE|YELLOW)\]/i";
  876.         if (preg_match($color_regex,$pattern,$matches)) {
  877.             $color = strtolower($matches[1]);
  878.             $pattern = preg_replace($color_regex,"",$pattern);
  879.         }
  880.  
  881.         // In Excel formats, "_" is used to add spacing, which we can't do in HTML
  882.         $pattern = preg_replace("/_./","",$pattern);
  883.  
  884.         // Some non-number characters are escaped with \, which we don't need
  885.         $pattern = preg_replace("/\\\/","",$pattern);
  886.  
  887.         // Some non-number strings are quoted, so we'll get rid of the quotes
  888.         $pattern = preg_replace("/\"/","",$pattern);
  889.  
  890.         // TEMPORARY - Convert # to 0
  891.         $pattern = preg_replace("/\#/","0",$pattern);
  892.  
  893.         // Find out if we need comma formatting
  894.         $has_commas = preg_match("/,/",$pattern);
  895.         if ($has_commas) {
  896.             $pattern = preg_replace("/,/","",$pattern);
  897.         }
  898.  
  899.         // Handle Percentages
  900.         if (preg_match("/\d(\%)([^\%]|$)/",$pattern,$matches)) {
  901.             $num = $num * 100;
  902.             $pattern = preg_replace("/(\d)(\%)([^\%]|$)/","$1%$3",$pattern);
  903.         }
  904.  
  905.         // Handle the number itself
  906.         $number_regex = "/(\d+)(\.?)(\d*)/";
  907.         if (preg_match($number_regex,$pattern,$matches)) {
  908.             $left = $matches[1];
  909.             $dec = $matches[2];
  910.             $right = $matches[3];
  911.             if ($has_commas) {
  912.                 $formatted = number_format($num,strlen($right));
  913.             }
  914.             else {
  915.                 $sprintf_pattern = "%1.".strlen($right)."f";
  916.                 $formatted = sprintf($sprintf_pattern, $num);
  917.             }
  918.             $pattern = preg_replace($number_regex, $formatted, $pattern);
  919.         }
  920.  
  921.         return array(
  922.             'string'=>$pattern,
  923.             'formatColor'=>$color
  924.         );
  925.     }
  926.  
  927.     /**
  928.      * Constructor
  929.      *
  930.      * Some basic initialisation
  931.      */
  932. //  function ExcelReader($file='',$store_extended_info=true,$outputEncoding='') {
  933. //  }
  934.  
  935.  
  936.     /**
  937.      * ExcelReader constructor.
  938.      *
  939.      * @param string $file
  940.      * @param bool   $store_extended_info
  941.      * @param string $outputEncoding
  942.      */
  943.     public function __construct($file='',$store_extended_info=true,$outputEncoding='')
  944.     {
  945.         $this->_ole = new OLERead();
  946.         $this->setUTFEncoder('iconv');
  947.         if ($outputEncoding != '') {
  948.             $this->setOutputEncoding($outputEncoding);
  949.         }
  950.         for ($i=1; $i<245; $i++) {
  951.             $name = strtolower(( (($i-1)/26>=1)?chr(($i-1)/26+64):'') . chr(($i-1)%26+65));
  952.             $this->colnames[$name] = $i;
  953.             $this->colindexes[$i] = $name;
  954.         }
  955.         $this->store_extended_info = $store_extended_info;
  956.         if ($file!="") {
  957.             $this->read($file);
  958.         }
  959.     }
  960.  
  961.     /**
  962.      * Set the encoding method
  963.      */
  964.     function setOutputEncoding($encoding) {
  965.         $this->_defaultEncoding = $encoding;
  966.     }
  967.  
  968.     /**
  969.      *  $encoder = 'iconv' or 'mb'
  970.      *  set iconv if you would like use 'iconv' for encode UTF-16LE to your encoding
  971.      *  set mb if you would like use 'mb_convert_encoding' for encode UTF-16LE to your encoding
  972.      */
  973.     function setUTFEncoder($encoder = 'iconv') {
  974.         $this->_encoderFunction = '';
  975.         if ($encoder == 'iconv') {
  976.             $this->_encoderFunction = function_exists('iconv') ? 'iconv' : '';
  977.         } elseif ($encoder == 'mb') {
  978.             $this->_encoderFunction = function_exists('mb_convert_encoding') ? 'mb_convert_encoding' : '';
  979.         }
  980.     }
  981.  
  982.     function setRowColOffset($iOffset) {
  983.         $this->_rowoffset = $iOffset;
  984.         $this->_coloffset = $iOffset;
  985.     }
  986.  
  987.     /**
  988.      * Set the default number format
  989.      */
  990.     function setDefaultFormat($sFormat) {
  991.         $this->_defaultFormat = $sFormat;
  992.     }
  993.  
  994.     /**
  995.      * Force a column to use a certain format
  996.      */
  997.     function setColumnFormat($column, $sFormat) {
  998.         $this->_columnsFormat[$column] = $sFormat;
  999.     }
  1000.  
  1001.     /**
  1002.      * Read the spreadsheet file using OLE, then parse
  1003.      */
  1004.     function read($sFileName) {
  1005.         $res = $this->_ole->read($sFileName);
  1006.  
  1007.         // oops, something goes wrong (Darko Miljanovic)
  1008.         if($res === false) {
  1009.             // check error code
  1010.             if($this->_ole->error == 1) {
  1011.                 // bad file
  1012.                 die('The filename ' . $sFileName . ' is not readable');
  1013.             }
  1014.             // check other error codes here (eg bad fileformat, etc...)
  1015.         }
  1016.         $this->data = $this->_ole->getWorkBook();
  1017.         $this->_parse();
  1018.     }
  1019.  
  1020.     /**
  1021.      * Parse a workbook
  1022.      *
  1023.      * @access private
  1024.      * @return bool
  1025.      */
  1026.     function _parse() {
  1027.         $pos = 0;
  1028.         $data = $this->data;
  1029.  
  1030.         $code = v($data,$pos);
  1031.         $length = v($data,$pos+2);
  1032.         $version = v($data,$pos+4);
  1033.         $substreamType = v($data,$pos+6);
  1034.  
  1035.         $this->version = $version;
  1036.  
  1037.         if (($version != SPREADSHEET_EXCEL_READER_BIFF8) &&
  1038.             ($version != SPREADSHEET_EXCEL_READER_BIFF7)) {
  1039.             return false;
  1040.         }
  1041.  
  1042.         if ($substreamType != SPREADSHEET_EXCEL_READER_WORKBOOKGLOBALS){
  1043.             return false;
  1044.         }
  1045.  
  1046.         $pos += $length + 4;
  1047.  
  1048.         $code = v($data,$pos);
  1049.         $length = v($data,$pos+2);
  1050.  
  1051.         while ($code != SPREADSHEET_EXCEL_READER_TYPE_EOF) {
  1052.             switch ($code) {
  1053.                 case SPREADSHEET_EXCEL_READER_TYPE_SST:
  1054.                     $spos = $pos + 4;
  1055.                     $limitpos = $spos + $length;
  1056.                     $uniqueStrings = $this->_GetInt4d($data, $spos+4);
  1057.                     $spos += 8;
  1058.                     for ($i = 0; $i < $uniqueStrings; $i++) {
  1059.                         // Read in the number of characters
  1060.                         if ($spos == $limitpos) {
  1061.                             $opcode = v($data,$spos);
  1062.                             $conlength = v($data,$spos+2);
  1063.                             if ($opcode != 0x3c) {
  1064.                                 return -1;
  1065.                             }
  1066.                             $spos += 4;
  1067.                             $limitpos = $spos + $conlength;
  1068.                         }
  1069.                         $numChars = ord($data[$spos]) | (ord($data[$spos+1]) << 8);
  1070.                         $spos += 2;
  1071.                         $optionFlags = ord($data[$spos]);
  1072.                         $spos++;
  1073.                         $asciiEncoding = (($optionFlags & 0x01) == 0) ;
  1074.                         $extendedString = ( ($optionFlags & 0x04) != 0);
  1075.  
  1076.                         // See if string contains formatting information
  1077.                         $richString = ( ($optionFlags & 0x08) != 0);
  1078.  
  1079.                         if ($richString) {
  1080.                             // Read in the crun
  1081.                             $formattingRuns = v($data,$spos);
  1082.                             $spos += 2;
  1083.                         }
  1084.  
  1085.                         if ($extendedString) {
  1086.                             // Read in cchExtRst
  1087.                             $extendedRunLength = $this->_GetInt4d($data, $spos);
  1088.                             $spos += 4;
  1089.                         }
  1090.  
  1091.                         $len = ($asciiEncoding)? $numChars : $numChars*2;
  1092.                         if ($spos + $len < $limitpos) {
  1093.                             $retstr = substr($data, $spos, $len);
  1094.                             $spos += $len;
  1095.                         }
  1096.                         else{
  1097.                             // found countinue
  1098.                             $retstr = substr($data, $spos, $limitpos - $spos);
  1099.                             $bytesRead = $limitpos - $spos;
  1100.                             $charsLeft = $numChars - (($asciiEncoding) ? $bytesRead : ($bytesRead / 2));
  1101.                             $spos = $limitpos;
  1102.  
  1103.                             while ($charsLeft > 0){
  1104.                                 $opcode = v($data,$spos);
  1105.                                 $conlength = v($data,$spos+2);
  1106.                                 if ($opcode != 0x3c) {
  1107.                                     return -1;
  1108.                                 }
  1109.                                 $spos += 4;
  1110.                                 $limitpos = $spos + $conlength;
  1111.                                 $option = ord($data[$spos]);
  1112.                                 $spos += 1;
  1113.                                 if ($asciiEncoding && ($option == 0)) {
  1114.                                     $len = min($charsLeft, $limitpos - $spos); // min($charsLeft, $conlength);
  1115.                                     $retstr .= substr($data, $spos, $len);
  1116.                                     $charsLeft -= $len;
  1117.                                     $asciiEncoding = true;
  1118.                                 }
  1119.                                 elseif (!$asciiEncoding && ($option != 0)) {
  1120.                                     $len = min($charsLeft * 2, $limitpos - $spos); // min($charsLeft, $conlength);
  1121.                                     $retstr .= substr($data, $spos, $len);
  1122.                                     $charsLeft -= $len/2;
  1123.                                     $asciiEncoding = false;
  1124.                                 }
  1125.                                 elseif (!$asciiEncoding && ($option == 0)) {
  1126.                                     // Bummer - the string starts off as Unicode, but after the
  1127.                                     // continuation it is in straightforward ASCII encoding
  1128.                                     $len = min($charsLeft, $limitpos - $spos); // min($charsLeft, $conlength);
  1129.                                     for ($j = 0; $j < $len; $j++) {
  1130.                                         $retstr .= $data[$spos + $j].chr(0);
  1131.                                     }
  1132.                                     $charsLeft -= $len;
  1133.                                     $asciiEncoding = false;
  1134.                                 }
  1135.                                 else{
  1136.                                     $newstr = '';
  1137.                                     for ($j = 0; $j < strlen($retstr); $j++) {
  1138.                                         $newstr = $retstr[$j].chr(0);
  1139.                                     }
  1140.                                     $retstr = $newstr;
  1141.                                     $len = min($charsLeft * 2, $limitpos - $spos); // min($charsLeft, $conlength);
  1142.                                     $retstr .= substr($data, $spos, $len);
  1143.                                     $charsLeft -= $len/2;
  1144.                                     $asciiEncoding = false;
  1145.                                 }
  1146.                                 $spos += $len;
  1147.                             }
  1148.                         }
  1149.                         $retstr = ($asciiEncoding) ? $retstr : $this->_encodeUTF16($retstr);
  1150.  
  1151.                         if ($richString){
  1152.                             $spos += 4 * $formattingRuns;
  1153.                         }
  1154.  
  1155.                         // For extended strings, skip over the extended string data
  1156.                         if ($extendedString) {
  1157.                             $spos += $extendedRunLength;
  1158.                         }
  1159.                         $this->sst[]=$retstr;
  1160.                     }
  1161.                     break;
  1162.                 case SPREADSHEET_EXCEL_READER_TYPE_FILEPASS:
  1163.                     return false;
  1164.                     break;
  1165.                 case SPREADSHEET_EXCEL_READER_TYPE_NAME:
  1166.                     break;
  1167.                 case SPREADSHEET_EXCEL_READER_TYPE_FORMAT:
  1168.                     $indexCode = v($data,$pos+4);
  1169.                     if ($version == SPREADSHEET_EXCEL_READER_BIFF8) {
  1170.                         $numchars = v($data,$pos+6);
  1171.                         if (ord($data[$pos+8]) == 0){
  1172.                             $formatString = substr($data, $pos+9, $numchars);
  1173.                         } else {
  1174.                             $formatString = substr($data, $pos+9, $numchars*2);
  1175.                         }
  1176.                     } else {
  1177.                         $numchars = ord($data[$pos+6]);
  1178.                         $formatString = substr($data, $pos+7, $numchars*2);
  1179.                     }
  1180.                     $this->formatRecords[$indexCode] = $formatString;
  1181.                     break;
  1182.                 case SPREADSHEET_EXCEL_READER_TYPE_FONT:
  1183.                         $height = v($data,$pos+4);
  1184.                         $option = v($data,$pos+6);
  1185.                         $color = v($data,$pos+8);
  1186.                         $weight = v($data,$pos+10);
  1187.                         $under  = ord($data[$pos+14]);
  1188.                         $font = "";
  1189.                         // Font name
  1190.                         $numchars = ord($data[$pos+18]);
  1191.                         if ((ord($data[$pos+19]) & 1) == 0){
  1192.                             $font = substr($data, $pos+20, $numchars);
  1193.                         } else {
  1194.                             $font = substr($data, $pos+20, $numchars*2);
  1195.                             $font =  $this->_encodeUTF16($font);
  1196.                         }
  1197.                         $this->fontRecords[] = array(
  1198.                                 'height' => $height / 20,
  1199.                                 'italic' => !!($option & 2),
  1200.                                 'color' => $color,
  1201.                                 'under' => !($under==0),
  1202.                                 'bold' => ($weight==700),
  1203.                                 'font' => $font,
  1204.                                 'raw' => $this->dumpHexData($data, $pos+3, $length)
  1205.                                 );
  1206.                         break;
  1207.  
  1208.                 case SPREADSHEET_EXCEL_READER_TYPE_PALETTE:
  1209.                         $colors = ord($data[$pos+4]) | ord($data[$pos+5]) << 8;
  1210.                         for ($coli = 0; $coli < $colors; $coli++) {
  1211.                             $colOff = $pos + 2 + ($coli * 4);
  1212.                             $colr = ord($data[$colOff]);
  1213.                             $colg = ord($data[$colOff+1]);
  1214.                             $colb = ord($data[$colOff+2]);
  1215.                             $this->colors[0x07 + $coli] = '#' . $this->myhex($colr) . $this->myhex($colg) . $this->myhex($colb);
  1216.                         }
  1217.                         break;
  1218.  
  1219.                 case SPREADSHEET_EXCEL_READER_TYPE_XF:
  1220.                         $fontIndexCode = (ord($data[$pos+4]) | ord($data[$pos+5]) << 8) - 1;
  1221.                         $fontIndexCode = max(0,$fontIndexCode);
  1222.                         $indexCode = ord($data[$pos+6]) | ord($data[$pos+7]) << 8;
  1223.                         $alignbit = ord($data[$pos+10]) & 3;
  1224.                         $bgi = (ord($data[$pos+22]) | ord($data[$pos+23]) << 8) & 0x3FFF;
  1225.                         $bgcolor = ($bgi & 0x7F);
  1226. //                      $bgcolor = ($bgi & 0x3f80) >> 7;
  1227.                         $align = "";
  1228.                         if ($alignbit==3) { $align="right"; }
  1229.                         if ($alignbit==2) { $align="center"; }
  1230.  
  1231.                         $fillPattern = (ord($data[$pos+21]) & 0xFC) >> 2;
  1232.                         if ($fillPattern == 0) {
  1233.                             $bgcolor = "";
  1234.                         }
  1235.  
  1236.                         $xf = array();
  1237.                         $xf['formatIndex'] = $indexCode;
  1238.                         $xf['align'] = $align;
  1239.                         $xf['fontIndex'] = $fontIndexCode;
  1240.                         $xf['bgColor'] = $bgcolor;
  1241.                         $xf['fillPattern'] = $fillPattern;
  1242.  
  1243.                         $border = ord($data[$pos+14]) | (ord($data[$pos+15]) << 8) | (ord($data[$pos+16]) << 16) | (ord($data[$pos+17]) << 24);
  1244.                         $xf['borderLeft'] = $this->lineStyles[($border & 0xF)];
  1245.                         $xf['borderRight'] = $this->lineStyles[($border & 0xF0) >> 4];
  1246.                         $xf['borderTop'] = $this->lineStyles[($border & 0xF00) >> 8];
  1247.                         $xf['borderBottom'] = $this->lineStyles[($border & 0xF000) >> 12];
  1248.  
  1249.                         $xf['borderLeftColor'] = ($border & 0x7F0000) >> 16;
  1250.                         $xf['borderRightColor'] = ($border & 0x3F800000) >> 23;
  1251.                         $border = (ord($data[$pos+18]) | ord($data[$pos+19]) << 8);
  1252.  
  1253.                         $xf['borderTopColor'] = ($border & 0x7F);
  1254.                         $xf['borderBottomColor'] = ($border & 0x3F80) >> 7;
  1255.  
  1256.                         if (array_key_exists($indexCode, $this->dateFormats)) {
  1257.                             $xf['type'] = 'date';
  1258.                             $xf['format'] = $this->dateFormats[$indexCode];
  1259.                             if ($align=='') { $xf['align'] = 'right'; }
  1260.                         }elseif (array_key_exists($indexCode, $this->numberFormats)) {
  1261.                             $xf['type'] = 'number';
  1262.                             $xf['format'] = $this->numberFormats[$indexCode];
  1263.                             if ($align=='') { $xf['align'] = 'right'; }
  1264.                         }else{
  1265.                             $isdate = FALSE;
  1266.                             $formatstr = '';
  1267.                             if ($indexCode > 0){
  1268.                                 if (isset($this->formatRecords[$indexCode]))
  1269.                                     $formatstr = $this->formatRecords[$indexCode];
  1270.                                 if ($formatstr!="") {
  1271.                                     $tmp = preg_replace("/\;.*/","",$formatstr);
  1272.                                     $tmp = preg_replace("/^\[[^\]]*\]/","",$tmp);
  1273.                                     if (preg_match("/[^hmsday\/\-:\s\\\,AMP]/i", $tmp) == 0) { // found day and time format
  1274.                                         $isdate = TRUE;
  1275.                                         $formatstr = $tmp;
  1276.                                         $formatstr = str_replace(array('AM/PM','mmmm','mmm'), array('a','F','M'), $formatstr);
  1277.                                         // m/mm are used for both minutes and months - oh SNAP!
  1278.                                         // This mess tries to fix for that.
  1279.                                         // 'm' == minutes only if following h/hh or preceding s/ss
  1280.                                         $formatstr = preg_replace("/(h:?)mm?/","$1i", $formatstr);
  1281.                                         $formatstr = preg_replace("/mm?(:?s)/","i$1", $formatstr);
  1282.                                         // A single 'm' = n in PHP
  1283.                                         $formatstr = preg_replace("/(^|[^m])m([^m]|$)/", '$1n$2', $formatstr);
  1284.                                         $formatstr = preg_replace("/(^|[^m])m([^m]|$)/", '$1n$2', $formatstr);
  1285.                                         // else it's months
  1286.                                         $formatstr = str_replace('mm', 'm', $formatstr);
  1287.                                         // Convert single 'd' to 'j'
  1288.                                         $formatstr = preg_replace("/(^|[^d])d([^d]|$)/", '$1j$2', $formatstr);
  1289.                                         $formatstr = str_replace(array('dddd','ddd','dd','yyyy','yy','hh','h'), array('l','D','d','Y','y','H','g'), $formatstr);
  1290.                                         $formatstr = preg_replace("/ss?/", 's', $formatstr);
  1291.                                     }
  1292.                                 }
  1293.                             }
  1294.                             if ($isdate){
  1295.                                 $xf['type'] = 'date';
  1296.                                 $xf['format'] = $formatstr;
  1297.                                 if ($align=='') { $xf['align'] = 'right'; }
  1298.                             }else{
  1299.                                 // If the format string has a 0 or # in it, we'll assume it's a number
  1300.                                 if (preg_match("/[0#]/", $formatstr)) {
  1301.                                     $xf['type'] = 'number';
  1302.                                     if ($align=='') { $xf['align']='right'; }
  1303.                                 }
  1304.                                 else {
  1305.                                 $xf['type'] = 'other';
  1306.                                 }
  1307.                                 $xf['format'] = $formatstr;
  1308.                                 $xf['code'] = $indexCode;
  1309.                             }
  1310.                         }
  1311.                         $this->xfRecords[] = $xf;
  1312.                     break;
  1313.                 case SPREADSHEET_EXCEL_READER_TYPE_NINETEENFOUR:
  1314.                     $this->nineteenFour = (ord($data[$pos+4]) == 1);
  1315.                     break;
  1316.                 case SPREADSHEET_EXCEL_READER_TYPE_BOUNDSHEET:
  1317.                         $rec_offset = $this->_GetInt4d($data, $pos+4);
  1318.                         $rec_typeFlag = ord($data[$pos+8]);
  1319.                         $rec_visibilityFlag = ord($data[$pos+9]);
  1320.                         $rec_length = ord($data[$pos+10]);
  1321.  
  1322.                         if ($version == SPREADSHEET_EXCEL_READER_BIFF8){
  1323.                             $chartype =  ord($data[$pos+11]);
  1324.                             if ($chartype == 0){
  1325.                                 $rec_name   = substr($data, $pos+12, $rec_length);
  1326.                             } else {
  1327.                                 $rec_name   = $this->_encodeUTF16(substr($data, $pos+12, $rec_length*2));
  1328.                             }
  1329.                         }elseif ($version == SPREADSHEET_EXCEL_READER_BIFF7){
  1330.                                 $rec_name   = substr($data, $pos+11, $rec_length);
  1331.                         }
  1332.                     $this->boundsheets[] = array('name'=>$rec_name,'offset'=>$rec_offset);
  1333.                     break;
  1334.  
  1335.             }
  1336.  
  1337.             $pos += $length + 4;
  1338.             $code = ord($data[$pos]) | ord($data[$pos+1])<<8;
  1339.             $length = ord($data[$pos+2]) | ord($data[$pos+3])<<8;
  1340.         }
  1341.  
  1342.         foreach ($this->boundsheets as $key=>$val){
  1343.             $this->sn = $key;
  1344.             $this->_parsesheet($val['offset']);
  1345.         }
  1346.         return true;
  1347.     }
  1348.  
  1349.     /**
  1350.      * Parse a worksheet
  1351.      */
  1352.     function _parsesheet($spos) {
  1353.         $cont = true;
  1354.         $data = $this->data;
  1355.         // read BOF
  1356.         $code = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1357.         $length = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1358.  
  1359.         $version = ord($data[$spos + 4]) | ord($data[$spos + 5])<<8;
  1360.         $substreamType = ord($data[$spos + 6]) | ord($data[$spos + 7])<<8;
  1361.  
  1362.         if (($version != SPREADSHEET_EXCEL_READER_BIFF8) && ($version != SPREADSHEET_EXCEL_READER_BIFF7)) {
  1363.             return -1;
  1364.         }
  1365.  
  1366.         if ($substreamType != SPREADSHEET_EXCEL_READER_WORKSHEET){
  1367.             return -2;
  1368.         }
  1369.         $spos += $length + 4;
  1370.         while($cont) {
  1371.             $lowcode = ord($data[$spos]);
  1372.             if ($lowcode == SPREADSHEET_EXCEL_READER_TYPE_EOF) break;
  1373.             $code = $lowcode | ord($data[$spos+1])<<8;
  1374.             $length = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1375.             $spos += 4;
  1376.             $this->sheets[$this->sn]['maxrow'] = $this->_rowoffset - 1;
  1377.             $this->sheets[$this->sn]['maxcol'] = $this->_coloffset - 1;
  1378.             unset($this->rectype);
  1379.             switch ($code) {
  1380.                 case SPREADSHEET_EXCEL_READER_TYPE_DIMENSION:
  1381.                     if (!isset($this->numRows)) {
  1382.                         if (($length == 10) ||  ($version == SPREADSHEET_EXCEL_READER_BIFF7)){
  1383.                             $this->sheets[$this->sn]['numRows'] = ord($data[$spos+2]) | ord($data[$spos+3]) << 8;
  1384.                             $this->sheets[$this->sn]['numCols'] = ord($data[$spos+6]) | ord($data[$spos+7]) << 8;
  1385.                         } else {
  1386.                             $this->sheets[$this->sn]['numRows'] = ord($data[$spos+4]) | ord($data[$spos+5]) << 8;
  1387.                             $this->sheets[$this->sn]['numCols'] = ord($data[$spos+10]) | ord($data[$spos+11]) << 8;
  1388.                         }
  1389.                     }
  1390.                     break;
  1391.                 case SPREADSHEET_EXCEL_READER_TYPE_MERGEDCELLS:
  1392.                     $cellRanges = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1393.                     for ($i = 0; $i < $cellRanges; $i++) {
  1394.                         $fr =  ord($data[$spos + 8*$i + 2]) | ord($data[$spos + 8*$i + 3])<<8;
  1395.                         $lr =  ord($data[$spos + 8*$i + 4]) | ord($data[$spos + 8*$i + 5])<<8;
  1396.                         $fc =  ord($data[$spos + 8*$i + 6]) | ord($data[$spos + 8*$i + 7])<<8;
  1397.                         $lc =  ord($data[$spos + 8*$i + 8]) | ord($data[$spos + 8*$i + 9])<<8;
  1398.                         if ($lr - $fr > 0) {
  1399.                             $this->sheets[$this->sn]['cellsInfo'][$fr+1][$fc+1]['rowspan'] = $lr - $fr + 1;
  1400.                         }
  1401.                         if ($lc - $fc > 0) {
  1402.                             $this->sheets[$this->sn]['cellsInfo'][$fr+1][$fc+1]['colspan'] = $lc - $fc + 1;
  1403.                         }
  1404.                     }
  1405.                     break;
  1406.                 case SPREADSHEET_EXCEL_READER_TYPE_RK:
  1407.                 case SPREADSHEET_EXCEL_READER_TYPE_RK2:
  1408.                     $row = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1409.                     $column = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1410.                     $rknum = $this->_GetInt4d($data, $spos + 6);
  1411.                     $numValue = $this->_GetIEEE754($rknum);
  1412.                     $info = $this->_getCellDetails($spos,$numValue,$column);
  1413.                     $this->addcell($row, $column, $info['string'],$info);
  1414.                     break;
  1415.                 case SPREADSHEET_EXCEL_READER_TYPE_LABELSST:
  1416.                     $row        = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1417.                     $column  = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1418.                     $xfindex    = ord($data[$spos+4]) | ord($data[$spos+5])<<8;
  1419.                     $index  = $this->_GetInt4d($data, $spos + 6);
  1420.                     $this->addcell($row, $column, $this->sst[$index], array('xfIndex'=>$xfindex) );
  1421.                     break;
  1422.                 case SPREADSHEET_EXCEL_READER_TYPE_MULRK:
  1423.                     $row        = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1424.                     $colFirst   = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1425.                     $colLast    = ord($data[$spos + $length - 2]) | ord($data[$spos + $length - 1])<<8;
  1426.                     $columns    = $colLast - $colFirst + 1;
  1427.                     $tmppos = $spos+4;
  1428.                     for ($i = 0; $i < $columns; $i++) {
  1429.                         $numValue = $this->_GetIEEE754($this->_GetInt4d($data, $tmppos + 2));
  1430.                         $info = $this->_getCellDetails($tmppos-4,$numValue,$colFirst + $i + 1);
  1431.                         $tmppos += 6;
  1432.                         $this->addcell($row, $colFirst + $i, $info['string'], $info);
  1433.                     }
  1434.                     break;
  1435.                 case SPREADSHEET_EXCEL_READER_TYPE_NUMBER:
  1436.                     $row    = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1437.                     $column = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1438.                     $tmp = unpack("ddouble", substr($data, $spos + 6, 8)); // It machine machine dependent
  1439.                     if ($this->isDate($spos)) {
  1440.                         $numValue = $tmp['double'];
  1441.                     }
  1442.                     else {
  1443.                         $numValue = $this->createNumber($spos);
  1444.                     }
  1445.                     $info = $this->_getCellDetails($spos,$numValue,$column);
  1446.                     $this->addcell($row, $column, $info['string'], $info);
  1447.                     break;
  1448.  
  1449.                 case SPREADSHEET_EXCEL_READER_TYPE_FORMULA:
  1450.                 case SPREADSHEET_EXCEL_READER_TYPE_FORMULA2:
  1451.                     $row    = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1452.                     $column = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1453.                     if ((ord($data[$spos+6])==0) && (ord($data[$spos+12])==255) && (ord($data[$spos+13])==255)) {
  1454.                         //String formula. Result follows in a STRING record
  1455.                         // This row/col are stored to be referenced in that record
  1456.                         // http://code.google.com/p/php-excel-reader/issues/detail?id=4
  1457.                         $previousRow = $row;
  1458.                         $previousCol = $column;
  1459.                     } elseif ((ord($data[$spos+6])==1) && (ord($data[$spos+12])==255) && (ord($data[$spos+13])==255)) {
  1460.                         //Boolean formula. Result is in +2; 0=false,1=true
  1461.                         // http://code.google.com/p/php-excel-reader/issues/detail?id=4
  1462.                         if (ord($this->data[$spos+8])==1) {
  1463.                             $this->addcell($row, $column, "TRUE");
  1464.                         } else {
  1465.                             $this->addcell($row, $column, "FALSE");
  1466.                         }
  1467.                     } elseif ((ord($data[$spos+6])==2) && (ord($data[$spos+12])==255) && (ord($data[$spos+13])==255)) {
  1468.                         //Error formula. Error code is in +2;
  1469.                     } elseif ((ord($data[$spos+6])==3) && (ord($data[$spos+12])==255) && (ord($data[$spos+13])==255)) {
  1470.                         //Formula result is a null string.
  1471.                         $this->addcell($row, $column, '');
  1472.                     } else {
  1473.                         // result is a number, so first 14 bytes are just like a _NUMBER record
  1474.                         $tmp = unpack("ddouble", substr($data, $spos + 6, 8)); // It machine machine dependent
  1475.                               if ($this->isDate($spos)) {
  1476.                                 $numValue = $tmp['double'];
  1477.                               }
  1478.                               else {
  1479.                                 $numValue = $this->createNumber($spos);
  1480.                               }
  1481.                         $info = $this->_getCellDetails($spos,$numValue,$column);
  1482.                         $this->addcell($row, $column, $info['string'], $info);
  1483.                     }
  1484.                     break;
  1485.                 case SPREADSHEET_EXCEL_READER_TYPE_BOOLERR:
  1486.                     $row    = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1487.                     $column = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1488.                     $string = ord($data[$spos+6]);
  1489.                     $this->addcell($row, $column, $string);
  1490.                     break;
  1491.                 case SPREADSHEET_EXCEL_READER_TYPE_STRING:
  1492.                     // http://code.google.com/p/php-excel-reader/issues/detail?id=4
  1493.                     if ($version == SPREADSHEET_EXCEL_READER_BIFF8){
  1494.                         // Unicode 16 string, like an SST record
  1495.                         $xpos = $spos;
  1496.                         $numChars =ord($data[$xpos]) | (ord($data[$xpos+1]) << 8);
  1497.                         $xpos += 2;
  1498.                         $optionFlags =ord($data[$xpos]);
  1499.                         $xpos++;
  1500.                         $asciiEncoding = (($optionFlags &0x01) == 0) ;
  1501.                         $extendedString = (($optionFlags & 0x04) != 0);
  1502.                         // See if string contains formatting information
  1503.                         $richString = (($optionFlags & 0x08) != 0);
  1504.                         if ($richString) {
  1505.                             // Read in the crun
  1506.                             $formattingRuns =ord($data[$xpos]) | (ord($data[$xpos+1]) << 8);
  1507.                             $xpos += 2;
  1508.                         }
  1509.                         if ($extendedString) {
  1510.                             // Read in cchExtRst
  1511.                             $extendedRunLength =$this->_GetInt4d($this->data, $xpos);
  1512.                             $xpos += 4;
  1513.                         }
  1514.                         $len = ($asciiEncoding)?$numChars : $numChars*2;
  1515.                         $retstr =substr($data, $xpos, $len);
  1516.                         $xpos += $len;
  1517.                         $retstr = ($asciiEncoding)? $retstr : $this->_encodeUTF16($retstr);
  1518.                     }
  1519.                     elseif ($version == SPREADSHEET_EXCEL_READER_BIFF7){
  1520.                         // Simple byte string
  1521.                         $xpos = $spos;
  1522.                         $numChars =ord($data[$xpos]) | (ord($data[$xpos+1]) << 8);
  1523.                         $xpos += 2;
  1524.                         $retstr =substr($data, $xpos, $numChars);
  1525.                     }
  1526.                     $this->addcell($previousRow, $previousCol, $retstr);
  1527.                     break;
  1528.                 case SPREADSHEET_EXCEL_READER_TYPE_ROW:
  1529.                     $row    = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1530.                     $rowInfo = ord($data[$spos + 6]) | ((ord($data[$spos+7]) << 8) & 0x7FFF);
  1531.                     if (($rowInfo & 0x8000) > 0) {
  1532.                         $rowHeight = -1;
  1533.                     } else {
  1534.                         $rowHeight = $rowInfo & 0x7FFF;
  1535.                     }
  1536.                     $rowHidden = (ord($data[$spos + 12]) & 0x20) >> 5;
  1537.                     $this->rowInfo[$this->sn][$row+1] = Array('height' => $rowHeight / 20, 'hidden'=>$rowHidden );
  1538.                     break;
  1539.                 case SPREADSHEET_EXCEL_READER_TYPE_DBCELL:
  1540.                     break;
  1541.                 case SPREADSHEET_EXCEL_READER_TYPE_MULBLANK:
  1542.                     $row = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1543.                     $column = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1544.                     $cols = ($length / 2) - 3;
  1545.                     for ($c = 0; $c < $cols; $c++) {
  1546.                         $xfindex = ord($data[$spos + 4 + ($c * 2)]) | ord($data[$spos + 5 + ($c * 2)])<<8;
  1547.                         $this->addcell($row, $column + $c, "", array('xfIndex'=>$xfindex));
  1548.                     }
  1549.                     break;
  1550.                 case SPREADSHEET_EXCEL_READER_TYPE_LABEL:
  1551.                     $row    = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1552.                     $column = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1553.                     $this->addcell($row, $column, substr($data, $spos + 8, ord($data[$spos + 6]) | ord($data[$spos + 7])<<8));
  1554.                     break;
  1555.                 case SPREADSHEET_EXCEL_READER_TYPE_EOF:
  1556.                     $cont = false;
  1557.                     break;
  1558.                 case SPREADSHEET_EXCEL_READER_TYPE_HYPER:
  1559.                     //  Only handle hyperlinks to a URL
  1560.                     $row    = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8;
  1561.                     $row2   = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8;
  1562.                     $column = ord($this->data[$spos+4]) | ord($this->data[$spos+5])<<8;
  1563.                     $column2 = ord($this->data[$spos+6]) | ord($this->data[$spos+7])<<8;
  1564.                     $linkdata = Array();
  1565.                     $flags = ord($this->data[$spos + 28]);
  1566.                     $udesc = "";
  1567.                     $ulink = "";
  1568.                     $uloc = 32;
  1569.                     $linkdata['flags'] = $flags;
  1570.                     if (($flags & 1) > 0 ) {   // is a type we understand
  1571.                         //  is there a description ?
  1572.                         if (($flags & 0x14) == 0x14 ) {   // has a description
  1573.                             $uloc += 4;
  1574.                             $descLen = ord($this->data[$spos + 32]) | ord($this->data[$spos + 33]) << 8;
  1575.                             $udesc = substr($this->data, $spos + $uloc, $descLen * 2);
  1576.                             $uloc += 2 * $descLen;
  1577.                         }
  1578.                         $ulink = $this->read16bitstring($this->data, $spos + $uloc + 20);
  1579.                         if ($udesc == "") {
  1580.                             $udesc = $ulink;
  1581.                         }
  1582.                     }
  1583.                     $linkdata['desc'] = $udesc;
  1584.                     $linkdata['link'] = $this->_encodeUTF16($ulink);
  1585.                     for ($r=$row; $r<=$row2; $r++) {
  1586.                         for ($c=$column; $c<=$column2; $c++) {
  1587.                             $this->sheets[$this->sn]['cellsInfo'][$r+1][$c+1]['hyperlink'] = $linkdata;
  1588.                         }
  1589.                     }
  1590.                     break;
  1591.                 case SPREADSHEET_EXCEL_READER_TYPE_DEFCOLWIDTH:
  1592.                     $this->defaultColWidth  = ord($data[$spos+4]) | ord($data[$spos+5]) << 8;
  1593.                     break;
  1594.                 case SPREADSHEET_EXCEL_READER_TYPE_STANDARDWIDTH:
  1595.                     $this->standardColWidth  = ord($data[$spos+4]) | ord($data[$spos+5]) << 8;
  1596.                     break;
  1597.                 case SPREADSHEET_EXCEL_READER_TYPE_COLINFO:
  1598.                     $colfrom = ord($data[$spos+0]) | ord($data[$spos+1]) << 8;
  1599.                     $colto = ord($data[$spos+2]) | ord($data[$spos+3]) << 8;
  1600.                     $cw = ord($data[$spos+4]) | ord($data[$spos+5]) << 8;
  1601.                     $cxf = ord($data[$spos+6]) | ord($data[$spos+7]) << 8;
  1602.                     $co = ord($data[$spos+8]);
  1603.                     for ($coli = $colfrom; $coli <= $colto; $coli++) {
  1604.                         $this->colInfo[$this->sn][$coli+1] = Array('width' => $cw, 'xf' => $cxf, 'hidden' => ($co & 0x01), 'collapsed' => ($co & 0x1000) >> 12);
  1605.                     }
  1606.                     break;
  1607.  
  1608.                 default:
  1609.                     break;
  1610.             }
  1611.             $spos += $length;
  1612.         }
  1613.  
  1614.         if (!isset($this->sheets[$this->sn]['numRows']))
  1615.              $this->sheets[$this->sn]['numRows'] = $this->sheets[$this->sn]['maxrow'];
  1616.         if (!isset($this->sheets[$this->sn]['numCols']))
  1617.              $this->sheets[$this->sn]['numCols'] = $this->sheets[$this->sn]['maxcol'];
  1618.         }
  1619.  
  1620.         function isDate($spos) {
  1621.             $xfindex = ord($this->data[$spos+4]) | ord($this->data[$spos+5]) << 8;
  1622.             return ($this->xfRecords[$xfindex]['type'] == 'date');
  1623.         }
  1624.  
  1625.         // Get the details for a particular cell
  1626.         function _getCellDetails($spos,$numValue,$column) {
  1627.             $xfindex = ord($this->data[$spos+4]) | ord($this->data[$spos+5]) << 8;
  1628.             $xfrecord = $this->xfRecords[$xfindex];
  1629.             $type = $xfrecord['type'];
  1630.  
  1631.             $format = $xfrecord['format'];
  1632.             $formatIndex = $xfrecord['formatIndex'];
  1633.             $fontIndex = $xfrecord['fontIndex'];
  1634.             $formatColor = "";
  1635.             $rectype = '';
  1636.             $string = '';
  1637.             $raw = '';
  1638.  
  1639.             if (isset($this->_columnsFormat[$column + 1])){
  1640.                 $format = $this->_columnsFormat[$column + 1];
  1641.             }
  1642.  
  1643.             if ($type == 'date') {
  1644.                 // See http://groups.google.com/group/php-excel-reader-discuss/browse_frm/thread/9c3f9790d12d8e10/f2045c2369ac79de
  1645.                 $rectype = 'date';
  1646.                 // Convert numeric value into a date
  1647.                 $utcDays = floor($numValue - ($this->nineteenFour ? SPREADSHEET_EXCEL_READER_UTCOFFSETDAYS1904 : SPREADSHEET_EXCEL_READER_UTCOFFSETDAYS));
  1648.                 $utcValue = ($utcDays) * SPREADSHEET_EXCEL_READER_MSINADAY;
  1649.                 $dateinfo = gmgetdate($utcValue);
  1650.  
  1651.                 $raw = $numValue;
  1652.                 $fractionalDay = $numValue - floor($numValue) + .0000001; // The .0000001 is to fix for php/excel fractional diffs
  1653.  
  1654.                 $totalseconds = floor(SPREADSHEET_EXCEL_READER_MSINADAY * $fractionalDay);
  1655.                 $secs = $totalseconds % 60;
  1656.                 $totalseconds -= $secs;
  1657.                 $hours = floor($totalseconds / (60 * 60));
  1658.                 $mins = floor($totalseconds / 60) % 60;
  1659.                 $string = date ($format, mktime($hours, $mins, $secs, $dateinfo["mon"], $dateinfo["mday"], $dateinfo["year"]));
  1660.             } else if ($type == 'number') {
  1661.                 $rectype = 'number';
  1662.                 $formatted = $this->_format_value($format, $numValue, $formatIndex);
  1663.                 $string = $formatted['string'];
  1664.                 $formatColor = $formatted['formatColor'];
  1665.                 $raw = $numValue;
  1666.             } else{
  1667.                 if ($format=="") {
  1668.                     $format = $this->_defaultFormat;
  1669.                 }
  1670.                 $rectype = 'unknown';
  1671.                 $formatted = $this->_format_value($format, $numValue, $formatIndex);
  1672.                 $string = $formatted['string'];
  1673.                 $formatColor = $formatted['formatColor'];
  1674.                 $raw = $numValue;
  1675.             }
  1676.  
  1677.             return array(
  1678.                 'string'=>$string,
  1679.                 'raw'=>$raw,
  1680.                 'rectype'=>$rectype,
  1681.                 'format'=>$format,
  1682.                 'formatIndex'=>$formatIndex,
  1683.                 'fontIndex'=>$fontIndex,
  1684.                 'formatColor'=>$formatColor,
  1685.                 'xfIndex'=>$xfindex
  1686.             );
  1687.  
  1688.         }
  1689.  
  1690.  
  1691.     function createNumber($spos) {
  1692.         $rknumhigh = $this->_GetInt4d($this->data, $spos + 10);
  1693.         $rknumlow = $this->_GetInt4d($this->data, $spos + 6);
  1694.         $sign = ($rknumhigh & 0x80000000) >> 31;
  1695.         $exp =  ($rknumhigh & 0x7ff00000) >> 20;
  1696.         $mantissa = (0x100000 | ($rknumhigh & 0x000fffff));
  1697.         $mantissalow1 = ($rknumlow & 0x80000000) >> 31;
  1698.         $mantissalow2 = ($rknumlow & 0x7fffffff);
  1699.         $value = $mantissa / pow( 2 , (20- ($exp - 1023)));
  1700.         if ($mantissalow1 != 0) $value += 1 / pow (2 , (21 - ($exp - 1023)));
  1701.         $value += $mantissalow2 / pow (2 , (52 - ($exp - 1023)));
  1702.         if ($sign) {$value = -1 * $value;}
  1703.         return  $value;
  1704.     }
  1705.  
  1706.     function addcell($row, $col, $string, $info=null) {
  1707.         $this->sheets[$this->sn]['maxrow'] = max($this->sheets[$this->sn]['maxrow'], $row + $this->_rowoffset);
  1708.         $this->sheets[$this->sn]['maxcol'] = max($this->sheets[$this->sn]['maxcol'], $col + $this->_coloffset);
  1709.         $this->sheets[$this->sn]['cells'][$row + $this->_rowoffset][$col + $this->_coloffset] = $string;
  1710.         if ($this->store_extended_info && $info) {
  1711.             foreach ($info as $key=>$val) {
  1712.                 $this->sheets[$this->sn]['cellsInfo'][$row + $this->_rowoffset][$col + $this->_coloffset][$key] = $val;
  1713.             }
  1714.         }
  1715.     }
  1716.  
  1717.  
  1718.     function _GetIEEE754($rknum) {
  1719.         if (($rknum & 0x02) != 0) {
  1720.                 $value = $rknum >> 2;
  1721.         } else {
  1722.             //mmp
  1723.             // I got my info on IEEE754 encoding from
  1724.             // http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html
  1725.             // The RK format calls for using only the most significant 30 bits of the
  1726.             // 64 bit floating point value. The other 34 bits are assumed to be 0
  1727.             // So, we use the upper 30 bits of $rknum as follows...
  1728.             $sign = ($rknum & 0x80000000) >> 31;
  1729.             $exp = ($rknum & 0x7ff00000) >> 20;
  1730.             $mantissa = (0x100000 | ($rknum & 0x000ffffc));
  1731.             $value = $mantissa / pow( 2 , (20- ($exp - 1023)));
  1732.             if ($sign) {
  1733.                 $value = -1 * $value;
  1734.             }
  1735.             //end of changes by mmp
  1736.         }
  1737.         if (($rknum & 0x01) != 0) {
  1738.             $value /= 100;
  1739.         }
  1740.         return $value;
  1741.     }
  1742.  
  1743.     function _encodeUTF16($string) {
  1744.         $result = $string;
  1745.         if ($this->_defaultEncoding){
  1746.             switch ($this->_encoderFunction){
  1747.                 case 'iconv' :   $result = iconv('UTF-16LE', $this->_defaultEncoding, $string);
  1748.                                 break;
  1749.                 case 'mb_convert_encoding' :     $result = mb_convert_encoding($string, $this->_defaultEncoding, 'UTF-16LE' );
  1750.                                 break;
  1751.             }
  1752.         }
  1753.         return $result;
  1754.     }
  1755.  
  1756.     function _GetInt4d($data, $pos) {
  1757.         $value = ord($data[$pos]) | (ord($data[$pos+1]) << 8) | (ord($data[$pos+2]) << 16) | (ord($data[$pos+3]) << 24);
  1758.         if ($value>=4294967294) {
  1759.             $value=-2;
  1760.         }
  1761.         return $value;
  1762.     }
  1763.  
  1764. }
  1765.  
  1766. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement