PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/core/vendors/excel/reader.php

https://bitbucket.org/gio_jgs/botica
PHP | 813 lines | 587 code | 80 blank | 146 comment | 126 complexity | c7b0d309383fd6fe68ce2bf8661b4e0b MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. //require_once 'PEAR.php';
  3. require_once 'oleread.inc';
  4. //define('Spreadsheet_Excel_Reader_HAVE_ICONV', function_exists('iconv'));
  5. //define('Spreadsheet_Excel_Reader_HAVE_MB', function_exists('mb_convert_encoding'));
  6. define('Spreadsheet_Excel_Reader_BIFF8', 0x600);
  7. define('Spreadsheet_Excel_Reader_BIFF7', 0x500);
  8. define('Spreadsheet_Excel_Reader_WorkbookGlobals', 0x5);
  9. define('Spreadsheet_Excel_Reader_Worksheet', 0x10);
  10. define('Spreadsheet_Excel_Reader_Type_BOF', 0x809);
  11. define('Spreadsheet_Excel_Reader_Type_EOF', 0x0a);
  12. define('Spreadsheet_Excel_Reader_Type_BOUNDSHEET', 0x85);
  13. define('Spreadsheet_Excel_Reader_Type_DIMENSION', 0x200);
  14. define('Spreadsheet_Excel_Reader_Type_ROW', 0x208);
  15. define('Spreadsheet_Excel_Reader_Type_DBCELL', 0xd7);
  16. define('Spreadsheet_Excel_Reader_Type_FILEPASS', 0x2f);
  17. define('Spreadsheet_Excel_Reader_Type_NOTE', 0x1c);
  18. define('Spreadsheet_Excel_Reader_Type_TXO', 0x1b6);
  19. define('Spreadsheet_Excel_Reader_Type_RK', 0x7e);
  20. define('Spreadsheet_Excel_Reader_Type_RK2', 0x27e);
  21. define('Spreadsheet_Excel_Reader_Type_MULRK', 0xbd);
  22. define('Spreadsheet_Excel_Reader_Type_MULBLANK', 0xbe);
  23. define('Spreadsheet_Excel_Reader_Type_INDEX', 0x20b);
  24. define('Spreadsheet_Excel_Reader_Type_SST', 0xfc);
  25. define('Spreadsheet_Excel_Reader_Type_EXTSST', 0xff);
  26. define('Spreadsheet_Excel_Reader_Type_CONTINUE', 0x3c);
  27. define('Spreadsheet_Excel_Reader_Type_LABEL', 0x204);
  28. define('Spreadsheet_Excel_Reader_Type_LABELSST', 0xfd);
  29. define('Spreadsheet_Excel_Reader_Type_NUMBER', 0x203);
  30. define('Spreadsheet_Excel_Reader_Type_NAME', 0x18);
  31. define('Spreadsheet_Excel_Reader_Type_ARRAY', 0x221);
  32. define('Spreadsheet_Excel_Reader_Type_STRING', 0x207);
  33. define('Spreadsheet_Excel_Reader_Type_FORMULA', 0x406);
  34. define('Spreadsheet_Excel_Reader_Type_FORMULA2', 0x6);
  35. define('Spreadsheet_Excel_Reader_Type_FORMAT', 0x41e);
  36. define('Spreadsheet_Excel_Reader_Type_XF', 0xe0);
  37. define('Spreadsheet_Excel_Reader_Type_BOOLERR', 0x205);
  38. define('Spreadsheet_Excel_Reader_Type_UNKNOWN', 0xffff);
  39. define('Spreadsheet_Excel_Reader_Type_NINETEENFOUR', 0x22);
  40. define('Spreadsheet_Excel_Reader_Type_MERGEDCELLS', 0xE5);
  41. define('Spreadsheet_Excel_Reader_utcOffsetDays' , 25569);
  42. define('Spreadsheet_Excel_Reader_utcOffsetDays1904', 24107);
  43. define('Spreadsheet_Excel_Reader_msInADay', 24 * 60 * 60);
  44. //define('Spreadsheet_Excel_Reader_DEF_NUM_FORMAT', "%.2f");
  45. define('Spreadsheet_Excel_Reader_DEF_NUM_FORMAT', "%s");
  46. // function file_get_contents for PHP < 4.3.0
  47. // Thanks Marian Steinbach for this function
  48. if (!function_exists('file_get_contents')) {
  49. function file_get_contents($filename, $use_include_path = 0) {
  50. $data = '';
  51. $file = @fopen($filename, "rb", $use_include_path);
  52. if ($file) {
  53. while (!feof($file)) $data .= fread($file, 1024);
  54. fclose($file);
  55. } else {
  56. // There was a problem opening the file
  57. $data = FALSE;
  58. }
  59. return $data;
  60. }
  61. }
  62. //class Spreadsheet_Excel_Reader extends PEAR {
  63. class Spreadsheet_Excel_Reader {
  64. var $boundsheets = array();
  65. var $formatRecords = array();
  66. var $sst = array();
  67. var $sheets = array();
  68. var $data;
  69. var $pos;
  70. var $_ole;
  71. var $_defaultEncoding;
  72. var $_defaultFormat = Spreadsheet_Excel_Reader_DEF_NUM_FORMAT;
  73. var $_columnsFormat = array();
  74. var $_rowoffset = 1;
  75. var $_coloffset = 1;
  76. var $dateFormats = array (
  77. 0xe => "d/m/Y",
  78. 0xf => "d-M-Y",
  79. 0x10 => "d-M",
  80. 0x11 => "M-Y",
  81. 0x12 => "h:i a",
  82. 0x13 => "h:i:s a",
  83. 0x14 => "H:i",
  84. 0x15 => "H:i:s",
  85. 0x16 => "d/m/Y H:i",
  86. 0x2d => "i:s",
  87. 0x2e => "H:i:s",
  88. 0x2f => "i:s.S");
  89. var $numberFormats = array(
  90. 0x1 => "%1.0f", // "0"
  91. 0x2 => "%1.2f", // "0.00",
  92. 0x3 => "%1.0f", //"#,##0",
  93. 0x4 => "%1.2f", //"#,##0.00",
  94. 0x5 => "%1.0f", /*"$#,##0;($#,##0)",*/
  95. 0x6 => '$%1.0f', /*"$#,##0;($#,##0)",*/
  96. 0x7 => '$%1.2f', //"$#,##0.00;($#,##0.00)",
  97. 0x8 => '$%1.2f', //"$#,##0.00;($#,##0.00)",
  98. 0x9 => '%1.0f%%', // "0%"
  99. 0xa => '%1.2f%%', // "0.00%"
  100. 0xb => '%1.2f', // 0.00E00",
  101. 0x25 => '%1.0f', // "#,##0;(#,##0)",
  102. 0x26 => '%1.0f', //"#,##0;(#,##0)",
  103. 0x27 => '%1.2f', //"#,##0.00;(#,##0.00)",
  104. 0x28 => '%1.2f', //"#,##0.00;(#,##0.00)",
  105. 0x29 => '%1.0f', //"#,##0;(#,##0)",
  106. 0x2a => '$%1.0f', //"$#,##0;($#,##0)",
  107. 0x2b => '%1.2f', //"#,##0.00;(#,##0.00)",
  108. 0x2c => '$%1.2f', //"$#,##0.00;($#,##0.00)",
  109. 0x30 => '%1.0f'); //"##0.0E0";
  110. function Spreadsheet_Excel_Reader(){
  111. $this->_ole = new OLERead();
  112. $this->setUTFEncoder('iconv');
  113. }
  114. function setOutputEncoding($Encoding){
  115. $this->_defaultEncoding = $Encoding;
  116. }
  117. /**
  118. * $encoder = 'iconv' or 'mb'
  119. * set iconv if you would like use 'iconv' for encode UTF-16LE to your encoding
  120. * set mb if you would like use 'mb_convert_encoding' for encode UTF-16LE to your encoding
  121. */
  122. function setUTFEncoder($encoder = 'iconv'){
  123. $this->_encoderFunction = '';
  124. if ($encoder == 'iconv'){
  125. $this->_encoderFunction = function_exists('iconv') ? 'iconv' : '';
  126. }elseif ($encoder == 'mb') {
  127. $this->_encoderFunction = function_exists('mb_convert_encoding') ? 'mb_convert_encoding' : '';
  128. }
  129. }
  130. function setRowColOffset($iOffset){
  131. $this->_rowoffset = $iOffset;
  132. $this->_coloffset = $iOffset;
  133. }
  134. function setDefaultFormat($sFormat){
  135. $this->_defaultFormat = $sFormat;
  136. }
  137. function setColumnFormat($column, $sFormat){
  138. $this->_columnsFormat[$column] = $sFormat;
  139. }
  140. function read($sFileName) {
  141. $errlevel = error_reporting();
  142. error_reporting($errlevel ^ E_NOTICE);
  143. $res = $this->_ole->read($sFileName);
  144. // oops, something goes wrong (Darko Miljanovic)
  145. if($res === false) {
  146. // check error code
  147. if($this->_ole->error == 1) {
  148. // bad file
  149. die('The filename ' . $sFileName . ' is not readable');
  150. }
  151. // check other error codes here (eg bad fileformat, etc...)
  152. }
  153. $this->data = $this->_ole->getWorkBook();
  154. /*
  155. $res = $this->_ole->read($sFileName);
  156. if ($this->isError($res)) {
  157. // var_dump($res);
  158. return $this->raiseError($res);
  159. }
  160. $total = $this->_ole->ppsTotal();
  161. for ($i = 0; $i < $total; $i++) {
  162. if ($this->_ole->isFile($i)) {
  163. $type = unpack("v", $this->_ole->getData($i, 0, 2));
  164. if ($type[''] == 0x0809) { // check if it's a BIFF stream
  165. $this->_index = $i;
  166. $this->data = $this->_ole->getData($i, 0, $this->_ole->getDataLength($i));
  167. break;
  168. }
  169. }
  170. }
  171. if ($this->_index === null) {
  172. return $this->raiseError("$file doesn't seem to be an Excel file");
  173. }
  174. */
  175. //var_dump($this->data);
  176. //echo "data =".$this->data;
  177. $this->pos = 0;
  178. //$this->readRecords();
  179. $this->_parse();
  180. error_reporting($errlevel);
  181. }
  182. function _parse(){
  183. $pos = 0;
  184. $code = ord($this->data[$pos]) | ord($this->data[$pos+1])<<8;
  185. $length = ord($this->data[$pos+2]) | ord($this->data[$pos+3])<<8;
  186. $version = ord($this->data[$pos + 4]) | ord($this->data[$pos + 5])<<8;
  187. $substreamType = ord($this->data[$pos + 6]) | ord($this->data[$pos + 7])<<8;
  188. //echo "Start parse code=".base_convert($code,10,16)." version=".base_convert($version,10,16)." substreamType=".base_convert($substreamType,10,16).""."\n";
  189. if (($version != Spreadsheet_Excel_Reader_BIFF8) && ($version != Spreadsheet_Excel_Reader_BIFF7)) {
  190. return false;
  191. }
  192. if ($substreamType != Spreadsheet_Excel_Reader_WorkbookGlobals){
  193. return false;
  194. }
  195. //print_r($rec);
  196. $pos += $length + 4;
  197. $code = ord($this->data[$pos]) | ord($this->data[$pos+1])<<8;
  198. $length = ord($this->data[$pos+2]) | ord($this->data[$pos+3])<<8;
  199. while ($code != Spreadsheet_Excel_Reader_Type_EOF){
  200. switch ($code) {
  201. case Spreadsheet_Excel_Reader_Type_SST:
  202. //echo "Type_SST\n";
  203. $spos = $pos + 4;
  204. $limitpos = $spos + $length;
  205. $uniqueStrings = $this->_GetInt4d($this->data, $spos+4);
  206. $spos += 8;
  207. for ($i = 0; $i < $uniqueStrings; $i++) {
  208. // Read in the number of characters
  209. if ($spos == $limitpos) {
  210. $opcode = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8;
  211. $conlength = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8;
  212. if ($opcode != 0x3c) {
  213. return -1;
  214. }
  215. $spos += 4;
  216. $limitpos = $spos + $conlength;
  217. }
  218. $numChars = ord($this->data[$spos]) | (ord($this->data[$spos+1]) << 8);
  219. //echo "i = $i pos = $pos numChars = $numChars ";
  220. $spos += 2;
  221. $optionFlags = ord($this->data[$spos]);
  222. $spos++;
  223. $asciiEncoding = (($optionFlags & 0x01) == 0) ;
  224. $extendedString = ( ($optionFlags & 0x04) != 0);
  225. // See if string contains formatting information
  226. $richString = ( ($optionFlags & 0x08) != 0);
  227. if ($richString) {
  228. // Read in the crun
  229. $formattingRuns = ord($this->data[$spos]) | (ord($this->data[$spos+1]) << 8);
  230. $spos += 2;
  231. }
  232. if ($extendedString) {
  233. // Read in cchExtRst
  234. $extendedRunLength = $this->_GetInt4d($this->data, $spos);
  235. $spos += 4;
  236. }
  237. $len = ($asciiEncoding)? $numChars : $numChars*2;
  238. if ($spos + $len < $limitpos) {
  239. $retstr = substr($this->data, $spos, $len);
  240. $spos += $len;
  241. }else{
  242. // found countinue
  243. $retstr = substr($this->data, $spos, $limitpos - $spos);
  244. $bytesRead = $limitpos - $spos;
  245. $charsLeft = $numChars - (($asciiEncoding) ? $bytesRead : ($bytesRead / 2));
  246. $spos = $limitpos;
  247. while ($charsLeft > 0){
  248. $opcode = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8;
  249. $conlength = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8;
  250. if ($opcode != 0x3c) {
  251. return -1;
  252. }
  253. $spos += 4;
  254. $limitpos = $spos + $conlength;
  255. $option = ord($this->data[$spos]);
  256. $spos += 1;
  257. if ($asciiEncoding && ($option == 0)) {
  258. $len = min($charsLeft, $limitpos - $spos); // min($charsLeft, $conlength);
  259. $retstr .= substr($this->data, $spos, $len);
  260. $charsLeft -= $len;
  261. $asciiEncoding = true;
  262. }elseif (!$asciiEncoding && ($option != 0)){
  263. $len = min($charsLeft * 2, $limitpos - $spos); // min($charsLeft, $conlength);
  264. $retstr .= substr($this->data, $spos, $len);
  265. $charsLeft -= $len/2;
  266. $asciiEncoding = false;
  267. }elseif (!$asciiEncoding && ($option == 0)) {
  268. // Bummer - the string starts off as Unicode, but after the
  269. // continuation it is in straightforward ASCII encoding
  270. $len = min($charsLeft, $limitpos - $spos); // min($charsLeft, $conlength);
  271. for ($j = 0; $j < $len; $j++) {
  272. $retstr .= $this->data[$spos + $j].chr(0);
  273. }
  274. $charsLeft -= $len;
  275. $asciiEncoding = false;
  276. }else{
  277. $newstr = '';
  278. for ($j = 0; $j < strlen($retstr); $j++) {
  279. $newstr = $retstr[$j].chr(0);
  280. }
  281. $retstr = $newstr;
  282. $len = min($charsLeft * 2, $limitpos - $spos); // min($charsLeft, $conlength);
  283. $retstr .= substr($this->data, $spos, $len);
  284. $charsLeft -= $len/2;
  285. $asciiEncoding = false;
  286. //echo "Izavrat\n";
  287. }
  288. $spos += $len;
  289. }
  290. }
  291. $retstr = ($asciiEncoding) ? $retstr : $this->_encodeUTF16($retstr);
  292. // echo "Str $i = $retstr\n";
  293. if ($richString){
  294. $spos += 4 * $formattingRuns;
  295. }
  296. // For extended strings, skip over the extended string data
  297. if ($extendedString) {
  298. $spos += $extendedRunLength;
  299. }
  300. //if ($retstr == 'Derby'){
  301. // echo "bb\n";
  302. //}
  303. $this->sst[]=$retstr;
  304. }
  305. /*$continueRecords = array();
  306. while ($this->getNextCode() == Type_CONTINUE) {
  307. $continueRecords[] = &$this->nextRecord();
  308. }
  309. //echo " 1 Type_SST\n";
  310. $this->shareStrings = new SSTRecord($r, $continueRecords);
  311. //print_r($this->shareStrings->strings);
  312. */
  313. // echo 'SST read: '.($time_end-$time_start)."\n";
  314. break;
  315. case Spreadsheet_Excel_Reader_Type_FILEPASS:
  316. return false;
  317. break;
  318. case Spreadsheet_Excel_Reader_Type_NAME:
  319. //echo "Type_NAME\n";
  320. break;
  321. case Spreadsheet_Excel_Reader_Type_FORMAT:
  322. $indexCode = ord($this->data[$pos+4]) | ord($this->data[$pos+5]) << 8;
  323. if ($version == Spreadsheet_Excel_Reader_BIFF8) {
  324. $numchars = ord($this->data[$pos+6]) | ord($this->data[$pos+7]) << 8;
  325. if (ord($this->data[$pos+8]) == 0){
  326. $formatString = substr($this->data, $pos+9, $numchars);
  327. } else {
  328. $formatString = substr($this->data, $pos+9, $numchars*2);
  329. }
  330. } else {
  331. $numchars = ord($this->data[$pos+6]);
  332. $formatString = substr($this->data, $pos+7, $numchars*2);
  333. }
  334. $this->formatRecords[$indexCode] = $formatString;
  335. // echo "Type.FORMAT\n";
  336. break;
  337. case Spreadsheet_Excel_Reader_Type_XF:
  338. //global $dateFormats, $numberFormats;
  339. $indexCode = ord($this->data[$pos+6]) | ord($this->data[$pos+7]) << 8;
  340. //echo "\nType.XF ".count($this->formatRecords['xfrecords'])." $indexCode ";
  341. if (array_key_exists($indexCode, $this->dateFormats)) {
  342. //echo "isdate ".$dateFormats[$indexCode];
  343. $this->formatRecords['xfrecords'][] = array(
  344. 'type' => 'date',
  345. 'format' => $this->dateFormats[$indexCode]
  346. );
  347. }elseif (array_key_exists($indexCode, $this->numberFormats)) {
  348. //echo "isnumber ".$this->numberFormats[$indexCode];
  349. $this->formatRecords['xfrecords'][] = array(
  350. 'type' => 'number',
  351. 'format' => $this->numberFormats[$indexCode]
  352. );
  353. }else{
  354. $isdate = FALSE;
  355. if ($indexCode > 0){
  356. if (isset($this->formatRecords[$indexCode]))
  357. $formatstr = $this->formatRecords[$indexCode];
  358. //echo '.other.';
  359. //echo "\ndate-time=$formatstr=\n";
  360. if ($formatstr)
  361. if (preg_match("/[^hmsday\/\-:\s]/i", $formatstr) == 0) { // found day and time format
  362. $isdate = TRUE;
  363. $formatstr = str_replace('mm', 'i', $formatstr);
  364. $formatstr = str_replace('h', 'H', $formatstr);
  365. //echo "\ndate-time $formatstr \n";
  366. }
  367. }
  368. if ($isdate){
  369. $this->formatRecords['xfrecords'][] = array(
  370. 'type' => 'date',
  371. 'format' => $formatstr,
  372. );
  373. }else{
  374. $this->formatRecords['xfrecords'][] = array(
  375. 'type' => 'other',
  376. 'format' => '',
  377. 'code' => $indexCode
  378. );
  379. }
  380. }
  381. //echo "\n";
  382. break;
  383. case Spreadsheet_Excel_Reader_Type_NINETEENFOUR:
  384. //echo "Type.NINETEENFOUR\n";
  385. $this->nineteenFour = (ord($this->data[$pos+4]) == 1);
  386. break;
  387. case Spreadsheet_Excel_Reader_Type_BOUNDSHEET:
  388. //echo "Type.BOUNDSHEET\n";
  389. $rec_offset = $this->_GetInt4d($this->data, $pos+4);
  390. $rec_typeFlag = ord($this->data[$pos+8]);
  391. $rec_visibilityFlag = ord($this->data[$pos+9]);
  392. $rec_length = ord($this->data[$pos+10]);
  393. if ($version == Spreadsheet_Excel_Reader_BIFF8){
  394. $chartype = ord($this->data[$pos+11]);
  395. if ($chartype == 0){
  396. $rec_name = substr($this->data, $pos+12, $rec_length);
  397. } else {
  398. $rec_name = $this->_encodeUTF16(substr($this->data, $pos+12, $rec_length*2));
  399. }
  400. }elseif ($version == Spreadsheet_Excel_Reader_BIFF7){
  401. $rec_name = substr($this->data, $pos+11, $rec_length);
  402. }
  403. $this->boundsheets[] = array('name'=>$rec_name,
  404. 'offset'=>$rec_offset);
  405. break;
  406. }
  407. //echo "Code = ".base_convert($r['code'],10,16)."\n";
  408. $pos += $length + 4;
  409. $code = ord($this->data[$pos]) | ord($this->data[$pos+1])<<8;
  410. $length = ord($this->data[$pos+2]) | ord($this->data[$pos+3])<<8;
  411. //$r = &$this->nextRecord();
  412. //echo "1 Code = ".base_convert($r['code'],10,16)."\n";
  413. }
  414. foreach ($this->boundsheets as $key=>$val){
  415. $this->sn = $key;
  416. $this->_parsesheet($val['offset']);
  417. }
  418. return true;
  419. }
  420. function _parsesheet($spos){
  421. $cont = true;
  422. // read BOF
  423. $code = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8;
  424. $length = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8;
  425. $version = ord($this->data[$spos + 4]) | ord($this->data[$spos + 5])<<8;
  426. $substreamType = ord($this->data[$spos + 6]) | ord($this->data[$spos + 7])<<8;
  427. if (($version != Spreadsheet_Excel_Reader_BIFF8) && ($version != Spreadsheet_Excel_Reader_BIFF7)) {
  428. return -1;
  429. }
  430. if ($substreamType != Spreadsheet_Excel_Reader_Worksheet){
  431. return -2;
  432. }
  433. //echo "Start parse code=".base_convert($code,10,16)." version=".base_convert($version,10,16)." substreamType=".base_convert($substreamType,10,16).""."\n";
  434. $spos += $length + 4;
  435. //var_dump($this->formatRecords);
  436. //echo "code $code $length";
  437. while($cont) {
  438. //echo "mem= ".memory_get_usage()."\n";
  439. // $r = &$this->file->nextRecord();
  440. $lowcode = ord($this->data[$spos]);
  441. if ($lowcode == Spreadsheet_Excel_Reader_Type_EOF) break;
  442. $code = $lowcode | ord($this->data[$spos+1])<<8;
  443. $length = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8;
  444. $spos += 4;
  445. $this->sheets[$this->sn]['maxrow'] = $this->_rowoffset - 1;
  446. $this->sheets[$this->sn]['maxcol'] = $this->_coloffset - 1;
  447. //echo "Code=".base_convert($code,10,16)." $code\n";
  448. unset($this->rectype);
  449. $this->multiplier = 1; // need for format with %
  450. switch ($code) {
  451. case Spreadsheet_Excel_Reader_Type_DIMENSION:
  452. //echo 'Type_DIMENSION ';
  453. if (!isset($this->numRows)) {
  454. if (($length == 10) || ($version == Spreadsheet_Excel_Reader_BIFF7)){
  455. $this->sheets[$this->sn]['numRows'] = ord($this->data[$spos+2]) | ord($this->data[$spos+3]) << 8;
  456. $this->sheets[$this->sn]['numCols'] = ord($this->data[$spos+6]) | ord($this->data[$spos+7]) << 8;
  457. } else {
  458. $this->sheets[$this->sn]['numRows'] = ord($this->data[$spos+4]) | ord($this->data[$spos+5]) << 8;
  459. $this->sheets[$this->sn]['numCols'] = ord($this->data[$spos+10]) | ord($this->data[$spos+11]) << 8;
  460. }
  461. }
  462. //echo 'numRows '.$this->numRows.' '.$this->numCols."\n";
  463. break;
  464. case Spreadsheet_Excel_Reader_Type_MERGEDCELLS:
  465. $cellRanges = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8;
  466. for ($i = 0; $i < $cellRanges; $i++) {
  467. $fr = ord($this->data[$spos + 8*$i + 2]) | ord($this->data[$spos + 8*$i + 3])<<8;
  468. $lr = ord($this->data[$spos + 8*$i + 4]) | ord($this->data[$spos + 8*$i + 5])<<8;
  469. $fc = ord($this->data[$spos + 8*$i + 6]) | ord($this->data[$spos + 8*$i + 7])<<8;
  470. $lc = ord($this->data[$spos + 8*$i + 8]) | ord($this->data[$spos + 8*$i + 9])<<8;
  471. //$this->sheets[$this->sn]['mergedCells'][] = array($fr + 1, $fc + 1, $lr + 1, $lc + 1);
  472. if ($lr - $fr > 0) {
  473. $this->sheets[$this->sn]['cellsInfo'][$fr+1][$fc+1]['rowspan'] = $lr - $fr + 1;
  474. }
  475. if ($lc - $fc > 0) {
  476. $this->sheets[$this->sn]['cellsInfo'][$fr+1][$fc+1]['colspan'] = $lc - $fc + 1;
  477. }
  478. }
  479. //echo "Merged Cells $cellRanges $lr $fr $lc $fc\n";
  480. break;
  481. case Spreadsheet_Excel_Reader_Type_RK:
  482. case Spreadsheet_Excel_Reader_Type_RK2:
  483. //echo 'Spreadsheet_Excel_Reader_Type_RK'."\n";
  484. $row = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8;
  485. $column = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8;
  486. $rknum = $this->_GetInt4d($this->data, $spos + 6);
  487. $numValue = $this->_GetIEEE754($rknum);
  488. //echo $numValue." ";
  489. if ($this->isDate($spos)) {
  490. list($string, $raw) = $this->createDate($numValue);
  491. }else{
  492. $raw = $numValue;
  493. if (isset($this->_columnsFormat[$column + 1])){
  494. $this->curformat = $this->_columnsFormat[$column + 1];
  495. }
  496. $string = sprintf($this->curformat, $numValue * $this->multiplier);
  497. //$this->addcell(RKRecord($r));
  498. }
  499. $this->addcell($row, $column, $string, $raw);
  500. //echo "Type_RK $row $column $string $raw {$this->curformat}\n";
  501. break;
  502. case Spreadsheet_Excel_Reader_Type_LABELSST:
  503. $row = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8;
  504. $column = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8;
  505. $xfindex = ord($this->data[$spos+4]) | ord($this->data[$spos+5])<<8;
  506. $index = $this->_GetInt4d($this->data, $spos + 6);
  507. //var_dump($this->sst);
  508. $this->addcell($row, $column, $this->sst[$index]);
  509. //echo "LabelSST $row $column $string\n";
  510. break;
  511. case Spreadsheet_Excel_Reader_Type_MULRK:
  512. $row = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8;
  513. $colFirst = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8;
  514. $colLast = ord($this->data[$spos + $length - 2]) | ord($this->data[$spos + $length - 1])<<8;
  515. $columns = $colLast - $colFirst + 1;
  516. $tmppos = $spos+4;
  517. for ($i = 0; $i < $columns; $i++) {
  518. $numValue = $this->_GetIEEE754($this->_GetInt4d($this->data, $tmppos + 2));
  519. if ($this->isDate($tmppos-4)) {
  520. list($string, $raw) = $this->createDate($numValue);
  521. }else{
  522. $raw = $numValue;
  523. if (isset($this->_columnsFormat[$colFirst + $i + 1])){
  524. $this->curformat = $this->_columnsFormat[$colFirst + $i + 1];
  525. }
  526. $string = sprintf($this->curformat, $numValue * $this->multiplier);
  527. }
  528. //$rec['rknumbers'][$i]['xfindex'] = ord($rec['data'][$pos]) | ord($rec['data'][$pos+1]) << 8;
  529. $tmppos += 6;
  530. $this->addcell($row, $colFirst + $i, $string, $raw);
  531. //echo "MULRK $row ".($colFirst + $i)." $string\n";
  532. }
  533. //MulRKRecord($r);
  534. // Get the individual cell records from the multiple record
  535. //$num = ;
  536. break;
  537. case Spreadsheet_Excel_Reader_Type_NUMBER:
  538. $row = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8;
  539. $column = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8;
  540. $tmp = unpack("ddouble", substr($this->data, $spos + 6, 8)); // It machine machine dependent
  541. if ($this->isDate($spos)) {
  542. list($string, $raw) = $this->createDate($tmp['double']);
  543. // $this->addcell(DateRecord($r, 1));
  544. }else{
  545. //$raw = $tmp[''];
  546. if (isset($this->_columnsFormat[$column + 1])){
  547. $this->curformat = $this->_columnsFormat[$column + 1];
  548. }
  549. $raw = $this->createNumber($spos);
  550. $string = sprintf($this->curformat, $raw * $this->multiplier);
  551. // $this->addcell(NumberRecord($r));
  552. }
  553. $this->addcell($row, $column, $string, $raw);
  554. //echo "Number $row $column $string\n";
  555. break;
  556. case Spreadsheet_Excel_Reader_Type_FORMULA:
  557. case Spreadsheet_Excel_Reader_Type_FORMULA2:
  558. $row = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8;
  559. $column = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8;
  560. if ((ord($this->data[$spos+6])==0) && (ord($this->data[$spos+12])==255) && (ord($this->data[$spos+13])==255)) {
  561. //String formula. Result follows in a STRING record
  562. //echo "FORMULA $row $column Formula with a string<br>\n";
  563. } elseif ((ord($this->data[$spos+6])==1) && (ord($this->data[$spos+12])==255) && (ord($this->data[$spos+13])==255)) {
  564. //Boolean formula. Result is in +2; 0=false,1=true
  565. } elseif ((ord($this->data[$spos+6])==2) && (ord($this->data[$spos+12])==255) && (ord($this->data[$spos+13])==255)) {
  566. //Error formula. Error code is in +2;
  567. } elseif ((ord($this->data[$spos+6])==3) && (ord($this->data[$spos+12])==255) && (ord($this->data[$spos+13])==255)) {
  568. //Formula result is a null string.
  569. } else {
  570. // result is a number, so first 14 bytes are just like a _NUMBER record
  571. $tmp = unpack("ddouble", substr($this->data, $spos + 6, 8)); // It machine machine dependent
  572. if ($this->isDate($spos)) {
  573. list($string, $raw) = $this->createDate($tmp['double']);
  574. // $this->addcell(DateRecord($r, 1));
  575. }else{
  576. //$raw = $tmp[''];
  577. if (isset($this->_columnsFormat[$column + 1])){
  578. $this->curformat = $this->_columnsFormat[$column + 1];
  579. }
  580. $raw = $this->createNumber($spos);
  581. $string = sprintf($this->curformat, $raw * $this->multiplier);
  582. // $this->addcell(NumberRecord($r));
  583. }
  584. $this->addcell($row, $column, $string, $raw);
  585. //echo "Number $row $column $string\n";
  586. }
  587. break;
  588. case Spreadsheet_Excel_Reader_Type_BOOLERR:
  589. $row = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8;
  590. $column = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8;
  591. $string = ord($this->data[$spos+6]);
  592. $this->addcell($row, $column, $string);
  593. //echo 'Type_BOOLERR '."\n";
  594. break;
  595. case Spreadsheet_Excel_Reader_Type_ROW:
  596. case Spreadsheet_Excel_Reader_Type_DBCELL:
  597. case Spreadsheet_Excel_Reader_Type_MULBLANK:
  598. break;
  599. case Spreadsheet_Excel_Reader_Type_LABEL:
  600. $row = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8;
  601. $column = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8;
  602. $this->addcell($row, $column, substr($this->data, $spos + 8, ord($this->data[$spos + 6]) | ord($this->data[$spos + 7])<<8));
  603. // $this->addcell(LabelRecord($r));
  604. break;
  605. case Spreadsheet_Excel_Reader_Type_EOF:
  606. $cont = false;
  607. break;
  608. default:
  609. //echo ' unknown :'.base_convert($r['code'],10,16)."\n";
  610. break;
  611. }
  612. $spos += $length;
  613. }
  614. if (!isset($this->sheets[$this->sn]['numRows']))
  615. $this->sheets[$this->sn]['numRows'] = $this->sheets[$this->sn]['maxrow'];
  616. if (!isset($this->sheets[$this->sn]['numCols']))
  617. $this->sheets[$this->sn]['numCols'] = $this->sheets[$this->sn]['maxcol'];
  618. }
  619. function isDate($spos){
  620. //$xfindex = GetInt2d(, 4);
  621. $xfindex = ord($this->data[$spos+4]) | ord($this->data[$spos+5]) << 8;
  622. //echo 'check is date '.$xfindex.' '.$this->formatRecords['xfrecords'][$xfindex]['type']."\n";
  623. //var_dump($this->formatRecords['xfrecords'][$xfindex]);
  624. if ($this->formatRecords['xfrecords'][$xfindex]['type'] == 'date') {
  625. $this->curformat = $this->formatRecords['xfrecords'][$xfindex]['format'];
  626. $this->rectype = 'date';
  627. return true;
  628. } else {
  629. if ($this->formatRecords['xfrecords'][$xfindex]['type'] == 'number') {
  630. $this->curformat = $this->formatRecords['xfrecords'][$xfindex]['format'];
  631. $this->rectype = 'number';
  632. if (($xfindex == 0x9) || ($xfindex == 0xa)){
  633. $this->multiplier = 100;
  634. }
  635. }else{
  636. $this->curformat = $this->_defaultFormat;
  637. $this->rectype = 'unknown';
  638. }
  639. return false;
  640. }
  641. }
  642. function createDate($numValue){
  643. if ($numValue > 1){
  644. $utcDays = $numValue - ($this->nineteenFour ? Spreadsheet_Excel_Reader_utcOffsetDays1904 : Spreadsheet_Excel_Reader_utcOffsetDays);
  645. $utcValue = round($utcDays * Spreadsheet_Excel_Reader_msInADay);
  646. $string = date ($this->curformat, $utcValue);
  647. $raw = $utcValue;
  648. }else{
  649. $raw = $numValue;
  650. $hours = floor($numValue * 24);
  651. $mins = floor($numValue * 24 * 60) - $hours * 60;
  652. $secs = floor($numValue * Spreadsheet_Excel_Reader_msInADay) - $hours * 60 * 60 - $mins * 60;
  653. $string = date ($this->curformat, mktime($hours, $mins, $secs));
  654. }
  655. return array($string, $raw);
  656. }
  657. function createNumber($spos){
  658. $rknumhigh = $this->_GetInt4d($this->data, $spos + 10);
  659. $rknumlow = $this->_GetInt4d($this->data, $spos + 6);
  660. //for ($i=0; $i<8; $i++) { echo ord($this->data[$i+$spos+6]) . " "; } echo "<br>";
  661. $sign = ($rknumhigh & 0x80000000) >> 31;
  662. $exp = ($rknumhigh & 0x7ff00000) >> 20;
  663. $mantissa = (0x100000 | ($rknumhigh & 0x000fffff));
  664. $mantissalow1 = ($rknumlow & 0x80000000) >> 31;
  665. $mantissalow2 = ($rknumlow & 0x7fffffff);
  666. $value = $mantissa / pow( 2 , (20- ($exp - 1023)));
  667. if ($mantissalow1 != 0) $value += 1 / pow (2 , (21 - ($exp - 1023)));
  668. $value += $mantissalow2 / pow (2 , (52 - ($exp - 1023)));
  669. //echo "Sign = $sign, Exp = $exp, mantissahighx = $mantissa, mantissalow1 = $mantissalow1, mantissalow2 = $mantissalow2<br>\n";
  670. if ($sign) {$value = -1 * $value;}
  671. return $value;
  672. }
  673. function addcell($row, $col, $string, $raw = ''){
  674. //echo "ADD cel $row-$col $string\n";
  675. $this->sheets[$this->sn]['maxrow'] = max($this->sheets[$this->sn]['maxrow'], $row + $this->_rowoffset);
  676. $this->sheets[$this->sn]['maxcol'] = max($this->sheets[$this->sn]['maxcol'], $col + $this->_coloffset);
  677. $this->sheets[$this->sn]['cells'][$row + $this->_rowoffset][$col + $this->_coloffset] = $string;
  678. if ($raw)
  679. $this->sheets[$this->sn]['cellsInfo'][$row + $this->_rowoffset][$col + $this->_coloffset]['raw'] = $raw;
  680. if (isset($this->rectype))
  681. $this->sheets[$this->sn]['cellsInfo'][$row + $this->_rowoffset][$col + $this->_coloffset]['type'] = $this->rectype;
  682. }
  683. function _GetIEEE754($rknum){
  684. if (($rknum & 0x02) != 0) {
  685. $value = $rknum >> 2;
  686. } else {
  687. //mmp
  688. // first comment out the previously existing 7 lines of code here
  689. // $tmp = unpack("d", pack("VV", 0, ($rknum & 0xfffffffc)));
  690. // //$value = $tmp[''];
  691. // if (array_key_exists(1, $tmp)) {
  692. // $value = $tmp[1];
  693. // } else {
  694. // $value = $tmp[''];
  695. // }
  696. // I got my info on IEEE754 encoding from
  697. // http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html
  698. // The RK format calls for using only the most significant 30 bits of the
  699. // 64 bit floating point value. The other 34 bits are assumed to be 0
  700. // So, we use the upper 30 bits of $rknum as follows...
  701. $sign = ($rknum & 0x80000000) >> 31;
  702. $exp = ($rknum & 0x7ff00000) >> 20;
  703. $mantissa = (0x100000 | ($rknum & 0x000ffffc));
  704. $value = $mantissa / pow( 2 , (20- ($exp - 1023)));
  705. if ($sign) {$value = -1 * $value;}
  706. //end of changes by mmp
  707. }
  708. if (($rknum & 0x01) != 0) {
  709. $value /= 100;
  710. }
  711. return $value;
  712. }
  713. function _encodeUTF16($string){
  714. $result = $string;
  715. if ($this->_defaultEncoding){
  716. switch ($this->_encoderFunction){
  717. case 'iconv' : $result = iconv('UTF-16LE', $this->_defaultEncoding, $string);
  718. break;
  719. case 'mb_convert_encoding' : $result = mb_convert_encoding($string, $this->_defaultEncoding, 'UTF-16LE' );
  720. break;
  721. }
  722. }
  723. return $result;
  724. }
  725. function _GetInt4d($data, $pos) {
  726. return ord($data[$pos]) | (ord($data[$pos+1]) << 8) | (ord($data[$pos+2]) << 16) | (ord($data[$pos+3]) << 24);
  727. }
  728. }
  729. ?>