Subversion Repositories eFlore/Applications.cel

Rev

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

Rev Author Line No. Line
2388 jpm 1
<?php
2
/**
3
 * PHPExcel
4
 *
5
 * Copyright (c) 2006 - 2013 PHPExcel
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20
 *
21
 * @category   PHPExcel
22
 * @package    PHPExcel_Reader_Excel5
23
 * @copyright  Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
24
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt	LGPL
25
 * @version    ##VERSION##, ##DATE##
26
 */
27
 
28
// Original file header of ParseXL (used as the base for this class):
29
// --------------------------------------------------------------------------------
30
// Adapted from Excel_Spreadsheet_Reader developed by users bizon153,
31
// trex005, and mmp11 (SourceForge.net)
32
// http://sourceforge.net/projects/phpexcelreader/
33
// Primary changes made by canyoncasa (dvc) for ParseXL 1.00 ...
34
//	 Modelled moreso after Perl Excel Parse/Write modules
35
//	 Added Parse_Excel_Spreadsheet object
36
//		 Reads a whole worksheet or tab as row,column array or as
37
//		 associated hash of indexed rows and named column fields
38
//	 Added variables for worksheet (tab) indexes and names
39
//	 Added an object call for loading individual woorksheets
40
//	 Changed default indexing defaults to 0 based arrays
41
//	 Fixed date/time and percent formats
42
//	 Includes patches found at SourceForge...
43
//		 unicode patch by nobody
44
//		 unpack("d") machine depedency patch by matchy
45
//		 boundsheet utf16 patch by bjaenichen
46
//	 Renamed functions for shorter names
47
//	 General code cleanup and rigor, including <80 column width
48
//	 Included a testcase Excel file and PHP example calls
49
//	 Code works for PHP 5.x
50
 
51
// Primary changes made by canyoncasa (dvc) for ParseXL 1.10 ...
52
// http://sourceforge.net/tracker/index.php?func=detail&aid=1466964&group_id=99160&atid=623334
53
//	 Decoding of formula conditions, results, and tokens.
54
//	 Support for user-defined named cells added as an array "namedcells"
55
//		 Patch code for user-defined named cells supports single cells only.
56
//		 NOTE: this patch only works for BIFF8 as BIFF5-7 use a different
57
//		 external sheet reference structure
58
 
59
 
60
/** PHPExcel root directory */
61
if (!defined('PHPEXCEL_ROOT')) {
62
	/**
63
	 * @ignore
64
	 */
65
	define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
66
	require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
67
}
68
 
69
/**
70
 * PHPExcel_Reader_Excel5
71
 *
72
 * This class uses {@link http://sourceforge.net/projects/phpexcelreader/parseXL}
73
 *
74
 * @category	PHPExcel
75
 * @package		PHPExcel_Reader_Excel5
76
 * @copyright	Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
77
 */
78
class PHPExcel_Reader_Excel5 extends PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
79
{
80
	// ParseXL definitions
81
	const XLS_BIFF8						= 0x0600;
82
	const XLS_BIFF7						= 0x0500;
83
	const XLS_WorkbookGlobals			= 0x0005;
84
	const XLS_Worksheet					= 0x0010;
85
 
86
	// record identifiers
87
	const XLS_Type_FORMULA				= 0x0006;
88
	const XLS_Type_EOF					= 0x000a;
89
	const XLS_Type_PROTECT				= 0x0012;
90
	const XLS_Type_OBJECTPROTECT		= 0x0063;
91
	const XLS_Type_SCENPROTECT			= 0x00dd;
92
	const XLS_Type_PASSWORD				= 0x0013;
93
	const XLS_Type_HEADER				= 0x0014;
94
	const XLS_Type_FOOTER				= 0x0015;
95
	const XLS_Type_EXTERNSHEET			= 0x0017;
96
	const XLS_Type_DEFINEDNAME			= 0x0018;
97
	const XLS_Type_VERTICALPAGEBREAKS	= 0x001a;
98
	const XLS_Type_HORIZONTALPAGEBREAKS	= 0x001b;
99
	const XLS_Type_NOTE					= 0x001c;
100
	const XLS_Type_SELECTION			= 0x001d;
101
	const XLS_Type_DATEMODE				= 0x0022;
102
	const XLS_Type_EXTERNNAME			= 0x0023;
103
	const XLS_Type_LEFTMARGIN			= 0x0026;
104
	const XLS_Type_RIGHTMARGIN			= 0x0027;
105
	const XLS_Type_TOPMARGIN			= 0x0028;
106
	const XLS_Type_BOTTOMMARGIN			= 0x0029;
107
	const XLS_Type_PRINTGRIDLINES		= 0x002b;
108
	const XLS_Type_FILEPASS				= 0x002f;
109
	const XLS_Type_FONT					= 0x0031;
110
	const XLS_Type_CONTINUE				= 0x003c;
111
	const XLS_Type_PANE					= 0x0041;
112
	const XLS_Type_CODEPAGE				= 0x0042;
113
	const XLS_Type_DEFCOLWIDTH 			= 0x0055;
114
	const XLS_Type_OBJ					= 0x005d;
115
	const XLS_Type_COLINFO				= 0x007d;
116
	const XLS_Type_IMDATA				= 0x007f;
117
	const XLS_Type_SHEETPR				= 0x0081;
118
	const XLS_Type_HCENTER				= 0x0083;
119
	const XLS_Type_VCENTER				= 0x0084;
120
	const XLS_Type_SHEET				= 0x0085;
121
	const XLS_Type_PALETTE				= 0x0092;
122
	const XLS_Type_SCL					= 0x00a0;
123
	const XLS_Type_PAGESETUP			= 0x00a1;
124
	const XLS_Type_MULRK				= 0x00bd;
125
	const XLS_Type_MULBLANK				= 0x00be;
126
	const XLS_Type_DBCELL				= 0x00d7;
127
	const XLS_Type_XF					= 0x00e0;
128
	const XLS_Type_MERGEDCELLS			= 0x00e5;
129
	const XLS_Type_MSODRAWINGGROUP		= 0x00eb;
130
	const XLS_Type_MSODRAWING			= 0x00ec;
131
	const XLS_Type_SST					= 0x00fc;
132
	const XLS_Type_LABELSST				= 0x00fd;
133
	const XLS_Type_EXTSST				= 0x00ff;
134
	const XLS_Type_EXTERNALBOOK			= 0x01ae;
135
	const XLS_Type_DATAVALIDATIONS		= 0x01b2;
136
	const XLS_Type_TXO					= 0x01b6;
137
	const XLS_Type_HYPERLINK			= 0x01b8;
138
	const XLS_Type_DATAVALIDATION		= 0x01be;
139
	const XLS_Type_DIMENSION			= 0x0200;
140
	const XLS_Type_BLANK				= 0x0201;
141
	const XLS_Type_NUMBER				= 0x0203;
142
	const XLS_Type_LABEL				= 0x0204;
143
	const XLS_Type_BOOLERR				= 0x0205;
144
	const XLS_Type_STRING				= 0x0207;
145
	const XLS_Type_ROW					= 0x0208;
146
	const XLS_Type_INDEX				= 0x020b;
147
	const XLS_Type_ARRAY				= 0x0221;
148
	const XLS_Type_DEFAULTROWHEIGHT 	= 0x0225;
149
	const XLS_Type_WINDOW2				= 0x023e;
150
	const XLS_Type_RK					= 0x027e;
151
	const XLS_Type_STYLE				= 0x0293;
152
	const XLS_Type_FORMAT				= 0x041e;
153
	const XLS_Type_SHAREDFMLA			= 0x04bc;
154
	const XLS_Type_BOF					= 0x0809;
155
	const XLS_Type_SHEETPROTECTION		= 0x0867;
156
	const XLS_Type_RANGEPROTECTION		= 0x0868;
157
	const XLS_Type_SHEETLAYOUT			= 0x0862;
158
	const XLS_Type_XFEXT				= 0x087d;
159
	const XLS_Type_PAGELAYOUTVIEW		= 0x088b;
160
	const XLS_Type_UNKNOWN				= 0xffff;
161
 
162
 
163
	/**
164
	 * Summary Information stream data.
165
	 *
166
	 * @var string
167
	 */
168
	private $_summaryInformation;
169
 
170
	/**
171
	 * Extended Summary Information stream data.
172
	 *
173
	 * @var string
174
	 */
175
	private $_documentSummaryInformation;
176
 
177
	/**
178
	 * User-Defined Properties stream data.
179
	 *
180
	 * @var string
181
	 */
182
	private $_userDefinedProperties;
183
 
184
	/**
185
	 * Workbook stream data. (Includes workbook globals substream as well as sheet substreams)
186
	 *
187
	 * @var string
188
	 */
189
	private $_data;
190
 
191
	/**
192
	 * Size in bytes of $this->_data
193
	 *
194
	 * @var int
195
	 */
196
	private $_dataSize;
197
 
198
	/**
199
	 * Current position in stream
200
	 *
201
	 * @var integer
202
	 */
203
	private $_pos;
204
 
205
	/**
206
	 * Workbook to be returned by the reader.
207
	 *
208
	 * @var PHPExcel
209
	 */
210
	private $_phpExcel;
211
 
212
	/**
213
	 * Worksheet that is currently being built by the reader.
214
	 *
215
	 * @var PHPExcel_Worksheet
216
	 */
217
	private $_phpSheet;
218
 
219
	/**
220
	 * BIFF version
221
	 *
222
	 * @var int
223
	 */
224
	private $_version;
225
 
226
	/**
227
	 * Codepage set in the Excel file being read. Only important for BIFF5 (Excel 5.0 - Excel 95)
228
	 * For BIFF8 (Excel 97 - Excel 2003) this will always have the value 'UTF-16LE'
229
	 *
230
	 * @var string
231
	 */
232
	private $_codepage;
233
 
234
	/**
235
	 * Shared formats
236
	 *
237
	 * @var array
238
	 */
239
	private $_formats;
240
 
241
	/**
242
	 * Shared fonts
243
	 *
244
	 * @var array
245
	 */
246
	private $_objFonts;
247
 
248
	/**
249
	 * Color palette
250
	 *
251
	 * @var array
252
	 */
253
	private $_palette;
254
 
255
	/**
256
	 * Worksheets
257
	 *
258
	 * @var array
259
	 */
260
	private $_sheets;
261
 
262
	/**
263
	 * External books
264
	 *
265
	 * @var array
266
	 */
267
	private $_externalBooks;
268
 
269
	/**
270
	 * REF structures. Only applies to BIFF8.
271
	 *
272
	 * @var array
273
	 */
274
	private $_ref;
275
 
276
	/**
277
	 * External names
278
	 *
279
	 * @var array
280
	 */
281
	private $_externalNames;
282
 
283
	/**
284
	 * Defined names
285
	 *
286
	 * @var array
287
	 */
288
	private $_definedname;
289
 
290
	/**
291
	 * Shared strings. Only applies to BIFF8.
292
	 *
293
	 * @var array
294
	 */
295
	private $_sst;
296
 
297
	/**
298
	 * Panes are frozen? (in sheet currently being read). See WINDOW2 record.
299
	 *
300
	 * @var boolean
301
	 */
302
	private $_frozen;
303
 
304
	/**
305
	 * Fit printout to number of pages? (in sheet currently being read). See SHEETPR record.
306
	 *
307
	 * @var boolean
308
	 */
309
	private $_isFitToPages;
310
 
311
	/**
312
	 * Objects. One OBJ record contributes with one entry.
313
	 *
314
	 * @var array
315
	 */
316
	private $_objs;
317
 
318
	/**
319
	 * Text Objects. One TXO record corresponds with one entry.
320
	 *
321
	 * @var array
322
	 */
323
	private $_textObjects;
324
 
325
	/**
326
	 * Cell Annotations (BIFF8)
327
	 *
328
	 * @var array
329
	 */
330
	private $_cellNotes;
331
 
332
	/**
333
	 * The combined MSODRAWINGGROUP data
334
	 *
335
	 * @var string
336
	 */
337
	private $_drawingGroupData;
338
 
339
	/**
340
	 * The combined MSODRAWING data (per sheet)
341
	 *
342
	 * @var string
343
	 */
344
	private $_drawingData;
345
 
346
	/**
347
	 * Keep track of XF index
348
	 *
349
	 * @var int
350
	 */
351
	private $_xfIndex;
352
 
353
	/**
354
	 * Mapping of XF index (that is a cell XF) to final index in cellXf collection
355
	 *
356
	 * @var array
357
	 */
358
	private $_mapCellXfIndex;
359
 
360
	/**
361
	 * Mapping of XF index (that is a style XF) to final index in cellStyleXf collection
362
	 *
363
	 * @var array
364
	 */
365
	private $_mapCellStyleXfIndex;
366
 
367
	/**
368
	 * The shared formulas in a sheet. One SHAREDFMLA record contributes with one value.
369
	 *
370
	 * @var array
371
	 */
372
	private $_sharedFormulas;
373
 
374
	/**
375
	 * The shared formula parts in a sheet. One FORMULA record contributes with one value if it
376
	 * refers to a shared formula.
377
	 *
378
	 * @var array
379
	 */
380
	private $_sharedFormulaParts;
381
 
382
 
383
	/**
384
	 * Create a new PHPExcel_Reader_Excel5 instance
385
	 */
386
	public function __construct() {
387
		$this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
388
	}
389
 
390
 
391
	/**
392
	 * Can the current PHPExcel_Reader_IReader read the file?
393
	 *
394
	 * @param 	string 		$pFilename
395
	 * @return 	boolean
396
	 * @throws PHPExcel_Reader_Exception
397
	 */
398
	public function canRead($pFilename)
399
	{
400
		// Check if file exists
401
		if (!file_exists($pFilename)) {
402
			throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
403
		}
404
 
405
		try {
406
			// Use ParseXL for the hard work.
407
			$ole = new PHPExcel_Shared_OLERead();
408
 
409
			// get excel data
410
			$res = $ole->read($pFilename);
411
			return true;
412
		} catch (PHPExcel_Exception $e) {
413
			return false;
414
		}
415
	}
416
 
417
 
418
	/**
419
	 * Reads names of the worksheets from a file, without parsing the whole file to a PHPExcel object
420
	 *
421
	 * @param 	string 		$pFilename
422
	 * @throws 	PHPExcel_Reader_Exception
423
	 */
424
	public function listWorksheetNames($pFilename)
425
	{
426
		// Check if file exists
427
		if (!file_exists($pFilename)) {
428
			throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
429
		}
430
 
431
		$worksheetNames = array();
432
 
433
		// Read the OLE file
434
		$this->_loadOLE($pFilename);
435
 
436
		// total byte size of Excel data (workbook global substream + sheet substreams)
437
		$this->_dataSize = strlen($this->_data);
438
 
439
		$this->_pos		= 0;
440
		$this->_sheets	= array();
441
 
442
		// Parse Workbook Global Substream
443
		while ($this->_pos < $this->_dataSize) {
444
			$code = self::_GetInt2d($this->_data, $this->_pos);
445
 
446
			switch ($code) {
447
				case self::XLS_Type_BOF:	$this->_readBof();		break;
448
				case self::XLS_Type_SHEET:	$this->_readSheet();	break;
449
				case self::XLS_Type_EOF:	$this->_readDefault();	break 2;
450
				default:					$this->_readDefault();	break;
451
			}
452
		}
453
 
454
		foreach ($this->_sheets as $sheet) {
455
			if ($sheet['sheetType'] != 0x00) {
456
				// 0x00: Worksheet, 0x02: Chart, 0x06: Visual Basic module
457
				continue;
458
			}
459
 
460
			$worksheetNames[] = $sheet['name'];
461
		}
462
 
463
		return $worksheetNames;
464
	}
465
 
466
 
467
	/**
468
	 * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)
469
	 *
470
	 * @param   string     $pFilename
471
	 * @throws   PHPExcel_Reader_Exception
472
	 */
473
	public function listWorksheetInfo($pFilename)
474
	{
475
		// Check if file exists
476
		if (!file_exists($pFilename)) {
477
			throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
478
		}
479
 
480
		$worksheetInfo = array();
481
 
482
		// Read the OLE file
483
		$this->_loadOLE($pFilename);
484
 
485
		// total byte size of Excel data (workbook global substream + sheet substreams)
486
		$this->_dataSize = strlen($this->_data);
487
 
488
		// initialize
489
		$this->_pos    = 0;
490
		$this->_sheets = array();
491
 
492
		// Parse Workbook Global Substream
493
		while ($this->_pos < $this->_dataSize) {
494
			$code = self::_GetInt2d($this->_data, $this->_pos);
495
 
496
			switch ($code) {
497
				case self::XLS_Type_BOF:        $this->_readBof();        break;
498
				case self::XLS_Type_SHEET:      $this->_readSheet();      break;
499
				case self::XLS_Type_EOF:        $this->_readDefault();    break 2;
500
				default:                        $this->_readDefault();    break;
501
			}
502
		}
503
 
504
		// Parse the individual sheets
505
		foreach ($this->_sheets as $sheet) {
506
 
507
			if ($sheet['sheetType'] != 0x00) {
508
				// 0x00: Worksheet
509
				// 0x02: Chart
510
				// 0x06: Visual Basic module
511
				continue;
512
			}
513
 
514
			$tmpInfo = array();
515
			$tmpInfo['worksheetName'] = $sheet['name'];
516
			$tmpInfo['lastColumnLetter'] = 'A';
517
			$tmpInfo['lastColumnIndex'] = 0;
518
			$tmpInfo['totalRows'] = 0;
519
			$tmpInfo['totalColumns'] = 0;
520
 
521
			$this->_pos = $sheet['offset'];
522
 
523
			while ($this->_pos <= $this->_dataSize - 4) {
524
				$code = self::_GetInt2d($this->_data, $this->_pos);
525
 
526
				switch ($code) {
527
					case self::XLS_Type_RK:
528
					case self::XLS_Type_LABELSST:
529
					case self::XLS_Type_NUMBER:
530
					case self::XLS_Type_FORMULA:
531
					case self::XLS_Type_BOOLERR:
532
					case self::XLS_Type_LABEL:
533
						$length = self::_GetInt2d($this->_data, $this->_pos + 2);
534
						$recordData = substr($this->_data, $this->_pos + 4, $length);
535
 
536
						// move stream pointer to next record
537
						$this->_pos += 4 + $length;
538
 
539
						$rowIndex = self::_GetInt2d($recordData, 0) + 1;
540
						$columnIndex = self::_GetInt2d($recordData, 2);
541
 
542
						$tmpInfo['totalRows'] = max($tmpInfo['totalRows'], $rowIndex);
543
						$tmpInfo['lastColumnIndex'] = max($tmpInfo['lastColumnIndex'], $columnIndex);
544
						break;
545
					case self::XLS_Type_BOF:      $this->_readBof();          break;
546
					case self::XLS_Type_EOF:      $this->_readDefault();      break 2;
547
					default:                      $this->_readDefault();      break;
548
				}
549
			}
550
 
551
			$tmpInfo['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($tmpInfo['lastColumnIndex']);
552
			$tmpInfo['totalColumns'] = $tmpInfo['lastColumnIndex'] + 1;
553
 
554
			$worksheetInfo[] = $tmpInfo;
555
		}
556
 
557
		return $worksheetInfo;
558
	}
559
 
560
 
561
	/**
562
	 * Loads PHPExcel from file
563
	 *
564
	 * @param 	string 		$pFilename
565
	 * @return 	PHPExcel
566
	 * @throws 	PHPExcel_Reader_Exception
567
	 */
568
	public function load($pFilename)
569
	{
570
		// Read the OLE file
571
		$this->_loadOLE($pFilename);
572
 
573
		// Initialisations
574
		$this->_phpExcel = new PHPExcel;
575
		$this->_phpExcel->removeSheetByIndex(0); // remove 1st sheet
576
		if (!$this->_readDataOnly) {
577
			$this->_phpExcel->removeCellStyleXfByIndex(0); // remove the default style
578
			$this->_phpExcel->removeCellXfByIndex(0); // remove the default style
579
		}
580
 
581
		// Read the summary information stream (containing meta data)
582
		$this->_readSummaryInformation();
583
 
584
		// Read the Additional document summary information stream (containing application-specific meta data)
585
		$this->_readDocumentSummaryInformation();
586
 
587
		// total byte size of Excel data (workbook global substream + sheet substreams)
588
		$this->_dataSize = strlen($this->_data);
589
 
590
		// initialize
591
		$this->_pos					= 0;
592
		$this->_codepage			= 'CP1252';
593
		$this->_formats				= array();
594
		$this->_objFonts			= array();
595
		$this->_palette				= array();
596
		$this->_sheets				= array();
597
		$this->_externalBooks		= array();
598
		$this->_ref					= array();
599
		$this->_definedname			= array();
600
		$this->_sst					= array();
601
		$this->_drawingGroupData	= '';
602
		$this->_xfIndex				= '';
603
		$this->_mapCellXfIndex		= array();
604
		$this->_mapCellStyleXfIndex	= array();
605
 
606
		// Parse Workbook Global Substream
607
		while ($this->_pos < $this->_dataSize) {
608
			$code = self::_GetInt2d($this->_data, $this->_pos);
609
 
610
			switch ($code) {
611
				case self::XLS_Type_BOF:			$this->_readBof();				break;
612
				case self::XLS_Type_FILEPASS:		$this->_readFilepass();			break;
613
				case self::XLS_Type_CODEPAGE:		$this->_readCodepage();			break;
614
				case self::XLS_Type_DATEMODE:		$this->_readDateMode();			break;
615
				case self::XLS_Type_FONT:			$this->_readFont();				break;
616
				case self::XLS_Type_FORMAT:			$this->_readFormat();			break;
617
				case self::XLS_Type_XF:				$this->_readXf();				break;
618
				case self::XLS_Type_XFEXT:			$this->_readXfExt();			break;
619
				case self::XLS_Type_STYLE:			$this->_readStyle();			break;
620
				case self::XLS_Type_PALETTE:		$this->_readPalette();			break;
621
				case self::XLS_Type_SHEET:			$this->_readSheet();			break;
622
				case self::XLS_Type_EXTERNALBOOK:	$this->_readExternalBook();		break;
623
				case self::XLS_Type_EXTERNNAME:		$this->_readExternName();		break;
624
				case self::XLS_Type_EXTERNSHEET:	$this->_readExternSheet();		break;
625
				case self::XLS_Type_DEFINEDNAME:	$this->_readDefinedName();		break;
626
				case self::XLS_Type_MSODRAWINGGROUP:	$this->_readMsoDrawingGroup();	break;
627
				case self::XLS_Type_SST:			$this->_readSst();				break;
628
				case self::XLS_Type_EOF:			$this->_readDefault();			break 2;
629
				default:							$this->_readDefault();			break;
630
			}
631
		}
632
 
633
		// Resolve indexed colors for font, fill, and border colors
634
		// Cannot be resolved already in XF record, because PALETTE record comes afterwards
635
		if (!$this->_readDataOnly) {
636
			foreach ($this->_objFonts as $objFont) {
637
				if (isset($objFont->colorIndex)) {
638
					$color = self::_readColor($objFont->colorIndex,$this->_palette,$this->_version);
639
					$objFont->getColor()->setRGB($color['rgb']);
640
				}
641
			}
642
 
643
			foreach ($this->_phpExcel->getCellXfCollection() as $objStyle) {
644
				// fill start and end color
645
				$fill = $objStyle->getFill();
646
 
647
				if (isset($fill->startcolorIndex)) {
648
					$startColor = self::_readColor($fill->startcolorIndex,$this->_palette,$this->_version);
649
					$fill->getStartColor()->setRGB($startColor['rgb']);
650
				}
651
 
652
				if (isset($fill->endcolorIndex)) {
653
					$endColor = self::_readColor($fill->endcolorIndex,$this->_palette,$this->_version);
654
					$fill->getEndColor()->setRGB($endColor['rgb']);
655
				}
656
 
657
				// border colors
658
				$top      = $objStyle->getBorders()->getTop();
659
				$right    = $objStyle->getBorders()->getRight();
660
				$bottom   = $objStyle->getBorders()->getBottom();
661
				$left     = $objStyle->getBorders()->getLeft();
662
				$diagonal = $objStyle->getBorders()->getDiagonal();
663
 
664
				if (isset($top->colorIndex)) {
665
					$borderTopColor = self::_readColor($top->colorIndex,$this->_palette,$this->_version);
666
					$top->getColor()->setRGB($borderTopColor['rgb']);
667
				}
668
 
669
				if (isset($right->colorIndex)) {
670
					$borderRightColor = self::_readColor($right->colorIndex,$this->_palette,$this->_version);
671
					$right->getColor()->setRGB($borderRightColor['rgb']);
672
				}
673
 
674
				if (isset($bottom->colorIndex)) {
675
					$borderBottomColor = self::_readColor($bottom->colorIndex,$this->_palette,$this->_version);
676
					$bottom->getColor()->setRGB($borderBottomColor['rgb']);
677
				}
678
 
679
				if (isset($left->colorIndex)) {
680
					$borderLeftColor = self::_readColor($left->colorIndex,$this->_palette,$this->_version);
681
					$left->getColor()->setRGB($borderLeftColor['rgb']);
682
				}
683
 
684
				if (isset($diagonal->colorIndex)) {
685
					$borderDiagonalColor = self::_readColor($diagonal->colorIndex,$this->_palette,$this->_version);
686
					$diagonal->getColor()->setRGB($borderDiagonalColor['rgb']);
687
				}
688
			}
689
		}
690
 
691
		// treat MSODRAWINGGROUP records, workbook-level Escher
692
		if (!$this->_readDataOnly && $this->_drawingGroupData) {
693
			$escherWorkbook = new PHPExcel_Shared_Escher();
694
			$reader = new PHPExcel_Reader_Excel5_Escher($escherWorkbook);
695
			$escherWorkbook = $reader->load($this->_drawingGroupData);
696
 
697
			// debug Escher stream
698
			//$debug = new Debug_Escher(new PHPExcel_Shared_Escher());
699
			//$debug->load($this->_drawingGroupData);
700
		}
701
 
702
		// Parse the individual sheets
703
		foreach ($this->_sheets as $sheet) {
704
 
705
			if ($sheet['sheetType'] != 0x00) {
706
				// 0x00: Worksheet, 0x02: Chart, 0x06: Visual Basic module
707
				continue;
708
			}
709
 
710
			// check if sheet should be skipped
711
			if (isset($this->_loadSheetsOnly) && !in_array($sheet['name'], $this->_loadSheetsOnly)) {
712
				continue;
713
			}
714
 
715
			// add sheet to PHPExcel object
716
			$this->_phpSheet = $this->_phpExcel->createSheet();
717
			//	Use false for $updateFormulaCellReferences to prevent adjustment of worksheet references in formula
718
			//		cells... during the load, all formulae should be correct, and we're simply bringing the worksheet
719
			//		name in line with the formula, not the reverse
720
			$this->_phpSheet->setTitle($sheet['name'],false);
721
			$this->_phpSheet->setSheetState($sheet['sheetState']);
722
 
723
			$this->_pos = $sheet['offset'];
724
 
725
			// Initialize isFitToPages. May change after reading SHEETPR record.
726
			$this->_isFitToPages = false;
727
 
728
			// Initialize drawingData
729
			$this->_drawingData = '';
730
 
731
			// Initialize objs
732
			$this->_objs = array();
733
 
734
			// Initialize shared formula parts
735
			$this->_sharedFormulaParts = array();
736
 
737
			// Initialize shared formulas
738
			$this->_sharedFormulas = array();
739
 
740
			// Initialize text objs
741
			$this->_textObjects = array();
742
 
743
			// Initialize cell annotations
744
			$this->_cellNotes = array();
745
			$this->textObjRef = -1;
746
 
747
			while ($this->_pos <= $this->_dataSize - 4) {
748
				$code = self::_GetInt2d($this->_data, $this->_pos);
749
 
750
				switch ($code) {
751
					case self::XLS_Type_BOF:					$this->_readBof();						break;
752
					case self::XLS_Type_PRINTGRIDLINES:			$this->_readPrintGridlines();			break;
753
					case self::XLS_Type_DEFAULTROWHEIGHT:		$this->_readDefaultRowHeight();			break;
754
					case self::XLS_Type_SHEETPR:				$this->_readSheetPr();					break;
755
					case self::XLS_Type_HORIZONTALPAGEBREAKS:	$this->_readHorizontalPageBreaks();		break;
756
					case self::XLS_Type_VERTICALPAGEBREAKS:		$this->_readVerticalPageBreaks();		break;
757
					case self::XLS_Type_HEADER:					$this->_readHeader();					break;
758
					case self::XLS_Type_FOOTER:					$this->_readFooter();					break;
759
					case self::XLS_Type_HCENTER:				$this->_readHcenter();					break;
760
					case self::XLS_Type_VCENTER:				$this->_readVcenter();					break;
761
					case self::XLS_Type_LEFTMARGIN:				$this->_readLeftMargin();				break;
762
					case self::XLS_Type_RIGHTMARGIN:			$this->_readRightMargin();				break;
763
					case self::XLS_Type_TOPMARGIN:				$this->_readTopMargin();				break;
764
					case self::XLS_Type_BOTTOMMARGIN:			$this->_readBottomMargin();				break;
765
					case self::XLS_Type_PAGESETUP:				$this->_readPageSetup();				break;
766
					case self::XLS_Type_PROTECT:				$this->_readProtect();					break;
767
					case self::XLS_Type_SCENPROTECT:			$this->_readScenProtect();				break;
768
					case self::XLS_Type_OBJECTPROTECT:			$this->_readObjectProtect();			break;
769
					case self::XLS_Type_PASSWORD:				$this->_readPassword();					break;
770
					case self::XLS_Type_DEFCOLWIDTH:			$this->_readDefColWidth();				break;
771
					case self::XLS_Type_COLINFO:				$this->_readColInfo();					break;
772
					case self::XLS_Type_DIMENSION:				$this->_readDefault();					break;
773
					case self::XLS_Type_ROW:					$this->_readRow();						break;
774
					case self::XLS_Type_DBCELL:					$this->_readDefault();					break;
775
					case self::XLS_Type_RK:						$this->_readRk();						break;
776
					case self::XLS_Type_LABELSST:				$this->_readLabelSst();					break;
777
					case self::XLS_Type_MULRK:					$this->_readMulRk();					break;
778
					case self::XLS_Type_NUMBER:					$this->_readNumber();					break;
779
					case self::XLS_Type_FORMULA:				$this->_readFormula();					break;
780
					case self::XLS_Type_SHAREDFMLA:				$this->_readSharedFmla();				break;
781
					case self::XLS_Type_BOOLERR:				$this->_readBoolErr();					break;
782
					case self::XLS_Type_MULBLANK:				$this->_readMulBlank();					break;
783
					case self::XLS_Type_LABEL:					$this->_readLabel();					break;
784
					case self::XLS_Type_BLANK:					$this->_readBlank();					break;
785
					case self::XLS_Type_MSODRAWING:				$this->_readMsoDrawing();				break;
786
					case self::XLS_Type_OBJ:					$this->_readObj();						break;
787
					case self::XLS_Type_WINDOW2:				$this->_readWindow2();					break;
788
					case self::XLS_Type_PAGELAYOUTVIEW:	$this->_readPageLayoutView();					break;
789
					case self::XLS_Type_SCL:					$this->_readScl();						break;
790
					case self::XLS_Type_PANE:					$this->_readPane();						break;
791
					case self::XLS_Type_SELECTION:				$this->_readSelection();				break;
792
					case self::XLS_Type_MERGEDCELLS:			$this->_readMergedCells();				break;
793
					case self::XLS_Type_HYPERLINK:				$this->_readHyperLink();				break;
794
					case self::XLS_Type_DATAVALIDATIONS:		$this->_readDataValidations();			break;
795
					case self::XLS_Type_DATAVALIDATION:			$this->_readDataValidation();			break;
796
					case self::XLS_Type_SHEETLAYOUT:			$this->_readSheetLayout();				break;
797
					case self::XLS_Type_SHEETPROTECTION:		$this->_readSheetProtection();			break;
798
					case self::XLS_Type_RANGEPROTECTION:		$this->_readRangeProtection();			break;
799
					case self::XLS_Type_NOTE:					$this->_readNote();						break;
800
					//case self::XLS_Type_IMDATA:				$this->_readImData();					break;
801
					case self::XLS_Type_TXO:					$this->_readTextObject();				break;
802
					case self::XLS_Type_CONTINUE:				$this->_readContinue();					break;
803
					case self::XLS_Type_EOF:					$this->_readDefault();					break 2;
804
					default:									$this->_readDefault();					break;
805
				}
806
 
807
			}
808
 
809
			// treat MSODRAWING records, sheet-level Escher
810
			if (!$this->_readDataOnly && $this->_drawingData) {
811
				$escherWorksheet = new PHPExcel_Shared_Escher();
812
				$reader = new PHPExcel_Reader_Excel5_Escher($escherWorksheet);
813
				$escherWorksheet = $reader->load($this->_drawingData);
814
 
815
				// debug Escher stream
816
				//$debug = new Debug_Escher(new PHPExcel_Shared_Escher());
817
				//$debug->load($this->_drawingData);
818
 
819
				// get all spContainers in one long array, so they can be mapped to OBJ records
820
				$allSpContainers = $escherWorksheet->getDgContainer()->getSpgrContainer()->getAllSpContainers();
821
			}
822
 
823
			// treat OBJ records
824
			foreach ($this->_objs as $n => $obj) {
825
//				echo '<hr /><b>Object</b> reference is ',$n,'<br />';
826
//				var_dump($obj);
827
//				echo '<br />';
828
 
829
				// the first shape container never has a corresponding OBJ record, hence $n + 1
830
				if (isset($allSpContainers[$n + 1]) && is_object($allSpContainers[$n + 1])) {
831
					$spContainer = $allSpContainers[$n + 1];
832
 
833
					// we skip all spContainers that are a part of a group shape since we cannot yet handle those
834
					if ($spContainer->getNestingLevel() > 1) {
835
						continue;
836
					}
837
 
838
					// calculate the width and height of the shape
839
					list($startColumn, $startRow) = PHPExcel_Cell::coordinateFromString($spContainer->getStartCoordinates());
840
					list($endColumn, $endRow) = PHPExcel_Cell::coordinateFromString($spContainer->getEndCoordinates());
841
 
842
					$startOffsetX = $spContainer->getStartOffsetX();
843
					$startOffsetY = $spContainer->getStartOffsetY();
844
					$endOffsetX = $spContainer->getEndOffsetX();
845
					$endOffsetY = $spContainer->getEndOffsetY();
846
 
847
					$width = PHPExcel_Shared_Excel5::getDistanceX($this->_phpSheet, $startColumn, $startOffsetX, $endColumn, $endOffsetX);
848
					$height = PHPExcel_Shared_Excel5::getDistanceY($this->_phpSheet, $startRow, $startOffsetY, $endRow, $endOffsetY);
849
 
850
					// calculate offsetX and offsetY of the shape
851
					$offsetX = $startOffsetX * PHPExcel_Shared_Excel5::sizeCol($this->_phpSheet, $startColumn) / 1024;
852
					$offsetY = $startOffsetY * PHPExcel_Shared_Excel5::sizeRow($this->_phpSheet, $startRow) / 256;
853
 
854
					switch ($obj['otObjType']) {
855
						case 0x19:
856
							// Note
857
//							echo 'Cell Annotation Object<br />';
858
//							echo 'Object ID is ',$obj['idObjID'],'<br />';
859
//
860
							if (isset($this->_cellNotes[$obj['idObjID']])) {
861
								$cellNote = $this->_cellNotes[$obj['idObjID']];
862
 
863
								if (isset($this->_textObjects[$obj['idObjID']])) {
864
									$textObject = $this->_textObjects[$obj['idObjID']];
865
									$this->_cellNotes[$obj['idObjID']]['objTextData'] = $textObject;
866
								}
867
							}
868
							break;
869
 
870
						case 0x08:
871
//							echo 'Picture Object<br />';
872
							// picture
873
 
874
							// get index to BSE entry (1-based)
875
							$BSEindex = $spContainer->getOPT(0x0104);
876
							$BSECollection = $escherWorkbook->getDggContainer()->getBstoreContainer()->getBSECollection();
877
							$BSE = $BSECollection[$BSEindex - 1];
878
							$blipType = $BSE->getBlipType();
879
 
880
							// need check because some blip types are not supported by Escher reader such as EMF
881
							if ($blip = $BSE->getBlip()) {
882
								$ih = imagecreatefromstring($blip->getData());
883
								$drawing = new PHPExcel_Worksheet_MemoryDrawing();
884
								$drawing->setImageResource($ih);
885
 
886
								// width, height, offsetX, offsetY
887
								$drawing->setResizeProportional(false);
888
								$drawing->setWidth($width);
889
								$drawing->setHeight($height);
890
								$drawing->setOffsetX($offsetX);
891
								$drawing->setOffsetY($offsetY);
892
 
893
								switch ($blipType) {
894
									case PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_JPEG:
895
										$drawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG);
896
										$drawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_JPEG);
897
										break;
898
 
899
									case PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_PNG:
900
										$drawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_PNG);
901
										$drawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_PNG);
902
										break;
903
								}
904
 
905
								$drawing->setWorksheet($this->_phpSheet);
906
								$drawing->setCoordinates($spContainer->getStartCoordinates());
907
							}
908
 
909
							break;
910
 
911
						default:
912
							// other object type
913
							break;
914
 
915
					}
916
				}
917
			}
918
 
919
			// treat SHAREDFMLA records
920
			if ($this->_version == self::XLS_BIFF8) {
921
				foreach ($this->_sharedFormulaParts as $cell => $baseCell) {
922
					list($column, $row) = PHPExcel_Cell::coordinateFromString($cell);
923
					if (($this->getReadFilter() !== NULL) && $this->getReadFilter()->readCell($column, $row, $this->_phpSheet->getTitle()) ) {
924
						$formula = $this->_getFormulaFromStructure($this->_sharedFormulas[$baseCell], $cell);
925
						$this->_phpSheet->getCell($cell)->setValueExplicit('=' . $formula, PHPExcel_Cell_DataType::TYPE_FORMULA);
926
					}
927
				}
928
			}
929
 
930
			if (!empty($this->_cellNotes)) {
931
				foreach($this->_cellNotes as $note => $noteDetails) {
932
					if (!isset($noteDetails['objTextData'])) {
933
						if (isset($this->_textObjects[$note])) {
934
							$textObject = $this->_textObjects[$note];
935
							$noteDetails['objTextData'] = $textObject;
936
						} else {
937
							$noteDetails['objTextData']['text'] = '';
938
						}
939
					}
940
//					echo '<b>Cell annotation ',$note,'</b><br />';
941
//					var_dump($noteDetails);
942
//					echo '<br />';
943
					$cellAddress = str_replace('$','',$noteDetails['cellRef']);
944
					$this->_phpSheet->getComment( $cellAddress )
945
													->setAuthor( $noteDetails['author'] )
946
													->setText($this->_parseRichText($noteDetails['objTextData']['text']) );
947
				}
948
			}
949
		}
950
 
951
		// add the named ranges (defined names)
952
		foreach ($this->_definedname as $definedName) {
953
			if ($definedName['isBuiltInName']) {
954
				switch ($definedName['name']) {
955
 
956
				case pack('C', 0x06):
957
					// print area
958
					//	in general, formula looks like this: Foo!$C$7:$J$66,Bar!$A$1:$IV$2
959
					$ranges = explode(',', $definedName['formula']); // FIXME: what if sheetname contains comma?
960
 
961
					$extractedRanges = array();
962
					foreach ($ranges as $range) {
963
						// $range should look like one of these
964
						//		Foo!$C$7:$J$66
965
						//		Bar!$A$1:$IV$2
966
 
967
						$explodes = explode('!', $range);	// FIXME: what if sheetname contains exclamation mark?
968
						$sheetName = trim($explodes[0], "'");
969
 
970
						if (count($explodes) == 2) {
971
							if (strpos($explodes[1], ':') === FALSE) {
972
								$explodes[1] = $explodes[1] . ':' . $explodes[1];
973
							}
974
							$extractedRanges[] = str_replace('$', '', $explodes[1]); // C7:J66
975
						}
976
					}
977
					if ($docSheet = $this->_phpExcel->getSheetByName($sheetName)) {
978
						$docSheet->getPageSetup()->setPrintArea(implode(',', $extractedRanges)); // C7:J66,A1:IV2
979
					}
980
					break;
981
 
982
				case pack('C', 0x07):
983
					// print titles (repeating rows)
984
					// Assuming BIFF8, there are 3 cases
985
					// 1. repeating rows
986
					//		formula looks like this: Sheet!$A$1:$IV$2
987
					//		rows 1-2 repeat
988
					// 2. repeating columns
989
					//		formula looks like this: Sheet!$A$1:$B$65536
990
					//		columns A-B repeat
991
					// 3. both repeating rows and repeating columns
992
					//		formula looks like this: Sheet!$A$1:$B$65536,Sheet!$A$1:$IV$2
993
 
994
					$ranges = explode(',', $definedName['formula']); // FIXME: what if sheetname contains comma?
995
 
996
					foreach ($ranges as $range) {
997
						// $range should look like this one of these
998
						//		Sheet!$A$1:$B$65536
999
						//		Sheet!$A$1:$IV$2
1000
 
1001
						$explodes = explode('!', $range);
1002
 
1003
						if (count($explodes) == 2) {
1004
							if ($docSheet = $this->_phpExcel->getSheetByName($explodes[0])) {
1005
 
1006
								$extractedRange = $explodes[1];
1007
								$extractedRange = str_replace('$', '', $extractedRange);
1008
 
1009
								$coordinateStrings = explode(':', $extractedRange);
1010
								if (count($coordinateStrings) == 2) {
1011
									list($firstColumn, $firstRow) = PHPExcel_Cell::coordinateFromString($coordinateStrings[0]);
1012
									list($lastColumn, $lastRow) = PHPExcel_Cell::coordinateFromString($coordinateStrings[1]);
1013
 
1014
									if ($firstColumn == 'A' and $lastColumn == 'IV') {
1015
										// then we have repeating rows
1016
										$docSheet->getPageSetup()->setRowsToRepeatAtTop(array($firstRow, $lastRow));
1017
									} elseif ($firstRow == 1 and $lastRow == 65536) {
1018
										// then we have repeating columns
1019
										$docSheet->getPageSetup()->setColumnsToRepeatAtLeft(array($firstColumn, $lastColumn));
1020
									}
1021
								}
1022
							}
1023
						}
1024
					}
1025
					break;
1026
 
1027
				}
1028
			} else {
1029
				// Extract range
1030
				$explodes = explode('!', $definedName['formula']);
1031
 
1032
				if (count($explodes) == 2) {
1033
					if (($docSheet = $this->_phpExcel->getSheetByName($explodes[0])) ||
1034
						($docSheet = $this->_phpExcel->getSheetByName(trim($explodes[0],"'")))) {
1035
						$extractedRange = $explodes[1];
1036
						$extractedRange = str_replace('$', '', $extractedRange);
1037
 
1038
						$localOnly = ($definedName['scope'] == 0) ? false : true;
1039
 
1040
						$scope = ($definedName['scope'] == 0) ?
1041
							null : $this->_phpExcel->getSheetByName($this->_sheets[$definedName['scope'] - 1]['name']);
1042
 
1043
						$this->_phpExcel->addNamedRange( new PHPExcel_NamedRange((string)$definedName['name'], $docSheet, $extractedRange, $localOnly, $scope) );
1044
					}
1045
				} else {
1046
					//	Named Value
1047
					//	TODO Provide support for named values
1048
				}
1049
			}
1050
		}
1051
 
1052
		return $this->_phpExcel;
1053
	}
1054
 
1055
 
1056
	/**
1057
	 * Use OLE reader to extract the relevant data streams from the OLE file
1058
	 *
1059
	 * @param string $pFilename
1060
	 */
1061
	private function _loadOLE($pFilename)
1062
	{
1063
		// OLE reader
1064
		$ole = new PHPExcel_Shared_OLERead();
1065
 
1066
		// get excel data,
1067
		$res = $ole->read($pFilename);
1068
		// Get workbook data: workbook stream + sheet streams
1069
		$this->_data = $ole->getStream($ole->wrkbook);
1070
 
1071
		// Get summary information data
1072
		$this->_summaryInformation = $ole->getStream($ole->summaryInformation);
1073
 
1074
		// Get additional document summary information data
1075
		$this->_documentSummaryInformation = $ole->getStream($ole->documentSummaryInformation);
1076
 
1077
		// Get user-defined property data
1078
//		$this->_userDefinedProperties = $ole->getUserDefinedProperties();
1079
	}
1080
 
1081
 
1082
	/**
1083
	 * Read summary information
1084
	 */
1085
	private function _readSummaryInformation()
1086
	{
1087
		if (!isset($this->_summaryInformation)) {
1088
			return;
1089
		}
1090
 
1091
		// offset: 0; size: 2; must be 0xFE 0xFF (UTF-16 LE byte order mark)
1092
		// offset: 2; size: 2;
1093
		// offset: 4; size: 2; OS version
1094
		// offset: 6; size: 2; OS indicator
1095
		// offset: 8; size: 16
1096
		// offset: 24; size: 4; section count
1097
		$secCount = self::_GetInt4d($this->_summaryInformation, 24);
1098
 
1099
		// offset: 28; size: 16; first section's class id: e0 85 9f f2 f9 4f 68 10 ab 91 08 00 2b 27 b3 d9
1100
		// offset: 44; size: 4
1101
		$secOffset = self::_GetInt4d($this->_summaryInformation, 44);
1102
 
1103
		// section header
1104
		// offset: $secOffset; size: 4; section length
1105
		$secLength = self::_GetInt4d($this->_summaryInformation, $secOffset);
1106
 
1107
		// offset: $secOffset+4; size: 4; property count
1108
		$countProperties = self::_GetInt4d($this->_summaryInformation, $secOffset+4);
1109
 
1110
		// initialize code page (used to resolve string values)
1111
		$codePage = 'CP1252';
1112
 
1113
		// offset: ($secOffset+8); size: var
1114
		// loop through property decarations and properties
1115
		for ($i = 0; $i < $countProperties; ++$i) {
1116
 
1117
			// offset: ($secOffset+8) + (8 * $i); size: 4; property ID
1118
			$id = self::_GetInt4d($this->_summaryInformation, ($secOffset+8) + (8 * $i));
1119
 
1120
			// Use value of property id as appropriate
1121
			// offset: ($secOffset+12) + (8 * $i); size: 4; offset from beginning of section (48)
1122
			$offset = self::_GetInt4d($this->_summaryInformation, ($secOffset+12) + (8 * $i));
1123
 
1124
			$type = self::_GetInt4d($this->_summaryInformation, $secOffset + $offset);
1125
 
1126
			// initialize property value
1127
			$value = null;
1128
 
1129
			// extract property value based on property type
1130
			switch ($type) {
1131
				case 0x02: // 2 byte signed integer
1132
					$value = self::_GetInt2d($this->_summaryInformation, $secOffset + 4 + $offset);
1133
					break;
1134
 
1135
				case 0x03: // 4 byte signed integer
1136
					$value = self::_GetInt4d($this->_summaryInformation, $secOffset + 4 + $offset);
1137
					break;
1138
 
1139
				case 0x13: // 4 byte unsigned integer
1140
					// not needed yet, fix later if necessary
1141
					break;
1142
 
1143
				case 0x1E: // null-terminated string prepended by dword string length
1144
					$byteLength = self::_GetInt4d($this->_summaryInformation, $secOffset + 4 + $offset);
1145
					$value = substr($this->_summaryInformation, $secOffset + 8 + $offset, $byteLength);
1146
					$value = PHPExcel_Shared_String::ConvertEncoding($value, 'UTF-8', $codePage);
1147
					$value = rtrim($value);
1148
					break;
1149
 
1150
				case 0x40: // Filetime (64-bit value representing the number of 100-nanosecond intervals since January 1, 1601)
1151
					// PHP-time
1152
					$value = PHPExcel_Shared_OLE::OLE2LocalDate(substr($this->_summaryInformation, $secOffset + 4 + $offset, 8));
1153
					break;
1154
 
1155
				case 0x47: // Clipboard format
1156
					// not needed yet, fix later if necessary
1157
					break;
1158
			}
1159
 
1160
			switch ($id) {
1161
				case 0x01:	//	Code Page
1162
					$codePage = PHPExcel_Shared_CodePage::NumberToName($value);
1163
					break;
1164
 
1165
				case 0x02:	//	Title
1166
					$this->_phpExcel->getProperties()->setTitle($value);
1167
					break;
1168
 
1169
				case 0x03:	//	Subject
1170
					$this->_phpExcel->getProperties()->setSubject($value);
1171
					break;
1172
 
1173
				case 0x04:	//	Author (Creator)
1174
					$this->_phpExcel->getProperties()->setCreator($value);
1175
					break;
1176
 
1177
				case 0x05:	//	Keywords
1178
					$this->_phpExcel->getProperties()->setKeywords($value);
1179
					break;
1180
 
1181
				case 0x06:	//	Comments (Description)
1182
					$this->_phpExcel->getProperties()->setDescription($value);
1183
					break;
1184
 
1185
				case 0x07:	//	Template
1186
					//	Not supported by PHPExcel
1187
					break;
1188
 
1189
				case 0x08:	//	Last Saved By (LastModifiedBy)
1190
					$this->_phpExcel->getProperties()->setLastModifiedBy($value);
1191
					break;
1192
 
1193
				case 0x09:	//	Revision
1194
					//	Not supported by PHPExcel
1195
					break;
1196
 
1197
				case 0x0A:	//	Total Editing Time
1198
					//	Not supported by PHPExcel
1199
					break;
1200
 
1201
				case 0x0B:	//	Last Printed
1202
					//	Not supported by PHPExcel
1203
					break;
1204
 
1205
				case 0x0C:	//	Created Date/Time
1206
					$this->_phpExcel->getProperties()->setCreated($value);
1207
					break;
1208
 
1209
				case 0x0D:	//	Modified Date/Time
1210
					$this->_phpExcel->getProperties()->setModified($value);
1211
					break;
1212
 
1213
				case 0x0E:	//	Number of Pages
1214
					//	Not supported by PHPExcel
1215
					break;
1216
 
1217
				case 0x0F:	//	Number of Words
1218
					//	Not supported by PHPExcel
1219
					break;
1220
 
1221
				case 0x10:	//	Number of Characters
1222
					//	Not supported by PHPExcel
1223
					break;
1224
 
1225
				case 0x11:	//	Thumbnail
1226
					//	Not supported by PHPExcel
1227
					break;
1228
 
1229
				case 0x12:	//	Name of creating application
1230
					//	Not supported by PHPExcel
1231
					break;
1232
 
1233
				case 0x13:	//	Security
1234
					//	Not supported by PHPExcel
1235
					break;
1236
 
1237
			}
1238
		}
1239
	}
1240
 
1241
 
1242
	/**
1243
	 * Read additional document summary information
1244
	 */
1245
	private function _readDocumentSummaryInformation()
1246
	{
1247
		if (!isset($this->_documentSummaryInformation)) {
1248
			return;
1249
		}
1250
 
1251
		//	offset: 0;	size: 2;	must be 0xFE 0xFF (UTF-16 LE byte order mark)
1252
		//	offset: 2;	size: 2;
1253
		//	offset: 4;	size: 2;	OS version
1254
		//	offset: 6;	size: 2;	OS indicator
1255
		//	offset: 8;	size: 16
1256
		//	offset: 24;	size: 4;	section count
1257
		$secCount = self::_GetInt4d($this->_documentSummaryInformation, 24);
1258
//		echo '$secCount = ',$secCount,'<br />';
1259
 
1260
		// offset: 28;	size: 16;	first section's class id: 02 d5 cd d5 9c 2e 1b 10 93 97 08 00 2b 2c f9 ae
1261
		// offset: 44;	size: 4;	first section offset
1262
		$secOffset = self::_GetInt4d($this->_documentSummaryInformation, 44);
1263
//		echo '$secOffset = ',$secOffset,'<br />';
1264
 
1265
		//	section header
1266
		//	offset: $secOffset;	size: 4;	section length
1267
		$secLength = self::_GetInt4d($this->_documentSummaryInformation, $secOffset);
1268
//		echo '$secLength = ',$secLength,'<br />';
1269
 
1270
		//	offset: $secOffset+4;	size: 4;	property count
1271
		$countProperties = self::_GetInt4d($this->_documentSummaryInformation, $secOffset+4);
1272
//		echo '$countProperties = ',$countProperties,'<br />';
1273
 
1274
		// initialize code page (used to resolve string values)
1275
		$codePage = 'CP1252';
1276
 
1277
		//	offset: ($secOffset+8);	size: var
1278
		//	loop through property decarations and properties
1279
		for ($i = 0; $i < $countProperties; ++$i) {
1280
//			echo 'Property ',$i,'<br />';
1281
			//	offset: ($secOffset+8) + (8 * $i);	size: 4;	property ID
1282
			$id = self::_GetInt4d($this->_documentSummaryInformation, ($secOffset+8) + (8 * $i));
1283
//			echo 'ID is ',$id,'<br />';
1284
 
1285
			// Use value of property id as appropriate
1286
			// offset: 60 + 8 * $i;	size: 4;	offset from beginning of section (48)
1287
			$offset = self::_GetInt4d($this->_documentSummaryInformation, ($secOffset+12) + (8 * $i));
1288
 
1289
			$type = self::_GetInt4d($this->_documentSummaryInformation, $secOffset + $offset);
1290
//			echo 'Type is ',$type,', ';
1291
 
1292
			// initialize property value
1293
			$value = null;
1294
 
1295
			// extract property value based on property type
1296
			switch ($type) {
1297
				case 0x02:	//	2 byte signed integer
1298
					$value = self::_GetInt2d($this->_documentSummaryInformation, $secOffset + 4 + $offset);
1299
					break;
1300
 
1301
				case 0x03:	//	4 byte signed integer
1302
					$value = self::_GetInt4d($this->_documentSummaryInformation, $secOffset + 4 + $offset);
1303
					break;
1304
 
1305
				case 0x0B:  // Boolean
1306
					$value = self::_GetInt2d($this->_documentSummaryInformation, $secOffset + 4 + $offset);
1307
					$value = ($value == 0 ? false : true);
1308
					break;
1309
 
1310
				case 0x13:	//	4 byte unsigned integer
1311
					// not needed yet, fix later if necessary
1312
					break;
1313
 
1314
				case 0x1E:	//	null-terminated string prepended by dword string length
1315
					$byteLength = self::_GetInt4d($this->_documentSummaryInformation, $secOffset + 4 + $offset);
1316
					$value = substr($this->_documentSummaryInformation, $secOffset + 8 + $offset, $byteLength);
1317
					$value = PHPExcel_Shared_String::ConvertEncoding($value, 'UTF-8', $codePage);
1318
					$value = rtrim($value);
1319
					break;
1320
 
1321
				case 0x40:	//	Filetime (64-bit value representing the number of 100-nanosecond intervals since January 1, 1601)
1322
					// PHP-Time
1323
					$value = PHPExcel_Shared_OLE::OLE2LocalDate(substr($this->_documentSummaryInformation, $secOffset + 4 + $offset, 8));
1324
					break;
1325
 
1326
				case 0x47:	//	Clipboard format
1327
					// not needed yet, fix later if necessary
1328
					break;
1329
			}
1330
 
1331
			switch ($id) {
1332
				case 0x01:	//	Code Page
1333
					$codePage = PHPExcel_Shared_CodePage::NumberToName($value);
1334
					break;
1335
 
1336
				case 0x02:	//	Category
1337
					$this->_phpExcel->getProperties()->setCategory($value);
1338
					break;
1339
 
1340
				case 0x03:	//	Presentation Target
1341
					//	Not supported by PHPExcel
1342
					break;
1343
 
1344
				case 0x04:	//	Bytes
1345
					//	Not supported by PHPExcel
1346
					break;
1347
 
1348
				case 0x05:	//	Lines
1349
					//	Not supported by PHPExcel
1350
					break;
1351
 
1352
				case 0x06:	//	Paragraphs
1353
					//	Not supported by PHPExcel
1354
					break;
1355
 
1356
				case 0x07:	//	Slides
1357
					//	Not supported by PHPExcel
1358
					break;
1359
 
1360
				case 0x08:	//	Notes
1361
					//	Not supported by PHPExcel
1362
					break;
1363
 
1364
				case 0x09:	//	Hidden Slides
1365
					//	Not supported by PHPExcel
1366
					break;
1367
 
1368
				case 0x0A:	//	MM Clips
1369
					//	Not supported by PHPExcel
1370
					break;
1371
 
1372
				case 0x0B:	//	Scale Crop
1373
					//	Not supported by PHPExcel
1374
					break;
1375
 
1376
				case 0x0C:	//	Heading Pairs
1377
					//	Not supported by PHPExcel
1378
					break;
1379
 
1380
				case 0x0D:	//	Titles of Parts
1381
					//	Not supported by PHPExcel
1382
					break;
1383
 
1384
				case 0x0E:	//	Manager
1385
					$this->_phpExcel->getProperties()->setManager($value);
1386
					break;
1387
 
1388
				case 0x0F:	//	Company
1389
					$this->_phpExcel->getProperties()->setCompany($value);
1390
					break;
1391
 
1392
				case 0x10:	//	Links up-to-date
1393
					//	Not supported by PHPExcel
1394
					break;
1395
 
1396
			}
1397
		}
1398
	}
1399
 
1400
 
1401
	/**
1402
	 * Reads a general type of BIFF record. Does nothing except for moving stream pointer forward to next record.
1403
	 */
1404
	private function _readDefault()
1405
	{
1406
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
1407
//		$recordData = substr($this->_data, $this->_pos + 4, $length);
1408
 
1409
		// move stream pointer to next record
1410
		$this->_pos += 4 + $length;
1411
	}
1412
 
1413
 
1414
	/**
1415
	 *	The NOTE record specifies a comment associated with a particular cell. In Excel 95 (BIFF7) and earlier versions,
1416
	 *		this record stores a note (cell note). This feature was significantly enhanced in Excel 97.
1417
	 */
1418
	private function _readNote()
1419
	{
1420
//		echo '<b>Read Cell Annotation</b><br />';
1421
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
1422
		$recordData = substr($this->_data, $this->_pos + 4, $length);
1423
 
1424
		// move stream pointer to next record
1425
		$this->_pos += 4 + $length;
1426
 
1427
		if ($this->_readDataOnly) {
1428
			return;
1429
		}
1430
 
1431
		$cellAddress = $this->_readBIFF8CellAddress(substr($recordData, 0, 4));
1432
		if ($this->_version == self::XLS_BIFF8) {
1433
			$noteObjID = self::_GetInt2d($recordData, 6);
1434
			$noteAuthor = self::_readUnicodeStringLong(substr($recordData, 8));
1435
			$noteAuthor = $noteAuthor['value'];
1436
//			echo 'Note Address=',$cellAddress,'<br />';
1437
//			echo 'Note Object ID=',$noteObjID,'<br />';
1438
//			echo 'Note Author=',$noteAuthor,'<hr />';
1439
//
1440
			$this->_cellNotes[$noteObjID] = array('cellRef'		=> $cellAddress,
1441
												  'objectID'	=> $noteObjID,
1442
												  'author'		=> $noteAuthor
1443
												 );
1444
		} else {
1445
			$extension = false;
1446
			if ($cellAddress == '$B$65536') {
1447
				//	If the address row is -1 and the column is 0, (which translates as $B$65536) then this is a continuation
1448
				//		note from the previous cell annotation. We're not yet handling this, so annotations longer than the
1449
				//		max 2048 bytes will probably throw a wobbly.
1450
				$row = self::_GetInt2d($recordData, 0);
1451
				$extension = true;
1452
				$cellAddress = array_pop(array_keys($this->_phpSheet->getComments()));
1453
			}
1454
//			echo 'Note Address=',$cellAddress,'<br />';
1455
 
1456
			$cellAddress = str_replace('$','',$cellAddress);
1457
			$noteLength = self::_GetInt2d($recordData, 4);
1458
			$noteText = trim(substr($recordData, 6));
1459
//			echo 'Note Length=',$noteLength,'<br />';
1460
//			echo 'Note Text=',$noteText,'<br />';
1461
 
1462
			if ($extension) {
1463
				//	Concatenate this extension with the currently set comment for the cell
1464
				$comment = $this->_phpSheet->getComment( $cellAddress );
1465
				$commentText = $comment->getText()->getPlainText();
1466
				$comment->setText($this->_parseRichText($commentText.$noteText) );
1467
			} else {
1468
				//	Set comment for the cell
1469
				$this->_phpSheet->getComment( $cellAddress )
1470
//													->setAuthor( $author )
1471
													->setText($this->_parseRichText($noteText) );
1472
			}
1473
		}
1474
 
1475
	}
1476
 
1477
 
1478
	/**
1479
	 *	The TEXT Object record contains the text associated with a cell annotation.
1480
	 */
1481
	private function _readTextObject()
1482
	{
1483
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
1484
		$recordData = substr($this->_data, $this->_pos + 4, $length);
1485
 
1486
		// move stream pointer to next record
1487
		$this->_pos += 4 + $length;
1488
 
1489
		if ($this->_readDataOnly) {
1490
			return;
1491
		}
1492
 
1493
		// recordData consists of an array of subrecords looking like this:
1494
		//	grbit: 2 bytes; Option Flags
1495
		//	rot: 2 bytes; rotation
1496
		//	cchText: 2 bytes; length of the text (in the first continue record)
1497
		//	cbRuns: 2 bytes; length of the formatting (in the second continue record)
1498
		// followed by the continuation records containing the actual text and formatting
1499
		$grbitOpts	= self::_GetInt2d($recordData, 0);
1500
		$rot		= self::_GetInt2d($recordData, 2);
1501
		$cchText	= self::_GetInt2d($recordData, 10);
1502
		$cbRuns		= self::_GetInt2d($recordData, 12);
1503
		$text		= $this->_getSplicedRecordData();
1504
 
1505
		$this->_textObjects[$this->textObjRef] = array(
1506
				'text'		=> substr($text["recordData"],$text["spliceOffsets"][0]+1,$cchText),
1507
				'format'	=> substr($text["recordData"],$text["spliceOffsets"][1],$cbRuns),
1508
				'alignment'	=> $grbitOpts,
1509
				'rotation'	=> $rot
1510
			 );
1511
 
1512
//		echo '<b>_readTextObject()</b><br />';
1513
//		var_dump($this->_textObjects[$this->textObjRef]);
1514
//		echo '<br />';
1515
	}
1516
 
1517
 
1518
	/**
1519
	 * Read BOF
1520
	 */
1521
	private function _readBof()
1522
	{
1523
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
1524
		$recordData = substr($this->_data, $this->_pos + 4, $length);
1525
 
1526
		// move stream pointer to next record
1527
		$this->_pos += 4 + $length;
1528
 
1529
		// offset: 2; size: 2; type of the following data
1530
		$substreamType = self::_GetInt2d($recordData, 2);
1531
 
1532
		switch ($substreamType) {
1533
			case self::XLS_WorkbookGlobals:
1534
				$version = self::_GetInt2d($recordData, 0);
1535
				if (($version != self::XLS_BIFF8) && ($version != self::XLS_BIFF7)) {
1536
					throw new PHPExcel_Reader_Exception('Cannot read this Excel file. Version is too old.');
1537
				}
1538
				$this->_version = $version;
1539
				break;
1540
 
1541
			case self::XLS_Worksheet:
1542
				// do not use this version information for anything
1543
				// it is unreliable (OpenOffice doc, 5.8), use only version information from the global stream
1544
				break;
1545
 
1546
			default:
1547
				// substream, e.g. chart
1548
				// just skip the entire substream
1549
				do {
1550
					$code = self::_GetInt2d($this->_data, $this->_pos);
1551
					$this->_readDefault();
1552
				} while ($code != self::XLS_Type_EOF && $this->_pos < $this->_dataSize);
1553
				break;
1554
		}
1555
	}
1556
 
1557
 
1558
	/**
1559
	 * FILEPASS
1560
	 *
1561
	 * This record is part of the File Protection Block. It
1562
	 * contains information about the read/write password of the
1563
	 * file. All record contents following this record will be
1564
	 * encrypted.
1565
	 *
1566
	 * --	"OpenOffice.org's Documentation of the Microsoft
1567
	 * 		Excel File Format"
1568
	 */
1569
	private function _readFilepass()
1570
	{
1571
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
1572
//		$recordData = substr($this->_data, $this->_pos + 4, $length);
1573
 
1574
		// move stream pointer to next record
1575
		$this->_pos += 4 + $length;
1576
 
1577
		throw new PHPExcel_Reader_Exception('Cannot read encrypted file');
1578
	}
1579
 
1580
 
1581
	/**
1582
	 * CODEPAGE
1583
	 *
1584
	 * This record stores the text encoding used to write byte
1585
	 * strings, stored as MS Windows code page identifier.
1586
	 *
1587
	 * --	"OpenOffice.org's Documentation of the Microsoft
1588
	 * 		Excel File Format"
1589
	 */
1590
	private function _readCodepage()
1591
	{
1592
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
1593
		$recordData = substr($this->_data, $this->_pos + 4, $length);
1594
 
1595
		// move stream pointer to next record
1596
		$this->_pos += 4 + $length;
1597
 
1598
		// offset: 0; size: 2; code page identifier
1599
		$codepage = self::_GetInt2d($recordData, 0);
1600
 
1601
		$this->_codepage = PHPExcel_Shared_CodePage::NumberToName($codepage);
1602
	}
1603
 
1604
 
1605
	/**
1606
	 * DATEMODE
1607
	 *
1608
	 * This record specifies the base date for displaying date
1609
	 * values. All dates are stored as count of days past this
1610
	 * base date. In BIFF2-BIFF4 this record is part of the
1611
	 * Calculation Settings Block. In BIFF5-BIFF8 it is
1612
	 * stored in the Workbook Globals Substream.
1613
	 *
1614
	 * --	"OpenOffice.org's Documentation of the Microsoft
1615
	 * 		Excel File Format"
1616
	 */
1617
	private function _readDateMode()
1618
	{
1619
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
1620
		$recordData = substr($this->_data, $this->_pos + 4, $length);
1621
 
1622
		// move stream pointer to next record
1623
		$this->_pos += 4 + $length;
1624
 
1625
		// offset: 0; size: 2; 0 = base 1900, 1 = base 1904
1626
		PHPExcel_Shared_Date::setExcelCalendar(PHPExcel_Shared_Date::CALENDAR_WINDOWS_1900);
1627
		if (ord($recordData{0}) == 1) {
1628
			PHPExcel_Shared_Date::setExcelCalendar(PHPExcel_Shared_Date::CALENDAR_MAC_1904);
1629
		}
1630
	}
1631
 
1632
 
1633
	/**
1634
	 * Read a FONT record
1635
	 */
1636
	private function _readFont()
1637
	{
1638
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
1639
		$recordData = substr($this->_data, $this->_pos + 4, $length);
1640
 
1641
		// move stream pointer to next record
1642
		$this->_pos += 4 + $length;
1643
 
1644
		if (!$this->_readDataOnly) {
1645
			$objFont = new PHPExcel_Style_Font();
1646
 
1647
			// offset: 0; size: 2; height of the font (in twips = 1/20 of a point)
1648
			$size = self::_GetInt2d($recordData, 0);
1649
			$objFont->setSize($size / 20);
1650
 
1651
			// offset: 2; size: 2; option flags
1652
				// bit: 0; mask 0x0001; bold (redundant in BIFF5-BIFF8)
1653
				// bit: 1; mask 0x0002; italic
1654
				$isItalic = (0x0002 & self::_GetInt2d($recordData, 2)) >> 1;
1655
				if ($isItalic) $objFont->setItalic(true);
1656
 
1657
				// bit: 2; mask 0x0004; underlined (redundant in BIFF5-BIFF8)
1658
				// bit: 3; mask 0x0008; strike
1659
				$isStrike = (0x0008 & self::_GetInt2d($recordData, 2)) >> 3;
1660
				if ($isStrike) $objFont->setStrikethrough(true);
1661
 
1662
			// offset: 4; size: 2; colour index
1663
			$colorIndex = self::_GetInt2d($recordData, 4);
1664
			$objFont->colorIndex = $colorIndex;
1665
 
1666
			// offset: 6; size: 2; font weight
1667
			$weight = self::_GetInt2d($recordData, 6);
1668
			switch ($weight) {
1669
				case 0x02BC:
1670
					$objFont->setBold(true);
1671
					break;
1672
			}
1673
 
1674
			// offset: 8; size: 2; escapement type
1675
			$escapement = self::_GetInt2d($recordData, 8);
1676
			switch ($escapement) {
1677
				case 0x0001:
1678
					$objFont->setSuperScript(true);
1679
					break;
1680
				case 0x0002:
1681
					$objFont->setSubScript(true);
1682
					break;
1683
			}
1684
 
1685
			// offset: 10; size: 1; underline type
1686
			$underlineType = ord($recordData{10});
1687
			switch ($underlineType) {
1688
				case 0x00:
1689
					break; // no underline
1690
				case 0x01:
1691
					$objFont->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
1692
					break;
1693
				case 0x02:
1694
					$objFont->setUnderline(PHPExcel_Style_Font::UNDERLINE_DOUBLE);
1695
					break;
1696
				case 0x21:
1697
					$objFont->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLEACCOUNTING);
1698
					break;
1699
				case 0x22:
1700
					$objFont->setUnderline(PHPExcel_Style_Font::UNDERLINE_DOUBLEACCOUNTING);
1701
					break;
1702
			}
1703
 
1704
			// offset: 11; size: 1; font family
1705
			// offset: 12; size: 1; character set
1706
			// offset: 13; size: 1; not used
1707
			// offset: 14; size: var; font name
1708
			if ($this->_version == self::XLS_BIFF8) {
1709
				$string = self::_readUnicodeStringShort(substr($recordData, 14));
1710
			} else {
1711
				$string = $this->_readByteStringShort(substr($recordData, 14));
1712
			}
1713
			$objFont->setName($string['value']);
1714
 
1715
			$this->_objFonts[] = $objFont;
1716
		}
1717
	}
1718
 
1719
 
1720
	/**
1721
	 * FORMAT
1722
	 *
1723
	 * This record contains information about a number format.
1724
	 * All FORMAT records occur together in a sequential list.
1725
	 *
1726
	 * In BIFF2-BIFF4 other records referencing a FORMAT record
1727
	 * contain a zero-based index into this list. From BIFF5 on
1728
	 * the FORMAT record contains the index itself that will be
1729
	 * used by other records.
1730
	 *
1731
	 * --	"OpenOffice.org's Documentation of the Microsoft
1732
	 * 		Excel File Format"
1733
	 */
1734
	private function _readFormat()
1735
	{
1736
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
1737
		$recordData = substr($this->_data, $this->_pos + 4, $length);
1738
 
1739
		// move stream pointer to next record
1740
		$this->_pos += 4 + $length;
1741
 
1742
		if (!$this->_readDataOnly) {
1743
			$indexCode = self::_GetInt2d($recordData, 0);
1744
 
1745
			if ($this->_version == self::XLS_BIFF8) {
1746
				$string = self::_readUnicodeStringLong(substr($recordData, 2));
1747
			} else {
1748
				// BIFF7
1749
				$string = $this->_readByteStringShort(substr($recordData, 2));
1750
			}
1751
 
1752
			$formatString = $string['value'];
1753
			$this->_formats[$indexCode] = $formatString;
1754
		}
1755
	}
1756
 
1757
 
1758
	/**
1759
	 * XF - Extended Format
1760
	 *
1761
	 * This record contains formatting information for cells, rows, columns or styles.
1762
	 * According to http://support.microsoft.com/kb/147732 there are always at least 15 cell style XF
1763
	 * and 1 cell XF.
1764
	 * Inspection of Excel files generated by MS Office Excel shows that XF records 0-14 are cell style XF
1765
	 * and XF record 15 is a cell XF
1766
	 * We only read the first cell style XF and skip the remaining cell style XF records
1767
	 * We read all cell XF records.
1768
	 *
1769
	 * --	"OpenOffice.org's Documentation of the Microsoft
1770
	 * 		Excel File Format"
1771
	 */
1772
	private function _readXf()
1773
	{
1774
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
1775
		$recordData = substr($this->_data, $this->_pos + 4, $length);
1776
 
1777
		// move stream pointer to next record
1778
		$this->_pos += 4 + $length;
1779
 
1780
		$objStyle = new PHPExcel_Style();
1781
 
1782
		if (!$this->_readDataOnly) {
1783
			// offset:  0; size: 2; Index to FONT record
1784
			if (self::_GetInt2d($recordData, 0) < 4) {
1785
				$fontIndex = self::_GetInt2d($recordData, 0);
1786
			} else {
1787
				// this has to do with that index 4 is omitted in all BIFF versions for some strange reason
1788
				// check the OpenOffice documentation of the FONT record
1789
				$fontIndex = self::_GetInt2d($recordData, 0) - 1;
1790
			}
1791
			$objStyle->setFont($this->_objFonts[$fontIndex]);
1792
 
1793
			// offset:  2; size: 2; Index to FORMAT record
1794
			$numberFormatIndex = self::_GetInt2d($recordData, 2);
1795
			if (isset($this->_formats[$numberFormatIndex])) {
1796
				// then we have user-defined format code
1797
				$numberformat = array('code' => $this->_formats[$numberFormatIndex]);
1798
			} elseif (($code = PHPExcel_Style_NumberFormat::builtInFormatCode($numberFormatIndex)) !== '') {
1799
				// then we have built-in format code
1800
				$numberformat = array('code' => $code);
1801
			} else {
1802
				// we set the general format code
1803
				$numberformat = array('code' => 'General');
1804
			}
1805
			$objStyle->getNumberFormat()->setFormatCode($numberformat['code']);
1806
 
1807
			// offset:  4; size: 2; XF type, cell protection, and parent style XF
1808
			// bit 2-0; mask 0x0007; XF_TYPE_PROT
1809
			$xfTypeProt = self::_GetInt2d($recordData, 4);
1810
			// bit 0; mask 0x01; 1 = cell is locked
1811
			$isLocked = (0x01 & $xfTypeProt) >> 0;
1812
			$objStyle->getProtection()->setLocked($isLocked ?
1813
				PHPExcel_Style_Protection::PROTECTION_INHERIT : PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);
1814
 
1815
			// bit 1; mask 0x02; 1 = Formula is hidden
1816
			$isHidden = (0x02 & $xfTypeProt) >> 1;
1817
			$objStyle->getProtection()->setHidden($isHidden ?
1818
				PHPExcel_Style_Protection::PROTECTION_PROTECTED : PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);
1819
 
1820
			// bit 2; mask 0x04; 0 = Cell XF, 1 = Cell Style XF
1821
			$isCellStyleXf = (0x04 & $xfTypeProt) >> 2;
1822
 
1823
			// offset:  6; size: 1; Alignment and text break
1824
			// bit 2-0, mask 0x07; horizontal alignment
1825
			$horAlign = (0x07 & ord($recordData{6})) >> 0;
1826
			switch ($horAlign) {
1827
				case 0:
1828
					$objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_GENERAL);
1829
					break;
1830
				case 1:
1831
					$objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
1832
					break;
1833
				case 2:
1834
					$objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
1835
					break;
1836
				case 3:
1837
					$objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
1838
					break;
1839
				case 5:
1840
					$objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);
1841
					break;
1842
				case 6:
1843
					$objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER_CONTINUOUS);
1844
					break;
1845
			}
1846
			// bit 3, mask 0x08; wrap text
1847
			$wrapText = (0x08 & ord($recordData{6})) >> 3;
1848
			switch ($wrapText) {
1849
				case 0:
1850
					$objStyle->getAlignment()->setWrapText(false);
1851
					break;
1852
				case 1:
1853
					$objStyle->getAlignment()->setWrapText(true);
1854
					break;
1855
			}
1856
			// bit 6-4, mask 0x70; vertical alignment
1857
			$vertAlign = (0x70 & ord($recordData{6})) >> 4;
1858
			switch ($vertAlign) {
1859
				case 0:
1860
					$objStyle->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
1861
					break;
1862
				case 1:
1863
					$objStyle->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
1864
					break;
1865
				case 2:
1866
					$objStyle->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_BOTTOM);
1867
					break;
1868
				case 3:
1869
					$objStyle->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_JUSTIFY);
1870
					break;
1871
			}
1872
 
1873
			if ($this->_version == self::XLS_BIFF8) {
1874
				// offset:  7; size: 1; XF_ROTATION: Text rotation angle
1875
					$angle = ord($recordData{7});
1876
					$rotation = 0;
1877
					if ($angle <= 90) {
1878
						$rotation = $angle;
1879
					} else if ($angle <= 180) {
1880
						$rotation = 90 - $angle;
1881
					} else if ($angle == 255) {
1882
						$rotation = -165;
1883
					}
1884
					$objStyle->getAlignment()->setTextRotation($rotation);
1885
 
1886
				// offset:  8; size: 1; Indentation, shrink to cell size, and text direction
1887
					// bit: 3-0; mask: 0x0F; indent level
1888
					$indent = (0x0F & ord($recordData{8})) >> 0;
1889
					$objStyle->getAlignment()->setIndent($indent);
1890
 
1891
					// bit: 4; mask: 0x10; 1 = shrink content to fit into cell
1892
					$shrinkToFit = (0x10 & ord($recordData{8})) >> 4;
1893
					switch ($shrinkToFit) {
1894
						case 0:
1895
							$objStyle->getAlignment()->setShrinkToFit(false);
1896
							break;
1897
						case 1:
1898
							$objStyle->getAlignment()->setShrinkToFit(true);
1899
							break;
1900
					}
1901
 
1902
				// offset:  9; size: 1; Flags used for attribute groups
1903
 
1904
				// offset: 10; size: 4; Cell border lines and background area
1905
					// bit: 3-0; mask: 0x0000000F; left style
1906
					if ($bordersLeftStyle = self::_mapBorderStyle((0x0000000F & self::_GetInt4d($recordData, 10)) >> 0)) {
1907
						$objStyle->getBorders()->getLeft()->setBorderStyle($bordersLeftStyle);
1908
					}
1909
					// bit: 7-4; mask: 0x000000F0; right style
1910
					if ($bordersRightStyle = self::_mapBorderStyle((0x000000F0 & self::_GetInt4d($recordData, 10)) >> 4)) {
1911
						$objStyle->getBorders()->getRight()->setBorderStyle($bordersRightStyle);
1912
					}
1913
					// bit: 11-8; mask: 0x00000F00; top style
1914
					if ($bordersTopStyle = self::_mapBorderStyle((0x00000F00 & self::_GetInt4d($recordData, 10)) >> 8)) {
1915
						$objStyle->getBorders()->getTop()->setBorderStyle($bordersTopStyle);
1916
					}
1917
					// bit: 15-12; mask: 0x0000F000; bottom style
1918
					if ($bordersBottomStyle = self::_mapBorderStyle((0x0000F000 & self::_GetInt4d($recordData, 10)) >> 12)) {
1919
						$objStyle->getBorders()->getBottom()->setBorderStyle($bordersBottomStyle);
1920
					}
1921
					// bit: 22-16; mask: 0x007F0000; left color
1922
					$objStyle->getBorders()->getLeft()->colorIndex = (0x007F0000 & self::_GetInt4d($recordData, 10)) >> 16;
1923
 
1924
					// bit: 29-23; mask: 0x3F800000; right color
1925
					$objStyle->getBorders()->getRight()->colorIndex = (0x3F800000 & self::_GetInt4d($recordData, 10)) >> 23;
1926
 
1927
					// bit: 30; mask: 0x40000000; 1 = diagonal line from top left to right bottom
1928
					$diagonalDown = (0x40000000 & self::_GetInt4d($recordData, 10)) >> 30 ?
1929
						true : false;
1930
 
1931
					// bit: 31; mask: 0x80000000; 1 = diagonal line from bottom left to top right
1932
					$diagonalUp = (0x80000000 & self::_GetInt4d($recordData, 10)) >> 31 ?
1933
						true : false;
1934
 
1935
					if ($diagonalUp == false && $diagonalDown == false) {
1936
						$objStyle->getBorders()->setDiagonalDirection(PHPExcel_Style_Borders::DIAGONAL_NONE);
1937
					} elseif ($diagonalUp == true && $diagonalDown == false) {
1938
						$objStyle->getBorders()->setDiagonalDirection(PHPExcel_Style_Borders::DIAGONAL_UP);
1939
					} elseif ($diagonalUp == false && $diagonalDown == true) {
1940
						$objStyle->getBorders()->setDiagonalDirection(PHPExcel_Style_Borders::DIAGONAL_DOWN);
1941
					} elseif ($diagonalUp == true && $diagonalDown == true) {
1942
						$objStyle->getBorders()->setDiagonalDirection(PHPExcel_Style_Borders::DIAGONAL_BOTH);
1943
					}
1944
 
1945
				// offset: 14; size: 4;
1946
					// bit: 6-0; mask: 0x0000007F; top color
1947
					$objStyle->getBorders()->getTop()->colorIndex = (0x0000007F & self::_GetInt4d($recordData, 14)) >> 0;
1948
 
1949
					// bit: 13-7; mask: 0x00003F80; bottom color
1950
					$objStyle->getBorders()->getBottom()->colorIndex = (0x00003F80 & self::_GetInt4d($recordData, 14)) >> 7;
1951
 
1952
					// bit: 20-14; mask: 0x001FC000; diagonal color
1953
					$objStyle->getBorders()->getDiagonal()->colorIndex = (0x001FC000 & self::_GetInt4d($recordData, 14)) >> 14;
1954
 
1955
					// bit: 24-21; mask: 0x01E00000; diagonal style
1956
					if ($bordersDiagonalStyle = self::_mapBorderStyle((0x01E00000 & self::_GetInt4d($recordData, 14)) >> 21)) {
1957
						$objStyle->getBorders()->getDiagonal()->setBorderStyle($bordersDiagonalStyle);
1958
					}
1959
 
1960
					// bit: 31-26; mask: 0xFC000000 fill pattern
1961
					if ($fillType = self::_mapFillPattern((0xFC000000 & self::_GetInt4d($recordData, 14)) >> 26)) {
1962
						$objStyle->getFill()->setFillType($fillType);
1963
					}
1964
				// offset: 18; size: 2; pattern and background colour
1965
					// bit: 6-0; mask: 0x007F; color index for pattern color
1966
					$objStyle->getFill()->startcolorIndex = (0x007F & self::_GetInt2d($recordData, 18)) >> 0;
1967
 
1968
					// bit: 13-7; mask: 0x3F80; color index for pattern background
1969
					$objStyle->getFill()->endcolorIndex = (0x3F80 & self::_GetInt2d($recordData, 18)) >> 7;
1970
			} else {
1971
				// BIFF5
1972
 
1973
				// offset: 7; size: 1; Text orientation and flags
1974
				$orientationAndFlags = ord($recordData{7});
1975
 
1976
				// bit: 1-0; mask: 0x03; XF_ORIENTATION: Text orientation
1977
				$xfOrientation = (0x03 & $orientationAndFlags) >> 0;
1978
				switch ($xfOrientation) {
1979
					case 0:
1980
						$objStyle->getAlignment()->setTextRotation(0);
1981
						break;
1982
					case 1:
1983
						$objStyle->getAlignment()->setTextRotation(-165);
1984
						break;
1985
					case 2:
1986
						$objStyle->getAlignment()->setTextRotation(90);
1987
						break;
1988
					case 3:
1989
						$objStyle->getAlignment()->setTextRotation(-90);
1990
						break;
1991
				}
1992
 
1993
				// offset: 8; size: 4; cell border lines and background area
1994
				$borderAndBackground = self::_GetInt4d($recordData, 8);
1995
 
1996
				// bit: 6-0; mask: 0x0000007F; color index for pattern color
1997
				$objStyle->getFill()->startcolorIndex = (0x0000007F & $borderAndBackground) >> 0;
1998
 
1999
				// bit: 13-7; mask: 0x00003F80; color index for pattern background
2000
				$objStyle->getFill()->endcolorIndex = (0x00003F80 & $borderAndBackground) >> 7;
2001
 
2002
				// bit: 21-16; mask: 0x003F0000; fill pattern
2003
				$objStyle->getFill()->setFillType(self::_mapFillPattern((0x003F0000 & $borderAndBackground) >> 16));
2004
 
2005
				// bit: 24-22; mask: 0x01C00000; bottom line style
2006
				$objStyle->getBorders()->getBottom()->setBorderStyle(self::_mapBorderStyle((0x01C00000 & $borderAndBackground) >> 22));
2007
 
2008
				// bit: 31-25; mask: 0xFE000000; bottom line color
2009
				$objStyle->getBorders()->getBottom()->colorIndex = (0xFE000000 & $borderAndBackground) >> 25;
2010
 
2011
				// offset: 12; size: 4; cell border lines
2012
				$borderLines = self::_GetInt4d($recordData, 12);
2013
 
2014
				// bit: 2-0; mask: 0x00000007; top line style
2015
				$objStyle->getBorders()->getTop()->setBorderStyle(self::_mapBorderStyle((0x00000007 & $borderLines) >> 0));
2016
 
2017
				// bit: 5-3; mask: 0x00000038; left line style
2018
				$objStyle->getBorders()->getLeft()->setBorderStyle(self::_mapBorderStyle((0x00000038 & $borderLines) >> 3));
2019
 
2020
				// bit: 8-6; mask: 0x000001C0; right line style
2021
				$objStyle->getBorders()->getRight()->setBorderStyle(self::_mapBorderStyle((0x000001C0 & $borderLines) >> 6));
2022
 
2023
				// bit: 15-9; mask: 0x0000FE00; top line color index
2024
				$objStyle->getBorders()->getTop()->colorIndex = (0x0000FE00 & $borderLines) >> 9;
2025
 
2026
				// bit: 22-16; mask: 0x007F0000; left line color index
2027
				$objStyle->getBorders()->getLeft()->colorIndex = (0x007F0000 & $borderLines) >> 16;
2028
 
2029
				// bit: 29-23; mask: 0x3F800000; right line color index
2030
				$objStyle->getBorders()->getRight()->colorIndex = (0x3F800000 & $borderLines) >> 23;
2031
			}
2032
 
2033
			// add cellStyleXf or cellXf and update mapping
2034
			if ($isCellStyleXf) {
2035
				// we only read one style XF record which is always the first
2036
				if ($this->_xfIndex == 0) {
2037
					$this->_phpExcel->addCellStyleXf($objStyle);
2038
					$this->_mapCellStyleXfIndex[$this->_xfIndex] = 0;
2039
				}
2040
			} else {
2041
				// we read all cell XF records
2042
				$this->_phpExcel->addCellXf($objStyle);
2043
				$this->_mapCellXfIndex[$this->_xfIndex] = count($this->_phpExcel->getCellXfCollection()) - 1;
2044
			}
2045
 
2046
			// update XF index for when we read next record
2047
			++$this->_xfIndex;
2048
		}
2049
	}
2050
 
2051
 
2052
	/**
2053
	 *
2054
	 */
2055
	private function _readXfExt()
2056
	{
2057
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
2058
		$recordData = substr($this->_data, $this->_pos + 4, $length);
2059
 
2060
		// move stream pointer to next record
2061
		$this->_pos += 4 + $length;
2062
 
2063
		if (!$this->_readDataOnly) {
2064
			// offset: 0; size: 2; 0x087D = repeated header
2065
 
2066
			// offset: 2; size: 2
2067
 
2068
			// offset: 4; size: 8; not used
2069
 
2070
			// offset: 12; size: 2; record version
2071
 
2072
			// offset: 14; size: 2; index to XF record which this record modifies
2073
			$ixfe = self::_GetInt2d($recordData, 14);
2074
 
2075
			// offset: 16; size: 2; not used
2076
 
2077
			// offset: 18; size: 2; number of extension properties that follow
2078
			$cexts = self::_GetInt2d($recordData, 18);
2079
 
2080
			// start reading the actual extension data
2081
			$offset = 20;
2082
			while ($offset < $length) {
2083
				// extension type
2084
				$extType = self::_GetInt2d($recordData, $offset);
2085
 
2086
				// extension length
2087
				$cb = self::_GetInt2d($recordData, $offset + 2);
2088
 
2089
				// extension data
2090
				$extData = substr($recordData, $offset + 4, $cb);
2091
 
2092
				switch ($extType) {
2093
					case 4:		// fill start color
2094
						$xclfType  = self::_GetInt2d($extData, 0); // color type
2095
						$xclrValue = substr($extData, 4, 4); // color value (value based on color type)
2096
 
2097
						if ($xclfType == 2) {
2098
							$rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
2099
 
2100
							// modify the relevant style property
2101
							if ( isset($this->_mapCellXfIndex[$ixfe]) ) {
2102
								$fill = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getFill();
2103
								$fill->getStartColor()->setRGB($rgb);
2104
								unset($fill->startcolorIndex); // normal color index does not apply, discard
2105
							}
2106
						}
2107
						break;
2108
 
2109
					case 5:		// fill end color
2110
						$xclfType  = self::_GetInt2d($extData, 0); // color type
2111
						$xclrValue = substr($extData, 4, 4); // color value (value based on color type)
2112
 
2113
						if ($xclfType == 2) {
2114
							$rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
2115
 
2116
							// modify the relevant style property
2117
							if ( isset($this->_mapCellXfIndex[$ixfe]) ) {
2118
								$fill = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getFill();
2119
								$fill->getEndColor()->setRGB($rgb);
2120
								unset($fill->endcolorIndex); // normal color index does not apply, discard
2121
							}
2122
						}
2123
						break;
2124
 
2125
					case 7:		// border color top
2126
						$xclfType  = self::_GetInt2d($extData, 0); // color type
2127
						$xclrValue = substr($extData, 4, 4); // color value (value based on color type)
2128
 
2129
						if ($xclfType == 2) {
2130
							$rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
2131
 
2132
							// modify the relevant style property
2133
							if ( isset($this->_mapCellXfIndex[$ixfe]) ) {
2134
								$top = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getBorders()->getTop();
2135
								$top->getColor()->setRGB($rgb);
2136
								unset($top->colorIndex); // normal color index does not apply, discard
2137
							}
2138
						}
2139
						break;
2140
 
2141
					case 8:		// border color bottom
2142
						$xclfType  = self::_GetInt2d($extData, 0); // color type
2143
						$xclrValue = substr($extData, 4, 4); // color value (value based on color type)
2144
 
2145
						if ($xclfType == 2) {
2146
							$rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
2147
 
2148
							// modify the relevant style property
2149
							if ( isset($this->_mapCellXfIndex[$ixfe]) ) {
2150
								$bottom = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getBorders()->getBottom();
2151
								$bottom->getColor()->setRGB($rgb);
2152
								unset($bottom->colorIndex); // normal color index does not apply, discard
2153
							}
2154
						}
2155
						break;
2156
 
2157
					case 9:		// border color left
2158
						$xclfType  = self::_GetInt2d($extData, 0); // color type
2159
						$xclrValue = substr($extData, 4, 4); // color value (value based on color type)
2160
 
2161
						if ($xclfType == 2) {
2162
							$rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
2163
 
2164
							// modify the relevant style property
2165
							if ( isset($this->_mapCellXfIndex[$ixfe]) ) {
2166
								$left = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getBorders()->getLeft();
2167
								$left->getColor()->setRGB($rgb);
2168
								unset($left->colorIndex); // normal color index does not apply, discard
2169
							}
2170
						}
2171
						break;
2172
 
2173
					case 10:		// border color right
2174
						$xclfType  = self::_GetInt2d($extData, 0); // color type
2175
						$xclrValue = substr($extData, 4, 4); // color value (value based on color type)
2176
 
2177
						if ($xclfType == 2) {
2178
							$rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
2179
 
2180
							// modify the relevant style property
2181
							if ( isset($this->_mapCellXfIndex[$ixfe]) ) {
2182
								$right = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getBorders()->getRight();
2183
								$right->getColor()->setRGB($rgb);
2184
								unset($right->colorIndex); // normal color index does not apply, discard
2185
							}
2186
						}
2187
						break;
2188
 
2189
					case 11:		// border color diagonal
2190
						$xclfType  = self::_GetInt2d($extData, 0); // color type
2191
						$xclrValue = substr($extData, 4, 4); // color value (value based on color type)
2192
 
2193
						if ($xclfType == 2) {
2194
							$rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
2195
 
2196
							// modify the relevant style property
2197
							if ( isset($this->_mapCellXfIndex[$ixfe]) ) {
2198
								$diagonal = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getBorders()->getDiagonal();
2199
								$diagonal->getColor()->setRGB($rgb);
2200
								unset($diagonal->colorIndex); // normal color index does not apply, discard
2201
							}
2202
						}
2203
						break;
2204
 
2205
					case 13:	// font color
2206
						$xclfType  = self::_GetInt2d($extData, 0); // color type
2207
						$xclrValue = substr($extData, 4, 4); // color value (value based on color type)
2208
 
2209
						if ($xclfType == 2) {
2210
							$rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
2211
 
2212
							// modify the relevant style property
2213
							if ( isset($this->_mapCellXfIndex[$ixfe]) ) {
2214
								$font = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getFont();
2215
								$font->getColor()->setRGB($rgb);
2216
								unset($font->colorIndex); // normal color index does not apply, discard
2217
							}
2218
						}
2219
						break;
2220
				}
2221
 
2222
				$offset += $cb;
2223
			}
2224
		}
2225
 
2226
	}
2227
 
2228
 
2229
	/**
2230
	 * Read STYLE record
2231
	 */
2232
	private function _readStyle()
2233
	{
2234
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
2235
		$recordData = substr($this->_data, $this->_pos + 4, $length);
2236
 
2237
		// move stream pointer to next record
2238
		$this->_pos += 4 + $length;
2239
 
2240
		if (!$this->_readDataOnly) {
2241
			// offset: 0; size: 2; index to XF record and flag for built-in style
2242
			$ixfe = self::_GetInt2d($recordData, 0);
2243
 
2244
			// bit: 11-0; mask 0x0FFF; index to XF record
2245
			$xfIndex = (0x0FFF & $ixfe) >> 0;
2246
 
2247
			// bit: 15; mask 0x8000; 0 = user-defined style, 1 = built-in style
2248
			$isBuiltIn = (bool) ((0x8000 & $ixfe) >> 15);
2249
 
2250
			if ($isBuiltIn) {
2251
				// offset: 2; size: 1; identifier for built-in style
2252
				$builtInId = ord($recordData{2});
2253
 
2254
				switch ($builtInId) {
2255
				case 0x00:
2256
					// currently, we are not using this for anything
2257
					break;
2258
 
2259
				default:
2260
					break;
2261
				}
2262
 
2263
			} else {
2264
				// user-defined; not supported by PHPExcel
2265
			}
2266
		}
2267
	}
2268
 
2269
 
2270
	/**
2271
	 * Read PALETTE record
2272
	 */
2273
	private function _readPalette()
2274
	{
2275
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
2276
		$recordData = substr($this->_data, $this->_pos + 4, $length);
2277
 
2278
		// move stream pointer to next record
2279
		$this->_pos += 4 + $length;
2280
 
2281
		if (!$this->_readDataOnly) {
2282
			// offset: 0; size: 2; number of following colors
2283
			$nm = self::_GetInt2d($recordData, 0);
2284
 
2285
			// list of RGB colors
2286
			for ($i = 0; $i < $nm; ++$i) {
2287
				$rgb = substr($recordData, 2 + 4 * $i, 4);
2288
				$this->_palette[] = self::_readRGB($rgb);
2289
			}
2290
		}
2291
	}
2292
 
2293
 
2294
	/**
2295
	 * SHEET
2296
	 *
2297
	 * This record is  located in the  Workbook Globals
2298
	 * Substream  and represents a sheet inside the workbook.
2299
	 * One SHEET record is written for each sheet. It stores the
2300
	 * sheet name and a stream offset to the BOF record of the
2301
	 * respective Sheet Substream within the Workbook Stream.
2302
	 *
2303
	 * --	"OpenOffice.org's Documentation of the Microsoft
2304
	 * 		Excel File Format"
2305
	 */
2306
	private function _readSheet()
2307
	{
2308
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
2309
		$recordData = substr($this->_data, $this->_pos + 4, $length);
2310
 
2311
		// move stream pointer to next record
2312
		$this->_pos += 4 + $length;
2313
 
2314
		// offset: 0; size: 4; absolute stream position of the BOF record of the sheet
2315
		$rec_offset = self::_GetInt4d($recordData, 0);
2316
 
2317
		// offset: 4; size: 1; sheet state
2318
		switch (ord($recordData{4})) {
2319
			case 0x00: $sheetState = PHPExcel_Worksheet::SHEETSTATE_VISIBLE;    break;
2320
			case 0x01: $sheetState = PHPExcel_Worksheet::SHEETSTATE_HIDDEN;     break;
2321
			case 0x02: $sheetState = PHPExcel_Worksheet::SHEETSTATE_VERYHIDDEN; break;
2322
			default: $sheetState = PHPExcel_Worksheet::SHEETSTATE_VISIBLE;      break;
2323
		}
2324
 
2325
		// offset: 5; size: 1; sheet type
2326
		$sheetType = ord($recordData{5});
2327
 
2328
		// offset: 6; size: var; sheet name
2329
		if ($this->_version == self::XLS_BIFF8) {
2330
			$string = self::_readUnicodeStringShort(substr($recordData, 6));
2331
			$rec_name = $string['value'];
2332
		} elseif ($this->_version == self::XLS_BIFF7) {
2333
			$string = $this->_readByteStringShort(substr($recordData, 6));
2334
			$rec_name = $string['value'];
2335
		}
2336
 
2337
		$this->_sheets[] = array(
2338
			'name' => $rec_name,
2339
			'offset' => $rec_offset,
2340
			'sheetState' => $sheetState,
2341
			'sheetType' => $sheetType,
2342
		);
2343
	}
2344
 
2345
 
2346
	/**
2347
	 * Read EXTERNALBOOK record
2348
	 */
2349
	private function _readExternalBook()
2350
	{
2351
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
2352
		$recordData = substr($this->_data, $this->_pos + 4, $length);
2353
 
2354
		// move stream pointer to next record
2355
		$this->_pos += 4 + $length;
2356
 
2357
		// offset within record data
2358
		$offset = 0;
2359
 
2360
		// there are 4 types of records
2361
		if (strlen($recordData) > 4) {
2362
			// external reference
2363
			// offset: 0; size: 2; number of sheet names ($nm)
2364
			$nm = self::_GetInt2d($recordData, 0);
2365
			$offset += 2;
2366
 
2367
			// offset: 2; size: var; encoded URL without sheet name (Unicode string, 16-bit length)
2368
			$encodedUrlString = self::_readUnicodeStringLong(substr($recordData, 2));
2369
			$offset += $encodedUrlString['size'];
2370
 
2371
			// offset: var; size: var; list of $nm sheet names (Unicode strings, 16-bit length)
2372
			$externalSheetNames = array();
2373
			for ($i = 0; $i < $nm; ++$i) {
2374
				$externalSheetNameString = self::_readUnicodeStringLong(substr($recordData, $offset));
2375
				$externalSheetNames[] = $externalSheetNameString['value'];
2376
				$offset += $externalSheetNameString['size'];
2377
			}
2378
 
2379
			// store the record data
2380
			$this->_externalBooks[] = array(
2381
				'type' => 'external',
2382
				'encodedUrl' => $encodedUrlString['value'],
2383
				'externalSheetNames' => $externalSheetNames,
2384
			);
2385
 
2386
		} elseif (substr($recordData, 2, 2) == pack('CC', 0x01, 0x04)) {
2387
			// internal reference
2388
			// offset: 0; size: 2; number of sheet in this document
2389
			// offset: 2; size: 2; 0x01 0x04
2390
			$this->_externalBooks[] = array(
2391
				'type' => 'internal',
2392
			);
2393
		} elseif (substr($recordData, 0, 4) == pack('vCC', 0x0001, 0x01, 0x3A)) {
2394
			// add-in function
2395
			// offset: 0; size: 2; 0x0001
2396
			$this->_externalBooks[] = array(
2397
				'type' => 'addInFunction',
2398
			);
2399
		} elseif (substr($recordData, 0, 2) == pack('v', 0x0000)) {
2400
			// DDE links, OLE links
2401
			// offset: 0; size: 2; 0x0000
2402
			// offset: 2; size: var; encoded source document name
2403
			$this->_externalBooks[] = array(
2404
				'type' => 'DDEorOLE',
2405
			);
2406
		}
2407
	}
2408
 
2409
 
2410
	/**
2411
	 * Read EXTERNNAME record.
2412
	 */
2413
	private function _readExternName()
2414
	{
2415
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
2416
		$recordData = substr($this->_data, $this->_pos + 4, $length);
2417
 
2418
		// move stream pointer to next record
2419
		$this->_pos += 4 + $length;
2420
 
2421
		// external sheet references provided for named cells
2422
		if ($this->_version == self::XLS_BIFF8) {
2423
			// offset: 0; size: 2; options
2424
			$options = self::_GetInt2d($recordData, 0);
2425
 
2426
			// offset: 2; size: 2;
2427
 
2428
			// offset: 4; size: 2; not used
2429
 
2430
			// offset: 6; size: var
2431
			$nameString = self::_readUnicodeStringShort(substr($recordData, 6));
2432
 
2433
			// offset: var; size: var; formula data
2434
			$offset = 6 + $nameString['size'];
2435
			$formula = $this->_getFormulaFromStructure(substr($recordData, $offset));
2436
 
2437
			$this->_externalNames[] = array(
2438
				'name' => $nameString['value'],
2439
				'formula' => $formula,
2440
			);
2441
		}
2442
	}
2443
 
2444
 
2445
	/**
2446
	 * Read EXTERNSHEET record
2447
	 */
2448
	private function _readExternSheet()
2449
	{
2450
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
2451
		$recordData = substr($this->_data, $this->_pos + 4, $length);
2452
 
2453
		// move stream pointer to next record
2454
		$this->_pos += 4 + $length;
2455
 
2456
		// external sheet references provided for named cells
2457
		if ($this->_version == self::XLS_BIFF8) {
2458
			// offset: 0; size: 2; number of following ref structures
2459
			$nm = self::_GetInt2d($recordData, 0);
2460
			for ($i = 0; $i < $nm; ++$i) {
2461
				$this->_ref[] = array(
2462
					// offset: 2 + 6 * $i; index to EXTERNALBOOK record
2463
					'externalBookIndex' => self::_GetInt2d($recordData, 2 + 6 * $i),
2464
					// offset: 4 + 6 * $i; index to first sheet in EXTERNALBOOK record
2465
					'firstSheetIndex' => self::_GetInt2d($recordData, 4 + 6 * $i),
2466
					// offset: 6 + 6 * $i; index to last sheet in EXTERNALBOOK record
2467
					'lastSheetIndex' => self::_GetInt2d($recordData, 6 + 6 * $i),
2468
				);
2469
			}
2470
		}
2471
	}
2472
 
2473
 
2474
	/**
2475
	 * DEFINEDNAME
2476
	 *
2477
	 * This record is part of a Link Table. It contains the name
2478
	 * and the token array of an internal defined name. Token
2479
	 * arrays of defined names contain tokens with aberrant
2480
	 * token classes.
2481
	 *
2482
	 * --	"OpenOffice.org's Documentation of the Microsoft
2483
	 * 		Excel File Format"
2484
	 */
2485
	private function _readDefinedName()
2486
	{
2487
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
2488
		$recordData = substr($this->_data, $this->_pos + 4, $length);
2489
 
2490
		// move stream pointer to next record
2491
		$this->_pos += 4 + $length;
2492
 
2493
		if ($this->_version == self::XLS_BIFF8) {
2494
			// retrieves named cells
2495
 
2496
			// offset: 0; size: 2; option flags
2497
			$opts = self::_GetInt2d($recordData, 0);
2498
 
2499
				// bit: 5; mask: 0x0020; 0 = user-defined name, 1 = built-in-name
2500
				$isBuiltInName = (0x0020 & $opts) >> 5;
2501
 
2502
			// offset: 2; size: 1; keyboard shortcut
2503
 
2504
			// offset: 3; size: 1; length of the name (character count)
2505
			$nlen = ord($recordData{3});
2506
 
2507
			// offset: 4; size: 2; size of the formula data (it can happen that this is zero)
2508
			// note: there can also be additional data, this is not included in $flen
2509
			$flen = self::_GetInt2d($recordData, 4);
2510
 
2511
			// offset: 8; size: 2; 0=Global name, otherwise index to sheet (1-based)
2512
			$scope = self::_GetInt2d($recordData, 8);
2513
 
2514
			// offset: 14; size: var; Name (Unicode string without length field)
2515
			$string = self::_readUnicodeString(substr($recordData, 14), $nlen);
2516
 
2517
			// offset: var; size: $flen; formula data
2518
			$offset = 14 + $string['size'];
2519
			$formulaStructure = pack('v', $flen) . substr($recordData, $offset);
2520
 
2521
			try {
2522
				$formula = $this->_getFormulaFromStructure($formulaStructure);
2523
			} catch (PHPExcel_Exception $e) {
2524
				$formula = '';
2525
			}
2526
 
2527
			$this->_definedname[] = array(
2528
				'isBuiltInName' => $isBuiltInName,
2529
				'name' => $string['value'],
2530
				'formula' => $formula,
2531
				'scope' => $scope,
2532
			);
2533
		}
2534
	}
2535
 
2536
 
2537
	/**
2538
	 * Read MSODRAWINGGROUP record
2539
	 */
2540
	private function _readMsoDrawingGroup()
2541
	{
2542
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
2543
 
2544
		// get spliced record data
2545
		$splicedRecordData = $this->_getSplicedRecordData();
2546
		$recordData = $splicedRecordData['recordData'];
2547
 
2548
		$this->_drawingGroupData .= $recordData;
2549
	}
2550
 
2551
 
2552
	/**
2553
	 * SST - Shared String Table
2554
	 *
2555
	 * This record contains a list of all strings used anywhere
2556
	 * in the workbook. Each string occurs only once. The
2557
	 * workbook uses indexes into the list to reference the
2558
	 * strings.
2559
	 *
2560
	 * --	"OpenOffice.org's Documentation of the Microsoft
2561
	 * 		Excel File Format"
2562
	 **/
2563
	private function _readSst()
2564
	{
2565
		// offset within (spliced) record data
2566
		$pos = 0;
2567
 
2568
		// get spliced record data
2569
		$splicedRecordData = $this->_getSplicedRecordData();
2570
 
2571
		$recordData = $splicedRecordData['recordData'];
2572
		$spliceOffsets = $splicedRecordData['spliceOffsets'];
2573
 
2574
		// offset: 0; size: 4; total number of strings in the workbook
2575
		$pos += 4;
2576
 
2577
		// offset: 4; size: 4; number of following strings ($nm)
2578
		$nm = self::_GetInt4d($recordData, 4);
2579
		$pos += 4;
2580
 
2581
		// loop through the Unicode strings (16-bit length)
2582
		for ($i = 0; $i < $nm; ++$i) {
2583
 
2584
			// number of characters in the Unicode string
2585
			$numChars = self::_GetInt2d($recordData, $pos);
2586
			$pos += 2;
2587
 
2588
			// option flags
2589
			$optionFlags = ord($recordData{$pos});
2590
			++$pos;
2591
 
2592
			// bit: 0; mask: 0x01; 0 = compressed; 1 = uncompressed
2593
			$isCompressed = (($optionFlags & 0x01) == 0) ;
2594
 
2595
			// bit: 2; mask: 0x02; 0 = ordinary; 1 = Asian phonetic
2596
			$hasAsian = (($optionFlags & 0x04) != 0);
2597
 
2598
			// bit: 3; mask: 0x03; 0 = ordinary; 1 = Rich-Text
2599
			$hasRichText = (($optionFlags & 0x08) != 0);
2600
 
2601
			if ($hasRichText) {
2602
				// number of Rich-Text formatting runs
2603
				$formattingRuns = self::_GetInt2d($recordData, $pos);
2604
				$pos += 2;
2605
			}
2606
 
2607
			if ($hasAsian) {
2608
				// size of Asian phonetic setting
2609
				$extendedRunLength = self::_GetInt4d($recordData, $pos);
2610
				$pos += 4;
2611
			}
2612
 
2613
			// expected byte length of character array if not split
2614
			$len = ($isCompressed) ? $numChars : $numChars * 2;
2615
 
2616
			// look up limit position
2617
			foreach ($spliceOffsets as $spliceOffset) {
2618
				// it can happen that the string is empty, therefore we need
2619
				// <= and not just <
2620
				if ($pos <= $spliceOffset) {
2621
					$limitpos = $spliceOffset;
2622
					break;
2623
				}
2624
			}
2625
 
2626
			if ($pos + $len <= $limitpos) {
2627
				// character array is not split between records
2628
 
2629
				$retstr = substr($recordData, $pos, $len);
2630
				$pos += $len;
2631
 
2632
			} else {
2633
				// character array is split between records
2634
 
2635
				// first part of character array
2636
				$retstr = substr($recordData, $pos, $limitpos - $pos);
2637
 
2638
				$bytesRead = $limitpos - $pos;
2639
 
2640
				// remaining characters in Unicode string
2641
				$charsLeft = $numChars - (($isCompressed) ? $bytesRead : ($bytesRead / 2));
2642
 
2643
				$pos = $limitpos;
2644
 
2645
				// keep reading the characters
2646
				while ($charsLeft > 0) {
2647
 
2648
					// look up next limit position, in case the string span more than one continue record
2649
					foreach ($spliceOffsets as $spliceOffset) {
2650
						if ($pos < $spliceOffset) {
2651
							$limitpos = $spliceOffset;
2652
							break;
2653
						}
2654
					}
2655
 
2656
					// repeated option flags
2657
					// OpenOffice.org documentation 5.21
2658
					$option = ord($recordData{$pos});
2659
					++$pos;
2660
 
2661
					if ($isCompressed && ($option == 0)) {
2662
						// 1st fragment compressed
2663
						// this fragment compressed
2664
						$len = min($charsLeft, $limitpos - $pos);
2665
						$retstr .= substr($recordData, $pos, $len);
2666
						$charsLeft -= $len;
2667
						$isCompressed = true;
2668
 
2669
					} elseif (!$isCompressed && ($option != 0)) {
2670
						// 1st fragment uncompressed
2671
						// this fragment uncompressed
2672
						$len = min($charsLeft * 2, $limitpos - $pos);
2673
						$retstr .= substr($recordData, $pos, $len);
2674
						$charsLeft -= $len / 2;
2675
						$isCompressed = false;
2676
 
2677
					} elseif (!$isCompressed && ($option == 0)) {
2678
						// 1st fragment uncompressed
2679
						// this fragment compressed
2680
						$len = min($charsLeft, $limitpos - $pos);
2681
						for ($j = 0; $j < $len; ++$j) {
2682
							$retstr .= $recordData{$pos + $j} . chr(0);
2683
						}
2684
						$charsLeft -= $len;
2685
						$isCompressed = false;
2686
 
2687
					} else {
2688
						// 1st fragment compressed
2689
						// this fragment uncompressed
2690
						$newstr = '';
2691
						for ($j = 0; $j < strlen($retstr); ++$j) {
2692
							$newstr .= $retstr[$j] . chr(0);
2693
						}
2694
						$retstr = $newstr;
2695
						$len = min($charsLeft * 2, $limitpos - $pos);
2696
						$retstr .= substr($recordData, $pos, $len);
2697
						$charsLeft -= $len / 2;
2698
						$isCompressed = false;
2699
					}
2700
 
2701
					$pos += $len;
2702
				}
2703
			}
2704
 
2705
			// convert to UTF-8
2706
			$retstr = self::_encodeUTF16($retstr, $isCompressed);
2707
 
2708
			// read additional Rich-Text information, if any
2709
			$fmtRuns = array();
2710
			if ($hasRichText) {
2711
				// list of formatting runs
2712
				for ($j = 0; $j < $formattingRuns; ++$j) {
2713
					// first formatted character; zero-based
2714
					$charPos = self::_GetInt2d($recordData, $pos + $j * 4);
2715
 
2716
					// index to font record
2717
					$fontIndex = self::_GetInt2d($recordData, $pos + 2 + $j * 4);
2718
 
2719
					$fmtRuns[] = array(
2720
						'charPos' => $charPos,
2721
						'fontIndex' => $fontIndex,
2722
					);
2723
				}
2724
				$pos += 4 * $formattingRuns;
2725
			}
2726
 
2727
			// read additional Asian phonetics information, if any
2728
			if ($hasAsian) {
2729
				// For Asian phonetic settings, we skip the extended string data
2730
				$pos += $extendedRunLength;
2731
			}
2732
 
2733
			// store the shared sting
2734
			$this->_sst[] = array(
2735
				'value' => $retstr,
2736
				'fmtRuns' => $fmtRuns,
2737
			);
2738
		}
2739
 
2740
		// _getSplicedRecordData() takes care of moving current position in data stream
2741
	}
2742
 
2743
 
2744
	/**
2745
	 * Read PRINTGRIDLINES record
2746
	 */
2747
	private function _readPrintGridlines()
2748
	{
2749
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
2750
		$recordData = substr($this->_data, $this->_pos + 4, $length);
2751
 
2752
		// move stream pointer to next record
2753
		$this->_pos += 4 + $length;
2754
 
2755
		if ($this->_version == self::XLS_BIFF8 && !$this->_readDataOnly) {
2756
			// offset: 0; size: 2; 0 = do not print sheet grid lines; 1 = print sheet gridlines
2757
			$printGridlines = (bool) self::_GetInt2d($recordData, 0);
2758
			$this->_phpSheet->setPrintGridlines($printGridlines);
2759
		}
2760
	}
2761
 
2762
 
2763
	/**
2764
	 * Read DEFAULTROWHEIGHT record
2765
	 */
2766
	private function _readDefaultRowHeight()
2767
	{
2768
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
2769
		$recordData = substr($this->_data, $this->_pos + 4, $length);
2770
 
2771
		// move stream pointer to next record
2772
		$this->_pos += 4 + $length;
2773
 
2774
		// offset: 0; size: 2; option flags
2775
		// offset: 2; size: 2; default height for unused rows, (twips 1/20 point)
2776
		$height = self::_GetInt2d($recordData, 2);
2777
		$this->_phpSheet->getDefaultRowDimension()->setRowHeight($height / 20);
2778
	}
2779
 
2780
 
2781
	/**
2782
	 * Read SHEETPR record
2783
	 */
2784
	private function _readSheetPr()
2785
	{
2786
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
2787
		$recordData = substr($this->_data, $this->_pos + 4, $length);
2788
 
2789
		// move stream pointer to next record
2790
		$this->_pos += 4 + $length;
2791
 
2792
		// offset: 0; size: 2
2793
 
2794
		// bit: 6; mask: 0x0040; 0 = outline buttons above outline group
2795
		$isSummaryBelow = (0x0040 & self::_GetInt2d($recordData, 0)) >> 6;
2796
		$this->_phpSheet->setShowSummaryBelow($isSummaryBelow);
2797
 
2798
		// bit: 7; mask: 0x0080; 0 = outline buttons left of outline group
2799
		$isSummaryRight = (0x0080 & self::_GetInt2d($recordData, 0)) >> 7;
2800
		$this->_phpSheet->setShowSummaryRight($isSummaryRight);
2801
 
2802
		// bit: 8; mask: 0x100; 0 = scale printout in percent, 1 = fit printout to number of pages
2803
		// this corresponds to radio button setting in page setup dialog in Excel
2804
		$this->_isFitToPages = (bool) ((0x0100 & self::_GetInt2d($recordData, 0)) >> 8);
2805
	}
2806
 
2807
 
2808
	/**
2809
	 * Read HORIZONTALPAGEBREAKS record
2810
	 */
2811
	private function _readHorizontalPageBreaks()
2812
	{
2813
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
2814
		$recordData = substr($this->_data, $this->_pos + 4, $length);
2815
 
2816
		// move stream pointer to next record
2817
		$this->_pos += 4 + $length;
2818
 
2819
		if ($this->_version == self::XLS_BIFF8 && !$this->_readDataOnly) {
2820
 
2821
			// offset: 0; size: 2; number of the following row index structures
2822
			$nm = self::_GetInt2d($recordData, 0);
2823
 
2824
			// offset: 2; size: 6 * $nm; list of $nm row index structures
2825
			for ($i = 0; $i < $nm; ++$i) {
2826
				$r = self::_GetInt2d($recordData, 2 + 6 * $i);
2827
				$cf = self::_GetInt2d($recordData, 2 + 6 * $i + 2);
2828
				$cl = self::_GetInt2d($recordData, 2 + 6 * $i + 4);
2829
 
2830
				// not sure why two column indexes are necessary?
2831
				$this->_phpSheet->setBreakByColumnAndRow($cf, $r, PHPExcel_Worksheet::BREAK_ROW);
2832
			}
2833
		}
2834
	}
2835
 
2836
 
2837
	/**
2838
	 * Read VERTICALPAGEBREAKS record
2839
	 */
2840
	private function _readVerticalPageBreaks()
2841
	{
2842
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
2843
		$recordData = substr($this->_data, $this->_pos + 4, $length);
2844
 
2845
		// move stream pointer to next record
2846
		$this->_pos += 4 + $length;
2847
 
2848
		if ($this->_version == self::XLS_BIFF8 && !$this->_readDataOnly) {
2849
			// offset: 0; size: 2; number of the following column index structures
2850
			$nm = self::_GetInt2d($recordData, 0);
2851
 
2852
			// offset: 2; size: 6 * $nm; list of $nm row index structures
2853
			for ($i = 0; $i < $nm; ++$i) {
2854
				$c = self::_GetInt2d($recordData, 2 + 6 * $i);
2855
				$rf = self::_GetInt2d($recordData, 2 + 6 * $i + 2);
2856
				$rl = self::_GetInt2d($recordData, 2 + 6 * $i + 4);
2857
 
2858
				// not sure why two row indexes are necessary?
2859
				$this->_phpSheet->setBreakByColumnAndRow($c, $rf, PHPExcel_Worksheet::BREAK_COLUMN);
2860
			}
2861
		}
2862
	}
2863
 
2864
 
2865
	/**
2866
	 * Read HEADER record
2867
	 */
2868
	private function _readHeader()
2869
	{
2870
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
2871
		$recordData = substr($this->_data, $this->_pos + 4, $length);
2872
 
2873
		// move stream pointer to next record
2874
		$this->_pos += 4 + $length;
2875
 
2876
		if (!$this->_readDataOnly) {
2877
			// offset: 0; size: var
2878
			// realized that $recordData can be empty even when record exists
2879
			if ($recordData) {
2880
				if ($this->_version == self::XLS_BIFF8) {
2881
					$string = self::_readUnicodeStringLong($recordData);
2882
				} else {
2883
					$string = $this->_readByteStringShort($recordData);
2884
				}
2885
 
2886
				$this->_phpSheet->getHeaderFooter()->setOddHeader($string['value']);
2887
				$this->_phpSheet->getHeaderFooter()->setEvenHeader($string['value']);
2888
			}
2889
		}
2890
	}
2891
 
2892
 
2893
	/**
2894
	 * Read FOOTER record
2895
	 */
2896
	private function _readFooter()
2897
	{
2898
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
2899
		$recordData = substr($this->_data, $this->_pos + 4, $length);
2900
 
2901
		// move stream pointer to next record
2902
		$this->_pos += 4 + $length;
2903
 
2904
		if (!$this->_readDataOnly) {
2905
			// offset: 0; size: var
2906
			// realized that $recordData can be empty even when record exists
2907
			if ($recordData) {
2908
				if ($this->_version == self::XLS_BIFF8) {
2909
					$string = self::_readUnicodeStringLong($recordData);
2910
				} else {
2911
					$string = $this->_readByteStringShort($recordData);
2912
				}
2913
				$this->_phpSheet->getHeaderFooter()->setOddFooter($string['value']);
2914
				$this->_phpSheet->getHeaderFooter()->setEvenFooter($string['value']);
2915
			}
2916
		}
2917
	}
2918
 
2919
 
2920
	/**
2921
	 * Read HCENTER record
2922
	 */
2923
	private function _readHcenter()
2924
	{
2925
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
2926
		$recordData = substr($this->_data, $this->_pos + 4, $length);
2927
 
2928
		// move stream pointer to next record
2929
		$this->_pos += 4 + $length;
2930
 
2931
		if (!$this->_readDataOnly) {
2932
			// offset: 0; size: 2; 0 = print sheet left aligned, 1 = print sheet centered horizontally
2933
			$isHorizontalCentered = (bool) self::_GetInt2d($recordData, 0);
2934
 
2935
			$this->_phpSheet->getPageSetup()->setHorizontalCentered($isHorizontalCentered);
2936
		}
2937
	}
2938
 
2939
 
2940
	/**
2941
	 * Read VCENTER record
2942
	 */
2943
	private function _readVcenter()
2944
	{
2945
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
2946
		$recordData = substr($this->_data, $this->_pos + 4, $length);
2947
 
2948
		// move stream pointer to next record
2949
		$this->_pos += 4 + $length;
2950
 
2951
		if (!$this->_readDataOnly) {
2952
			// offset: 0; size: 2; 0 = print sheet aligned at top page border, 1 = print sheet vertically centered
2953
			$isVerticalCentered = (bool) self::_GetInt2d($recordData, 0);
2954
 
2955
			$this->_phpSheet->getPageSetup()->setVerticalCentered($isVerticalCentered);
2956
		}
2957
	}
2958
 
2959
 
2960
	/**
2961
	 * Read LEFTMARGIN record
2962
	 */
2963
	private function _readLeftMargin()
2964
	{
2965
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
2966
		$recordData = substr($this->_data, $this->_pos + 4, $length);
2967
 
2968
		// move stream pointer to next record
2969
		$this->_pos += 4 + $length;
2970
 
2971
		if (!$this->_readDataOnly) {
2972
			// offset: 0; size: 8
2973
			$this->_phpSheet->getPageMargins()->setLeft(self::_extractNumber($recordData));
2974
		}
2975
	}
2976
 
2977
 
2978
	/**
2979
	 * Read RIGHTMARGIN record
2980
	 */
2981
	private function _readRightMargin()
2982
	{
2983
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
2984
		$recordData = substr($this->_data, $this->_pos + 4, $length);
2985
 
2986
		// move stream pointer to next record
2987
		$this->_pos += 4 + $length;
2988
 
2989
		if (!$this->_readDataOnly) {
2990
			// offset: 0; size: 8
2991
			$this->_phpSheet->getPageMargins()->setRight(self::_extractNumber($recordData));
2992
		}
2993
	}
2994
 
2995
 
2996
	/**
2997
	 * Read TOPMARGIN record
2998
	 */
2999
	private function _readTopMargin()
3000
	{
3001
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
3002
		$recordData = substr($this->_data, $this->_pos + 4, $length);
3003
 
3004
		// move stream pointer to next record
3005
		$this->_pos += 4 + $length;
3006
 
3007
		if (!$this->_readDataOnly) {
3008
			// offset: 0; size: 8
3009
			$this->_phpSheet->getPageMargins()->setTop(self::_extractNumber($recordData));
3010
		}
3011
	}
3012
 
3013
 
3014
	/**
3015
	 * Read BOTTOMMARGIN record
3016
	 */
3017
	private function _readBottomMargin()
3018
	{
3019
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
3020
		$recordData = substr($this->_data, $this->_pos + 4, $length);
3021
 
3022
		// move stream pointer to next record
3023
		$this->_pos += 4 + $length;
3024
 
3025
		if (!$this->_readDataOnly) {
3026
			// offset: 0; size: 8
3027
			$this->_phpSheet->getPageMargins()->setBottom(self::_extractNumber($recordData));
3028
		}
3029
	}
3030
 
3031
 
3032
	/**
3033
	 * Read PAGESETUP record
3034
	 */
3035
	private function _readPageSetup()
3036
	{
3037
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
3038
		$recordData = substr($this->_data, $this->_pos + 4, $length);
3039
 
3040
		// move stream pointer to next record
3041
		$this->_pos += 4 + $length;
3042
 
3043
		if (!$this->_readDataOnly) {
3044
			// offset: 0; size: 2; paper size
3045
			$paperSize = self::_GetInt2d($recordData, 0);
3046
 
3047
			// offset: 2; size: 2; scaling factor
3048
			$scale = self::_GetInt2d($recordData, 2);
3049
 
3050
			// offset: 6; size: 2; fit worksheet width to this number of pages, 0 = use as many as needed
3051
			$fitToWidth = self::_GetInt2d($recordData, 6);
3052
 
3053
			// offset: 8; size: 2; fit worksheet height to this number of pages, 0 = use as many as needed
3054
			$fitToHeight = self::_GetInt2d($recordData, 8);
3055
 
3056
			// offset: 10; size: 2; option flags
3057
 
3058
				// bit: 1; mask: 0x0002; 0=landscape, 1=portrait
3059
				$isPortrait = (0x0002 & self::_GetInt2d($recordData, 10)) >> 1;
3060
 
3061
				// bit: 2; mask: 0x0004; 1= paper size, scaling factor, paper orient. not init
3062
				// when this bit is set, do not use flags for those properties
3063
				$isNotInit = (0x0004 & self::_GetInt2d($recordData, 10)) >> 2;
3064
 
3065
			if (!$isNotInit) {
3066
				$this->_phpSheet->getPageSetup()->setPaperSize($paperSize);
3067
				switch ($isPortrait) {
3068
				case 0: $this->_phpSheet->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE); break;
3069
				case 1: $this->_phpSheet->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT); break;
3070
				}
3071
 
3072
				$this->_phpSheet->getPageSetup()->setScale($scale, false);
3073
				$this->_phpSheet->getPageSetup()->setFitToPage((bool) $this->_isFitToPages);
3074
				$this->_phpSheet->getPageSetup()->setFitToWidth($fitToWidth, false);
3075
				$this->_phpSheet->getPageSetup()->setFitToHeight($fitToHeight, false);
3076
			}
3077
 
3078
			// offset: 16; size: 8; header margin (IEEE 754 floating-point value)
3079
			$marginHeader = self::_extractNumber(substr($recordData, 16, 8));
3080
			$this->_phpSheet->getPageMargins()->setHeader($marginHeader);
3081
 
3082
			// offset: 24; size: 8; footer margin (IEEE 754 floating-point value)
3083
			$marginFooter = self::_extractNumber(substr($recordData, 24, 8));
3084
			$this->_phpSheet->getPageMargins()->setFooter($marginFooter);
3085
		}
3086
	}
3087
 
3088
 
3089
	/**
3090
	 * PROTECT - Sheet protection (BIFF2 through BIFF8)
3091
	 *   if this record is omitted, then it also means no sheet protection
3092
	 */
3093
	private function _readProtect()
3094
	{
3095
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
3096
		$recordData = substr($this->_data, $this->_pos + 4, $length);
3097
 
3098
		// move stream pointer to next record
3099
		$this->_pos += 4 + $length;
3100
 
3101
		if ($this->_readDataOnly) {
3102
			return;
3103
		}
3104
 
3105
		// offset: 0; size: 2;
3106
 
3107
		// bit 0, mask 0x01; 1 = sheet is protected
3108
		$bool = (0x01 & self::_GetInt2d($recordData, 0)) >> 0;
3109
		$this->_phpSheet->getProtection()->setSheet((bool)$bool);
3110
	}
3111
 
3112
 
3113
	/**
3114
	 * SCENPROTECT
3115
	 */
3116
	private function _readScenProtect()
3117
	{
3118
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
3119
		$recordData = substr($this->_data, $this->_pos + 4, $length);
3120
 
3121
		// move stream pointer to next record
3122
		$this->_pos += 4 + $length;
3123
 
3124
		if ($this->_readDataOnly) {
3125
			return;
3126
		}
3127
 
3128
		// offset: 0; size: 2;
3129
 
3130
		// bit: 0, mask 0x01; 1 = scenarios are protected
3131
		$bool = (0x01 & self::_GetInt2d($recordData, 0)) >> 0;
3132
 
3133
		$this->_phpSheet->getProtection()->setScenarios((bool)$bool);
3134
	}
3135
 
3136
 
3137
	/**
3138
	 * OBJECTPROTECT
3139
	 */
3140
	private function _readObjectProtect()
3141
	{
3142
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
3143
		$recordData = substr($this->_data, $this->_pos + 4, $length);
3144
 
3145
		// move stream pointer to next record
3146
		$this->_pos += 4 + $length;
3147
 
3148
		if ($this->_readDataOnly) {
3149
			return;
3150
		}
3151
 
3152
		// offset: 0; size: 2;
3153
 
3154
		// bit: 0, mask 0x01; 1 = objects are protected
3155
		$bool = (0x01 & self::_GetInt2d($recordData, 0)) >> 0;
3156
 
3157
		$this->_phpSheet->getProtection()->setObjects((bool)$bool);
3158
	}
3159
 
3160
 
3161
	/**
3162
	 * PASSWORD - Sheet protection (hashed) password (BIFF2 through BIFF8)
3163
	 */
3164
	private function _readPassword()
3165
	{
3166
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
3167
		$recordData = substr($this->_data, $this->_pos + 4, $length);
3168
 
3169
		// move stream pointer to next record
3170
		$this->_pos += 4 + $length;
3171
 
3172
		if (!$this->_readDataOnly) {
3173
			// offset: 0; size: 2; 16-bit hash value of password
3174
			$password = strtoupper(dechex(self::_GetInt2d($recordData, 0))); // the hashed password
3175
			$this->_phpSheet->getProtection()->setPassword($password, true);
3176
		}
3177
	}
3178
 
3179
 
3180
	/**
3181
	 * Read DEFCOLWIDTH record
3182
	 */
3183
	private function _readDefColWidth()
3184
	{
3185
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
3186
		$recordData = substr($this->_data, $this->_pos + 4, $length);
3187
 
3188
		// move stream pointer to next record
3189
		$this->_pos += 4 + $length;
3190
 
3191
		// offset: 0; size: 2; default column width
3192
		$width = self::_GetInt2d($recordData, 0);
3193
		if ($width != 8) {
3194
			$this->_phpSheet->getDefaultColumnDimension()->setWidth($width);
3195
		}
3196
	}
3197
 
3198
 
3199
	/**
3200
	 * Read COLINFO record
3201
	 */
3202
	private function _readColInfo()
3203
	{
3204
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
3205
		$recordData = substr($this->_data, $this->_pos + 4, $length);
3206
 
3207
		// move stream pointer to next record
3208
		$this->_pos += 4 + $length;
3209
 
3210
		if (!$this->_readDataOnly) {
3211
			// offset: 0; size: 2; index to first column in range
3212
			$fc = self::_GetInt2d($recordData, 0); // first column index
3213
 
3214
			// offset: 2; size: 2; index to last column in range
3215
			$lc = self::_GetInt2d($recordData, 2); // first column index
3216
 
3217
			// offset: 4; size: 2; width of the column in 1/256 of the width of the zero character
3218
			$width = self::_GetInt2d($recordData, 4);
3219
 
3220
			// offset: 6; size: 2; index to XF record for default column formatting
3221
			$xfIndex = self::_GetInt2d($recordData, 6);
3222
 
3223
			// offset: 8; size: 2; option flags
3224
 
3225
				// bit: 0; mask: 0x0001; 1= columns are hidden
3226
				$isHidden = (0x0001 & self::_GetInt2d($recordData, 8)) >> 0;
3227
 
3228
				// bit: 10-8; mask: 0x0700; outline level of the columns (0 = no outline)
3229
				$level = (0x0700 & self::_GetInt2d($recordData, 8)) >> 8;
3230
 
3231
				// bit: 12; mask: 0x1000; 1 = collapsed
3232
				$isCollapsed = (0x1000 & self::_GetInt2d($recordData, 8)) >> 12;
3233
 
3234
			// offset: 10; size: 2; not used
3235
 
3236
			for ($i = $fc; $i <= $lc; ++$i) {
3237
				if ($lc == 255 || $lc == 256) {
3238
					$this->_phpSheet->getDefaultColumnDimension()->setWidth($width / 256);
3239
					break;
3240
				}
3241
				$this->_phpSheet->getColumnDimensionByColumn($i)->setWidth($width / 256);
3242
				$this->_phpSheet->getColumnDimensionByColumn($i)->setVisible(!$isHidden);
3243
				$this->_phpSheet->getColumnDimensionByColumn($i)->setOutlineLevel($level);
3244
				$this->_phpSheet->getColumnDimensionByColumn($i)->setCollapsed($isCollapsed);
3245
				$this->_phpSheet->getColumnDimensionByColumn($i)->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
3246
			}
3247
		}
3248
	}
3249
 
3250
 
3251
	/**
3252
	 * ROW
3253
	 *
3254
	 * This record contains the properties of a single row in a
3255
	 * sheet. Rows and cells in a sheet are divided into blocks
3256
	 * of 32 rows.
3257
	 *
3258
	 * --	"OpenOffice.org's Documentation of the Microsoft
3259
	 * 		Excel File Format"
3260
	 */
3261
	private function _readRow()
3262
	{
3263
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
3264
		$recordData = substr($this->_data, $this->_pos + 4, $length);
3265
 
3266
		// move stream pointer to next record
3267
		$this->_pos += 4 + $length;
3268
 
3269
		if (!$this->_readDataOnly) {
3270
			// offset: 0; size: 2; index of this row
3271
			$r = self::_GetInt2d($recordData, 0);
3272
 
3273
			// offset: 2; size: 2; index to column of the first cell which is described by a cell record
3274
 
3275
			// offset: 4; size: 2; index to column of the last cell which is described by a cell record, increased by 1
3276
 
3277
			// offset: 6; size: 2;
3278
 
3279
			// bit: 14-0; mask: 0x7FFF; height of the row, in twips = 1/20 of a point
3280
			$height = (0x7FFF & self::_GetInt2d($recordData, 6)) >> 0;
3281
 
3282
			// bit: 15: mask: 0x8000; 0 = row has custom height; 1= row has default height
3283
			$useDefaultHeight = (0x8000 & self::_GetInt2d($recordData, 6)) >> 15;
3284
 
3285
			if (!$useDefaultHeight) {
3286
				$this->_phpSheet->getRowDimension($r + 1)->setRowHeight($height / 20);
3287
			}
3288
 
3289
			// offset: 8; size: 2; not used
3290
 
3291
			// offset: 10; size: 2; not used in BIFF5-BIFF8
3292
 
3293
			// offset: 12; size: 4; option flags and default row formatting
3294
 
3295
			// bit: 2-0: mask: 0x00000007; outline level of the row
3296
			$level = (0x00000007 & self::_GetInt4d($recordData, 12)) >> 0;
3297
			$this->_phpSheet->getRowDimension($r + 1)->setOutlineLevel($level);
3298
 
3299
			// bit: 4; mask: 0x00000010; 1 = outline group start or ends here... and is collapsed
3300
			$isCollapsed = (0x00000010 & self::_GetInt4d($recordData, 12)) >> 4;
3301
			$this->_phpSheet->getRowDimension($r + 1)->setCollapsed($isCollapsed);
3302
 
3303
			// bit: 5; mask: 0x00000020; 1 = row is hidden
3304
			$isHidden = (0x00000020 & self::_GetInt4d($recordData, 12)) >> 5;
3305
			$this->_phpSheet->getRowDimension($r + 1)->setVisible(!$isHidden);
3306
 
3307
			// bit: 7; mask: 0x00000080; 1 = row has explicit format
3308
			$hasExplicitFormat = (0x00000080 & self::_GetInt4d($recordData, 12)) >> 7;
3309
 
3310
			// bit: 27-16; mask: 0x0FFF0000; only applies when hasExplicitFormat = 1; index to XF record
3311
			$xfIndex = (0x0FFF0000 & self::_GetInt4d($recordData, 12)) >> 16;
3312
 
3313
			if ($hasExplicitFormat) {
3314
				$this->_phpSheet->getRowDimension($r + 1)->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
3315
			}
3316
		}
3317
	}
3318
 
3319
 
3320
	/**
3321
	 * Read RK record
3322
	 * This record represents a cell that contains an RK value
3323
	 * (encoded integer or floating-point value). If a
3324
	 * floating-point value cannot be encoded to an RK value,
3325
	 * a NUMBER record will be written. This record replaces the
3326
	 * record INTEGER written in BIFF2.
3327
	 *
3328
	 * --	"OpenOffice.org's Documentation of the Microsoft
3329
	 * 		Excel File Format"
3330
	 */
3331
	private function _readRk()
3332
	{
3333
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
3334
		$recordData = substr($this->_data, $this->_pos + 4, $length);
3335
 
3336
		// move stream pointer to next record
3337
		$this->_pos += 4 + $length;
3338
 
3339
		// offset: 0; size: 2; index to row
3340
		$row = self::_GetInt2d($recordData, 0);
3341
 
3342
		// offset: 2; size: 2; index to column
3343
		$column = self::_GetInt2d($recordData, 2);
3344
		$columnString = PHPExcel_Cell::stringFromColumnIndex($column);
3345
 
3346
		// Read cell?
3347
		if (($this->getReadFilter() !== NULL) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
3348
			// offset: 4; size: 2; index to XF record
3349
			$xfIndex = self::_GetInt2d($recordData, 4);
3350
 
3351
			// offset: 6; size: 4; RK value
3352
			$rknum = self::_GetInt4d($recordData, 6);
3353
			$numValue = self::_GetIEEE754($rknum);
3354
 
3355
			$cell = $this->_phpSheet->getCell($columnString . ($row + 1));
3356
			if (!$this->_readDataOnly) {
3357
				// add style information
3358
				$cell->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
3359
			}
3360
 
3361
			// add cell
3362
			$cell->setValueExplicit($numValue, PHPExcel_Cell_DataType::TYPE_NUMERIC);
3363
		}
3364
	}
3365
 
3366
 
3367
	/**
3368
	 * Read LABELSST record
3369
	 * This record represents a cell that contains a string. It
3370
	 * replaces the LABEL record and RSTRING record used in
3371
	 * BIFF2-BIFF5.
3372
	 *
3373
	 * --	"OpenOffice.org's Documentation of the Microsoft
3374
	 * 		Excel File Format"
3375
	 */
3376
	private function _readLabelSst()
3377
	{
3378
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
3379
		$recordData = substr($this->_data, $this->_pos + 4, $length);
3380
 
3381
		// move stream pointer to next record
3382
		$this->_pos += 4 + $length;
3383
 
3384
		// offset: 0; size: 2; index to row
3385
		$row = self::_GetInt2d($recordData, 0);
3386
 
3387
		// offset: 2; size: 2; index to column
3388
		$column = self::_GetInt2d($recordData, 2);
3389
		$columnString = PHPExcel_Cell::stringFromColumnIndex($column);
3390
 
3391
		// Read cell?
3392
		if (($this->getReadFilter() !== NULL) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
3393
			// offset: 4; size: 2; index to XF record
3394
			$xfIndex = self::_GetInt2d($recordData, 4);
3395
 
3396
			// offset: 6; size: 4; index to SST record
3397
			$index = self::_GetInt4d($recordData, 6);
3398
 
3399
			// add cell
3400
			if (($fmtRuns = $this->_sst[$index]['fmtRuns']) && !$this->_readDataOnly) {
3401
				// then we should treat as rich text
3402
				$richText = new PHPExcel_RichText();
3403
				$charPos = 0;
3404
				$sstCount = count($this->_sst[$index]['fmtRuns']);
3405
				for ($i = 0; $i <= $sstCount; ++$i) {
3406
					if (isset($fmtRuns[$i])) {
3407
						$text = PHPExcel_Shared_String::Substring($this->_sst[$index]['value'], $charPos, $fmtRuns[$i]['charPos'] - $charPos);
3408
						$charPos = $fmtRuns[$i]['charPos'];
3409
					} else {
3410
						$text = PHPExcel_Shared_String::Substring($this->_sst[$index]['value'], $charPos, PHPExcel_Shared_String::CountCharacters($this->_sst[$index]['value']));
3411
					}
3412
 
3413
					if (PHPExcel_Shared_String::CountCharacters($text) > 0) {
3414
						if ($i == 0) { // first text run, no style
3415
							$richText->createText($text);
3416
						} else {
3417
							$textRun = $richText->createTextRun($text);
3418
							if (isset($fmtRuns[$i - 1])) {
3419
								if ($fmtRuns[$i - 1]['fontIndex'] < 4) {
3420
									$fontIndex = $fmtRuns[$i - 1]['fontIndex'];
3421
								} else {
3422
									// this has to do with that index 4 is omitted in all BIFF versions for some strange reason
3423
									// check the OpenOffice documentation of the FONT record
3424
									$fontIndex = $fmtRuns[$i - 1]['fontIndex'] - 1;
3425
								}
3426
								$textRun->setFont(clone $this->_objFonts[$fontIndex]);
3427
							}
3428
						}
3429
					}
3430
				}
3431
				$cell = $this->_phpSheet->getCell($columnString . ($row + 1));
3432
				$cell->setValueExplicit($richText, PHPExcel_Cell_DataType::TYPE_STRING);
3433
			} else {
3434
				$cell = $this->_phpSheet->getCell($columnString . ($row + 1));
3435
				$cell->setValueExplicit($this->_sst[$index]['value'], PHPExcel_Cell_DataType::TYPE_STRING);
3436
			}
3437
 
3438
			if (!$this->_readDataOnly) {
3439
				// add style information
3440
				$cell->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
3441
			}
3442
		}
3443
	}
3444
 
3445
 
3446
	/**
3447
	 * Read MULRK record
3448
	 * This record represents a cell range containing RK value
3449
	 * cells. All cells are located in the same row.
3450
	 *
3451
	 * --	"OpenOffice.org's Documentation of the Microsoft
3452
	 * 		Excel File Format"
3453
	 */
3454
	private function _readMulRk()
3455
	{
3456
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
3457
		$recordData = substr($this->_data, $this->_pos + 4, $length);
3458
 
3459
		// move stream pointer to next record
3460
		$this->_pos += 4 + $length;
3461
 
3462
		// offset: 0; size: 2; index to row
3463
		$row = self::_GetInt2d($recordData, 0);
3464
 
3465
		// offset: 2; size: 2; index to first column
3466
		$colFirst = self::_GetInt2d($recordData, 2);
3467
 
3468
		// offset: var; size: 2; index to last column
3469
		$colLast = self::_GetInt2d($recordData, $length - 2);
3470
		$columns = $colLast - $colFirst + 1;
3471
 
3472
		// offset within record data
3473
		$offset = 4;
3474
 
3475
		for ($i = 0; $i < $columns; ++$i) {
3476
			$columnString = PHPExcel_Cell::stringFromColumnIndex($colFirst + $i);
3477
 
3478
			// Read cell?
3479
			if (($this->getReadFilter() !== NULL) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
3480
 
3481
				// offset: var; size: 2; index to XF record
3482
				$xfIndex = self::_GetInt2d($recordData, $offset);
3483
 
3484
				// offset: var; size: 4; RK value
3485
				$numValue = self::_GetIEEE754(self::_GetInt4d($recordData, $offset + 2));
3486
				$cell = $this->_phpSheet->getCell($columnString . ($row + 1));
3487
				if (!$this->_readDataOnly) {
3488
					// add style
3489
					$cell->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
3490
				}
3491
 
3492
				// add cell value
3493
				$cell->setValueExplicit($numValue, PHPExcel_Cell_DataType::TYPE_NUMERIC);
3494
			}
3495
 
3496
			$offset += 6;
3497
		}
3498
	}
3499
 
3500
 
3501
	/**
3502
	 * Read NUMBER record
3503
	 * This record represents a cell that contains a
3504
	 * floating-point value.
3505
	 *
3506
	 * --	"OpenOffice.org's Documentation of the Microsoft
3507
	 * 		Excel File Format"
3508
	 */
3509
	private function _readNumber()
3510
	{
3511
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
3512
		$recordData = substr($this->_data, $this->_pos + 4, $length);
3513
 
3514
		// move stream pointer to next record
3515
		$this->_pos += 4 + $length;
3516
 
3517
		// offset: 0; size: 2; index to row
3518
		$row = self::_GetInt2d($recordData, 0);
3519
 
3520
		// offset: 2; size 2; index to column
3521
		$column = self::_GetInt2d($recordData, 2);
3522
		$columnString = PHPExcel_Cell::stringFromColumnIndex($column);
3523
 
3524
		// Read cell?
3525
		if (($this->getReadFilter() !== NULL) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
3526
			// offset 4; size: 2; index to XF record
3527
			$xfIndex = self::_GetInt2d($recordData, 4);
3528
 
3529
			$numValue = self::_extractNumber(substr($recordData, 6, 8));
3530
 
3531
			$cell = $this->_phpSheet->getCell($columnString . ($row + 1));
3532
			if (!$this->_readDataOnly) {
3533
				// add cell style
3534
				$cell->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
3535
			}
3536
 
3537
			// add cell value
3538
			$cell->setValueExplicit($numValue, PHPExcel_Cell_DataType::TYPE_NUMERIC);
3539
		}
3540
	}
3541
 
3542
 
3543
	/**
3544
	 * Read FORMULA record + perhaps a following STRING record if formula result is a string
3545
	 * This record contains the token array and the result of a
3546
	 * formula cell.
3547
	 *
3548
	 * --	"OpenOffice.org's Documentation of the Microsoft
3549
	 * 		Excel File Format"
3550
	 */
3551
	private function _readFormula()
3552
	{
3553
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
3554
		$recordData = substr($this->_data, $this->_pos + 4, $length);
3555
 
3556
		// move stream pointer to next record
3557
		$this->_pos += 4 + $length;
3558
 
3559
		// offset: 0; size: 2; row index
3560
		$row = self::_GetInt2d($recordData, 0);
3561
 
3562
		// offset: 2; size: 2; col index
3563
		$column = self::_GetInt2d($recordData, 2);
3564
		$columnString = PHPExcel_Cell::stringFromColumnIndex($column);
3565
 
3566
		// offset: 20: size: variable; formula structure
3567
		$formulaStructure = substr($recordData, 20);
3568
 
3569
		// offset: 14: size: 2; option flags, recalculate always, recalculate on open etc.
3570
		$options = self::_GetInt2d($recordData, 14);
3571
 
3572
		// bit: 0; mask: 0x0001; 1 = recalculate always
3573
		// bit: 1; mask: 0x0002; 1 = calculate on open
3574
		// bit: 2; mask: 0x0008; 1 = part of a shared formula
3575
		$isPartOfSharedFormula = (bool) (0x0008 & $options);
3576
 
3577
		// WARNING:
3578
		// We can apparently not rely on $isPartOfSharedFormula. Even when $isPartOfSharedFormula = true
3579
		// the formula data may be ordinary formula data, therefore we need to check
3580
		// explicitly for the tExp token (0x01)
3581
		$isPartOfSharedFormula = $isPartOfSharedFormula && ord($formulaStructure{2}) == 0x01;
3582
 
3583
		if ($isPartOfSharedFormula) {
3584
			// part of shared formula which means there will be a formula with a tExp token and nothing else
3585
			// get the base cell, grab tExp token
3586
			$baseRow = self::_GetInt2d($formulaStructure, 3);
3587
			$baseCol = self::_GetInt2d($formulaStructure, 5);
3588
			$this->_baseCell = PHPExcel_Cell::stringFromColumnIndex($baseCol). ($baseRow + 1);
3589
		}
3590
 
3591
		// Read cell?
3592
		if (($this->getReadFilter() !== NULL) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
3593
 
3594
			if ($isPartOfSharedFormula) {
3595
				// formula is added to this cell after the sheet has been read
3596
				$this->_sharedFormulaParts[$columnString . ($row + 1)] = $this->_baseCell;
3597
			}
3598
 
3599
			// offset: 16: size: 4; not used
3600
 
3601
			// offset: 4; size: 2; XF index
3602
			$xfIndex = self::_GetInt2d($recordData, 4);
3603
 
3604
			// offset: 6; size: 8; result of the formula
3605
			if ( (ord($recordData{6}) == 0)
3606
				&& (ord($recordData{12}) == 255)
3607
				&& (ord($recordData{13}) == 255) ) {
3608
 
3609
				// String formula. Result follows in appended STRING record
3610
				$dataType = PHPExcel_Cell_DataType::TYPE_STRING;
3611
 
3612
				// read possible SHAREDFMLA record
3613
				$code = self::_GetInt2d($this->_data, $this->_pos);
3614
				if ($code == self::XLS_Type_SHAREDFMLA) {
3615
					$this->_readSharedFmla();
3616
				}
3617
 
3618
				// read STRING record
3619
				$value = $this->_readString();
3620
 
3621
			} elseif ((ord($recordData{6}) == 1)
3622
				&& (ord($recordData{12}) == 255)
3623
				&& (ord($recordData{13}) == 255)) {
3624
 
3625
				// Boolean formula. Result is in +2; 0=false, 1=true
3626
				$dataType = PHPExcel_Cell_DataType::TYPE_BOOL;
3627
				$value = (bool) ord($recordData{8});
3628
 
3629
			} elseif ((ord($recordData{6}) == 2)
3630
				&& (ord($recordData{12}) == 255)
3631
				&& (ord($recordData{13}) == 255)) {
3632
 
3633
				// Error formula. Error code is in +2
3634
				$dataType = PHPExcel_Cell_DataType::TYPE_ERROR;
3635
				$value = self::_mapErrorCode(ord($recordData{8}));
3636
 
3637
			} elseif ((ord($recordData{6}) == 3)
3638
				&& (ord($recordData{12}) == 255)
3639
				&& (ord($recordData{13}) == 255)) {
3640
 
3641
				// Formula result is a null string
3642
				$dataType = PHPExcel_Cell_DataType::TYPE_NULL;
3643
				$value = '';
3644
 
3645
			} else {
3646
 
3647
				// forumla result is a number, first 14 bytes like _NUMBER record
3648
				$dataType = PHPExcel_Cell_DataType::TYPE_NUMERIC;
3649
				$value = self::_extractNumber(substr($recordData, 6, 8));
3650
 
3651
			}
3652
 
3653
			$cell = $this->_phpSheet->getCell($columnString . ($row + 1));
3654
			if (!$this->_readDataOnly) {
3655
				// add cell style
3656
				$cell->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
3657
			}
3658
 
3659
			// store the formula
3660
			if (!$isPartOfSharedFormula) {
3661
				// not part of shared formula
3662
				// add cell value. If we can read formula, populate with formula, otherwise just used cached value
3663
				try {
3664
					if ($this->_version != self::XLS_BIFF8) {
3665
						throw new PHPExcel_Reader_Exception('Not BIFF8. Can only read BIFF8 formulas');
3666
					}
3667
					$formula = $this->_getFormulaFromStructure($formulaStructure); // get formula in human language
3668
					$cell->setValueExplicit('=' . $formula, PHPExcel_Cell_DataType::TYPE_FORMULA);
3669
 
3670
				} catch (PHPExcel_Exception $e) {
3671
					$cell->setValueExplicit($value, $dataType);
3672
				}
3673
			} else {
3674
				if ($this->_version == self::XLS_BIFF8) {
3675
					// do nothing at this point, formula id added later in the code
3676
				} else {
3677
					$cell->setValueExplicit($value, $dataType);
3678
				}
3679
			}
3680
 
3681
			// store the cached calculated value
3682
			$cell->setCalculatedValue($value);
3683
		}
3684
	}
3685
 
3686
 
3687
	/**
3688
	 * Read a SHAREDFMLA record. This function just stores the binary shared formula in the reader,
3689
	 * which usually contains relative references.
3690
	 * These will be used to construct the formula in each shared formula part after the sheet is read.
3691
	 */
3692
	private function _readSharedFmla()
3693
	{
3694
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
3695
		$recordData = substr($this->_data, $this->_pos + 4, $length);
3696
 
3697
		// move stream pointer to next record
3698
		$this->_pos += 4 + $length;
3699
 
3700
		// offset: 0, size: 6; cell range address of the area used by the shared formula, not used for anything
3701
		$cellRange = substr($recordData, 0, 6);
3702
		$cellRange = $this->_readBIFF5CellRangeAddressFixed($cellRange); // note: even BIFF8 uses BIFF5 syntax
3703
 
3704
		// offset: 6, size: 1; not used
3705
 
3706
		// offset: 7, size: 1; number of existing FORMULA records for this shared formula
3707
		$no = ord($recordData{7});
3708
 
3709
		// offset: 8, size: var; Binary token array of the shared formula
3710
		$formula = substr($recordData, 8);
3711
 
3712
		// at this point we only store the shared formula for later use
3713
		$this->_sharedFormulas[$this->_baseCell] = $formula;
3714
 
3715
	}
3716
 
3717
 
3718
	/**
3719
	 * Read a STRING record from current stream position and advance the stream pointer to next record
3720
	 * This record is used for storing result from FORMULA record when it is a string, and
3721
	 * it occurs directly after the FORMULA record
3722
	 *
3723
	 * @return string The string contents as UTF-8
3724
	 */
3725
	private function _readString()
3726
	{
3727
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
3728
		$recordData = substr($this->_data, $this->_pos + 4, $length);
3729
 
3730
		// move stream pointer to next record
3731
		$this->_pos += 4 + $length;
3732
 
3733
		if ($this->_version == self::XLS_BIFF8) {
3734
			$string = self::_readUnicodeStringLong($recordData);
3735
			$value = $string['value'];
3736
		} else {
3737
			$string = $this->_readByteStringLong($recordData);
3738
			$value = $string['value'];
3739
		}
3740
 
3741
		return $value;
3742
	}
3743
 
3744
 
3745
	/**
3746
	 * Read BOOLERR record
3747
	 * This record represents a Boolean value or error value
3748
	 * cell.
3749
	 *
3750
	 * --	"OpenOffice.org's Documentation of the Microsoft
3751
	 * 		Excel File Format"
3752
	 */
3753
	private function _readBoolErr()
3754
	{
3755
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
3756
		$recordData = substr($this->_data, $this->_pos + 4, $length);
3757
 
3758
		// move stream pointer to next record
3759
		$this->_pos += 4 + $length;
3760
 
3761
		// offset: 0; size: 2; row index
3762
		$row = self::_GetInt2d($recordData, 0);
3763
 
3764
		// offset: 2; size: 2; column index
3765
		$column = self::_GetInt2d($recordData, 2);
3766
		$columnString = PHPExcel_Cell::stringFromColumnIndex($column);
3767
 
3768
		// Read cell?
3769
		if (($this->getReadFilter() !== NULL) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
3770
			// offset: 4; size: 2; index to XF record
3771
			$xfIndex = self::_GetInt2d($recordData, 4);
3772
 
3773
			// offset: 6; size: 1; the boolean value or error value
3774
			$boolErr = ord($recordData{6});
3775
 
3776
			// offset: 7; size: 1; 0=boolean; 1=error
3777
			$isError = ord($recordData{7});
3778
 
3779
			$cell = $this->_phpSheet->getCell($columnString . ($row + 1));
3780
			switch ($isError) {
3781
				case 0: // boolean
3782
					$value = (bool) $boolErr;
3783
 
3784
					// add cell value
3785
					$cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_BOOL);
3786
					break;
3787
 
3788
				case 1: // error type
3789
					$value = self::_mapErrorCode($boolErr);
3790
 
3791
					// add cell value
3792
					$cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_ERROR);
3793
					break;
3794
			}
3795
 
3796
			if (!$this->_readDataOnly) {
3797
				// add cell style
3798
				$cell->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
3799
			}
3800
		}
3801
	}
3802
 
3803
 
3804
	/**
3805
	 * Read MULBLANK record
3806
	 * This record represents a cell range of empty cells. All
3807
	 * cells are located in the same row
3808
	 *
3809
	 * --	"OpenOffice.org's Documentation of the Microsoft
3810
	 * 		Excel File Format"
3811
	 */
3812
	private function _readMulBlank()
3813
	{
3814
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
3815
		$recordData = substr($this->_data, $this->_pos + 4, $length);
3816
 
3817
		// move stream pointer to next record
3818
		$this->_pos += 4 + $length;
3819
 
3820
		// offset: 0; size: 2; index to row
3821
		$row = self::_GetInt2d($recordData, 0);
3822
 
3823
		// offset: 2; size: 2; index to first column
3824
		$fc = self::_GetInt2d($recordData, 2);
3825
 
3826
		// offset: 4; size: 2 x nc; list of indexes to XF records
3827
		// add style information
3828
		if (!$this->_readDataOnly) {
3829
			for ($i = 0; $i < $length / 2 - 3; ++$i) {
3830
				$columnString = PHPExcel_Cell::stringFromColumnIndex($fc + $i);
3831
 
3832
				// Read cell?
3833
				if (($this->getReadFilter() !== NULL) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
3834
					$xfIndex = self::_GetInt2d($recordData, 4 + 2 * $i);
3835
					$this->_phpSheet->getCell($columnString . ($row + 1))->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
3836
				}
3837
			}
3838
		}
3839
 
3840
		// offset: 6; size 2; index to last column (not needed)
3841
	}
3842
 
3843
 
3844
	/**
3845
	 * Read LABEL record
3846
	 * This record represents a cell that contains a string. In
3847
	 * BIFF8 it is usually replaced by the LABELSST record.
3848
	 * Excel still uses this record, if it copies unformatted
3849
	 * text cells to the clipboard.
3850
	 *
3851
	 * --	"OpenOffice.org's Documentation of the Microsoft
3852
	 * 		Excel File Format"
3853
	 */
3854
	private function _readLabel()
3855
	{
3856
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
3857
		$recordData = substr($this->_data, $this->_pos + 4, $length);
3858
 
3859
		// move stream pointer to next record
3860
		$this->_pos += 4 + $length;
3861
 
3862
		// offset: 0; size: 2; index to row
3863
		$row = self::_GetInt2d($recordData, 0);
3864
 
3865
		// offset: 2; size: 2; index to column
3866
		$column = self::_GetInt2d($recordData, 2);
3867
		$columnString = PHPExcel_Cell::stringFromColumnIndex($column);
3868
 
3869
		// Read cell?
3870
		if (($this->getReadFilter() !== NULL) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
3871
			// offset: 4; size: 2; XF index
3872
			$xfIndex = self::_GetInt2d($recordData, 4);
3873
 
3874
			// add cell value
3875
			// todo: what if string is very long? continue record
3876
			if ($this->_version == self::XLS_BIFF8) {
3877
				$string = self::_readUnicodeStringLong(substr($recordData, 6));
3878
				$value = $string['value'];
3879
			} else {
3880
				$string = $this->_readByteStringLong(substr($recordData, 6));
3881
				$value = $string['value'];
3882
			}
3883
			$cell = $this->_phpSheet->getCell($columnString . ($row + 1));
3884
			$cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_STRING);
3885
 
3886
			if (!$this->_readDataOnly) {
3887
				// add cell style
3888
				$cell->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
3889
			}
3890
		}
3891
	}
3892
 
3893
 
3894
	/**
3895
	 * Read BLANK record
3896
	 */
3897
	private function _readBlank()
3898
	{
3899
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
3900
		$recordData = substr($this->_data, $this->_pos + 4, $length);
3901
 
3902
		// move stream pointer to next record
3903
		$this->_pos += 4 + $length;
3904
 
3905
		// offset: 0; size: 2; row index
3906
		$row = self::_GetInt2d($recordData, 0);
3907
 
3908
		// offset: 2; size: 2; col index
3909
		$col = self::_GetInt2d($recordData, 2);
3910
		$columnString = PHPExcel_Cell::stringFromColumnIndex($col);
3911
 
3912
		// Read cell?
3913
		if (($this->getReadFilter() !== NULL) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
3914
			// offset: 4; size: 2; XF index
3915
			$xfIndex = self::_GetInt2d($recordData, 4);
3916
 
3917
			// add style information
3918
			if (!$this->_readDataOnly) {
3919
				$this->_phpSheet->getCell($columnString . ($row + 1))->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
3920
			}
3921
		}
3922
 
3923
	}
3924
 
3925
 
3926
	/**
3927
	 * Read MSODRAWING record
3928
	 */
3929
	private function _readMsoDrawing()
3930
	{
3931
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
3932
 
3933
		// get spliced record data
3934
		$splicedRecordData = $this->_getSplicedRecordData();
3935
		$recordData = $splicedRecordData['recordData'];
3936
 
3937
		$this->_drawingData .= $recordData;
3938
	}
3939
 
3940
 
3941
	/**
3942
	 * Read OBJ record
3943
	 */
3944
	private function _readObj()
3945
	{
3946
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
3947
		$recordData = substr($this->_data, $this->_pos + 4, $length);
3948
 
3949
		// move stream pointer to next record
3950
		$this->_pos += 4 + $length;
3951
 
3952
		if ($this->_readDataOnly || $this->_version != self::XLS_BIFF8) {
3953
			return;
3954
		}
3955
 
3956
		// recordData consists of an array of subrecords looking like this:
3957
		//	ft: 2 bytes; ftCmo type (0x15)
3958
		//	cb: 2 bytes; size in bytes of ftCmo data
3959
		//	ot: 2 bytes; Object Type
3960
		//	id: 2 bytes; Object id number
3961
		//	grbit: 2 bytes; Option Flags
3962
		//	data: var; subrecord data
3963
 
3964
		// for now, we are just interested in the second subrecord containing the object type
3965
		$ftCmoType	= self::_GetInt2d($recordData, 0);
3966
		$cbCmoSize	= self::_GetInt2d($recordData, 2);
3967
		$otObjType	= self::_GetInt2d($recordData, 4);
3968
		$idObjID	= self::_GetInt2d($recordData, 6);
3969
		$grbitOpts	= self::_GetInt2d($recordData, 6);
3970
 
3971
		$this->_objs[] = array(
3972
			'ftCmoType'	=> $ftCmoType,
3973
			'cbCmoSize'	=> $cbCmoSize,
3974
			'otObjType'	=> $otObjType,
3975
			'idObjID'	=> $idObjID,
3976
			'grbitOpts'	=> $grbitOpts
3977
		);
3978
		$this->textObjRef = $idObjID;
3979
 
3980
//		echo '<b>_readObj()</b><br />';
3981
//		var_dump(end($this->_objs));
3982
//		echo '<br />';
3983
	}
3984
 
3985
 
3986
	/**
3987
	 * Read WINDOW2 record
3988
	 */
3989
	private function _readWindow2()
3990
	{
3991
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
3992
		$recordData = substr($this->_data, $this->_pos + 4, $length);
3993
 
3994
		// move stream pointer to next record
3995
		$this->_pos += 4 + $length;
3996
 
3997
		// offset: 0; size: 2; option flags
3998
		$options = self::_GetInt2d($recordData, 0);
3999
 
4000
		// offset: 2; size: 2; index to first visible row
4001
		$firstVisibleRow = self::_GetInt2d($recordData, 2);
4002
 
4003
		// offset: 4; size: 2; index to first visible colum
4004
		$firstVisibleColumn = self::_GetInt2d($recordData, 4);
4005
		if ($this->_version === self::XLS_BIFF8) {
4006
			// offset:  8; size: 2; not used
4007
			// offset: 10; size: 2; cached magnification factor in page break preview (in percent); 0 = Default (60%)
4008
			// offset: 12; size: 2; cached magnification factor in normal view (in percent); 0 = Default (100%)
4009
			// offset: 14; size: 4; not used
4010
			$zoomscaleInPageBreakPreview = self::_GetInt2d($recordData, 10);
4011
			if ($zoomscaleInPageBreakPreview === 0) $zoomscaleInPageBreakPreview = 60;
4012
			$zoomscaleInNormalView = self::_GetInt2d($recordData, 12);
4013
			if ($zoomscaleInNormalView === 0) $zoomscaleInNormalView = 100;
4014
		}
4015
 
4016
		// bit: 1; mask: 0x0002; 0 = do not show gridlines, 1 = show gridlines
4017
		$showGridlines = (bool) ((0x0002 & $options) >> 1);
4018
		$this->_phpSheet->setShowGridlines($showGridlines);
4019
 
4020
		// bit: 2; mask: 0x0004; 0 = do not show headers, 1 = show headers
4021
		$showRowColHeaders = (bool) ((0x0004 & $options) >> 2);
4022
		$this->_phpSheet->setShowRowColHeaders($showRowColHeaders);
4023
 
4024
		// bit: 3; mask: 0x0008; 0 = panes are not frozen, 1 = panes are frozen
4025
		$this->_frozen = (bool) ((0x0008 & $options) >> 3);
4026
 
4027
		// bit: 6; mask: 0x0040; 0 = columns from left to right, 1 = columns from right to left
4028
		$this->_phpSheet->setRightToLeft((bool)((0x0040 & $options) >> 6));
4029
 
4030
		// bit: 10; mask: 0x0400; 0 = sheet not active, 1 = sheet active
4031
		$isActive = (bool) ((0x0400 & $options) >> 10);
4032
		if ($isActive) {
4033
			$this->_phpExcel->setActiveSheetIndex($this->_phpExcel->getIndex($this->_phpSheet));
4034
		}
4035
 
4036
		// bit: 11; mask: 0x0800; 0 = normal view, 1 = page break view
4037
		$isPageBreakPreview = (bool) ((0x0800 & $options) >> 11);
4038
 
4039
		//FIXME: set $firstVisibleRow and $firstVisibleColumn
4040
 
4041
		if ($this->_phpSheet->getSheetView()->getView() !== PHPExcel_Worksheet_SheetView::SHEETVIEW_PAGE_LAYOUT) {
4042
			//NOTE: this setting is inferior to page layout view(Excel2007-)
4043
			$view = $isPageBreakPreview? PHPExcel_Worksheet_SheetView::SHEETVIEW_PAGE_BREAK_PREVIEW :
4044
				PHPExcel_Worksheet_SheetView::SHEETVIEW_NORMAL;
4045
			$this->_phpSheet->getSheetView()->setView($view);
4046
			if ($this->_version === self::XLS_BIFF8) {
4047
				$zoomScale = $isPageBreakPreview? $zoomscaleInPageBreakPreview : $zoomscaleInNormalView;
4048
				$this->_phpSheet->getSheetView()->setZoomScale($zoomScale);
4049
				$this->_phpSheet->getSheetView()->setZoomScaleNormal($zoomscaleInNormalView);
4050
			}
4051
		}
4052
	}
4053
 
4054
	/**
4055
	 * Read PLV Record(Created by Excel2007 or upper)
4056
	 */
4057
	private function _readPageLayoutView(){
4058
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
4059
		$recordData = substr($this->_data, $this->_pos + 4, $length);
4060
 
4061
		// move stream pointer to next record
4062
		$this->_pos += 4 + $length;
4063
 
4064
		//var_dump(unpack("vrt/vgrbitFrt/V2reserved/vwScalePLV/vgrbit", $recordData));
4065
 
4066
		// offset: 0; size: 2; rt
4067
		//->ignore
4068
		$rt = self::_GetInt2d($recordData, 0);
4069
		// offset: 2; size: 2; grbitfr
4070
		//->ignore
4071
		$grbitFrt = self::_GetInt2d($recordData, 2);
4072
		// offset: 4; size: 8; reserved
4073
		//->ignore
4074
 
4075
		// offset: 12; size 2; zoom scale
4076
		$wScalePLV = self::_GetInt2d($recordData, 12);
4077
		// offset: 14; size 2; grbit
4078
		$grbit = self::_GetInt2d($recordData, 14);
4079
 
4080
		// decomprise grbit
4081
		$fPageLayoutView   = $grbit & 0x01;
4082
		$fRulerVisible     = ($grbit >> 1) & 0x01; //no support
4083
		$fWhitespaceHidden = ($grbit >> 3) & 0x01; //no support
4084
 
4085
		if ($fPageLayoutView === 1) {
4086
			$this->_phpSheet->getSheetView()->setView(PHPExcel_Worksheet_SheetView::SHEETVIEW_PAGE_LAYOUT);
4087
			$this->_phpSheet->getSheetView()->setZoomScale($wScalePLV); //set by Excel2007 only if SHEETVIEW_PAGE_LAYOUT
4088
		}
4089
		//otherwise, we cannot know whether SHEETVIEW_PAGE_LAYOUT or SHEETVIEW_PAGE_BREAK_PREVIEW.
4090
	}
4091
 
4092
	/**
4093
	 * Read SCL record
4094
	 */
4095
	private function _readScl()
4096
	{
4097
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
4098
		$recordData = substr($this->_data, $this->_pos + 4, $length);
4099
 
4100
		// move stream pointer to next record
4101
		$this->_pos += 4 + $length;
4102
 
4103
		// offset: 0; size: 2; numerator of the view magnification
4104
		$numerator = self::_GetInt2d($recordData, 0);
4105
 
4106
		// offset: 2; size: 2; numerator of the view magnification
4107
		$denumerator = self::_GetInt2d($recordData, 2);
4108
 
4109
		// set the zoom scale (in percent)
4110
		$this->_phpSheet->getSheetView()->setZoomScale($numerator * 100 / $denumerator);
4111
	}
4112
 
4113
 
4114
	/**
4115
	 * Read PANE record
4116
	 */
4117
	private function _readPane()
4118
	{
4119
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
4120
		$recordData = substr($this->_data, $this->_pos + 4, $length);
4121
 
4122
		// move stream pointer to next record
4123
		$this->_pos += 4 + $length;
4124
 
4125
		if (!$this->_readDataOnly) {
4126
			// offset: 0; size: 2; position of vertical split
4127
			$px = self::_GetInt2d($recordData, 0);
4128
 
4129
			// offset: 2; size: 2; position of horizontal split
4130
			$py = self::_GetInt2d($recordData, 2);
4131
 
4132
			if ($this->_frozen) {
4133
				// frozen panes
4134
				$this->_phpSheet->freezePane(PHPExcel_Cell::stringFromColumnIndex($px) . ($py + 1));
4135
			} else {
4136
				// unfrozen panes; split windows; not supported by PHPExcel core
4137
			}
4138
		}
4139
	}
4140
 
4141
 
4142
	/**
4143
	 * Read SELECTION record. There is one such record for each pane in the sheet.
4144
	 */
4145
	private function _readSelection()
4146
	{
4147
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
4148
		$recordData = substr($this->_data, $this->_pos + 4, $length);
4149
 
4150
		// move stream pointer to next record
4151
		$this->_pos += 4 + $length;
4152
 
4153
		if (!$this->_readDataOnly) {
4154
			// offset: 0; size: 1; pane identifier
4155
			$paneId = ord($recordData{0});
4156
 
4157
			// offset: 1; size: 2; index to row of the active cell
4158
			$r = self::_GetInt2d($recordData, 1);
4159
 
4160
			// offset: 3; size: 2; index to column of the active cell
4161
			$c = self::_GetInt2d($recordData, 3);
4162
 
4163
			// offset: 5; size: 2; index into the following cell range list to the
4164
			//  entry that contains the active cell
4165
			$index = self::_GetInt2d($recordData, 5);
4166
 
4167
			// offset: 7; size: var; cell range address list containing all selected cell ranges
4168
			$data = substr($recordData, 7);
4169
			$cellRangeAddressList = $this->_readBIFF5CellRangeAddressList($data); // note: also BIFF8 uses BIFF5 syntax
4170
 
4171
			$selectedCells = $cellRangeAddressList['cellRangeAddresses'][0];
4172
 
4173
			// first row '1' + last row '16384' indicates that full column is selected (apparently also in BIFF8!)
4174
			if (preg_match('/^([A-Z]+1\:[A-Z]+)16384$/', $selectedCells)) {
4175
				$selectedCells = preg_replace('/^([A-Z]+1\:[A-Z]+)16384$/', '${1}1048576', $selectedCells);
4176
			}
4177
 
4178
			// first row '1' + last row '65536' indicates that full column is selected
4179
			if (preg_match('/^([A-Z]+1\:[A-Z]+)65536$/', $selectedCells)) {
4180
				$selectedCells = preg_replace('/^([A-Z]+1\:[A-Z]+)65536$/', '${1}1048576', $selectedCells);
4181
			}
4182
 
4183
			// first column 'A' + last column 'IV' indicates that full row is selected
4184
			if (preg_match('/^(A[0-9]+\:)IV([0-9]+)$/', $selectedCells)) {
4185
				$selectedCells = preg_replace('/^(A[0-9]+\:)IV([0-9]+)$/', '${1}XFD${2}', $selectedCells);
4186
			}
4187
 
4188
			$this->_phpSheet->setSelectedCells($selectedCells);
4189
		}
4190
	}
4191
 
4192
 
4193
	private function _includeCellRangeFiltered($cellRangeAddress)
4194
	{
4195
		$includeCellRange = true;
4196
		if ($this->getReadFilter() !== NULL) {
4197
			$includeCellRange = false;
4198
			$rangeBoundaries = PHPExcel_Cell::getRangeBoundaries($cellRangeAddress);
4199
			$rangeBoundaries[1][0]++;
4200
			for ($row = $rangeBoundaries[0][1]; $row <= $rangeBoundaries[1][1]; $row++) {
4201
				for ($column = $rangeBoundaries[0][0]; $column != $rangeBoundaries[1][0]; $column++) {
4202
					if ($this->getReadFilter()->readCell($column, $row, $this->_phpSheet->getTitle())) {
4203
						$includeCellRange = true;
4204
						break 2;
4205
					}
4206
				}
4207
			}
4208
		}
4209
		return $includeCellRange;
4210
	}
4211
 
4212
 
4213
	/**
4214
	 * MERGEDCELLS
4215
	 *
4216
	 * This record contains the addresses of merged cell ranges
4217
	 * in the current sheet.
4218
	 *
4219
	 * --	"OpenOffice.org's Documentation of the Microsoft
4220
	 * 		Excel File Format"
4221
	 */
4222
	private function _readMergedCells()
4223
	{
4224
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
4225
		$recordData = substr($this->_data, $this->_pos + 4, $length);
4226
 
4227
		// move stream pointer to next record
4228
		$this->_pos += 4 + $length;
4229
 
4230
		if ($this->_version == self::XLS_BIFF8 && !$this->_readDataOnly) {
4231
			$cellRangeAddressList = $this->_readBIFF8CellRangeAddressList($recordData);
4232
			foreach ($cellRangeAddressList['cellRangeAddresses'] as $cellRangeAddress) {
4233
				if ((strpos($cellRangeAddress,':') !== FALSE) &&
4234
					($this->_includeCellRangeFiltered($cellRangeAddress))) {
4235
					$this->_phpSheet->mergeCells($cellRangeAddress);
4236
				}
4237
			}
4238
		}
4239
	}
4240
 
4241
 
4242
	/**
4243
	 * Read HYPERLINK record
4244
	 */
4245
	private function _readHyperLink()
4246
	{
4247
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
4248
		$recordData = substr($this->_data, $this->_pos + 4, $length);
4249
 
4250
		// move stream pointer forward to next record
4251
		$this->_pos += 4 + $length;
4252
 
4253
		if (!$this->_readDataOnly) {
4254
			// offset: 0; size: 8; cell range address of all cells containing this hyperlink
4255
			try {
4256
				$cellRange = $this->_readBIFF8CellRangeAddressFixed($recordData, 0, 8);
4257
			} catch (PHPExcel_Exception $e) {
4258
				return;
4259
			}
4260
 
4261
			// offset: 8, size: 16; GUID of StdLink
4262
 
4263
			// offset: 24, size: 4; unknown value
4264
 
4265
			// offset: 28, size: 4; option flags
4266
 
4267
				// bit: 0; mask: 0x00000001; 0 = no link or extant, 1 = file link or URL
4268
				$isFileLinkOrUrl = (0x00000001 & self::_GetInt2d($recordData, 28)) >> 0;
4269
 
4270
				// bit: 1; mask: 0x00000002; 0 = relative path, 1 = absolute path or URL
4271
				$isAbsPathOrUrl = (0x00000001 & self::_GetInt2d($recordData, 28)) >> 1;
4272
 
4273
				// bit: 2 (and 4); mask: 0x00000014; 0 = no description
4274
				$hasDesc = (0x00000014 & self::_GetInt2d($recordData, 28)) >> 2;
4275
 
4276
				// bit: 3; mask: 0x00000008; 0 = no text, 1 = has text
4277
				$hasText = (0x00000008 & self::_GetInt2d($recordData, 28)) >> 3;
4278
 
4279
				// bit: 7; mask: 0x00000080; 0 = no target frame, 1 = has target frame
4280
				$hasFrame = (0x00000080 & self::_GetInt2d($recordData, 28)) >> 7;
4281
 
4282
				// bit: 8; mask: 0x00000100; 0 = file link or URL, 1 = UNC path (inc. server name)
4283
				$isUNC = (0x00000100 & self::_GetInt2d($recordData, 28)) >> 8;
4284
 
4285
			// offset within record data
4286
			$offset = 32;
4287
 
4288
			if ($hasDesc) {
4289
				// offset: 32; size: var; character count of description text
4290
				$dl = self::_GetInt4d($recordData, 32);
4291
				// offset: 36; size: var; character array of description text, no Unicode string header, always 16-bit characters, zero terminated
4292
				$desc = self::_encodeUTF16(substr($recordData, 36, 2 * ($dl - 1)), false);
4293
				$offset += 4 + 2 * $dl;
4294
			}
4295
			if ($hasFrame) {
4296
				$fl = self::_GetInt4d($recordData, $offset);
4297
				$offset += 4 + 2 * $fl;
4298
			}
4299
 
4300
			// detect type of hyperlink (there are 4 types)
4301
			$hyperlinkType = null;
4302
 
4303
			if ($isUNC) {
4304
				$hyperlinkType = 'UNC';
4305
			} else if (!$isFileLinkOrUrl) {
4306
				$hyperlinkType = 'workbook';
4307
			} else if (ord($recordData{$offset}) == 0x03) {
4308
				$hyperlinkType = 'local';
4309
			} else if (ord($recordData{$offset}) == 0xE0) {
4310
				$hyperlinkType = 'URL';
4311
			}
4312
 
4313
			switch ($hyperlinkType) {
4314
			case 'URL':
4315
				// section 5.58.2: Hyperlink containing a URL
4316
				// e.g. http://example.org/index.php
4317
 
4318
				// offset: var; size: 16; GUID of URL Moniker
4319
				$offset += 16;
4320
				// offset: var; size: 4; size (in bytes) of character array of the URL including trailing zero word
4321
				$us = self::_GetInt4d($recordData, $offset);
4322
				$offset += 4;
4323
				// offset: var; size: $us; character array of the URL, no Unicode string header, always 16-bit characters, zero-terminated
4324
				$url = self::_encodeUTF16(substr($recordData, $offset, $us - 2), false);
4325
				$url .= $hasText ? '#' : '';
4326
				$offset += $us;
4327
				break;
4328
 
4329
			case 'local':
4330
				// section 5.58.3: Hyperlink to local file
4331
				// examples:
4332
				//   mydoc.txt
4333
				//   ../../somedoc.xls#Sheet!A1
4334
 
4335
				// offset: var; size: 16; GUI of File Moniker
4336
				$offset += 16;
4337
 
4338
				// offset: var; size: 2; directory up-level count.
4339
				$upLevelCount = self::_GetInt2d($recordData, $offset);
4340
				$offset += 2;
4341
 
4342
				// offset: var; size: 4; character count of the shortened file path and name, including trailing zero word
4343
				$sl = self::_GetInt4d($recordData, $offset);
4344
				$offset += 4;
4345
 
4346
				// offset: var; size: sl; character array of the shortened file path and name in 8.3-DOS-format (compressed Unicode string)
4347
				$shortenedFilePath = substr($recordData, $offset, $sl);
4348
				$shortenedFilePath = self::_encodeUTF16($shortenedFilePath, true);
4349
				$shortenedFilePath = substr($shortenedFilePath, 0, -1); // remove trailing zero
4350
 
4351
				$offset += $sl;
4352
 
4353
				// offset: var; size: 24; unknown sequence
4354
				$offset += 24;
4355
 
4356
				// extended file path
4357
				// offset: var; size: 4; size of the following file link field including string lenth mark
4358
				$sz = self::_GetInt4d($recordData, $offset);
4359
				$offset += 4;
4360
 
4361
				// only present if $sz > 0
4362
				if ($sz > 0) {
4363
					// offset: var; size: 4; size of the character array of the extended file path and name
4364
					$xl = self::_GetInt4d($recordData, $offset);
4365
					$offset += 4;
4366
 
4367
					// offset: var; size 2; unknown
4368
					$offset += 2;
4369
 
4370
					// offset: var; size $xl; character array of the extended file path and name.
4371
					$extendedFilePath = substr($recordData, $offset, $xl);
4372
					$extendedFilePath = self::_encodeUTF16($extendedFilePath, false);
4373
					$offset += $xl;
4374
				}
4375
 
4376
				// construct the path
4377
				$url = str_repeat('..\\', $upLevelCount);
4378
				$url .= ($sz > 0) ?
4379
					$extendedFilePath : $shortenedFilePath; // use extended path if available
4380
				$url .= $hasText ? '#' : '';
4381
 
4382
				break;
4383
 
4384
 
4385
			case 'UNC':
4386
				// section 5.58.4: Hyperlink to a File with UNC (Universal Naming Convention) Path
4387
				// todo: implement
4388
				return;
4389
 
4390
			case 'workbook':
4391
				// section 5.58.5: Hyperlink to the Current Workbook
4392
				// e.g. Sheet2!B1:C2, stored in text mark field
4393
				$url = 'sheet://';
4394
				break;
4395
 
4396
			default:
4397
				return;
4398
 
4399
			}
4400
 
4401
			if ($hasText) {
4402
				// offset: var; size: 4; character count of text mark including trailing zero word
4403
				$tl = self::_GetInt4d($recordData, $offset);
4404
				$offset += 4;
4405
				// offset: var; size: var; character array of the text mark without the # sign, no Unicode header, always 16-bit characters, zero-terminated
4406
				$text = self::_encodeUTF16(substr($recordData, $offset, 2 * ($tl - 1)), false);
4407
				$url .= $text;
4408
			}
4409
 
4410
			// apply the hyperlink to all the relevant cells
4411
			foreach (PHPExcel_Cell::extractAllCellReferencesInRange($cellRange) as $coordinate) {
4412
				$this->_phpSheet->getCell($coordinate)->getHyperLink()->setUrl($url);
4413
			}
4414
		}
4415
	}
4416
 
4417
 
4418
	/**
4419
	 * Read DATAVALIDATIONS record
4420
	 */
4421
	private function _readDataValidations()
4422
	{
4423
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
4424
		$recordData = substr($this->_data, $this->_pos + 4, $length);
4425
 
4426
		// move stream pointer forward to next record
4427
		$this->_pos += 4 + $length;
4428
	}
4429
 
4430
 
4431
	/**
4432
	 * Read DATAVALIDATION record
4433
	 */
4434
	private function _readDataValidation()
4435
	{
4436
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
4437
		$recordData = substr($this->_data, $this->_pos + 4, $length);
4438
 
4439
		// move stream pointer forward to next record
4440
		$this->_pos += 4 + $length;
4441
 
4442
		if ($this->_readDataOnly) {
4443
			return;
4444
		}
4445
 
4446
		// offset: 0; size: 4; Options
4447
		$options = self::_GetInt4d($recordData, 0);
4448
 
4449
		// bit: 0-3; mask: 0x0000000F; type
4450
		$type = (0x0000000F & $options) >> 0;
4451
		switch ($type) {
4452
			case 0x00:	$type = PHPExcel_Cell_DataValidation::TYPE_NONE;		break;
4453
			case 0x01:	$type = PHPExcel_Cell_DataValidation::TYPE_WHOLE;		break;
4454
			case 0x02:	$type = PHPExcel_Cell_DataValidation::TYPE_DECIMAL;		break;
4455
			case 0x03:	$type = PHPExcel_Cell_DataValidation::TYPE_LIST;		break;
4456
			case 0x04:	$type = PHPExcel_Cell_DataValidation::TYPE_DATE;		break;
4457
			case 0x05:	$type = PHPExcel_Cell_DataValidation::TYPE_TIME;		break;
4458
			case 0x06:	$type = PHPExcel_Cell_DataValidation::TYPE_TEXTLENGTH;	break;
4459
			case 0x07:	$type = PHPExcel_Cell_DataValidation::TYPE_CUSTOM;		break;
4460
		}
4461
 
4462
		// bit: 4-6; mask: 0x00000070; error type
4463
		$errorStyle = (0x00000070 & $options) >> 4;
4464
		switch ($errorStyle) {
4465
			case 0x00:	$errorStyle = PHPExcel_Cell_DataValidation::STYLE_STOP;			break;
4466
			case 0x01:	$errorStyle = PHPExcel_Cell_DataValidation::STYLE_WARNING;		break;
4467
			case 0x02:	$errorStyle = PHPExcel_Cell_DataValidation::STYLE_INFORMATION;	break;
4468
		}
4469
 
4470
		// bit: 7; mask: 0x00000080; 1= formula is explicit (only applies to list)
4471
		// I have only seen cases where this is 1
4472
		$explicitFormula = (0x00000080 & $options) >> 7;
4473
 
4474
		// bit: 8; mask: 0x00000100; 1= empty cells allowed
4475
		$allowBlank = (0x00000100 & $options) >> 8;
4476
 
4477
		// bit: 9; mask: 0x00000200; 1= suppress drop down arrow in list type validity
4478
		$suppressDropDown = (0x00000200 & $options) >> 9;
4479
 
4480
		// bit: 18; mask: 0x00040000; 1= show prompt box if cell selected
4481
		$showInputMessage = (0x00040000 & $options) >> 18;
4482
 
4483
		// bit: 19; mask: 0x00080000; 1= show error box if invalid values entered
4484
		$showErrorMessage = (0x00080000 & $options) >> 19;
4485
 
4486
		// bit: 20-23; mask: 0x00F00000; condition operator
4487
		$operator = (0x00F00000 & $options) >> 20;
4488
		switch ($operator) {
4489
			case 0x00: $operator = PHPExcel_Cell_DataValidation::OPERATOR_BETWEEN			;	break;
4490
			case 0x01: $operator = PHPExcel_Cell_DataValidation::OPERATOR_NOTBETWEEN		;	break;
4491
			case 0x02: $operator = PHPExcel_Cell_DataValidation::OPERATOR_EQUAL				;	break;
4492
			case 0x03: $operator = PHPExcel_Cell_DataValidation::OPERATOR_NOTEQUAL			;	break;
4493
			case 0x04: $operator = PHPExcel_Cell_DataValidation::OPERATOR_GREATERTHAN		;	break;
4494
			case 0x05: $operator = PHPExcel_Cell_DataValidation::OPERATOR_LESSTHAN			;	break;
4495
			case 0x06: $operator = PHPExcel_Cell_DataValidation::OPERATOR_GREATERTHANOREQUAL;	break;
4496
			case 0x07: $operator = PHPExcel_Cell_DataValidation::OPERATOR_LESSTHANOREQUAL	;	break;
4497
		}
4498
 
4499
		// offset: 4; size: var; title of the prompt box
4500
		$offset = 4;
4501
		$string = self::_readUnicodeStringLong(substr($recordData, $offset));
4502
		$promptTitle = $string['value'] !== chr(0) ?
4503
			$string['value'] : '';
4504
		$offset += $string['size'];
4505
 
4506
		// offset: var; size: var; title of the error box
4507
		$string = self::_readUnicodeStringLong(substr($recordData, $offset));
4508
		$errorTitle = $string['value'] !== chr(0) ?
4509
			$string['value'] : '';
4510
		$offset += $string['size'];
4511
 
4512
		// offset: var; size: var; text of the prompt box
4513
		$string = self::_readUnicodeStringLong(substr($recordData, $offset));
4514
		$prompt = $string['value'] !== chr(0) ?
4515
			$string['value'] : '';
4516
		$offset += $string['size'];
4517
 
4518
		// offset: var; size: var; text of the error box
4519
		$string = self::_readUnicodeStringLong(substr($recordData, $offset));
4520
		$error = $string['value'] !== chr(0) ?
4521
			$string['value'] : '';
4522
		$offset += $string['size'];
4523
 
4524
		// offset: var; size: 2; size of the formula data for the first condition
4525
		$sz1 = self::_GetInt2d($recordData, $offset);
4526
		$offset += 2;
4527
 
4528
		// offset: var; size: 2; not used
4529
		$offset += 2;
4530
 
4531
		// offset: var; size: $sz1; formula data for first condition (without size field)
4532
		$formula1 = substr($recordData, $offset, $sz1);
4533
		$formula1 = pack('v', $sz1) . $formula1; // prepend the length
4534
		try {
4535
			$formula1 = $this->_getFormulaFromStructure($formula1);
4536
 
4537
			// in list type validity, null characters are used as item separators
4538
			if ($type == PHPExcel_Cell_DataValidation::TYPE_LIST) {
4539
				$formula1 = str_replace(chr(0), ',', $formula1);
4540
			}
4541
		} catch (PHPExcel_Exception $e) {
4542
			return;
4543
		}
4544
		$offset += $sz1;
4545
 
4546
		// offset: var; size: 2; size of the formula data for the first condition
4547
		$sz2 = self::_GetInt2d($recordData, $offset);
4548
		$offset += 2;
4549
 
4550
		// offset: var; size: 2; not used
4551
		$offset += 2;
4552
 
4553
		// offset: var; size: $sz2; formula data for second condition (without size field)
4554
		$formula2 = substr($recordData, $offset, $sz2);
4555
		$formula2 = pack('v', $sz2) . $formula2; // prepend the length
4556
		try {
4557
			$formula2 = $this->_getFormulaFromStructure($formula2);
4558
		} catch (PHPExcel_Exception $e) {
4559
			return;
4560
		}
4561
		$offset += $sz2;
4562
 
4563
		// offset: var; size: var; cell range address list with
4564
		$cellRangeAddressList = $this->_readBIFF8CellRangeAddressList(substr($recordData, $offset));
4565
		$cellRangeAddresses = $cellRangeAddressList['cellRangeAddresses'];
4566
 
4567
		foreach ($cellRangeAddresses as $cellRange) {
4568
			$stRange = $this->_phpSheet->shrinkRangeToFit($cellRange);
4569
			$stRange = PHPExcel_Cell::extractAllCellReferencesInRange($stRange);
4570
			foreach ($stRange as $coordinate) {
4571
				$objValidation = $this->_phpSheet->getCell($coordinate)->getDataValidation();
4572
				$objValidation->setType($type);
4573
				$objValidation->setErrorStyle($errorStyle);
4574
				$objValidation->setAllowBlank((bool)$allowBlank);
4575
				$objValidation->setShowInputMessage((bool)$showInputMessage);
4576
				$objValidation->setShowErrorMessage((bool)$showErrorMessage);
4577
				$objValidation->setShowDropDown(!$suppressDropDown);
4578
				$objValidation->setOperator($operator);
4579
				$objValidation->setErrorTitle($errorTitle);
4580
				$objValidation->setError($error);
4581
				$objValidation->setPromptTitle($promptTitle);
4582
				$objValidation->setPrompt($prompt);
4583
				$objValidation->setFormula1($formula1);
4584
				$objValidation->setFormula2($formula2);
4585
			}
4586
		}
4587
 
4588
	}
4589
 
4590
 
4591
	/**
4592
	 * Read SHEETLAYOUT record. Stores sheet tab color information.
4593
	 */
4594
	private function _readSheetLayout()
4595
	{
4596
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
4597
		$recordData = substr($this->_data, $this->_pos + 4, $length);
4598
 
4599
		// move stream pointer to next record
4600
		$this->_pos += 4 + $length;
4601
 
4602
		// local pointer in record data
4603
		$offset = 0;
4604
 
4605
		if (!$this->_readDataOnly) {
4606
			// offset: 0; size: 2; repeated record identifier 0x0862
4607
 
4608
			// offset: 2; size: 10; not used
4609
 
4610
			// offset: 12; size: 4; size of record data
4611
			// Excel 2003 uses size of 0x14 (documented), Excel 2007 uses size of 0x28 (not documented?)
4612
			$sz = self::_GetInt4d($recordData, 12);
4613
 
4614
			switch ($sz) {
4615
				case 0x14:
4616
					// offset: 16; size: 2; color index for sheet tab
4617
					$colorIndex = self::_GetInt2d($recordData, 16);
4618
					$color = self::_readColor($colorIndex,$this->_palette,$this->_version);
4619
					$this->_phpSheet->getTabColor()->setRGB($color['rgb']);
4620
					break;
4621
 
4622
				case 0x28:
4623
					// TODO: Investigate structure for .xls SHEETLAYOUT record as saved by MS Office Excel 2007
4624
					return;
4625
					break;
4626
			}
4627
		}
4628
	}
4629
 
4630
 
4631
	/**
4632
	 * Read SHEETPROTECTION record (FEATHEADR)
4633
	 */
4634
	private function _readSheetProtection()
4635
	{
4636
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
4637
		$recordData = substr($this->_data, $this->_pos + 4, $length);
4638
 
4639
		// move stream pointer to next record
4640
		$this->_pos += 4 + $length;
4641
 
4642
		if ($this->_readDataOnly) {
4643
			return;
4644
		}
4645
 
4646
		// offset: 0; size: 2; repeated record header
4647
 
4648
		// offset: 2; size: 2; FRT cell reference flag (=0 currently)
4649
 
4650
		// offset: 4; size: 8; Currently not used and set to 0
4651
 
4652
		// offset: 12; size: 2; Shared feature type index (2=Enhanced Protetion, 4=SmartTag)
4653
		$isf = self::_GetInt2d($recordData, 12);
4654
		if ($isf != 2) {
4655
			return;
4656
		}
4657
 
4658
		// offset: 14; size: 1; =1 since this is a feat header
4659
 
4660
		// offset: 15; size: 4; size of rgbHdrSData
4661
 
4662
		// rgbHdrSData, assume "Enhanced Protection"
4663
		// offset: 19; size: 2; option flags
4664
		$options = self::_GetInt2d($recordData, 19);
4665
 
4666
		// bit: 0; mask 0x0001; 1 = user may edit objects, 0 = users must not edit objects
4667
		$bool = (0x0001 & $options) >> 0;
4668
		$this->_phpSheet->getProtection()->setObjects(!$bool);
4669
 
4670
		// bit: 1; mask 0x0002; edit scenarios
4671
		$bool = (0x0002 & $options) >> 1;
4672
		$this->_phpSheet->getProtection()->setScenarios(!$bool);
4673
 
4674
		// bit: 2; mask 0x0004; format cells
4675
		$bool = (0x0004 & $options) >> 2;
4676
		$this->_phpSheet->getProtection()->setFormatCells(!$bool);
4677
 
4678
		// bit: 3; mask 0x0008; format columns
4679
		$bool = (0x0008 & $options) >> 3;
4680
		$this->_phpSheet->getProtection()->setFormatColumns(!$bool);
4681
 
4682
		// bit: 4; mask 0x0010; format rows
4683
		$bool = (0x0010 & $options) >> 4;
4684
		$this->_phpSheet->getProtection()->setFormatRows(!$bool);
4685
 
4686
		// bit: 5; mask 0x0020; insert columns
4687
		$bool = (0x0020 & $options) >> 5;
4688
		$this->_phpSheet->getProtection()->setInsertColumns(!$bool);
4689
 
4690
		// bit: 6; mask 0x0040; insert rows
4691
		$bool = (0x0040 & $options) >> 6;
4692
		$this->_phpSheet->getProtection()->setInsertRows(!$bool);
4693
 
4694
		// bit: 7; mask 0x0080; insert hyperlinks
4695
		$bool = (0x0080 & $options) >> 7;
4696
		$this->_phpSheet->getProtection()->setInsertHyperlinks(!$bool);
4697
 
4698
		// bit: 8; mask 0x0100; delete columns
4699
		$bool = (0x0100 & $options) >> 8;
4700
		$this->_phpSheet->getProtection()->setDeleteColumns(!$bool);
4701
 
4702
		// bit: 9; mask 0x0200; delete rows
4703
		$bool = (0x0200 & $options) >> 9;
4704
		$this->_phpSheet->getProtection()->setDeleteRows(!$bool);
4705
 
4706
		// bit: 10; mask 0x0400; select locked cells
4707
		$bool = (0x0400 & $options) >> 10;
4708
		$this->_phpSheet->getProtection()->setSelectLockedCells(!$bool);
4709
 
4710
		// bit: 11; mask 0x0800; sort cell range
4711
		$bool = (0x0800 & $options) >> 11;
4712
		$this->_phpSheet->getProtection()->setSort(!$bool);
4713
 
4714
		// bit: 12; mask 0x1000; auto filter
4715
		$bool = (0x1000 & $options) >> 12;
4716
		$this->_phpSheet->getProtection()->setAutoFilter(!$bool);
4717
 
4718
		// bit: 13; mask 0x2000; pivot tables
4719
		$bool = (0x2000 & $options) >> 13;
4720
		$this->_phpSheet->getProtection()->setPivotTables(!$bool);
4721
 
4722
		// bit: 14; mask 0x4000; select unlocked cells
4723
		$bool = (0x4000 & $options) >> 14;
4724
		$this->_phpSheet->getProtection()->setSelectUnlockedCells(!$bool);
4725
 
4726
		// offset: 21; size: 2; not used
4727
	}
4728
 
4729
 
4730
	/**
4731
	 * Read RANGEPROTECTION record
4732
	 * Reading of this record is based on Microsoft Office Excel 97-2000 Binary File Format Specification,
4733
	 * where it is referred to as FEAT record
4734
	 */
4735
	private function _readRangeProtection()
4736
	{
4737
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
4738
		$recordData = substr($this->_data, $this->_pos + 4, $length);
4739
 
4740
		// move stream pointer to next record
4741
		$this->_pos += 4 + $length;
4742
 
4743
		// local pointer in record data
4744
		$offset = 0;
4745
 
4746
		if (!$this->_readDataOnly) {
4747
			$offset += 12;
4748
 
4749
			// offset: 12; size: 2; shared feature type, 2 = enhanced protection, 4 = smart tag
4750
			$isf = self::_GetInt2d($recordData, 12);
4751
			if ($isf != 2) {
4752
				// we only read FEAT records of type 2
4753
				return;
4754
			}
4755
			$offset += 2;
4756
 
4757
			$offset += 5;
4758
 
4759
			// offset: 19; size: 2; count of ref ranges this feature is on
4760
			$cref = self::_GetInt2d($recordData, 19);
4761
			$offset += 2;
4762
 
4763
			$offset += 6;
4764
 
4765
			// offset: 27; size: 8 * $cref; list of cell ranges (like in hyperlink record)
4766
			$cellRanges = array();
4767
			for ($i = 0; $i < $cref; ++$i) {
4768
				try {
4769
					$cellRange = $this->_readBIFF8CellRangeAddressFixed(substr($recordData, 27 + 8 * $i, 8));
4770
				} catch (PHPExcel_Exception $e) {
4771
					return;
4772
				}
4773
				$cellRanges[] = $cellRange;
4774
				$offset += 8;
4775
			}
4776
 
4777
			// offset: var; size: var; variable length of feature specific data
4778
			$rgbFeat = substr($recordData, $offset);
4779
			$offset += 4;
4780
 
4781
			// offset: var; size: 4; the encrypted password (only 16-bit although field is 32-bit)
4782
			$wPassword = self::_GetInt4d($recordData, $offset);
4783
			$offset += 4;
4784
 
4785
			// Apply range protection to sheet
4786
			if ($cellRanges) {
4787
				$this->_phpSheet->protectCells(implode(' ', $cellRanges), strtoupper(dechex($wPassword)), true);
4788
			}
4789
		}
4790
	}
4791
 
4792
 
4793
	/**
4794
	 * Read IMDATA record
4795
	 */
4796
	private function _readImData()
4797
	{
4798
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
4799
 
4800
		// get spliced record data
4801
		$splicedRecordData = $this->_getSplicedRecordData();
4802
		$recordData = $splicedRecordData['recordData'];
4803
 
4804
		// UNDER CONSTRUCTION
4805
 
4806
		// offset: 0; size: 2; image format
4807
		$cf = self::_GetInt2d($recordData, 0);
4808
 
4809
		// offset: 2; size: 2; environment from which the file was written
4810
		$env = self::_GetInt2d($recordData, 2);
4811
 
4812
		// offset: 4; size: 4; length of the image data
4813
		$lcb = self::_GetInt4d($recordData, 4);
4814
 
4815
		// offset: 8; size: var; image data
4816
		$iData = substr($recordData, 8);
4817
 
4818
		switch ($cf) {
4819
		case 0x09: // Windows bitmap format
4820
			// BITMAPCOREINFO
4821
			// 1. BITMAPCOREHEADER
4822
			// offset: 0; size: 4; bcSize, Specifies the number of bytes required by the structure
4823
			$bcSize = self::_GetInt4d($iData, 0);
4824
//			var_dump($bcSize);
4825
 
4826
			// offset: 4; size: 2; bcWidth, specifies the width of the bitmap, in pixels
4827
			$bcWidth = self::_GetInt2d($iData, 4);
4828
//			var_dump($bcWidth);
4829
 
4830
			// offset: 6; size: 2; bcHeight, specifies the height of the bitmap, in pixels.
4831
			$bcHeight = self::_GetInt2d($iData, 6);
4832
//			var_dump($bcHeight);
4833
			$ih = imagecreatetruecolor($bcWidth, $bcHeight);
4834
 
4835
			// offset: 8; size: 2; bcPlanes, specifies the number of planes for the target device. This value must be 1
4836
 
4837
			// offset: 10; size: 2; bcBitCount specifies the number of bits-per-pixel. This value must be 1, 4, 8, or 24
4838
			$bcBitCount = self::_GetInt2d($iData, 10);
4839
//			var_dump($bcBitCount);
4840
 
4841
			$rgbString = substr($iData, 12);
4842
			$rgbTriples = array();
4843
			while (strlen($rgbString) > 0) {
4844
				$rgbTriples[] = unpack('Cb/Cg/Cr', $rgbString);
4845
				$rgbString = substr($rgbString, 3);
4846
			}
4847
			$x = 0;
4848
			$y = 0;
4849
			foreach ($rgbTriples as $i => $rgbTriple) {
4850
				$color = imagecolorallocate($ih, $rgbTriple['r'], $rgbTriple['g'], $rgbTriple['b']);
4851
				imagesetpixel($ih, $x, $bcHeight - 1 - $y, $color);
4852
				$x = ($x + 1) % $bcWidth;
4853
				$y = $y + floor(($x + 1) / $bcWidth);
4854
			}
4855
			//imagepng($ih, 'image.png');
4856
 
4857
			$drawing = new PHPExcel_Worksheet_Drawing();
4858
			$drawing->setPath($filename);
4859
			$drawing->setWorksheet($this->_phpSheet);
4860
 
4861
			break;
4862
 
4863
		case 0x02: // Windows metafile or Macintosh PICT format
4864
		case 0x0e: // native format
4865
		default;
4866
			break;
4867
 
4868
		}
4869
 
4870
		// _getSplicedRecordData() takes care of moving current position in data stream
4871
	}
4872
 
4873
 
4874
	/**
4875
	 * Read a free CONTINUE record. Free CONTINUE record may be a camouflaged MSODRAWING record
4876
	 * When MSODRAWING data on a sheet exceeds 8224 bytes, CONTINUE records are used instead. Undocumented.
4877
	 * In this case, we must treat the CONTINUE record as a MSODRAWING record
4878
	 */
4879
	private function _readContinue()
4880
	{
4881
		$length = self::_GetInt2d($this->_data, $this->_pos + 2);
4882
		$recordData = substr($this->_data, $this->_pos + 4, $length);
4883
 
4884
		// check if we are reading drawing data
4885
		// this is in case a free CONTINUE record occurs in other circumstances we are unaware of
4886
		if ($this->_drawingData == '') {
4887
			// move stream pointer to next record
4888
			$this->_pos += 4 + $length;
4889
 
4890
			return;
4891
		}
4892
 
4893
		// check if record data is at least 4 bytes long, otherwise there is no chance this is MSODRAWING data
4894
		if ($length < 4) {
4895
			// move stream pointer to next record
4896
			$this->_pos += 4 + $length;
4897
 
4898
			return;
4899
		}
4900
 
4901
		// dirty check to see if CONTINUE record could be a camouflaged MSODRAWING record
4902
		// look inside CONTINUE record to see if it looks like a part of an Escher stream
4903
		// we know that Escher stream may be split at least at
4904
		//		0xF003 MsofbtSpgrContainer
4905
		//		0xF004 MsofbtSpContainer
4906
		//		0xF00D MsofbtClientTextbox
4907
		$validSplitPoints = array(0xF003, 0xF004, 0xF00D); // add identifiers if we find more
4908
 
4909
		$splitPoint = self::_GetInt2d($recordData, 2);
4910
		if (in_array($splitPoint, $validSplitPoints)) {
4911
			// get spliced record data (and move pointer to next record)
4912
			$splicedRecordData = $this->_getSplicedRecordData();
4913
			$this->_drawingData .= $splicedRecordData['recordData'];
4914
 
4915
			return;
4916
		}
4917
 
4918
		// move stream pointer to next record
4919
		$this->_pos += 4 + $length;
4920
 
4921
	}
4922
 
4923
 
4924
	/**
4925
	 * Reads a record from current position in data stream and continues reading data as long as CONTINUE
4926
	 * records are found. Splices the record data pieces and returns the combined string as if record data
4927
	 * is in one piece.
4928
	 * Moves to next current position in data stream to start of next record different from a CONtINUE record
4929
	 *
4930
	 * @return array
4931
	 */
4932
	private function _getSplicedRecordData()
4933
	{
4934
		$data = '';
4935
		$spliceOffsets = array();
4936
 
4937
		$i = 0;
4938
		$spliceOffsets[0] = 0;
4939
 
4940
		do {
4941
			++$i;
4942
 
4943
			// offset: 0; size: 2; identifier
4944
			$identifier = self::_GetInt2d($this->_data, $this->_pos);
4945
			// offset: 2; size: 2; length
4946
			$length = self::_GetInt2d($this->_data, $this->_pos + 2);
4947
			$data .= substr($this->_data, $this->_pos + 4, $length);
4948
 
4949
			$spliceOffsets[$i] = $spliceOffsets[$i - 1] + $length;
4950
 
4951
			$this->_pos += 4 + $length;
4952
			$nextIdentifier = self::_GetInt2d($this->_data, $this->_pos);
4953
		}
4954
		while ($nextIdentifier == self::XLS_Type_CONTINUE);
4955
 
4956
		$splicedData = array(
4957
			'recordData' => $data,
4958
			'spliceOffsets' => $spliceOffsets,
4959
		);
4960
 
4961
		return $splicedData;
4962
 
4963
	}
4964
 
4965
 
4966
	/**
4967
	 * Convert formula structure into human readable Excel formula like 'A3+A5*5'
4968
	 *
4969
	 * @param string $formulaStructure The complete binary data for the formula
4970
	 * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
4971
	 * @return string Human readable formula
4972
	 */
4973
	private function _getFormulaFromStructure($formulaStructure, $baseCell = 'A1')
4974
	{
4975
		// offset: 0; size: 2; size of the following formula data
4976
		$sz = self::_GetInt2d($formulaStructure, 0);
4977
 
4978
		// offset: 2; size: sz
4979
		$formulaData = substr($formulaStructure, 2, $sz);
4980
 
4981
		// for debug: dump the formula data
4982
		//echo '<xmp>';
4983
		//echo 'size: ' . $sz . "\n";
4984
		//echo 'the entire formula data: ';
4985
		//Debug::dump($formulaData);
4986
		//echo "\n----\n";
4987
 
4988
		// offset: 2 + sz; size: variable (optional)
4989
		if (strlen($formulaStructure) > 2 + $sz) {
4990
			$additionalData = substr($formulaStructure, 2 + $sz);
4991
 
4992
			// for debug: dump the additional data
4993
			//echo 'the entire additional data: ';
4994
			//Debug::dump($additionalData);
4995
			//echo "\n----\n";
4996
 
4997
		} else {
4998
			$additionalData = '';
4999
		}
5000
 
5001
		return $this->_getFormulaFromData($formulaData, $additionalData, $baseCell);
5002
	}
5003
 
5004
 
5005
	/**
5006
	 * Take formula data and additional data for formula and return human readable formula
5007
	 *
5008
	 * @param string $formulaData The binary data for the formula itself
5009
	 * @param string $additionalData Additional binary data going with the formula
5010
	 * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
5011
	 * @return string Human readable formula
5012
	 */
5013
	private function _getFormulaFromData($formulaData,  $additionalData = '', $baseCell = 'A1')
5014
	{
5015
		// start parsing the formula data
5016
		$tokens = array();
5017
 
5018
		while (strlen($formulaData) > 0 and $token = $this->_getNextToken($formulaData, $baseCell)) {
5019
			$tokens[] = $token;
5020
			$formulaData = substr($formulaData, $token['size']);
5021
 
5022
			// for debug: dump the token
5023
			//var_dump($token);
5024
		}
5025
 
5026
		$formulaString = $this->_createFormulaFromTokens($tokens, $additionalData);
5027
 
5028
		return $formulaString;
5029
	}
5030
 
5031
 
5032
	/**
5033
	 * Take array of tokens together with additional data for formula and return human readable formula
5034
	 *
5035
	 * @param array $tokens
5036
	 * @param array $additionalData Additional binary data going with the formula
5037
	 * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
5038
	 * @return string Human readable formula
5039
	 */
5040
	private function _createFormulaFromTokens($tokens, $additionalData)
5041
	{
5042
		// empty formula?
5043
		if (empty($tokens)) {
5044
			return '';
5045
		}
5046
 
5047
		$formulaStrings = array();
5048
		foreach ($tokens as $token) {
5049
			// initialize spaces
5050
			$space0 = isset($space0) ? $space0 : ''; // spaces before next token, not tParen
5051
			$space1 = isset($space1) ? $space1 : ''; // carriage returns before next token, not tParen
5052
			$space2 = isset($space2) ? $space2 : ''; // spaces before opening parenthesis
5053
			$space3 = isset($space3) ? $space3 : ''; // carriage returns before opening parenthesis
5054
			$space4 = isset($space4) ? $space4 : ''; // spaces before closing parenthesis
5055
			$space5 = isset($space5) ? $space5 : ''; // carriage returns before closing parenthesis
5056
 
5057
			switch ($token['name']) {
5058
			case 'tAdd': // addition
5059
			case 'tConcat': // addition
5060
			case 'tDiv': // division
5061
			case 'tEQ': // equality
5062
			case 'tGE': // greater than or equal
5063
			case 'tGT': // greater than
5064
			case 'tIsect': // intersection
5065
			case 'tLE': // less than or equal
5066
			case 'tList': // less than or equal
5067
			case 'tLT': // less than
5068
			case 'tMul': // multiplication
5069
			case 'tNE': // multiplication
5070
			case 'tPower': // power
5071
			case 'tRange': // range
5072
			case 'tSub': // subtraction
5073
				$op2 = array_pop($formulaStrings);
5074
				$op1 = array_pop($formulaStrings);
5075
				$formulaStrings[] = "$op1$space1$space0{$token['data']}$op2";
5076
				unset($space0, $space1);
5077
				break;
5078
			case 'tUplus': // unary plus
5079
			case 'tUminus': // unary minus
5080
				$op = array_pop($formulaStrings);
5081
				$formulaStrings[] = "$space1$space0{$token['data']}$op";
5082
				unset($space0, $space1);
5083
				break;
5084
			case 'tPercent': // percent sign
5085
				$op = array_pop($formulaStrings);
5086
				$formulaStrings[] = "$op$space1$space0{$token['data']}";
5087
				unset($space0, $space1);
5088
				break;
5089
			case 'tAttrVolatile': // indicates volatile function
5090
			case 'tAttrIf':
5091
			case 'tAttrSkip':
5092
			case 'tAttrChoose':
5093
				// token is only important for Excel formula evaluator
5094
				// do nothing
5095
				break;
5096
			case 'tAttrSpace': // space / carriage return
5097
				// space will be used when next token arrives, do not alter formulaString stack
5098
				switch ($token['data']['spacetype']) {
5099
				case 'type0':
5100
					$space0 = str_repeat(' ', $token['data']['spacecount']);
5101
					break;
5102
				case 'type1':
5103
					$space1 = str_repeat("\n", $token['data']['spacecount']);
5104
					break;
5105
				case 'type2':
5106
					$space2 = str_repeat(' ', $token['data']['spacecount']);
5107
					break;
5108
				case 'type3':
5109
					$space3 = str_repeat("\n", $token['data']['spacecount']);
5110
					break;
5111
				case 'type4':
5112
					$space4 = str_repeat(' ', $token['data']['spacecount']);
5113
					break;
5114
				case 'type5':
5115
					$space5 = str_repeat("\n", $token['data']['spacecount']);
5116
					break;
5117
				}
5118
				break;
5119
			case 'tAttrSum': // SUM function with one parameter
5120
				$op = array_pop($formulaStrings);
5121
				$formulaStrings[] = "{$space1}{$space0}SUM($op)";
5122
				unset($space0, $space1);
5123
				break;
5124
			case 'tFunc': // function with fixed number of arguments
5125
			case 'tFuncV': // function with variable number of arguments
5126
				if ($token['data']['function'] != '') {
5127
					// normal function
5128
					$ops = array(); // array of operators
5129
					for ($i = 0; $i < $token['data']['args']; ++$i) {
5130
						$ops[] = array_pop($formulaStrings);
5131
					}
5132
					$ops = array_reverse($ops);
5133
					$formulaStrings[] = "$space1$space0{$token['data']['function']}(" . implode(',', $ops) . ")";
5134
					unset($space0, $space1);
5135
				} else {
5136
					// add-in function
5137
					$ops = array(); // array of operators
5138
					for ($i = 0; $i < $token['data']['args'] - 1; ++$i) {
5139
						$ops[] = array_pop($formulaStrings);
5140
					}
5141
					$ops = array_reverse($ops);
5142
					$function = array_pop($formulaStrings);
5143
					$formulaStrings[] = "$space1$space0$function(" . implode(',', $ops) . ")";
5144
					unset($space0, $space1);
5145
				}
5146
				break;
5147
			case 'tParen': // parenthesis
5148
				$expression = array_pop($formulaStrings);
5149
				$formulaStrings[] = "$space3$space2($expression$space5$space4)";
5150
				unset($space2, $space3, $space4, $space5);
5151
				break;
5152
			case 'tArray': // array constant
5153
				$constantArray = self::_readBIFF8ConstantArray($additionalData);
5154
				$formulaStrings[] = $space1 . $space0 . $constantArray['value'];
5155
				$additionalData = substr($additionalData, $constantArray['size']); // bite of chunk of additional data
5156
				unset($space0, $space1);
5157
				break;
5158
			case 'tMemArea':
5159
				// bite off chunk of additional data
5160
				$cellRangeAddressList = $this->_readBIFF8CellRangeAddressList($additionalData);
5161
				$additionalData = substr($additionalData, $cellRangeAddressList['size']);
5162
				$formulaStrings[] = "$space1$space0{$token['data']}";
5163
				unset($space0, $space1);
5164
				break;
5165
			case 'tArea': // cell range address
5166
			case 'tBool': // boolean
5167
			case 'tErr': // error code
5168
			case 'tInt': // integer
5169
			case 'tMemErr':
5170
			case 'tMemFunc':
5171
			case 'tMissArg':
5172
			case 'tName':
5173
			case 'tNameX':
5174
			case 'tNum': // number
5175
			case 'tRef': // single cell reference
5176
			case 'tRef3d': // 3d cell reference
5177
			case 'tArea3d': // 3d cell range reference
5178
			case 'tRefN':
5179
			case 'tAreaN':
5180
			case 'tStr': // string
5181
				$formulaStrings[] = "$space1$space0{$token['data']}";
5182
				unset($space0, $space1);
5183
				break;
5184
			}
5185
		}
5186
		$formulaString = $formulaStrings[0];
5187
 
5188
		// for debug: dump the human readable formula
5189
		//echo '----' . "\n";
5190
		//echo 'Formula: ' . $formulaString;
5191
 
5192
		return $formulaString;
5193
	}
5194
 
5195
 
5196
	/**
5197
	 * Fetch next token from binary formula data
5198
	 *
5199
	 * @param string Formula data
5200
	 * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
5201
	 * @return array
5202
	 * @throws PHPExcel_Reader_Exception
5203
	 */
5204
	private function _getNextToken($formulaData, $baseCell = 'A1')
5205
	{
5206
		// offset: 0; size: 1; token id
5207
		$id = ord($formulaData[0]); // token id
5208
		$name = false; // initialize token name
5209
 
5210
		switch ($id) {
5211
		case 0x03: $name = 'tAdd';		$size = 1;	$data = '+';	break;
5212
		case 0x04: $name = 'tSub';		$size = 1;	$data = '-';	break;
5213
		case 0x05: $name = 'tMul';		$size = 1;	$data = '*';	break;
5214
		case 0x06: $name = 'tDiv';		$size = 1;	$data = '/';	break;
5215
		case 0x07: $name = 'tPower';	$size = 1;	$data = '^';	break;
5216
		case 0x08: $name = 'tConcat';	$size = 1;	$data = '&';	break;
5217
		case 0x09: $name = 'tLT';		$size = 1;	$data = '<';	break;
5218
		case 0x0A: $name = 'tLE';		$size = 1;	$data = '<=';	break;
5219
		case 0x0B: $name = 'tEQ';		$size = 1;	$data = '=';	break;
5220
		case 0x0C: $name = 'tGE';		$size = 1;	$data = '>=';	break;
5221
		case 0x0D: $name = 'tGT';		$size = 1;	$data = '>';	break;
5222
		case 0x0E: $name = 'tNE';		$size = 1;	$data = '<>';	break;
5223
		case 0x0F: $name = 'tIsect';	$size = 1;	$data = ' ';	break;
5224
		case 0x10: $name = 'tList';		$size = 1;	$data = ',';	break;
5225
		case 0x11: $name = 'tRange';	$size = 1;	$data = ':';	break;
5226
		case 0x12: $name = 'tUplus';	$size = 1;	$data = '+';	break;
5227
		case 0x13: $name = 'tUminus';	$size = 1;	$data = '-';	break;
5228
		case 0x14: $name = 'tPercent';	$size = 1;	$data = '%';	break;
5229
		case 0x15:	//	parenthesis
5230
			$name  = 'tParen';
5231
			$size  = 1;
5232
			$data = null;
5233
			break;
5234
		case 0x16:	//	missing argument
5235
			$name = 'tMissArg';
5236
			$size = 1;
5237
			$data = '';
5238
			break;
5239
		case 0x17:	//	string
5240
			$name = 'tStr';
5241
			// offset: 1; size: var; Unicode string, 8-bit string length
5242
			$string = self::_readUnicodeStringShort(substr($formulaData, 1));
5243
			$size = 1 + $string['size'];
5244
			$data = self::_UTF8toExcelDoubleQuoted($string['value']);
5245
			break;
5246
		case 0x19:	//	Special attribute
5247
			// offset: 1; size: 1; attribute type flags:
5248
			switch (ord($formulaData[1])) {
5249
			case 0x01:
5250
				$name = 'tAttrVolatile';
5251
				$size = 4;
5252
				$data = null;
5253
				break;
5254
			case 0x02:
5255
				$name = 'tAttrIf';
5256
				$size = 4;
5257
				$data = null;
5258
				break;
5259
			case 0x04:
5260
				$name = 'tAttrChoose';
5261
				// offset: 2; size: 2; number of choices in the CHOOSE function ($nc, number of parameters decreased by 1)
5262
				$nc = self::_GetInt2d($formulaData, 2);
5263
				// offset: 4; size: 2 * $nc
5264
				// offset: 4 + 2 * $nc; size: 2
5265
				$size = 2 * $nc + 6;
5266
				$data = null;
5267
				break;
5268
			case 0x08:
5269
				$name = 'tAttrSkip';
5270
				$size = 4;
5271
				$data = null;
5272
				break;
5273
			case 0x10:
5274
				$name = 'tAttrSum';
5275
				$size = 4;
5276
				$data = null;
5277
				break;
5278
			case 0x40:
5279
			case 0x41:
5280
				$name = 'tAttrSpace';
5281
				$size = 4;
5282
				// offset: 2; size: 2; space type and position
5283
				switch (ord($formulaData[2])) {
5284
				case 0x00:
5285
					$spacetype = 'type0';
5286
					break;
5287
				case 0x01:
5288
					$spacetype = 'type1';
5289
					break;
5290
				case 0x02:
5291
					$spacetype = 'type2';
5292
					break;
5293
				case 0x03:
5294
					$spacetype = 'type3';
5295
					break;
5296
				case 0x04:
5297
					$spacetype = 'type4';
5298
					break;
5299
				case 0x05:
5300
					$spacetype = 'type5';
5301
					break;
5302
				default:
5303
					throw new PHPExcel_Reader_Exception('Unrecognized space type in tAttrSpace token');
5304
					break;
5305
				}
5306
				// offset: 3; size: 1; number of inserted spaces/carriage returns
5307
				$spacecount = ord($formulaData[3]);
5308
 
5309
				$data = array('spacetype' => $spacetype, 'spacecount' => $spacecount);
5310
				break;
5311
			default:
5312
				throw new PHPExcel_Reader_Exception('Unrecognized attribute flag in tAttr token');
5313
				break;
5314
			}
5315
			break;
5316
		case 0x1C:	//	error code
5317
			// offset: 1; size: 1; error code
5318
			$name = 'tErr';
5319
			$size = 2;
5320
			$data = self::_mapErrorCode(ord($formulaData[1]));
5321
			break;
5322
		case 0x1D:	//	boolean
5323
			// offset: 1; size: 1; 0 = false, 1 = true;
5324
			$name = 'tBool';
5325
			$size = 2;
5326
			$data = ord($formulaData[1]) ? 'TRUE' : 'FALSE';
5327
			break;
5328
		case 0x1E:	//	integer
5329
			// offset: 1; size: 2; unsigned 16-bit integer
5330
			$name = 'tInt';
5331
			$size = 3;
5332
			$data = self::_GetInt2d($formulaData, 1);
5333
			break;
5334
		case 0x1F:	//	number
5335
			// offset: 1; size: 8;
5336
			$name = 'tNum';
5337
			$size = 9;
5338
			$data = self::_extractNumber(substr($formulaData, 1));
5339
			$data = str_replace(',', '.', (string)$data); // in case non-English locale
5340
			break;
5341
		case 0x20:	//	array constant
5342
		case 0x40:
5343
		case 0x60:
5344
			// offset: 1; size: 7; not used
5345
			$name = 'tArray';
5346
			$size = 8;
5347
			$data = null;
5348
			break;
5349
		case 0x21:	//	function with fixed number of arguments
5350
		case 0x41:
5351
		case 0x61:
5352
			$name = 'tFunc';
5353
			$size = 3;
5354
			// offset: 1; size: 2; index to built-in sheet function
5355
			switch (self::_GetInt2d($formulaData, 1)) {
5356
			case   2: $function = 'ISNA'; 			$args = 1; 	break;
5357
			case   3: $function = 'ISERROR'; 		$args = 1; 	break;
5358
			case  10: $function = 'NA'; 			$args = 0; 	break;
5359
			case  15: $function = 'SIN'; 			$args = 1; 	break;
5360
			case  16: $function = 'COS'; 			$args = 1; 	break;
5361
			case  17: $function = 'TAN'; 			$args = 1; 	break;
5362
			case  18: $function = 'ATAN'; 			$args = 1; 	break;
5363
			case  19: $function = 'PI'; 			$args = 0; 	break;
5364
			case  20: $function = 'SQRT'; 			$args = 1; 	break;
5365
			case  21: $function = 'EXP'; 			$args = 1; 	break;
5366
			case  22: $function = 'LN'; 			$args = 1; 	break;
5367
			case  23: $function = 'LOG10'; 			$args = 1; 	break;
5368
			case  24: $function = 'ABS'; 			$args = 1; 	break;
5369
			case  25: $function = 'INT'; 			$args = 1; 	break;
5370
			case  26: $function = 'SIGN'; 			$args = 1; 	break;
5371
			case  27: $function = 'ROUND'; 			$args = 2; 	break;
5372
			case  30: $function = 'REPT'; 			$args = 2; 	break;
5373
			case  31: $function = 'MID'; 			$args = 3; 	break;
5374
			case  32: $function = 'LEN'; 			$args = 1; 	break;
5375
			case  33: $function = 'VALUE'; 			$args = 1; 	break;
5376
			case  34: $function = 'TRUE'; 			$args = 0; 	break;
5377
			case  35: $function = 'FALSE'; 			$args = 0; 	break;
5378
			case  38: $function = 'NOT'; 			$args = 1; 	break;
5379
			case  39: $function = 'MOD'; 			$args = 2;	break;
5380
			case  40: $function = 'DCOUNT'; 		$args = 3;	break;
5381
			case  41: $function = 'DSUM'; 			$args = 3;	break;
5382
			case  42: $function = 'DAVERAGE'; 		$args = 3;	break;
5383
			case  43: $function = 'DMIN'; 			$args = 3;	break;
5384
			case  44: $function = 'DMAX'; 			$args = 3;	break;
5385
			case  45: $function = 'DSTDEV'; 		$args = 3;	break;
5386
			case  48: $function = 'TEXT'; 			$args = 2;	break;
5387
			case  61: $function = 'MIRR'; 			$args = 3;	break;
5388
			case  63: $function = 'RAND'; 			$args = 0;	break;
5389
			case  65: $function = 'DATE'; 			$args = 3;	break;
5390
			case  66: $function = 'TIME'; 			$args = 3;	break;
5391
			case  67: $function = 'DAY'; 			$args = 1;	break;
5392
			case  68: $function = 'MONTH'; 			$args = 1;	break;
5393
			case  69: $function = 'YEAR'; 			$args = 1;	break;
5394
			case  71: $function = 'HOUR'; 			$args = 1;	break;
5395
			case  72: $function = 'MINUTE'; 		$args = 1;	break;
5396
			case  73: $function = 'SECOND'; 		$args = 1;	break;
5397
			case  74: $function = 'NOW'; 			$args = 0;	break;
5398
			case  75: $function = 'AREAS'; 			$args = 1;	break;
5399
			case  76: $function = 'ROWS'; 			$args = 1;	break;
5400
			case  77: $function = 'COLUMNS'; 		$args = 1;	break;
5401
			case  83: $function = 'TRANSPOSE'; 		$args = 1;	break;
5402
			case  86: $function = 'TYPE'; 			$args = 1;	break;
5403
			case  97: $function = 'ATAN2'; 			$args = 2;	break;
5404
			case  98: $function = 'ASIN'; 			$args = 1;	break;
5405
			case  99: $function = 'ACOS'; 			$args = 1;	break;
5406
			case 105: $function = 'ISREF'; 			$args = 1;	break;
5407
			case 111: $function = 'CHAR'; 			$args = 1;	break;
5408
			case 112: $function = 'LOWER'; 			$args = 1;	break;
5409
			case 113: $function = 'UPPER'; 			$args = 1;	break;
5410
			case 114: $function = 'PROPER'; 		$args = 1;	break;
5411
			case 117: $function = 'EXACT'; 			$args = 2;	break;
5412
			case 118: $function = 'TRIM'; 			$args = 1;	break;
5413
			case 119: $function = 'REPLACE'; 		$args = 4;	break;
5414
			case 121: $function = 'CODE'; 			$args = 1;	break;
5415
			case 126: $function = 'ISERR'; 			$args = 1;	break;
5416
			case 127: $function = 'ISTEXT'; 		$args = 1;	break;
5417
			case 128: $function = 'ISNUMBER'; 		$args = 1;	break;
5418
			case 129: $function = 'ISBLANK'; 		$args = 1;	break;
5419
			case 130: $function = 'T'; 				$args = 1;	break;
5420
			case 131: $function = 'N'; 				$args = 1;	break;
5421
			case 140: $function = 'DATEVALUE'; 		$args = 1;	break;
5422
			case 141: $function = 'TIMEVALUE'; 		$args = 1;	break;
5423
			case 142: $function = 'SLN'; 			$args = 3;	break;
5424
			case 143: $function = 'SYD'; 			$args = 4;	break;
5425
			case 162: $function = 'CLEAN'; 			$args = 1;	break;
5426
			case 163: $function = 'MDETERM'; 		$args = 1;	break;
5427
			case 164: $function = 'MINVERSE'; 		$args = 1;	break;
5428
			case 165: $function = 'MMULT'; 			$args = 2;	break;
5429
			case 184: $function = 'FACT'; 			$args = 1;	break;
5430
			case 189: $function = 'DPRODUCT'; 		$args = 3;	break;
5431
			case 190: $function = 'ISNONTEXT'; 		$args = 1;	break;
5432
			case 195: $function = 'DSTDEVP'; 		$args = 3;	break;
5433
			case 196: $function = 'DVARP'; 			$args = 3;	break;
5434
			case 198: $function = 'ISLOGICAL'; 		$args = 1;	break;
5435
			case 199: $function = 'DCOUNTA'; 		$args = 3;	break;
5436
			case 207: $function = 'REPLACEB'; 		$args = 4;	break;
5437
			case 210: $function = 'MIDB'; 			$args = 3;	break;
5438
			case 211: $function = 'LENB'; 			$args = 1;	break;
5439
			case 212: $function = 'ROUNDUP'; 		$args = 2;	break;
5440
			case 213: $function = 'ROUNDDOWN'; 		$args = 2;	break;
5441
			case 214: $function = 'ASC'; 			$args = 1;	break;
5442
			case 215: $function = 'DBCS'; 			$args = 1;	break;
5443
			case 221: $function = 'TODAY'; 			$args = 0;	break;
5444
			case 229: $function = 'SINH'; 			$args = 1;	break;
5445
			case 230: $function = 'COSH'; 			$args = 1;	break;
5446
			case 231: $function = 'TANH'; 			$args = 1;	break;
5447
			case 232: $function = 'ASINH'; 			$args = 1;	break;
5448
			case 233: $function = 'ACOSH'; 			$args = 1;	break;
5449
			case 234: $function = 'ATANH'; 			$args = 1;	break;
5450
			case 235: $function = 'DGET'; 			$args = 3;	break;
5451
			case 244: $function = 'INFO'; 			$args = 1;	break;
5452
			case 252: $function = 'FREQUENCY'; 		$args = 2;	break;
5453
			case 261: $function = 'ERROR.TYPE'; 	$args = 1;	break;
5454
			case 271: $function = 'GAMMALN'; 		$args = 1;	break;
5455
			case 273: $function = 'BINOMDIST'; 		$args = 4;	break;
5456
			case 274: $function = 'CHIDIST'; 		$args = 2;	break;
5457
			case 275: $function = 'CHIINV'; 		$args = 2;	break;
5458
			case 276: $function = 'COMBIN'; 		$args = 2;	break;
5459
			case 277: $function = 'CONFIDENCE'; 	$args = 3;	break;
5460
			case 278: $function = 'CRITBINOM'; 		$args = 3;	break;
5461
			case 279: $function = 'EVEN'; 			$args = 1;	break;
5462
			case 280: $function = 'EXPONDIST'; 		$args = 3;	break;
5463
			case 281: $function = 'FDIST'; 			$args = 3;	break;
5464
			case 282: $function = 'FINV'; 			$args = 3;	break;
5465
			case 283: $function = 'FISHER'; 		$args = 1;	break;
5466
			case 284: $function = 'FISHERINV'; 		$args = 1;	break;
5467
			case 285: $function = 'FLOOR'; 			$args = 2;	break;
5468
			case 286: $function = 'GAMMADIST'; 		$args = 4;	break;
5469
			case 287: $function = 'GAMMAINV'; 		$args = 3;	break;
5470
			case 288: $function = 'CEILING'; 		$args = 2;	break;
5471
			case 289: $function = 'HYPGEOMDIST';	$args = 4;	break;
5472
			case 290: $function = 'LOGNORMDIST';	$args = 3;	break;
5473
			case 291: $function = 'LOGINV';			$args = 3;	break;
5474
			case 292: $function = 'NEGBINOMDIST';	$args = 3;	break;
5475
			case 293: $function = 'NORMDIST';		$args = 4;	break;
5476
			case 294: $function = 'NORMSDIST';		$args = 1;	break;
5477
			case 295: $function = 'NORMINV';		$args = 3;	break;
5478
			case 296: $function = 'NORMSINV';		$args = 1;	break;
5479
			case 297: $function = 'STANDARDIZE';	$args = 3;	break;
5480
			case 298: $function = 'ODD';			$args = 1;	break;
5481
			case 299: $function = 'PERMUT';			$args = 2;	break;
5482
			case 300: $function = 'POISSON';		$args = 3;	break;
5483
			case 301: $function = 'TDIST';			$args = 3;	break;
5484
			case 302: $function = 'WEIBULL';		$args = 4;	break;
5485
			case 303: $function = 'SUMXMY2';		$args = 2;	break;
5486
			case 304: $function = 'SUMX2MY2';		$args = 2;	break;
5487
			case 305: $function = 'SUMX2PY2';		$args = 2;	break;
5488
			case 306: $function = 'CHITEST';		$args = 2;	break;
5489
			case 307: $function = 'CORREL';			$args = 2;	break;
5490
			case 308: $function = 'COVAR';			$args = 2;	break;
5491
			case 309: $function = 'FORECAST';		$args = 3;	break;
5492
			case 310: $function = 'FTEST';			$args = 2;	break;
5493
			case 311: $function = 'INTERCEPT';		$args = 2;	break;
5494
			case 312: $function = 'PEARSON';		$args = 2;	break;
5495
			case 313: $function = 'RSQ';			$args = 2;	break;
5496
			case 314: $function = 'STEYX';			$args = 2;	break;
5497
			case 315: $function = 'SLOPE';			$args = 2;	break;
5498
			case 316: $function = 'TTEST';			$args = 4;	break;
5499
			case 325: $function = 'LARGE';			$args = 2;	break;
5500
			case 326: $function = 'SMALL';			$args = 2;	break;
5501
			case 327: $function = 'QUARTILE';		$args = 2;	break;
5502
			case 328: $function = 'PERCENTILE';		$args = 2;	break;
5503
			case 331: $function = 'TRIMMEAN';		$args = 2;	break;
5504
			case 332: $function = 'TINV';			$args = 2;	break;
5505
			case 337: $function = 'POWER';			$args = 2;	break;
5506
			case 342: $function = 'RADIANS';		$args = 1;	break;
5507
			case 343: $function = 'DEGREES';		$args = 1;	break;
5508
			case 346: $function = 'COUNTIF';		$args = 2;	break;
5509
			case 347: $function = 'COUNTBLANK';		$args = 1;	break;
5510
			case 350: $function = 'ISPMT';			$args = 4;	break;
5511
			case 351: $function = 'DATEDIF';		$args = 3;	break;
5512
			case 352: $function = 'DATESTRING';		$args = 1;	break;
5513
			case 353: $function = 'NUMBERSTRING';	$args = 2;	break;
5514
			case 360: $function = 'PHONETIC';		$args = 1;	break;
5515
			case 368: $function = 'BAHTTEXT';		$args = 1;	break;
5516
			default:
5517
				throw new PHPExcel_Reader_Exception('Unrecognized function in formula');
5518
				break;
5519
			}
5520
			$data = array('function' => $function, 'args' => $args);
5521
			break;
5522
		case 0x22:	//	function with variable number of arguments
5523
		case 0x42:
5524
		case 0x62:
5525
			$name = 'tFuncV';
5526
			$size = 4;
5527
			// offset: 1; size: 1; number of arguments
5528
			$args = ord($formulaData[1]);
5529
			// offset: 2: size: 2; index to built-in sheet function
5530
			$index = self::_GetInt2d($formulaData, 2);
5531
			switch ($index) {
5532
			case   0: $function = 'COUNT';			break;
5533
			case   1: $function = 'IF';				break;
5534
			case   4: $function = 'SUM';			break;
5535
			case   5: $function = 'AVERAGE';		break;
5536
			case   6: $function = 'MIN';			break;
5537
			case   7: $function = 'MAX';			break;
5538
			case   8: $function = 'ROW';			break;
5539
			case   9: $function = 'COLUMN';			break;
5540
			case  11: $function = 'NPV';			break;
5541
			case  12: $function = 'STDEV';			break;
5542
			case  13: $function = 'DOLLAR';			break;
5543
			case  14: $function = 'FIXED';			break;
5544
			case  28: $function = 'LOOKUP';			break;
5545
			case  29: $function = 'INDEX';			break;
5546
			case  36: $function = 'AND';			break;
5547
			case  37: $function = 'OR';				break;
5548
			case  46: $function = 'VAR';			break;
5549
			case  49: $function = 'LINEST';			break;
5550
			case  50: $function = 'TREND';			break;
5551
			case  51: $function = 'LOGEST';			break;
5552
			case  52: $function = 'GROWTH';			break;
5553
			case  56: $function = 'PV';				break;
5554
			case  57: $function = 'FV';				break;
5555
			case  58: $function = 'NPER';			break;
5556
			case  59: $function = 'PMT';			break;
5557
			case  60: $function = 'RATE';			break;
5558
			case  62: $function = 'IRR';			break;
5559
			case  64: $function = 'MATCH';			break;
5560
			case  70: $function = 'WEEKDAY';		break;
5561
			case  78: $function = 'OFFSET';			break;
5562
			case  82: $function = 'SEARCH';			break;
5563
			case 100: $function = 'CHOOSE';			break;
5564
			case 101: $function = 'HLOOKUP';		break;
5565
			case 102: $function = 'VLOOKUP';		break;
5566
			case 109: $function = 'LOG';			break;
5567
			case 115: $function = 'LEFT';			break;
5568
			case 116: $function = 'RIGHT';			break;
5569
			case 120: $function = 'SUBSTITUTE';		break;
5570
			case 124: $function = 'FIND';			break;
5571
			case 125: $function = 'CELL';			break;
5572
			case 144: $function = 'DDB';			break;
5573
			case 148: $function = 'INDIRECT';		break;
5574
			case 167: $function = 'IPMT';			break;
5575
			case 168: $function = 'PPMT';			break;
5576
			case 169: $function = 'COUNTA';			break;
5577
			case 183: $function = 'PRODUCT';		break;
5578
			case 193: $function = 'STDEVP';			break;
5579
			case 194: $function = 'VARP';			break;
5580
			case 197: $function = 'TRUNC';			break;
5581
			case 204: $function = 'USDOLLAR';		break;
5582
			case 205: $function = 'FINDB';			break;
5583
			case 206: $function = 'SEARCHB';		break;
5584
			case 208: $function = 'LEFTB';			break;
5585
			case 209: $function = 'RIGHTB';			break;
5586
			case 216: $function = 'RANK';			break;
5587
			case 219: $function = 'ADDRESS';		break;
5588
			case 220: $function = 'DAYS360';		break;
5589
			case 222: $function = 'VDB';			break;
5590
			case 227: $function = 'MEDIAN';			break;
5591
			case 228: $function = 'SUMPRODUCT';		break;
5592
			case 247: $function = 'DB';				break;
5593
			case 255: $function = '';				break;
5594
			case 269: $function = 'AVEDEV';			break;
5595
			case 270: $function = 'BETADIST';		break;
5596
			case 272: $function = 'BETAINV';		break;
5597
			case 317: $function = 'PROB';			break;
5598
			case 318: $function = 'DEVSQ';			break;
5599
			case 319: $function = 'GEOMEAN';		break;
5600
			case 320: $function = 'HARMEAN';		break;
5601
			case 321: $function = 'SUMSQ';			break;
5602
			case 322: $function = 'KURT';			break;
5603
			case 323: $function = 'SKEW';			break;
5604
			case 324: $function = 'ZTEST';			break;
5605
			case 329: $function = 'PERCENTRANK';	break;
5606
			case 330: $function = 'MODE';			break;
5607
			case 336: $function = 'CONCATENATE';	break;
5608
			case 344: $function = 'SUBTOTAL';		break;
5609
			case 345: $function = 'SUMIF';			break;
5610
			case 354: $function = 'ROMAN';			break;
5611
			case 358: $function = 'GETPIVOTDATA';	break;
5612
			case 359: $function = 'HYPERLINK';		break;
5613
			case 361: $function = 'AVERAGEA';		break;
5614
			case 362: $function = 'MAXA';			break;
5615
			case 363: $function = 'MINA';			break;
5616
			case 364: $function = 'STDEVPA';		break;
5617
			case 365: $function = 'VARPA';			break;
5618
			case 366: $function = 'STDEVA';			break;
5619
			case 367: $function = 'VARA';			break;
5620
			default:
5621
				throw new PHPExcel_Reader_Exception('Unrecognized function in formula');
5622
				break;
5623
			}
5624
			$data = array('function' => $function, 'args' => $args);
5625
			break;
5626
		case 0x23:	//	index to defined name
5627
		case 0x43:
5628
		case 0x63:
5629
			$name = 'tName';
5630
			$size = 5;
5631
			// offset: 1; size: 2; one-based index to definedname record
5632
			$definedNameIndex = self::_GetInt2d($formulaData, 1) - 1;
5633
			// offset: 2; size: 2; not used
5634
			$data = $this->_definedname[$definedNameIndex]['name'];
5635
			break;
5636
		case 0x24:	//	single cell reference e.g. A5
5637
		case 0x44:
5638
		case 0x64:
5639
			$name = 'tRef';
5640
			$size = 5;
5641
			$data = $this->_readBIFF8CellAddress(substr($formulaData, 1, 4));
5642
			break;
5643
		case 0x25:	//	cell range reference to cells in the same sheet (2d)
5644
		case 0x45:
5645
		case 0x65:
5646
			$name = 'tArea';
5647
			$size = 9;
5648
			$data = $this->_readBIFF8CellRangeAddress(substr($formulaData, 1, 8));
5649
			break;
5650
		case 0x26:	//	Constant reference sub-expression
5651
		case 0x46:
5652
		case 0x66:
5653
			$name = 'tMemArea';
5654
			// offset: 1; size: 4; not used
5655
			// offset: 5; size: 2; size of the following subexpression
5656
			$subSize = self::_GetInt2d($formulaData, 5);
5657
			$size = 7 + $subSize;
5658
			$data = $this->_getFormulaFromData(substr($formulaData, 7, $subSize));
5659
			break;
5660
		case 0x27:	//	Deleted constant reference sub-expression
5661
		case 0x47:
5662
		case 0x67:
5663
			$name = 'tMemErr';
5664
			// offset: 1; size: 4; not used
5665
			// offset: 5; size: 2; size of the following subexpression
5666
			$subSize = self::_GetInt2d($formulaData, 5);
5667
			$size = 7 + $subSize;
5668
			$data = $this->_getFormulaFromData(substr($formulaData, 7, $subSize));
5669
			break;
5670
		case 0x29:	//	Variable reference sub-expression
5671
		case 0x49:
5672
		case 0x69:
5673
			$name = 'tMemFunc';
5674
			// offset: 1; size: 2; size of the following sub-expression
5675
			$subSize = self::_GetInt2d($formulaData, 1);
5676
			$size = 3 + $subSize;
5677
			$data = $this->_getFormulaFromData(substr($formulaData, 3, $subSize));
5678
			break;
5679
 
5680
		case 0x2C: // Relative 2d cell reference reference, used in shared formulas and some other places
5681
		case 0x4C:
5682
		case 0x6C:
5683
			$name = 'tRefN';
5684
			$size = 5;
5685
			$data = $this->_readBIFF8CellAddressB(substr($formulaData, 1, 4), $baseCell);
5686
			break;
5687
 
5688
		case 0x2D:	//	Relative 2d range reference
5689
		case 0x4D:
5690
		case 0x6D:
5691
			$name = 'tAreaN';
5692
			$size = 9;
5693
			$data = $this->_readBIFF8CellRangeAddressB(substr($formulaData, 1, 8), $baseCell);
5694
			break;
5695
 
5696
		case 0x39:	//	External name
5697
		case 0x59:
5698
		case 0x79:
5699
			$name = 'tNameX';
5700
			$size = 7;
5701
			// offset: 1; size: 2; index to REF entry in EXTERNSHEET record
5702
			// offset: 3; size: 2; one-based index to DEFINEDNAME or EXTERNNAME record
5703
			$index = self::_GetInt2d($formulaData, 3);
5704
			// assume index is to EXTERNNAME record
5705
			$data = $this->_externalNames[$index - 1]['name'];
5706
			// offset: 5; size: 2; not used
5707
			break;
5708
 
5709
		case 0x3A:	//	3d reference to cell
5710
		case 0x5A:
5711
		case 0x7A:
5712
			$name = 'tRef3d';
5713
			$size = 7;
5714
 
5715
			try {
5716
				// offset: 1; size: 2; index to REF entry
5717
				$sheetRange = $this->_readSheetRangeByRefIndex(self::_GetInt2d($formulaData, 1));
5718
				// offset: 3; size: 4; cell address
5719
				$cellAddress = $this->_readBIFF8CellAddress(substr($formulaData, 3, 4));
5720
 
5721
				$data = "$sheetRange!$cellAddress";
5722
			} catch (PHPExcel_Exception $e) {
5723
				// deleted sheet reference
5724
				$data = '#REF!';
5725
			}
5726
 
5727
			break;
5728
		case 0x3B:	//	3d reference to cell range
5729
		case 0x5B:
5730
		case 0x7B:
5731
			$name = 'tArea3d';
5732
			$size = 11;
5733
 
5734
			try {
5735
				// offset: 1; size: 2; index to REF entry
5736
				$sheetRange = $this->_readSheetRangeByRefIndex(self::_GetInt2d($formulaData, 1));
5737
				// offset: 3; size: 8; cell address
5738
				$cellRangeAddress = $this->_readBIFF8CellRangeAddress(substr($formulaData, 3, 8));
5739
 
5740
				$data = "$sheetRange!$cellRangeAddress";
5741
			} catch (PHPExcel_Exception $e) {
5742
				// deleted sheet reference
5743
				$data = '#REF!';
5744
			}
5745
 
5746
			break;
5747
		// Unknown cases	// don't know how to deal with
5748
		default:
5749
			throw new PHPExcel_Reader_Exception('Unrecognized token ' . sprintf('%02X', $id) . ' in formula');
5750
			break;
5751
		}
5752
 
5753
		return array(
5754
			'id' => $id,
5755
			'name' => $name,
5756
			'size' => $size,
5757
			'data' => $data,
5758
		);
5759
	}
5760
 
5761
 
5762
	/**
5763
	 * Reads a cell address in BIFF8 e.g. 'A2' or '$A$2'
5764
	 * section 3.3.4
5765
	 *
5766
	 * @param string $cellAddressStructure
5767
	 * @return string
5768
	 */
5769
	private function _readBIFF8CellAddress($cellAddressStructure)
5770
	{
5771
		// offset: 0; size: 2; index to row (0... 65535) (or offset (-32768... 32767))
5772
		$row = self::_GetInt2d($cellAddressStructure, 0) + 1;
5773
 
5774
		// offset: 2; size: 2; index to column or column offset + relative flags
5775
 
5776
			// bit: 7-0; mask 0x00FF; column index
5777
			$column = PHPExcel_Cell::stringFromColumnIndex(0x00FF & self::_GetInt2d($cellAddressStructure, 2));
5778
 
5779
			// bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
5780
			if (!(0x4000 & self::_GetInt2d($cellAddressStructure, 2))) {
5781
				$column = '$' . $column;
5782
			}
5783
			// bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
5784
			if (!(0x8000 & self::_GetInt2d($cellAddressStructure, 2))) {
5785
				$row = '$' . $row;
5786
			}
5787
 
5788
		return $column . $row;
5789
	}
5790
 
5791
 
5792
	/**
5793
	 * Reads a cell address in BIFF8 for shared formulas. Uses positive and negative values for row and column
5794
	 * to indicate offsets from a base cell
5795
	 * section 3.3.4
5796
	 *
5797
	 * @param string $cellAddressStructure
5798
	 * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
5799
	 * @return string
5800
	 */
5801
	private function _readBIFF8CellAddressB($cellAddressStructure, $baseCell = 'A1')
5802
	{
5803
		list($baseCol, $baseRow) = PHPExcel_Cell::coordinateFromString($baseCell);
5804
		$baseCol = PHPExcel_Cell::columnIndexFromString($baseCol) - 1;
5805
 
5806
		// offset: 0; size: 2; index to row (0... 65535) (or offset (-32768... 32767))
5807
			$rowIndex = self::_GetInt2d($cellAddressStructure, 0);
5808
			$row = self::_GetInt2d($cellAddressStructure, 0) + 1;
5809
 
5810
		// offset: 2; size: 2; index to column or column offset + relative flags
5811
 
5812
			// bit: 7-0; mask 0x00FF; column index
5813
			$colIndex = 0x00FF & self::_GetInt2d($cellAddressStructure, 2);
5814
 
5815
			// bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
5816
			if (!(0x4000 & self::_GetInt2d($cellAddressStructure, 2))) {
5817
				$column = PHPExcel_Cell::stringFromColumnIndex($colIndex);
5818
				$column = '$' . $column;
5819
			} else {
5820
				$colIndex = ($colIndex <= 127) ? $colIndex : $colIndex - 256;
5821
				$column = PHPExcel_Cell::stringFromColumnIndex($baseCol + $colIndex);
5822
			}
5823
 
5824
			// bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
5825
			if (!(0x8000 & self::_GetInt2d($cellAddressStructure, 2))) {
5826
				$row = '$' . $row;
5827
			} else {
5828
				$rowIndex = ($rowIndex <= 32767) ? $rowIndex : $rowIndex - 65536;
5829
				$row = $baseRow + $rowIndex;
5830
			}
5831
 
5832
		return $column . $row;
5833
	}
5834
 
5835
 
5836
	/**
5837
	 * Reads a cell range address in BIFF5 e.g. 'A2:B6' or 'A1'
5838
	 * always fixed range
5839
	 * section 2.5.14
5840
	 *
5841
	 * @param string $subData
5842
	 * @return string
5843
	 * @throws PHPExcel_Reader_Exception
5844
	 */
5845
	private function _readBIFF5CellRangeAddressFixed($subData)
5846
	{
5847
		// offset: 0; size: 2; index to first row
5848
		$fr = self::_GetInt2d($subData, 0) + 1;
5849
 
5850
		// offset: 2; size: 2; index to last row
5851
		$lr = self::_GetInt2d($subData, 2) + 1;
5852
 
5853
		// offset: 4; size: 1; index to first column
5854
		$fc = ord($subData{4});
5855
 
5856
		// offset: 5; size: 1; index to last column
5857
		$lc = ord($subData{5});
5858
 
5859
		// check values
5860
		if ($fr > $lr || $fc > $lc) {
5861
			throw new PHPExcel_Reader_Exception('Not a cell range address');
5862
		}
5863
 
5864
		// column index to letter
5865
		$fc = PHPExcel_Cell::stringFromColumnIndex($fc);
5866
		$lc = PHPExcel_Cell::stringFromColumnIndex($lc);
5867
 
5868
		if ($fr == $lr and $fc == $lc) {
5869
			return "$fc$fr";
5870
		}
5871
		return "$fc$fr:$lc$lr";
5872
	}
5873
 
5874
 
5875
	/**
5876
	 * Reads a cell range address in BIFF8 e.g. 'A2:B6' or 'A1'
5877
	 * always fixed range
5878
	 * section 2.5.14
5879
	 *
5880
	 * @param string $subData
5881
	 * @return string
5882
	 * @throws PHPExcel_Reader_Exception
5883
	 */
5884
	private function _readBIFF8CellRangeAddressFixed($subData)
5885
	{
5886
		// offset: 0; size: 2; index to first row
5887
		$fr = self::_GetInt2d($subData, 0) + 1;
5888
 
5889
		// offset: 2; size: 2; index to last row
5890
		$lr = self::_GetInt2d($subData, 2) + 1;
5891
 
5892
		// offset: 4; size: 2; index to first column
5893
		$fc = self::_GetInt2d($subData, 4);
5894
 
5895
		// offset: 6; size: 2; index to last column
5896
		$lc = self::_GetInt2d($subData, 6);
5897
 
5898
		// check values
5899
		if ($fr > $lr || $fc > $lc) {
5900
			throw new PHPExcel_Reader_Exception('Not a cell range address');
5901
		}
5902
 
5903
		// column index to letter
5904
		$fc = PHPExcel_Cell::stringFromColumnIndex($fc);
5905
		$lc = PHPExcel_Cell::stringFromColumnIndex($lc);
5906
 
5907
		if ($fr == $lr and $fc == $lc) {
5908
			return "$fc$fr";
5909
		}
5910
		return "$fc$fr:$lc$lr";
5911
	}
5912
 
5913
 
5914
	/**
5915
	 * Reads a cell range address in BIFF8 e.g. 'A2:B6' or '$A$2:$B$6'
5916
	 * there are flags indicating whether column/row index is relative
5917
	 * section 3.3.4
5918
	 *
5919
	 * @param string $subData
5920
	 * @return string
5921
	 */
5922
	private function _readBIFF8CellRangeAddress($subData)
5923
	{
5924
		// todo: if cell range is just a single cell, should this funciton
5925
		// not just return e.g. 'A1' and not 'A1:A1' ?
5926
 
5927
		// offset: 0; size: 2; index to first row (0... 65535) (or offset (-32768... 32767))
5928
			$fr = self::_GetInt2d($subData, 0) + 1;
5929
 
5930
		// offset: 2; size: 2; index to last row (0... 65535) (or offset (-32768... 32767))
5931
			$lr = self::_GetInt2d($subData, 2) + 1;
5932
 
5933
		// offset: 4; size: 2; index to first column or column offset + relative flags
5934
 
5935
		// bit: 7-0; mask 0x00FF; column index
5936
		$fc = PHPExcel_Cell::stringFromColumnIndex(0x00FF & self::_GetInt2d($subData, 4));
5937
 
5938
		// bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
5939
		if (!(0x4000 & self::_GetInt2d($subData, 4))) {
5940
			$fc = '$' . $fc;
5941
		}
5942
 
5943
		// bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
5944
		if (!(0x8000 & self::_GetInt2d($subData, 4))) {
5945
			$fr = '$' . $fr;
5946
		}
5947
 
5948
		// offset: 6; size: 2; index to last column or column offset + relative flags
5949
 
5950
		// bit: 7-0; mask 0x00FF; column index
5951
		$lc = PHPExcel_Cell::stringFromColumnIndex(0x00FF & self::_GetInt2d($subData, 6));
5952
 
5953
		// bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
5954
		if (!(0x4000 & self::_GetInt2d($subData, 6))) {
5955
			$lc = '$' . $lc;
5956
		}
5957
 
5958
		// bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
5959
		if (!(0x8000 & self::_GetInt2d($subData, 6))) {
5960
			$lr = '$' . $lr;
5961
		}
5962
 
5963
		return "$fc$fr:$lc$lr";
5964
	}
5965
 
5966
 
5967
	/**
5968
	 * Reads a cell range address in BIFF8 for shared formulas. Uses positive and negative values for row and column
5969
	 * to indicate offsets from a base cell
5970
	 * section 3.3.4
5971
	 *
5972
	 * @param string $subData
5973
	 * @param string $baseCell Base cell
5974
	 * @return string Cell range address
5975
	 */
5976
	private function _readBIFF8CellRangeAddressB($subData, $baseCell = 'A1')
5977
	{
5978
		list($baseCol, $baseRow) = PHPExcel_Cell::coordinateFromString($baseCell);
5979
		$baseCol = PHPExcel_Cell::columnIndexFromString($baseCol) - 1;
5980
 
5981
		// TODO: if cell range is just a single cell, should this funciton
5982
		// not just return e.g. 'A1' and not 'A1:A1' ?
5983
 
5984
		// offset: 0; size: 2; first row
5985
		$frIndex = self::_GetInt2d($subData, 0); // adjust below
5986
 
5987
		// offset: 2; size: 2; relative index to first row (0... 65535) should be treated as offset (-32768... 32767)
5988
		$lrIndex = self::_GetInt2d($subData, 2); // adjust below
5989
 
5990
		// offset: 4; size: 2; first column with relative/absolute flags
5991
 
5992
		// bit: 7-0; mask 0x00FF; column index
5993
		$fcIndex = 0x00FF & self::_GetInt2d($subData, 4);
5994
 
5995
		// bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
5996
		if (!(0x4000 & self::_GetInt2d($subData, 4))) {
5997
			// absolute column index
5998
			$fc = PHPExcel_Cell::stringFromColumnIndex($fcIndex);
5999
			$fc = '$' . $fc;
6000
		} else {
6001
			// column offset
6002
			$fcIndex = ($fcIndex <= 127) ? $fcIndex : $fcIndex - 256;
6003
			$fc = PHPExcel_Cell::stringFromColumnIndex($baseCol + $fcIndex);
6004
		}
6005
 
6006
		// bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
6007
		if (!(0x8000 & self::_GetInt2d($subData, 4))) {
6008
			// absolute row index
6009
			$fr = $frIndex + 1;
6010
			$fr = '$' . $fr;
6011
		} else {
6012
			// row offset
6013
			$frIndex = ($frIndex <= 32767) ? $frIndex : $frIndex - 65536;
6014
			$fr = $baseRow + $frIndex;
6015
		}
6016
 
6017
		// offset: 6; size: 2; last column with relative/absolute flags
6018
 
6019
		// bit: 7-0; mask 0x00FF; column index
6020
		$lcIndex = 0x00FF & self::_GetInt2d($subData, 6);
6021
		$lcIndex = ($lcIndex <= 127) ? $lcIndex : $lcIndex - 256;
6022
		$lc = PHPExcel_Cell::stringFromColumnIndex($baseCol + $lcIndex);
6023
 
6024
		// bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
6025
		if (!(0x4000 & self::_GetInt2d($subData, 6))) {
6026
			// absolute column index
6027
			$lc = PHPExcel_Cell::stringFromColumnIndex($lcIndex);
6028
			$lc = '$' . $lc;
6029
		} else {
6030
			// column offset
6031
			$lcIndex = ($lcIndex <= 127) ? $lcIndex : $lcIndex - 256;
6032
			$lc = PHPExcel_Cell::stringFromColumnIndex($baseCol + $lcIndex);
6033
		}
6034
 
6035
		// bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
6036
		if (!(0x8000 & self::_GetInt2d($subData, 6))) {
6037
			// absolute row index
6038
			$lr = $lrIndex + 1;
6039
			$lr = '$' . $lr;
6040
		} else {
6041
			// row offset
6042
			$lrIndex = ($lrIndex <= 32767) ? $lrIndex : $lrIndex - 65536;
6043
			$lr = $baseRow + $lrIndex;
6044
		}
6045
 
6046
		return "$fc$fr:$lc$lr";
6047
	}
6048
 
6049
 
6050
	/**
6051
	 * Read BIFF8 cell range address list
6052
	 * section 2.5.15
6053
	 *
6054
	 * @param string $subData
6055
	 * @return array
6056
	 */
6057
	private function _readBIFF8CellRangeAddressList($subData)
6058
	{
6059
		$cellRangeAddresses = array();
6060
 
6061
		// offset: 0; size: 2; number of the following cell range addresses
6062
		$nm = self::_GetInt2d($subData, 0);
6063
 
6064
		$offset = 2;
6065
		// offset: 2; size: 8 * $nm; list of $nm (fixed) cell range addresses
6066
		for ($i = 0; $i < $nm; ++$i) {
6067
			$cellRangeAddresses[] = $this->_readBIFF8CellRangeAddressFixed(substr($subData, $offset, 8));
6068
			$offset += 8;
6069
		}
6070
 
6071
		return array(
6072
			'size' => 2 + 8 * $nm,
6073
			'cellRangeAddresses' => $cellRangeAddresses,
6074
		);
6075
	}
6076
 
6077
 
6078
	/**
6079
	 * Read BIFF5 cell range address list
6080
	 * section 2.5.15
6081
	 *
6082
	 * @param string $subData
6083
	 * @return array
6084
	 */
6085
	private function _readBIFF5CellRangeAddressList($subData)
6086
	{
6087
		$cellRangeAddresses = array();
6088
 
6089
		// offset: 0; size: 2; number of the following cell range addresses
6090
		$nm = self::_GetInt2d($subData, 0);
6091
 
6092
		$offset = 2;
6093
		// offset: 2; size: 6 * $nm; list of $nm (fixed) cell range addresses
6094
		for ($i = 0; $i < $nm; ++$i) {
6095
			$cellRangeAddresses[] = $this->_readBIFF5CellRangeAddressFixed(substr($subData, $offset, 6));
6096
			$offset += 6;
6097
		}
6098
 
6099
		return array(
6100
			'size' => 2 + 6 * $nm,
6101
			'cellRangeAddresses' => $cellRangeAddresses,
6102
		);
6103
	}
6104
 
6105
 
6106
	/**
6107
	 * Get a sheet range like Sheet1:Sheet3 from REF index
6108
	 * Note: If there is only one sheet in the range, one gets e.g Sheet1
6109
	 * It can also happen that the REF structure uses the -1 (FFFF) code to indicate deleted sheets,
6110
	 * in which case an PHPExcel_Reader_Exception is thrown
6111
	 *
6112
	 * @param int $index
6113
	 * @return string|false
6114
	 * @throws PHPExcel_Reader_Exception
6115
	 */
6116
	private function _readSheetRangeByRefIndex($index)
6117
	{
6118
		if (isset($this->_ref[$index])) {
6119
 
6120
			$type = $this->_externalBooks[$this->_ref[$index]['externalBookIndex']]['type'];
6121
 
6122
			switch ($type) {
6123
				case 'internal':
6124
					// check if we have a deleted 3d reference
6125
					if ($this->_ref[$index]['firstSheetIndex'] == 0xFFFF or $this->_ref[$index]['lastSheetIndex'] == 0xFFFF) {
6126
						throw new PHPExcel_Reader_Exception('Deleted sheet reference');
6127
					}
6128
 
6129
					// we have normal sheet range (collapsed or uncollapsed)
6130
					$firstSheetName = $this->_sheets[$this->_ref[$index]['firstSheetIndex']]['name'];
6131
					$lastSheetName = $this->_sheets[$this->_ref[$index]['lastSheetIndex']]['name'];
6132
 
6133
					if ($firstSheetName == $lastSheetName) {
6134
						// collapsed sheet range
6135
						$sheetRange = $firstSheetName;
6136
					} else {
6137
						$sheetRange = "$firstSheetName:$lastSheetName";
6138
					}
6139
 
6140
					// escape the single-quotes
6141
					$sheetRange = str_replace("'", "''", $sheetRange);
6142
 
6143
					// if there are special characters, we need to enclose the range in single-quotes
6144
					// todo: check if we have identified the whole set of special characters
6145
					// it seems that the following characters are not accepted for sheet names
6146
					// and we may assume that they are not present: []*/:\?
6147
					if (preg_match("/[ !\"@#£$%&{()}<>=+'|^,;-]/", $sheetRange)) {
6148
						$sheetRange = "'$sheetRange'";
6149
					}
6150
 
6151
					return $sheetRange;
6152
					break;
6153
 
6154
				default:
6155
					// TODO: external sheet support
6156
					throw new PHPExcel_Reader_Exception('Excel5 reader only supports internal sheets in fomulas');
6157
					break;
6158
			}
6159
		}
6160
		return false;
6161
	}
6162
 
6163
 
6164
	/**
6165
	 * read BIFF8 constant value array from array data
6166
	 * returns e.g. array('value' => '{1,2;3,4}', 'size' => 40}
6167
	 * section 2.5.8
6168
	 *
6169
	 * @param string $arrayData
6170
	 * @return array
6171
	 */
6172
	private static function _readBIFF8ConstantArray($arrayData)
6173
	{
6174
		// offset: 0; size: 1; number of columns decreased by 1
6175
		$nc = ord($arrayData[0]);
6176
 
6177
		// offset: 1; size: 2; number of rows decreased by 1
6178
		$nr = self::_GetInt2d($arrayData, 1);
6179
		$size = 3; // initialize
6180
		$arrayData = substr($arrayData, 3);
6181
 
6182
		// offset: 3; size: var; list of ($nc + 1) * ($nr + 1) constant values
6183
		$matrixChunks = array();
6184
		for ($r = 1; $r <= $nr + 1; ++$r) {
6185
			$items = array();
6186
			for ($c = 1; $c <= $nc + 1; ++$c) {
6187
				$constant = self::_readBIFF8Constant($arrayData);
6188
				$items[] = $constant['value'];
6189
				$arrayData = substr($arrayData, $constant['size']);
6190
				$size += $constant['size'];
6191
			}
6192
			$matrixChunks[] = implode(',', $items); // looks like e.g. '1,"hello"'
6193
		}
6194
		$matrix = '{' . implode(';', $matrixChunks) . '}';
6195
 
6196
		return array(
6197
			'value' => $matrix,
6198
			'size' => $size,
6199
		);
6200
	}
6201
 
6202
 
6203
	/**
6204
	 * read BIFF8 constant value which may be 'Empty Value', 'Number', 'String Value', 'Boolean Value', 'Error Value'
6205
	 * section 2.5.7
6206
	 * returns e.g. array('value' => '5', 'size' => 9)
6207
	 *
6208
	 * @param string $valueData
6209
	 * @return array
6210
	 */
6211
	private static function _readBIFF8Constant($valueData)
6212
	{
6213
		// offset: 0; size: 1; identifier for type of constant
6214
		$identifier = ord($valueData[0]);
6215
 
6216
		switch ($identifier) {
6217
		case 0x00: // empty constant (what is this?)
6218
			$value = '';
6219
			$size = 9;
6220
			break;
6221
		case 0x01: // number
6222
			// offset: 1; size: 8; IEEE 754 floating-point value
6223
			$value = self::_extractNumber(substr($valueData, 1, 8));
6224
			$size = 9;
6225
			break;
6226
		case 0x02: // string value
6227
			// offset: 1; size: var; Unicode string, 16-bit string length
6228
			$string = self::_readUnicodeStringLong(substr($valueData, 1));
6229
			$value = '"' . $string['value'] . '"';
6230
			$size = 1 + $string['size'];
6231
			break;
6232
		case 0x04: // boolean
6233
			// offset: 1; size: 1; 0 = FALSE, 1 = TRUE
6234
			if (ord($valueData[1])) {
6235
				$value = 'TRUE';
6236
			} else {
6237
				$value = 'FALSE';
6238
			}
6239
			$size = 9;
6240
			break;
6241
		case 0x10: // error code
6242
			// offset: 1; size: 1; error code
6243
			$value = self::_mapErrorCode(ord($valueData[1]));
6244
			$size = 9;
6245
			break;
6246
		}
6247
		return array(
6248
			'value' => $value,
6249
			'size' => $size,
6250
		);
6251
	}
6252
 
6253
 
6254
	/**
6255
	 * Extract RGB color
6256
	 * OpenOffice.org's Documentation of the Microsoft Excel File Format, section 2.5.4
6257
	 *
6258
	 * @param string $rgb Encoded RGB value (4 bytes)
6259
	 * @return array
6260
	 */
6261
	private static function _readRGB($rgb)
6262
	{
6263
		// offset: 0; size 1; Red component
6264
		$r = ord($rgb{0});
6265
 
6266
		// offset: 1; size: 1; Green component
6267
		$g = ord($rgb{1});
6268
 
6269
		// offset: 2; size: 1; Blue component
6270
		$b = ord($rgb{2});
6271
 
6272
		// HEX notation, e.g. 'FF00FC'
6273
		$rgb = sprintf('%02X%02X%02X', $r, $g, $b);
6274
 
6275
		return array('rgb' => $rgb);
6276
	}
6277
 
6278
 
6279
	/**
6280
	 * Read byte string (8-bit string length)
6281
	 * OpenOffice documentation: 2.5.2
6282
	 *
6283
	 * @param string $subData
6284
	 * @return array
6285
	 */
6286
	private function _readByteStringShort($subData)
6287
	{
6288
		// offset: 0; size: 1; length of the string (character count)
6289
		$ln = ord($subData[0]);
6290
 
6291
		// offset: 1: size: var; character array (8-bit characters)
6292
		$value = $this->_decodeCodepage(substr($subData, 1, $ln));
6293
 
6294
		return array(
6295
			'value' => $value,
6296
			'size' => 1 + $ln, // size in bytes of data structure
6297
		);
6298
	}
6299
 
6300
 
6301
	/**
6302
	 * Read byte string (16-bit string length)
6303
	 * OpenOffice documentation: 2.5.2
6304
	 *
6305
	 * @param string $subData
6306
	 * @return array
6307
	 */
6308
	private function _readByteStringLong($subData)
6309
	{
6310
		// offset: 0; size: 2; length of the string (character count)
6311
		$ln = self::_GetInt2d($subData, 0);
6312
 
6313
		// offset: 2: size: var; character array (8-bit characters)
6314
		$value = $this->_decodeCodepage(substr($subData, 2));
6315
 
6316
		//return $string;
6317
		return array(
6318
			'value' => $value,
6319
			'size' => 2 + $ln, // size in bytes of data structure
6320
		);
6321
	}
6322
 
6323
 
6324
	/**
6325
	 * Extracts an Excel Unicode short string (8-bit string length)
6326
	 * OpenOffice documentation: 2.5.3
6327
	 * function will automatically find out where the Unicode string ends.
6328
	 *
6329
	 * @param string $subData
6330
	 * @return array
6331
	 */
6332
	private static function _readUnicodeStringShort($subData)
6333
	{
6334
		$value = '';
6335
 
6336
		// offset: 0: size: 1; length of the string (character count)
6337
		$characterCount = ord($subData[0]);
6338
 
6339
		$string = self::_readUnicodeString(substr($subData, 1), $characterCount);
6340
 
6341
		// add 1 for the string length
6342
		$string['size'] += 1;
6343
 
6344
		return $string;
6345
	}
6346
 
6347
 
6348
	/**
6349
	 * Extracts an Excel Unicode long string (16-bit string length)
6350
	 * OpenOffice documentation: 2.5.3
6351
	 * this function is under construction, needs to support rich text, and Asian phonetic settings
6352
	 *
6353
	 * @param string $subData
6354
	 * @return array
6355
	 */
6356
	private static function _readUnicodeStringLong($subData)
6357
	{
6358
		$value = '';
6359
 
6360
		// offset: 0: size: 2; length of the string (character count)
6361
		$characterCount = self::_GetInt2d($subData, 0);
6362
 
6363
		$string = self::_readUnicodeString(substr($subData, 2), $characterCount);
6364
 
6365
		// add 2 for the string length
6366
		$string['size'] += 2;
6367
 
6368
		return $string;
6369
	}
6370
 
6371
 
6372
	/**
6373
	 * Read Unicode string with no string length field, but with known character count
6374
	 * this function is under construction, needs to support rich text, and Asian phonetic settings
6375
	 * OpenOffice.org's Documentation of the Microsoft Excel File Format, section 2.5.3
6376
	 *
6377
	 * @param string $subData
6378
	 * @param int $characterCount
6379
	 * @return array
6380
	 */
6381
	private static function _readUnicodeString($subData, $characterCount)
6382
	{
6383
		$value = '';
6384
 
6385
		// offset: 0: size: 1; option flags
6386
 
6387
			// bit: 0; mask: 0x01; character compression (0 = compressed 8-bit, 1 = uncompressed 16-bit)
6388
			$isCompressed = !((0x01 & ord($subData[0])) >> 0);
6389
 
6390
			// bit: 2; mask: 0x04; Asian phonetic settings
6391
			$hasAsian = (0x04) & ord($subData[0]) >> 2;
6392
 
6393
			// bit: 3; mask: 0x08; Rich-Text settings
6394
			$hasRichText = (0x08) & ord($subData[0]) >> 3;
6395
 
6396
		// offset: 1: size: var; character array
6397
		// this offset assumes richtext and Asian phonetic settings are off which is generally wrong
6398
		// needs to be fixed
6399
		$value = self::_encodeUTF16(substr($subData, 1, $isCompressed ? $characterCount : 2 * $characterCount), $isCompressed);
6400
 
6401
		return array(
6402
			'value' => $value,
6403
			'size' => $isCompressed ? 1 + $characterCount : 1 + 2 * $characterCount, // the size in bytes including the option flags
6404
		);
6405
	}
6406
 
6407
 
6408
	/**
6409
	 * Convert UTF-8 string to string surounded by double quotes. Used for explicit string tokens in formulas.
6410
	 * Example:  hello"world  -->  "hello""world"
6411
	 *
6412
	 * @param string $value UTF-8 encoded string
6413
	 * @return string
6414
	 */
6415
	private static function _UTF8toExcelDoubleQuoted($value)
6416
	{
6417
		return '"' . str_replace('"', '""', $value) . '"';
6418
	}
6419
 
6420
 
6421
	/**
6422
	 * Reads first 8 bytes of a string and return IEEE 754 float
6423
	 *
6424
	 * @param string $data Binary string that is at least 8 bytes long
6425
	 * @return float
6426
	 */
6427
	private static function _extractNumber($data)
6428
	{
6429
		$rknumhigh = self::_GetInt4d($data, 4);
6430
		$rknumlow = self::_GetInt4d($data, 0);
6431
		$sign = ($rknumhigh & 0x80000000) >> 31;
6432
		$exp = (($rknumhigh & 0x7ff00000) >> 20) - 1023;
6433
		$mantissa = (0x100000 | ($rknumhigh & 0x000fffff));
6434
		$mantissalow1 = ($rknumlow & 0x80000000) >> 31;
6435
		$mantissalow2 = ($rknumlow & 0x7fffffff);
6436
		$value = $mantissa / pow( 2 , (20 - $exp));
6437
 
6438
		if ($mantissalow1 != 0) {
6439
			$value += 1 / pow (2 , (21 - $exp));
6440
		}
6441
 
6442
		$value += $mantissalow2 / pow (2 , (52 - $exp));
6443
		if ($sign) {
6444
			$value *= -1;
6445
		}
6446
 
6447
		return $value;
6448
	}
6449
 
6450
 
6451
	private static function _GetIEEE754($rknum)
6452
	{
6453
		if (($rknum & 0x02) != 0) {
6454
			$value = $rknum >> 2;
6455
		} else {
6456
			// changes by mmp, info on IEEE754 encoding from
6457
			// research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html
6458
			// The RK format calls for using only the most significant 30 bits
6459
			// of the 64 bit floating point value. The other 34 bits are assumed
6460
			// to be 0 so we use the upper 30 bits of $rknum as follows...
6461
			$sign = ($rknum & 0x80000000) >> 31;
6462
			$exp = ($rknum & 0x7ff00000) >> 20;
6463
			$mantissa = (0x100000 | ($rknum & 0x000ffffc));
6464
			$value = $mantissa / pow( 2 , (20- ($exp - 1023)));
6465
			if ($sign) {
6466
				$value = -1 * $value;
6467
			}
6468
			//end of changes by mmp
6469
		}
6470
		if (($rknum & 0x01) != 0) {
6471
			$value /= 100;
6472
		}
6473
		return $value;
6474
	}
6475
 
6476
 
6477
	/**
6478
	 * Get UTF-8 string from (compressed or uncompressed) UTF-16 string
6479
	 *
6480
	 * @param string $string
6481
	 * @param bool $compressed
6482
	 * @return string
6483
	 */
6484
	private static function _encodeUTF16($string, $compressed = '')
6485
	{
6486
		if ($compressed) {
6487
			$string = self::_uncompressByteString($string);
6488
 		}
6489
 
6490
		return PHPExcel_Shared_String::ConvertEncoding($string, 'UTF-8', 'UTF-16LE');
6491
	}
6492
 
6493
 
6494
	/**
6495
	 * Convert UTF-16 string in compressed notation to uncompressed form. Only used for BIFF8.
6496
	 *
6497
	 * @param string $string
6498
	 * @return string
6499
	 */
6500
	private static function _uncompressByteString($string)
6501
	{
6502
		$uncompressedString = '';
6503
		$strLen = strlen($string);
6504
		for ($i = 0; $i < $strLen; ++$i) {
6505
			$uncompressedString .= $string[$i] . "\0";
6506
		}
6507
 
6508
		return $uncompressedString;
6509
	}
6510
 
6511
 
6512
	/**
6513
	 * Convert string to UTF-8. Only used for BIFF5.
6514
	 *
6515
	 * @param string $string
6516
	 * @return string
6517
	 */
6518
	private function _decodeCodepage($string)
6519
	{
6520
		return PHPExcel_Shared_String::ConvertEncoding($string, 'UTF-8', $this->_codepage);
6521
	}
6522
 
6523
 
6524
	/**
6525
	 * Read 16-bit unsigned integer
6526
	 *
6527
	 * @param string $data
6528
	 * @param int $pos
6529
	 * @return int
6530
	 */
6531
	public static function _GetInt2d($data, $pos)
6532
	{
6533
		return ord($data[$pos]) | (ord($data[$pos+1]) << 8);
6534
	}
6535
 
6536
 
6537
	/**
6538
	 * Read 32-bit signed integer
6539
	 *
6540
	 * @param string $data
6541
	 * @param int $pos
6542
	 * @return int
6543
	 */
6544
	public static function _GetInt4d($data, $pos)
6545
	{
6546
		// FIX: represent numbers correctly on 64-bit system
6547
		// http://sourceforge.net/tracker/index.php?func=detail&aid=1487372&group_id=99160&atid=623334
6548
		// Hacked by Andreas Rehm 2006 to ensure correct result of the <<24 block on 32 and 64bit systems
6549
		$_or_24 = ord($data[$pos + 3]);
6550
		if ($_or_24 >= 128) {
6551
			// negative number
6552
			$_ord_24 = -abs((256 - $_or_24) << 24);
6553
		} else {
6554
			$_ord_24 = ($_or_24 & 127) << 24;
6555
		}
6556
		return ord($data[$pos]) | (ord($data[$pos+1]) << 8) | (ord($data[$pos+2]) << 16) | $_ord_24;
6557
	}
6558
 
6559
 
6560
	/**
6561
	 * Read color
6562
	 *
6563
	 * @param int $color Indexed color
6564
	 * @param array $palette Color palette
6565
	 * @return array RGB color value, example: array('rgb' => 'FF0000')
6566
	 */
6567
	private static function _readColor($color,$palette,$version)
6568
	{
6569
		if ($color <= 0x07 || $color >= 0x40) {
6570
			// special built-in color
6571
			return self::_mapBuiltInColor($color);
6572
		} elseif (isset($palette) && isset($palette[$color - 8])) {
6573
			// palette color, color index 0x08 maps to pallete index 0
6574
			return $palette[$color - 8];
6575
		} else {
6576
			// default color table
6577
			if ($version == self::XLS_BIFF8) {
6578
				return self::_mapColor($color);
6579
			} else {
6580
				// BIFF5
6581
				return self::_mapColorBIFF5($color);
6582
			}
6583
		}
6584
 
6585
		return $color;
6586
	}
6587
 
6588
 
6589
	/**
6590
	 * Map border style
6591
	 * OpenOffice documentation: 2.5.11
6592
	 *
6593
	 * @param int $index
6594
	 * @return string
6595
	 */
6596
	private static function _mapBorderStyle($index)
6597
	{
6598
		switch ($index) {
6599
			case 0x00: return PHPExcel_Style_Border::BORDER_NONE;
6600
			case 0x01: return PHPExcel_Style_Border::BORDER_THIN;
6601
			case 0x02: return PHPExcel_Style_Border::BORDER_MEDIUM;
6602
			case 0x03: return PHPExcel_Style_Border::BORDER_DASHED;
6603
			case 0x04: return PHPExcel_Style_Border::BORDER_DOTTED;
6604
			case 0x05: return PHPExcel_Style_Border::BORDER_THICK;
6605
			case 0x06: return PHPExcel_Style_Border::BORDER_DOUBLE;
6606
			case 0x07: return PHPExcel_Style_Border::BORDER_HAIR;
6607
			case 0x08: return PHPExcel_Style_Border::BORDER_MEDIUMDASHED;
6608
			case 0x09: return PHPExcel_Style_Border::BORDER_DASHDOT;
6609
			case 0x0A: return PHPExcel_Style_Border::BORDER_MEDIUMDASHDOT;
6610
			case 0x0B: return PHPExcel_Style_Border::BORDER_DASHDOTDOT;
6611
			case 0x0C: return PHPExcel_Style_Border::BORDER_MEDIUMDASHDOTDOT;
6612
			case 0x0D: return PHPExcel_Style_Border::BORDER_SLANTDASHDOT;
6613
			default:   return PHPExcel_Style_Border::BORDER_NONE;
6614
		}
6615
	}
6616
 
6617
 
6618
	/**
6619
	 * Get fill pattern from index
6620
	 * OpenOffice documentation: 2.5.12
6621
	 *
6622
	 * @param int $index
6623
	 * @return string
6624
	 */
6625
	private static function _mapFillPattern($index)
6626
	{
6627
		switch ($index) {
6628
			case 0x00: return PHPExcel_Style_Fill::FILL_NONE;
6629
			case 0x01: return PHPExcel_Style_Fill::FILL_SOLID;
6630
			case 0x02: return PHPExcel_Style_Fill::FILL_PATTERN_MEDIUMGRAY;
6631
			case 0x03: return PHPExcel_Style_Fill::FILL_PATTERN_DARKGRAY;
6632
			case 0x04: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRAY;
6633
			case 0x05: return PHPExcel_Style_Fill::FILL_PATTERN_DARKHORIZONTAL;
6634
			case 0x06: return PHPExcel_Style_Fill::FILL_PATTERN_DARKVERTICAL;
6635
			case 0x07: return PHPExcel_Style_Fill::FILL_PATTERN_DARKDOWN;
6636
			case 0x08: return PHPExcel_Style_Fill::FILL_PATTERN_DARKUP;
6637
			case 0x09: return PHPExcel_Style_Fill::FILL_PATTERN_DARKGRID;
6638
			case 0x0A: return PHPExcel_Style_Fill::FILL_PATTERN_DARKTRELLIS;
6639
			case 0x0B: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTHORIZONTAL;
6640
			case 0x0C: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTVERTICAL;
6641
			case 0x0D: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTDOWN;
6642
			case 0x0E: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTUP;
6643
			case 0x0F: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRID;
6644
			case 0x10: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTTRELLIS;
6645
			case 0x11: return PHPExcel_Style_Fill::FILL_PATTERN_GRAY125;
6646
			case 0x12: return PHPExcel_Style_Fill::FILL_PATTERN_GRAY0625;
6647
			default:   return PHPExcel_Style_Fill::FILL_NONE;
6648
		}
6649
	}
6650
 
6651
 
6652
	/**
6653
	 * Map error code, e.g. '#N/A'
6654
	 *
6655
	 * @param int $subData
6656
	 * @return string
6657
	 */
6658
	private static function _mapErrorCode($subData)
6659
	{
6660
		switch ($subData) {
6661
			case 0x00: return '#NULL!';		break;
6662
			case 0x07: return '#DIV/0!';	break;
6663
			case 0x0F: return '#VALUE!';	break;
6664
			case 0x17: return '#REF!';		break;
6665
			case 0x1D: return '#NAME?';		break;
6666
			case 0x24: return '#NUM!';		break;
6667
			case 0x2A: return '#N/A';		break;
6668
			default: return false;
6669
		}
6670
	}
6671
 
6672
 
6673
	/**
6674
	 * Map built-in color to RGB value
6675
	 *
6676
	 * @param int $color Indexed color
6677
	 * @return array
6678
	 */
6679
	private static function _mapBuiltInColor($color)
6680
	{
6681
		switch ($color) {
6682
			case 0x00: return array('rgb' => '000000');
6683
			case 0x01: return array('rgb' => 'FFFFFF');
6684
			case 0x02: return array('rgb' => 'FF0000');
6685
			case 0x03: return array('rgb' => '00FF00');
6686
			case 0x04: return array('rgb' => '0000FF');
6687
			case 0x05: return array('rgb' => 'FFFF00');
6688
			case 0x06: return array('rgb' => 'FF00FF');
6689
			case 0x07: return array('rgb' => '00FFFF');
6690
			case 0x40: return array('rgb' => '000000'); // system window text color
6691
			case 0x41: return array('rgb' => 'FFFFFF'); // system window background color
6692
			default:   return array('rgb' => '000000');
6693
		}
6694
	}
6695
 
6696
 
6697
	/**
6698
	 * Map color array from BIFF5 built-in color index
6699
	 *
6700
	 * @param int $subData
6701
	 * @return array
6702
	 */
6703
	private static function _mapColorBIFF5($subData)
6704
	{
6705
		switch ($subData) {
6706
			case 0x08: return array('rgb' => '000000');
6707
			case 0x09: return array('rgb' => 'FFFFFF');
6708
			case 0x0A: return array('rgb' => 'FF0000');
6709
			case 0x0B: return array('rgb' => '00FF00');
6710
			case 0x0C: return array('rgb' => '0000FF');
6711
			case 0x0D: return array('rgb' => 'FFFF00');
6712
			case 0x0E: return array('rgb' => 'FF00FF');
6713
			case 0x0F: return array('rgb' => '00FFFF');
6714
			case 0x10: return array('rgb' => '800000');
6715
			case 0x11: return array('rgb' => '008000');
6716
			case 0x12: return array('rgb' => '000080');
6717
			case 0x13: return array('rgb' => '808000');
6718
			case 0x14: return array('rgb' => '800080');
6719
			case 0x15: return array('rgb' => '008080');
6720
			case 0x16: return array('rgb' => 'C0C0C0');
6721
			case 0x17: return array('rgb' => '808080');
6722
			case 0x18: return array('rgb' => '8080FF');
6723
			case 0x19: return array('rgb' => '802060');
6724
			case 0x1A: return array('rgb' => 'FFFFC0');
6725
			case 0x1B: return array('rgb' => 'A0E0F0');
6726
			case 0x1C: return array('rgb' => '600080');
6727
			case 0x1D: return array('rgb' => 'FF8080');
6728
			case 0x1E: return array('rgb' => '0080C0');
6729
			case 0x1F: return array('rgb' => 'C0C0FF');
6730
			case 0x20: return array('rgb' => '000080');
6731
			case 0x21: return array('rgb' => 'FF00FF');
6732
			case 0x22: return array('rgb' => 'FFFF00');
6733
			case 0x23: return array('rgb' => '00FFFF');
6734
			case 0x24: return array('rgb' => '800080');
6735
			case 0x25: return array('rgb' => '800000');
6736
			case 0x26: return array('rgb' => '008080');
6737
			case 0x27: return array('rgb' => '0000FF');
6738
			case 0x28: return array('rgb' => '00CFFF');
6739
			case 0x29: return array('rgb' => '69FFFF');
6740
			case 0x2A: return array('rgb' => 'E0FFE0');
6741
			case 0x2B: return array('rgb' => 'FFFF80');
6742
			case 0x2C: return array('rgb' => 'A6CAF0');
6743
			case 0x2D: return array('rgb' => 'DD9CB3');
6744
			case 0x2E: return array('rgb' => 'B38FEE');
6745
			case 0x2F: return array('rgb' => 'E3E3E3');
6746
			case 0x30: return array('rgb' => '2A6FF9');
6747
			case 0x31: return array('rgb' => '3FB8CD');
6748
			case 0x32: return array('rgb' => '488436');
6749
			case 0x33: return array('rgb' => '958C41');
6750
			case 0x34: return array('rgb' => '8E5E42');
6751
			case 0x35: return array('rgb' => 'A0627A');
6752
			case 0x36: return array('rgb' => '624FAC');
6753
			case 0x37: return array('rgb' => '969696');
6754
			case 0x38: return array('rgb' => '1D2FBE');
6755
			case 0x39: return array('rgb' => '286676');
6756
			case 0x3A: return array('rgb' => '004500');
6757
			case 0x3B: return array('rgb' => '453E01');
6758
			case 0x3C: return array('rgb' => '6A2813');
6759
			case 0x3D: return array('rgb' => '85396A');
6760
			case 0x3E: return array('rgb' => '4A3285');
6761
			case 0x3F: return array('rgb' => '424242');
6762
			default:   return array('rgb' => '000000');
6763
		}
6764
	}
6765
 
6766
 
6767
	/**
6768
	 * Map color array from BIFF8 built-in color index
6769
	 *
6770
	 * @param int $subData
6771
	 * @return array
6772
	 */
6773
	private static function _mapColor($subData)
6774
	{
6775
		switch ($subData) {
6776
			case 0x08: return array('rgb' => '000000');
6777
			case 0x09: return array('rgb' => 'FFFFFF');
6778
			case 0x0A: return array('rgb' => 'FF0000');
6779
			case 0x0B: return array('rgb' => '00FF00');
6780
			case 0x0C: return array('rgb' => '0000FF');
6781
			case 0x0D: return array('rgb' => 'FFFF00');
6782
			case 0x0E: return array('rgb' => 'FF00FF');
6783
			case 0x0F: return array('rgb' => '00FFFF');
6784
			case 0x10: return array('rgb' => '800000');
6785
			case 0x11: return array('rgb' => '008000');
6786
			case 0x12: return array('rgb' => '000080');
6787
			case 0x13: return array('rgb' => '808000');
6788
			case 0x14: return array('rgb' => '800080');
6789
			case 0x15: return array('rgb' => '008080');
6790
			case 0x16: return array('rgb' => 'C0C0C0');
6791
			case 0x17: return array('rgb' => '808080');
6792
			case 0x18: return array('rgb' => '9999FF');
6793
			case 0x19: return array('rgb' => '993366');
6794
			case 0x1A: return array('rgb' => 'FFFFCC');
6795
			case 0x1B: return array('rgb' => 'CCFFFF');
6796
			case 0x1C: return array('rgb' => '660066');
6797
			case 0x1D: return array('rgb' => 'FF8080');
6798
			case 0x1E: return array('rgb' => '0066CC');
6799
			case 0x1F: return array('rgb' => 'CCCCFF');
6800
			case 0x20: return array('rgb' => '000080');
6801
			case 0x21: return array('rgb' => 'FF00FF');
6802
			case 0x22: return array('rgb' => 'FFFF00');
6803
			case 0x23: return array('rgb' => '00FFFF');
6804
			case 0x24: return array('rgb' => '800080');
6805
			case 0x25: return array('rgb' => '800000');
6806
			case 0x26: return array('rgb' => '008080');
6807
			case 0x27: return array('rgb' => '0000FF');
6808
			case 0x28: return array('rgb' => '00CCFF');
6809
			case 0x29: return array('rgb' => 'CCFFFF');
6810
			case 0x2A: return array('rgb' => 'CCFFCC');
6811
			case 0x2B: return array('rgb' => 'FFFF99');
6812
			case 0x2C: return array('rgb' => '99CCFF');
6813
			case 0x2D: return array('rgb' => 'FF99CC');
6814
			case 0x2E: return array('rgb' => 'CC99FF');
6815
			case 0x2F: return array('rgb' => 'FFCC99');
6816
			case 0x30: return array('rgb' => '3366FF');
6817
			case 0x31: return array('rgb' => '33CCCC');
6818
			case 0x32: return array('rgb' => '99CC00');
6819
			case 0x33: return array('rgb' => 'FFCC00');
6820
			case 0x34: return array('rgb' => 'FF9900');
6821
			case 0x35: return array('rgb' => 'FF6600');
6822
			case 0x36: return array('rgb' => '666699');
6823
			case 0x37: return array('rgb' => '969696');
6824
			case 0x38: return array('rgb' => '003366');
6825
			case 0x39: return array('rgb' => '339966');
6826
			case 0x3A: return array('rgb' => '003300');
6827
			case 0x3B: return array('rgb' => '333300');
6828
			case 0x3C: return array('rgb' => '993300');
6829
			case 0x3D: return array('rgb' => '993366');
6830
			case 0x3E: return array('rgb' => '333399');
6831
			case 0x3F: return array('rgb' => '333333');
6832
			default:   return array('rgb' => '000000');
6833
		}
6834
	}
6835
 
6836
 
6837
	private function _parseRichText($is = '') {
6838
		$value = new PHPExcel_RichText();
6839
 
6840
		$value->createText($is);
6841
 
6842
		return $value;
6843
	}
6844
 
6845
}