reader.php 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * A class for reading Microsoft Excel Spreadsheets.
  5. *
  6. * Originally developed by Vadim Tkachenko under the name PHPExcelReader.
  7. * (http://sourceforge.net/projects/phpexcelreader)
  8. * Based on the Java version by Andy Khan (http://www.andykhan.com). Now
  9. * maintained by David Sanders. Reads only Biff 7 and Biff 8 formats.
  10. *
  11. * PHP versions 4 and 5
  12. *
  13. * LICENSE: This source file is subject to version 3.0 of the PHP license
  14. * that is available through the world-wide-web at the following URI:
  15. * http://www.php.net/license/3_0.txt. If you did not receive a copy of
  16. * the PHP License and are unable to obtain it through the web, please
  17. * send a note to license@php.net so we can mail you a copy immediately.
  18. *
  19. * @category Spreadsheet
  20. * @package Spreadsheet_Excel_Reader
  21. * @author Vadim Tkachenko <vt@apachephp.com>
  22. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  23. * @version CVS: $Id: reader.php 19 2007-03-13 12:42:41Z shangxiao $
  24. * @link http://pear.php.net/package/Spreadsheet_Excel_Reader
  25. * @see OLE, Spreadsheet_Excel_Writer
  26. */
  27. /**
  28. * Code
  29. */
  30. //require_once 'PEAR.php';
  31. require_once 'OLERead.inc';
  32. //require_once 'OLE.php';
  33. define('SPREADSHEET_EXCEL_READER_BIFF8', 0x600);
  34. define('SPREADSHEET_EXCEL_READER_BIFF7', 0x500);
  35. define('SPREADSHEET_EXCEL_READER_WORKBOOKGLOBALS', 0x5);
  36. define('SPREADSHEET_EXCEL_READER_WORKSHEET', 0x10);
  37. define('SPREADSHEET_EXCEL_READER_TYPE_BOF', 0x809);
  38. define('SPREADSHEET_EXCEL_READER_TYPE_EOF', 0x0a);
  39. define('SPREADSHEET_EXCEL_READER_TYPE_BOUNDSHEET', 0x85);
  40. define('SPREADSHEET_EXCEL_READER_TYPE_DIMENSION', 0x200);
  41. define('SPREADSHEET_EXCEL_READER_TYPE_ROW', 0x208);
  42. define('SPREADSHEET_EXCEL_READER_TYPE_DBCELL', 0xd7);
  43. define('SPREADSHEET_EXCEL_READER_TYPE_FILEPASS', 0x2f);
  44. define('SPREADSHEET_EXCEL_READER_TYPE_NOTE', 0x1c);
  45. define('SPREADSHEET_EXCEL_READER_TYPE_TXO', 0x1b6);
  46. define('SPREADSHEET_EXCEL_READER_TYPE_RK', 0x7e);
  47. define('SPREADSHEET_EXCEL_READER_TYPE_RK2', 0x27e);
  48. define('SPREADSHEET_EXCEL_READER_TYPE_MULRK', 0xbd);
  49. define('SPREADSHEET_EXCEL_READER_TYPE_MULBLANK', 0xbe);
  50. define('SPREADSHEET_EXCEL_READER_TYPE_INDEX', 0x20b);
  51. define('SPREADSHEET_EXCEL_READER_TYPE_SST', 0xfc);
  52. define('SPREADSHEET_EXCEL_READER_TYPE_EXTSST', 0xff);
  53. define('SPREADSHEET_EXCEL_READER_TYPE_CONTINUE', 0x3c);
  54. define('SPREADSHEET_EXCEL_READER_TYPE_LABEL', 0x204);
  55. define('SPREADSHEET_EXCEL_READER_TYPE_LABELSST', 0xfd);
  56. define('SPREADSHEET_EXCEL_READER_TYPE_NUMBER', 0x203);
  57. define('SPREADSHEET_EXCEL_READER_TYPE_NAME', 0x18);
  58. define('SPREADSHEET_EXCEL_READER_TYPE_ARRAY', 0x221);
  59. define('SPREADSHEET_EXCEL_READER_TYPE_STRING', 0x207);
  60. define('SPREADSHEET_EXCEL_READER_TYPE_FORMULA', 0x406);
  61. define('SPREADSHEET_EXCEL_READER_TYPE_FORMULA2', 0x6);
  62. define('SPREADSHEET_EXCEL_READER_TYPE_FORMAT', 0x41e);
  63. define('SPREADSHEET_EXCEL_READER_TYPE_XF', 0xe0);
  64. define('SPREADSHEET_EXCEL_READER_TYPE_BOOLERR', 0x205);
  65. define('SPREADSHEET_EXCEL_READER_TYPE_UNKNOWN', 0xffff);
  66. define('SPREADSHEET_EXCEL_READER_TYPE_NINETEENFOUR', 0x22);
  67. define('SPREADSHEET_EXCEL_READER_TYPE_MERGEDCELLS', 0xE5);
  68. define('SPREADSHEET_EXCEL_READER_UTCOFFSETDAYS' , 25569);
  69. define('SPREADSHEET_EXCEL_READER_UTCOFFSETDAYS1904', 24107);
  70. define('SPREADSHEET_EXCEL_READER_MSINADAY', 86400);
  71. //define('SPREADSHEET_EXCEL_READER_MSINADAY', 24 * 60 * 60);
  72. //define('SPREADSHEET_EXCEL_READER_DEF_NUM_FORMAT', "%.2f");
  73. define('SPREADSHEET_EXCEL_READER_DEF_NUM_FORMAT', "%s");
  74. /*
  75. * Place includes, constant defines and $_GLOBAL settings here.
  76. * Make sure they have appropriate docblocks to avoid phpDocumentor
  77. * construing they are documented by the page-level docblock.
  78. */
  79. /**
  80. * A class for reading Microsoft Excel Spreadsheets.
  81. *
  82. * Originally developed by Vadim Tkachenko under the name PHPExcelReader.
  83. * (http://sourceforge.net/projects/phpexcelreader)
  84. * Based on the Java version by Andy Khan (http://www.andykhan.com). Now
  85. * maintained by David Sanders. Reads only Biff 7 and Biff 8 formats.
  86. *
  87. * @category Spreadsheet
  88. * @package Spreadsheet_Excel_Reader
  89. * @author Vadim Tkachenko <vt@phpapache.com>
  90. * @copyright 1997-2005 The PHP Group
  91. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  92. * @version Release: @package_version@
  93. * @link http://pear.php.net/package/PackageName
  94. * @see OLE, Spreadsheet_Excel_Writer
  95. */
  96. class Spreadsheet_Excel_Reader
  97. {
  98. /**
  99. * Array of worksheets found
  100. *
  101. * @var array
  102. * @access public
  103. */
  104. var $boundsheets = array();
  105. /**
  106. * Array of format records found
  107. *
  108. * @var array
  109. * @access public
  110. */
  111. var $formatRecords = array();
  112. /**
  113. * todo
  114. *
  115. * @var array
  116. * @access public
  117. */
  118. var $sst = array();
  119. /**
  120. * Array of worksheets
  121. *
  122. * The data is stored in 'cells' and the meta-data is stored in an array
  123. * called 'cellsInfo'
  124. *
  125. * Example:
  126. *
  127. * $sheets --> 'cells' --> row --> column --> Interpreted value
  128. * --> 'cellsInfo' --> row --> column --> 'type' - Can be 'date', 'number', or 'unknown'
  129. * --> 'raw' - The raw data that Excel stores for that data cell
  130. *
  131. * @var array
  132. * @access public
  133. */
  134. var $sheets = array();
  135. /**
  136. * The data returned by OLE
  137. *
  138. * @var string
  139. * @access public
  140. */
  141. var $data;
  142. /**
  143. * OLE object for reading the file
  144. *
  145. * @var OLE object
  146. * @access private
  147. */
  148. var $_ole;
  149. /**
  150. * Default encoding
  151. *
  152. * @var string
  153. * @access private
  154. */
  155. var $_defaultEncoding;
  156. /**
  157. * Default number format
  158. *
  159. * @var integer
  160. * @access private
  161. */
  162. var $_defaultFormat = SPREADSHEET_EXCEL_READER_DEF_NUM_FORMAT;
  163. /**
  164. * todo
  165. * List of formats to use for each column
  166. *
  167. * @var array
  168. * @access private
  169. */
  170. var $_columnsFormat = array();
  171. /**
  172. * todo
  173. *
  174. * @var integer
  175. * @access private
  176. */
  177. var $_rowoffset = 1;
  178. /**
  179. * todo
  180. *
  181. * @var integer
  182. * @access private
  183. */
  184. var $_coloffset = 1;
  185. /**
  186. * List of default date formats used by Excel
  187. *
  188. * @var array
  189. * @access public
  190. */
  191. var $dateFormats = array (
  192. 0xe => "d/m/Y",
  193. 0xf => "d-M-Y",
  194. 0x10 => "d-M",
  195. 0x11 => "M-Y",
  196. 0x12 => "h:i a",
  197. 0x13 => "h:i:s a",
  198. 0x14 => "H:i",
  199. 0x15 => "H:i:s",
  200. 0x16 => "d/m/Y H:i",
  201. 0x2d => "i:s",
  202. 0x2e => "H:i:s",
  203. 0x2f => "i:s.S");
  204. /**
  205. * Default number formats used by Excel
  206. *
  207. * @var array
  208. * @access public
  209. */
  210. var $numberFormats = array(
  211. 0x1 => "%1.0f", // "0"
  212. 0x2 => "%1.2f", // "0.00",
  213. 0x3 => "%1.0f", //"#,##0",
  214. 0x4 => "%1.2f", //"#,##0.00",
  215. 0x5 => "%1.0f", /*"$#,##0;($#,##0)",*/
  216. 0x6 => '$%1.0f', /*"$#,##0;($#,##0)",*/
  217. 0x7 => '$%1.2f', //"$#,##0.00;($#,##0.00)",
  218. 0x8 => '$%1.2f', //"$#,##0.00;($#,##0.00)",
  219. 0x9 => '%1.0f%%', // "0%"
  220. 0xa => '%1.2f%%', // "0.00%"
  221. 0xb => '%1.2f', // 0.00E00",
  222. 0x25 => '%1.0f', // "#,##0;(#,##0)",
  223. 0x26 => '%1.0f', //"#,##0;(#,##0)",
  224. 0x27 => '%1.2f', //"#,##0.00;(#,##0.00)",
  225. 0x28 => '%1.2f', //"#,##0.00;(#,##0.00)",
  226. 0x29 => '%1.0f', //"#,##0;(#,##0)",
  227. 0x2a => '$%1.0f', //"$#,##0;($#,##0)",
  228. 0x2b => '%1.2f', //"#,##0.00;(#,##0.00)",
  229. 0x2c => '$%1.2f', //"$#,##0.00;($#,##0.00)",
  230. 0x30 => '%1.0f'); //"##0.0E0";
  231. // }}}
  232. // {{{ Spreadsheet_Excel_Reader()
  233. /**
  234. * Constructor
  235. *
  236. * Some basic initialisation
  237. */
  238. function Spreadsheet_Excel_Reader()
  239. {
  240. $this->_ole =& new OLERead();
  241. $this->setUTFEncoder('iconv');
  242. }
  243. // }}}
  244. // {{{ setOutputEncoding()
  245. /**
  246. * Set the encoding method
  247. *
  248. * @param string Encoding to use
  249. * @access public
  250. */
  251. function setOutputEncoding($encoding)
  252. {
  253. $this->_defaultEncoding = $encoding;
  254. }
  255. // }}}
  256. // {{{ setUTFEncoder()
  257. /**
  258. * $encoder = 'iconv' or 'mb'
  259. * set iconv if you would like use 'iconv' for encode UTF-16LE to your encoding
  260. * set mb if you would like use 'mb_convert_encoding' for encode UTF-16LE to your encoding
  261. *
  262. * @access public
  263. * @param string Encoding type to use. Either 'iconv' or 'mb'
  264. */
  265. function setUTFEncoder($encoder = 'iconv')
  266. {
  267. $this->_encoderFunction = '';
  268. if ($encoder == 'iconv') {
  269. $this->_encoderFunction = function_exists('iconv') ? 'iconv' : '';
  270. } elseif ($encoder == 'mb') {
  271. $this->_encoderFunction = function_exists('mb_convert_encoding') ?
  272. 'mb_convert_encoding' :
  273. '';
  274. }
  275. }
  276. // }}}
  277. // {{{ setRowColOffset()
  278. /**
  279. * todo
  280. *
  281. * @access public
  282. * @param offset
  283. */
  284. function setRowColOffset($iOffset)
  285. {
  286. $this->_rowoffset = $iOffset;
  287. $this->_coloffset = $iOffset;
  288. }
  289. // }}}
  290. // {{{ setDefaultFormat()
  291. /**
  292. * Set the default number format
  293. *
  294. * @access public
  295. * @param Default format
  296. */
  297. function setDefaultFormat($sFormat)
  298. {
  299. $this->_defaultFormat = $sFormat;
  300. }
  301. // }}}
  302. // {{{ setColumnFormat()
  303. /**
  304. * Force a column to use a certain format
  305. *
  306. * @access public
  307. * @param integer Column number
  308. * @param string Format
  309. */
  310. function setColumnFormat($column, $sFormat)
  311. {
  312. $this->_columnsFormat[$column] = $sFormat;
  313. }
  314. // }}}
  315. // {{{ read()
  316. /**
  317. * Read the spreadsheet file using OLE, then parse
  318. *
  319. * @access public
  320. * @param filename
  321. * @todo return a valid value
  322. */
  323. function read($sFileName)
  324. {
  325. /*
  326. require_once 'OLE.php';
  327. $ole = new OLE();
  328. $ole->read($sFileName);
  329. foreach ($ole->_list as $i => $pps) {
  330. if (($pps->Name == 'Workbook' || $pps->Name == 'Book') &&
  331. $pps->Size >= SMALL_BLOCK_THRESHOLD) {
  332. $this->data = $ole->getData($i, 0, $ole->getDataLength($i));
  333. } elseif ($pps->Name == 'Root Entry') {
  334. $this->data = $ole->getData($i, 0, $ole->getDataLength($i));
  335. }
  336. //var_dump(strlen($ole->getData($i, 0, $ole->getDataLength($i))), $pps->Name, md5($this->data), $ole->getDataLength($i));
  337. }
  338. //exit;
  339. $this->_parse();
  340. return sizeof($this->sheets) > 0;
  341. */
  342. $res = $this->_ole->read($sFileName);
  343. // oops, something goes wrong (Darko Miljanovic)
  344. if($res === false) {
  345. // check error code
  346. if($this->_ole->error == 1) {
  347. // bad file
  348. //die('The filename ' . $sFileName . ' is not readable');
  349. Display::display_header('');
  350. Display::display_error_message(get_lang('XLSFileNotValid'));
  351. Display::display_footer();
  352. exit;
  353. }
  354. // check other error codes here (eg bad fileformat, etc...)
  355. }
  356. $this->data = $this->_ole->getWorkBook();
  357. /*
  358. $res = $this->_ole->read($sFileName);
  359. if ($this->isError($res)) {
  360. // var_dump($res);
  361. return $this->raiseError($res);
  362. }
  363. $total = $this->_ole->ppsTotal();
  364. for ($i = 0; $i < $total; $i++) {
  365. if ($this->_ole->isFile($i)) {
  366. $type = unpack("v", $this->_ole->getData($i, 0, 2));
  367. if ($type[''] == 0x0809) { // check if it's a BIFF stream
  368. $this->_index = $i;
  369. $this->data = $this->_ole->getData($i, 0, $this->_ole->getDataLength($i));
  370. break;
  371. }
  372. }
  373. }
  374. if ($this->_index === null) {
  375. return $this->raiseError("$file doesn't seem to be an Excel file");
  376. }
  377. */
  378. //echo "data =".$this->data;
  379. //$this->readRecords();
  380. $this->_parse();
  381. }
  382. // }}}
  383. // {{{ _parse()
  384. /**
  385. * Parse a workbook
  386. *
  387. * @access private
  388. * @return bool
  389. */
  390. function _parse()
  391. {
  392. $pos = 0;
  393. $code = ord($this->data[$pos]) | ord($this->data[$pos+1])<<8;
  394. $length = ord($this->data[$pos+2]) | ord($this->data[$pos+3])<<8;
  395. $version = ord($this->data[$pos + 4]) | ord($this->data[$pos + 5])<<8;
  396. $substreamType = ord($this->data[$pos + 6]) | ord($this->data[$pos + 7])<<8;
  397. //echo "Start parse code=".base_convert($code,10,16)." version=".base_convert($version,10,16)." substreamType=".base_convert($substreamType,10,16).""."\n";
  398. if (($version != SPREADSHEET_EXCEL_READER_BIFF8) &&
  399. ($version != SPREADSHEET_EXCEL_READER_BIFF7)) {
  400. return false;
  401. }
  402. if ($substreamType != SPREADSHEET_EXCEL_READER_WORKBOOKGLOBALS){
  403. return false;
  404. }
  405. //print_r($rec);
  406. $pos += $length + 4;
  407. $code = ord($this->data[$pos]) | ord($this->data[$pos+1])<<8;
  408. $length = ord($this->data[$pos+2]) | ord($this->data[$pos+3])<<8;
  409. while ($code != SPREADSHEET_EXCEL_READER_TYPE_EOF) {
  410. switch ($code) {
  411. case SPREADSHEET_EXCEL_READER_TYPE_SST:
  412. //echo "Type_SST\n";
  413. $spos = $pos + 4;
  414. $limitpos = $spos + $length;
  415. $uniqueStrings = $this->_GetInt4d($this->data, $spos+4);
  416. $spos += 8;
  417. for ($i = 0; $i < $uniqueStrings; $i++) {
  418. // Read in the number of characters
  419. if ($spos == $limitpos) {
  420. $opcode = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8;
  421. $conlength = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8;
  422. if ($opcode != 0x3c) {
  423. return -1;
  424. }
  425. $spos += 4;
  426. $limitpos = $spos + $conlength;
  427. }
  428. $numChars = ord($this->data[$spos]) | (ord($this->data[$spos+1]) << 8);
  429. //echo "i = $i pos = $pos numChars = $numChars ";
  430. $spos += 2;
  431. $optionFlags = ord($this->data[$spos]);
  432. $spos++;
  433. $asciiEncoding = (($optionFlags & 0x01) == 0) ;
  434. $extendedString = ( ($optionFlags & 0x04) != 0);
  435. // See if string contains formatting information
  436. $richString = ( ($optionFlags & 0x08) != 0);
  437. if ($richString) {
  438. // Read in the crun
  439. $formattingRuns = ord($this->data[$spos]) | (ord($this->data[$spos+1]) << 8);
  440. $spos += 2;
  441. }
  442. if ($extendedString) {
  443. // Read in cchExtRst
  444. $extendedRunLength = $this->_GetInt4d($this->data, $spos);
  445. $spos += 4;
  446. }
  447. $len = ($asciiEncoding)? $numChars : $numChars*2;
  448. if ($spos + $len < $limitpos) {
  449. $retstr = substr($this->data, $spos, $len);
  450. $spos += $len;
  451. }else{
  452. // found countinue
  453. $retstr = substr($this->data, $spos, $limitpos - $spos);
  454. $bytesRead = $limitpos - $spos;
  455. $charsLeft = $numChars - (($asciiEncoding) ? $bytesRead : ($bytesRead / 2));
  456. $spos = $limitpos;
  457. while ($charsLeft > 0){
  458. $opcode = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8;
  459. $conlength = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8;
  460. if ($opcode != 0x3c) {
  461. return -1;
  462. }
  463. $spos += 4;
  464. $limitpos = $spos + $conlength;
  465. $option = ord($this->data[$spos]);
  466. $spos += 1;
  467. if ($asciiEncoding && ($option == 0)) {
  468. $len = min($charsLeft, $limitpos - $spos); // min($charsLeft, $conlength);
  469. $retstr .= substr($this->data, $spos, $len);
  470. $charsLeft -= $len;
  471. $asciiEncoding = true;
  472. }elseif (!$asciiEncoding && ($option != 0)){
  473. $len = min($charsLeft * 2, $limitpos - $spos); // min($charsLeft, $conlength);
  474. $retstr .= substr($this->data, $spos, $len);
  475. $charsLeft -= $len/2;
  476. $asciiEncoding = false;
  477. }elseif (!$asciiEncoding && ($option == 0)) {
  478. // Bummer - the string starts off as Unicode, but after the
  479. // continuation it is in straightforward ASCII encoding
  480. $len = min($charsLeft, $limitpos - $spos); // min($charsLeft, $conlength);
  481. for ($j = 0; $j < $len; $j++) {
  482. $retstr .= $this->data[$spos + $j].chr(0);
  483. }
  484. $charsLeft -= $len;
  485. $asciiEncoding = false;
  486. }else{
  487. $newstr = '';
  488. for ($j = 0; $j < strlen($retstr); $j++) {
  489. $newstr = $retstr[$j].chr(0);
  490. }
  491. $retstr = $newstr;
  492. $len = min($charsLeft * 2, $limitpos - $spos); // min($charsLeft, $conlength);
  493. $retstr .= substr($this->data, $spos, $len);
  494. $charsLeft -= $len/2;
  495. $asciiEncoding = false;
  496. //echo "Izavrat\n";
  497. }
  498. $spos += $len;
  499. }
  500. }
  501. //We check the string before send it check the _encodeUTF16
  502. //$retstr = ($asciiEncoding) ? $retstr : $this->_encodeUTF16($retstr);
  503. $retstr = ($asciiEncoding) ? $this->_encodeUTF16($retstr, true) : $this->_encodeUTF16($retstr);
  504. // echo "Str $i = $retstr\n";
  505. if ($richString){
  506. $spos += 4 * $formattingRuns;
  507. }
  508. // For extended strings, skip over the extended string data
  509. if ($extendedString) {
  510. $spos += $extendedRunLength;
  511. }
  512. //if ($retstr == 'Derby'){
  513. // echo "bb\n";
  514. //}
  515. $this->sst[]=$retstr;
  516. }
  517. /*$continueRecords = array();
  518. while ($this->getNextCode() == Type_CONTINUE) {
  519. $continueRecords[] = &$this->nextRecord();
  520. }
  521. //echo " 1 Type_SST\n";
  522. $this->shareStrings = new SSTRecord($r, $continueRecords);
  523. //print_r($this->shareStrings->strings);
  524. */
  525. // echo 'SST read: '.($time_end-$time_start)."\n";
  526. break;
  527. case SPREADSHEET_EXCEL_READER_TYPE_FILEPASS:
  528. return false;
  529. break;
  530. case SPREADSHEET_EXCEL_READER_TYPE_NAME:
  531. //echo "Type_NAME\n";
  532. break;
  533. case SPREADSHEET_EXCEL_READER_TYPE_FORMAT:
  534. $indexCode = ord($this->data[$pos+4]) | ord($this->data[$pos+5]) << 8;
  535. if ($version == SPREADSHEET_EXCEL_READER_BIFF8) {
  536. $numchars = ord($this->data[$pos+6]) | ord($this->data[$pos+7]) << 8;
  537. if (ord($this->data[$pos+8]) == 0){
  538. $formatString = substr($this->data, $pos+9, $numchars);
  539. } else {
  540. $formatString = substr($this->data, $pos+9, $numchars*2);
  541. }
  542. } else {
  543. $numchars = ord($this->data[$pos+6]);
  544. $formatString = substr($this->data, $pos+7, $numchars*2);
  545. }
  546. $this->formatRecords[$indexCode] = $formatString;
  547. // echo "Type.FORMAT\n";
  548. break;
  549. case SPREADSHEET_EXCEL_READER_TYPE_XF:
  550. //global $dateFormats, $numberFormats;
  551. $indexCode = ord($this->data[$pos+6]) | ord($this->data[$pos+7]) << 8;
  552. //echo "\nType.XF ".count($this->formatRecords['xfrecords'])." $indexCode ";
  553. if (array_key_exists($indexCode, $this->dateFormats)) {
  554. //echo "isdate ".$dateFormats[$indexCode];
  555. $this->formatRecords['xfrecords'][] = array(
  556. 'type' => 'date',
  557. 'format' => $this->dateFormats[$indexCode]
  558. );
  559. }elseif (array_key_exists($indexCode, $this->numberFormats)) {
  560. //echo "isnumber ".$this->numberFormats[$indexCode];
  561. $this->formatRecords['xfrecords'][] = array(
  562. 'type' => 'number',
  563. 'format' => $this->numberFormats[$indexCode]
  564. );
  565. }else{
  566. $isdate = FALSE;
  567. if ($indexCode > 0){
  568. if (isset($this->formatRecords[$indexCode]))
  569. $formatstr = $this->formatRecords[$indexCode];
  570. //echo '.other.';
  571. //echo "\ndate-time=$formatstr=\n";
  572. if ($formatstr)
  573. if (preg_match("/[^hmsday\/\-:\s]/i", $formatstr) == 0) { // found day and time format
  574. $isdate = TRUE;
  575. $formatstr = str_replace('mm', 'i', $formatstr);
  576. $formatstr = str_replace('h', 'H', $formatstr);
  577. //echo "\ndate-time $formatstr \n";
  578. }
  579. }
  580. if ($isdate){
  581. $this->formatRecords['xfrecords'][] = array(
  582. 'type' => 'date',
  583. 'format' => $formatstr,
  584. );
  585. }else{
  586. $this->formatRecords['xfrecords'][] = array(
  587. 'type' => 'other',
  588. 'format' => '',
  589. 'code' => $indexCode
  590. );
  591. }
  592. }
  593. //echo "\n";
  594. break;
  595. case SPREADSHEET_EXCEL_READER_TYPE_NINETEENFOUR:
  596. //echo "Type.NINETEENFOUR\n";
  597. $this->nineteenFour = (ord($this->data[$pos+4]) == 1);
  598. break;
  599. case SPREADSHEET_EXCEL_READER_TYPE_BOUNDSHEET:
  600. //echo "Type.BOUNDSHEET\n";
  601. $rec_offset = $this->_GetInt4d($this->data, $pos+4);
  602. $rec_typeFlag = ord($this->data[$pos+8]);
  603. $rec_visibilityFlag = ord($this->data[$pos+9]);
  604. $rec_length = ord($this->data[$pos+10]);
  605. if ($version == SPREADSHEET_EXCEL_READER_BIFF8){
  606. $chartype = ord($this->data[$pos+11]);
  607. if ($chartype == 0){
  608. $rec_name = substr($this->data, $pos+12, $rec_length);
  609. } else {
  610. $rec_name = $this->_encodeUTF16(substr($this->data, $pos+12, $rec_length*2));
  611. }
  612. }elseif ($version == SPREADSHEET_EXCEL_READER_BIFF7){
  613. $rec_name = substr($this->data, $pos+11, $rec_length);
  614. }
  615. $this->boundsheets[] = array('name'=>$rec_name,
  616. 'offset'=>$rec_offset);
  617. break;
  618. }
  619. //echo "Code = ".base_convert($r['code'],10,16)."\n";
  620. $pos += $length + 4;
  621. $code = ord($this->data[$pos]) | ord($this->data[$pos+1])<<8;
  622. $length = ord($this->data[$pos+2]) | ord($this->data[$pos+3])<<8;
  623. //$r = &$this->nextRecord();
  624. //echo "1 Code = ".base_convert($r['code'],10,16)."\n";
  625. }
  626. foreach ($this->boundsheets as $key=>$val){
  627. $this->sn = $key;
  628. $this->_parsesheet($val['offset']);
  629. }
  630. return true;
  631. }
  632. /**
  633. * Parse a worksheet
  634. *
  635. * @access private
  636. * @param todo
  637. * @todo fix return codes
  638. */
  639. function _parsesheet($spos)
  640. {
  641. $cont = true;
  642. // read BOF
  643. $code = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8;
  644. $length = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8;
  645. $version = ord($this->data[$spos + 4]) | ord($this->data[$spos + 5])<<8;
  646. $substreamType = ord($this->data[$spos + 6]) | ord($this->data[$spos + 7])<<8;
  647. if (($version != SPREADSHEET_EXCEL_READER_BIFF8) && ($version != SPREADSHEET_EXCEL_READER_BIFF7)) {
  648. return -1;
  649. }
  650. if ($substreamType != SPREADSHEET_EXCEL_READER_WORKSHEET){
  651. return -2;
  652. }
  653. //echo "Start parse code=".base_convert($code,10,16)." version=".base_convert($version,10,16)." substreamType=".base_convert($substreamType,10,16).""."\n";
  654. $spos += $length + 4;
  655. //var_dump($this->formatRecords);
  656. //echo "code $code $length";
  657. while($cont) {
  658. //echo "mem= ".memory_get_usage()."\n";
  659. // $r = &$this->file->nextRecord();
  660. $lowcode = ord($this->data[$spos]);
  661. if ($lowcode == SPREADSHEET_EXCEL_READER_TYPE_EOF) break;
  662. $code = $lowcode | ord($this->data[$spos+1])<<8;
  663. $length = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8;
  664. $spos += 4;
  665. $this->sheets[$this->sn]['maxrow'] = $this->_rowoffset - 1;
  666. $this->sheets[$this->sn]['maxcol'] = $this->_coloffset - 1;
  667. //echo "Code=".base_convert($code,10,16)." $code\n";
  668. unset($this->rectype);
  669. $this->multiplier = 1; // need for format with %
  670. switch ($code) {
  671. case SPREADSHEET_EXCEL_READER_TYPE_DIMENSION:
  672. //echo 'Type_DIMENSION ';
  673. if (!isset($this->numRows)) {
  674. if (($length == 10) || ($version == SPREADSHEET_EXCEL_READER_BIFF7)){
  675. $this->sheets[$this->sn]['numRows'] = ord($this->data[$spos+2]) | ord($this->data[$spos+3]) << 8;
  676. $this->sheets[$this->sn]['numCols'] = ord($this->data[$spos+6]) | ord($this->data[$spos+7]) << 8;
  677. } else {
  678. $this->sheets[$this->sn]['numRows'] = ord($this->data[$spos+4]) | ord($this->data[$spos+5]) << 8;
  679. $this->sheets[$this->sn]['numCols'] = ord($this->data[$spos+10]) | ord($this->data[$spos+11]) << 8;
  680. }
  681. }
  682. //echo 'numRows '.$this->numRows.' '.$this->numCols."\n";
  683. break;
  684. case SPREADSHEET_EXCEL_READER_TYPE_MERGEDCELLS:
  685. $cellRanges = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8;
  686. for ($i = 0; $i < $cellRanges; $i++) {
  687. $fr = ord($this->data[$spos + 8*$i + 2]) | ord($this->data[$spos + 8*$i + 3])<<8;
  688. $lr = ord($this->data[$spos + 8*$i + 4]) | ord($this->data[$spos + 8*$i + 5])<<8;
  689. $fc = ord($this->data[$spos + 8*$i + 6]) | ord($this->data[$spos + 8*$i + 7])<<8;
  690. $lc = ord($this->data[$spos + 8*$i + 8]) | ord($this->data[$spos + 8*$i + 9])<<8;
  691. //$this->sheets[$this->sn]['mergedCells'][] = array($fr + 1, $fc + 1, $lr + 1, $lc + 1);
  692. if ($lr - $fr > 0) {
  693. $this->sheets[$this->sn]['cellsInfo'][$fr+1][$fc+1]['rowspan'] = $lr - $fr + 1;
  694. }
  695. if ($lc - $fc > 0) {
  696. $this->sheets[$this->sn]['cellsInfo'][$fr+1][$fc+1]['colspan'] = $lc - $fc + 1;
  697. }
  698. }
  699. //echo "Merged Cells $cellRanges $lr $fr $lc $fc\n";
  700. break;
  701. case SPREADSHEET_EXCEL_READER_TYPE_RK:
  702. case SPREADSHEET_EXCEL_READER_TYPE_RK2:
  703. //echo 'SPREADSHEET_EXCEL_READER_TYPE_RK'."\n";
  704. $row = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8;
  705. $column = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8;
  706. $rknum = $this->_GetInt4d($this->data, $spos + 6);
  707. $numValue = $this->_GetIEEE754($rknum);
  708. //echo $numValue." ";
  709. if ($this->isDate($spos)) {
  710. list($string, $raw) = $this->createDate($numValue);
  711. }else{
  712. $raw = $numValue;
  713. if (isset($this->_columnsFormat[$column + 1])){
  714. $this->curformat = $this->_columnsFormat[$column + 1];
  715. }
  716. $string = sprintf($this->curformat, $numValue * $this->multiplier);
  717. //$this->addcell(RKRecord($r));
  718. }
  719. $this->addcell($row, $column, $string, $raw);
  720. //echo "Type_RK $row $column $string $raw {$this->curformat}\n";
  721. break;
  722. case SPREADSHEET_EXCEL_READER_TYPE_LABELSST:
  723. $row = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8;
  724. $column = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8;
  725. $xfindex = ord($this->data[$spos+4]) | ord($this->data[$spos+5])<<8;
  726. $index = $this->_GetInt4d($this->data, $spos + 6);
  727. //var_dump($this->sst);
  728. $this->addcell($row, $column, $this->sst[$index]);
  729. //echo "LabelSST $row $column $string\n";
  730. break;
  731. case SPREADSHEET_EXCEL_READER_TYPE_MULRK:
  732. $row = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8;
  733. $colFirst = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8;
  734. $colLast = ord($this->data[$spos + $length - 2]) | ord($this->data[$spos + $length - 1])<<8;
  735. $columns = $colLast - $colFirst + 1;
  736. $tmppos = $spos+4;
  737. for ($i = 0; $i < $columns; $i++) {
  738. $numValue = $this->_GetIEEE754($this->_GetInt4d($this->data, $tmppos + 2));
  739. if ($this->isDate($tmppos-4)) {
  740. list($string, $raw) = $this->createDate($numValue);
  741. }else{
  742. $raw = $numValue;
  743. if (isset($this->_columnsFormat[$colFirst + $i + 1])){
  744. $this->curformat = $this->_columnsFormat[$colFirst + $i + 1];
  745. }
  746. $string = sprintf($this->curformat, $numValue * $this->multiplier);
  747. }
  748. //$rec['rknumbers'][$i]['xfindex'] = ord($rec['data'][$pos]) | ord($rec['data'][$pos+1]) << 8;
  749. $tmppos += 6;
  750. $this->addcell($row, $colFirst + $i, $string, $raw);
  751. //echo "MULRK $row ".($colFirst + $i)." $string\n";
  752. }
  753. //MulRKRecord($r);
  754. // Get the individual cell records from the multiple record
  755. //$num = ;
  756. break;
  757. case SPREADSHEET_EXCEL_READER_TYPE_NUMBER:
  758. $row = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8;
  759. $column = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8;
  760. $tmp = unpack("ddouble", substr($this->data, $spos + 6, 8)); // It machine machine dependent
  761. if ($this->isDate($spos)) {
  762. list($string, $raw) = $this->createDate($tmp['double']);
  763. // $this->addcell(DateRecord($r, 1));
  764. }else{
  765. //$raw = $tmp[''];
  766. if (isset($this->_columnsFormat[$column + 1])){
  767. $this->curformat = $this->_columnsFormat[$column + 1];
  768. }
  769. $raw = $this->createNumber($spos);
  770. $string = sprintf($this->curformat, $raw * $this->multiplier);
  771. // $this->addcell(NumberRecord($r));
  772. }
  773. $this->addcell($row, $column, $string, $raw);
  774. //echo "Number $row $column $string\n";
  775. break;
  776. case SPREADSHEET_EXCEL_READER_TYPE_FORMULA:
  777. case SPREADSHEET_EXCEL_READER_TYPE_FORMULA2:
  778. $row = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8;
  779. $column = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8;
  780. if ((ord($this->data[$spos+6])==0) && (ord($this->data[$spos+12])==255) && (ord($this->data[$spos+13])==255)) {
  781. //String formula. Result follows in a STRING record
  782. //echo "FORMULA $row $column Formula with a string<br>\n";
  783. } elseif ((ord($this->data[$spos+6])==1) && (ord($this->data[$spos+12])==255) && (ord($this->data[$spos+13])==255)) {
  784. //Boolean formula. Result is in +2; 0=false,1=true
  785. } elseif ((ord($this->data[$spos+6])==2) && (ord($this->data[$spos+12])==255) && (ord($this->data[$spos+13])==255)) {
  786. //Error formula. Error code is in +2;
  787. } elseif ((ord($this->data[$spos+6])==3) && (ord($this->data[$spos+12])==255) && (ord($this->data[$spos+13])==255)) {
  788. //Formula result is a null string.
  789. } else {
  790. // result is a number, so first 14 bytes are just like a _NUMBER record
  791. $tmp = unpack("ddouble", substr($this->data, $spos + 6, 8)); // It machine machine dependent
  792. if ($this->isDate($spos)) {
  793. list($string, $raw) = $this->createDate($tmp['double']);
  794. // $this->addcell(DateRecord($r, 1));
  795. }else{
  796. //$raw = $tmp[''];
  797. if (isset($this->_columnsFormat[$column + 1])){
  798. $this->curformat = $this->_columnsFormat[$column + 1];
  799. }
  800. $raw = $this->createNumber($spos);
  801. $string = sprintf($this->curformat, $raw * $this->multiplier);
  802. // $this->addcell(NumberRecord($r));
  803. }
  804. $this->addcell($row, $column, $string, $raw);
  805. //echo "Number $row $column $string\n";
  806. }
  807. break;
  808. case SPREADSHEET_EXCEL_READER_TYPE_BOOLERR:
  809. $row = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8;
  810. $column = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8;
  811. $string = ord($this->data[$spos+6]);
  812. $this->addcell($row, $column, $string);
  813. //echo 'Type_BOOLERR '."\n";
  814. break;
  815. case SPREADSHEET_EXCEL_READER_TYPE_ROW:
  816. case SPREADSHEET_EXCEL_READER_TYPE_DBCELL:
  817. case SPREADSHEET_EXCEL_READER_TYPE_MULBLANK:
  818. break;
  819. case SPREADSHEET_EXCEL_READER_TYPE_LABEL:
  820. $row = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8;
  821. $column = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8;
  822. $this->addcell($row, $column, substr($this->data, $spos + 8, ord($this->data[$spos + 6]) | ord($this->data[$spos + 7])<<8));
  823. // $this->addcell(LabelRecord($r));
  824. break;
  825. case SPREADSHEET_EXCEL_READER_TYPE_EOF:
  826. $cont = false;
  827. break;
  828. default:
  829. //echo ' unknown :'.base_convert($r['code'],10,16)."\n";
  830. break;
  831. }
  832. $spos += $length;
  833. }
  834. if (!isset($this->sheets[$this->sn]['numRows']))
  835. $this->sheets[$this->sn]['numRows'] = $this->sheets[$this->sn]['maxrow'];
  836. if (!isset($this->sheets[$this->sn]['numCols']))
  837. $this->sheets[$this->sn]['numCols'] = $this->sheets[$this->sn]['maxcol'];
  838. }
  839. /**
  840. * Check whether the current record read is a date
  841. *
  842. * @param todo
  843. * @return boolean True if date, false otherwise
  844. */
  845. function isDate($spos)
  846. {
  847. //$xfindex = GetInt2d(, 4);
  848. $xfindex = ord($this->data[$spos+4]) | ord($this->data[$spos+5]) << 8;
  849. //echo 'check is date '.$xfindex.' '.$this->formatRecords['xfrecords'][$xfindex]['type']."\n";
  850. //var_dump($this->formatRecords['xfrecords'][$xfindex]);
  851. if ($this->formatRecords['xfrecords'][$xfindex]['type'] == 'date') {
  852. $this->curformat = $this->formatRecords['xfrecords'][$xfindex]['format'];
  853. $this->rectype = 'date';
  854. return true;
  855. } else {
  856. if ($this->formatRecords['xfrecords'][$xfindex]['type'] == 'number') {
  857. $this->curformat = $this->formatRecords['xfrecords'][$xfindex]['format'];
  858. $this->rectype = 'number';
  859. if (($xfindex == 0x9) || ($xfindex == 0xa)){
  860. $this->multiplier = 100;
  861. }
  862. }else{
  863. $this->curformat = $this->_defaultFormat;
  864. $this->rectype = 'unknown';
  865. }
  866. return false;
  867. }
  868. }
  869. //}}}
  870. //{{{ createDate()
  871. /**
  872. * Convert the raw Excel date into a human readable format
  873. *
  874. * Dates in Excel are stored as number of seconds from an epoch. On
  875. * Windows, the epoch is 30/12/1899 and on Mac it's 01/01/1904
  876. *
  877. * @access private
  878. * @param integer The raw Excel value to convert
  879. * @return array First element is the converted date, the second element is number a unix timestamp
  880. */
  881. function createDate($numValue)
  882. {
  883. if ($numValue > 1) {
  884. $utcDays = $numValue - ($this->nineteenFour ? SPREADSHEET_EXCEL_READER_UTCOFFSETDAYS1904 : SPREADSHEET_EXCEL_READER_UTCOFFSETDAYS);
  885. $utcValue = round(($utcDays+1) * SPREADSHEET_EXCEL_READER_MSINADAY);
  886. $string = date ($this->curformat, $utcValue);
  887. $raw = $utcValue;
  888. } else {
  889. $raw = $numValue;
  890. $hours = floor($numValue * 24);
  891. $mins = floor($numValue * 24 * 60) - $hours * 60;
  892. $secs = floor($numValue * SPREADSHEET_EXCEL_READER_MSINADAY) - $hours * 60 * 60 - $mins * 60;
  893. $string = date ($this->curformat, mktime($hours, $mins, $secs));
  894. }
  895. return array($string, $raw);
  896. }
  897. function createNumber($spos)
  898. {
  899. $rknumhigh = $this->_GetInt4d($this->data, $spos + 10);
  900. $rknumlow = $this->_GetInt4d($this->data, $spos + 6);
  901. //for ($i=0; $i<8; $i++) { echo ord($this->data[$i+$spos+6]) . " "; } echo "<br>";
  902. $sign = ($rknumhigh & 0x80000000) >> 31;
  903. $exp = ($rknumhigh & 0x7ff00000) >> 20;
  904. $mantissa = (0x100000 | ($rknumhigh & 0x000fffff));
  905. $mantissalow1 = ($rknumlow & 0x80000000) >> 31;
  906. $mantissalow2 = ($rknumlow & 0x7fffffff);
  907. $value = $mantissa / pow( 2 , (20- ($exp - 1023)));
  908. if ($mantissalow1 != 0) $value += 1 / pow (2 , (21 - ($exp - 1023)));
  909. $value += $mantissalow2 / pow (2 , (52 - ($exp - 1023)));
  910. //echo "Sign = $sign, Exp = $exp, mantissahighx = $mantissa, mantissalow1 = $mantissalow1, mantissalow2 = $mantissalow2<br>\n";
  911. if ($sign) {$value = -1 * $value;}
  912. return $value;
  913. }
  914. function addcell($row, $col, $string, $raw = '')
  915. {
  916. //echo "ADD cel $row-$col $string\n";
  917. $this->sheets[$this->sn]['maxrow'] = max($this->sheets[$this->sn]['maxrow'], $row + $this->_rowoffset);
  918. $this->sheets[$this->sn]['maxcol'] = max($this->sheets[$this->sn]['maxcol'], $col + $this->_coloffset);
  919. $this->sheets[$this->sn]['cells'][$row + $this->_rowoffset][$col + $this->_coloffset] = $string;
  920. if ($raw)
  921. $this->sheets[$this->sn]['cellsInfo'][$row + $this->_rowoffset][$col + $this->_coloffset]['raw'] = $raw;
  922. if (isset($this->rectype))
  923. $this->sheets[$this->sn]['cellsInfo'][$row + $this->_rowoffset][$col + $this->_coloffset]['type'] = $this->rectype;
  924. }
  925. function _GetIEEE754($rknum)
  926. {
  927. if (($rknum & 0x02) != 0) {
  928. $value = $rknum >> 2;
  929. } else {
  930. //mmp
  931. // first comment out the previously existing 7 lines of code here
  932. // $tmp = unpack("d", pack("VV", 0, ($rknum & 0xfffffffc)));
  933. // //$value = $tmp[''];
  934. // if (array_key_exists(1, $tmp)) {
  935. // $value = $tmp[1];
  936. // } else {
  937. // $value = $tmp[''];
  938. // }
  939. // I got my info on IEEE754 encoding from
  940. // http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html
  941. // The RK format calls for using only the most significant 30 bits of the
  942. // 64 bit floating point value. The other 34 bits are assumed to be 0
  943. // So, we use the upper 30 bits of $rknum as follows...
  944. $sign = ($rknum & 0x80000000) >> 31;
  945. $exp = ($rknum & 0x7ff00000) >> 20;
  946. $mantissa = (0x100000 | ($rknum & 0x000ffffc));
  947. $value = $mantissa / pow( 2 , (20- ($exp - 1023)));
  948. if ($sign) {$value = -1 * $value;}
  949. //end of changes by mmp
  950. }
  951. if (($rknum & 0x01) != 0) {
  952. $value /= 100;
  953. }
  954. return $value;
  955. }
  956. /*
  957. * Function modified by jmontoya in order to fix a problem with encoding in excels
  958. * */
  959. function _encodeUTF16($string, $check = false) {
  960. //var_dump($this->_defaultEncoding.' '.$this->_encoderFunction.' '.$from);
  961. if ($check) {
  962. $from = api_detect_encoding($string);
  963. $string = api_convert_encoding($string, $this->_defaultEncoding, $from);
  964. return $string;
  965. }
  966. $string = api_convert_encoding($string, $this->_defaultEncoding, 'UTF-16LE');
  967. return $string;
  968. /*
  969. * Default behaviour
  970. if ($this->_defaultEncoding){
  971. switch ($this->_encoderFunction){
  972. case 'iconv' : $result = iconv('UTF-16LE', $this->_defaultEncoding, $string);
  973. break;
  974. case 'iconv' : $result = mb_convert_encoding($string, $this->_defaultEncoding, 'UTF-16LE' );
  975. break;
  976. }
  977. }
  978. return $result;
  979. */
  980. }
  981. function _GetInt4d($data, $pos)
  982. {
  983. $value = ord($data[$pos]) | (ord($data[$pos+1]) << 8) | (ord($data[$pos+2]) << 16) | (ord($data[$pos+3]) << 24);
  984. if ($value>=4294967294)
  985. {
  986. $value=-2;
  987. }
  988. return $value;
  989. }
  990. function _GetInt2d($data, $pos)
  991. {
  992. return ord($data[$pos]) | (ord($data[$pos + 1]) << 8);
  993. }
  994. }
  995. /*
  996. * Local variables:
  997. * tab-width: 4
  998. * c-basic-offset: 4
  999. * c-hanging-comment-ender-p: nil
  1000. * End:
  1001. */
  1002. ?>