1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Reader; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\NamedRange; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Gnumeric\PageSetup; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner; |
10
|
|
|
use PhpOffice\PhpSpreadsheet\ReferenceHelper; |
11
|
|
|
use PhpOffice\PhpSpreadsheet\RichText\RichText; |
12
|
|
|
use PhpOffice\PhpSpreadsheet\Settings; |
13
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\Date; |
14
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\File; |
15
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
16
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Alignment; |
17
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Border; |
18
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Borders; |
19
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Fill; |
20
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Font; |
21
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
22
|
|
|
use SimpleXMLElement; |
23
|
|
|
use XMLReader; |
24
|
|
|
|
25
|
|
|
class Gnumeric extends BaseReader |
26
|
|
|
{ |
27
|
|
|
private const UOM_CONVERSION_POINTS_TO_CENTIMETERS = 0.03527777778; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Shared Expressions. |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $expressions = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
5 |
|
* Spreadsheet shared across all functions. |
38
|
|
|
* |
39
|
5 |
|
* @var Spreadsheet |
40
|
5 |
|
*/ |
41
|
5 |
|
private $spreadsheet; |
42
|
5 |
|
|
43
|
|
|
private $referenceHelper; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Namespace shared across all functions. |
47
|
|
|
* It is 'gnm', except for really old sheets which use 'gmr'. |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
4 |
|
private $gnm = 'gnm'; |
52
|
|
|
|
53
|
4 |
|
/** |
54
|
|
|
* Create a new Gnumeric. |
55
|
|
|
*/ |
56
|
4 |
|
public function __construct() |
57
|
|
|
{ |
58
|
|
|
parent::__construct(); |
59
|
|
|
$this->referenceHelper = ReferenceHelper::getInstance(); |
60
|
|
|
$this->securityScanner = XmlScanner::getInstance($this); |
61
|
4 |
|
} |
62
|
4 |
|
|
63
|
4 |
|
/** |
64
|
|
|
* Can the current IReader read the file? |
65
|
4 |
|
* |
66
|
|
|
* @param string $pFilename |
67
|
|
|
* |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
public function canRead($pFilename) |
71
|
|
|
{ |
72
|
|
|
File::assertFile($pFilename); |
73
|
|
|
|
74
|
|
|
// Check if gzlib functions are available |
75
|
|
|
$data = ''; |
76
|
|
|
if (function_exists('gzread')) { |
77
|
|
|
// Read signature data (first 3 bytes) |
78
|
|
|
$fh = fopen($pFilename, 'rb'); |
79
|
|
|
$data = fread($fh, 2); |
|
|
|
|
80
|
|
|
fclose($fh); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $data == chr(0x1F) . chr(0x8B); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private static function matchXml(string $name, string $field): bool |
87
|
|
|
{ |
88
|
|
|
return 1 === preg_match("/^(gnm|gmr):$field$/", $name); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Reads names of the worksheets from a file, without parsing the whole file to a Spreadsheet object. |
93
|
|
|
* |
94
|
|
|
* @param string $pFilename |
95
|
|
|
* |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
public function listWorksheetNames($pFilename) |
99
|
|
|
{ |
100
|
|
|
File::assertFile($pFilename); |
101
|
|
|
|
102
|
|
|
$xml = new XMLReader(); |
103
|
|
|
$xml->xml($this->securityScanner->scanFile('compress.zlib://' . realpath($pFilename)), null, Settings::getLibXmlLoaderOptions()); |
104
|
|
|
$xml->setParserProperty(2, true); |
105
|
|
|
|
106
|
|
|
$worksheetNames = []; |
107
|
|
|
while ($xml->read()) { |
108
|
|
|
if (self::matchXml($xml->name, 'SheetName') && $xml->nodeType == XMLReader::ELEMENT) { |
109
|
|
|
$xml->read(); // Move onto the value node |
110
|
|
|
$worksheetNames[] = (string) $xml->value; |
111
|
|
|
} elseif (self::matchXml($xml->name, 'Sheets')) { |
112
|
|
|
// break out of the loop once we've got our sheet names rather than parse the entire file |
113
|
|
|
break; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $worksheetNames; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns). |
122
|
|
|
* |
123
|
|
|
* @param string $pFilename |
124
|
|
|
* |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
public function listWorksheetInfo($pFilename) |
128
|
|
|
{ |
129
|
|
|
File::assertFile($pFilename); |
130
|
|
|
|
131
|
|
|
$xml = new XMLReader(); |
132
|
|
|
$xml->xml($this->securityScanner->scanFile('compress.zlib://' . realpath($pFilename)), null, Settings::getLibXmlLoaderOptions()); |
133
|
|
|
$xml->setParserProperty(2, true); |
134
|
|
|
|
135
|
|
|
$worksheetInfo = []; |
136
|
|
|
while ($xml->read()) { |
137
|
|
|
if (self::matchXml($xml->name, 'Sheet') && $xml->nodeType == XMLReader::ELEMENT) { |
138
|
|
|
$tmpInfo = [ |
139
|
|
|
'worksheetName' => '', |
140
|
|
|
'lastColumnLetter' => 'A', |
141
|
|
|
'lastColumnIndex' => 0, |
142
|
|
|
'totalRows' => 0, |
143
|
|
|
'totalColumns' => 0, |
144
|
|
|
]; |
145
|
|
|
|
146
|
|
|
while ($xml->read()) { |
147
|
|
|
if ($xml->nodeType == XMLReader::ELEMENT) { |
148
|
|
|
if (self::matchXml($xml->name, 'Name')) { |
149
|
|
|
$xml->read(); // Move onto the value node |
150
|
|
|
$tmpInfo['worksheetName'] = (string) $xml->value; |
151
|
2 |
|
} elseif (self::matchXml($xml->name, 'MaxCol')) { |
152
|
|
|
$xml->read(); // Move onto the value node |
153
|
2 |
|
$tmpInfo['lastColumnIndex'] = (int) $xml->value; |
154
|
2 |
|
$tmpInfo['totalColumns'] = (int) $xml->value + 1; |
155
|
2 |
|
} elseif (self::matchXml($xml->name, 'MaxRow')) { |
156
|
2 |
|
$xml->read(); // Move onto the value node |
157
|
2 |
|
$tmpInfo['totalRows'] = (int) $xml->value + 1; |
158
|
|
|
|
159
|
2 |
|
break; |
160
|
|
|
} |
161
|
|
|
} |
162
|
2 |
|
} |
163
|
|
|
$tmpInfo['lastColumnLetter'] = Coordinate::stringFromColumnIndex($tmpInfo['lastColumnIndex'] + 1); |
164
|
|
|
$worksheetInfo[] = $tmpInfo; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $worksheetInfo; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
2 |
|
* @param string $filename |
173
|
|
|
* |
174
|
|
|
* @return string |
175
|
2 |
|
*/ |
176
|
|
|
private function gzfileGetContents($filename) |
177
|
|
|
{ |
178
|
2 |
|
$file = @gzopen($filename, 'rb'); |
179
|
|
|
$data = ''; |
180
|
|
|
if ($file !== false) { |
181
|
|
|
while (!gzeof($file)) { |
182
|
|
|
$data .= gzread($file, 1024); |
183
|
|
|
} |
184
|
|
|
gzclose($file); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $data; |
188
|
|
|
} |
189
|
2 |
|
|
190
|
|
|
private static $mappings = [ |
191
|
2 |
|
'borderStyle' => [ |
192
|
|
|
'0' => Border::BORDER_NONE, |
193
|
2 |
|
'1' => Border::BORDER_THIN, |
194
|
|
|
'2' => Border::BORDER_MEDIUM, |
195
|
2 |
|
'3' => Border::BORDER_SLANTDASHDOT, |
196
|
2 |
|
'4' => Border::BORDER_DASHED, |
197
|
|
|
'5' => Border::BORDER_THICK, |
198
|
2 |
|
'6' => Border::BORDER_DOUBLE, |
199
|
|
|
'7' => Border::BORDER_DOTTED, |
200
|
2 |
|
'8' => Border::BORDER_MEDIUMDASHED, |
201
|
|
|
'9' => Border::BORDER_DASHDOT, |
202
|
2 |
|
'10' => Border::BORDER_MEDIUMDASHDOT, |
203
|
2 |
|
'11' => Border::BORDER_DASHDOTDOT, |
204
|
2 |
|
'12' => Border::BORDER_MEDIUMDASHDOTDOT, |
205
|
2 |
|
'13' => Border::BORDER_MEDIUMDASHDOTDOT, |
206
|
|
|
], |
207
|
2 |
|
'dataType' => [ |
208
|
2 |
|
'10' => DataType::TYPE_NULL, |
209
|
2 |
|
'20' => DataType::TYPE_BOOL, |
210
|
2 |
|
'30' => DataType::TYPE_NUMERIC, // Integer doesn't exist in Excel |
211
|
|
|
'40' => DataType::TYPE_NUMERIC, // Float |
212
|
2 |
|
'50' => DataType::TYPE_ERROR, |
213
|
2 |
|
'60' => DataType::TYPE_STRING, |
214
|
|
|
//'70': // Cell Range |
215
|
2 |
|
//'80': // Array |
216
|
2 |
|
], |
217
|
|
|
'fillType' => [ |
218
|
2 |
|
'1' => Fill::FILL_SOLID, |
219
|
2 |
|
'2' => Fill::FILL_PATTERN_DARKGRAY, |
220
|
2 |
|
'3' => Fill::FILL_PATTERN_MEDIUMGRAY, |
221
|
|
|
'4' => Fill::FILL_PATTERN_LIGHTGRAY, |
222
|
2 |
|
'5' => Fill::FILL_PATTERN_GRAY125, |
223
|
2 |
|
'6' => Fill::FILL_PATTERN_GRAY0625, |
224
|
2 |
|
'7' => Fill::FILL_PATTERN_DARKHORIZONTAL, // horizontal stripe |
225
|
2 |
|
'8' => Fill::FILL_PATTERN_DARKVERTICAL, // vertical stripe |
226
|
|
|
'9' => Fill::FILL_PATTERN_DARKDOWN, // diagonal stripe |
227
|
2 |
|
'10' => Fill::FILL_PATTERN_DARKUP, // reverse diagonal stripe |
228
|
2 |
|
'11' => Fill::FILL_PATTERN_DARKGRID, // diagoanl crosshatch |
229
|
2 |
|
'12' => Fill::FILL_PATTERN_DARKTRELLIS, // thick diagonal crosshatch |
230
|
2 |
|
'13' => Fill::FILL_PATTERN_LIGHTHORIZONTAL, |
231
|
2 |
|
'14' => Fill::FILL_PATTERN_LIGHTVERTICAL, |
232
|
|
|
'15' => Fill::FILL_PATTERN_LIGHTUP, |
233
|
2 |
|
'16' => Fill::FILL_PATTERN_LIGHTDOWN, |
234
|
2 |
|
'17' => Fill::FILL_PATTERN_LIGHTGRID, // thin horizontal crosshatch |
235
|
2 |
|
'18' => Fill::FILL_PATTERN_LIGHTTRELLIS, // thin diagonal crosshatch |
236
|
|
|
], |
237
|
2 |
|
'horizontal' => [ |
238
|
|
|
'1' => Alignment::HORIZONTAL_GENERAL, |
239
|
|
|
'2' => Alignment::HORIZONTAL_LEFT, |
240
|
2 |
|
'4' => Alignment::HORIZONTAL_RIGHT, |
241
|
2 |
|
'8' => Alignment::HORIZONTAL_CENTER, |
242
|
2 |
|
'16' => Alignment::HORIZONTAL_CENTER_CONTINUOUS, |
243
|
|
|
'32' => Alignment::HORIZONTAL_JUSTIFY, |
244
|
2 |
|
'64' => Alignment::HORIZONTAL_CENTER_CONTINUOUS, |
245
|
2 |
|
], |
246
|
2 |
|
'underline' => [ |
247
|
|
|
'1' => Font::UNDERLINE_SINGLE, |
248
|
2 |
|
'2' => Font::UNDERLINE_DOUBLE, |
249
|
2 |
|
'3' => Font::UNDERLINE_SINGLEACCOUNTING, |
250
|
|
|
'4' => Font::UNDERLINE_DOUBLEACCOUNTING, |
251
|
2 |
|
], |
252
|
2 |
|
'vertical' => [ |
253
|
2 |
|
'1' => Alignment::VERTICAL_TOP, |
254
|
2 |
|
'2' => Alignment::VERTICAL_BOTTOM, |
255
|
|
|
'4' => Alignment::VERTICAL_CENTER, |
256
|
2 |
|
'8' => Alignment::VERTICAL_JUSTIFY, |
257
|
2 |
|
], |
258
|
2 |
|
]; |
259
|
2 |
|
|
260
|
2 |
|
public static function gnumericMappings(): array |
261
|
|
|
{ |
262
|
2 |
|
return self::$mappings; |
263
|
2 |
|
} |
264
|
2 |
|
|
265
|
|
|
private function docPropertiesOld(SimpleXMLElement $gnmXML): void |
266
|
2 |
|
{ |
267
|
2 |
|
$docProps = $this->spreadsheet->getProperties(); |
268
|
|
|
foreach ($gnmXML->Summary->Item as $summaryItem) { |
269
|
2 |
|
$propertyName = $summaryItem->name; |
270
|
2 |
|
$propertyValue = $summaryItem->{'val-string'}; |
271
|
2 |
|
switch ($propertyName) { |
272
|
|
|
case 'title': |
273
|
2 |
|
$docProps->setTitle(trim($propertyValue)); |
274
|
2 |
|
|
275
|
2 |
|
break; |
276
|
|
|
case 'comments': |
277
|
2 |
|
$docProps->setDescription(trim($propertyValue)); |
278
|
|
|
|
279
|
|
|
break; |
280
|
2 |
|
case 'keywords': |
281
|
|
|
$docProps->setKeywords(trim($propertyValue)); |
282
|
|
|
|
283
|
|
|
break; |
284
|
|
|
case 'category': |
285
|
|
|
$docProps->setCategory(trim($propertyValue)); |
286
|
|
|
|
287
|
|
|
break; |
288
|
|
|
case 'manager': |
289
|
|
|
$docProps->setManager(trim($propertyValue)); |
290
|
|
|
|
291
|
|
|
break; |
292
|
|
|
case 'author': |
293
|
|
|
$docProps->setCreator(trim($propertyValue)); |
294
|
|
|
$docProps->setLastModifiedBy(trim($propertyValue)); |
295
|
|
|
|
296
|
|
|
break; |
297
|
|
|
case 'company': |
298
|
|
|
$docProps->setCompany(trim($propertyValue)); |
299
|
|
|
|
300
|
|
|
break; |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
private function docPropertiesDC(SimpleXMLElement $officePropertyDC): void |
306
|
|
|
{ |
307
|
|
|
$docProps = $this->spreadsheet->getProperties(); |
308
|
|
|
foreach ($officePropertyDC as $propertyName => $propertyValue) { |
309
|
|
|
$propertyValue = trim((string) $propertyValue); |
310
|
|
|
switch ($propertyName) { |
311
|
|
|
case 'title': |
312
|
|
|
$docProps->setTitle($propertyValue); |
313
|
|
|
|
314
|
|
|
break; |
315
|
|
|
case 'subject': |
316
|
|
|
$docProps->setSubject($propertyValue); |
317
|
|
|
|
318
|
|
|
break; |
319
|
|
|
case 'creator': |
320
|
|
|
$docProps->setCreator($propertyValue); |
321
|
|
|
$docProps->setLastModifiedBy($propertyValue); |
322
|
2 |
|
|
323
|
2 |
|
break; |
324
|
2 |
|
case 'date': |
325
|
2 |
|
$creationDate = strtotime($propertyValue); |
326
|
|
|
$docProps->setCreated($creationDate); |
327
|
|
|
$docProps->setModified($creationDate); |
328
|
|
|
|
329
|
2 |
|
break; |
330
|
|
|
case 'description': |
331
|
|
|
$docProps->setDescription($propertyValue); |
332
|
2 |
|
|
333
|
2 |
|
break; |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
} |
337
|
2 |
|
|
338
|
|
|
private function docPropertiesMeta(SimpleXMLElement $officePropertyMeta, array $namespacesMeta): void |
339
|
2 |
|
{ |
340
|
2 |
|
$docProps = $this->spreadsheet->getProperties(); |
341
|
2 |
|
foreach ($officePropertyMeta as $propertyName => $propertyValue) { |
342
|
2 |
|
$attributes = $propertyValue->attributes($namespacesMeta['meta']); |
343
|
2 |
|
$propertyValue = trim((string) $propertyValue); |
344
|
2 |
|
switch ($propertyName) { |
345
|
2 |
|
case 'keyword': |
346
|
2 |
|
$docProps->setKeywords($propertyValue); |
347
|
|
|
|
348
|
2 |
|
break; |
349
|
|
|
case 'initial-creator': |
350
|
|
|
$docProps->setCreator($propertyValue); |
351
|
2 |
|
$docProps->setLastModifiedBy($propertyValue); |
352
|
2 |
|
|
353
|
|
|
break; |
354
|
2 |
|
case 'creation-date': |
355
|
2 |
|
$creationDate = strtotime($propertyValue); |
356
|
2 |
|
$docProps->setCreated($creationDate); |
357
|
|
|
$docProps->setModified($creationDate); |
358
|
2 |
|
|
359
|
2 |
|
break; |
360
|
2 |
|
case 'user-defined': |
361
|
|
|
[, $attrName] = explode(':', $attributes['name']); |
362
|
2 |
|
switch ($attrName) { |
363
|
2 |
|
case 'publisher': |
364
|
2 |
|
$docProps->setCompany($propertyValue); |
365
|
|
|
|
366
|
2 |
|
break; |
367
|
2 |
|
case 'category': |
368
|
2 |
|
$docProps->setCategory($propertyValue); |
369
|
|
|
|
370
|
2 |
|
break; |
371
|
2 |
|
case 'manager': |
372
|
2 |
|
$docProps->setManager($propertyValue); |
373
|
|
|
|
374
|
2 |
|
break; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
break; |
378
|
|
|
} |
379
|
|
|
} |
380
|
2 |
|
} |
381
|
2 |
|
|
382
|
2 |
|
private function docProperties(SimpleXMLElement $xml, SimpleXMLElement $gnmXML, array $namespacesMeta): void |
383
|
2 |
|
{ |
384
|
|
|
if (isset($namespacesMeta['office'])) { |
385
|
2 |
|
$officeXML = $xml->children($namespacesMeta['office']); |
386
|
2 |
|
$officeDocXML = $officeXML->{'document-meta'}; |
387
|
|
|
$officeDocMetaXML = $officeDocXML->meta; |
388
|
2 |
|
|
389
|
2 |
|
foreach ($officeDocMetaXML as $officePropertyData) { |
390
|
|
|
$officePropertyDC = []; |
391
|
|
|
if (isset($namespacesMeta['dc'])) { |
392
|
2 |
|
$officePropertyDC = $officePropertyData->children($namespacesMeta['dc']); |
393
|
|
|
} |
394
|
|
|
$this->docPropertiesDC($officePropertyDC); |
395
|
2 |
|
|
396
|
2 |
|
$officePropertyMeta = []; |
397
|
|
|
if (isset($namespacesMeta['meta'])) { |
398
|
|
|
$officePropertyMeta = $officePropertyData->children($namespacesMeta['meta']); |
399
|
|
|
} |
400
|
|
|
$this->docPropertiesMeta($officePropertyMeta, $namespacesMeta); |
401
|
2 |
|
} |
402
|
2 |
|
} elseif (isset($gnmXML->Summary)) { |
403
|
2 |
|
$this->docPropertiesOld($gnmXML); |
404
|
2 |
|
} |
405
|
2 |
|
} |
406
|
2 |
|
|
407
|
2 |
|
private function processComments(SimpleXMLElement $sheet): void |
408
|
2 |
|
{ |
409
|
2 |
|
if ((!$this->readDataOnly) && (isset($sheet->Objects))) { |
410
|
|
|
foreach ($sheet->Objects->children($this->gnm, true) as $key => $comment) { |
411
|
|
|
$commentAttributes = $comment->attributes(); |
412
|
2 |
|
// Only comment objects are handled at the moment |
413
|
|
|
if ($commentAttributes->Text) { |
414
|
2 |
|
$this->spreadsheet->getActiveSheet()->getComment((string) $commentAttributes->ObjectBound)->setAuthor((string) $commentAttributes->Author)->setText($this->parseRichText((string) $commentAttributes->Text)); |
415
|
2 |
|
} |
416
|
2 |
|
} |
417
|
2 |
|
} |
418
|
2 |
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* Loads Spreadsheet from file. |
422
|
2 |
|
* |
423
|
|
|
* @param string $pFilename |
424
|
|
|
* |
425
|
2 |
|
* @return Spreadsheet |
426
|
|
|
*/ |
427
|
|
|
public function load($pFilename) |
428
|
|
|
{ |
429
|
2 |
|
// Create new Spreadsheet |
430
|
2 |
|
$spreadsheet = new Spreadsheet(); |
431
|
2 |
|
$spreadsheet->removeSheetByIndex(0); |
432
|
|
|
|
433
|
2 |
|
// Load into this instance |
434
|
2 |
|
return $this->loadIntoExisting($pFilename, $spreadsheet); |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
/** |
438
|
2 |
|
* Loads from file into Spreadsheet instance. |
439
|
2 |
|
*/ |
440
|
|
|
public function loadIntoExisting(string $pFilename, Spreadsheet $spreadsheet): Spreadsheet |
441
|
2 |
|
{ |
442
|
2 |
|
$this->spreadsheet = $spreadsheet; |
443
|
|
|
File::assertFile($pFilename); |
444
|
|
|
|
445
|
|
|
$gFileData = $this->gzfileGetContents($pFilename); |
446
|
2 |
|
|
447
|
2 |
|
$xml2 = simplexml_load_string($this->securityScanner->scan($gFileData), 'SimpleXMLElement', Settings::getLibXmlLoaderOptions()); |
448
|
|
|
$xml = ($xml2 !== false) ? $xml2 : new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><root></root>'); |
449
|
2 |
|
$namespacesMeta = $xml->getNamespaces(true); |
450
|
2 |
|
$this->gnm = array_key_exists('gmr', $namespacesMeta) ? 'gmr' : 'gnm'; |
451
|
2 |
|
|
452
|
|
|
$gnmXML = $xml->children($namespacesMeta[$this->gnm]); |
453
|
|
|
$this->docProperties($xml, $gnmXML, $namespacesMeta); |
454
|
2 |
|
|
455
|
|
|
$worksheetID = 0; |
456
|
|
|
foreach ($gnmXML->Sheets->Sheet as $sheet) { |
457
|
2 |
|
$worksheetName = (string) $sheet->Name; |
458
|
2 |
|
if ((isset($this->loadSheetsOnly)) && (!in_array($worksheetName, $this->loadSheetsOnly))) { |
459
|
2 |
|
continue; |
460
|
|
|
} |
461
|
2 |
|
|
462
|
2 |
|
$maxRow = $maxCol = 0; |
463
|
|
|
|
464
|
|
|
// Create new Worksheet |
465
|
|
|
$this->spreadsheet->createSheet(); |
466
|
2 |
|
$this->spreadsheet->setActiveSheetIndex($worksheetID); |
467
|
2 |
|
// Use false for $updateFormulaCellReferences to prevent adjustment of worksheet references in formula |
468
|
2 |
|
// cells... during the load, all formulae should be correct, and we're simply bringing the worksheet |
469
|
2 |
|
// name in line with the formula, not the reverse |
470
|
2 |
|
$this->spreadsheet->getActiveSheet()->setTitle($worksheetName, false, false); |
471
|
2 |
|
|
472
|
|
|
if (!$this->readDataOnly) { |
473
|
2 |
|
(new PageSetup($this->spreadsheet, $this->gnm)) |
474
|
2 |
|
->printInformation($sheet) |
475
|
2 |
|
->sheetMargins($sheet); |
476
|
2 |
|
} |
477
|
2 |
|
|
478
|
|
|
foreach ($sheet->Cells->Cell as $cell) { |
479
|
2 |
|
$cellAttributes = $cell->attributes(); |
480
|
|
|
$row = (int) $cellAttributes->Row + 1; |
481
|
|
|
$column = (int) $cellAttributes->Col; |
482
|
2 |
|
|
483
|
2 |
|
if ($row > $maxRow) { |
484
|
2 |
|
$maxRow = $row; |
485
|
2 |
|
} |
486
|
|
|
if ($column > $maxCol) { |
487
|
2 |
|
$maxCol = $column; |
488
|
2 |
|
} |
489
|
2 |
|
|
490
|
2 |
|
$column = Coordinate::stringFromColumnIndex($column + 1); |
491
|
|
|
|
492
|
2 |
|
// Read cell? |
493
|
2 |
|
if ($this->getReadFilter() !== null) { |
494
|
2 |
|
if (!$this->getReadFilter()->readCell($column, $row, $worksheetName)) { |
495
|
|
|
continue; |
496
|
2 |
|
} |
497
|
2 |
|
} |
498
|
2 |
|
|
499
|
|
|
$ValueType = $cellAttributes->ValueType; |
500
|
2 |
|
$ExprID = (string) $cellAttributes->ExprID; |
501
|
2 |
|
$type = DataType::TYPE_FORMULA; |
502
|
2 |
|
if ($ExprID > '') { |
503
|
|
|
if (((string) $cell) > '') { |
504
|
2 |
|
$this->expressions[$ExprID] = [ |
505
|
|
|
'column' => $cellAttributes->Col, |
506
|
|
|
'row' => $cellAttributes->Row, |
507
|
|
|
'formula' => (string) $cell, |
508
|
|
|
]; |
509
|
|
|
} else { |
510
|
|
|
$expression = $this->expressions[$ExprID]; |
511
|
|
|
|
512
|
|
|
$cell = $this->referenceHelper->updateFormulaReferences( |
513
|
|
|
$expression['formula'], |
514
|
|
|
'A1', |
515
|
|
|
$cellAttributes->Col - $expression['column'], |
516
|
2 |
|
$cellAttributes->Row - $expression['row'], |
517
|
2 |
|
$worksheetName |
518
|
2 |
|
); |
519
|
|
|
} |
520
|
2 |
|
$type = DataType::TYPE_FORMULA; |
521
|
2 |
|
} else { |
522
|
2 |
|
$vtype = (string) $ValueType; |
523
|
|
|
if (array_key_exists($vtype, self::$mappings['dataType'])) { |
524
|
2 |
|
$type = self::$mappings['dataType'][$vtype]; |
525
|
2 |
|
} |
526
|
2 |
|
if ($vtype == '20') { // Boolean |
527
|
|
|
$cell = $cell == 'TRUE'; |
528
|
2 |
|
} |
529
|
|
|
} |
530
|
|
|
$this->spreadsheet->getActiveSheet()->getCell($column . $row)->setValueExplicit((string) $cell, $type); |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
$this->processComments($sheet); |
534
|
|
|
|
535
|
2 |
|
foreach ($sheet->Styles->StyleRegion as $styleRegion) { |
536
|
2 |
|
$styleAttributes = $styleRegion->attributes(); |
537
|
2 |
|
if (($styleAttributes['startRow'] <= $maxRow) && |
538
|
|
|
($styleAttributes['startCol'] <= $maxCol)) { |
539
|
2 |
|
$startColumn = Coordinate::stringFromColumnIndex((int) $styleAttributes['startCol'] + 1); |
540
|
2 |
|
$startRow = $styleAttributes['startRow'] + 1; |
541
|
2 |
|
|
542
|
2 |
|
$endColumn = ($styleAttributes['endCol'] > $maxCol) ? $maxCol : (int) $styleAttributes['endCol']; |
543
|
2 |
|
$endColumn = Coordinate::stringFromColumnIndex($endColumn + 1); |
544
|
2 |
|
$endRow = 1 + (($styleAttributes['endRow'] > $maxRow) ? $maxRow : $styleAttributes['endRow']); |
545
|
2 |
|
$cellRange = $startColumn . $startRow . ':' . $endColumn . $endRow; |
546
|
2 |
|
|
547
|
|
|
$styleAttributes = $styleRegion->Style->attributes(); |
548
|
2 |
|
|
549
|
2 |
|
$styleArray = []; |
550
|
|
|
// We still set the number format mask for date/time values, even if readDataOnly is true |
551
|
2 |
|
$formatCode = (string) $styleAttributes['Format']; |
552
|
2 |
|
if (Date::isDateTimeFormatCode($formatCode)) { |
553
|
|
|
$styleArray['numberFormat']['formatCode'] = $formatCode; |
554
|
|
|
} |
555
|
|
|
if (!$this->readDataOnly) { |
556
|
2 |
|
// If readDataOnly is false, we set all formatting information |
557
|
|
|
$styleArray['numberFormat']['formatCode'] = $formatCode; |
558
|
|
|
|
559
|
|
|
self::addStyle2($styleArray, 'alignment', 'horizontal', $styleAttributes['HAlign']); |
560
|
2 |
|
self::addStyle2($styleArray, 'alignment', 'vertical', $styleAttributes['VAlign']); |
561
|
|
|
$styleArray['alignment']['wrapText'] = $styleAttributes['WrapText'] == '1'; |
562
|
|
|
$styleArray['alignment']['textRotation'] = $this->calcRotation($styleAttributes); |
563
|
|
|
$styleArray['alignment']['shrinkToFit'] = $styleAttributes['ShrinkToFit'] == '1'; |
564
|
2 |
|
$styleArray['alignment']['indent'] = ((int) ($styleAttributes['Indent']) > 0) ? $styleAttributes['indent'] : 0; |
565
|
|
|
|
566
|
|
|
$this->addColors($styleArray, $styleAttributes); |
567
|
|
|
|
568
|
2 |
|
$fontAttributes = $styleRegion->Style->Font->attributes(); |
569
|
2 |
|
$styleArray['font']['name'] = (string) $styleRegion->Style->Font; |
570
|
|
|
$styleArray['font']['size'] = (int) ($fontAttributes['Unit']); |
571
|
2 |
|
$styleArray['font']['bold'] = $fontAttributes['Bold'] == '1'; |
572
|
2 |
|
$styleArray['font']['italic'] = $fontAttributes['Italic'] == '1'; |
573
|
2 |
|
$styleArray['font']['strikethrough'] = $fontAttributes['StrikeThrough'] == '1'; |
574
|
|
|
self::addStyle2($styleArray, 'font', 'underline', $fontAttributes['Underline']); |
575
|
2 |
|
|
576
|
2 |
|
switch ($fontAttributes['Script']) { |
577
|
|
|
case '1': |
578
|
|
|
$styleArray['font']['superscript'] = true; |
579
|
|
|
|
580
|
2 |
|
break; |
581
|
|
|
case '-1': |
582
|
|
|
$styleArray['font']['subscript'] = true; |
583
|
|
|
|
584
|
2 |
|
break; |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
if (isset($styleRegion->Style->StyleBorder)) { |
588
|
2 |
|
$srssb = $styleRegion->Style->StyleBorder; |
589
|
|
|
$this->addBorderStyle($srssb, $styleArray, 'top'); |
590
|
|
|
$this->addBorderStyle($srssb, $styleArray, 'bottom'); |
591
|
|
|
$this->addBorderStyle($srssb, $styleArray, 'left'); |
592
|
2 |
|
$this->addBorderStyle($srssb, $styleArray, 'right'); |
593
|
|
|
$this->addBorderDiagonal($srssb, $styleArray); |
594
|
|
|
} |
595
|
|
|
if (isset($styleRegion->Style->HyperLink)) { |
596
|
2 |
|
// TO DO |
597
|
|
|
$hyperlink = $styleRegion->Style->HyperLink->attributes(); |
|
|
|
|
598
|
|
|
} |
599
|
|
|
} |
600
|
2 |
|
$this->spreadsheet->getActiveSheet()->getStyle($cellRange)->applyFromArray($styleArray); |
601
|
|
|
} |
602
|
|
|
} |
603
|
|
|
|
604
|
2 |
|
$this->processColumnWidths($sheet, $maxCol); |
605
|
|
|
$this->processRowHeights($sheet, $maxRow); |
606
|
|
|
$this->processMergedCells($sheet); |
607
|
|
|
|
608
|
2 |
|
++$worksheetID; |
609
|
|
|
} |
610
|
|
|
|
611
|
|
|
$this->processDefinedNames($gnmXML); |
612
|
2 |
|
|
613
|
|
|
// Return |
614
|
|
|
return $this->spreadsheet; |
615
|
|
|
} |
616
|
2 |
|
|
617
|
|
|
private function addBorderDiagonal(SimpleXMLElement $srssb, array &$styleArray): void |
618
|
|
|
{ |
619
|
|
|
if (isset($srssb->Diagonal, $srssb->{'Rev-Diagonal'})) { |
620
|
2 |
|
$styleArray['borders']['diagonal'] = self::parseBorderAttributes($srssb->Diagonal->attributes()); |
621
|
|
|
$styleArray['borders']['diagonalDirection'] = Borders::DIAGONAL_BOTH; |
622
|
|
|
} elseif (isset($srssb->Diagonal)) { |
623
|
|
|
$styleArray['borders']['diagonal'] = self::parseBorderAttributes($srssb->Diagonal->attributes()); |
624
|
2 |
|
$styleArray['borders']['diagonalDirection'] = Borders::DIAGONAL_UP; |
625
|
|
|
} elseif (isset($srssb->{'Rev-Diagonal'})) { |
626
|
|
|
$styleArray['borders']['diagonal'] = self::parseBorderAttributes($srssb->{'Rev-Diagonal'}->attributes()); |
627
|
|
|
$styleArray['borders']['diagonalDirection'] = Borders::DIAGONAL_DOWN; |
628
|
|
|
} |
629
|
|
|
} |
630
|
|
|
|
631
|
2 |
|
private function addBorderStyle(SimpleXMLElement $srssb, array &$styleArray, string $direction): void |
632
|
2 |
|
{ |
633
|
2 |
|
$ucDirection = ucfirst($direction); |
634
|
2 |
|
if (isset($srssb->$ucDirection)) { |
635
|
2 |
|
$styleArray['borders'][$direction] = self::parseBorderAttributes($srssb->$ucDirection->attributes()); |
636
|
2 |
|
} |
637
|
2 |
|
} |
638
|
2 |
|
|
639
|
2 |
|
private function processMergedCells(SimpleXMLElement $sheet): void |
640
|
|
|
{ |
641
|
2 |
|
// Handle Merged Cells in this worksheet |
642
|
2 |
|
if (isset($sheet->MergedRegions)) { |
643
|
2 |
|
foreach ($sheet->MergedRegions->Merge as $mergeCells) { |
644
|
|
|
if (strpos($mergeCells, ':') !== false) { |
645
|
2 |
|
$this->spreadsheet->getActiveSheet()->mergeCells($mergeCells); |
646
|
2 |
|
} |
647
|
2 |
|
} |
648
|
|
|
} |
649
|
2 |
|
} |
650
|
2 |
|
|
651
|
2 |
|
private function processColumnLoop(int $c, int $maxCol, SimpleXMLElement $columnOverride, float $defaultWidth): int |
652
|
|
|
{ |
653
|
2 |
|
$columnAttributes = $columnOverride->attributes(); |
654
|
|
|
$column = $columnAttributes['No']; |
655
|
2 |
|
$columnWidth = ((float) $columnAttributes['Unit']) / 5.4; |
656
|
|
|
$hidden = (isset($columnAttributes['Hidden'])) && ((string) $columnAttributes['Hidden'] == '1'); |
657
|
2 |
|
$columnCount = (isset($columnAttributes['Count'])) ? $columnAttributes['Count'] : 1; |
658
|
|
|
while ($c < $column) { |
659
|
2 |
|
$this->spreadsheet->getActiveSheet()->getColumnDimension(Coordinate::stringFromColumnIndex($c + 1))->setWidth($defaultWidth); |
660
|
2 |
|
++$c; |
661
|
2 |
|
} |
662
|
|
|
while (($c < ($column + $columnCount)) && ($c <= $maxCol)) { |
663
|
2 |
|
$this->spreadsheet->getActiveSheet()->getColumnDimension(Coordinate::stringFromColumnIndex($c + 1))->setWidth($columnWidth); |
664
|
2 |
|
if ($hidden) { |
665
|
2 |
|
$this->spreadsheet->getActiveSheet()->getColumnDimension(Coordinate::stringFromColumnIndex($c + 1))->setVisible(false); |
666
|
|
|
} |
667
|
2 |
|
++$c; |
668
|
|
|
} |
669
|
|
|
|
670
|
2 |
|
return $c; |
671
|
2 |
|
} |
672
|
2 |
|
|
673
|
|
|
private function processColumnWidths(SimpleXMLElement $sheet, int $maxCol): void |
674
|
2 |
|
{ |
675
|
2 |
|
if ((!$this->readDataOnly) && (isset($sheet->Cols))) { |
676
|
|
|
// Column Widths |
677
|
2 |
|
$columnAttributes = $sheet->Cols->attributes(); |
678
|
2 |
|
$defaultWidth = $columnAttributes['DefaultSizePts'] / 5.4; |
679
|
|
|
$c = 0; |
680
|
2 |
|
foreach ($sheet->Cols->ColInfo as $columnOverride) { |
681
|
2 |
|
$c = $this->processColumnLoop($c, $maxCol, $columnOverride, $defaultWidth); |
682
|
|
|
} |
683
|
2 |
|
while ($c <= $maxCol) { |
684
|
2 |
|
$this->spreadsheet->getActiveSheet()->getColumnDimension(Coordinate::stringFromColumnIndex($c + 1))->setWidth($defaultWidth); |
685
|
2 |
|
++$c; |
686
|
2 |
|
} |
687
|
2 |
|
} |
688
|
2 |
|
} |
689
|
2 |
|
|
690
|
2 |
|
private function processRowLoop(int $r, int $maxRow, SimpleXMLElement $rowOverride, float $defaultHeight): int |
691
|
2 |
|
{ |
692
|
|
|
$rowAttributes = $rowOverride->attributes(); |
693
|
|
|
$row = $rowAttributes['No']; |
694
|
2 |
|
$rowHeight = (float) $rowAttributes['Unit']; |
695
|
|
|
$hidden = (isset($rowAttributes['Hidden'])) && ((string) $rowAttributes['Hidden'] == '1'); |
696
|
2 |
|
$rowCount = (isset($rowAttributes['Count'])) ? $rowAttributes['Count'] : 1; |
697
|
|
|
while ($r < $row) { |
698
|
|
|
++$r; |
699
|
2 |
|
$this->spreadsheet->getActiveSheet()->getRowDimension($r)->setRowHeight($defaultHeight); |
700
|
|
|
} |
701
|
|
|
while (($r < ($row + $rowCount)) && ($r < $maxRow)) { |
702
|
|
|
++$r; |
703
|
|
|
$this->spreadsheet->getActiveSheet()->getRowDimension($r)->setRowHeight($rowHeight); |
704
|
2 |
|
if ($hidden) { |
705
|
|
|
$this->spreadsheet->getActiveSheet()->getRowDimension($r)->setVisible(false); |
706
|
2 |
|
} |
707
|
2 |
|
} |
708
|
2 |
|
|
709
|
2 |
|
return $r; |
710
|
2 |
|
} |
711
|
2 |
|
|
712
|
2 |
|
private function processRowHeights(SimpleXMLElement $sheet, int $maxRow): void |
713
|
2 |
|
{ |
714
|
2 |
|
if ((!$this->readDataOnly) && (isset($sheet->Rows))) { |
715
|
2 |
|
// Row Heights |
716
|
2 |
|
$rowAttributes = $sheet->Rows->attributes(); |
717
|
2 |
|
$defaultHeight = (float) $rowAttributes['DefaultSizePts']; |
718
|
|
|
$r = 0; |
719
|
2 |
|
|
720
|
2 |
|
foreach ($sheet->Rows->RowInfo as $rowOverride) { |
721
|
2 |
|
$r = $this->processRowLoop($r, $maxRow, $rowOverride, $defaultHeight); |
722
|
2 |
|
} |
723
|
|
|
// never executed, I can't figure out any circumstances |
724
|
2 |
|
// under which it would be executed, and, even if |
725
|
|
|
// such exist, I'm not convinced this is needed. |
726
|
|
|
//while ($r < $maxRow) { |
727
|
2 |
|
// ++$r; |
728
|
|
|
// $this->spreadsheet->getActiveSheet()->getRowDimension($r)->setRowHeight($defaultHeight); |
729
|
|
|
//} |
730
|
|
|
} |
731
|
|
|
} |
732
|
|
|
|
733
|
2 |
|
private function processDefinedNames(SimpleXMLElement $gnmXML): void |
734
|
|
|
{ |
735
|
2 |
|
// Loop through definedNames (global named ranges) |
736
|
2 |
|
if (isset($gnmXML->Names)) { |
737
|
2 |
|
foreach ($gnmXML->Names->Name as $namedRange) { |
738
|
|
|
$name = (string) $namedRange->name; |
739
|
2 |
|
$range = (string) $namedRange->value; |
740
|
2 |
|
if (stripos($range, '#REF!') !== false) { |
741
|
2 |
|
continue; |
742
|
2 |
|
} |
743
|
2 |
|
|
744
|
2 |
|
$range = Worksheet::extractSheetTitle($range, true); |
745
|
2 |
|
$range[0] = trim($range[0], "'"); |
746
|
2 |
|
if ($worksheet = $this->spreadsheet->getSheetByName($range[0])) { |
747
|
2 |
|
$extractedRange = str_replace('$', '', $range[1]); |
748
|
|
|
$this->spreadsheet->addNamedRange(new NamedRange($name, $worksheet, $extractedRange)); |
749
|
2 |
|
} |
750
|
2 |
|
} |
751
|
2 |
|
} |
752
|
2 |
|
} |
753
|
|
|
|
754
|
|
|
private function calcRotation(SimpleXMLElement $styleAttributes): int |
755
|
|
|
{ |
756
|
|
|
$rotation = (int) $styleAttributes->Rotation; |
757
|
2 |
|
if ($rotation >= 270 && $rotation <= 360) { |
758
|
|
|
$rotation -= 360; |
759
|
|
|
} |
760
|
|
|
$rotation = (abs($rotation) > 90) ? 0 : $rotation; |
761
|
|
|
|
762
|
|
|
return $rotation; |
763
|
|
|
} |
764
|
2 |
|
|
765
|
2 |
|
private static function addStyle(array &$styleArray, string $key, string $value): void |
766
|
2 |
|
{ |
767
|
2 |
|
if (array_key_exists($value, self::$mappings[$key])) { |
768
|
|
|
$styleArray[$key] = self::$mappings[$key][$value]; |
769
|
|
|
} |
770
|
|
|
} |
771
|
|
|
|
772
|
2 |
|
private static function addStyle2(array &$styleArray, string $key1, string $key, string $value): void |
773
|
|
|
{ |
774
|
|
|
if (array_key_exists($value, self::$mappings[$key])) { |
775
|
|
|
$styleArray[$key1][$key] = self::$mappings[$key][$value]; |
776
|
2 |
|
} |
777
|
2 |
|
} |
778
|
2 |
|
|
779
|
2 |
|
private static function parseBorderAttributes($borderAttributes) |
780
|
2 |
|
{ |
781
|
|
|
$styleArray = []; |
782
|
|
|
if (isset($borderAttributes['Color'])) { |
783
|
|
|
$styleArray['color']['rgb'] = self::parseGnumericColour($borderAttributes['Color']); |
784
|
2 |
|
} |
785
|
2 |
|
|
786
|
2 |
|
self::addStyle($styleArray, 'borderStyle', $borderAttributes['Style']); |
787
|
2 |
|
|
788
|
2 |
|
return $styleArray; |
789
|
|
|
} |
790
|
|
|
|
791
|
|
|
private function parseRichText($is) |
792
|
|
|
{ |
793
|
|
|
$value = new RichText(); |
794
|
2 |
|
$value->createText($is); |
795
|
|
|
|
796
|
|
|
return $value; |
797
|
2 |
|
} |
798
|
|
|
|
799
|
2 |
|
private static function parseGnumericColour($gnmColour) |
800
|
2 |
|
{ |
801
|
2 |
|
[$gnmR, $gnmG, $gnmB] = explode(':', $gnmColour); |
802
|
|
|
$gnmR = substr(str_pad($gnmR, 4, '0', STR_PAD_RIGHT), 0, 2); |
803
|
|
|
$gnmG = substr(str_pad($gnmG, 4, '0', STR_PAD_RIGHT), 0, 2); |
804
|
2 |
|
$gnmB = substr(str_pad($gnmB, 4, '0', STR_PAD_RIGHT), 0, 2); |
805
|
2 |
|
|
806
|
|
|
return $gnmR . $gnmG . $gnmB; |
807
|
|
|
} |
808
|
|
|
|
809
|
2 |
|
private function addColors(array &$styleArray, SimpleXMLElement $styleAttributes): void |
810
|
2 |
|
{ |
811
|
|
|
$RGB = self::parseGnumericColour($styleAttributes['Fore']); |
812
|
2 |
|
$styleArray['font']['color']['rgb'] = $RGB; |
813
|
2 |
|
$RGB = self::parseGnumericColour($styleAttributes['Back']); |
814
|
2 |
|
$shade = (string) $styleAttributes['Shade']; |
815
|
|
|
if (($RGB != '000000') || ($shade != '0')) { |
816
|
2 |
|
$RGB2 = self::parseGnumericColour($styleAttributes['PatternColor']); |
817
|
2 |
|
if ($shade == '1') { |
818
|
|
|
$styleArray['fill']['startColor']['rgb'] = $RGB; |
819
|
|
|
$styleArray['fill']['endColor']['rgb'] = $RGB2; |
820
|
|
|
} else { |
821
|
2 |
|
$styleArray['fill']['endColor']['rgb'] = $RGB; |
822
|
2 |
|
$styleArray['fill']['startColor']['rgb'] = $RGB2; |
823
|
|
|
} |
824
|
2 |
|
self::addStyle2($styleArray, 'fill', 'fillType', $shade); |
825
|
2 |
|
} |
826
|
2 |
|
} |
827
|
|
|
} |
828
|
|
|
|