Subversion Repositories eFlore/Projets.eflore-projets

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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