Advertisement
LayZee

PHP Excel Reader PHP5 fix

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