reader.php 48 KB

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