1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Reader; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Hyperlink; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Comment; |
10
|
|
|
use PhpOffice\PhpSpreadsheet\DefinedName; |
11
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner; |
12
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\AutoFilter; |
13
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\Chart; |
14
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\ColumnAndRowAttributes; |
15
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\ConditionalStyles; |
16
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\DataValidations; |
17
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\Hyperlinks; |
18
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\Namespaces; |
19
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\PageSetup; |
20
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\Properties as PropertyReader; |
21
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\SharedFormula; |
22
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\SheetViewOptions; |
23
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\SheetViews; |
24
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\Styles; |
25
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\TableReader; |
26
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\Theme; |
27
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\WorkbookView; |
28
|
|
|
use PhpOffice\PhpSpreadsheet\ReferenceHelper; |
29
|
|
|
use PhpOffice\PhpSpreadsheet\RichText\RichText; |
30
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\Date; |
31
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\Drawing; |
32
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\File; |
33
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\Font; |
34
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\StringHelper; |
35
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
36
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Color; |
37
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Font as StyleFont; |
38
|
|
|
use PhpOffice\PhpSpreadsheet\Style\NumberFormat; |
39
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Style; |
40
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooterDrawing; |
41
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
42
|
|
|
use SimpleXMLElement; |
43
|
|
|
use Throwable; |
44
|
|
|
use XMLReader; |
45
|
|
|
use ZipArchive; |
46
|
|
|
|
47
|
|
|
class Xlsx extends BaseReader |
48
|
|
|
{ |
49
|
|
|
const INITIAL_FILE = '_rels/.rels'; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* ReferenceHelper instance. |
53
|
|
|
*/ |
54
|
|
|
private ReferenceHelper $referenceHelper; |
55
|
|
|
|
56
|
|
|
private ZipArchive $zip; |
57
|
|
|
|
58
|
|
|
private Styles $styleReader; |
59
|
|
|
|
60
|
|
|
private array $sharedFormulae = []; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Create a new Xlsx Reader instance. |
64
|
|
|
*/ |
65
|
720 |
|
public function __construct() |
66
|
|
|
{ |
67
|
720 |
|
parent::__construct(); |
68
|
720 |
|
$this->referenceHelper = ReferenceHelper::getInstance(); |
69
|
720 |
|
$this->securityScanner = XmlScanner::getInstance($this); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Can the current IReader read the file? |
74
|
|
|
*/ |
75
|
34 |
|
public function canRead(string $filename): bool |
76
|
|
|
{ |
77
|
34 |
|
if (!File::testFileNoThrow($filename, self::INITIAL_FILE)) { |
78
|
14 |
|
return false; |
79
|
|
|
} |
80
|
|
|
|
81
|
20 |
|
$result = false; |
82
|
20 |
|
$this->zip = $zip = new ZipArchive(); |
83
|
|
|
|
84
|
20 |
|
if ($zip->open($filename) === true) { |
85
|
20 |
|
[$workbookBasename] = $this->getWorkbookBaseName(); |
86
|
20 |
|
$result = !empty($workbookBasename); |
87
|
|
|
|
88
|
20 |
|
$zip->close(); |
89
|
|
|
} |
90
|
|
|
|
91
|
20 |
|
return $result; |
92
|
|
|
} |
93
|
|
|
|
94
|
696 |
|
public static function testSimpleXml(mixed $value): SimpleXMLElement |
95
|
|
|
{ |
96
|
696 |
|
return ($value instanceof SimpleXMLElement) ? $value : new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><root></root>'); |
97
|
|
|
} |
98
|
|
|
|
99
|
692 |
|
public static function getAttributes(?SimpleXMLElement $value, string $ns = ''): SimpleXMLElement |
100
|
|
|
{ |
101
|
692 |
|
return self::testSimpleXml($value === null ? $value : $value->attributes($ns)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// Phpstan thinks, correctly, that xpath can return false. |
105
|
662 |
|
private static function xpathNoFalse(SimpleXMLElement $sxml, string $path): array |
106
|
|
|
{ |
107
|
662 |
|
return self::falseToArray($sxml->xpath($path)); |
108
|
|
|
} |
109
|
|
|
|
110
|
662 |
|
public static function falseToArray(mixed $value): array |
111
|
|
|
{ |
112
|
662 |
|
return is_array($value) ? $value : []; |
113
|
|
|
} |
114
|
|
|
|
115
|
692 |
|
private function loadZip(string $filename, string $ns = '', bool $replaceUnclosedBr = false): SimpleXMLElement |
116
|
|
|
{ |
117
|
692 |
|
$contents = $this->getFromZipArchive($this->zip, $filename); |
118
|
692 |
|
if ($replaceUnclosedBr) { |
119
|
37 |
|
$contents = str_replace('<br>', '<br/>', $contents); |
120
|
|
|
} |
121
|
692 |
|
$rels = @simplexml_load_string( |
122
|
692 |
|
$this->getSecurityScannerOrThrow()->scan($contents), |
123
|
692 |
|
'SimpleXMLElement', |
124
|
692 |
|
0, |
125
|
692 |
|
$ns |
126
|
692 |
|
); |
127
|
|
|
|
128
|
692 |
|
return self::testSimpleXml($rels); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// This function is just to identify cases where I'm not sure |
132
|
|
|
// why empty namespace is required. |
133
|
660 |
|
private function loadZipNonamespace(string $filename, string $ns): SimpleXMLElement |
134
|
|
|
{ |
135
|
660 |
|
$contents = $this->getFromZipArchive($this->zip, $filename); |
136
|
660 |
|
$rels = simplexml_load_string( |
137
|
660 |
|
$this->getSecurityScannerOrThrow()->scan($contents), |
138
|
660 |
|
'SimpleXMLElement', |
139
|
660 |
|
0, |
140
|
660 |
|
($ns === '' ? $ns : '') |
141
|
660 |
|
); |
142
|
|
|
|
143
|
655 |
|
return self::testSimpleXml($rels); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
private const REL_TO_MAIN = [ |
147
|
|
|
Namespaces::PURL_OFFICE_DOCUMENT => Namespaces::PURL_MAIN, |
148
|
|
|
Namespaces::THUMBNAIL => '', |
149
|
|
|
]; |
150
|
|
|
|
151
|
|
|
private const REL_TO_DRAWING = [ |
152
|
|
|
Namespaces::PURL_RELATIONSHIPS => Namespaces::PURL_DRAWING, |
153
|
|
|
]; |
154
|
|
|
|
155
|
|
|
private const REL_TO_CHART = [ |
156
|
|
|
Namespaces::PURL_RELATIONSHIPS => Namespaces::PURL_CHART, |
157
|
|
|
]; |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Reads names of the worksheets from a file, without parsing the whole file to a Spreadsheet object. |
161
|
|
|
*/ |
162
|
18 |
|
public function listWorksheetNames(string $filename): array |
163
|
|
|
{ |
164
|
18 |
|
File::assertFile($filename, self::INITIAL_FILE); |
165
|
|
|
|
166
|
15 |
|
$worksheetNames = []; |
167
|
|
|
|
168
|
15 |
|
$this->zip = $zip = new ZipArchive(); |
169
|
15 |
|
$zip->open($filename); |
170
|
|
|
|
171
|
|
|
// The files we're looking at here are small enough that simpleXML is more efficient than XMLReader |
172
|
15 |
|
$rels = $this->loadZip(self::INITIAL_FILE, Namespaces::RELATIONSHIPS); |
173
|
15 |
|
foreach ($rels->Relationship as $relx) { |
174
|
15 |
|
$rel = self::getAttributes($relx); |
175
|
15 |
|
$relType = (string) $rel['Type']; |
176
|
15 |
|
$mainNS = self::REL_TO_MAIN[$relType] ?? Namespaces::MAIN; |
177
|
15 |
|
if ($mainNS !== '') { |
178
|
15 |
|
$xmlWorkbook = $this->loadZip((string) $rel['Target'], $mainNS); |
179
|
|
|
|
180
|
15 |
|
if ($xmlWorkbook->sheets) { |
181
|
15 |
|
foreach ($xmlWorkbook->sheets->sheet as $eleSheet) { |
182
|
|
|
// Check if sheet should be skipped |
183
|
15 |
|
$worksheetNames[] = (string) self::getAttributes($eleSheet)['name']; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
15 |
|
$zip->close(); |
190
|
|
|
|
191
|
15 |
|
return $worksheetNames; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns). |
196
|
|
|
*/ |
197
|
19 |
|
public function listWorksheetInfo(string $filename): array |
198
|
|
|
{ |
199
|
19 |
|
File::assertFile($filename, self::INITIAL_FILE); |
200
|
|
|
|
201
|
16 |
|
$worksheetInfo = []; |
202
|
|
|
|
203
|
16 |
|
$this->zip = $zip = new ZipArchive(); |
204
|
16 |
|
$zip->open($filename); |
205
|
|
|
|
206
|
16 |
|
$rels = $this->loadZip(self::INITIAL_FILE, Namespaces::RELATIONSHIPS); |
207
|
16 |
|
foreach ($rels->Relationship as $relx) { |
208
|
16 |
|
$rel = self::getAttributes($relx); |
209
|
16 |
|
$relType = (string) $rel['Type']; |
210
|
16 |
|
$mainNS = self::REL_TO_MAIN[$relType] ?? Namespaces::MAIN; |
211
|
16 |
|
if ($mainNS !== '') { |
212
|
16 |
|
$relTarget = (string) $rel['Target']; |
213
|
16 |
|
$dir = dirname($relTarget); |
214
|
16 |
|
$namespace = dirname($relType); |
215
|
16 |
|
$relsWorkbook = $this->loadZip("$dir/_rels/" . basename($relTarget) . '.rels', Namespaces::RELATIONSHIPS); |
216
|
|
|
|
217
|
16 |
|
$worksheets = []; |
218
|
16 |
|
foreach ($relsWorkbook->Relationship as $elex) { |
219
|
16 |
|
$ele = self::getAttributes($elex); |
220
|
|
|
if ( |
221
|
16 |
|
((string) $ele['Type'] === "$namespace/worksheet") |
222
|
16 |
|
|| ((string) $ele['Type'] === "$namespace/chartsheet") |
223
|
|
|
) { |
224
|
16 |
|
$worksheets[(string) $ele['Id']] = $ele['Target']; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
228
|
16 |
|
$xmlWorkbook = $this->loadZip($relTarget, $mainNS); |
229
|
16 |
|
if ($xmlWorkbook->sheets) { |
230
|
16 |
|
$dir = dirname($relTarget); |
231
|
|
|
|
232
|
16 |
|
foreach ($xmlWorkbook->sheets->sheet as $eleSheet) { |
233
|
16 |
|
$tmpInfo = [ |
234
|
16 |
|
'worksheetName' => (string) self::getAttributes($eleSheet)['name'], |
235
|
16 |
|
'lastColumnLetter' => 'A', |
236
|
16 |
|
'lastColumnIndex' => 0, |
237
|
16 |
|
'totalRows' => 0, |
238
|
16 |
|
'totalColumns' => 0, |
239
|
16 |
|
]; |
240
|
16 |
|
$sheetState = (string) (self::getAttributes($eleSheet)['state'] ?? Worksheet::SHEETSTATE_VISIBLE); |
241
|
16 |
|
$tmpInfo['sheetState'] = $sheetState; |
242
|
|
|
|
243
|
16 |
|
$fileWorksheet = (string) $worksheets[self::getArrayItemString(self::getAttributes($eleSheet, $namespace), 'id')]; |
244
|
16 |
|
$fileWorksheetPath = str_starts_with($fileWorksheet, '/') ? substr($fileWorksheet, 1) : "$dir/$fileWorksheet"; |
245
|
|
|
|
246
|
16 |
|
$xml = new XMLReader(); |
247
|
16 |
|
$xml->xml( |
248
|
16 |
|
$this->getSecurityScannerOrThrow() |
249
|
16 |
|
->scan( |
250
|
16 |
|
$this->getFromZipArchive( |
251
|
16 |
|
$this->zip, |
252
|
16 |
|
$fileWorksheetPath |
253
|
16 |
|
) |
254
|
16 |
|
) |
255
|
16 |
|
); |
256
|
16 |
|
$xml->setParserProperty(2, true); |
257
|
|
|
|
258
|
16 |
|
$currCells = 0; |
259
|
16 |
|
while ($xml->read()) { |
260
|
16 |
|
if ($xml->localName == 'row' && $xml->nodeType == XMLReader::ELEMENT && $xml->namespaceURI === $mainNS) { |
261
|
16 |
|
$row = (int) $xml->getAttribute('r'); |
262
|
16 |
|
$tmpInfo['totalRows'] = $row; |
263
|
16 |
|
$tmpInfo['totalColumns'] = max($tmpInfo['totalColumns'], $currCells); |
264
|
16 |
|
$currCells = 0; |
265
|
16 |
|
} elseif ($xml->localName == 'c' && $xml->nodeType == XMLReader::ELEMENT && $xml->namespaceURI === $mainNS) { |
266
|
16 |
|
$cell = $xml->getAttribute('r'); |
267
|
16 |
|
$currCells = $cell ? max($currCells, Coordinate::indexesFromString($cell)[0]) : ($currCells + 1); |
268
|
|
|
} |
269
|
|
|
} |
270
|
16 |
|
$tmpInfo['totalColumns'] = max($tmpInfo['totalColumns'], $currCells); |
271
|
16 |
|
$xml->close(); |
272
|
|
|
|
273
|
16 |
|
$tmpInfo['lastColumnIndex'] = $tmpInfo['totalColumns'] - 1; |
274
|
16 |
|
$tmpInfo['lastColumnLetter'] = Coordinate::stringFromColumnIndex($tmpInfo['lastColumnIndex'] + 1); |
275
|
|
|
|
276
|
16 |
|
$worksheetInfo[] = $tmpInfo; |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
282
|
16 |
|
$zip->close(); |
283
|
|
|
|
284
|
16 |
|
return $worksheetInfo; |
285
|
|
|
} |
286
|
|
|
|
287
|
22 |
|
private static function castToBoolean(SimpleXMLElement $c): bool |
288
|
|
|
{ |
289
|
22 |
|
$value = isset($c->v) ? (string) $c->v : null; |
290
|
22 |
|
if ($value == '0') { |
291
|
15 |
|
return false; |
292
|
18 |
|
} elseif ($value == '1') { |
293
|
18 |
|
return true; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
return (bool) $c->v; |
297
|
|
|
} |
298
|
|
|
|
299
|
188 |
|
private static function castToError(?SimpleXMLElement $c): ?string |
300
|
|
|
{ |
301
|
188 |
|
return isset($c, $c->v) ? (string) $c->v : null; |
302
|
|
|
} |
303
|
|
|
|
304
|
522 |
|
private static function castToString(?SimpleXMLElement $c): ?string |
305
|
|
|
{ |
306
|
522 |
|
return isset($c, $c->v) ? (string) $c->v : null; |
307
|
|
|
} |
308
|
|
|
|
309
|
368 |
|
public static function replacePrefixes(string $formula): string |
310
|
|
|
{ |
311
|
368 |
|
return str_replace(['_xlfn.', '_xlws.'], '', $formula); |
312
|
|
|
} |
313
|
|
|
|
314
|
358 |
|
private function castToFormula(?SimpleXMLElement $c, string $r, string &$cellDataType, mixed &$value, mixed &$calculatedValue, string $castBaseType, bool $updateSharedCells = true): void |
315
|
|
|
{ |
316
|
358 |
|
if ($c === null) { |
317
|
|
|
return; |
318
|
|
|
} |
319
|
358 |
|
$attr = $c->f->attributes(); |
320
|
358 |
|
$cellDataType = DataType::TYPE_FORMULA; |
321
|
358 |
|
$formula = self::replacePrefixes((string) $c->f); |
322
|
358 |
|
$value = "=$formula"; |
323
|
358 |
|
$calculatedValue = self::$castBaseType($c); |
324
|
|
|
|
325
|
|
|
// Shared formula? |
326
|
358 |
|
if (isset($attr['t']) && strtolower((string) $attr['t']) == 'shared') { |
327
|
219 |
|
$instance = (string) $attr['si']; |
328
|
|
|
|
329
|
219 |
|
if (!isset($this->sharedFormulae[(string) $attr['si']])) { |
330
|
219 |
|
$this->sharedFormulae[$instance] = new SharedFormula($r, $value); |
331
|
218 |
|
} elseif ($updateSharedCells === true) { |
332
|
|
|
// It's only worth the overhead of adjusting the shared formula for this cell if we're actually loading |
333
|
|
|
// the cell, which may not be the case if we're using a read filter. |
334
|
218 |
|
$master = Coordinate::indexesFromString($this->sharedFormulae[$instance]->master()); |
335
|
218 |
|
$current = Coordinate::indexesFromString($r); |
336
|
|
|
|
337
|
218 |
|
$difference = [0, 0]; |
338
|
218 |
|
$difference[0] = $current[0] - $master[0]; |
339
|
218 |
|
$difference[1] = $current[1] - $master[1]; |
340
|
|
|
|
341
|
218 |
|
$value = $this->referenceHelper->updateFormulaReferences($this->sharedFormulae[$instance]->formula(), 'A1', $difference[0], $difference[1]); |
342
|
|
|
} |
343
|
|
|
} |
344
|
|
|
} |
345
|
|
|
|
346
|
647 |
|
private function fileExistsInArchive(ZipArchive $archive, string $fileName = ''): bool |
347
|
|
|
{ |
348
|
|
|
// Root-relative paths |
349
|
647 |
|
if (str_contains($fileName, '//')) { |
350
|
1 |
|
$fileName = substr($fileName, strpos($fileName, '//') + 1); |
351
|
|
|
} |
352
|
647 |
|
$fileName = File::realpath($fileName); |
353
|
|
|
|
354
|
|
|
// Sadly, some 3rd party xlsx generators don't use consistent case for filenaming |
355
|
|
|
// so we need to load case-insensitively from the zip file |
356
|
|
|
|
357
|
|
|
// Apache POI fixes |
358
|
647 |
|
$contents = $archive->locateName($fileName, ZipArchive::FL_NOCASE); |
359
|
647 |
|
if ($contents === false) { |
360
|
4 |
|
$contents = $archive->locateName(substr($fileName, 1), ZipArchive::FL_NOCASE); |
361
|
|
|
} |
362
|
|
|
|
363
|
647 |
|
return $contents !== false; |
364
|
|
|
} |
365
|
|
|
|
366
|
692 |
|
private function getFromZipArchive(ZipArchive $archive, string $fileName = ''): string |
367
|
|
|
{ |
368
|
|
|
// Root-relative paths |
369
|
692 |
|
if (str_contains($fileName, '//')) { |
370
|
2 |
|
$fileName = substr($fileName, strpos($fileName, '//') + 1); |
371
|
|
|
} |
372
|
|
|
// Relative paths generated by dirname($filename) when $filename |
373
|
|
|
// has no path (i.e.files in root of the zip archive) |
374
|
692 |
|
$fileName = (string) preg_replace('/^\.\//', '', $fileName); |
375
|
692 |
|
$fileName = File::realpath($fileName); |
376
|
|
|
|
377
|
|
|
// Sadly, some 3rd party xlsx generators don't use consistent case for filenaming |
378
|
|
|
// so we need to load case-insensitively from the zip file |
379
|
|
|
|
380
|
692 |
|
$contents = $archive->getFromName($fileName, 0, ZipArchive::FL_NOCASE); |
381
|
|
|
|
382
|
|
|
// Apache POI fixes |
383
|
692 |
|
if ($contents === false) { |
384
|
42 |
|
$contents = $archive->getFromName(substr($fileName, 1), 0, ZipArchive::FL_NOCASE); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
// Has the file been saved with Windoze directory separators rather than unix? |
388
|
692 |
|
if ($contents === false) { |
389
|
39 |
|
$contents = $archive->getFromName(str_replace('/', '\\', $fileName), 0, ZipArchive::FL_NOCASE); |
390
|
|
|
} |
391
|
|
|
|
392
|
692 |
|
return ($contents === false) ? '' : $contents; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* Loads Spreadsheet from file. |
397
|
|
|
*/ |
398
|
665 |
|
protected function loadSpreadsheetFromFile(string $filename): Spreadsheet |
399
|
|
|
{ |
400
|
665 |
|
File::assertFile($filename, self::INITIAL_FILE); |
401
|
|
|
|
402
|
|
|
// Initialisations |
403
|
662 |
|
$excel = $this->newSpreadsheet(); |
404
|
662 |
|
$excel->setValueBinder($this->valueBinder); |
405
|
662 |
|
$excel->removeSheetByIndex(0); |
406
|
662 |
|
$addingFirstCellStyleXf = true; |
407
|
662 |
|
$addingFirstCellXf = true; |
408
|
|
|
|
409
|
662 |
|
$unparsedLoadedData = []; |
410
|
|
|
|
411
|
662 |
|
$this->zip = $zip = new ZipArchive(); |
412
|
662 |
|
$zip->open($filename); |
413
|
|
|
|
414
|
|
|
// Read the theme first, because we need the colour scheme when reading the styles |
415
|
662 |
|
[$workbookBasename, $xmlNamespaceBase] = $this->getWorkbookBaseName(); |
416
|
662 |
|
$drawingNS = self::REL_TO_DRAWING[$xmlNamespaceBase] ?? Namespaces::DRAWINGML; |
417
|
662 |
|
$chartNS = self::REL_TO_CHART[$xmlNamespaceBase] ?? Namespaces::CHART; |
418
|
662 |
|
$wbRels = $this->loadZip("xl/_rels/{$workbookBasename}.rels", Namespaces::RELATIONSHIPS); |
419
|
662 |
|
$theme = null; |
420
|
662 |
|
$this->styleReader = new Styles(); |
421
|
662 |
|
foreach ($wbRels->Relationship as $relx) { |
422
|
661 |
|
$rel = self::getAttributes($relx); |
423
|
661 |
|
$relTarget = (string) $rel['Target']; |
424
|
661 |
|
if (str_starts_with($relTarget, '/xl/')) { |
425
|
12 |
|
$relTarget = substr($relTarget, 4); |
426
|
|
|
} |
427
|
661 |
|
switch ($rel['Type']) { |
428
|
661 |
|
case "$xmlNamespaceBase/theme": |
429
|
645 |
|
if (!$this->fileExistsInArchive($zip, "xl/{$relTarget}")) { |
430
|
3 |
|
break; // issue3770 |
431
|
|
|
} |
432
|
642 |
|
$themeOrderArray = ['lt1', 'dk1', 'lt2', 'dk2']; |
433
|
642 |
|
$themeOrderAdditional = count($themeOrderArray); |
434
|
|
|
|
435
|
642 |
|
$xmlTheme = $this->loadZip("xl/{$relTarget}", $drawingNS); |
436
|
642 |
|
$xmlThemeName = self::getAttributes($xmlTheme); |
437
|
642 |
|
$xmlTheme = $xmlTheme->children($drawingNS); |
438
|
642 |
|
$themeName = (string) $xmlThemeName['name']; |
439
|
|
|
|
440
|
642 |
|
$colourScheme = self::getAttributes($xmlTheme->themeElements->clrScheme); |
441
|
642 |
|
$colourSchemeName = (string) $colourScheme['name']; |
442
|
642 |
|
$excel->getTheme()->setThemeColorName($colourSchemeName); |
443
|
642 |
|
$colourScheme = $xmlTheme->themeElements->clrScheme->children($drawingNS); |
444
|
|
|
|
445
|
642 |
|
$themeColours = []; |
446
|
642 |
|
foreach ($colourScheme as $k => $xmlColour) { |
447
|
642 |
|
$themePos = array_search($k, $themeOrderArray); |
448
|
642 |
|
if ($themePos === false) { |
449
|
642 |
|
$themePos = $themeOrderAdditional++; |
450
|
|
|
} |
451
|
642 |
|
if (isset($xmlColour->sysClr)) { |
452
|
634 |
|
$xmlColourData = self::getAttributes($xmlColour->sysClr); |
453
|
634 |
|
$themeColours[$themePos] = (string) $xmlColourData['lastClr']; |
454
|
634 |
|
$excel->getTheme()->setThemeColor($k, (string) $xmlColourData['lastClr']); |
455
|
642 |
|
} elseif (isset($xmlColour->srgbClr)) { |
456
|
642 |
|
$xmlColourData = self::getAttributes($xmlColour->srgbClr); |
457
|
642 |
|
$themeColours[$themePos] = (string) $xmlColourData['val']; |
458
|
642 |
|
$excel->getTheme()->setThemeColor($k, (string) $xmlColourData['val']); |
459
|
|
|
} |
460
|
|
|
} |
461
|
642 |
|
$theme = new Theme($themeName, $colourSchemeName, $themeColours); |
462
|
642 |
|
$this->styleReader->setTheme($theme); |
463
|
|
|
|
464
|
642 |
|
$fontScheme = self::getAttributes($xmlTheme->themeElements->fontScheme); |
465
|
642 |
|
$fontSchemeName = (string) $fontScheme['name']; |
466
|
642 |
|
$excel->getTheme()->setThemeFontName($fontSchemeName); |
467
|
642 |
|
$majorFonts = []; |
468
|
642 |
|
$minorFonts = []; |
469
|
642 |
|
$fontScheme = $xmlTheme->themeElements->fontScheme->children($drawingNS); |
470
|
642 |
|
$majorLatin = self::getAttributes($fontScheme->majorFont->latin)['typeface'] ?? ''; |
471
|
642 |
|
$majorEastAsian = self::getAttributes($fontScheme->majorFont->ea)['typeface'] ?? ''; |
472
|
642 |
|
$majorComplexScript = self::getAttributes($fontScheme->majorFont->cs)['typeface'] ?? ''; |
473
|
642 |
|
$minorLatin = self::getAttributes($fontScheme->minorFont->latin)['typeface'] ?? ''; |
474
|
642 |
|
$minorEastAsian = self::getAttributes($fontScheme->minorFont->ea)['typeface'] ?? ''; |
475
|
642 |
|
$minorComplexScript = self::getAttributes($fontScheme->minorFont->cs)['typeface'] ?? ''; |
476
|
|
|
|
477
|
642 |
|
foreach ($fontScheme->majorFont->font as $xmlFont) { |
478
|
632 |
|
$fontAttributes = self::getAttributes($xmlFont); |
479
|
632 |
|
$script = (string) ($fontAttributes['script'] ?? ''); |
480
|
632 |
|
if (!empty($script)) { |
481
|
632 |
|
$majorFonts[$script] = (string) ($fontAttributes['typeface'] ?? ''); |
482
|
|
|
} |
483
|
|
|
} |
484
|
642 |
|
foreach ($fontScheme->minorFont->font as $xmlFont) { |
485
|
632 |
|
$fontAttributes = self::getAttributes($xmlFont); |
486
|
632 |
|
$script = (string) ($fontAttributes['script'] ?? ''); |
487
|
632 |
|
if (!empty($script)) { |
488
|
632 |
|
$minorFonts[$script] = (string) ($fontAttributes['typeface'] ?? ''); |
489
|
|
|
} |
490
|
|
|
} |
491
|
642 |
|
$excel->getTheme()->setMajorFontValues($majorLatin, $majorEastAsian, $majorComplexScript, $majorFonts); |
492
|
642 |
|
$excel->getTheme()->setMinorFontValues($minorLatin, $minorEastAsian, $minorComplexScript, $minorFonts); |
493
|
|
|
|
494
|
642 |
|
break; |
495
|
|
|
} |
496
|
|
|
} |
497
|
|
|
|
498
|
662 |
|
$rels = $this->loadZip(self::INITIAL_FILE, Namespaces::RELATIONSHIPS); |
499
|
|
|
|
500
|
662 |
|
$propertyReader = new PropertyReader($this->getSecurityScannerOrThrow(), $excel->getProperties()); |
501
|
662 |
|
$charts = $chartDetails = []; |
502
|
662 |
|
foreach ($rels->Relationship as $relx) { |
503
|
662 |
|
$rel = self::getAttributes($relx); |
504
|
662 |
|
$relTarget = (string) $rel['Target']; |
505
|
|
|
// issue 3553 |
506
|
662 |
|
if ($relTarget[0] === '/') { |
507
|
7 |
|
$relTarget = substr($relTarget, 1); |
508
|
|
|
} |
509
|
662 |
|
$relType = (string) $rel['Type']; |
510
|
662 |
|
$mainNS = self::REL_TO_MAIN[$relType] ?? Namespaces::MAIN; |
511
|
|
|
switch ($relType) { |
512
|
655 |
|
case Namespaces::CORE_PROPERTIES: |
513
|
645 |
|
$propertyReader->readCoreProperties($this->getFromZipArchive($zip, $relTarget)); |
514
|
|
|
|
515
|
645 |
|
break; |
516
|
662 |
|
case "$xmlNamespaceBase/extended-properties": |
517
|
644 |
|
$propertyReader->readExtendedProperties($this->getFromZipArchive($zip, $relTarget)); |
518
|
|
|
|
519
|
644 |
|
break; |
520
|
662 |
|
case "$xmlNamespaceBase/custom-properties": |
521
|
57 |
|
$propertyReader->readCustomProperties($this->getFromZipArchive($zip, $relTarget)); |
522
|
|
|
|
523
|
57 |
|
break; |
524
|
|
|
//Ribbon |
525
|
662 |
|
case Namespaces::EXTENSIBILITY: |
526
|
2 |
|
$customUI = $relTarget; |
527
|
2 |
|
if ($customUI) { |
528
|
2 |
|
$this->readRibbon($excel, $customUI, $zip); |
529
|
|
|
} |
530
|
|
|
|
531
|
2 |
|
break; |
532
|
662 |
|
case "$xmlNamespaceBase/officeDocument": |
533
|
662 |
|
$dir = dirname($relTarget); |
534
|
|
|
|
535
|
|
|
// Do not specify namespace in next stmt - do it in Xpath |
536
|
662 |
|
$relsWorkbook = $this->loadZip("$dir/_rels/" . basename($relTarget) . '.rels', Namespaces::RELATIONSHIPS); |
537
|
662 |
|
$relsWorkbook->registerXPathNamespace('rel', Namespaces::RELATIONSHIPS); |
538
|
|
|
|
539
|
662 |
|
$worksheets = []; |
540
|
662 |
|
$macros = $customUI = null; |
541
|
662 |
|
foreach ($relsWorkbook->Relationship as $elex) { |
542
|
662 |
|
$ele = self::getAttributes($elex); |
543
|
662 |
|
switch ($ele['Type']) { |
544
|
655 |
|
case Namespaces::WORKSHEET: |
545
|
655 |
|
case Namespaces::PURL_WORKSHEET: |
546
|
662 |
|
$worksheets[(string) $ele['Id']] = $ele['Target']; |
547
|
|
|
|
548
|
662 |
|
break; |
549
|
655 |
|
case Namespaces::CHARTSHEET: |
550
|
2 |
|
if ($this->includeCharts === true) { |
551
|
1 |
|
$worksheets[(string) $ele['Id']] = $ele['Target']; |
552
|
|
|
} |
553
|
|
|
|
554
|
2 |
|
break; |
555
|
|
|
// a vbaProject ? (: some macros) |
556
|
655 |
|
case Namespaces::VBA: |
557
|
3 |
|
$macros = $ele['Target']; |
558
|
|
|
|
559
|
3 |
|
break; |
560
|
|
|
} |
561
|
|
|
} |
562
|
|
|
|
563
|
662 |
|
if ($macros !== null) { |
564
|
3 |
|
$macrosCode = $this->getFromZipArchive($zip, 'xl/vbaProject.bin'); //vbaProject.bin always in 'xl' dir and always named vbaProject.bin |
565
|
3 |
|
if (!empty($macrosCode)) { |
566
|
3 |
|
$excel->setMacrosCode($macrosCode); |
567
|
3 |
|
$excel->setHasMacros(true); |
568
|
|
|
//short-circuit : not reading vbaProject.bin.rel to get Signature =>allways vbaProjectSignature.bin in 'xl' dir |
569
|
3 |
|
$Certificate = $this->getFromZipArchive($zip, 'xl/vbaProjectSignature.bin'); |
570
|
3 |
|
$excel->setMacrosCertificate($Certificate); |
571
|
|
|
} |
572
|
|
|
} |
573
|
|
|
|
574
|
662 |
|
$relType = "rel:Relationship[@Type='" |
575
|
662 |
|
. "$xmlNamespaceBase/styles" |
576
|
662 |
|
. "']"; |
577
|
|
|
/** @var ?SimpleXMLElement */ |
578
|
662 |
|
$xpath = self::getArrayItem(self::xpathNoFalse($relsWorkbook, $relType)); |
579
|
|
|
|
580
|
662 |
|
if ($xpath === null) { |
581
|
1 |
|
$xmlStyles = self::testSimpleXml(null); |
582
|
|
|
} else { |
583
|
662 |
|
$stylesTarget = (string) $xpath['Target']; |
584
|
662 |
|
$stylesTarget = str_starts_with($stylesTarget, '/') ? substr($stylesTarget, 1) : "$dir/$stylesTarget"; |
585
|
662 |
|
$xmlStyles = $this->loadZip($stylesTarget, $mainNS); |
586
|
|
|
} |
587
|
|
|
|
588
|
662 |
|
$palette = self::extractPalette($xmlStyles); |
589
|
662 |
|
$this->styleReader->setWorkbookPalette($palette); |
590
|
662 |
|
$fills = self::extractStyles($xmlStyles, 'fills', 'fill'); |
591
|
662 |
|
$fonts = self::extractStyles($xmlStyles, 'fonts', 'font'); |
592
|
662 |
|
$borders = self::extractStyles($xmlStyles, 'borders', 'border'); |
593
|
662 |
|
$xfTags = self::extractStyles($xmlStyles, 'cellXfs', 'xf'); |
594
|
662 |
|
$cellXfTags = self::extractStyles($xmlStyles, 'cellStyleXfs', 'xf'); |
595
|
|
|
|
596
|
662 |
|
$styles = []; |
597
|
662 |
|
$cellStyles = []; |
598
|
662 |
|
$numFmts = null; |
599
|
662 |
|
if (/*$xmlStyles && */ $xmlStyles->numFmts[0]) { |
600
|
245 |
|
$numFmts = $xmlStyles->numFmts[0]; |
601
|
|
|
} |
602
|
662 |
|
if (isset($numFmts)) { |
603
|
245 |
|
$numFmts->registerXPathNamespace('sml', $mainNS); |
604
|
|
|
} |
605
|
662 |
|
$this->styleReader->setNamespace($mainNS); |
606
|
662 |
|
if (!$this->readDataOnly/* && $xmlStyles*/) { |
607
|
660 |
|
foreach ($xfTags as $xfTag) { |
608
|
660 |
|
$xf = self::getAttributes($xfTag); |
609
|
660 |
|
$numFmt = null; |
610
|
|
|
|
611
|
660 |
|
if ($xf['numFmtId']) { |
612
|
658 |
|
if (isset($numFmts)) { |
613
|
|
|
/** @var ?SimpleXMLElement */ |
614
|
245 |
|
$tmpNumFmt = self::getArrayItem($numFmts->xpath("sml:numFmt[@numFmtId=$xf[numFmtId]]")); |
615
|
|
|
|
616
|
245 |
|
if (isset($tmpNumFmt['formatCode'])) { |
617
|
244 |
|
$numFmt = (string) $tmpNumFmt['formatCode']; |
618
|
|
|
} |
619
|
|
|
} |
620
|
|
|
|
621
|
|
|
// We shouldn't override any of the built-in MS Excel values (values below id 164) |
622
|
|
|
// But there's a lot of naughty homebrew xlsx writers that do use "reserved" id values that aren't actually used |
623
|
|
|
// So we make allowance for them rather than lose formatting masks |
624
|
|
|
if ( |
625
|
658 |
|
$numFmt === null |
626
|
658 |
|
&& (int) $xf['numFmtId'] < 164 |
627
|
658 |
|
&& NumberFormat::builtInFormatCode((int) $xf['numFmtId']) !== '' |
628
|
|
|
) { |
629
|
647 |
|
$numFmt = NumberFormat::builtInFormatCode((int) $xf['numFmtId']); |
630
|
|
|
} |
631
|
|
|
} |
632
|
660 |
|
$quotePrefix = (bool) (string) ($xf['quotePrefix'] ?? ''); |
633
|
|
|
|
634
|
660 |
|
$style = (object) [ |
635
|
660 |
|
'numFmt' => $numFmt ?? NumberFormat::FORMAT_GENERAL, |
636
|
660 |
|
'font' => $fonts[(int) ($xf['fontId'])], |
637
|
660 |
|
'fill' => $fills[(int) ($xf['fillId'])], |
638
|
660 |
|
'border' => $borders[(int) ($xf['borderId'])], |
639
|
660 |
|
'alignment' => $xfTag->alignment, |
640
|
660 |
|
'protection' => $xfTag->protection, |
641
|
660 |
|
'quotePrefix' => $quotePrefix, |
642
|
660 |
|
]; |
643
|
660 |
|
$styles[] = $style; |
644
|
|
|
|
645
|
|
|
// add style to cellXf collection |
646
|
660 |
|
$objStyle = new Style(); |
647
|
660 |
|
$this->styleReader->readStyle($objStyle, $style); |
648
|
660 |
|
if ($addingFirstCellXf) { |
649
|
660 |
|
$excel->removeCellXfByIndex(0); // remove the default style |
650
|
660 |
|
$addingFirstCellXf = false; |
651
|
|
|
} |
652
|
660 |
|
$excel->addCellXf($objStyle); |
653
|
|
|
} |
654
|
|
|
|
655
|
660 |
|
foreach ($cellXfTags as $xfTag) { |
656
|
659 |
|
$xf = self::getAttributes($xfTag); |
657
|
659 |
|
$numFmt = NumberFormat::FORMAT_GENERAL; |
658
|
659 |
|
if ($numFmts && $xf['numFmtId']) { |
659
|
|
|
/** @var ?SimpleXMLElement */ |
660
|
245 |
|
$tmpNumFmt = self::getArrayItem($numFmts->xpath("sml:numFmt[@numFmtId=$xf[numFmtId]]")); |
661
|
245 |
|
if (isset($tmpNumFmt['formatCode'])) { |
662
|
29 |
|
$numFmt = (string) $tmpNumFmt['formatCode']; |
663
|
243 |
|
} elseif ((int) $xf['numFmtId'] < 165) { |
664
|
243 |
|
$numFmt = NumberFormat::builtInFormatCode((int) $xf['numFmtId']); |
665
|
|
|
} |
666
|
|
|
} |
667
|
|
|
|
668
|
659 |
|
$quotePrefix = (bool) (string) ($xf['quotePrefix'] ?? ''); |
669
|
|
|
|
670
|
659 |
|
$cellStyle = (object) [ |
671
|
659 |
|
'numFmt' => $numFmt, |
672
|
659 |
|
'font' => $fonts[(int) ($xf['fontId'])], |
673
|
659 |
|
'fill' => $fills[((int) $xf['fillId'])], |
674
|
659 |
|
'border' => $borders[(int) ($xf['borderId'])], |
675
|
659 |
|
'alignment' => $xfTag->alignment, |
676
|
659 |
|
'protection' => $xfTag->protection, |
677
|
659 |
|
'quotePrefix' => $quotePrefix, |
678
|
659 |
|
]; |
679
|
659 |
|
$cellStyles[] = $cellStyle; |
680
|
|
|
|
681
|
|
|
// add style to cellStyleXf collection |
682
|
659 |
|
$objStyle = new Style(); |
683
|
659 |
|
$this->styleReader->readStyle($objStyle, $cellStyle); |
684
|
659 |
|
if ($addingFirstCellStyleXf) { |
685
|
659 |
|
$excel->removeCellStyleXfByIndex(0); // remove the default style |
686
|
659 |
|
$addingFirstCellStyleXf = false; |
687
|
|
|
} |
688
|
659 |
|
$excel->addCellStyleXf($objStyle); |
689
|
|
|
} |
690
|
|
|
} |
691
|
662 |
|
$this->styleReader->setStyleXml($xmlStyles); |
692
|
662 |
|
$this->styleReader->setNamespace($mainNS); |
693
|
662 |
|
$this->styleReader->setStyleBaseData($theme, $styles, $cellStyles); |
694
|
662 |
|
$dxfs = $this->styleReader->dxfs($this->readDataOnly); |
695
|
662 |
|
$styles = $this->styleReader->styles(); |
696
|
|
|
|
697
|
|
|
// Read content after setting the styles |
698
|
662 |
|
$sharedStrings = []; |
699
|
662 |
|
$relType = "rel:Relationship[@Type='" |
700
|
662 |
|
//. Namespaces::SHARED_STRINGS |
701
|
662 |
|
. "$xmlNamespaceBase/sharedStrings" |
702
|
662 |
|
. "']"; |
703
|
|
|
/** @var ?SimpleXMLElement */ |
704
|
662 |
|
$xpath = self::getArrayItem($relsWorkbook->xpath($relType)); |
705
|
|
|
|
706
|
662 |
|
if ($xpath) { |
707
|
605 |
|
$sharedStringsTarget = (string) $xpath['Target']; |
708
|
605 |
|
$sharedStringsTarget = str_starts_with($sharedStringsTarget, '/') ? substr($sharedStringsTarget, 1) : "$dir/$sharedStringsTarget"; |
709
|
605 |
|
$xmlStrings = $this->loadZip($sharedStringsTarget, $mainNS); |
710
|
603 |
|
if (isset($xmlStrings->si)) { |
711
|
485 |
|
foreach ($xmlStrings->si as $val) { |
712
|
485 |
|
if (isset($val->t)) { |
713
|
482 |
|
$sharedStrings[] = StringHelper::controlCharacterOOXML2PHP((string) $val->t); |
714
|
39 |
|
} elseif (isset($val->r)) { |
715
|
39 |
|
$sharedStrings[] = $this->parseRichText($val); |
716
|
|
|
} else { |
717
|
1 |
|
$sharedStrings[] = ''; |
718
|
|
|
} |
719
|
|
|
} |
720
|
|
|
} |
721
|
|
|
} |
722
|
|
|
|
723
|
660 |
|
$xmlWorkbook = $this->loadZipNoNamespace($relTarget, $mainNS); |
724
|
655 |
|
$xmlWorkbookNS = $this->loadZip($relTarget, $mainNS); |
725
|
|
|
|
726
|
|
|
// Set base date |
727
|
655 |
|
$excel->setExcelCalendar(Date::CALENDAR_WINDOWS_1900); |
728
|
655 |
|
if ($xmlWorkbookNS->workbookPr) { |
729
|
646 |
|
Date::setExcelCalendar(Date::CALENDAR_WINDOWS_1900); |
730
|
646 |
|
$attrs1904 = self::getAttributes($xmlWorkbookNS->workbookPr); |
731
|
646 |
|
if (isset($attrs1904['date1904'])) { |
732
|
14 |
|
if (self::boolean((string) $attrs1904['date1904'])) { |
733
|
3 |
|
Date::setExcelCalendar(Date::CALENDAR_MAC_1904); |
734
|
3 |
|
$excel->setExcelCalendar(Date::CALENDAR_MAC_1904); |
735
|
|
|
} |
736
|
|
|
} |
737
|
|
|
} |
738
|
|
|
|
739
|
|
|
// Set protection |
740
|
655 |
|
$this->readProtection($excel, $xmlWorkbook); |
741
|
|
|
|
742
|
655 |
|
$sheetId = 0; // keep track of new sheet id in final workbook |
743
|
655 |
|
$oldSheetId = -1; // keep track of old sheet id in final workbook |
744
|
655 |
|
$countSkippedSheets = 0; // keep track of number of skipped sheets |
745
|
655 |
|
$mapSheetId = []; // mapping of sheet ids from old to new |
746
|
|
|
|
747
|
655 |
|
$charts = $chartDetails = []; |
748
|
|
|
|
749
|
655 |
|
if ($xmlWorkbookNS->sheets) { |
750
|
655 |
|
foreach ($xmlWorkbookNS->sheets->sheet as $eleSheet) { |
751
|
655 |
|
$eleSheetAttr = self::getAttributes($eleSheet); |
752
|
655 |
|
++$oldSheetId; |
753
|
|
|
|
754
|
|
|
// Check if sheet should be skipped |
755
|
655 |
|
if (is_array($this->loadSheetsOnly) && !in_array((string) $eleSheetAttr['name'], $this->loadSheetsOnly)) { |
756
|
6 |
|
++$countSkippedSheets; |
757
|
6 |
|
$mapSheetId[$oldSheetId] = null; |
758
|
|
|
|
759
|
6 |
|
continue; |
760
|
|
|
} |
761
|
|
|
|
762
|
654 |
|
$sheetReferenceId = self::getArrayItemString(self::getAttributes($eleSheet, $xmlNamespaceBase), 'id'); |
763
|
654 |
|
if (isset($worksheets[$sheetReferenceId]) === false) { |
764
|
1 |
|
++$countSkippedSheets; |
765
|
1 |
|
$mapSheetId[$oldSheetId] = null; |
766
|
|
|
|
767
|
1 |
|
continue; |
768
|
|
|
} |
769
|
|
|
// Map old sheet id in original workbook to new sheet id. |
770
|
|
|
// They will differ if loadSheetsOnly() is being used |
771
|
654 |
|
$mapSheetId[$oldSheetId] = $oldSheetId - $countSkippedSheets; |
772
|
|
|
|
773
|
|
|
// Load sheet |
774
|
654 |
|
$docSheet = $excel->createSheet(); |
775
|
|
|
// Use false for $updateFormulaCellReferences to prevent adjustment of worksheet |
776
|
|
|
// references in formula cells... during the load, all formulae should be correct, |
777
|
|
|
// and we're simply bringing the worksheet name in line with the formula, not the |
778
|
|
|
// reverse |
779
|
654 |
|
$docSheet->setTitle((string) $eleSheetAttr['name'], false, false); |
780
|
|
|
|
781
|
654 |
|
$fileWorksheet = (string) $worksheets[$sheetReferenceId]; |
782
|
|
|
// issue 3665 adds test for /. |
783
|
|
|
// This broke XlsxRootZipFilesTest, |
784
|
|
|
// but Excel reports an error with that file. |
785
|
|
|
// Testing dir for . avoids this problem. |
786
|
|
|
// It might be better just to drop the test. |
787
|
654 |
|
if ($fileWorksheet[0] == '/' && $dir !== '.') { |
788
|
12 |
|
$fileWorksheet = substr($fileWorksheet, strlen($dir) + 2); |
789
|
|
|
} |
790
|
654 |
|
$xmlSheet = $this->loadZipNoNamespace("$dir/$fileWorksheet", $mainNS); |
791
|
654 |
|
$xmlSheetNS = $this->loadZip("$dir/$fileWorksheet", $mainNS); |
792
|
|
|
|
793
|
|
|
// Shared Formula table is unique to each Worksheet, so we need to reset it here |
794
|
654 |
|
$this->sharedFormulae = []; |
795
|
|
|
|
796
|
654 |
|
if (isset($eleSheetAttr['state']) && (string) $eleSheetAttr['state'] != '') { |
797
|
28 |
|
$docSheet->setSheetState((string) $eleSheetAttr['state']); |
798
|
|
|
} |
799
|
654 |
|
if ($xmlSheetNS) { |
800
|
654 |
|
$xmlSheetMain = $xmlSheetNS->children($mainNS); |
801
|
|
|
// Setting Conditional Styles adjusts selected cells, so we need to execute this |
802
|
|
|
// before reading the sheet view data to get the actual selected cells |
803
|
654 |
|
if (!$this->readDataOnly && ($xmlSheet->conditionalFormatting)) { |
804
|
211 |
|
(new ConditionalStyles($docSheet, $xmlSheet, $dxfs, $this->styleReader))->load(); |
805
|
|
|
} |
806
|
654 |
|
if (!$this->readDataOnly && $xmlSheet->extLst) { |
807
|
198 |
|
(new ConditionalStyles($docSheet, $xmlSheet, $dxfs, $this->styleReader))->loadFromExt(); |
808
|
|
|
} |
809
|
654 |
|
if (isset($xmlSheetMain->sheetViews, $xmlSheetMain->sheetViews->sheetView)) { |
810
|
651 |
|
$sheetViews = new SheetViews($xmlSheetMain->sheetViews->sheetView, $docSheet); |
811
|
651 |
|
$sheetViews->load(); |
812
|
|
|
} |
813
|
|
|
|
814
|
654 |
|
$sheetViewOptions = new SheetViewOptions($docSheet, $xmlSheetNS); |
815
|
654 |
|
$sheetViewOptions->load($this->readDataOnly, $this->styleReader); |
816
|
|
|
|
817
|
654 |
|
(new ColumnAndRowAttributes($docSheet, $xmlSheetNS)) |
818
|
654 |
|
->load($this->getReadFilter(), $this->readDataOnly, $this->ignoreRowsWithNoCells); |
819
|
|
|
} |
820
|
|
|
|
821
|
654 |
|
$holdSelectedCells = $docSheet->getSelectedCells(); |
822
|
654 |
|
if ($xmlSheetNS && $xmlSheetNS->sheetData && $xmlSheetNS->sheetData->row) { |
823
|
612 |
|
$cIndex = 1; // Cell Start from 1 |
824
|
612 |
|
foreach ($xmlSheetNS->sheetData->row as $row) { |
825
|
612 |
|
$rowIndex = 1; |
826
|
612 |
|
foreach ($row->c as $c) { |
827
|
611 |
|
$cAttr = self::getAttributes($c); |
828
|
611 |
|
$r = (string) $cAttr['r']; |
829
|
611 |
|
if ($r == '') { |
830
|
2 |
|
$r = Coordinate::stringFromColumnIndex($rowIndex) . $cIndex; |
831
|
|
|
} |
832
|
611 |
|
$cellDataType = (string) $cAttr['t']; |
833
|
611 |
|
$originalCellDataTypeNumeric = $cellDataType === ''; |
834
|
611 |
|
$value = null; |
835
|
611 |
|
$calculatedValue = null; |
836
|
|
|
|
837
|
|
|
// Read cell? |
838
|
611 |
|
$coordinates = Coordinate::coordinateFromString($r); |
839
|
|
|
|
840
|
611 |
|
if (!$this->getReadFilter()->readCell($coordinates[0], (int) $coordinates[1], $docSheet->getTitle())) { |
841
|
|
|
// Normally, just testing for the f attribute should identify this cell as containing a formula |
842
|
|
|
// that we need to read, even though it is outside of the filter range, in case it is a shared formula. |
843
|
|
|
// But in some cases, this attribute isn't set; so we need to delve a level deeper and look at |
844
|
|
|
// whether or not the cell has a child formula element that is shared. |
845
|
3 |
|
if (isset($cAttr->f) || (isset($c->f, $c->f->attributes()['t']) && strtolower((string) $c->f->attributes()['t']) === 'shared')) { |
846
|
|
|
$this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, 'castToError', false); |
847
|
|
|
} |
848
|
3 |
|
++$rowIndex; |
849
|
|
|
|
850
|
3 |
|
continue; |
851
|
|
|
} |
852
|
|
|
|
853
|
|
|
// Read cell! |
854
|
611 |
|
$useFormula = isset($c->f) |
855
|
611 |
|
&& ((string) $c->f !== '' || (isset($c->f->attributes()['t']) && strtolower((string) $c->f->attributes()['t']) === 'shared')); |
856
|
|
|
switch ($cellDataType) { |
857
|
17 |
|
case DataType::TYPE_STRING: |
858
|
484 |
|
if ((string) $c->v != '') { |
859
|
484 |
|
$value = $sharedStrings[(int) ($c->v)]; |
860
|
|
|
|
861
|
484 |
|
if ($value instanceof RichText) { |
862
|
36 |
|
$value = clone $value; |
863
|
|
|
} |
864
|
|
|
} else { |
865
|
16 |
|
$value = ''; |
866
|
|
|
} |
867
|
|
|
|
868
|
484 |
|
break; |
869
|
13 |
|
case DataType::TYPE_BOOL: |
870
|
22 |
|
if (!$useFormula) { |
871
|
16 |
|
if (isset($c->v)) { |
872
|
16 |
|
$value = self::castToBoolean($c); |
873
|
|
|
} else { |
874
|
1 |
|
$value = null; |
875
|
1 |
|
$cellDataType = DataType::TYPE_NULL; |
876
|
|
|
} |
877
|
|
|
} else { |
878
|
|
|
// Formula |
879
|
6 |
|
$this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, 'castToBoolean'); |
880
|
6 |
|
self::storeFormulaAttributes($c->f, $docSheet, $r); |
881
|
|
|
} |
882
|
|
|
|
883
|
22 |
|
break; |
884
|
13 |
|
case DataType::TYPE_STRING2: |
885
|
220 |
|
if ($useFormula) { |
886
|
218 |
|
$this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, 'castToString'); |
887
|
218 |
|
self::storeFormulaAttributes($c->f, $docSheet, $r); |
888
|
|
|
} else { |
889
|
3 |
|
$value = self::castToString($c); |
890
|
|
|
} |
891
|
|
|
|
892
|
220 |
|
break; |
893
|
13 |
|
case DataType::TYPE_INLINE: |
894
|
13 |
|
if ($useFormula) { |
895
|
|
|
$this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, 'castToError'); |
896
|
|
|
self::storeFormulaAttributes($c->f, $docSheet, $r); |
897
|
|
|
} else { |
898
|
13 |
|
$value = $this->parseRichText($c->is); |
899
|
|
|
} |
900
|
|
|
|
901
|
13 |
|
break; |
902
|
13 |
|
case DataType::TYPE_ERROR: |
903
|
188 |
|
if (!$useFormula) { |
904
|
|
|
$value = self::castToError($c); |
905
|
|
|
} else { |
906
|
|
|
// Formula |
907
|
188 |
|
$this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, 'castToError'); |
908
|
188 |
|
$eattr = $c->attributes(); |
909
|
188 |
|
if (isset($eattr['vm'])) { |
910
|
1 |
|
if ($calculatedValue === ExcelError::VALUE()) { |
911
|
1 |
|
$calculatedValue = ExcelError::SPILL(); |
912
|
|
|
} |
913
|
|
|
} |
914
|
|
|
} |
915
|
|
|
|
916
|
188 |
|
break; |
917
|
|
|
default: |
918
|
512 |
|
if (!$useFormula) { |
919
|
506 |
|
$value = self::castToString($c); |
920
|
506 |
|
if (is_numeric($value)) { |
921
|
484 |
|
$value += 0; |
922
|
484 |
|
$cellDataType = DataType::TYPE_NUMERIC; |
923
|
|
|
} |
924
|
|
|
} else { |
925
|
|
|
// Formula |
926
|
339 |
|
$this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, 'castToString'); |
927
|
339 |
|
if (is_numeric($calculatedValue)) { |
928
|
336 |
|
$calculatedValue += 0; |
929
|
|
|
} |
930
|
339 |
|
self::storeFormulaAttributes($c->f, $docSheet, $r); |
931
|
|
|
} |
932
|
|
|
|
933
|
512 |
|
break; |
934
|
|
|
} |
935
|
|
|
|
936
|
|
|
// read empty cells or the cells are not empty |
937
|
611 |
|
if ($this->readEmptyCells || ($value !== null && $value !== '')) { |
938
|
|
|
// Rich text? |
939
|
611 |
|
if ($value instanceof RichText && $this->readDataOnly) { |
940
|
|
|
$value = $value->getPlainText(); |
941
|
|
|
} |
942
|
|
|
|
943
|
611 |
|
$cell = $docSheet->getCell($r); |
944
|
|
|
// Assign value |
945
|
611 |
|
if ($cellDataType != '') { |
946
|
|
|
// it is possible, that datatype is numeric but with an empty string, which result in an error |
947
|
604 |
|
if ($cellDataType === DataType::TYPE_NUMERIC && ($value === '' || $value === null)) { |
948
|
1 |
|
$cellDataType = DataType::TYPE_NULL; |
949
|
|
|
} |
950
|
604 |
|
if ($cellDataType !== DataType::TYPE_NULL) { |
951
|
604 |
|
$cell->setValueExplicit($value, $cellDataType); |
952
|
|
|
} |
953
|
|
|
} else { |
954
|
291 |
|
$cell->setValue($value); |
955
|
|
|
} |
956
|
611 |
|
if ($calculatedValue !== null) { |
957
|
349 |
|
$cell->setCalculatedValue($calculatedValue, $originalCellDataTypeNumeric); |
958
|
|
|
} |
959
|
|
|
|
960
|
|
|
// Style information? |
961
|
611 |
|
if (!$this->readDataOnly) { |
962
|
609 |
|
$cAttrS = (int) ($cAttr['s'] ?? 0); |
963
|
|
|
// no style index means 0, it seems |
964
|
609 |
|
$cAttrS = isset($styles[$cAttrS]) ? $cAttrS : 0; |
965
|
609 |
|
$cell->setXfIndex($cAttrS); |
966
|
|
|
// issue 3495 |
967
|
609 |
|
if ($cellDataType === DataType::TYPE_FORMULA && $styles[$cAttrS]->quotePrefix === true) { |
968
|
2 |
|
$holdSelected = $docSheet->getSelectedCells(); |
969
|
2 |
|
$cell->getStyle()->setQuotePrefix(false); |
970
|
2 |
|
$docSheet->setSelectedCells($holdSelected); |
971
|
|
|
} |
972
|
|
|
} |
973
|
|
|
} |
974
|
611 |
|
++$rowIndex; |
975
|
|
|
} |
976
|
612 |
|
++$cIndex; |
977
|
|
|
} |
978
|
|
|
} |
979
|
654 |
|
$docSheet->setSelectedCells($holdSelectedCells); |
980
|
654 |
|
if (!$this->readDataOnly && $xmlSheetNS && $xmlSheetNS->ignoredErrors) { |
981
|
4 |
|
foreach ($xmlSheetNS->ignoredErrors->ignoredError as $ignoredError) { |
982
|
4 |
|
$this->processIgnoredErrors($ignoredError, $docSheet); |
983
|
|
|
} |
984
|
|
|
} |
985
|
|
|
|
986
|
654 |
|
if (!$this->readDataOnly && $xmlSheetNS && $xmlSheetNS->sheetProtection) { |
987
|
68 |
|
$protAttr = $xmlSheetNS->sheetProtection->attributes() ?? []; |
988
|
68 |
|
foreach ($protAttr as $key => $value) { |
989
|
68 |
|
$method = 'set' . ucfirst($key); |
990
|
68 |
|
$docSheet->getProtection()->$method(self::boolean((string) $value)); |
991
|
|
|
} |
992
|
|
|
} |
993
|
|
|
|
994
|
654 |
|
if ($xmlSheet) { |
995
|
644 |
|
$this->readSheetProtection($docSheet, $xmlSheet); |
996
|
|
|
} |
997
|
|
|
|
998
|
654 |
|
if ($this->readDataOnly === false) { |
999
|
652 |
|
$this->readAutoFilter($xmlSheetNS, $docSheet); |
1000
|
652 |
|
$this->readBackgroundImage($xmlSheetNS, $docSheet, dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels'); |
1001
|
|
|
} |
1002
|
|
|
|
1003
|
654 |
|
$this->readTables($xmlSheetNS, $docSheet, $dir, $fileWorksheet, $zip, $mainNS); |
1004
|
|
|
|
1005
|
654 |
|
if ($xmlSheetNS && $xmlSheetNS->mergeCells && $xmlSheetNS->mergeCells->mergeCell && !$this->readDataOnly) { |
1006
|
65 |
|
foreach ($xmlSheetNS->mergeCells->mergeCell as $mergeCellx) { |
1007
|
65 |
|
$mergeCell = $mergeCellx->attributes(); |
1008
|
65 |
|
$mergeRef = (string) ($mergeCell['ref'] ?? ''); |
1009
|
65 |
|
if (str_contains($mergeRef, ':')) { |
1010
|
65 |
|
$docSheet->mergeCells($mergeRef, Worksheet::MERGE_CELL_CONTENT_HIDE); |
1011
|
|
|
} |
1012
|
|
|
} |
1013
|
|
|
} |
1014
|
|
|
|
1015
|
654 |
|
if ($xmlSheet && !$this->readDataOnly) { |
1016
|
642 |
|
$unparsedLoadedData = (new PageSetup($docSheet, $xmlSheet))->load($unparsedLoadedData); |
1017
|
|
|
} |
1018
|
|
|
|
1019
|
654 |
|
if (isset($xmlSheet->extLst->ext)) { |
1020
|
198 |
|
foreach ($xmlSheet->extLst->ext as $extlst) { |
1021
|
198 |
|
$extAttrs = $extlst->attributes() ?? []; |
1022
|
198 |
|
$extUri = (string) ($extAttrs['uri'] ?? ''); |
1023
|
198 |
|
if ($extUri !== '{CCE6A557-97BC-4b89-ADB6-D9C93CAAB3DF}') { |
1024
|
192 |
|
continue; |
1025
|
|
|
} |
1026
|
|
|
// Create dataValidations node if does not exists, maybe is better inside the foreach ? |
1027
|
6 |
|
if (!$xmlSheet->dataValidations) { |
1028
|
1 |
|
$xmlSheet->addChild('dataValidations'); |
1029
|
|
|
} |
1030
|
|
|
|
1031
|
6 |
|
foreach ($extlst->children(Namespaces::DATA_VALIDATIONS1)->dataValidations->dataValidation as $item) { |
1032
|
6 |
|
$item = self::testSimpleXml($item); |
1033
|
6 |
|
$node = self::testSimpleXml($xmlSheet->dataValidations)->addChild('dataValidation'); |
1034
|
6 |
|
foreach ($item->attributes() ?? [] as $attr) { |
1035
|
6 |
|
$node->addAttribute($attr->getName(), $attr); |
1036
|
|
|
} |
1037
|
6 |
|
$node->addAttribute('sqref', $item->children(Namespaces::DATA_VALIDATIONS2)->sqref); |
1038
|
6 |
|
if (isset($item->formula1)) { |
1039
|
6 |
|
$childNode = $node->addChild('formula1'); |
1040
|
6 |
|
if ($childNode !== null) { // null should never happen |
1041
|
|
|
// see https://github.com/phpstan/phpstan/issues/8236 |
1042
|
6 |
|
$childNode[0] = (string) $item->formula1->children(Namespaces::DATA_VALIDATIONS2)->f; // @phpstan-ignore-line |
1043
|
|
|
} |
1044
|
|
|
} |
1045
|
|
|
} |
1046
|
|
|
} |
1047
|
|
|
} |
1048
|
|
|
|
1049
|
654 |
|
if ($xmlSheet && $xmlSheet->dataValidations && !$this->readDataOnly) { |
1050
|
22 |
|
(new DataValidations($docSheet, $xmlSheet))->load(); |
1051
|
|
|
} |
1052
|
|
|
|
1053
|
|
|
// unparsed sheet AlternateContent |
1054
|
654 |
|
if ($xmlSheet && !$this->readDataOnly) { |
1055
|
642 |
|
$mc = $xmlSheet->children(Namespaces::COMPATIBILITY); |
1056
|
642 |
|
if ($mc->AlternateContent) { |
1057
|
4 |
|
foreach ($mc->AlternateContent as $alternateContent) { |
1058
|
4 |
|
$alternateContent = self::testSimpleXml($alternateContent); |
1059
|
4 |
|
$unparsedLoadedData['sheets'][$docSheet->getCodeName()]['AlternateContents'][] = $alternateContent->asXML(); |
1060
|
|
|
} |
1061
|
|
|
} |
1062
|
|
|
} |
1063
|
|
|
|
1064
|
|
|
// Add hyperlinks |
1065
|
654 |
|
if (!$this->readDataOnly) { |
1066
|
652 |
|
$hyperlinkReader = new Hyperlinks($docSheet); |
1067
|
|
|
// Locate hyperlink relations |
1068
|
652 |
|
$relationsFileName = dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels'; |
1069
|
652 |
|
if ($zip->locateName($relationsFileName) !== false) { |
1070
|
537 |
|
$relsWorksheet = $this->loadZip($relationsFileName, Namespaces::RELATIONSHIPS); |
1071
|
537 |
|
$hyperlinkReader->readHyperlinks($relsWorksheet); |
1072
|
|
|
} |
1073
|
|
|
|
1074
|
|
|
// Loop through hyperlinks |
1075
|
652 |
|
if ($xmlSheetNS && $xmlSheetNS->children($mainNS)->hyperlinks) { |
1076
|
18 |
|
$hyperlinkReader->setHyperlinks($xmlSheetNS->children($mainNS)->hyperlinks); |
1077
|
|
|
} |
1078
|
|
|
} |
1079
|
|
|
|
1080
|
|
|
// Add comments |
1081
|
654 |
|
$comments = []; |
1082
|
654 |
|
$vmlComments = []; |
1083
|
654 |
|
if (!$this->readDataOnly) { |
1084
|
|
|
// Locate comment relations |
1085
|
652 |
|
$commentRelations = dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels'; |
1086
|
652 |
|
if ($zip->locateName($commentRelations) !== false) { |
1087
|
537 |
|
$relsWorksheet = $this->loadZip($commentRelations, Namespaces::RELATIONSHIPS); |
1088
|
537 |
|
foreach ($relsWorksheet->Relationship as $elex) { |
1089
|
386 |
|
$ele = self::getAttributes($elex); |
1090
|
386 |
|
if ($ele['Type'] == Namespaces::COMMENTS) { |
1091
|
34 |
|
$comments[(string) $ele['Id']] = (string) $ele['Target']; |
1092
|
|
|
} |
1093
|
386 |
|
if ($ele['Type'] == Namespaces::VML) { |
1094
|
37 |
|
$vmlComments[(string) $ele['Id']] = (string) $ele['Target']; |
1095
|
|
|
} |
1096
|
|
|
} |
1097
|
|
|
} |
1098
|
|
|
|
1099
|
|
|
// Loop through comments |
1100
|
652 |
|
foreach ($comments as $relName => $relPath) { |
1101
|
|
|
// Load comments file |
1102
|
34 |
|
$relPath = File::realpath(dirname("$dir/$fileWorksheet") . '/' . $relPath); |
1103
|
|
|
// okay to ignore namespace - using xpath |
1104
|
34 |
|
$commentsFile = $this->loadZip($relPath, ''); |
1105
|
|
|
|
1106
|
|
|
// Utility variables |
1107
|
34 |
|
$authors = []; |
1108
|
34 |
|
$commentsFile->registerXpathNamespace('com', $mainNS); |
1109
|
34 |
|
$authorPath = self::xpathNoFalse($commentsFile, 'com:authors/com:author'); |
1110
|
34 |
|
foreach ($authorPath as $author) { |
1111
|
34 |
|
$authors[] = (string) $author; |
1112
|
|
|
} |
1113
|
|
|
|
1114
|
|
|
// Loop through contents |
1115
|
34 |
|
$contentPath = self::xpathNoFalse($commentsFile, 'com:commentList/com:comment'); |
1116
|
34 |
|
foreach ($contentPath as $comment) { |
1117
|
34 |
|
$commentx = $comment->attributes(); |
1118
|
34 |
|
$commentModel = $docSheet->getComment((string) $commentx['ref']); |
1119
|
34 |
|
if (isset($commentx['authorId'])) { |
1120
|
34 |
|
$commentModel->setAuthor($authors[(int) $commentx['authorId']]); |
1121
|
|
|
} |
1122
|
34 |
|
$commentModel->setText($this->parseRichText($comment->children($mainNS)->text)); |
1123
|
|
|
} |
1124
|
|
|
} |
1125
|
|
|
|
1126
|
|
|
// later we will remove from it real vmlComments |
1127
|
652 |
|
$unparsedVmlDrawings = $vmlComments; |
1128
|
652 |
|
$vmlDrawingContents = []; |
1129
|
|
|
|
1130
|
|
|
// Loop through VML comments |
1131
|
652 |
|
foreach ($vmlComments as $relName => $relPath) { |
1132
|
|
|
// Load VML comments file |
1133
|
37 |
|
$relPath = File::realpath(dirname("$dir/$fileWorksheet") . '/' . $relPath); |
1134
|
|
|
|
1135
|
|
|
try { |
1136
|
|
|
// no namespace okay - processed with Xpath |
1137
|
37 |
|
$vmlCommentsFile = $this->loadZip($relPath, '', true); |
1138
|
37 |
|
$vmlCommentsFile->registerXPathNamespace('v', Namespaces::URN_VML); |
1139
|
|
|
} catch (Throwable) { |
1140
|
|
|
//Ignore unparsable vmlDrawings. Later they will be moved from $unparsedVmlDrawings to $unparsedLoadedData |
1141
|
|
|
continue; |
1142
|
|
|
} |
1143
|
|
|
|
1144
|
|
|
// Locate VML drawings image relations |
1145
|
37 |
|
$drowingImages = []; |
1146
|
37 |
|
$VMLDrawingsRelations = dirname($relPath) . '/_rels/' . basename($relPath) . '.rels'; |
1147
|
37 |
|
$vmlDrawingContents[$relName] = $this->getSecurityScannerOrThrow()->scan($this->getFromZipArchive($zip, $relPath)); |
1148
|
37 |
|
if ($zip->locateName($VMLDrawingsRelations) !== false) { |
1149
|
17 |
|
$relsVMLDrawing = $this->loadZip($VMLDrawingsRelations, Namespaces::RELATIONSHIPS); |
1150
|
17 |
|
foreach ($relsVMLDrawing->Relationship as $elex) { |
1151
|
8 |
|
$ele = self::getAttributes($elex); |
1152
|
8 |
|
if ($ele['Type'] == Namespaces::IMAGE) { |
1153
|
8 |
|
$drowingImages[(string) $ele['Id']] = (string) $ele['Target']; |
1154
|
|
|
} |
1155
|
|
|
} |
1156
|
|
|
} |
1157
|
|
|
|
1158
|
37 |
|
$shapes = self::xpathNoFalse($vmlCommentsFile, '//v:shape'); |
1159
|
37 |
|
foreach ($shapes as $shape) { |
1160
|
36 |
|
$shape->registerXPathNamespace('v', Namespaces::URN_VML); |
1161
|
|
|
|
1162
|
36 |
|
if (isset($shape['style'])) { |
1163
|
36 |
|
$style = (string) $shape['style']; |
1164
|
36 |
|
$fillColor = strtoupper(substr((string) $shape['fillcolor'], 1)); |
1165
|
36 |
|
$column = null; |
1166
|
36 |
|
$row = null; |
1167
|
36 |
|
$textHAlign = null; |
1168
|
36 |
|
$fillImageRelId = null; |
1169
|
36 |
|
$fillImageTitle = ''; |
1170
|
|
|
|
1171
|
36 |
|
$clientData = $shape->xpath('.//x:ClientData'); |
1172
|
36 |
|
$textboxDirection = ''; |
1173
|
36 |
|
$textboxPath = $shape->xpath('.//v:textbox'); |
1174
|
36 |
|
$textbox = (string) ($textboxPath[0]['style'] ?? ''); |
1175
|
36 |
|
if (preg_match('/rtl/i', $textbox) === 1) { |
1176
|
1 |
|
$textboxDirection = Comment::TEXTBOX_DIRECTION_RTL; |
1177
|
35 |
|
} elseif (preg_match('/ltr/i', $textbox) === 1) { |
1178
|
1 |
|
$textboxDirection = Comment::TEXTBOX_DIRECTION_LTR; |
1179
|
|
|
} |
1180
|
36 |
|
if (is_array($clientData) && !empty($clientData)) { |
1181
|
|
|
/** @var SimpleXMLElement */ |
1182
|
35 |
|
$clientData = $clientData[0]; |
1183
|
|
|
|
1184
|
35 |
|
if (isset($clientData['ObjectType']) && (string) $clientData['ObjectType'] == 'Note') { |
1185
|
33 |
|
$temp = $clientData->xpath('.//x:Row'); |
1186
|
33 |
|
if (is_array($temp)) { |
1187
|
33 |
|
$row = $temp[0]; |
1188
|
|
|
} |
1189
|
|
|
|
1190
|
33 |
|
$temp = $clientData->xpath('.//x:Column'); |
1191
|
33 |
|
if (is_array($temp)) { |
1192
|
33 |
|
$column = $temp[0]; |
1193
|
|
|
} |
1194
|
33 |
|
$temp = $clientData->xpath('.//x:TextHAlign'); |
1195
|
33 |
|
if (!empty($temp)) { |
1196
|
2 |
|
$textHAlign = strtolower($temp[0]); |
1197
|
|
|
} |
1198
|
|
|
} |
1199
|
|
|
} |
1200
|
36 |
|
$rowx = (string) $row; |
1201
|
36 |
|
$colx = (string) $column; |
1202
|
36 |
|
if (is_numeric($rowx) && is_numeric($colx) && $textHAlign !== null) { |
1203
|
2 |
|
$docSheet->getComment([1 + (int) $colx, 1 + (int) $rowx], false)->setAlignment((string) $textHAlign); |
1204
|
|
|
} |
1205
|
36 |
|
if (is_numeric($rowx) && is_numeric($colx) && $textboxDirection !== '') { |
1206
|
2 |
|
$docSheet->getComment([1 + (int) $colx, 1 + (int) $rowx], false)->setTextboxDirection($textboxDirection); |
1207
|
|
|
} |
1208
|
|
|
|
1209
|
36 |
|
$fillImageRelNode = $shape->xpath('.//v:fill/@o:relid'); |
1210
|
36 |
|
if (is_array($fillImageRelNode) && !empty($fillImageRelNode)) { |
1211
|
|
|
/** @var SimpleXMLElement */ |
1212
|
5 |
|
$fillImageRelNode = $fillImageRelNode[0]; |
1213
|
|
|
|
1214
|
5 |
|
if (isset($fillImageRelNode['relid'])) { |
1215
|
5 |
|
$fillImageRelId = (string) $fillImageRelNode['relid']; |
1216
|
|
|
} |
1217
|
|
|
} |
1218
|
|
|
|
1219
|
36 |
|
$fillImageTitleNode = $shape->xpath('.//v:fill/@o:title'); |
1220
|
36 |
|
if (is_array($fillImageTitleNode) && !empty($fillImageTitleNode)) { |
1221
|
|
|
/** @var SimpleXMLElement */ |
1222
|
3 |
|
$fillImageTitleNode = $fillImageTitleNode[0]; |
1223
|
|
|
|
1224
|
3 |
|
if (isset($fillImageTitleNode['title'])) { |
1225
|
3 |
|
$fillImageTitle = (string) $fillImageTitleNode['title']; |
1226
|
|
|
} |
1227
|
|
|
} |
1228
|
|
|
|
1229
|
36 |
|
if (($column !== null) && ($row !== null)) { |
1230
|
|
|
// Set comment properties |
1231
|
33 |
|
$comment = $docSheet->getComment([(int) $column + 1, (int) $row + 1]); |
1232
|
33 |
|
$comment->getFillColor()->setRGB($fillColor); |
1233
|
33 |
|
if (isset($drowingImages[$fillImageRelId])) { |
1234
|
5 |
|
$objDrawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing(); |
1235
|
5 |
|
$objDrawing->setName($fillImageTitle); |
1236
|
5 |
|
$imagePath = str_replace(['../', '/xl/'], 'xl/', $drowingImages[$fillImageRelId]); |
1237
|
5 |
|
$objDrawing->setPath( |
1238
|
5 |
|
'zip://' . File::realpath($filename) . '#' . $imagePath, |
1239
|
5 |
|
true, |
1240
|
5 |
|
$zip |
1241
|
5 |
|
); |
1242
|
5 |
|
$comment->setBackgroundImage($objDrawing); |
1243
|
|
|
} |
1244
|
|
|
|
1245
|
|
|
// Parse style |
1246
|
33 |
|
$styleArray = explode(';', str_replace(' ', '', $style)); |
1247
|
33 |
|
foreach ($styleArray as $stylePair) { |
1248
|
33 |
|
$stylePair = explode(':', $stylePair); |
1249
|
|
|
|
1250
|
33 |
|
if ($stylePair[0] == 'margin-left') { |
1251
|
29 |
|
$comment->setMarginLeft($stylePair[1]); |
1252
|
|
|
} |
1253
|
33 |
|
if ($stylePair[0] == 'margin-top') { |
1254
|
29 |
|
$comment->setMarginTop($stylePair[1]); |
1255
|
|
|
} |
1256
|
33 |
|
if ($stylePair[0] == 'width') { |
1257
|
29 |
|
$comment->setWidth($stylePair[1]); |
1258
|
|
|
} |
1259
|
33 |
|
if ($stylePair[0] == 'height') { |
1260
|
29 |
|
$comment->setHeight($stylePair[1]); |
1261
|
|
|
} |
1262
|
33 |
|
if ($stylePair[0] == 'visibility') { |
1263
|
33 |
|
$comment->setVisible($stylePair[1] == 'visible'); |
1264
|
|
|
} |
1265
|
|
|
} |
1266
|
|
|
|
1267
|
33 |
|
unset($unparsedVmlDrawings[$relName]); |
1268
|
|
|
} |
1269
|
|
|
} |
1270
|
|
|
} |
1271
|
|
|
} |
1272
|
|
|
|
1273
|
|
|
// unparsed vmlDrawing |
1274
|
652 |
|
if ($unparsedVmlDrawings) { |
1275
|
6 |
|
foreach ($unparsedVmlDrawings as $rId => $relPath) { |
1276
|
6 |
|
$rId = substr($rId, 3); // rIdXXX |
1277
|
6 |
|
$unparsedVmlDrawing = &$unparsedLoadedData['sheets'][$docSheet->getCodeName()]['vmlDrawings']; |
1278
|
6 |
|
$unparsedVmlDrawing[$rId] = []; |
1279
|
6 |
|
$unparsedVmlDrawing[$rId]['filePath'] = self::dirAdd("$dir/$fileWorksheet", $relPath); |
1280
|
6 |
|
$unparsedVmlDrawing[$rId]['relFilePath'] = $relPath; |
1281
|
6 |
|
$unparsedVmlDrawing[$rId]['content'] = $this->getSecurityScannerOrThrow()->scan($this->getFromZipArchive($zip, $unparsedVmlDrawing[$rId]['filePath'])); |
1282
|
6 |
|
unset($unparsedVmlDrawing); |
1283
|
|
|
} |
1284
|
|
|
} |
1285
|
|
|
|
1286
|
|
|
// Header/footer images |
1287
|
652 |
|
if ($xmlSheetNS && $xmlSheetNS->legacyDrawingHF) { |
1288
|
2 |
|
$vmlHfRid = ''; |
1289
|
2 |
|
$vmlHfRidAttr = $xmlSheetNS->legacyDrawingHF->attributes(Namespaces::SCHEMA_OFFICE_DOCUMENT); |
1290
|
2 |
|
if ($vmlHfRidAttr !== null && isset($vmlHfRidAttr['id'])) { |
1291
|
2 |
|
$vmlHfRid = (string) $vmlHfRidAttr['id'][0]; |
1292
|
|
|
} |
1293
|
2 |
|
if ($zip->locateName(dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels') !== false) { |
1294
|
2 |
|
$relsWorksheet = $this->loadZipNoNamespace(dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels', Namespaces::RELATIONSHIPS); |
1295
|
2 |
|
$vmlRelationship = ''; |
1296
|
|
|
|
1297
|
2 |
|
foreach ($relsWorksheet->Relationship as $ele) { |
1298
|
2 |
|
if ((string) $ele['Type'] == Namespaces::VML && (string) $ele['Id'] === $vmlHfRid) { |
1299
|
2 |
|
$vmlRelationship = self::dirAdd("$dir/$fileWorksheet", $ele['Target']); |
1300
|
|
|
|
1301
|
2 |
|
break; |
1302
|
|
|
} |
1303
|
|
|
} |
1304
|
|
|
|
1305
|
2 |
|
if ($vmlRelationship != '') { |
1306
|
|
|
// Fetch linked images |
1307
|
2 |
|
$relsVML = $this->loadZipNoNamespace(dirname($vmlRelationship) . '/_rels/' . basename($vmlRelationship) . '.rels', Namespaces::RELATIONSHIPS); |
1308
|
2 |
|
$drawings = []; |
1309
|
2 |
|
if (isset($relsVML->Relationship)) { |
1310
|
2 |
|
foreach ($relsVML->Relationship as $ele) { |
1311
|
2 |
|
if ($ele['Type'] == Namespaces::IMAGE) { |
1312
|
2 |
|
$drawings[(string) $ele['Id']] = self::dirAdd($vmlRelationship, $ele['Target']); |
1313
|
|
|
} |
1314
|
|
|
} |
1315
|
|
|
} |
1316
|
|
|
// Fetch VML document |
1317
|
2 |
|
$vmlDrawing = $this->loadZipNoNamespace($vmlRelationship, ''); |
1318
|
2 |
|
$vmlDrawing->registerXPathNamespace('v', Namespaces::URN_VML); |
1319
|
|
|
|
1320
|
2 |
|
$hfImages = []; |
1321
|
|
|
|
1322
|
2 |
|
$shapes = self::xpathNoFalse($vmlDrawing, '//v:shape'); |
1323
|
2 |
|
foreach ($shapes as $idx => $shape) { |
1324
|
2 |
|
$shape->registerXPathNamespace('v', Namespaces::URN_VML); |
1325
|
2 |
|
$imageData = $shape->xpath('//v:imagedata'); |
1326
|
|
|
|
1327
|
2 |
|
if (empty($imageData)) { |
1328
|
|
|
continue; |
1329
|
|
|
} |
1330
|
|
|
|
1331
|
2 |
|
$imageData = $imageData[$idx]; |
1332
|
|
|
|
1333
|
2 |
|
$imageData = self::getAttributes($imageData, Namespaces::URN_MSOFFICE); |
1334
|
2 |
|
$style = self::toCSSArray((string) $shape['style']); |
1335
|
|
|
|
1336
|
2 |
|
if (array_key_exists((string) $imageData['relid'], $drawings)) { |
1337
|
2 |
|
$shapeId = (string) $shape['id']; |
1338
|
2 |
|
$hfImages[$shapeId] = new HeaderFooterDrawing(); |
1339
|
2 |
|
if (isset($imageData['title'])) { |
1340
|
2 |
|
$hfImages[$shapeId]->setName((string) $imageData['title']); |
1341
|
|
|
} |
1342
|
|
|
|
1343
|
2 |
|
$hfImages[$shapeId]->setPath('zip://' . File::realpath($filename) . '#' . $drawings[(string) $imageData['relid']], false, $zip); |
1344
|
2 |
|
$hfImages[$shapeId]->setResizeProportional(false); |
1345
|
2 |
|
$hfImages[$shapeId]->setWidth($style['width']); |
1346
|
2 |
|
$hfImages[$shapeId]->setHeight($style['height']); |
1347
|
2 |
|
if (isset($style['margin-left'])) { |
1348
|
2 |
|
$hfImages[$shapeId]->setOffsetX($style['margin-left']); |
1349
|
|
|
} |
1350
|
2 |
|
$hfImages[$shapeId]->setOffsetY($style['margin-top']); |
1351
|
2 |
|
$hfImages[$shapeId]->setResizeProportional(true); |
1352
|
|
|
} |
1353
|
|
|
} |
1354
|
|
|
|
1355
|
2 |
|
$docSheet->getHeaderFooter()->setImages($hfImages); |
1356
|
|
|
} |
1357
|
|
|
} |
1358
|
|
|
} |
1359
|
|
|
} |
1360
|
|
|
|
1361
|
|
|
// TODO: Autoshapes from twoCellAnchors! |
1362
|
654 |
|
$drawingFilename = dirname("$dir/$fileWorksheet") |
1363
|
654 |
|
. '/_rels/' |
1364
|
654 |
|
. basename($fileWorksheet) |
1365
|
654 |
|
. '.rels'; |
1366
|
654 |
|
if (str_starts_with($drawingFilename, 'xl//xl/')) { |
1367
|
|
|
$drawingFilename = substr($drawingFilename, 4); |
1368
|
|
|
} |
1369
|
654 |
|
if (str_starts_with($drawingFilename, '/xl//xl/')) { |
1370
|
|
|
$drawingFilename = substr($drawingFilename, 5); |
1371
|
|
|
} |
1372
|
654 |
|
if ($zip->locateName($drawingFilename) !== false) { |
1373
|
539 |
|
$relsWorksheet = $this->loadZip($drawingFilename, Namespaces::RELATIONSHIPS); |
1374
|
539 |
|
$drawings = []; |
1375
|
539 |
|
foreach ($relsWorksheet->Relationship as $elex) { |
1376
|
387 |
|
$ele = self::getAttributes($elex); |
1377
|
387 |
|
if ((string) $ele['Type'] === "$xmlNamespaceBase/drawing") { |
1378
|
119 |
|
$eleTarget = (string) $ele['Target']; |
1379
|
119 |
|
if (str_starts_with($eleTarget, '/xl/')) { |
1380
|
4 |
|
$drawings[(string) $ele['Id']] = substr($eleTarget, 1); |
1381
|
|
|
} else { |
1382
|
116 |
|
$drawings[(string) $ele['Id']] = self::dirAdd("$dir/$fileWorksheet", $ele['Target']); |
1383
|
|
|
} |
1384
|
|
|
} |
1385
|
|
|
} |
1386
|
|
|
|
1387
|
539 |
|
if ($xmlSheetNS->drawing && !$this->readDataOnly) { |
1388
|
119 |
|
$unparsedDrawings = []; |
1389
|
119 |
|
$fileDrawing = null; |
1390
|
119 |
|
foreach ($xmlSheetNS->drawing as $drawing) { |
1391
|
119 |
|
$drawingRelId = self::getArrayItemString(self::getAttributes($drawing, $xmlNamespaceBase), 'id'); |
1392
|
119 |
|
$fileDrawing = $drawings[$drawingRelId]; |
1393
|
119 |
|
$drawingFilename = dirname($fileDrawing) . '/_rels/' . basename($fileDrawing) . '.rels'; |
1394
|
119 |
|
$relsDrawing = $this->loadZip($drawingFilename, Namespaces::RELATIONSHIPS); |
1395
|
|
|
|
1396
|
119 |
|
$images = []; |
1397
|
119 |
|
$hyperlinks = []; |
1398
|
119 |
|
if ($relsDrawing && $relsDrawing->Relationship) { |
1399
|
109 |
|
foreach ($relsDrawing->Relationship as $elex) { |
1400
|
109 |
|
$ele = self::getAttributes($elex); |
1401
|
109 |
|
$eleType = (string) $ele['Type']; |
1402
|
109 |
|
if ($eleType === Namespaces::HYPERLINK) { |
1403
|
3 |
|
$hyperlinks[(string) $ele['Id']] = (string) $ele['Target']; |
1404
|
|
|
} |
1405
|
109 |
|
if ($eleType === "$xmlNamespaceBase/image") { |
1406
|
59 |
|
$eleTarget = (string) $ele['Target']; |
1407
|
59 |
|
if (str_starts_with($eleTarget, '/xl/')) { |
1408
|
1 |
|
$eleTarget = substr($eleTarget, 1); |
1409
|
1 |
|
$images[(string) $ele['Id']] = $eleTarget; |
1410
|
|
|
} else { |
1411
|
58 |
|
$images[(string) $ele['Id']] = self::dirAdd($fileDrawing, $eleTarget); |
1412
|
|
|
} |
1413
|
74 |
|
} elseif ($eleType === "$xmlNamespaceBase/chart") { |
1414
|
70 |
|
if ($this->includeCharts) { |
1415
|
69 |
|
$eleTarget = (string) $ele['Target']; |
1416
|
69 |
|
if (str_starts_with($eleTarget, '/xl/')) { |
1417
|
3 |
|
$index = substr($eleTarget, 1); |
1418
|
|
|
} else { |
1419
|
67 |
|
$index = self::dirAdd($fileDrawing, $eleTarget); |
1420
|
|
|
} |
1421
|
69 |
|
$charts[$index] = [ |
1422
|
69 |
|
'id' => (string) $ele['Id'], |
1423
|
69 |
|
'sheet' => $docSheet->getTitle(), |
1424
|
69 |
|
]; |
1425
|
|
|
} |
1426
|
|
|
} |
1427
|
|
|
} |
1428
|
|
|
} |
1429
|
|
|
|
1430
|
119 |
|
$xmlDrawing = $this->loadZipNoNamespace($fileDrawing, ''); |
1431
|
119 |
|
$xmlDrawingChildren = $xmlDrawing->children(Namespaces::SPREADSHEET_DRAWING); |
1432
|
|
|
|
1433
|
119 |
|
if ($xmlDrawingChildren->oneCellAnchor) { |
1434
|
23 |
|
foreach ($xmlDrawingChildren->oneCellAnchor as $oneCellAnchor) { |
1435
|
23 |
|
$oneCellAnchor = self::testSimpleXml($oneCellAnchor); |
1436
|
23 |
|
if ($oneCellAnchor->pic->blipFill) { |
1437
|
16 |
|
$objDrawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing(); |
1438
|
16 |
|
$blip = $oneCellAnchor->pic->blipFill->children(Namespaces::DRAWINGML)->blip; |
1439
|
16 |
|
if (isset($blip, $blip->alphaModFix)) { |
1440
|
1 |
|
$temp = (string) $blip->alphaModFix->attributes()->amt; |
1441
|
1 |
|
if (is_numeric($temp)) { |
1442
|
1 |
|
$objDrawing->setOpacity((int) $temp); |
1443
|
|
|
} |
1444
|
|
|
} |
1445
|
16 |
|
$xfrm = $oneCellAnchor->pic->spPr->children(Namespaces::DRAWINGML)->xfrm; |
1446
|
16 |
|
$outerShdw = $oneCellAnchor->pic->spPr->children(Namespaces::DRAWINGML)->effectLst->outerShdw; |
1447
|
|
|
|
1448
|
16 |
|
$objDrawing->setName(self::getArrayItemString(self::getAttributes($oneCellAnchor->pic->nvPicPr->cNvPr), 'name')); |
1449
|
16 |
|
$objDrawing->setDescription(self::getArrayItemString(self::getAttributes($oneCellAnchor->pic->nvPicPr->cNvPr), 'descr')); |
1450
|
16 |
|
$embedImageKey = self::getArrayItemString( |
1451
|
16 |
|
self::getAttributes($blip, $xmlNamespaceBase), |
1452
|
16 |
|
'embed' |
1453
|
16 |
|
); |
1454
|
16 |
|
if (isset($images[$embedImageKey])) { |
1455
|
16 |
|
$objDrawing->setPath( |
1456
|
16 |
|
'zip://' . File::realpath($filename) . '#' |
1457
|
16 |
|
. $images[$embedImageKey], |
1458
|
16 |
|
false, |
1459
|
16 |
|
$zip |
1460
|
16 |
|
); |
1461
|
|
|
} else { |
1462
|
|
|
$linkImageKey = self::getArrayItemString( |
1463
|
|
|
$blip->attributes('http://schemas.openxmlformats.org/officeDocument/2006/relationships'), |
1464
|
|
|
'link' |
1465
|
|
|
); |
1466
|
|
|
if (isset($images[$linkImageKey])) { |
1467
|
|
|
$url = str_replace('xl/drawings/', '', $images[$linkImageKey]); |
1468
|
|
|
$objDrawing->setPath($url, false); |
1469
|
|
|
} |
1470
|
|
|
if ($objDrawing->getPath() === '') { |
1471
|
|
|
continue; |
1472
|
|
|
} |
1473
|
|
|
} |
1474
|
16 |
|
$objDrawing->setCoordinates(Coordinate::stringFromColumnIndex(((int) $oneCellAnchor->from->col) + 1) . ($oneCellAnchor->from->row + 1)); |
1475
|
|
|
|
1476
|
16 |
|
$objDrawing->setOffsetX((int) Drawing::EMUToPixels($oneCellAnchor->from->colOff)); |
1477
|
16 |
|
$objDrawing->setOffsetY(Drawing::EMUToPixels($oneCellAnchor->from->rowOff)); |
1478
|
16 |
|
$objDrawing->setResizeProportional(false); |
1479
|
16 |
|
$objDrawing->setWidth(Drawing::EMUToPixels(self::getArrayItemIntOrSxml(self::getAttributes($oneCellAnchor->ext), 'cx'))); |
1480
|
16 |
|
$objDrawing->setHeight(Drawing::EMUToPixels(self::getArrayItemIntOrSxml(self::getAttributes($oneCellAnchor->ext), 'cy'))); |
1481
|
16 |
|
if ($xfrm) { |
1482
|
16 |
|
$objDrawing->setRotation((int) Drawing::angleToDegrees(self::getArrayItemIntOrSxml(self::getAttributes($xfrm), 'rot'))); |
1483
|
16 |
|
$objDrawing->setFlipVertical((bool) self::getArrayItem(self::getAttributes($xfrm), 'flipV')); |
1484
|
16 |
|
$objDrawing->setFlipHorizontal((bool) self::getArrayItem(self::getAttributes($xfrm), 'flipH')); |
1485
|
|
|
} |
1486
|
16 |
|
if ($outerShdw) { |
1487
|
3 |
|
$shadow = $objDrawing->getShadow(); |
1488
|
3 |
|
$shadow->setVisible(true); |
1489
|
3 |
|
$shadow->setBlurRadius(Drawing::EMUToPixels(self::getArrayItemIntOrSxml(self::getAttributes($outerShdw), 'blurRad'))); |
1490
|
3 |
|
$shadow->setDistance(Drawing::EMUToPixels(self::getArrayItemIntOrSxml(self::getAttributes($outerShdw), 'dist'))); |
1491
|
3 |
|
$shadow->setDirection(Drawing::angleToDegrees(self::getArrayItemIntOrSxml(self::getAttributes($outerShdw), 'dir'))); |
1492
|
3 |
|
$shadow->setAlignment(self::getArrayItemString(self::getAttributes($outerShdw), 'algn')); |
1493
|
3 |
|
$clr = $outerShdw->srgbClr ?? $outerShdw->prstClr; |
1494
|
3 |
|
$shadow->getColor()->setRGB(self::getArrayItemString(self::getAttributes($clr), 'val')); |
1495
|
3 |
|
if ($clr->alpha) { |
1496
|
3 |
|
$alpha = StringHelper::convertToString(self::getArrayItem(self::getAttributes($clr->alpha), 'val')); |
1497
|
3 |
|
if (is_numeric($alpha)) { |
1498
|
3 |
|
$alpha = (int) ($alpha / 1000); |
1499
|
3 |
|
$shadow->setAlpha($alpha); |
1500
|
|
|
} |
1501
|
|
|
} |
1502
|
|
|
} |
1503
|
|
|
|
1504
|
16 |
|
$this->readHyperLinkDrawing($objDrawing, $oneCellAnchor, $hyperlinks); |
1505
|
|
|
|
1506
|
16 |
|
$objDrawing->setWorksheet($docSheet); |
1507
|
7 |
|
} elseif ($this->includeCharts && $oneCellAnchor->graphicFrame) { |
1508
|
|
|
// Exported XLSX from Google Sheets positions charts with a oneCellAnchor |
1509
|
4 |
|
$coordinates = Coordinate::stringFromColumnIndex(((int) $oneCellAnchor->from->col) + 1) . ($oneCellAnchor->from->row + 1); |
1510
|
4 |
|
$offsetX = Drawing::EMUToPixels($oneCellAnchor->from->colOff); |
1511
|
4 |
|
$offsetY = Drawing::EMUToPixels($oneCellAnchor->from->rowOff); |
1512
|
4 |
|
$width = Drawing::EMUToPixels(self::getArrayItemIntOrSxml(self::getAttributes($oneCellAnchor->ext), 'cx')); |
1513
|
4 |
|
$height = Drawing::EMUToPixels(self::getArrayItemIntOrSxml(self::getAttributes($oneCellAnchor->ext), 'cy')); |
1514
|
|
|
|
1515
|
4 |
|
$graphic = $oneCellAnchor->graphicFrame->children(Namespaces::DRAWINGML)->graphic; |
1516
|
4 |
|
$chartRef = $graphic->graphicData->children(Namespaces::CHART)->chart; |
1517
|
4 |
|
$thisChart = (string) self::getAttributes($chartRef, $xmlNamespaceBase); |
1518
|
|
|
|
1519
|
4 |
|
$chartDetails[$docSheet->getTitle() . '!' . $thisChart] = [ |
1520
|
4 |
|
'fromCoordinate' => $coordinates, |
1521
|
4 |
|
'fromOffsetX' => $offsetX, |
1522
|
4 |
|
'fromOffsetY' => $offsetY, |
1523
|
4 |
|
'width' => $width, |
1524
|
4 |
|
'height' => $height, |
1525
|
4 |
|
'worksheetTitle' => $docSheet->getTitle(), |
1526
|
4 |
|
'oneCellAnchor' => true, |
1527
|
4 |
|
]; |
1528
|
|
|
} |
1529
|
|
|
} |
1530
|
|
|
} |
1531
|
119 |
|
if ($xmlDrawingChildren->twoCellAnchor) { |
1532
|
92 |
|
foreach ($xmlDrawingChildren->twoCellAnchor as $twoCellAnchor) { |
1533
|
92 |
|
$twoCellAnchor = self::testSimpleXml($twoCellAnchor); |
1534
|
92 |
|
if ($twoCellAnchor->pic->blipFill) { |
1535
|
45 |
|
$objDrawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing(); |
1536
|
45 |
|
$blip = $twoCellAnchor->pic->blipFill->children(Namespaces::DRAWINGML)->blip; |
1537
|
45 |
|
if (isset($blip, $blip->alphaModFix)) { |
1538
|
3 |
|
$temp = (string) $blip->alphaModFix->attributes()->amt; |
1539
|
3 |
|
if (is_numeric($temp)) { |
1540
|
3 |
|
$objDrawing->setOpacity((int) $temp); |
1541
|
|
|
} |
1542
|
|
|
} |
1543
|
45 |
|
if (isset($twoCellAnchor->pic->blipFill->children(Namespaces::DRAWINGML)->srcRect)) { |
1544
|
7 |
|
$objDrawing->setSrcRect($twoCellAnchor->pic->blipFill->children(Namespaces::DRAWINGML)->srcRect->attributes()); |
1545
|
|
|
} |
1546
|
45 |
|
$xfrm = $twoCellAnchor->pic->spPr->children(Namespaces::DRAWINGML)->xfrm; |
1547
|
45 |
|
$outerShdw = $twoCellAnchor->pic->spPr->children(Namespaces::DRAWINGML)->effectLst->outerShdw; |
1548
|
45 |
|
$editAs = $twoCellAnchor->attributes(); |
1549
|
45 |
|
if (isset($editAs, $editAs['editAs'])) { |
1550
|
40 |
|
$objDrawing->setEditAs($editAs['editAs']); |
1551
|
|
|
} |
1552
|
45 |
|
$objDrawing->setName((string) self::getArrayItemString(self::getAttributes($twoCellAnchor->pic->nvPicPr->cNvPr), 'name')); |
1553
|
45 |
|
$objDrawing->setDescription(self::getArrayItemString(self::getAttributes($twoCellAnchor->pic->nvPicPr->cNvPr), 'descr')); |
1554
|
45 |
|
$embedImageKey = self::getArrayItemString( |
1555
|
45 |
|
self::getAttributes($blip, $xmlNamespaceBase), |
1556
|
45 |
|
'embed' |
1557
|
45 |
|
); |
1558
|
45 |
|
if (isset($images[$embedImageKey])) { |
1559
|
42 |
|
$objDrawing->setPath( |
1560
|
42 |
|
'zip://' . File::realpath($filename) . '#' |
1561
|
42 |
|
. $images[$embedImageKey], |
1562
|
42 |
|
false, |
1563
|
42 |
|
$zip |
1564
|
42 |
|
); |
1565
|
|
|
} else { |
1566
|
3 |
|
$linkImageKey = self::getArrayItemString( |
1567
|
3 |
|
$blip->attributes('http://schemas.openxmlformats.org/officeDocument/2006/relationships'), |
1568
|
3 |
|
'link' |
1569
|
3 |
|
); |
1570
|
3 |
|
if (isset($images[$linkImageKey])) { |
1571
|
3 |
|
$url = str_replace('xl/drawings/', '', $images[$linkImageKey]); |
1572
|
3 |
|
$objDrawing->setPath($url, false); |
1573
|
|
|
} |
1574
|
2 |
|
if ($objDrawing->getPath() === '') { |
1575
|
1 |
|
continue; |
1576
|
|
|
} |
1577
|
|
|
} |
1578
|
43 |
|
$objDrawing->setCoordinates(Coordinate::stringFromColumnIndex(((int) $twoCellAnchor->from->col) + 1) . ($twoCellAnchor->from->row + 1)); |
1579
|
|
|
|
1580
|
43 |
|
$objDrawing->setOffsetX(Drawing::EMUToPixels($twoCellAnchor->from->colOff)); |
1581
|
43 |
|
$objDrawing->setOffsetY(Drawing::EMUToPixels($twoCellAnchor->from->rowOff)); |
1582
|
|
|
|
1583
|
43 |
|
$objDrawing->setCoordinates2(Coordinate::stringFromColumnIndex(((int) $twoCellAnchor->to->col) + 1) . ($twoCellAnchor->to->row + 1)); |
1584
|
|
|
|
1585
|
43 |
|
$objDrawing->setOffsetX2(Drawing::EMUToPixels($twoCellAnchor->to->colOff)); |
1586
|
43 |
|
$objDrawing->setOffsetY2(Drawing::EMUToPixels($twoCellAnchor->to->rowOff)); |
1587
|
|
|
|
1588
|
43 |
|
$objDrawing->setResizeProportional(false); |
1589
|
|
|
|
1590
|
43 |
|
if ($xfrm) { |
1591
|
43 |
|
$objDrawing->setWidth(Drawing::EMUToPixels(self::getArrayItemIntOrSxml(self::getAttributes($xfrm->ext), 'cx'))); |
1592
|
43 |
|
$objDrawing->setHeight(Drawing::EMUToPixels(self::getArrayItemIntOrSxml(self::getAttributes($xfrm->ext), 'cy'))); |
1593
|
43 |
|
$objDrawing->setRotation(Drawing::angleToDegrees(self::getArrayItemIntOrSxml(self::getAttributes($xfrm), 'rot'))); |
1594
|
43 |
|
$objDrawing->setFlipVertical((bool) self::getArrayItem(self::getAttributes($xfrm), 'flipV')); |
1595
|
43 |
|
$objDrawing->setFlipHorizontal((bool) self::getArrayItem(self::getAttributes($xfrm), 'flipH')); |
1596
|
|
|
} |
1597
|
43 |
|
if ($outerShdw) { |
1598
|
1 |
|
$shadow = $objDrawing->getShadow(); |
1599
|
1 |
|
$shadow->setVisible(true); |
1600
|
1 |
|
$shadow->setBlurRadius(Drawing::EMUToPixels(self::getArrayItemIntOrSxml(self::getAttributes($outerShdw), 'blurRad'))); |
1601
|
1 |
|
$shadow->setDistance(Drawing::EMUToPixels(self::getArrayItemIntOrSxml(self::getAttributes($outerShdw), 'dist'))); |
1602
|
1 |
|
$shadow->setDirection(Drawing::angleToDegrees(self::getArrayItemIntOrSxml(self::getAttributes($outerShdw), 'dir'))); |
1603
|
1 |
|
$shadow->setAlignment(self::getArrayItemString(self::getAttributes($outerShdw), 'algn')); |
1604
|
1 |
|
$clr = $outerShdw->srgbClr ?? $outerShdw->prstClr; |
1605
|
1 |
|
$shadow->getColor()->setRGB(self::getArrayItemString(self::getAttributes($clr), 'val')); |
1606
|
1 |
|
if ($clr->alpha) { |
1607
|
1 |
|
$alpha = StringHelper::convertToString(self::getArrayItem(self::getAttributes($clr->alpha), 'val')); |
1608
|
1 |
|
if (is_numeric($alpha)) { |
1609
|
1 |
|
$alpha = (int) ($alpha / 1000); |
1610
|
1 |
|
$shadow->setAlpha($alpha); |
1611
|
|
|
} |
1612
|
|
|
} |
1613
|
|
|
} |
1614
|
|
|
|
1615
|
43 |
|
$this->readHyperLinkDrawing($objDrawing, $twoCellAnchor, $hyperlinks); |
1616
|
|
|
|
1617
|
43 |
|
$objDrawing->setWorksheet($docSheet); |
1618
|
71 |
|
} elseif (($this->includeCharts) && ($twoCellAnchor->graphicFrame)) { |
1619
|
65 |
|
$fromCoordinate = Coordinate::stringFromColumnIndex(((int) $twoCellAnchor->from->col) + 1) . ($twoCellAnchor->from->row + 1); |
1620
|
65 |
|
$fromOffsetX = Drawing::EMUToPixels($twoCellAnchor->from->colOff); |
1621
|
65 |
|
$fromOffsetY = Drawing::EMUToPixels($twoCellAnchor->from->rowOff); |
1622
|
65 |
|
$toCoordinate = Coordinate::stringFromColumnIndex(((int) $twoCellAnchor->to->col) + 1) . ($twoCellAnchor->to->row + 1); |
1623
|
65 |
|
$toOffsetX = Drawing::EMUToPixels($twoCellAnchor->to->colOff); |
1624
|
65 |
|
$toOffsetY = Drawing::EMUToPixels($twoCellAnchor->to->rowOff); |
1625
|
65 |
|
$graphic = $twoCellAnchor->graphicFrame->children(Namespaces::DRAWINGML)->graphic; |
1626
|
65 |
|
$chartRef = $graphic->graphicData->children(Namespaces::CHART)->chart; |
1627
|
65 |
|
$thisChart = (string) self::getAttributes($chartRef, $xmlNamespaceBase); |
1628
|
|
|
|
1629
|
65 |
|
$chartDetails[$docSheet->getTitle() . '!' . $thisChart] = [ |
1630
|
65 |
|
'fromCoordinate' => $fromCoordinate, |
1631
|
65 |
|
'fromOffsetX' => $fromOffsetX, |
1632
|
65 |
|
'fromOffsetY' => $fromOffsetY, |
1633
|
65 |
|
'toCoordinate' => $toCoordinate, |
1634
|
65 |
|
'toOffsetX' => $toOffsetX, |
1635
|
65 |
|
'toOffsetY' => $toOffsetY, |
1636
|
65 |
|
'worksheetTitle' => $docSheet->getTitle(), |
1637
|
65 |
|
]; |
1638
|
|
|
} |
1639
|
|
|
} |
1640
|
|
|
} |
1641
|
118 |
|
if ($xmlDrawingChildren->absoluteAnchor) { |
1642
|
1 |
|
foreach ($xmlDrawingChildren->absoluteAnchor as $absoluteAnchor) { |
1643
|
1 |
|
if (($this->includeCharts) && ($absoluteAnchor->graphicFrame)) { |
1644
|
1 |
|
$graphic = $absoluteAnchor->graphicFrame->children(Namespaces::DRAWINGML)->graphic; |
1645
|
1 |
|
$chartRef = $graphic->graphicData->children(Namespaces::CHART)->chart; |
1646
|
1 |
|
$thisChart = (string) self::getAttributes($chartRef, $xmlNamespaceBase); |
1647
|
1 |
|
$width = Drawing::EMUToPixels((int) self::getArrayItemString(self::getAttributes($absoluteAnchor->ext), 'cx')[0]); |
1648
|
1 |
|
$height = Drawing::EMUToPixels((int) self::getArrayItemString(self::getAttributes($absoluteAnchor->ext), 'cy')[0]); |
1649
|
|
|
|
1650
|
1 |
|
$chartDetails[$docSheet->getTitle() . '!' . $thisChart] = [ |
1651
|
1 |
|
'fromCoordinate' => 'A1', |
1652
|
1 |
|
'fromOffsetX' => 0, |
1653
|
1 |
|
'fromOffsetY' => 0, |
1654
|
1 |
|
'width' => $width, |
1655
|
1 |
|
'height' => $height, |
1656
|
1 |
|
'worksheetTitle' => $docSheet->getTitle(), |
1657
|
1 |
|
]; |
1658
|
|
|
} |
1659
|
|
|
} |
1660
|
|
|
} |
1661
|
118 |
|
if (empty($relsDrawing) && $xmlDrawing->count() == 0) { |
1662
|
|
|
// Save Drawing without rels and children as unparsed |
1663
|
15 |
|
$unparsedDrawings[$drawingRelId] = $xmlDrawing->asXML(); |
1664
|
|
|
} |
1665
|
|
|
} |
1666
|
|
|
|
1667
|
|
|
// store original rId of drawing files |
1668
|
118 |
|
$unparsedLoadedData['sheets'][$docSheet->getCodeName()]['drawingOriginalIds'] = []; |
1669
|
118 |
|
foreach ($relsWorksheet->Relationship as $elex) { |
1670
|
118 |
|
$ele = self::getAttributes($elex); |
1671
|
118 |
|
if ((string) $ele['Type'] === "$xmlNamespaceBase/drawing") { |
1672
|
118 |
|
$drawingRelId = (string) $ele['Id']; |
1673
|
118 |
|
$unparsedLoadedData['sheets'][$docSheet->getCodeName()]['drawingOriginalIds'][(string) $ele['Target']] = $drawingRelId; |
1674
|
118 |
|
if (isset($unparsedDrawings[$drawingRelId])) { |
1675
|
15 |
|
$unparsedLoadedData['sheets'][$docSheet->getCodeName()]['Drawings'][$drawingRelId] = $unparsedDrawings[$drawingRelId]; |
1676
|
|
|
} |
1677
|
|
|
} |
1678
|
|
|
} |
1679
|
118 |
|
if ($xmlSheet->legacyDrawing && !$this->readDataOnly) { |
1680
|
13 |
|
foreach ($xmlSheet->legacyDrawing as $drawing) { |
1681
|
13 |
|
$drawingRelId = self::getArrayItemString(self::getAttributes($drawing, $xmlNamespaceBase), 'id'); |
1682
|
13 |
|
if (isset($vmlDrawingContents[$drawingRelId])) { |
1683
|
13 |
|
if (self::onlyNoteVml($vmlDrawingContents[$drawingRelId]) === false) { |
1684
|
5 |
|
$unparsedLoadedData['sheets'][$docSheet->getCodeName()]['legacyDrawing'] = $vmlDrawingContents[$drawingRelId]; |
1685
|
|
|
} |
1686
|
|
|
} |
1687
|
|
|
} |
1688
|
|
|
} |
1689
|
|
|
|
1690
|
|
|
// unparsed drawing AlternateContent |
1691
|
118 |
|
$xmlAltDrawing = $this->loadZip((string) $fileDrawing, Namespaces::COMPATIBILITY); |
1692
|
|
|
|
1693
|
118 |
|
if ($xmlAltDrawing->AlternateContent) { |
1694
|
4 |
|
foreach ($xmlAltDrawing->AlternateContent as $alternateContent) { |
1695
|
4 |
|
$alternateContent = self::testSimpleXml($alternateContent); |
1696
|
4 |
|
$unparsedLoadedData['sheets'][$docSheet->getCodeName()]['drawingAlternateContents'][] = $alternateContent->asXML(); |
1697
|
|
|
} |
1698
|
|
|
} |
1699
|
|
|
} |
1700
|
|
|
} |
1701
|
|
|
|
1702
|
653 |
|
$this->readFormControlProperties($excel, $dir, $fileWorksheet, $docSheet, $unparsedLoadedData); |
1703
|
653 |
|
$this->readPrinterSettings($excel, $dir, $fileWorksheet, $docSheet, $unparsedLoadedData); |
1704
|
|
|
|
1705
|
|
|
// Loop through definedNames |
1706
|
653 |
|
if ($xmlWorkbook->definedNames) { |
1707
|
346 |
|
foreach ($xmlWorkbook->definedNames->definedName as $definedName) { |
1708
|
|
|
// Extract range |
1709
|
104 |
|
$extractedRange = (string) $definedName; |
1710
|
104 |
|
if (($spos = strpos($extractedRange, '!')) !== false) { |
1711
|
86 |
|
$extractedRange = substr($extractedRange, 0, $spos) . str_replace('$', '', substr($extractedRange, $spos)); |
1712
|
|
|
} else { |
1713
|
33 |
|
$extractedRange = str_replace('$', '', $extractedRange); |
1714
|
|
|
} |
1715
|
|
|
|
1716
|
|
|
// Valid range? |
1717
|
104 |
|
if ($extractedRange == '') { |
1718
|
|
|
continue; |
1719
|
|
|
} |
1720
|
|
|
|
1721
|
|
|
// Some definedNames are only applicable if we are on the same sheet... |
1722
|
104 |
|
if ((string) $definedName['localSheetId'] != '' && (string) $definedName['localSheetId'] == $oldSheetId) { |
1723
|
|
|
// Switch on type |
1724
|
48 |
|
switch ((string) $definedName['name']) { |
1725
|
48 |
|
case '_xlnm._FilterDatabase': |
1726
|
18 |
|
if ((string) $definedName['hidden'] !== '1') { |
1727
|
|
|
$extractedRange = explode(',', $extractedRange); |
1728
|
|
|
foreach ($extractedRange as $range) { |
1729
|
|
|
$autoFilterRange = $range; |
1730
|
|
|
if (str_contains($autoFilterRange, ':')) { |
1731
|
|
|
$docSheet->getAutoFilter()->setRange($autoFilterRange); |
1732
|
|
|
} |
1733
|
|
|
} |
1734
|
|
|
} |
1735
|
|
|
|
1736
|
18 |
|
break; |
1737
|
30 |
|
case '_xlnm.Print_Titles': |
1738
|
|
|
// Split $extractedRange |
1739
|
3 |
|
$extractedRange = explode(',', $extractedRange); |
1740
|
|
|
|
1741
|
|
|
// Set print titles |
1742
|
3 |
|
foreach ($extractedRange as $range) { |
1743
|
3 |
|
$matches = []; |
1744
|
3 |
|
$range = str_replace('$', '', $range); |
1745
|
|
|
|
1746
|
|
|
// check for repeating columns, e g. 'A:A' or 'A:D' |
1747
|
3 |
|
if (preg_match('/!?([A-Z]+)\:([A-Z]+)$/', $range, $matches)) { |
1748
|
|
|
$docSheet->getPageSetup()->setColumnsToRepeatAtLeft([$matches[1], $matches[2]]); |
1749
|
3 |
|
} elseif (preg_match('/!?(\d+)\:(\d+)$/', $range, $matches)) { |
1750
|
|
|
// check for repeating rows, e.g. '1:1' or '1:5' |
1751
|
3 |
|
$docSheet->getPageSetup()->setRowsToRepeatAtTop([$matches[1], $matches[2]]); |
1752
|
|
|
} |
1753
|
|
|
} |
1754
|
|
|
|
1755
|
3 |
|
break; |
1756
|
29 |
|
case '_xlnm.Print_Area': |
1757
|
11 |
|
$rangeSets = preg_split("/('?(?:.*?)'?(?:![A-Z0-9]+:[A-Z0-9]+)),?/", $extractedRange, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) ?: []; |
1758
|
11 |
|
$newRangeSets = []; |
1759
|
11 |
|
foreach ($rangeSets as $rangeSet) { |
1760
|
11 |
|
[, $rangeSet] = Worksheet::extractSheetTitle($rangeSet, true); |
1761
|
11 |
|
if (empty($rangeSet)) { |
1762
|
|
|
continue; |
1763
|
|
|
} |
1764
|
11 |
|
if (!str_contains($rangeSet, ':')) { |
1765
|
|
|
$rangeSet = $rangeSet . ':' . $rangeSet; |
1766
|
|
|
} |
1767
|
11 |
|
$newRangeSets[] = str_replace('$', '', $rangeSet); |
1768
|
|
|
} |
1769
|
11 |
|
if (count($newRangeSets) > 0) { |
1770
|
11 |
|
$docSheet->getPageSetup()->setPrintArea(implode(',', $newRangeSets)); |
1771
|
|
|
} |
1772
|
|
|
|
1773
|
11 |
|
break; |
1774
|
|
|
default: |
1775
|
19 |
|
break; |
1776
|
|
|
} |
1777
|
|
|
} |
1778
|
|
|
} |
1779
|
|
|
} |
1780
|
|
|
|
1781
|
|
|
// Next sheet id |
1782
|
653 |
|
++$sheetId; |
1783
|
|
|
} |
1784
|
|
|
|
1785
|
|
|
// Loop through definedNames |
1786
|
654 |
|
if ($xmlWorkbook->definedNames) { |
1787
|
346 |
|
foreach ($xmlWorkbook->definedNames->definedName as $definedName) { |
1788
|
|
|
// Extract range |
1789
|
104 |
|
$extractedRange = (string) $definedName; |
1790
|
|
|
|
1791
|
|
|
// Valid range? |
1792
|
104 |
|
if ($extractedRange == '') { |
1793
|
|
|
continue; |
1794
|
|
|
} |
1795
|
|
|
|
1796
|
|
|
// Some definedNames are only applicable if we are on the same sheet... |
1797
|
104 |
|
if ((string) $definedName['localSheetId'] != '') { |
1798
|
|
|
// Local defined name |
1799
|
|
|
// Switch on type |
1800
|
48 |
|
switch ((string) $definedName['name']) { |
1801
|
48 |
|
case '_xlnm._FilterDatabase': |
1802
|
30 |
|
case '_xlnm.Print_Titles': |
1803
|
29 |
|
case '_xlnm.Print_Area': |
1804
|
31 |
|
break; |
1805
|
|
|
default: |
1806
|
19 |
|
if ($mapSheetId[(int) $definedName['localSheetId']] !== null) { |
1807
|
19 |
|
$range = Worksheet::extractSheetTitle($extractedRange, true); |
1808
|
19 |
|
$scope = $excel->getSheet($mapSheetId[(int) $definedName['localSheetId']]); |
1809
|
19 |
|
if (str_contains((string) $definedName, '!')) { |
1810
|
19 |
|
$range[0] = str_replace("''", "'", $range[0]); |
1811
|
19 |
|
$range[0] = str_replace("'", '', $range[0]); |
1812
|
19 |
|
if ($worksheet = $excel->getSheetByName($range[0])) { |
1813
|
19 |
|
$excel->addDefinedName(DefinedName::createInstance((string) $definedName['name'], $worksheet, $extractedRange, true, $scope)); |
1814
|
|
|
} else { |
1815
|
14 |
|
$excel->addDefinedName(DefinedName::createInstance((string) $definedName['name'], $scope, $extractedRange, true, $scope)); |
1816
|
|
|
} |
1817
|
|
|
} else { |
1818
|
|
|
$excel->addDefinedName(DefinedName::createInstance((string) $definedName['name'], $scope, $extractedRange, true)); |
1819
|
|
|
} |
1820
|
|
|
} |
1821
|
|
|
|
1822
|
19 |
|
break; |
1823
|
|
|
} |
1824
|
76 |
|
} elseif (!isset($definedName['localSheetId'])) { |
1825
|
|
|
// "Global" definedNames |
1826
|
76 |
|
$locatedSheet = null; |
1827
|
76 |
|
if (str_contains((string) $definedName, '!')) { |
1828
|
|
|
// Modify range, and extract the first worksheet reference |
1829
|
|
|
// Need to split on a comma or a space if not in quotes, and extract the first part. |
1830
|
57 |
|
$definedNameValueParts = preg_split("/[ ,](?=([^']*'[^']*')*[^']*$)/miuU", $extractedRange); |
1831
|
57 |
|
if (is_array($definedNameValueParts)) { |
1832
|
|
|
// Extract sheet name |
1833
|
57 |
|
[$extractedSheetName] = Worksheet::extractSheetTitle((string) $definedNameValueParts[0], true, true); |
1834
|
|
|
|
1835
|
|
|
// Locate sheet |
1836
|
57 |
|
$locatedSheet = $excel->getSheetByName("$extractedSheetName"); |
1837
|
|
|
} |
1838
|
|
|
} |
1839
|
|
|
|
1840
|
76 |
|
if ($locatedSheet === null && !DefinedName::testIfFormula($extractedRange)) { |
1841
|
1 |
|
$extractedRange = '#REF!'; |
1842
|
|
|
} |
1843
|
76 |
|
$excel->addDefinedName(DefinedName::createInstance((string) $definedName['name'], $locatedSheet, $extractedRange, false)); |
1844
|
|
|
} |
1845
|
|
|
} |
1846
|
|
|
} |
1847
|
|
|
} |
1848
|
|
|
|
1849
|
654 |
|
(new WorkbookView($excel))->viewSettings($xmlWorkbook, $mainNS, $mapSheetId, $this->readDataOnly); |
1850
|
|
|
|
1851
|
653 |
|
break; |
1852
|
|
|
} |
1853
|
|
|
} |
1854
|
|
|
|
1855
|
653 |
|
if (!$this->readDataOnly) { |
1856
|
651 |
|
$contentTypes = $this->loadZip('[Content_Types].xml'); |
1857
|
|
|
|
1858
|
|
|
// Default content types |
1859
|
651 |
|
foreach ($contentTypes->Default as $contentType) { |
1860
|
649 |
|
switch ($contentType['ContentType']) { |
1861
|
649 |
|
case 'application/vnd.openxmlformats-officedocument.spreadsheetml.printerSettings': |
1862
|
291 |
|
$unparsedLoadedData['default_content_types'][(string) $contentType['Extension']] = (string) $contentType['ContentType']; |
1863
|
|
|
|
1864
|
291 |
|
break; |
1865
|
|
|
} |
1866
|
|
|
} |
1867
|
|
|
|
1868
|
|
|
// Override content types |
1869
|
651 |
|
foreach ($contentTypes->Override as $contentType) { |
1870
|
650 |
|
switch ($contentType['ContentType']) { |
1871
|
650 |
|
case 'application/vnd.openxmlformats-officedocument.drawingml.chart+xml': |
1872
|
71 |
|
if ($this->includeCharts) { |
1873
|
69 |
|
$chartEntryRef = ltrim((string) $contentType['PartName'], '/'); |
1874
|
69 |
|
$chartElements = $this->loadZip($chartEntryRef); |
1875
|
69 |
|
$chartReader = new Chart($chartNS, $drawingNS); |
1876
|
69 |
|
$objChart = $chartReader->readChart($chartElements, basename($chartEntryRef, '.xml')); |
1877
|
69 |
|
if (isset($charts[$chartEntryRef])) { |
1878
|
69 |
|
$chartPositionRef = $charts[$chartEntryRef]['sheet'] . '!' . $charts[$chartEntryRef]['id']; |
1879
|
69 |
|
if (isset($chartDetails[$chartPositionRef]) && $excel->getSheetByName($charts[$chartEntryRef]['sheet']) !== null) { |
1880
|
69 |
|
$excel->getSheetByName($charts[$chartEntryRef]['sheet'])->addChart($objChart); |
1881
|
69 |
|
$objChart->setWorksheet($excel->getSheetByName($charts[$chartEntryRef]['sheet'])); |
1882
|
|
|
// For oneCellAnchor or absoluteAnchor positioned charts, |
1883
|
|
|
// toCoordinate is not in the data. Does it need to be calculated? |
1884
|
69 |
|
if (array_key_exists('toCoordinate', $chartDetails[$chartPositionRef])) { |
1885
|
|
|
// twoCellAnchor |
1886
|
65 |
|
$objChart->setTopLeftPosition($chartDetails[$chartPositionRef]['fromCoordinate'], $chartDetails[$chartPositionRef]['fromOffsetX'], $chartDetails[$chartPositionRef]['fromOffsetY']); |
1887
|
65 |
|
$objChart->setBottomRightPosition($chartDetails[$chartPositionRef]['toCoordinate'], $chartDetails[$chartPositionRef]['toOffsetX'], $chartDetails[$chartPositionRef]['toOffsetY']); |
1888
|
|
|
} else { |
1889
|
|
|
// oneCellAnchor or absoluteAnchor (e.g. Chart sheet) |
1890
|
5 |
|
$objChart->setTopLeftPosition($chartDetails[$chartPositionRef]['fromCoordinate'], $chartDetails[$chartPositionRef]['fromOffsetX'], $chartDetails[$chartPositionRef]['fromOffsetY']); |
1891
|
5 |
|
$objChart->setBottomRightPosition('', $chartDetails[$chartPositionRef]['width'], $chartDetails[$chartPositionRef]['height']); |
1892
|
5 |
|
if (array_key_exists('oneCellAnchor', $chartDetails[$chartPositionRef])) { |
1893
|
4 |
|
$objChart->setOneCellAnchor($chartDetails[$chartPositionRef]['oneCellAnchor']); |
1894
|
|
|
} |
1895
|
|
|
} |
1896
|
|
|
} |
1897
|
|
|
} |
1898
|
|
|
} |
1899
|
|
|
|
1900
|
71 |
|
break; |
1901
|
|
|
|
1902
|
|
|
// unparsed |
1903
|
650 |
|
case 'application/vnd.ms-excel.controlproperties+xml': |
1904
|
4 |
|
$unparsedLoadedData['override_content_types'][(string) $contentType['PartName']] = (string) $contentType['ContentType']; |
1905
|
|
|
|
1906
|
4 |
|
break; |
1907
|
|
|
} |
1908
|
|
|
} |
1909
|
|
|
} |
1910
|
|
|
|
1911
|
653 |
|
$excel->setUnparsedLoadedData($unparsedLoadedData); |
1912
|
|
|
|
1913
|
653 |
|
$zip->close(); |
1914
|
|
|
|
1915
|
653 |
|
return $excel; |
1916
|
|
|
} |
1917
|
|
|
|
1918
|
75 |
|
private function parseRichText(?SimpleXMLElement $is): RichText |
1919
|
|
|
{ |
1920
|
75 |
|
$value = new RichText(); |
1921
|
|
|
|
1922
|
75 |
|
if (isset($is->t)) { |
1923
|
21 |
|
$value->createText(StringHelper::controlCharacterOOXML2PHP((string) $is->t)); |
1924
|
55 |
|
} elseif ($is !== null) { |
1925
|
55 |
|
if (is_object($is->r)) { |
1926
|
55 |
|
foreach ($is->r as $run) { |
1927
|
52 |
|
if (!isset($run->rPr)) { |
1928
|
36 |
|
$value->createText(StringHelper::controlCharacterOOXML2PHP((string) $run->t)); |
1929
|
|
|
} else { |
1930
|
48 |
|
$objText = $value->createTextRun(StringHelper::controlCharacterOOXML2PHP((string) $run->t)); |
1931
|
48 |
|
$objFont = $objText->getFont() ?? new StyleFont(); |
1932
|
|
|
|
1933
|
48 |
|
if (isset($run->rPr->rFont)) { |
1934
|
48 |
|
$attr = $run->rPr->rFont->attributes(); |
1935
|
48 |
|
if (isset($attr['val'])) { |
1936
|
48 |
|
$objFont->setName((string) $attr['val']); |
1937
|
|
|
} |
1938
|
|
|
} |
1939
|
48 |
|
if (isset($run->rPr->sz)) { |
1940
|
48 |
|
$attr = $run->rPr->sz->attributes(); |
1941
|
48 |
|
if (isset($attr['val'])) { |
1942
|
48 |
|
$objFont->setSize((float) $attr['val']); |
1943
|
|
|
} |
1944
|
|
|
} |
1945
|
48 |
|
if (isset($run->rPr->color)) { |
1946
|
46 |
|
$objFont->setColor(new Color($this->styleReader->readColor($run->rPr->color))); |
1947
|
|
|
} |
1948
|
48 |
|
if (isset($run->rPr->b)) { |
1949
|
41 |
|
$attr = $run->rPr->b->attributes(); |
1950
|
|
|
if ( |
1951
|
41 |
|
(isset($attr['val']) && self::boolean((string) $attr['val'])) |
1952
|
41 |
|
|| (!isset($attr['val'])) |
1953
|
|
|
) { |
1954
|
39 |
|
$objFont->setBold(true); |
1955
|
|
|
} |
1956
|
|
|
} |
1957
|
48 |
|
if (isset($run->rPr->i)) { |
1958
|
14 |
|
$attr = $run->rPr->i->attributes(); |
1959
|
|
|
if ( |
1960
|
14 |
|
(isset($attr['val']) && self::boolean((string) $attr['val'])) |
1961
|
14 |
|
|| (!isset($attr['val'])) |
1962
|
|
|
) { |
1963
|
7 |
|
$objFont->setItalic(true); |
1964
|
|
|
} |
1965
|
|
|
} |
1966
|
48 |
|
if (isset($run->rPr->vertAlign)) { |
1967
|
|
|
$attr = $run->rPr->vertAlign->attributes(); |
1968
|
|
|
if (isset($attr['val'])) { |
1969
|
|
|
$vertAlign = strtolower((string) $attr['val']); |
1970
|
|
|
if ($vertAlign == 'superscript') { |
1971
|
|
|
$objFont->setSuperscript(true); |
1972
|
|
|
} |
1973
|
|
|
if ($vertAlign == 'subscript') { |
1974
|
|
|
$objFont->setSubscript(true); |
1975
|
|
|
} |
1976
|
|
|
} |
1977
|
|
|
} |
1978
|
48 |
|
if (isset($run->rPr->u)) { |
1979
|
11 |
|
$attr = $run->rPr->u->attributes(); |
1980
|
11 |
|
if (!isset($attr['val'])) { |
1981
|
1 |
|
$objFont->setUnderline(StyleFont::UNDERLINE_SINGLE); |
1982
|
|
|
} else { |
1983
|
10 |
|
$objFont->setUnderline((string) $attr['val']); |
1984
|
|
|
} |
1985
|
|
|
} |
1986
|
48 |
|
if (isset($run->rPr->strike)) { |
1987
|
10 |
|
$attr = $run->rPr->strike->attributes(); |
1988
|
|
|
if ( |
1989
|
10 |
|
(isset($attr['val']) && self::boolean((string) $attr['val'])) |
1990
|
10 |
|
|| (!isset($attr['val'])) |
1991
|
|
|
) { |
1992
|
|
|
$objFont->setStrikethrough(true); |
1993
|
|
|
} |
1994
|
|
|
} |
1995
|
|
|
} |
1996
|
|
|
} |
1997
|
|
|
} |
1998
|
|
|
} |
1999
|
|
|
|
2000
|
75 |
|
return $value; |
2001
|
|
|
} |
2002
|
|
|
|
2003
|
2 |
|
private function readRibbon(Spreadsheet $excel, string $customUITarget, ZipArchive $zip): void |
2004
|
|
|
{ |
2005
|
2 |
|
$baseDir = dirname($customUITarget); |
2006
|
2 |
|
$nameCustomUI = basename($customUITarget); |
2007
|
|
|
// get the xml file (ribbon) |
2008
|
2 |
|
$localRibbon = $this->getFromZipArchive($zip, $customUITarget); |
2009
|
2 |
|
$customUIImagesNames = []; |
2010
|
2 |
|
$customUIImagesBinaries = []; |
2011
|
|
|
// something like customUI/_rels/customUI.xml.rels |
2012
|
2 |
|
$pathRels = $baseDir . '/_rels/' . $nameCustomUI . '.rels'; |
2013
|
2 |
|
$dataRels = $this->getFromZipArchive($zip, $pathRels); |
2014
|
2 |
|
if ($dataRels) { |
2015
|
|
|
// exists and not empty if the ribbon have some pictures (other than internal MSO) |
2016
|
|
|
$UIRels = simplexml_load_string( |
2017
|
|
|
$this->getSecurityScannerOrThrow() |
2018
|
|
|
->scan($dataRels) |
2019
|
|
|
); |
2020
|
|
|
if (false !== $UIRels) { |
2021
|
|
|
// we need to save id and target to avoid parsing customUI.xml and "guess" if it's a pseudo callback who load the image |
2022
|
|
|
foreach ($UIRels->Relationship as $ele) { |
2023
|
|
|
if ((string) $ele['Type'] === Namespaces::SCHEMA_OFFICE_DOCUMENT . '/image') { |
2024
|
|
|
// an image ? |
2025
|
|
|
$customUIImagesNames[(string) $ele['Id']] = (string) $ele['Target']; |
2026
|
|
|
$customUIImagesBinaries[(string) $ele['Target']] = $this->getFromZipArchive($zip, $baseDir . '/' . (string) $ele['Target']); |
2027
|
|
|
} |
2028
|
|
|
} |
2029
|
|
|
} |
2030
|
|
|
} |
2031
|
2 |
|
if ($localRibbon) { |
2032
|
2 |
|
$excel->setRibbonXMLData($customUITarget, $localRibbon); |
2033
|
2 |
|
if (count($customUIImagesNames) > 0 && count($customUIImagesBinaries) > 0) { |
2034
|
|
|
$excel->setRibbonBinObjects($customUIImagesNames, $customUIImagesBinaries); |
2035
|
|
|
} else { |
2036
|
2 |
|
$excel->setRibbonBinObjects(null, null); |
2037
|
|
|
} |
2038
|
|
|
} else { |
2039
|
|
|
$excel->setRibbonXMLData(null, null); |
2040
|
|
|
$excel->setRibbonBinObjects(null, null); |
2041
|
|
|
} |
2042
|
|
|
} |
2043
|
|
|
|
2044
|
678 |
|
private static function getArrayItem(null|array|bool|SimpleXMLElement $array, int|string $key = 0): mixed |
2045
|
|
|
{ |
2046
|
678 |
|
return ($array === null || is_bool($array)) ? null : ($array[$key] ?? null); |
2047
|
|
|
} |
2048
|
|
|
|
2049
|
670 |
|
private static function getArrayItemString(null|array|bool|SimpleXMLElement $array, int|string $key = 0): string |
2050
|
|
|
{ |
2051
|
670 |
|
$retVal = self::getArrayItem($array, $key); |
2052
|
|
|
|
2053
|
670 |
|
return StringHelper::convertToString($retVal, false); |
2054
|
|
|
} |
2055
|
|
|
|
2056
|
60 |
|
private static function getArrayItemIntOrSxml(null|array|bool|SimpleXMLElement $array, int|string $key = 0): int|SimpleXMLElement |
2057
|
|
|
{ |
2058
|
60 |
|
$retVal = self::getArrayItem($array, $key); |
2059
|
|
|
|
2060
|
60 |
|
return (is_int($retVal) || $retVal instanceof SimpleXMLElement) ? $retVal : 0; |
2061
|
|
|
} |
2062
|
|
|
|
2063
|
359 |
|
private static function dirAdd(null|SimpleXMLElement|string $base, null|SimpleXMLElement|string $add): string |
2064
|
|
|
{ |
2065
|
359 |
|
$base = (string) $base; |
2066
|
359 |
|
$add = (string) $add; |
2067
|
|
|
|
2068
|
359 |
|
return (string) preg_replace('~[^/]+/\.\./~', '', dirname($base) . "/$add"); |
2069
|
|
|
} |
2070
|
|
|
|
2071
|
2 |
|
private static function toCSSArray(string $style): array |
2072
|
|
|
{ |
2073
|
2 |
|
$style = self::stripWhiteSpaceFromStyleString($style); |
2074
|
|
|
|
2075
|
2 |
|
$temp = explode(';', $style); |
2076
|
2 |
|
$style = []; |
2077
|
2 |
|
foreach ($temp as $item) { |
2078
|
2 |
|
$item = explode(':', $item); |
2079
|
|
|
|
2080
|
2 |
|
if (str_contains($item[1], 'px')) { |
2081
|
1 |
|
$item[1] = str_replace('px', '', $item[1]); |
2082
|
|
|
} |
2083
|
2 |
|
if (str_contains($item[1], 'pt')) { |
2084
|
2 |
|
$item[1] = str_replace('pt', '', $item[1]); |
2085
|
2 |
|
$item[1] = (string) Font::fontSizeToPixels((int) $item[1]); |
2086
|
|
|
} |
2087
|
2 |
|
if (str_contains($item[1], 'in')) { |
2088
|
|
|
$item[1] = str_replace('in', '', $item[1]); |
2089
|
|
|
$item[1] = (string) Font::inchSizeToPixels((int) $item[1]); |
2090
|
|
|
} |
2091
|
2 |
|
if (str_contains($item[1], 'cm')) { |
2092
|
|
|
$item[1] = str_replace('cm', '', $item[1]); |
2093
|
|
|
$item[1] = (string) Font::centimeterSizeToPixels((int) $item[1]); |
2094
|
|
|
} |
2095
|
|
|
|
2096
|
2 |
|
$style[$item[0]] = $item[1]; |
2097
|
|
|
} |
2098
|
|
|
|
2099
|
2 |
|
return $style; |
2100
|
|
|
} |
2101
|
|
|
|
2102
|
5 |
|
public static function stripWhiteSpaceFromStyleString(string $string): string |
2103
|
|
|
{ |
2104
|
5 |
|
return trim(str_replace(["\r", "\n", ' '], '', $string), ';'); |
2105
|
|
|
} |
2106
|
|
|
|
2107
|
88 |
|
private static function boolean(string $value): bool |
2108
|
|
|
{ |
2109
|
88 |
|
if (is_numeric($value)) { |
2110
|
69 |
|
return (bool) $value; |
2111
|
|
|
} |
2112
|
|
|
|
2113
|
32 |
|
return $value === 'true' || $value === 'TRUE'; |
2114
|
|
|
} |
2115
|
|
|
|
2116
|
57 |
|
private function readHyperLinkDrawing(\PhpOffice\PhpSpreadsheet\Worksheet\Drawing $objDrawing, SimpleXMLElement $cellAnchor, array $hyperlinks): void |
2117
|
|
|
{ |
2118
|
57 |
|
$hlinkClick = $cellAnchor->pic->nvPicPr->cNvPr->children(Namespaces::DRAWINGML)->hlinkClick; |
2119
|
|
|
|
2120
|
57 |
|
if ($hlinkClick->count() === 0) { |
2121
|
55 |
|
return; |
2122
|
|
|
} |
2123
|
|
|
|
2124
|
2 |
|
$hlinkId = (string) self::getAttributes($hlinkClick, Namespaces::SCHEMA_OFFICE_DOCUMENT)['id']; |
2125
|
2 |
|
$hyperlink = new Hyperlink( |
2126
|
2 |
|
$hyperlinks[$hlinkId], |
2127
|
2 |
|
self::getArrayItemString(self::getAttributes($cellAnchor->pic->nvPicPr->cNvPr), 'name') |
2128
|
2 |
|
); |
2129
|
2 |
|
$objDrawing->setHyperlink($hyperlink); |
2130
|
|
|
} |
2131
|
|
|
|
2132
|
655 |
|
private function readProtection(Spreadsheet $excel, SimpleXMLElement $xmlWorkbook): void |
2133
|
|
|
{ |
2134
|
655 |
|
if (!$xmlWorkbook->workbookProtection) { |
2135
|
635 |
|
return; |
2136
|
|
|
} |
2137
|
|
|
|
2138
|
24 |
|
$excel->getSecurity()->setLockRevision(self::getLockValue($xmlWorkbook->workbookProtection, 'lockRevision')); |
2139
|
24 |
|
$excel->getSecurity()->setLockStructure(self::getLockValue($xmlWorkbook->workbookProtection, 'lockStructure')); |
2140
|
24 |
|
$excel->getSecurity()->setLockWindows(self::getLockValue($xmlWorkbook->workbookProtection, 'lockWindows')); |
2141
|
|
|
|
2142
|
24 |
|
if ($xmlWorkbook->workbookProtection['revisionsPassword']) { |
2143
|
1 |
|
$excel->getSecurity()->setRevisionsPassword( |
2144
|
1 |
|
(string) $xmlWorkbook->workbookProtection['revisionsPassword'], |
2145
|
1 |
|
true |
2146
|
1 |
|
); |
2147
|
|
|
} |
2148
|
|
|
|
2149
|
24 |
|
if ($xmlWorkbook->workbookProtection['workbookPassword']) { |
2150
|
2 |
|
$excel->getSecurity()->setWorkbookPassword( |
2151
|
2 |
|
(string) $xmlWorkbook->workbookProtection['workbookPassword'], |
2152
|
2 |
|
true |
2153
|
2 |
|
); |
2154
|
|
|
} |
2155
|
|
|
} |
2156
|
|
|
|
2157
|
24 |
|
private static function getLockValue(SimpleXMLElement $protection, string $key): ?bool |
2158
|
|
|
{ |
2159
|
24 |
|
$returnValue = null; |
2160
|
24 |
|
$protectKey = $protection[$key]; |
2161
|
24 |
|
if (!empty($protectKey)) { |
2162
|
10 |
|
$protectKey = (string) $protectKey; |
2163
|
10 |
|
$returnValue = $protectKey !== 'false' && (bool) $protectKey; |
2164
|
|
|
} |
2165
|
|
|
|
2166
|
24 |
|
return $returnValue; |
2167
|
|
|
} |
2168
|
|
|
|
2169
|
653 |
|
private function readFormControlProperties(Spreadsheet $excel, string $dir, string $fileWorksheet, Worksheet $docSheet, array &$unparsedLoadedData): void |
2170
|
|
|
{ |
2171
|
653 |
|
$zip = $this->zip; |
2172
|
653 |
|
if ($zip->locateName(dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels') === false) { |
2173
|
338 |
|
return; |
2174
|
|
|
} |
2175
|
|
|
|
2176
|
538 |
|
$filename = dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels'; |
2177
|
538 |
|
$relsWorksheet = $this->loadZipNoNamespace($filename, Namespaces::RELATIONSHIPS); |
2178
|
538 |
|
$ctrlProps = []; |
2179
|
538 |
|
foreach ($relsWorksheet->Relationship as $ele) { |
2180
|
381 |
|
if ((string) $ele['Type'] === Namespaces::SCHEMA_OFFICE_DOCUMENT . '/ctrlProp') { |
2181
|
4 |
|
$ctrlProps[(string) $ele['Id']] = $ele; |
2182
|
|
|
} |
2183
|
|
|
} |
2184
|
|
|
|
2185
|
538 |
|
$unparsedCtrlProps = &$unparsedLoadedData['sheets'][$docSheet->getCodeName()]['ctrlProps']; |
2186
|
538 |
|
foreach ($ctrlProps as $rId => $ctrlProp) { |
2187
|
4 |
|
$rId = substr($rId, 3); // rIdXXX |
2188
|
4 |
|
$unparsedCtrlProps[$rId] = []; |
2189
|
4 |
|
$unparsedCtrlProps[$rId]['filePath'] = self::dirAdd("$dir/$fileWorksheet", $ctrlProp['Target']); |
2190
|
4 |
|
$unparsedCtrlProps[$rId]['relFilePath'] = (string) $ctrlProp['Target']; |
2191
|
4 |
|
$unparsedCtrlProps[$rId]['content'] = $this->getSecurityScannerOrThrow()->scan($this->getFromZipArchive($zip, $unparsedCtrlProps[$rId]['filePath'])); |
2192
|
|
|
} |
2193
|
538 |
|
unset($unparsedCtrlProps); |
2194
|
|
|
} |
2195
|
|
|
|
2196
|
653 |
|
private function readPrinterSettings(Spreadsheet $excel, string $dir, string $fileWorksheet, Worksheet $docSheet, array &$unparsedLoadedData): void |
2197
|
|
|
{ |
2198
|
653 |
|
$zip = $this->zip; |
2199
|
653 |
|
if ($zip->locateName(dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels') === false) { |
2200
|
338 |
|
return; |
2201
|
|
|
} |
2202
|
|
|
|
2203
|
538 |
|
$filename = dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels'; |
2204
|
538 |
|
$relsWorksheet = $this->loadZipNoNamespace($filename, Namespaces::RELATIONSHIPS); |
2205
|
538 |
|
$sheetPrinterSettings = []; |
2206
|
538 |
|
foreach ($relsWorksheet->Relationship as $ele) { |
2207
|
381 |
|
if ((string) $ele['Type'] === Namespaces::SCHEMA_OFFICE_DOCUMENT . '/printerSettings') { |
2208
|
283 |
|
$sheetPrinterSettings[(string) $ele['Id']] = $ele; |
2209
|
|
|
} |
2210
|
|
|
} |
2211
|
|
|
|
2212
|
538 |
|
$unparsedPrinterSettings = &$unparsedLoadedData['sheets'][$docSheet->getCodeName()]['printerSettings']; |
2213
|
538 |
|
foreach ($sheetPrinterSettings as $rId => $printerSettings) { |
2214
|
283 |
|
$rId = substr($rId, 3); // rIdXXX |
2215
|
283 |
|
if (!str_ends_with($rId, 'ps')) { |
2216
|
283 |
|
$rId = $rId . 'ps'; // rIdXXX, add 'ps' suffix to avoid identical resource identifier collision with unparsed vmlDrawing |
2217
|
|
|
} |
2218
|
283 |
|
$unparsedPrinterSettings[$rId] = []; |
2219
|
283 |
|
$target = (string) str_replace('/xl/', '../', (string) $printerSettings['Target']); |
2220
|
283 |
|
$unparsedPrinterSettings[$rId]['filePath'] = self::dirAdd("$dir/$fileWorksheet", $target); |
2221
|
283 |
|
$unparsedPrinterSettings[$rId]['relFilePath'] = $target; |
2222
|
283 |
|
$unparsedPrinterSettings[$rId]['content'] = $this->getSecurityScannerOrThrow()->scan($this->getFromZipArchive($zip, $unparsedPrinterSettings[$rId]['filePath'])); |
2223
|
|
|
} |
2224
|
538 |
|
unset($unparsedPrinterSettings); |
2225
|
|
|
} |
2226
|
|
|
|
2227
|
666 |
|
private function getWorkbookBaseName(): array |
2228
|
|
|
{ |
2229
|
666 |
|
$workbookBasename = ''; |
2230
|
666 |
|
$xmlNamespaceBase = ''; |
2231
|
|
|
|
2232
|
|
|
// check if it is an OOXML archive |
2233
|
666 |
|
$rels = $this->loadZip(self::INITIAL_FILE); |
2234
|
666 |
|
foreach ($rels->children(Namespaces::RELATIONSHIPS)->Relationship as $rel) { |
2235
|
666 |
|
$rel = self::getAttributes($rel); |
2236
|
666 |
|
$type = (string) $rel['Type']; |
2237
|
|
|
switch ($type) { |
2238
|
658 |
|
case Namespaces::OFFICE_DOCUMENT: |
2239
|
647 |
|
case Namespaces::PURL_OFFICE_DOCUMENT: |
2240
|
666 |
|
$basename = basename((string) $rel['Target']); |
2241
|
666 |
|
$xmlNamespaceBase = dirname($type); |
2242
|
666 |
|
if (preg_match('/workbook.*\.xml/', $basename)) { |
2243
|
666 |
|
$workbookBasename = $basename; |
2244
|
|
|
} |
2245
|
|
|
|
2246
|
666 |
|
break; |
2247
|
|
|
} |
2248
|
|
|
} |
2249
|
|
|
|
2250
|
666 |
|
return [$workbookBasename, $xmlNamespaceBase]; |
2251
|
|
|
} |
2252
|
|
|
|
2253
|
644 |
|
private function readSheetProtection(Worksheet $docSheet, SimpleXMLElement $xmlSheet): void |
2254
|
|
|
{ |
2255
|
644 |
|
if ($this->readDataOnly || !$xmlSheet->sheetProtection) { |
2256
|
587 |
|
return; |
2257
|
|
|
} |
2258
|
|
|
|
2259
|
67 |
|
$algorithmName = (string) $xmlSheet->sheetProtection['algorithmName']; |
2260
|
67 |
|
$protection = $docSheet->getProtection(); |
2261
|
67 |
|
$protection->setAlgorithm($algorithmName); |
2262
|
|
|
|
2263
|
67 |
|
if ($algorithmName) { |
2264
|
2 |
|
$protection->setPassword((string) $xmlSheet->sheetProtection['hashValue'], true); |
2265
|
2 |
|
$protection->setSalt((string) $xmlSheet->sheetProtection['saltValue']); |
2266
|
2 |
|
$protection->setSpinCount((int) $xmlSheet->sheetProtection['spinCount']); |
2267
|
|
|
} else { |
2268
|
66 |
|
$protection->setPassword((string) $xmlSheet->sheetProtection['password'], true); |
2269
|
|
|
} |
2270
|
|
|
|
2271
|
67 |
|
if ($xmlSheet->protectedRanges->protectedRange) { |
2272
|
4 |
|
foreach ($xmlSheet->protectedRanges->protectedRange as $protectedRange) { |
2273
|
4 |
|
$docSheet->protectCells((string) $protectedRange['sqref'], (string) $protectedRange['password'], true, (string) $protectedRange['name'], (string) $protectedRange['securityDescriptor']); |
2274
|
|
|
} |
2275
|
|
|
} |
2276
|
|
|
} |
2277
|
|
|
|
2278
|
652 |
|
private function readAutoFilter( |
2279
|
|
|
SimpleXMLElement $xmlSheet, |
2280
|
|
|
Worksheet $docSheet |
2281
|
|
|
): void { |
2282
|
652 |
|
if ($xmlSheet && $xmlSheet->autoFilter) { |
2283
|
16 |
|
(new AutoFilter($docSheet, $xmlSheet))->load(); |
2284
|
|
|
} |
2285
|
|
|
} |
2286
|
|
|
|
2287
|
652 |
|
private function readBackgroundImage( |
2288
|
|
|
SimpleXMLElement $xmlSheet, |
2289
|
|
|
Worksheet $docSheet, |
2290
|
|
|
string $relsName |
2291
|
|
|
): void { |
2292
|
652 |
|
if ($xmlSheet && $xmlSheet->picture) { |
2293
|
1 |
|
$id = (string) self::getArrayItemString(self::getAttributes($xmlSheet->picture, Namespaces::SCHEMA_OFFICE_DOCUMENT), 'id'); |
2294
|
1 |
|
$rels = $this->loadZip($relsName); |
2295
|
1 |
|
foreach ($rels->Relationship as $rel) { |
2296
|
1 |
|
$attrs = $rel->attributes() ?? []; |
2297
|
1 |
|
$rid = (string) ($attrs['Id'] ?? ''); |
2298
|
1 |
|
$target = (string) ($attrs['Target'] ?? ''); |
2299
|
1 |
|
if ($rid === $id && substr($target, 0, 2) === '..') { |
2300
|
1 |
|
$target = 'xl' . substr($target, 2); |
2301
|
1 |
|
$content = $this->getFromZipArchive($this->zip, $target); |
2302
|
1 |
|
$docSheet->setBackgroundImage($content); |
2303
|
|
|
} |
2304
|
|
|
} |
2305
|
|
|
} |
2306
|
|
|
} |
2307
|
|
|
|
2308
|
654 |
|
private function readTables( |
2309
|
|
|
SimpleXMLElement $xmlSheet, |
2310
|
|
|
Worksheet $docSheet, |
2311
|
|
|
string $dir, |
2312
|
|
|
string $fileWorksheet, |
2313
|
|
|
ZipArchive $zip, |
2314
|
|
|
string $namespaceTable |
2315
|
|
|
): void { |
2316
|
654 |
|
if ($xmlSheet && $xmlSheet->tableParts) { |
2317
|
33 |
|
$attributes = $xmlSheet->tableParts->attributes() ?? ['count' => 0]; |
2318
|
33 |
|
if (((int) $attributes['count']) > 0) { |
2319
|
29 |
|
$this->readTablesInTablesFile($xmlSheet, $dir, $fileWorksheet, $zip, $docSheet, $namespaceTable); |
2320
|
|
|
} |
2321
|
|
|
} |
2322
|
|
|
} |
2323
|
|
|
|
2324
|
29 |
|
private function readTablesInTablesFile( |
2325
|
|
|
SimpleXMLElement $xmlSheet, |
2326
|
|
|
string $dir, |
2327
|
|
|
string $fileWorksheet, |
2328
|
|
|
ZipArchive $zip, |
2329
|
|
|
Worksheet $docSheet, |
2330
|
|
|
string $namespaceTable |
2331
|
|
|
): void { |
2332
|
29 |
|
foreach ($xmlSheet->tableParts->tablePart as $tablePart) { |
2333
|
29 |
|
$relation = self::getAttributes($tablePart, Namespaces::SCHEMA_OFFICE_DOCUMENT); |
2334
|
29 |
|
$tablePartRel = (string) $relation['id']; |
2335
|
29 |
|
$relationsFileName = dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels'; |
2336
|
|
|
|
2337
|
29 |
|
if ($zip->locateName($relationsFileName) !== false) { |
2338
|
29 |
|
$relsTableReferences = $this->loadZip($relationsFileName, Namespaces::RELATIONSHIPS); |
2339
|
29 |
|
foreach ($relsTableReferences->Relationship as $relationship) { |
2340
|
29 |
|
$relationshipAttributes = self::getAttributes($relationship, ''); |
2341
|
|
|
|
2342
|
29 |
|
if ((string) $relationshipAttributes['Id'] === $tablePartRel) { |
2343
|
29 |
|
$relationshipFileName = (string) $relationshipAttributes['Target']; |
2344
|
29 |
|
$relationshipFilePath = dirname("$dir/$fileWorksheet") . '/' . $relationshipFileName; |
2345
|
29 |
|
$relationshipFilePath = File::realpath($relationshipFilePath); |
2346
|
|
|
|
2347
|
29 |
|
if ($this->fileExistsInArchive($this->zip, $relationshipFilePath)) { |
2348
|
29 |
|
$tableXml = $this->loadZip($relationshipFilePath, $namespaceTable); |
2349
|
29 |
|
(new TableReader($docSheet, $tableXml))->load(); |
2350
|
|
|
} |
2351
|
|
|
} |
2352
|
|
|
} |
2353
|
|
|
} |
2354
|
|
|
} |
2355
|
|
|
} |
2356
|
|
|
|
2357
|
662 |
|
private static function extractStyles(?SimpleXMLElement $sxml, string $node1, string $node2): array |
2358
|
|
|
{ |
2359
|
662 |
|
$array = []; |
2360
|
662 |
|
if ($sxml && $sxml->{$node1}->{$node2}) { |
2361
|
662 |
|
foreach ($sxml->{$node1}->{$node2} as $node) { |
2362
|
662 |
|
$array[] = $node; |
2363
|
|
|
} |
2364
|
|
|
} |
2365
|
|
|
|
2366
|
662 |
|
return $array; |
2367
|
|
|
} |
2368
|
|
|
|
2369
|
662 |
|
private static function extractPalette(?SimpleXMLElement $sxml): array |
2370
|
|
|
{ |
2371
|
662 |
|
$array = []; |
2372
|
662 |
|
if ($sxml && $sxml->colors->indexedColors) { |
2373
|
15 |
|
foreach ($sxml->colors->indexedColors->rgbColor as $node) { |
2374
|
15 |
|
$attr = $node->attributes(); |
2375
|
15 |
|
if (isset($attr['rgb'])) { |
2376
|
15 |
|
$array[] = (string) $attr['rgb']; |
2377
|
|
|
} |
2378
|
|
|
} |
2379
|
|
|
} |
2380
|
|
|
|
2381
|
662 |
|
return $array; |
2382
|
|
|
} |
2383
|
|
|
|
2384
|
4 |
|
private function processIgnoredErrors(SimpleXMLElement $xml, Worksheet $sheet): void |
2385
|
|
|
{ |
2386
|
4 |
|
$cellCollection = $sheet->getCellCollection(); |
2387
|
4 |
|
$attributes = self::getAttributes($xml); |
2388
|
4 |
|
$sqref = (string) ($attributes['sqref'] ?? ''); |
2389
|
4 |
|
$numberStoredAsText = (string) ($attributes['numberStoredAsText'] ?? ''); |
2390
|
4 |
|
$formula = (string) ($attributes['formula'] ?? ''); |
2391
|
4 |
|
$formulaRange = (string) ($attributes['formulaRange'] ?? ''); |
2392
|
4 |
|
$twoDigitTextYear = (string) ($attributes['twoDigitTextYear'] ?? ''); |
2393
|
4 |
|
$evalError = (string) ($attributes['evalError'] ?? ''); |
2394
|
4 |
|
if (!empty($sqref)) { |
2395
|
4 |
|
$explodedSqref = explode(' ', $sqref); |
2396
|
4 |
|
$pattern1 = '/^([A-Z]{1,3})([0-9]{1,7})(:([A-Z]{1,3})([0-9]{1,7}))?$/'; |
2397
|
4 |
|
foreach ($explodedSqref as $sqref1) { |
2398
|
4 |
|
if (preg_match($pattern1, $sqref1, $matches) === 1) { |
2399
|
4 |
|
$firstRow = $matches[2]; |
2400
|
4 |
|
$firstCol = $matches[1]; |
2401
|
4 |
|
if (array_key_exists(3, $matches)) { |
2402
|
|
|
// https://github.com/phpstan/phpstan/issues/11602 |
2403
|
3 |
|
$lastCol = $matches[4]; // @phpstan-ignore-line |
2404
|
3 |
|
$lastRow = $matches[5]; // @phpstan-ignore-line |
2405
|
|
|
} else { |
2406
|
3 |
|
$lastCol = $firstCol; |
2407
|
3 |
|
$lastRow = $firstRow; |
2408
|
|
|
} |
2409
|
4 |
|
++$lastCol; |
2410
|
4 |
|
for ($row = $firstRow; $row <= $lastRow; ++$row) { |
2411
|
4 |
|
for ($col = $firstCol; $col !== $lastCol; ++$col) { |
2412
|
4 |
|
if (!$cellCollection->has2("$col$row")) { |
2413
|
1 |
|
continue; |
2414
|
|
|
} |
2415
|
4 |
|
if ($numberStoredAsText === '1') { |
2416
|
4 |
|
$sheet->getCell("$col$row")->getIgnoredErrors()->setNumberStoredAsText(true); |
2417
|
|
|
} |
2418
|
4 |
|
if ($formula === '1') { |
2419
|
1 |
|
$sheet->getCell("$col$row")->getIgnoredErrors()->setFormula(true); |
2420
|
|
|
} |
2421
|
4 |
|
if ($formulaRange === '1') { |
2422
|
1 |
|
$sheet->getCell("$col$row")->getIgnoredErrors()->setFormulaRange(true); |
2423
|
|
|
} |
2424
|
4 |
|
if ($twoDigitTextYear === '1') { |
2425
|
1 |
|
$sheet->getCell("$col$row")->getIgnoredErrors()->setTwoDigitTextYear(true); |
2426
|
|
|
} |
2427
|
4 |
|
if ($evalError === '1') { |
2428
|
1 |
|
$sheet->getCell("$col$row")->getIgnoredErrors()->setEvalError(true); |
2429
|
|
|
} |
2430
|
|
|
} |
2431
|
|
|
} |
2432
|
|
|
} |
2433
|
|
|
} |
2434
|
|
|
} |
2435
|
|
|
} |
2436
|
|
|
|
2437
|
357 |
|
private static function storeFormulaAttributes(SimpleXMLElement $f, Worksheet $docSheet, string $r): void |
2438
|
|
|
{ |
2439
|
357 |
|
$formulaAttributes = []; |
2440
|
357 |
|
$attributes = $f->attributes(); |
2441
|
357 |
|
if (isset($attributes['t'])) { |
2442
|
247 |
|
$formulaAttributes['t'] = (string) $attributes['t']; |
2443
|
|
|
} |
2444
|
357 |
|
if (isset($attributes['ref'])) { |
2445
|
247 |
|
$formulaAttributes['ref'] = (string) $attributes['ref']; |
2446
|
|
|
} |
2447
|
357 |
|
if (!empty($formulaAttributes)) { |
2448
|
247 |
|
$docSheet->getCell($r)->setFormulaAttributes($formulaAttributes); |
2449
|
|
|
} |
2450
|
|
|
} |
2451
|
|
|
|
2452
|
13 |
|
private static function onlyNoteVml(string $data): bool |
2453
|
|
|
{ |
2454
|
13 |
|
$data = str_replace('<br>', '<br/>', $data); |
2455
|
|
|
|
2456
|
|
|
try { |
2457
|
13 |
|
$sxml = @simplexml_load_string($data); |
2458
|
|
|
} catch (Throwable) { |
2459
|
|
|
$sxml = false; |
2460
|
|
|
} |
2461
|
|
|
|
2462
|
13 |
|
if ($sxml === false) { |
2463
|
1 |
|
return false; |
2464
|
|
|
} |
2465
|
12 |
|
$shapes = $sxml->children(Namespaces::URN_VML); |
2466
|
12 |
|
foreach ($shapes->shape as $shape) { |
2467
|
12 |
|
$clientData = $shape->children(Namespaces::URN_EXCEL); |
2468
|
12 |
|
if (!isset($clientData->ClientData)) { |
2469
|
|
|
return false; |
2470
|
|
|
} |
2471
|
12 |
|
$attrs = $clientData->ClientData->attributes(); |
2472
|
12 |
|
if (!isset($attrs['ObjectType'])) { |
2473
|
|
|
return false; |
2474
|
|
|
} |
2475
|
12 |
|
$objectType = (string) $attrs['ObjectType']; |
2476
|
12 |
|
if ($objectType !== 'Note') { |
2477
|
4 |
|
return false; |
2478
|
|
|
} |
2479
|
|
|
} |
2480
|
|
|
|
2481
|
9 |
|
return true; |
2482
|
|
|
} |
2483
|
|
|
} |
2484
|
|
|
|