?
Define (' Abc_critical ', 0);
Define (' Abc_error ', 1);
Define (' Abc_alert ', 2);
Define (' Abc_warning ', 3);
Define (' Abc_notice ', 4);
Define (' Abc_info ', 5);
Define (' Abc_debug ', 6);
Define (' Abc_trace ', 7);
Define (' Abc_var_dump ', 8);
Define (' Abc_no_log ',-1);
$php _version = Split ("\.", Phpversion ());
if ($php _version[0] = = 4 && $php _version[1] <= 1) {
if (!function_exists (' Var_export ')) {
function Var_export ($exp, $ret) {
Ob_start ();
Var_dump ($EXP);
$result = Ob_get_contents ();
Ob_end_clean ();
return $result;
}}}function PRINT_BT ()
{
print "<code>\n";
$cs = Debug_backtrace ();
for ($i = 1; $i < count ($cs); $i + +)
{
$item = $cs [$i];
for ($j = 0; $j < count ($item [' args ']); $j + +)
if (is_string ($item [' args '] [$j])
$item [' args '] [$j] = "". $item [' args '] [$j]. "\"";
$args = Join (",", $item [' args ']);
if (Isset ($item [' class '])
$str = sprintf ('%s (%d):%s%s%s (%s) ')
$item [' File '],
$item [' line '],
$item [' class '],
$item [' type '],
$item [' function '],
$args);
Else
$str = sprintf ("%s" (%d):%s (%s))
$item [' File '],
$item [' line '],
$item [' function '],
$args);
Echo $str. "<br>\n";
}print "</code>\n";
}function _die ($STR)
{
Print "Script died with reason: $str <br>\n";
PRINT_BT ();
Exit ();
}class Debugout
{
var $priorities = array (abc_critical => ' CRITICAL '),
Abc_error => ' ERROR ',
Abc_alert => ' ALERT ',
Abc_warning => ' WARNING ',
Abc_notice => ' NOTICE ',
Abc_info => ' INFO ',
Abc_debug => ' DEBUG ',
Abc_trace => ' TRACE ',
Abc_var_dump => ' DUMP '
);
var $_ready = false;
var $_currentpriority = Abc_debug;
var $_consumers = array ();
var $_filename;
var $_fp;
var $_logger_name;
function Debugout ($name, $logger _name, $level) {
$this->_filename = $name;
$this->_currentpriority = $level;
$this->_logger_name = $logger _name;
if ($level > Abc_no_log) {
$this->_openfile ();
}
Register_shutdown_function (Array ($this, "close"));
} function log ($message, $priority = abc_info) {
if ($priority > $this->_currentpriority) {
return false;
}
return $this->_writeline ($message, $priority, strftime ('%b%d%h:%m:%s '));
function dump ($variable, $name) {
$priority = Abc_var_dump;
if ($priority > $this->_currentpriority) {
return false;
} $time = Strftime ('%b%d%h:%m:%s ');
$message = Var_export ($variable, true);
Return fwrite ($this->_fp,
sprintf ('%s%s [%s] variable%s =%s \ r \ n ',
$time,
$this->_logger_name,
$this->priorities[$priority],
$name,
$message)
);
} function info ($message) {
return $this->log ($message, abc_info);
} function Debug ($message) {
return $this->log ($message, abc_debug);
} function Notice ($message) {
return $this->log ($message, Abc_notice);
} function Warning ($message) {
return $this->log ($message, abc_warning);
function Trace ($message) {
return $this->log ($message, abc_trace);
Function error ($message) {
return $this->log ($message, abc_error);
}
function _writeline ($message, $priority, $time) {
if (fwrite ($this->_fp, sprintf ("%s [%s]%s\r\n", $time, $this->_logger_name, $this->priorities[$priority], $message))) {
Return Fflush ($this->_fp);
} else {
return false;
}} function _openfile () {
if ($this->_fp = @fopen ($this->_filename, ' a ') = = False) {
return false;
return true;
function Close () {
if ($this->_currentpriority!= abc_no_log) {
$this->info ("Logger stoped");
Return fclose ($this->_fp);
} }
function Factory ($name, $logger _name, $level) {
$instance = new Debugout ($name, $logger _name, $level);
return $instance;
function &getwritersingleton ($name, $logger _name, $level = abc_debug) {
Static $instances;
if (!isset ($instances)) {
$instances = Array ();
} $signature = Serialize (Array ($name, $level));
if (!isset ($instances [$signature])) {
$instances [$signature] = Debugout::factory ($name, $logger _name, $level);
}
return $instances [$signature];
function Attach (& $logObserver) {
if (!is_object ($logObserver)) {
return false;
} $logObserver->_listenerid = Uniqid (rand ());
$this->_listeners[$logObserver->_listenerid] = & $logObserver;
}}define (' Abc_bad_date ',-1);
Class exceldateutil{
function Xls2tstamp ($date) {
$date = $date >25568 $date: 25569;
$ofs = (70 * 365 + 17+2) * 86400;
Return ($date * 86400)-$ofs;
}function Getdatearray ($xls _date) {
$ret = Array ();
if ($xls _date = = 60) {
$ret [' Day '] = 29;
$ret [' month '] = 2;
$ret [' year '] = 1900;
return $ret;
else if ($xls _date < 60) {
$xls _date++;
}
$l = $xls _date + 68569 + 2415019;
$n = (int) ((4 * $l)/146097);
$l = $l-(int) ((146097 * $n + 3)/4);
$i = (int) ((4000 * ($l + 1))/1461001);
$l = $l-(int) ((1461 * $i)/4) + 31;
$j = (int) (($l)/2447);
$ret [' Day '] = $l-(int) ((2447 * $j)/80);
$l = (int) ($J/11);
$ret [' month '] = $j + 2-(A * $l);
$ret [' year '] = + * ($n -49) + $i + $l;
return $ret;
}function Isinternaldateformat ($format) {
$retval =false;
Switch (format) {
Case 0X0E:
Case 0X0F:
Case 0X10:
Case 0X11:
Case 0x12:
Case 0X13:
Case 0X14:
Case 0X15:
Case 0x16:
Case 0X2D:
Case 0X2E:
Case 0X2F:
Case 0XA4:
Case 0XA5:
Case 0XA6:
Case 0XA7:
Case 0XA8:
Case 0XA9:
Case 0XAA:
Case 0XAB:
Case 0XAC:
Case 0XAD:
$retval = true; Break
Default: $retval = false; Break
return $retval;
}}define (' Excel_font_rid ', 0x31);
Define (' Xf_script_none ', 0);
Define (' Xf_script_superscript ', 1);
Define (' Xf_script_subscript ', 2);
Define (' Xf_underline_none ', 0x0);
Define (' Xf_underline_single ', 0x1);
Define (' xf_underline_double ', 0x2);
Define (' xf_underline_single_accounting ', 0x3);
Define (' xf_underline_double_accounting ', 0x4);
Define (' Xf_style_italic ', 0x2);
Define (' Xf_style_strikeout ', 0x8);
Define (' Xf_boldness_regular ', 0x190);
Define (' Xf_boldness_bold ', 0X2BC);
Class Excelfont {
function Basicfontrecord () {
Return Array (' Size ' => 10,
' Script ' => Xf_script_none,
' Undeline ' => xf_underline_none,
' Italic ' => false,
' Strikeout ' => false,
' Bold ' => false,
' Boldness ' => xf_boldness_regular,
' Palete ' => 0,
' Name ' => ' Arial ');
Function Getfontrecord (& $WB, $ptr) {
$retval = Array (' size ' => 0,
' Script ' => Xf_script_none,
' Undeline ' => xf_underline_none,
' Italic ' => false,
' Strikeout ' => false,
' Bold ' => false,
' Boldness ' => xf_boldness_regular,
' Palete ' => 0,
' Name ' => ');
$retval [' size '] = (ord ($WB [$ptr]) + 256*ord ($WB [$ptr +1]))/20;
$style =ord ($WB [$ptr +2]);
if ($style & Xf_style_italic)!= 0) {
$retval [' italic '] = true;
} if (($style & Xf_style_strikeout)!= 0) {
$retval [' strikeout '] = true;
$retval [' palete '] = Ord ($WB [$ptr +4]) +256*ord ($WB [$ptr +5]);
$retval [' boldness '] = Ord ($WB [$ptr +6]) +256*ord ($WB [$ptr +7]);
$retval [' bold '] = $retval [' boldness '] = = Xf_boldness_regular? False:true;
$retval [' script '] = Ord ($WB [$ptr +8]) +256*ord ($WB [$ptr +9]);
$retval [' underline '] = Ord ($WB [$ptr +10]);
$length = Ord ($WB [$ptr +14]);
if ($length >0) {
if (Ord ($WB [$ptr +15]) = = 0) {
$retval [' name '] = substr ($WB, $ptr +16, $length);
} else {
$retval [' name '] = excelfont::getunicodestring ($WB, $ptr +15, $length);
} return $retval;
Function toString (& $record, $index) {
$retval = sprintf ("Font Index =%d \nfont Size =%d\nitalic =%s\nstrikeoout=%s\npalete=%s\nboldness =%s bold=%s\n Script =%d\n underline =%d\n fontname=%s<hr> ",
$index,
$record [' Size '],
$record [' italic '] = = true? True ': ' false ',
$record [' strikeout '] = = true? True ': ' false ',
$record [' Palete '],
$record [' Boldness '],
$record [' bold '] = = true? True ': ' false ',
$record [' Script '],
$record [' Underline '],
$record [' name ']
);
return $retval;
Function getunicodestring (& $string, $offset, $length) {
$bstring = "";
$index = $offset + 1;
for ($k = 0; $k < $length; $k + +)
{
$bstring = $bstring. $string [$index];
$index + 2;
Return substr ($bstring, 0, $length);
function Exceltocss ($rec, $app _font=true, $app _size=true, $app _italic=true, $app _bold=true) {
$ret = "";
if ($app _font==true) {
$ret = $ret. " Font-family: ". $rec [' name ']."; ";
} if ($app _size==true) {
$ret = $ret. " Font-size: ". $rec [' Size ']." Pt ";
} if ($app _bold==true) {
if ($rec [' bold ']==true] {
$ret = $ret. " Font-weight:bold; ";
} else {
$ret = $ret. " Font-weight:normal; ";
} if ($app _italic==true) {
if ($rec [' Italic ']==true) {
$ret = $ret. " Font-style:italic; ";
} else {
$ret = $ret. " Font-style:normal; ";
} return $ret;
}}define (dp_empty, 0);
Define (Dp_string_source, 1);
Define (Dp_file_source, 2);
Class Excelparserutil
{
function Str2long ($STR) {
Return ord ($str [0]) + 256* (ord ($str [1]) +
256* (Ord ($str [2]) + 256* (Ord ($str [3])));
}}
Class Dataprovider
{
function Dataprovider ($data, $dataType)
{
Switch ($dataType)
{
Case Dp_file_source:
if (! ($this->_data = @fopen ($data, "RB"))
Return
$this->_size = @filesize ($data);
if (! $this->_size)
_die ("Failed to determine file size.");
Break
Case Dp_string_source:
$this->_data = $data;
$this->_size = strlen ($data);
Break
Default
_die ("Invalid data type provided.");
} $this->_type = $dataType;
Register_shutdown_function (Array ($this, "close"));
}function get ($offset, $length)
{
if (! $this->isvalid ())
_die ("Data provider is empty.");
if ($this->_baseofs + $offset + $length > $this->_size)
_die ("Invalid offset/length.");
Switch ($this->_type)
{
Case Dp_file_source:
{
if (@fseek ($this->_data, $this->_baseofs + $offset, seek_set) = = 1)
_die ("Failed to seek file position specified by Offest.");
Return @fread ($this->_data, $length);
}case Dp_string_source:
{
$RC = substr ($this->_data, $this->_baseofs + $offset, $length);
return $RC;
}default:
_die ("Invalid data type or class is not initialized.");
}}function GetByte ($offset)
{
return $this->get ($offset, 1);
}function Getord ($offset)
{
Return Ord ($this->getbyte ($offset));
}function Getlong ($offset)
{
$str = $this->get ($offset, 4);
Return Excelparserutil::str2long ($STR);
}function GetSize ()
{
if (! $this->isvalid ())
_die ("Data provider is empty.");
return $this->_size;
}function Getblocks ()
{
if (! $this->isvalid ())
_die ("Data provider is empty.");
return (int) ($this->_size-1)/0x200)-1;
}function Readfromfat ($chain, $gran = 0x200)
{
$RC = ';
for ($i = 0; $i < count ($chain); $i + +)
$RC. = $this->get ($chain [$i] * $gran, $gran);
return $RC;
}function Close ()
{
Switch ($this->_type)
{
Case Dp_file_source:
@fclose ($this->_data);
Case Dp_string_source:
$this->_data = null;
Default
$_type = Dp_empty;
Break
}}function IsValid ()
{
return $this->_type!= dp_empty;
}var $_type = Dp_empty;
var $_data = null;
var $_size =-1;
var $_baseofs = 0;
}class Excelfileparser {
var $DP = null;
var $max _blocks;
var $max _sblocks;
var $fat;
var $sfat;
var $formats;
var $xf;
var $fonts;
var $dbglog;
function Excelfileparser ($logfile = "", $level =abc_no_log) {
$this->dbglog = &debugout::getwritersingleton ($logfile, "", $level);
$this->dbglog->info ("Logger started");
}function Populateformat () {
$this->dbglog->trace ("Populateformat () function call");
$ret = Array (
0=> "General",
1=> "0",
2=> "0.00",
3=> "#,# #0",
4=> "#,# #0.00",
5=> "($#,# #0_);($#,# #0)",
6=> "($#,# #0_); [Red] ($#,# #0) ",
7=> "($#,# #0);($#,# #0.00)",
8=> "($#,# #0.00_); [Red] ($#,# #0.00) ",
9=> "0%",
0xa=> "0%",
0xb=> "0.00E+00",
0xc=> "#?/?",
0xd=> "#?? /??",
0xe=> "M/d/yy",
0xf=> "D-mmm-yy",
0x10=> "D-mmm",
0x11=> "Mmm-yy",
0x12=> "h:mm am/pm",
0x13=> "H:mm:ss am/pm",
0x14=> "h:mm",
0x15=> "H:mm:ss",
0x16=> "M/d/yy h:mm",
0x17=> "0x17",
0x18=> "0x18",
0x19=> "0x19",
0x1a=> "0x1a",
0x1b=> "0x1b",
0x1c=> "0x1c",
0x1d=> "0x1d",
0x1e=> "0x1e",
0x1f=> "0x1f",
0x20=> "0x20",
0x21=> "0x21",
0x22=> "0x22",
0x23=> "0x23",
0x24=> "0x24",
0x25=> "(#,# #0_);(#,# #0)",
0x26=> "(#,# #0_); [Red] (#,# #0) ",
0x27=> "(#,# #0.00_);(#,# #0.00)",
0x28=> "(#,# #0.00_); [Red] (#,# #0.00) ",
0x29=> "_ (*#,# #0_); _ (* (#,# #0); _ (* \"-\ "_); _ (@_)",
0x2a=> "_ ($*#,# #0_); _ ($* (#,# #0); _ ($* \"-\ "_); _ (@_)",
0x2b=> "_ (*#,# #0.00_); _ (* (#,# #0.00); _ (*\"-\ "??) _);_(@_)",
0x2c=> "_ ($*#,# #0.00_); _ ($* (#,# #0.00); _ ($*\"-\ "?? _);_(@_)",
0x2d=> "Mm:ss",
0x2e=> "[H]:mm:ss",
0x2f=> "mm:ss.0",
0x30=> "# #0.0E+0",
0x31=> "@");
$this->dbglog->dump ($ret, "$ret");
$this->dbglog->trace ("Populateformat () function return");
return $ret;
}function Xls2tstamp ($date) {
$date = $date >25568 $date: 25569;
$ofs = (70 * 365 + 17+2) * 86400;
Return ($date * 86400)-$ofs;
}function Getdatearray ($date) {
Return Exceldateutil::getdatearray ($date);
}function Isdateformat ($val) {
$f _i= $this->xf[' format ' [$val];
if (Preg_match ("/[m|d|y]/i", $this->format[$f _i])!=0) {
if (Strrpos ($this->format[$f _i], ' [')!=false) {
$tmp = Preg_replace ("/(\[\/?) (\w+) ([^\]]*\])/"," ' \\1 '. '. ' \\3 ' ", $this->format[$f _i]);
if (Preg_match ("/[m|d|y]/i", $tmp)!=0)
return TRUE;
Else
return FALSE;
} else {
return TRUE;
} else
return FALSE;
}function getunicodestring ($str, $ofs) {
$size = 0;
$i _ofs=0;
Return substr ($str, $ofs + $i _ofs+1, $size);
}function getbytestring ($str, $ofs) {
$size = 0;
$i _ofs=0;
$size =ord ($str [$ofs]);
$i _ofs=1;
}
function Get_blocks_chain ($start, $small _fat=false) {
$this->dbglog->trace ("Get_blocks_chain () Var_export ($start, True).", ". Var_export ($small _fat,true).") function call ");
$chain = Array ();
$next _block = $start;
if (! $small _fat) {
while (($next _block!=0xfffffffe) &&
($next _block <= $this->max_blocks) &&
($next _block < count ($this->fat)))
{
$chain [] = $next _block;
$next _block = $this->fat[$next _block];
}} else {
while (($next _block!=0xfffffffe) &&
($next _block <= $this->max_sblocks) &&
($next _block < count ($this->sfat)))
{
$chain [] = $next _block;
$next _block = $this->sfat[$next _block];
}}if ($next _block!= 0xFFFFFFFE)
return false;
$this->dbglog->dump ($chain, "$chain");
$this->dbglog->trace ("Get_blocks_chain () function return");
return $chain;
}
function Find_stream ($dir, $item _name, $item _num=0) {
$this->dbglog->trace ("Find_stream () Var_export ($dir, True).", ". Var_export ($item _name,true).", ". Var_export ($item _num,true). ") function call ");
$dt = $dir->getord ($item _num * 0x80 + 0x42);
$prev = $dir->getlong ($item _num * 0x80 + 0x44);
$next = $dir->getlong ($item _num * 0x80 + 0x48);
$dir _ = $dir->getlong ($item _num * 0x80 + 0x4c);
$curr _name = ';
if (($dt ==2) | | ($dt ==5))
for ($i = 0;
$i < ($dir->getord ($item _num * 0x80 + 0x40) +
256 * $dir->getord ($item _num * 0x80 + 0x41))/2-1;
$i + +)
$curr _name. = $dir->getbyte ($item _num * 0x80 + $i * 2);
if (($dt ==2) | | ($dt ==5)) && (strcmp ($curr _name, $item _name) ==0)) {
$this->dbglog->trace ("Find_stream () function return with". Var_export ($item _num,true));
return $item _num;
}if ($prev!= 0xFFFFFFFF) {
$i = $this->find_stream ($dir, $item _name, $prev);
if ($i >=0) {
$this->dbglog->trace ("Find_stream () function return with". Var_export ($i, true));
return $i;
}}if ($next!= 0xFFFFFFFF) {
$i = $this->find_stream ($dir, $item _name, $next);
if ($i >=0) {
$this->dbglog->trace ("Find_stream () function return with". Var_export ($i, true));
return $i;
}}if ($dir _!= 0xFFFFFFFF) {
$i = $this->find_stream ($dir, $item _name, $dir _);
if ($i >=0) {
$this->dbglog->trace ("Find_stream () function return with". Var_export ($i, true));
return $i;
}} $this->dbglog->trace ("Find_stream () function return with-1");
return-1;
}function Rk_decode ($RK) {
$this->dbglog->trace ("Rk_decode var_export ($rk, True).") function call ");
$res = Array ();
if ($rk & 2) {
$val = ($rk & 0XFFFFFFFC) >> 2;
if ($rk & 1) $val = $val/100;
if (((float) $val) = = Floor ((float) $val)) {
$res [' val '] = (int) $val;
$res [' type '] = 1;
} else {
$res [' val '] = (float) $val;
$res [' type '] = 2;
}} else {
$res [' type '] = 2;
$FRK = $rk;
$fexp = (($frk & 0x7ff00000) >> 20)-1023;
$val = 1+ ($FRK & 0X000FFFFF) >> 2)/262144;
if ($fexp > 0) {
for ($i =0; $i < $fexp; $i + +)
$val *= 2;
} else {
if ($fexp ==-1023) {
$val = 0;
} else {
for ($i =0; $i <abs ($FEXP); $i + +)
$val/= 2;
}}if ($rk & 1) $val = $val/100;
if ($rk & 0x80000000) $val =-$val;
$res [' val '] = (float) $val;
} $this->dbglog->trace ("Rk_decode () function returns");
return $res;
}
function Parse_worksheet ($WS) {
$this->dbglog->debug ("Parse_worksheet (DATA) function");
if (strlen ($WS) <= 0) {
$this->dbglog->trace ("Parse_worksheet () function returns 7 (Data not Found)");
return 7;
}if (strlen ($WS) < 4) {
$this->dbglog->trace ("Parse_worksheet () function returns 6 (File corrupted)");
return 6;
}
if (strlen ($WS) < 256*ord ($ws [3]) +ord ($ws [2])) return 6;
if (Ord ($ws [0])!= 0x09) return 6;
$vers = Ord ($ws [1]);
if (($vers!=0) && ($vers!=2) && ($vers!=4) && ($vers!=8))
return 8;
if ($vers!=8) {
$biff _ver = ($ver +4)/2;
} else {
if (strlen ($WS) <) return 6;
Switch (ORD ($WS [4]) +256*ord ($ws [5])) {
Case 0X0500:
if (Ord ($ws [0x0a]) +256*ord ($ws [0x0b]) < 1994) {
$biff _ver = 5;
} else {
Switch (ORD ($WS [8]) +256*ord ($ws [9])) {
Case 2412:
Case 3218:
Case 3321:
$this->dbglog->debug ("Parsed BIFF version is 5");
$biff _ver = 5;
Break
Default
$this->dbglog->debug ("parsed BIFF version is 7");
$biff _ver = 7;
Break
}}break;
Case 0X0600:
$this->dbglog->debug ("parsed BIFF version is 8");
$biff _ver = 8;
Break
Default
return 8;
}}if ($biff _ver < 5) {
$this->dbglog->debug ("Parse_worksheet () function found ($biff _ver < 5) return 8");
return 8;
} $ptr = 0;
$data = Array (' biff_version ' => $biff _ver);
while ((Ord ($ws [$ptr])!=0x0a) && ($ptr <strlen ($WS))) {
Switch (ORD ($ws [$ptr]) +256*ord ($ws [$ptr +1])) {
Case 0x0203:
$this->dbglog->trace ("found number");
if (($biff _ver < 3)) {
$this->dbglog->trace ("$biff _ver < 3 break;");
Break
}if (Ord ($ws [$ptr +2]) +256*ord ($ws [$ptr +3])) < 14) {
$this->dbglog->debug ("Parse_worksheet () return 6");
return 6;
$row = Ord ($ws [$ptr +4]) +256*ord ($ws [$ptr +5]);
$col = Ord ($ws [$ptr +6]) +256*ord ($ws [$ptr +7]);
$num _lo = Excelparserutil::str2long (substr ($ws, $ptr +10,4));
$num _hi = Excelparserutil::str2long (substr ($ws, $ptr +14,4));
$XF _i = Ord ($ws [$ptr +8]) +256*ord ($ws [$ptr +9]);
if ($this->isdateformat ($xf _i)) {
$data [' cell '] [$row] [$col] [' type '] = 3;
} else {
$data [' cell '] [$row] [$col] [' type '] = 2;
$fonti = $this->xf[' font ' [$xf _i];
$data [' cell '] [$row] [$FC + $i] [' font '] = $fonti;
$fexp = (($num _hi & 0x7ff00000) >> 20)-1023;
$val = 1+ ($num _hi & 0x000fffff) + $num _lo/4294967296)/1048576;
if ($fexp > 0) {
for ($i =0; $i < $fexp; $i + +)
$val *= 2;
} else {
for ($i =0; $i <abs ($FEXP); $i + +)
$val/= 2;
}if ($num _hi & 0x80000000) $val =-$val;
$data [' cell '] [$row] [$col] [' data '] = (float) $val;
if (!isset ($data [' Max_row ']) | |
($data [' Max_row '] < $row))
$data [' max_row '] = $row;
if (!isset ($data [' Max_col ']) | |
($data [' Max_col '] < $col))
$data [' max_col '] = $col;
Break
Case 0x027e:
$this->dbglog->trace ("Found RK");
if ($biff _ver < 3)) break;
if ((Ord ($ws [$ptr +2]) +256*ord ($ws [$ptr +3])) < 0x0a)
return 6;
$row = Ord ($ws [$ptr +4]) +256*ord ($ws [$ptr +5]);
$col = Ord ($ws [$ptr +6]) +256*ord ($ws [$ptr +7]);
$XF _i = Ord ($ws [$ptr +8]) +256*ord ($ws [$ptr +9]);
$val = $this->rk_decode (
Excelparserutil::str2long (substr ($ws, $ptr +10,4))
);
if ($this->isdateformat ($xf _i) ==true) {
$data [' cell '] [$row] [$col] [' type '] = 3;
} else {
$data [' cell '] [$row] [$col] [' type '] = $val [' type '];
$fonti = $this->xf[' font ' [$xf _i];
$data [' cell '] [$row] [$col] [' font '] = $fonti;
$data [' cell '] [$row] [$col] [' data '] = $val [' Val '];
if (!isset ($data [' Max_row ']) | |
($data [' Max_row '] < $row))
$data [' max_row '] = $row;
if (!isset ($data [' Max_col ']) | |
($data [' Max_col '] < $col))
$data [' max_col '] = $col;
Break
Case 0X00BD:
$this->dbglog->trace ("found Mull RK");
if ($biff _ver < 5)) break;
$sz = Ord ($ws [$ptr +2]) +256*ord ($ws [$ptr +3]);
if ($sz < 6) return 6;
$row = Ord ($ws [$ptr +4]) +256*ord ($ws [$ptr +5]);
$FC = Ord ($ws [$ptr +6]) +256*ord ($ws [$ptr +7]);
$LC = Ord ($ws [$ptr + $sz +2]) +256*ord ($ws [$ptr + $sz +3]);
For ($i =0 $i <= $lc-$fc; $i + +) {
$val = $this->rk_decode (
Excelparserutil::str2long (substr ($ws, $ptr +10+ $i *6,4))
);
$XF _i=ord ($ws [$ptr +8+ $i *6]) +256*ord ($ws [($ptr +9+ $i *6)]);
if ($this->isdateformat ($xf _i) ==true) {
$data [' cell '] [$row] [$FC + $i] [' type '] = 3;
} else {
$data [' cell '] [$row] [$FC + $i] [' type '] = $val [' type '];
$fonti = $this->xf[' font ' [$xf _i];
$data [' cell '] [$row] [$FC + $i] [' font '] = $fonti;
$data [' cell '] [$row] [$FC + $i] [' data '] = $val [' Val '];
}if (!isset ($data [' Max_row ']) | |
($data [' Max_row '] < $row))
$data [' max_row '] = $row;
if (!isset ($data [' Max_col ']) | |
($data [' Max_col '] < $LC))
$data [' max_col '] = $LC;
Break
Case 0X0204:
$this->dbglog->trace ("Found LABEL");
if (($biff _ver < 3)) {
Break
}if (Ord ($ws [$ptr +2]) +256*ord ($ws [$ptr +3])) < 8) {
return 6;
$row = Ord ($ws [$ptr +4]) +256*ord ($ws [$ptr +5]);
$col = Ord ($ws [$ptr +6]) +256*ord ($ws [$ptr +7]);
$XF = Ord ($ws [$ptr +8]) +256*ord ($ws [$ptr +9]);
$fonti = $this->xf[' font ' [$XF];
$font = $this->fonts[$fonti];
$str _len = Ord ($ws [$ptr +10]) +256*ord ($ws [$ptr +11]);
if ($ptr +12+ $str _len > strlen ($WS))
return 6;
$this->sst[' Unicode '] = false;
$this->sst[' data '] = substr ($ws, $ptr +12, $str _len);
$data [' cell '] [$row] [$col] [' type '] = 0;
$sst _ind = count ($this->sst[' data ')-1;
$data [' cell '] [$row] [$col] [' data '] = $sst _ind;
$data [' cell '] [$row] [$col] [' font '] = $fonti;
if (!isset ($data [' Max_row ']) | |
($data [' Max_row '] < $row))
$data [' max_row '] = $row;
if (!isset ($data [' Max_col ']) | |
($data [' Max_col '] < $col))
$data [' max_col '] = $col;
Break
Case 0X00FD:
if ($biff _ver < 8) break;
if ((Ord ($ws [$ptr +2]) +256*ord ($ws [$ptr +3])) < 0x0a)
return 6;
$row = Ord ($ws [$ptr +4]) +256*ord ($ws [$ptr +5]);
$col = Ord ($ws [$ptr +6]) +256*ord ($ws [$ptr +7]);
$XF = Ord ($ws [$ptr +8]) +256*ord ($ws [$ptr +9]);
$fonti = $this->xf[' font ' [$XF];
$font = & $this->fonts[$fonti];
$data [' cell '] [$row] [$col] [' type '] = 0;
$sst _ind = Excelparserutil::str2long (substr ($ws, $ptr +10,4));
$data [' cell '] [$row] [$col] [' data '] = $sst _ind;
$data [' cell '] [$row] [$col] [' font '] = $fonti;
if (!isset ($data [' Max_row ']) | |
($data [' Max_row '] < $row))
$data [' max_row '] = $row;
if (!isset ($data [' Max_col ']) | |
($data [' Max_col '] < $col))
$data [' max_col '] = $col;
Break
Default
Break
$ptr + + 4+256*ord ($ws [$ptr +3]) +ord ($ws [$ptr +2]);
$this->dbglog->debug ("Parse_worksheet () function returns". Var_export ($data, true)); /*debug*/
return $data;
}
function Parse_workbook ($f _header, $DP) {
$this->dbglog->debug ("Parse_workbook () function");
$root _entry_block = $f _header->getlong (0x30);
$num _fat_blocks = $f _header->getlong (0x2c);
$this->dbglog->trace ("Header parsed");
$this->fat = Array ();
for ($i = 0; $i < $num _fat_blocks; $i + +) {
$this->dbglog->trace ("for loop iteration i =". $i);
$fat _block = $f _header->getlong (0x4c + 4 * $i);
$fatbuf = $DP->get ($fat _block * 0x200, 0x200);
$fat = new Dataprovider ($fatbuf, Dp_string_source);
if ($fat->getsize () < 0x200) {
$this->dbglog->debug ("Parse_workbook () function found (strlen ($FAT) < 0x200) returns 6");
return 6;
}for ($j =0; $j <0x80; $j + +)
$this->fat[] = $fat->getlong ($j * 4);
$fat->close ();
Unset ($fat _block, $fatbuf, $fat);
$this->dbglog->dump ($this->fat, "\ $fat");
if (count ($this->fat) < $num _fat_blocks) {
$this->dbglog->debug ("Parse_workbook () function found (count ($this->fat) < $num _fat_blocks) returns 6");
return 6;
$chain = $this->get_blocks_chain ($root _entry_block);
$dir = new Dataprovider ($DP->readfromfat ($chain), dp_string_source);
Unset ($chain);
$this->sfat = Array ();
$small _block = $f _header->getlong (0x3c);
if ($small _block!= 0xfeffffff) {
$root _entry_index = $this->find_stream ($dir, ' root entry ');
if ($root _entry_index < 0) {
$this->dbglog->debug ("parse_workbook () function dont found Root Entry returns 6");
return 6;
}
$SDC _start_block = $dir->getlong ($root _entry_index * 0x80 + 0x74);
$small _data_chain = $this->get_blocks_chain ($SDC _start_block);
$this->max_sblocks = count ($small _data_chain) * 8;
$schain = $this->get_blocks_chain ($small _block);
for ($i = 0; $i < count ($schain); $i + +) {
$sfatbuf = $DP->get ($schain [$i] * 0x200, 0x200);
$sfat = new Dataprovider ($sfatbuf, Dp_string_source);
if ($sfat->getsize () < 0x200) {
$this->dbglog->debug ("Parse_workbook () function found (strlen ($SFAT) < 0x200) returns 6");
return 6;
}
For ($j =0 $j <0x80; $j + +)
$this->sfat[] = $sfat->getlong ($j * 4);
$sfat->close ();
Unset ($sfatbuf, $sfat);
} unset ($schain);
$sfcbuf = $DP->readfromfat ($small _data_chain);
$SDP = new Dataprovider ($sfcbuf, Dp_string_source);
Unset ($sfcbuf, $small _data_chain);
$workbook _index = $this->find_stream ($dir, ' Workbook ');
if ($workbook _index<0) {
$workbook _index = $this->find_stream ($dir, ' book ');
if ($workbook _index<0) {
$this->dbglog->debug ("Parse_workbook () function Workbook index not found returns 7");
return 7;
}} $workbook _start_block = $dir->getlong ($workbook _index * 0x80 + 0x74);
$workbook _length = $dir->getlong ($workbook _index * 0x80 + 0x78);
$WB = ';
if ($workbook _length > 0) {
if ($workbook _length >= 0x1000) {
$chain = $this->get_blocks_chain ($workbook _start_block);
$WB = $DP->readfromfat ($chain);
} else {
$chain = $this->get_blocks_chain ($workbook _start_block,true);
$WB = $SDP->readfromfat ($chain, 0x40);
Unset ($SDP);
$WB = substr ($WB, 0, $workbook _length);
if (strlen ($WB)!= $workbook _length)
return 6;
Unset ($chain);
}
Unset ($this->fat, $this->sfat);
if (strlen ($WB) <= 0) {
$this->dbglog->debug ("Parse_workbook () function workbook found (strlen ($WB) <= 0) returns 7");
return 7;
}if (strlen ($WB) < 4) {
$this->dbglog->debug ("Parse_workbook () function workbook found (strlen ($WB) < 4) returns 6");
return 6;
}
if (strlen ($WB) < 256*ord ($WB [3]) +ord ($WB [2])) {
$this->dbglog->debug ("Parse_workbook () function workbook found (strlen ($WB) < 256*ord ($WB [3]) +ord ($WB [2]) < 4) returns 6 ");
return 6;
}if (Ord ($WB [0])!= 0x09) {
$this->dbglog->debug ("Parse_workbook () function workbook found (ord ($WB [0])!= 0x09) returns 6");
return 6;
} $vers = Ord ($WB [1]);
if (($vers!=0) && ($vers!=2) && ($vers!=4) && ($vers!=8)) {
return 8;
}if ($vers!=8)
$biff _ver = ($ver +4)/2;
else {
if (strlen ($WB) <) return 6;
Switch (ORD ($WB [4]) +256*ord ($WB [5]))
{
Case 0X0500:
if (Ord ($WB [0x0a]) +256*ord ($WB [0x0b]) < 1994)
$biff _ver = 5;
else {
Switch (ORD ($WB [8]) +256*ord ($WB [9])) {
Case 2412:
Case 3218:
Case 3321:
$biff _ver = 5;
Break
Default
$biff _ver = 7;
Break
}}break;
Case 0X0600:
$biff _ver = 8;
Break
Default
return 8;
}}if ($biff _ver < 5) return 8;
$ptr = 0;
$this->worksheet[' offset '] = array ();
$this->worksheet[' options '] = Array ();
$this->worksheet[' Unicode '] = array ();
$this->worksheet[' name ' = Array ();
$this->worksheet[' data '] = array ();
$this->format = $this->populateformat ();
$this->fonts = Array ();
$this->fonts[0] = Excelfont::basicfontrecord ();
$this->xf = Array ();
$this->xf[' format '] = array ();
$this->xf[' font '] = array ();
$this->xf[' Type_prot '] = array ();
$this->xf[' alignment '] = array ();
$this->xf[' decoration '] = array ();
$XF _cnt=0;
$this->sst[' Unicode '] = array ();
$this->sst[' data '] = array ();
$opcode = 0;
$sst _defined = false;
$wblen = strlen ($WB);
while ((Ord ($WB [$ptr])!=0x0a) && ($ptr < $wblen))
{
$oc = Ord ($WB [$ptr]) +256*ord ($WB [$ptr +1]);
if ($oc!= 0x3c)
$opcode = $oc;
Switch ($opcode)
{
Case 0x0085:
$ofs = Excelparserutil::str2long (substr ($WB, $ptr +4,4));
$this->worksheet[' offset '] = $ofs;
$this->worksheet[' options '] = Ord ($WB [$ptr +8]) +256*ord ($WB [$ptr +9]);
if ($biff _ver==8) {
$len = Ord ($WB [$ptr +10]);
if ((Ord ($WB [$ptr +11]) & 1) > 0) {
$this->worksheet[' Unicode '] = true;
$len = $len *2;
} else {
$this->worksheet[' Unicode '] = false;
$this->worksheet[' name ' [] = substr ($WB, $ptr +12, $len);
} else {
$this->worksheet[' Unicode '] = false;
$len = Ord ($WB [$ptr +10]);
$this->worksheet[' name '] = substr ($WB, $ptr +11, $len);
$PWS = $this->parse_worksheet (substr ($WB, $ofs));
if (Is_array ($PWS))
$this->worksheet[' data '] = $PWS;
Else
return $PWS;
Break
Case 0X041E:
$fidx = Ord ($WB [$ptr +4]) +256*ord ($WB [$ptr +5]);
if ($fidx <0x31 | | $fidx ==0x31)
Break
ElseIf ($biff _ver>7)
$this->format[$fidx] = $this->getunicodestring ($WB, $ptr +6);
Break
Case Excel_font_rid:
$rec = Excelfont::getfontrecord ($WB, $ptr +4);
$this->fonts[count ($this->fonts)] = $rec;
Break
Case 0X00E0:
$this->xf[' font '] [$xf _cnt] = Ord ($WB [$ptr +4]) +256*ord ($WB [$ptr +5]);
$this->xf[' format '] [$xf _cnt] = Ord ($WB [$ptr +6]) +256*ord ($WB [$ptr +7]);
$this->xf[' type ' [$xf _cnt] = "1";
$this->xf[' bitmask ' [$xf _cnt] = "1";
$XF _cnt++;
Break
Case 0X00FC:
if ($biff _ver < 8) break;
$sbuflen = Ord ($WB [$ptr +2]) + 256*ord ($WB [$ptr +3]);
if ($oc!= 0x3c) {
if ($sst _defined) return 6;
$snum = Excelparserutil::str2long (substr ($WB, $ptr +8,4));
$sptr = $ptr +12;
$sst _defined = true;
} else {
if ($rslen > $slen) {
$sptr = $ptr +4;
$rslen-= $slen;
$slen = $rslen;
if ((Ord ($WB [$sptr]) & 1) > 0) {
if ($char _bytes = = 1) {
$sstr = ';
for ($i =0; $i <strlen ($STR); $i + +)
$sstr. = $str [$i].chr (0);
$str = $sstr;
$char _bytes=2;
} $schar _bytes = 2;
} else {
$schar _bytes = 1;
}if ($sptr + $slen * $schar _bytes > $ptr +4+ $sbuflen)
$slen = ($ptr + $sbuflen-$sptr +3)/$schar _bytes;
$sstr = substr ($WB, $sptr +1, $slen * $schar _bytes);
if ($char _bytes = 2) && ($schar _bytes = = 1))
{
$sstr 2 = ';
for ($i =0; $i <strlen ($SSTR); $i + +)
$sstr 2. = $sstr [$i].chr (0);
$sstr = $sstr 2;
$str. = $sstr;
$sptr + + $slen * $schar _bytes+1+4* $rt + $fesz;
if ($slen < $rslen) {
if ($sptr >= strlen ($WB)) | |
($sptr < $ptr +4+ $sbuflen) | |
(Ord ($WB [$sptr])!= 0x3c))
{
return 6;
}break;
} else {
if ($char _bytes = = 2) {
$this->sst[' Unicode '] = true;
} else {
$this->sst[' Unicode '] = false;
$this->sst[' data ' [] = $STR;
$snum--;
}} else {
$sptr = $ptr +4;
if ($sptr > $ptr) $sptr + = 4* $rt + $fesz;
}}while ($sptr < $ptr +4+ $sbuflen) &&
($sptr < strlen ($WB)) &&
($snum > 0))
{
$rslen = Ord ($WB [$sptr]) +256*ord ($WB [$sptr +1]);
$slen = $rslen;
if ((Ord ($WB [$sptr +2]) & 1) > 0) {
$char _bytes = 2;
} else {
$char _bytes = 1;
} $rt = 0;
$fesz = 0;
Switch (ORD ($WB [$sptr +2]) & 0x0c) {
Case 0X0C:
$rt = Ord ($WB [$sptr +3]) +256* (Ord ($WB [$sptr +4]));
$fesz = Excelparserutil::str2long (substr ($WB, $sptr +5,4));
if ($sptr +9+ $slen * $char _bytes > $ptr +4+ $sbuflen)
$slen = ($ptr + $sbuflen-$sptr-5)/$char _bytes;
$str = substr ($WB, $sptr +9, $slen * $char _bytes);
$sptr + + $slen * $char _bytes+9;
Break
Case 8:
$rt = Ord ($WB [$sptr +3]) +256* (Ord ($WB [$sptr +4]));
if ($sptr +5+ $slen * $char _bytes > $ptr +4+ $sbuflen)
$slen = ($ptr + $sbuflen-$sptr-1)/$char _bytes;
$str = substr ($WB, $sptr +5, $slen * $char _bytes);
$sptr + + $slen * $char _bytes+5;
Break
Case 4:
$fesz = Excelparserutil::str2long (substr ($WB, $sptr +3,4));
if ($sptr +7+ $slen * $char _bytes > $ptr +4+ $sbuflen)
$slen = ($ptr + $sbuflen-$sptr-3)/$char _bytes;
$str = substr ($WB, $sptr +7, $slen * $char _bytes);
$sptr + + $slen * $char _bytes+7;
Break
Case 0:
if ($sptr +3+ $slen * $char _bytes > $ptr +4+ $sbuflen)
$slen = ($ptr + $sbuflen-$sptr + 1)/$char _bytes;
$str = substr ($WB, $sptr +3, $slen * $char _bytes);
$sptr + + $slen * $char _bytes+3;
Break
} if ($slen < $rslen) {
if ($sptr >= strlen ($WB)) | |
($sptr < $ptr +4+ $sbuflen) | |
(Ord ($WB [$sptr])!= 0x3c)) return 6;
} else {
if ($char _bytes = = 2) {
$this->sst[' Unicode '] = true;
} else {
$this->sst[' Unicode '] = false;
} $sptr + + 4* $rt + $fesz;
$this->sst[' data '] = $STR;
$snum--;
}}
Break
}
$ptr + + 4+256*ord ($WB [$ptr +3]) +ord ($WB [$ptr +2]);
}
$this->biff_version = $biff _ver;
$this->dbglog->debug ("Parse_workbook () function returns 0");
return 0;
}
function parsefromstring ($contents)
{
$this->dbglog->info ("parsefromstring () enter.");
$this->DP = new Dataprovider ($contents, Dp_string_source);
return $this->initparser ();
}function Parsefromfile ($filename)
{
$this->dbglog->info ("Parsefromfile () enter.");
$this->DP = new Dataprovider ($filename, Dp_file_source);
return $this->initparser ();
}function Initparser ()
{
$this->dbglog->info ("Initparser () enter.");
if (! $this->dp->isvalid ())
{
$this->dbglog->error ("Initparser () Failed to open file.");
$this->dbglog->error ("Initparser () function returns 1");
return 1;
}if ($this->dp->getsize () <= 0x200)
{
$this->dbglog->error ("Initparser () file too small to is an Excel file.");
$this->dbglog->error ("Initparser () function returns 2");
return 2;
$this->max_blocks = $this->dp->getblocks ();
$hdrbuf = $this->dp->get (0, 0x200);
if (strlen ($HDRBUF) < 0x200)
{
$this->dbglog->error ("Initparser () Error reading header.");
$this->dbglog->error ("Initparser () function returns 3");
return 3;
}
$header _sig = Array (0xd0,0xcf,0x11,0xe0,0xa1,0xb1,0x1a,0xe1);
for ($i = 0; $i < count ($header _sig); $i + +)
if ($header _sig[$i]!= ord ($hdrbuf [$i])) {
$this->dbglog->error ("Initparser () function founds invalid header");
$this->dbglog->error ("Initparser () function returns 5");
return 5;
$f _header = new Dataprovider ($hdrbuf, Dp_string_source);
Unset ($hdrbuf, $header _sig, $i);
$this->dp->_baseofs = 0x200;
$RC = $this->parse_workbook ($f _header, $this->DP);
unset ($f _header);
Unset ($this->dp, $this->max_blocks, $this->max_sblocks);
return $RC;
}}
function Unescape ($STR) {
$str = Rawurldecode ($STR);
Preg_match_all ("/(?:%u.{4}) |& #x. {4};|&#\d+;|.+/u", $str, $r);
$ar = $r [0];
foreach ($ar as $k => $v) {
if (substr ($v, 0,2) = = "%u")
$ar [$k] = Iconv ("UCS-2", "GB2312", Pack ("H4", substr ($v,-4));
ElseIf (substr ($v, 0,3) = = "& #x")
$ar [$k] = Iconv ("UCS-2", "GB2312", Pack ("H4", substr ($v, 3,-1));
ElseIf (substr ($v, 0,2) = = "&#") {
$ar [$k] = Iconv ("UCS-2", "GB2312", Pack ("n", substr ($v, 2,-1));
}
}
return join ("", $ar);
}
function uc2html ($STR) {
$ret = ';
For ($i =0 $i <strlen ($STR)/2; $i + +) {
$charcode = Ord ($str [$i *2]) +256*ord ($str [$i *2+1]);
$ret. = ' &# '. $charcode. ';
}return $ret;
}
function Read_excel_file ($ExcelFile,& $result) {
$exc = new Excelfileparser ("", Abc_no_log);
$res = $exc->parsefromfile ($ExcelFile); $result =null;
Switch ($res) {
Case 0:break;
Case 1: $err = "Cannot open file"; Break
Case 2: $err = "file is too small, may not be an Excel file"; Break
Case 3: $err = "File header read error"; Break
Case 4: $err = "Error reading file"; Break
Case 5: $err = "This is not an Excel file or Excel5.0 previous version file"; Break
Case 6: $err = "file corrupted"; Break
Case 7: $err = "No Excel data found in file"; Break
Case 8: $err = "Unsupported file version"; Break
Default
$err = "Unknown error"; Break
}
For ($ws _num=0 $ws _num<count ($exc->worksheet[' name '); $ws _num++)
{
$Sheetname = $exc->worksheet[' name ' [$ws _num];
$ws = $exc->worksheet[' data '] [$ws _num];
For ($j =0 $j <= $ws [' Max_row ']; $j + +) {
For ($i =0 $i <= $ws [' Max_col ']; $i + +) {
$data = $ws [' cell '] [$j] [$i];
Switch ($data [' type ']) {
Case 0:
$ind = $data [' Data '];
if ($exc->sst[' Unicode '] [$ind]) {
$s = unescape (uc2html ($exc->sst[' data '] [$ind]);
} else
$s = $exc->sst[' data '] [$ind];
if (strlen ($s)) ==0)
$V = "";
Else
$V = $s;
Break
Case 1:
$V = (int) ($data [' data ']);
Break
Case 2:
$V = (float) ($data [' data ']);
Break
Case 3:
$ret = $exc->getdatearray ($data [' data ']);
$V = $ret [' Year ']. " -". $ret [' Month ']." -". $ret [' Day ']." ". $ret [' Hour '];
Break
Default
Break
}
$result [$Sheetname] [$j] [$i]= $V;
}
}
}
if ($err = = ") {return 0;} else {return $err;}
}
?>