Total Complexity | 584 |
Total Lines | 2565 |
Duplicated Lines | 0 % |
Coverage | 78.52% |
Changes | 0 |
Complex classes like Xlsx often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Xlsx, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class Xlsx extends BaseReader |
||
35 | { |
||
36 | /** |
||
37 | * ReferenceHelper instance. |
||
38 | * |
||
39 | * @var ReferenceHelper |
||
40 | */ |
||
41 | private $referenceHelper; |
||
42 | |||
43 | /** |
||
44 | * Xlsx\Theme instance. |
||
45 | * |
||
46 | * @var Xlsx\Theme |
||
47 | */ |
||
48 | private static $theme = null; |
||
49 | |||
50 | /** |
||
51 | * Create a new Xlsx Reader instance. |
||
52 | */ |
||
53 | 44 | public function __construct() |
|
54 | { |
||
55 | 44 | $this->readFilter = new DefaultReadFilter(); |
|
56 | 44 | $this->referenceHelper = ReferenceHelper::getInstance(); |
|
57 | 44 | $this->securityScanner = XmlScanner::getInstance($this); |
|
58 | 44 | } |
|
59 | |||
60 | /** |
||
61 | * Can the current IReader read the file? |
||
62 | * |
||
63 | * @param string $pFilename |
||
64 | * |
||
65 | * @throws Exception |
||
66 | * |
||
67 | * @return bool |
||
68 | */ |
||
69 | 6 | public function canRead($pFilename) |
|
70 | { |
||
71 | 6 | File::assertFile($pFilename); |
|
72 | |||
73 | 6 | $xl = false; |
|
74 | // Load file |
||
75 | 6 | $zip = new ZipArchive(); |
|
76 | 6 | if ($zip->open($pFilename) === true) { |
|
77 | // check if it is an OOXML archive |
||
78 | 6 | $rels = simplexml_load_string( |
|
79 | 6 | $this->securityScanner->scan( |
|
80 | 6 | $this->getFromZipArchive($zip, '_rels/.rels') |
|
81 | ), |
||
82 | 6 | 'SimpleXMLElement', |
|
83 | 6 | Settings::getLibXmlLoaderOptions() |
|
84 | ); |
||
85 | 6 | if ($rels !== false) { |
|
86 | 6 | foreach ($rels->Relationship as $rel) { |
|
87 | 6 | switch ($rel['Type']) { |
|
88 | 6 | case 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument': |
|
89 | 6 | if (basename($rel['Target']) == 'workbook.xml') { |
|
90 | 6 | $xl = true; |
|
91 | } |
||
92 | |||
93 | 6 | break; |
|
94 | } |
||
95 | } |
||
96 | } |
||
97 | 6 | $zip->close(); |
|
98 | } |
||
99 | |||
100 | 6 | return $xl; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Reads names of the worksheets from a file, without parsing the whole file to a Spreadsheet object. |
||
105 | * |
||
106 | * @param string $pFilename |
||
107 | * |
||
108 | * @throws Exception |
||
109 | * |
||
110 | * @return array |
||
111 | */ |
||
112 | 1 | public function listWorksheetNames($pFilename) |
|
113 | { |
||
114 | 1 | File::assertFile($pFilename); |
|
115 | |||
116 | 1 | $worksheetNames = []; |
|
117 | |||
118 | 1 | $zip = new ZipArchive(); |
|
119 | 1 | $zip->open($pFilename); |
|
120 | |||
121 | // The files we're looking at here are small enough that simpleXML is more efficient than XMLReader |
||
122 | //~ http://schemas.openxmlformats.org/package/2006/relationships"); |
||
123 | 1 | $rels = simplexml_load_string( |
|
124 | 1 | $this->securityScanner->scan($this->getFromZipArchive($zip, '_rels/.rels')) |
|
125 | ); |
||
126 | 1 | foreach ($rels->Relationship as $rel) { |
|
127 | 1 | switch ($rel['Type']) { |
|
128 | 1 | case 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument': |
|
129 | //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main" |
||
130 | 1 | $xmlWorkbook = simplexml_load_string( |
|
131 | 1 | $this->securityScanner->scan($this->getFromZipArchive($zip, "{$rel['Target']}")) |
|
132 | ); |
||
133 | |||
134 | 1 | if ($xmlWorkbook->sheets) { |
|
135 | 1 | foreach ($xmlWorkbook->sheets->sheet as $eleSheet) { |
|
136 | // Check if sheet should be skipped |
||
137 | 1 | $worksheetNames[] = (string) $eleSheet['name']; |
|
138 | } |
||
139 | } |
||
140 | } |
||
141 | } |
||
142 | |||
143 | 1 | $zip->close(); |
|
144 | |||
145 | 1 | return $worksheetNames; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns). |
||
150 | * |
||
151 | * @param string $pFilename |
||
152 | * |
||
153 | * @throws Exception |
||
154 | * |
||
155 | * @return array |
||
156 | */ |
||
157 | 1 | public function listWorksheetInfo($pFilename) |
|
158 | { |
||
159 | 1 | File::assertFile($pFilename); |
|
160 | |||
161 | 1 | $worksheetInfo = []; |
|
162 | |||
163 | 1 | $zip = new ZipArchive(); |
|
164 | 1 | $zip->open($pFilename); |
|
165 | |||
166 | //~ http://schemas.openxmlformats.org/package/2006/relationships" |
||
167 | 1 | $rels = simplexml_load_string( |
|
168 | 1 | $this->securityScanner->scan($this->getFromZipArchive($zip, '_rels/.rels')), |
|
169 | 1 | 'SimpleXMLElement', |
|
170 | 1 | Settings::getLibXmlLoaderOptions() |
|
171 | ); |
||
172 | 1 | foreach ($rels->Relationship as $rel) { |
|
173 | 1 | if ($rel['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument') { |
|
174 | 1 | $dir = dirname($rel['Target']); |
|
175 | |||
176 | //~ http://schemas.openxmlformats.org/package/2006/relationships" |
||
177 | 1 | $relsWorkbook = simplexml_load_string( |
|
178 | 1 | $this->securityScanner->scan( |
|
179 | 1 | $this->getFromZipArchive($zip, "$dir/_rels/" . basename($rel['Target']) . '.rels') |
|
180 | ), |
||
181 | 1 | 'SimpleXMLElement', |
|
182 | 1 | Settings::getLibXmlLoaderOptions() |
|
183 | ); |
||
184 | 1 | $relsWorkbook->registerXPathNamespace('rel', 'http://schemas.openxmlformats.org/package/2006/relationships'); |
|
185 | |||
186 | 1 | $worksheets = []; |
|
187 | 1 | foreach ($relsWorkbook->Relationship as $ele) { |
|
188 | 1 | if ($ele['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet') { |
|
189 | 1 | $worksheets[(string) $ele['Id']] = $ele['Target']; |
|
190 | } |
||
191 | } |
||
192 | |||
193 | //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main" |
||
194 | 1 | $xmlWorkbook = simplexml_load_string( |
|
195 | 1 | $this->securityScanner->scan( |
|
196 | 1 | $this->getFromZipArchive($zip, "{$rel['Target']}") |
|
197 | ), |
||
198 | 1 | 'SimpleXMLElement', |
|
199 | 1 | Settings::getLibXmlLoaderOptions() |
|
200 | ); |
||
201 | 1 | if ($xmlWorkbook->sheets) { |
|
202 | 1 | $dir = dirname($rel['Target']); |
|
203 | /** @var SimpleXMLElement $eleSheet */ |
||
204 | 1 | foreach ($xmlWorkbook->sheets->sheet as $eleSheet) { |
|
205 | $tmpInfo = [ |
||
206 | 1 | 'worksheetName' => (string) $eleSheet['name'], |
|
207 | 1 | 'lastColumnLetter' => 'A', |
|
208 | 1 | 'lastColumnIndex' => 0, |
|
209 | 1 | 'totalRows' => 0, |
|
210 | 1 | 'totalColumns' => 0, |
|
211 | ]; |
||
212 | |||
213 | 1 | $fileWorksheet = $worksheets[(string) self::getArrayItem($eleSheet->attributes('http://schemas.openxmlformats.org/officeDocument/2006/relationships'), 'id')]; |
|
214 | |||
215 | 1 | $xml = new XMLReader(); |
|
216 | 1 | $xml->xml( |
|
217 | 1 | $this->securityScanner->scanFile( |
|
218 | 1 | 'zip://' . File::realpath($pFilename) . '#' . "$dir/$fileWorksheet" |
|
219 | ), |
||
220 | 1 | null, |
|
221 | 1 | Settings::getLibXmlLoaderOptions() |
|
222 | ); |
||
223 | 1 | $xml->setParserProperty(2, true); |
|
224 | |||
225 | 1 | $currCells = 0; |
|
226 | 1 | while ($xml->read()) { |
|
227 | 1 | if ($xml->name == 'row' && $xml->nodeType == XMLReader::ELEMENT) { |
|
228 | 1 | $row = $xml->getAttribute('r'); |
|
229 | 1 | $tmpInfo['totalRows'] = $row; |
|
230 | 1 | $tmpInfo['totalColumns'] = max($tmpInfo['totalColumns'], $currCells); |
|
231 | 1 | $currCells = 0; |
|
232 | 1 | } elseif ($xml->name == 'c' && $xml->nodeType == XMLReader::ELEMENT) { |
|
233 | 1 | ++$currCells; |
|
234 | } |
||
235 | } |
||
236 | 1 | $tmpInfo['totalColumns'] = max($tmpInfo['totalColumns'], $currCells); |
|
237 | 1 | $xml->close(); |
|
238 | |||
239 | 1 | $tmpInfo['lastColumnIndex'] = $tmpInfo['totalColumns'] - 1; |
|
240 | 1 | $tmpInfo['lastColumnLetter'] = Coordinate::stringFromColumnIndex($tmpInfo['lastColumnIndex'] + 1); |
|
241 | |||
242 | 1 | $worksheetInfo[] = $tmpInfo; |
|
243 | } |
||
244 | } |
||
245 | } |
||
246 | } |
||
247 | |||
248 | 1 | $zip->close(); |
|
249 | |||
250 | 1 | return $worksheetInfo; |
|
251 | } |
||
252 | |||
253 | 4 | private static function castToBoolean($c) |
|
254 | { |
||
255 | 4 | $value = isset($c->v) ? (string) $c->v : null; |
|
256 | 4 | if ($value == '0') { |
|
257 | return false; |
||
258 | 4 | } elseif ($value == '1') { |
|
259 | 4 | return true; |
|
260 | } |
||
261 | |||
262 | return (bool) $c->v; |
||
263 | } |
||
264 | |||
265 | private static function castToError($c) |
||
266 | { |
||
267 | return isset($c->v) ? (string) $c->v : null; |
||
268 | } |
||
269 | |||
270 | 19 | private static function castToString($c) |
|
271 | { |
||
272 | 19 | return isset($c->v) ? (string) $c->v : null; |
|
273 | } |
||
274 | |||
275 | 7 | private function castToFormula($c, $r, &$cellDataType, &$value, &$calculatedValue, &$sharedFormulas, $castBaseType) |
|
276 | { |
||
277 | 7 | $cellDataType = 'f'; |
|
278 | 7 | $value = "={$c->f}"; |
|
279 | 7 | $calculatedValue = self::$castBaseType($c); |
|
280 | |||
281 | // Shared formula? |
||
282 | 7 | if (isset($c->f['t']) && strtolower((string) $c->f['t']) == 'shared') { |
|
283 | 2 | $instance = (string) $c->f['si']; |
|
284 | |||
285 | 2 | if (!isset($sharedFormulas[(string) $c->f['si']])) { |
|
286 | 2 | $sharedFormulas[$instance] = ['master' => $r, 'formula' => $value]; |
|
287 | } else { |
||
288 | 2 | $master = Coordinate::coordinateFromString($sharedFormulas[$instance]['master']); |
|
289 | 2 | $current = Coordinate::coordinateFromString($r); |
|
290 | |||
291 | 2 | $difference = [0, 0]; |
|
292 | 2 | $difference[0] = Coordinate::columnIndexFromString($current[0]) - Coordinate::columnIndexFromString($master[0]); |
|
293 | 2 | $difference[1] = $current[1] - $master[1]; |
|
294 | |||
295 | 2 | $value = $this->referenceHelper->updateFormulaReferences($sharedFormulas[$instance]['formula'], 'A1', $difference[0], $difference[1]); |
|
|
|||
296 | } |
||
297 | } |
||
298 | 7 | } |
|
299 | |||
300 | /** |
||
301 | * @param ZipArchive $archive |
||
302 | * @param string $fileName |
||
303 | * |
||
304 | * @return string |
||
305 | */ |
||
306 | 41 | private function getFromZipArchive(ZipArchive $archive, $fileName = '') |
|
307 | { |
||
308 | // Root-relative paths |
||
309 | 41 | if (strpos($fileName, '//') !== false) { |
|
310 | $fileName = substr($fileName, strpos($fileName, '//') + 1); |
||
311 | } |
||
312 | 41 | $fileName = File::realpath($fileName); |
|
313 | |||
314 | // Sadly, some 3rd party xlsx generators don't use consistent case for filenaming |
||
315 | // so we need to load case-insensitively from the zip file |
||
316 | |||
317 | // Apache POI fixes |
||
318 | 41 | $contents = $archive->getFromName($fileName, 0, ZipArchive::FL_NOCASE); |
|
319 | 41 | if ($contents === false) { |
|
320 | 1 | $contents = $archive->getFromName(substr($fileName, 1), 0, ZipArchive::FL_NOCASE); |
|
321 | } |
||
322 | |||
323 | 41 | return $contents; |
|
324 | } |
||
325 | |||
326 | /** |
||
327 | * Set Worksheet column attributes by attributes array passed. |
||
328 | * |
||
329 | * @param Worksheet $docSheet |
||
330 | * @param string $column A, B, ... DX, ... |
||
331 | * @param array $columnAttributes array of attributes (indexes are attribute name, values are value) |
||
332 | * 'xfIndex', 'visible', 'collapsed', 'outlineLevel', 'width', ... ? |
||
333 | */ |
||
334 | 9 | private function setColumnAttributes(Worksheet $docSheet, $column, array $columnAttributes) |
|
335 | { |
||
336 | 9 | if (isset($columnAttributes['xfIndex'])) { |
|
337 | 4 | $docSheet->getColumnDimension($column)->setXfIndex($columnAttributes['xfIndex']); |
|
338 | } |
||
339 | 9 | if (isset($columnAttributes['visible'])) { |
|
340 | 1 | $docSheet->getColumnDimension($column)->setVisible($columnAttributes['visible']); |
|
341 | } |
||
342 | 9 | if (isset($columnAttributes['collapsed'])) { |
|
343 | 1 | $docSheet->getColumnDimension($column)->setCollapsed($columnAttributes['collapsed']); |
|
344 | } |
||
345 | 9 | if (isset($columnAttributes['outlineLevel'])) { |
|
346 | 1 | $docSheet->getColumnDimension($column)->setOutlineLevel($columnAttributes['outlineLevel']); |
|
347 | } |
||
348 | 9 | if (isset($columnAttributes['width'])) { |
|
349 | 9 | $docSheet->getColumnDimension($column)->setWidth($columnAttributes['width']); |
|
350 | } |
||
351 | 9 | } |
|
352 | |||
353 | /** |
||
354 | * Set Worksheet row attributes by attributes array passed. |
||
355 | * |
||
356 | * @param Worksheet $docSheet |
||
357 | * @param int $row 1, 2, 3, ... 99, ... |
||
358 | * @param array $rowAttributes array of attributes (indexes are attribute name, values are value) |
||
359 | * 'xfIndex', 'visible', 'collapsed', 'outlineLevel', 'rowHeight', ... ? |
||
360 | */ |
||
361 | 3 | private function setRowAttributes(Worksheet $docSheet, $row, array $rowAttributes) |
|
362 | { |
||
363 | 3 | if (isset($rowAttributes['xfIndex'])) { |
|
364 | $docSheet->getRowDimension($row)->setXfIndex($rowAttributes['xfIndex']); |
||
365 | } |
||
366 | 3 | if (isset($rowAttributes['visible'])) { |
|
367 | $docSheet->getRowDimension($row)->setVisible($rowAttributes['visible']); |
||
368 | } |
||
369 | 3 | if (isset($rowAttributes['collapsed'])) { |
|
370 | $docSheet->getRowDimension($row)->setCollapsed($rowAttributes['collapsed']); |
||
371 | } |
||
372 | 3 | if (isset($rowAttributes['outlineLevel'])) { |
|
373 | $docSheet->getRowDimension($row)->setOutlineLevel($rowAttributes['outlineLevel']); |
||
374 | } |
||
375 | 3 | if (isset($rowAttributes['rowHeight'])) { |
|
376 | 3 | $docSheet->getRowDimension($row)->setRowHeight($rowAttributes['rowHeight']); |
|
377 | } |
||
378 | 3 | } |
|
379 | |||
380 | /** |
||
381 | * Loads Spreadsheet from file. |
||
382 | * |
||
383 | * @param string $pFilename |
||
384 | * |
||
385 | * @throws Exception |
||
386 | * |
||
387 | * @return Spreadsheet |
||
388 | */ |
||
389 | 38 | public function load($pFilename) |
|
390 | { |
||
391 | 38 | File::assertFile($pFilename); |
|
392 | |||
393 | // Initialisations |
||
394 | 38 | $excel = new Spreadsheet(); |
|
395 | 38 | $excel->removeSheetByIndex(0); |
|
396 | 38 | if (!$this->readDataOnly) { |
|
397 | 38 | $excel->removeCellStyleXfByIndex(0); // remove the default style |
|
398 | 38 | $excel->removeCellXfByIndex(0); // remove the default style |
|
399 | } |
||
400 | 38 | $unparsedLoadedData = []; |
|
401 | |||
402 | 38 | $zip = new ZipArchive(); |
|
403 | 38 | $zip->open($pFilename); |
|
404 | |||
405 | // Read the theme first, because we need the colour scheme when reading the styles |
||
406 | //~ http://schemas.openxmlformats.org/package/2006/relationships" |
||
407 | 38 | $wbRels = simplexml_load_string( |
|
408 | 38 | $this->securityScanner->scan($this->getFromZipArchive($zip, 'xl/_rels/workbook.xml.rels')), |
|
409 | 38 | 'SimpleXMLElement', |
|
410 | 38 | Settings::getLibXmlLoaderOptions() |
|
411 | ); |
||
412 | 38 | foreach ($wbRels->Relationship as $rel) { |
|
413 | 38 | switch ($rel['Type']) { |
|
414 | 38 | case 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme': |
|
415 | 38 | $themeOrderArray = ['lt1', 'dk1', 'lt2', 'dk2']; |
|
416 | 38 | $themeOrderAdditional = count($themeOrderArray); |
|
417 | |||
418 | 38 | $xmlTheme = simplexml_load_string( |
|
419 | 38 | $this->securityScanner->scan($this->getFromZipArchive($zip, "xl/{$rel['Target']}")), |
|
420 | 38 | 'SimpleXMLElement', |
|
421 | 38 | Settings::getLibXmlLoaderOptions() |
|
422 | ); |
||
423 | 38 | if (is_object($xmlTheme)) { |
|
424 | 38 | $xmlThemeName = $xmlTheme->attributes(); |
|
425 | 38 | $xmlTheme = $xmlTheme->children('http://schemas.openxmlformats.org/drawingml/2006/main'); |
|
426 | 38 | $themeName = (string) $xmlThemeName['name']; |
|
427 | |||
428 | 38 | $colourScheme = $xmlTheme->themeElements->clrScheme->attributes(); |
|
429 | 38 | $colourSchemeName = (string) $colourScheme['name']; |
|
430 | 38 | $colourScheme = $xmlTheme->themeElements->clrScheme->children('http://schemas.openxmlformats.org/drawingml/2006/main'); |
|
431 | |||
432 | 38 | $themeColours = []; |
|
433 | 38 | foreach ($colourScheme as $k => $xmlColour) { |
|
434 | 38 | $themePos = array_search($k, $themeOrderArray); |
|
435 | 38 | if ($themePos === false) { |
|
436 | 38 | $themePos = $themeOrderAdditional++; |
|
437 | } |
||
438 | 38 | if (isset($xmlColour->sysClr)) { |
|
439 | 38 | $xmlColourData = $xmlColour->sysClr->attributes(); |
|
440 | 38 | $themeColours[$themePos] = $xmlColourData['lastClr']; |
|
441 | 38 | } elseif (isset($xmlColour->srgbClr)) { |
|
442 | 38 | $xmlColourData = $xmlColour->srgbClr->attributes(); |
|
443 | 38 | $themeColours[$themePos] = $xmlColourData['val']; |
|
444 | } |
||
445 | } |
||
446 | 38 | self::$theme = new Xlsx\Theme($themeName, $colourSchemeName, $themeColours); |
|
447 | } |
||
448 | |||
449 | 38 | break; |
|
450 | } |
||
451 | } |
||
452 | |||
453 | //~ http://schemas.openxmlformats.org/package/2006/relationships" |
||
454 | 38 | $rels = simplexml_load_string( |
|
455 | 38 | $this->securityScanner->scan($this->getFromZipArchive($zip, '_rels/.rels')), |
|
456 | 38 | 'SimpleXMLElement', |
|
457 | 38 | Settings::getLibXmlLoaderOptions() |
|
458 | ); |
||
459 | 38 | foreach ($rels->Relationship as $rel) { |
|
460 | 38 | switch ($rel['Type']) { |
|
461 | 38 | case 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties': |
|
462 | 37 | $xmlCore = simplexml_load_string( |
|
463 | 37 | $this->securityScanner->scan($this->getFromZipArchive($zip, "{$rel['Target']}")), |
|
464 | 37 | 'SimpleXMLElement', |
|
465 | 37 | Settings::getLibXmlLoaderOptions() |
|
466 | ); |
||
467 | 37 | if (is_object($xmlCore)) { |
|
468 | 37 | $xmlCore->registerXPathNamespace('dc', 'http://purl.org/dc/elements/1.1/'); |
|
469 | 37 | $xmlCore->registerXPathNamespace('dcterms', 'http://purl.org/dc/terms/'); |
|
470 | 37 | $xmlCore->registerXPathNamespace('cp', 'http://schemas.openxmlformats.org/package/2006/metadata/core-properties'); |
|
471 | 37 | $docProps = $excel->getProperties(); |
|
472 | 37 | $docProps->setCreator((string) self::getArrayItem($xmlCore->xpath('dc:creator'))); |
|
473 | 37 | $docProps->setLastModifiedBy((string) self::getArrayItem($xmlCore->xpath('cp:lastModifiedBy'))); |
|
474 | 37 | $docProps->setCreated(strtotime(self::getArrayItem($xmlCore->xpath('dcterms:created')))); //! respect xsi:type |
|
475 | 37 | $docProps->setModified(strtotime(self::getArrayItem($xmlCore->xpath('dcterms:modified')))); //! respect xsi:type |
|
476 | 37 | $docProps->setTitle((string) self::getArrayItem($xmlCore->xpath('dc:title'))); |
|
477 | 37 | $docProps->setDescription((string) self::getArrayItem($xmlCore->xpath('dc:description'))); |
|
478 | 37 | $docProps->setSubject((string) self::getArrayItem($xmlCore->xpath('dc:subject'))); |
|
479 | 37 | $docProps->setKeywords((string) self::getArrayItem($xmlCore->xpath('cp:keywords'))); |
|
480 | 37 | $docProps->setCategory((string) self::getArrayItem($xmlCore->xpath('cp:category'))); |
|
481 | } |
||
482 | |||
483 | 37 | break; |
|
484 | 38 | case 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties': |
|
485 | 37 | $xmlCore = simplexml_load_string( |
|
486 | 37 | $this->securityScanner->scan($this->getFromZipArchive($zip, "{$rel['Target']}")), |
|
487 | 37 | 'SimpleXMLElement', |
|
488 | 37 | Settings::getLibXmlLoaderOptions() |
|
489 | ); |
||
490 | 37 | if (is_object($xmlCore)) { |
|
491 | 37 | $docProps = $excel->getProperties(); |
|
492 | 37 | if (isset($xmlCore->Company)) { |
|
493 | 35 | $docProps->setCompany((string) $xmlCore->Company); |
|
494 | } |
||
495 | 37 | if (isset($xmlCore->Manager)) { |
|
496 | 32 | $docProps->setManager((string) $xmlCore->Manager); |
|
497 | } |
||
498 | } |
||
499 | |||
500 | 37 | break; |
|
501 | 38 | case 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties': |
|
502 | 3 | $xmlCore = simplexml_load_string( |
|
503 | 3 | $this->securityScanner->scan($this->getFromZipArchive($zip, "{$rel['Target']}")), |
|
504 | 3 | 'SimpleXMLElement', |
|
505 | 3 | Settings::getLibXmlLoaderOptions() |
|
506 | ); |
||
507 | 3 | if (is_object($xmlCore)) { |
|
508 | 3 | $docProps = $excel->getProperties(); |
|
509 | /** @var SimpleXMLElement $xmlProperty */ |
||
510 | 3 | foreach ($xmlCore as $xmlProperty) { |
|
511 | 3 | $cellDataOfficeAttributes = $xmlProperty->attributes(); |
|
512 | 3 | if (isset($cellDataOfficeAttributes['name'])) { |
|
513 | 3 | $propertyName = (string) $cellDataOfficeAttributes['name']; |
|
514 | 3 | $cellDataOfficeChildren = $xmlProperty->children('http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes'); |
|
515 | 3 | $attributeType = $cellDataOfficeChildren->getName(); |
|
516 | 3 | $attributeValue = (string) $cellDataOfficeChildren->{$attributeType}; |
|
517 | 3 | $attributeValue = Properties::convertProperty($attributeValue, $attributeType); |
|
518 | 3 | $attributeType = Properties::convertPropertyType($attributeType); |
|
519 | 3 | $docProps->setCustomProperty($propertyName, $attributeValue, $attributeType); |
|
520 | } |
||
521 | } |
||
522 | } |
||
523 | |||
524 | 3 | break; |
|
525 | //Ribbon |
||
526 | 38 | case 'http://schemas.microsoft.com/office/2006/relationships/ui/extensibility': |
|
527 | $customUI = $rel['Target']; |
||
528 | if ($customUI !== null) { |
||
529 | $this->readRibbon($excel, $customUI, $zip); |
||
530 | } |
||
531 | |||
532 | break; |
||
533 | 38 | case 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument': |
|
534 | 38 | $dir = dirname($rel['Target']); |
|
535 | //~ http://schemas.openxmlformats.org/package/2006/relationships" |
||
536 | 38 | $relsWorkbook = simplexml_load_string( |
|
537 | 38 | $this->securityScanner->scan($this->getFromZipArchive($zip, "$dir/_rels/" . basename($rel['Target']) . '.rels')), |
|
538 | 38 | 'SimpleXMLElement', |
|
539 | 38 | Settings::getLibXmlLoaderOptions() |
|
540 | ); |
||
541 | 38 | $relsWorkbook->registerXPathNamespace('rel', 'http://schemas.openxmlformats.org/package/2006/relationships'); |
|
542 | |||
543 | 38 | $sharedStrings = []; |
|
544 | 38 | $xpath = self::getArrayItem($relsWorkbook->xpath("rel:Relationship[@Type='http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings']")); |
|
545 | //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main" |
||
546 | 38 | $xmlStrings = simplexml_load_string( |
|
547 | 38 | $this->securityScanner->scan($this->getFromZipArchive($zip, "$dir/$xpath[Target]")), |
|
548 | 38 | 'SimpleXMLElement', |
|
549 | 38 | Settings::getLibXmlLoaderOptions() |
|
550 | ); |
||
551 | 38 | if (isset($xmlStrings, $xmlStrings->si)) { |
|
552 | 19 | foreach ($xmlStrings->si as $val) { |
|
553 | 19 | if (isset($val->t)) { |
|
554 | 19 | $sharedStrings[] = StringHelper::controlCharacterOOXML2PHP((string) $val->t); |
|
555 | 3 | } elseif (isset($val->r)) { |
|
556 | 19 | $sharedStrings[] = $this->parseRichText($val); |
|
557 | } |
||
558 | } |
||
559 | } |
||
560 | |||
561 | 38 | $worksheets = []; |
|
562 | 38 | $macros = $customUI = null; |
|
563 | 38 | foreach ($relsWorkbook->Relationship as $ele) { |
|
564 | 38 | switch ($ele['Type']) { |
|
565 | 38 | case 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet': |
|
566 | 38 | $worksheets[(string) $ele['Id']] = $ele['Target']; |
|
567 | |||
568 | 38 | break; |
|
569 | // a vbaProject ? (: some macros) |
||
570 | 38 | case 'http://schemas.microsoft.com/office/2006/relationships/vbaProject': |
|
571 | 1 | $macros = $ele['Target']; |
|
572 | |||
573 | 38 | break; |
|
574 | } |
||
575 | } |
||
576 | |||
577 | 38 | if ($macros !== null) { |
|
578 | 1 | $macrosCode = $this->getFromZipArchive($zip, 'xl/vbaProject.bin'); //vbaProject.bin always in 'xl' dir and always named vbaProject.bin |
|
579 | 1 | if ($macrosCode !== false) { |
|
580 | 1 | $excel->setMacrosCode($macrosCode); |
|
581 | 1 | $excel->setHasMacros(true); |
|
582 | //short-circuit : not reading vbaProject.bin.rel to get Signature =>allways vbaProjectSignature.bin in 'xl' dir |
||
583 | 1 | $Certificate = $this->getFromZipArchive($zip, 'xl/vbaProjectSignature.bin'); |
|
584 | 1 | if ($Certificate !== false) { |
|
585 | $excel->setMacrosCertificate($Certificate); |
||
586 | } |
||
587 | } |
||
588 | } |
||
589 | 38 | $styles = []; |
|
590 | 38 | $cellStyles = []; |
|
591 | 38 | $xpath = self::getArrayItem($relsWorkbook->xpath("rel:Relationship[@Type='http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles']")); |
|
592 | //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main" |
||
593 | 38 | $xmlStyles = simplexml_load_string( |
|
594 | 38 | $this->securityScanner->scan($this->getFromZipArchive($zip, "$dir/$xpath[Target]")), |
|
595 | 38 | 'SimpleXMLElement', |
|
596 | 38 | Settings::getLibXmlLoaderOptions() |
|
597 | ); |
||
598 | 38 | $numFmts = null; |
|
599 | 38 | if ($xmlStyles && $xmlStyles->numFmts[0]) { |
|
600 | 31 | $numFmts = $xmlStyles->numFmts[0]; |
|
601 | } |
||
602 | 38 | if (isset($numFmts) && ($numFmts !== null)) { |
|
603 | 31 | $numFmts->registerXPathNamespace('sml', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main'); |
|
604 | } |
||
605 | 38 | if (!$this->readDataOnly && $xmlStyles) { |
|
606 | 38 | foreach ($xmlStyles->cellXfs->xf as $xf) { |
|
607 | 38 | $numFmt = NumberFormat::FORMAT_GENERAL; |
|
608 | |||
609 | 38 | if ($xf['numFmtId']) { |
|
610 | 38 | if (isset($numFmts)) { |
|
611 | 31 | $tmpNumFmt = self::getArrayItem($numFmts->xpath("sml:numFmt[@numFmtId=$xf[numFmtId]]")); |
|
612 | |||
613 | 31 | if (isset($tmpNumFmt['formatCode'])) { |
|
614 | 3 | $numFmt = (string) $tmpNumFmt['formatCode']; |
|
615 | } |
||
616 | } |
||
617 | |||
618 | // We shouldn't override any of the built-in MS Excel values (values below id 164) |
||
619 | // But there's a lot of naughty homebrew xlsx writers that do use "reserved" id values that aren't actually used |
||
620 | // So we make allowance for them rather than lose formatting masks |
||
621 | 38 | if ((int) $xf['numFmtId'] < 164 && |
|
622 | 38 | NumberFormat::builtInFormatCode((int) $xf['numFmtId']) !== '') { |
|
623 | 38 | $numFmt = NumberFormat::builtInFormatCode((int) $xf['numFmtId']); |
|
624 | } |
||
625 | } |
||
626 | 38 | $quotePrefix = false; |
|
627 | 38 | if (isset($xf['quotePrefix'])) { |
|
628 | $quotePrefix = (bool) $xf['quotePrefix']; |
||
629 | } |
||
630 | |||
631 | $style = (object) [ |
||
632 | 38 | 'numFmt' => $numFmt, |
|
633 | 38 | 'font' => $xmlStyles->fonts->font[(int) ($xf['fontId'])], |
|
634 | 38 | 'fill' => $xmlStyles->fills->fill[(int) ($xf['fillId'])], |
|
635 | 38 | 'border' => $xmlStyles->borders->border[(int) ($xf['borderId'])], |
|
636 | 38 | 'alignment' => $xf->alignment, |
|
637 | 38 | 'protection' => $xf->protection, |
|
638 | 38 | 'quotePrefix' => $quotePrefix, |
|
639 | ]; |
||
640 | 38 | $styles[] = $style; |
|
641 | |||
642 | // add style to cellXf collection |
||
643 | 38 | $objStyle = new Style(); |
|
644 | 38 | self::readStyle($objStyle, $style); |
|
645 | 38 | $excel->addCellXf($objStyle); |
|
646 | } |
||
647 | |||
648 | 38 | foreach (isset($xmlStyles->cellStyleXfs->xf) ? $xmlStyles->cellStyleXfs->xf : [] as $xf) { |
|
649 | 38 | $numFmt = NumberFormat::FORMAT_GENERAL; |
|
650 | 38 | if ($numFmts && $xf['numFmtId']) { |
|
651 | 31 | $tmpNumFmt = self::getArrayItem($numFmts->xpath("sml:numFmt[@numFmtId=$xf[numFmtId]]")); |
|
652 | 31 | if (isset($tmpNumFmt['formatCode'])) { |
|
653 | 1 | $numFmt = (string) $tmpNumFmt['formatCode']; |
|
654 | 31 | } elseif ((int) $xf['numFmtId'] < 165) { |
|
655 | 31 | $numFmt = NumberFormat::builtInFormatCode((int) $xf['numFmtId']); |
|
656 | } |
||
657 | } |
||
658 | |||
659 | $cellStyle = (object) [ |
||
660 | 38 | 'numFmt' => $numFmt, |
|
661 | 38 | 'font' => $xmlStyles->fonts->font[(int) ($xf['fontId'])], |
|
662 | 38 | 'fill' => $xmlStyles->fills->fill[(int) ($xf['fillId'])], |
|
663 | 38 | 'border' => $xmlStyles->borders->border[(int) ($xf['borderId'])], |
|
664 | 38 | 'alignment' => $xf->alignment, |
|
665 | 38 | 'protection' => $xf->protection, |
|
666 | 38 | 'quotePrefix' => $quotePrefix, |
|
667 | ]; |
||
668 | 38 | $cellStyles[] = $cellStyle; |
|
669 | |||
670 | // add style to cellStyleXf collection |
||
671 | 38 | $objStyle = new Style(); |
|
672 | 38 | self::readStyle($objStyle, $cellStyle); |
|
673 | 38 | $excel->addCellStyleXf($objStyle); |
|
674 | } |
||
675 | } |
||
676 | |||
677 | 38 | $dxfs = []; |
|
678 | 38 | if (!$this->readDataOnly && $xmlStyles) { |
|
679 | // Conditional Styles |
||
680 | 38 | if ($xmlStyles->dxfs) { |
|
681 | 38 | foreach ($xmlStyles->dxfs->dxf as $dxf) { |
|
682 | 1 | $style = new Style(false, true); |
|
683 | 1 | self::readStyle($style, $dxf); |
|
684 | 1 | $dxfs[] = $style; |
|
685 | } |
||
686 | } |
||
687 | // Cell Styles |
||
688 | 38 | if ($xmlStyles->cellStyles) { |
|
689 | 38 | foreach ($xmlStyles->cellStyles->cellStyle as $cellStyle) { |
|
690 | 38 | if ((int) ($cellStyle['builtinId']) == 0) { |
|
691 | 38 | if (isset($cellStyles[(int) ($cellStyle['xfId'])])) { |
|
692 | // Set default style |
||
693 | 38 | $style = new Style(); |
|
694 | 38 | self::readStyle($style, $cellStyles[(int) ($cellStyle['xfId'])]); |
|
695 | |||
696 | // normal style, currently not using it for anything |
||
697 | } |
||
698 | } |
||
699 | } |
||
700 | } |
||
701 | } |
||
702 | |||
703 | //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main" |
||
704 | 38 | $xmlWorkbook = simplexml_load_string( |
|
705 | 38 | $this->securityScanner->scan($this->getFromZipArchive($zip, "{$rel['Target']}")), |
|
706 | 38 | 'SimpleXMLElement', |
|
707 | 38 | Settings::getLibXmlLoaderOptions() |
|
708 | ); |
||
709 | |||
710 | // Set base date |
||
711 | 38 | if ($xmlWorkbook->workbookPr) { |
|
712 | 37 | Date::setExcelCalendar(Date::CALENDAR_WINDOWS_1900); |
|
713 | 37 | if (isset($xmlWorkbook->workbookPr['date1904'])) { |
|
714 | if (self::boolean((string) $xmlWorkbook->workbookPr['date1904'])) { |
||
715 | Date::setExcelCalendar(Date::CALENDAR_MAC_1904); |
||
716 | } |
||
717 | } |
||
718 | } |
||
719 | |||
720 | // Set protection |
||
721 | 38 | $this->readProtection($excel, $xmlWorkbook); |
|
1 ignored issue
–
show
|
|||
722 | |||
723 | 38 | $sheetId = 0; // keep track of new sheet id in final workbook |
|
724 | 38 | $oldSheetId = -1; // keep track of old sheet id in final workbook |
|
725 | 38 | $countSkippedSheets = 0; // keep track of number of skipped sheets |
|
726 | 38 | $mapSheetId = []; // mapping of sheet ids from old to new |
|
727 | |||
728 | 38 | $charts = $chartDetails = []; |
|
729 | |||
730 | 38 | if ($xmlWorkbook->sheets) { |
|
731 | /** @var SimpleXMLElement $eleSheet */ |
||
732 | 38 | foreach ($xmlWorkbook->sheets->sheet as $eleSheet) { |
|
733 | 38 | ++$oldSheetId; |
|
734 | |||
735 | // Check if sheet should be skipped |
||
736 | 38 | if (isset($this->loadSheetsOnly) && !in_array((string) $eleSheet['name'], $this->loadSheetsOnly)) { |
|
737 | 1 | ++$countSkippedSheets; |
|
738 | 1 | $mapSheetId[$oldSheetId] = null; |
|
739 | |||
740 | 1 | continue; |
|
741 | } |
||
742 | |||
743 | // Map old sheet id in original workbook to new sheet id. |
||
744 | // They will differ if loadSheetsOnly() is being used |
||
745 | 38 | $mapSheetId[$oldSheetId] = $oldSheetId - $countSkippedSheets; |
|
746 | |||
747 | // Load sheet |
||
748 | 38 | $docSheet = $excel->createSheet(); |
|
749 | // Use false for $updateFormulaCellReferences to prevent adjustment of worksheet |
||
750 | // references in formula cells... during the load, all formulae should be correct, |
||
751 | // and we're simply bringing the worksheet name in line with the formula, not the |
||
752 | // reverse |
||
753 | 38 | $docSheet->setTitle((string) $eleSheet['name'], false, false); |
|
754 | 38 | $fileWorksheet = $worksheets[(string) self::getArrayItem($eleSheet->attributes('http://schemas.openxmlformats.org/officeDocument/2006/relationships'), 'id')]; |
|
755 | //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main" |
||
756 | 38 | $xmlSheet = simplexml_load_string( |
|
757 | 38 | $this->securityScanner->scan($this->getFromZipArchive($zip, "$dir/$fileWorksheet")), |
|
758 | 38 | 'SimpleXMLElement', |
|
759 | 38 | Settings::getLibXmlLoaderOptions() |
|
760 | ); |
||
761 | |||
762 | 38 | $sharedFormulas = []; |
|
763 | |||
764 | 38 | if (isset($eleSheet['state']) && (string) $eleSheet['state'] != '') { |
|
765 | 1 | $docSheet->setSheetState((string) $eleSheet['state']); |
|
766 | } |
||
767 | |||
768 | 38 | if (isset($xmlSheet->sheetViews, $xmlSheet->sheetViews->sheetView)) { |
|
769 | 38 | if (isset($xmlSheet->sheetViews->sheetView['zoomScale'])) { |
|
770 | $zoomScale = (int) ($xmlSheet->sheetViews->sheetView['zoomScale']); |
||
771 | if ($zoomScale <= 0) { |
||
772 | // setZoomScale will throw an Exception if the scale is less than or equals 0 |
||
773 | // that is OK when manually creating documents, but we should be able to read all documents |
||
774 | $zoomScale = 100; |
||
775 | } |
||
776 | |||
777 | $docSheet->getSheetView()->setZoomScale($zoomScale); |
||
778 | } |
||
779 | 38 | if (isset($xmlSheet->sheetViews->sheetView['zoomScaleNormal'])) { |
|
780 | $zoomScaleNormal = (int) ($xmlSheet->sheetViews->sheetView['zoomScaleNormal']); |
||
781 | if ($zoomScaleNormal <= 0) { |
||
782 | // setZoomScaleNormal will throw an Exception if the scale is less than or equals 0 |
||
783 | // that is OK when manually creating documents, but we should be able to read all documents |
||
784 | $zoomScaleNormal = 100; |
||
785 | } |
||
786 | |||
787 | $docSheet->getSheetView()->setZoomScaleNormal($zoomScaleNormal); |
||
788 | } |
||
789 | 38 | if (isset($xmlSheet->sheetViews->sheetView['view'])) { |
|
790 | $docSheet->getSheetView()->setView((string) $xmlSheet->sheetViews->sheetView['view']); |
||
791 | } |
||
792 | 38 | if (isset($xmlSheet->sheetViews->sheetView['showGridLines'])) { |
|
793 | 30 | $docSheet->setShowGridLines(self::boolean((string) $xmlSheet->sheetViews->sheetView['showGridLines'])); |
|
794 | } |
||
795 | 38 | if (isset($xmlSheet->sheetViews->sheetView['showRowColHeaders'])) { |
|
796 | 30 | $docSheet->setShowRowColHeaders(self::boolean((string) $xmlSheet->sheetViews->sheetView['showRowColHeaders'])); |
|
797 | } |
||
798 | 38 | if (isset($xmlSheet->sheetViews->sheetView['rightToLeft'])) { |
|
799 | $docSheet->setRightToLeft(self::boolean((string) $xmlSheet->sheetViews->sheetView['rightToLeft'])); |
||
800 | } |
||
801 | 38 | if (isset($xmlSheet->sheetViews->sheetView->pane)) { |
|
802 | 3 | $xSplit = 0; |
|
803 | 3 | $ySplit = 0; |
|
804 | 3 | $topLeftCell = null; |
|
805 | |||
806 | 3 | if (isset($xmlSheet->sheetViews->sheetView->pane['xSplit'])) { |
|
807 | 1 | $xSplit = (int) ($xmlSheet->sheetViews->sheetView->pane['xSplit']); |
|
808 | } |
||
809 | |||
810 | 3 | if (isset($xmlSheet->sheetViews->sheetView->pane['ySplit'])) { |
|
811 | 3 | $ySplit = (int) ($xmlSheet->sheetViews->sheetView->pane['ySplit']); |
|
812 | } |
||
813 | |||
814 | 3 | if (isset($xmlSheet->sheetViews->sheetView->pane['topLeftCell'])) { |
|
815 | 3 | $topLeftCell = (string) $xmlSheet->sheetViews->sheetView->pane['topLeftCell']; |
|
816 | } |
||
817 | |||
818 | 3 | $docSheet->freezePane(Coordinate::stringFromColumnIndex($xSplit + 1) . ($ySplit + 1), $topLeftCell); |
|
819 | } |
||
820 | |||
821 | 38 | if (isset($xmlSheet->sheetViews->sheetView->selection)) { |
|
822 | 36 | if (isset($xmlSheet->sheetViews->sheetView->selection['sqref'])) { |
|
823 | 35 | $sqref = (string) $xmlSheet->sheetViews->sheetView->selection['sqref']; |
|
824 | 35 | $sqref = explode(' ', $sqref); |
|
825 | 35 | $sqref = $sqref[0]; |
|
826 | 35 | $docSheet->setSelectedCells($sqref); |
|
827 | } |
||
828 | } |
||
829 | } |
||
830 | |||
831 | 38 | if (isset($xmlSheet->sheetPr, $xmlSheet->sheetPr->tabColor)) { |
|
832 | 2 | if (isset($xmlSheet->sheetPr->tabColor['rgb'])) { |
|
833 | 2 | $docSheet->getTabColor()->setARGB((string) $xmlSheet->sheetPr->tabColor['rgb']); |
|
834 | } |
||
835 | } |
||
836 | 38 | if (isset($xmlSheet->sheetPr, $xmlSheet->sheetPr['codeName'])) { |
|
837 | 1 | $docSheet->setCodeName((string) $xmlSheet->sheetPr['codeName'], false); |
|
838 | } |
||
839 | 38 | if (isset($xmlSheet->sheetPr, $xmlSheet->sheetPr->outlinePr)) { |
|
840 | 30 | if (isset($xmlSheet->sheetPr->outlinePr['summaryRight']) && |
|
841 | 30 | !self::boolean((string) $xmlSheet->sheetPr->outlinePr['summaryRight'])) { |
|
842 | $docSheet->setShowSummaryRight(false); |
||
843 | } else { |
||
844 | 30 | $docSheet->setShowSummaryRight(true); |
|
845 | } |
||
846 | |||
847 | 30 | if (isset($xmlSheet->sheetPr->outlinePr['summaryBelow']) && |
|
848 | 30 | !self::boolean((string) $xmlSheet->sheetPr->outlinePr['summaryBelow'])) { |
|
849 | $docSheet->setShowSummaryBelow(false); |
||
850 | } else { |
||
851 | 30 | $docSheet->setShowSummaryBelow(true); |
|
852 | } |
||
853 | } |
||
854 | |||
855 | 38 | if (isset($xmlSheet->sheetPr, $xmlSheet->sheetPr->pageSetUpPr)) { |
|
856 | if (isset($xmlSheet->sheetPr->pageSetUpPr['fitToPage']) && |
||
857 | !self::boolean((string) $xmlSheet->sheetPr->pageSetUpPr['fitToPage'])) { |
||
858 | $docSheet->getPageSetup()->setFitToPage(false); |
||
859 | } else { |
||
860 | $docSheet->getPageSetup()->setFitToPage(true); |
||
861 | } |
||
862 | } |
||
863 | |||
864 | 38 | if (isset($xmlSheet->sheetFormatPr)) { |
|
865 | 38 | if (isset($xmlSheet->sheetFormatPr['customHeight']) && |
|
866 | 38 | self::boolean((string) $xmlSheet->sheetFormatPr['customHeight']) && |
|
867 | 38 | isset($xmlSheet->sheetFormatPr['defaultRowHeight'])) { |
|
868 | 1 | $docSheet->getDefaultRowDimension()->setRowHeight((float) $xmlSheet->sheetFormatPr['defaultRowHeight']); |
|
869 | } |
||
870 | 38 | if (isset($xmlSheet->sheetFormatPr['defaultColWidth'])) { |
|
871 | 1 | $docSheet->getDefaultColumnDimension()->setWidth((float) $xmlSheet->sheetFormatPr['defaultColWidth']); |
|
872 | } |
||
873 | 38 | if (isset($xmlSheet->sheetFormatPr['zeroHeight']) && |
|
874 | 38 | ((string) $xmlSheet->sheetFormatPr['zeroHeight'] == '1')) { |
|
875 | $docSheet->getDefaultRowDimension()->setZeroHeight(true); |
||
876 | } |
||
877 | } |
||
878 | |||
879 | 38 | if (isset($xmlSheet->printOptions) && !$this->readDataOnly) { |
|
880 | 30 | if (self::boolean((string) $xmlSheet->printOptions['gridLinesSet'])) { |
|
881 | 30 | $docSheet->setShowGridlines(true); |
|
882 | } |
||
883 | 30 | if (self::boolean((string) $xmlSheet->printOptions['gridLines'])) { |
|
884 | $docSheet->setPrintGridlines(true); |
||
885 | } |
||
886 | 30 | if (self::boolean((string) $xmlSheet->printOptions['horizontalCentered'])) { |
|
887 | $docSheet->getPageSetup()->setHorizontalCentered(true); |
||
888 | } |
||
889 | 30 | if (self::boolean((string) $xmlSheet->printOptions['verticalCentered'])) { |
|
890 | $docSheet->getPageSetup()->setVerticalCentered(true); |
||
891 | } |
||
892 | } |
||
893 | |||
894 | 38 | $this->readColumnsAndRowsAttributes($xmlSheet, $docSheet); |
|
1 ignored issue
–
show
|
|||
895 | |||
896 | 38 | if ($xmlSheet && $xmlSheet->sheetData && $xmlSheet->sheetData->row) { |
|
897 | 32 | $cIndex = 1; // Cell Start from 1 |
|
898 | 32 | foreach ($xmlSheet->sheetData->row as $row) { |
|
899 | 32 | $rowIndex = 1; |
|
900 | 32 | foreach ($row->c as $c) { |
|
901 | 32 | $r = (string) $c['r']; |
|
902 | 32 | if ($r == '') { |
|
903 | 1 | $r = Coordinate::stringFromColumnIndex($rowIndex) . $cIndex; |
|
904 | } |
||
905 | 32 | $cellDataType = (string) $c['t']; |
|
906 | 32 | $value = null; |
|
907 | 32 | $calculatedValue = null; |
|
908 | |||
909 | // Read cell? |
||
910 | 32 | if ($this->getReadFilter() !== null) { |
|
911 | 32 | $coordinates = Coordinate::coordinateFromString($r); |
|
912 | |||
913 | 32 | if (!$this->getReadFilter()->readCell($coordinates[0], (int) $coordinates[1], $docSheet->getTitle())) { |
|
914 | 2 | continue; |
|
915 | } |
||
916 | } |
||
917 | |||
918 | // Read cell! |
||
919 | 32 | switch ($cellDataType) { |
|
920 | 32 | case 's': |
|
921 | 19 | if ((string) $c->v != '') { |
|
922 | 19 | $value = $sharedStrings[(int) ($c->v)]; |
|
923 | |||
924 | 19 | if ($value instanceof RichText) { |
|
925 | 19 | $value = clone $value; |
|
926 | } |
||
927 | } else { |
||
928 | $value = ''; |
||
929 | } |
||
930 | |||
931 | 19 | break; |
|
932 | 22 | case 'b': |
|
933 | 4 | if (!isset($c->f)) { |
|
934 | 2 | $value = self::castToBoolean($c); |
|
935 | } else { |
||
936 | // Formula |
||
937 | 2 | $this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToBoolean'); |
|
938 | 2 | if (isset($c->f['t'])) { |
|
939 | $att = $c->f; |
||
940 | $docSheet->getCell($r)->setFormulaAttributes($att); |
||
941 | } |
||
942 | } |
||
943 | |||
944 | 4 | break; |
|
945 | 19 | case 'inlineStr': |
|
946 | 2 | if (isset($c->f)) { |
|
947 | $this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToError'); |
||
948 | } else { |
||
949 | 2 | $value = $this->parseRichText($c->is); |
|
950 | } |
||
951 | |||
952 | 2 | break; |
|
953 | 19 | case 'e': |
|
954 | if (!isset($c->f)) { |
||
955 | $value = self::castToError($c); |
||
956 | } else { |
||
957 | // Formula |
||
958 | $this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToError'); |
||
959 | } |
||
960 | |||
961 | break; |
||
962 | default: |
||
963 | 19 | if (!isset($c->f)) { |
|
964 | 17 | $value = self::castToString($c); |
|
965 | } else { |
||
966 | // Formula |
||
967 | 6 | $this->castToFormula($c, $r, $cellDataType, $value, $calculatedValue, $sharedFormulas, 'castToString'); |
|
968 | } |
||
969 | |||
970 | 19 | break; |
|
971 | } |
||
972 | |||
973 | // Check for numeric values |
||
974 | 32 | if (is_numeric($value) && $cellDataType != 's') { |
|
975 | 15 | if ($value == (int) $value) { |
|
976 | 14 | $value = (int) $value; |
|
977 | 1 | } elseif ($value == (float) $value) { |
|
978 | 1 | $value = (float) $value; |
|
979 | } elseif ($value == (float) $value) { |
||
980 | $value = (float) $value; |
||
981 | } |
||
982 | } |
||
983 | |||
984 | // Rich text? |
||
985 | 32 | if ($value instanceof RichText && $this->readDataOnly) { |
|
986 | $value = $value->getPlainText(); |
||
987 | } |
||
988 | |||
989 | 32 | $cell = $docSheet->getCell($r); |
|
990 | // Assign value |
||
991 | 32 | if ($cellDataType != '') { |
|
992 | 23 | $cell->setValueExplicit($value, $cellDataType); |
|
993 | } else { |
||
994 | 17 | $cell->setValue($value); |
|
995 | } |
||
996 | 32 | if ($calculatedValue !== null) { |
|
997 | 7 | $cell->setCalculatedValue($calculatedValue); |
|
998 | } |
||
999 | |||
1000 | // Style information? |
||
1001 | 32 | if ($c['s'] && !$this->readDataOnly) { |
|
1002 | // no style index means 0, it seems |
||
1003 | 7 | $cell->setXfIndex(isset($styles[(int) ($c['s'])]) ? |
|
1004 | 7 | (int) ($c['s']) : 0); |
|
1005 | } |
||
1006 | 32 | $rowIndex += 1; |
|
1007 | } |
||
1008 | 32 | $cIndex += 1; |
|
1009 | } |
||
1010 | } |
||
1011 | |||
1012 | 38 | $conditionals = []; |
|
1013 | 38 | if (!$this->readDataOnly && $xmlSheet && $xmlSheet->conditionalFormatting) { |
|
1014 | 1 | foreach ($xmlSheet->conditionalFormatting as $conditional) { |
|
1015 | 1 | foreach ($conditional->cfRule as $cfRule) { |
|
1016 | 1 | if (((string) $cfRule['type'] == Conditional::CONDITION_NONE || (string) $cfRule['type'] == Conditional::CONDITION_CELLIS || (string) $cfRule['type'] == Conditional::CONDITION_CONTAINSTEXT || (string) $cfRule['type'] == Conditional::CONDITION_EXPRESSION) && isset($dxfs[(int) ($cfRule['dxfId'])])) { |
|
1017 | 1 | $conditionals[(string) $conditional['sqref']][(int) ($cfRule['priority'])] = $cfRule; |
|
1018 | } |
||
1019 | } |
||
1020 | } |
||
1021 | |||
1022 | 1 | foreach ($conditionals as $ref => $cfRules) { |
|
1023 | 1 | ksort($cfRules); |
|
1024 | 1 | $conditionalStyles = []; |
|
1025 | 1 | foreach ($cfRules as $cfRule) { |
|
1026 | 1 | $objConditional = new Conditional(); |
|
1027 | 1 | $objConditional->setConditionType((string) $cfRule['type']); |
|
1028 | 1 | $objConditional->setOperatorType((string) $cfRule['operator']); |
|
1029 | |||
1030 | 1 | if ((string) $cfRule['text'] != '') { |
|
1031 | $objConditional->setText((string) $cfRule['text']); |
||
1032 | } |
||
1033 | |||
1034 | 1 | if (isset($cfRule['stopIfTrue']) && (int) $cfRule['stopIfTrue'] === 1) { |
|
1035 | 1 | $objConditional->setStopIfTrue(true); |
|
1036 | } |
||
1037 | |||
1038 | 1 | if (count($cfRule->formula) > 1) { |
|
1039 | foreach ($cfRule->formula as $formula) { |
||
1040 | $objConditional->addCondition((string) $formula); |
||
1041 | } |
||
1042 | } else { |
||
1043 | 1 | $objConditional->addCondition((string) $cfRule->formula); |
|
1044 | } |
||
1045 | 1 | $objConditional->setStyle(clone $dxfs[(int) ($cfRule['dxfId'])]); |
|
1046 | 1 | $conditionalStyles[] = $objConditional; |
|
1047 | } |
||
1048 | |||
1049 | // Extract all cell references in $ref |
||
1050 | 1 | $cellBlocks = explode(' ', str_replace('$', '', strtoupper($ref))); |
|
1051 | 1 | foreach ($cellBlocks as $cellBlock) { |
|
1052 | 1 | $docSheet->getStyle($cellBlock)->setConditionalStyles($conditionalStyles); |
|
1053 | } |
||
1054 | } |
||
1055 | } |
||
1056 | |||
1057 | 38 | $aKeys = ['sheet', 'objects', 'scenarios', 'formatCells', 'formatColumns', 'formatRows', 'insertColumns', 'insertRows', 'insertHyperlinks', 'deleteColumns', 'deleteRows', 'selectLockedCells', 'sort', 'autoFilter', 'pivotTables', 'selectUnlockedCells']; |
|
1058 | 38 | if (!$this->readDataOnly && $xmlSheet && $xmlSheet->sheetProtection) { |
|
1059 | 33 | foreach ($aKeys as $key) { |
|
1060 | 33 | $method = 'set' . ucfirst($key); |
|
1061 | 33 | $docSheet->getProtection()->$method(self::boolean((string) $xmlSheet->sheetProtection[$key])); |
|
1062 | } |
||
1063 | } |
||
1064 | |||
1065 | 38 | if (!$this->readDataOnly && $xmlSheet && $xmlSheet->sheetProtection) { |
|
1066 | 33 | $docSheet->getProtection()->setPassword((string) $xmlSheet->sheetProtection['password'], true); |
|
1067 | 33 | if ($xmlSheet->protectedRanges->protectedRange) { |
|
1068 | 2 | foreach ($xmlSheet->protectedRanges->protectedRange as $protectedRange) { |
|
1069 | 2 | $docSheet->protectCells((string) $protectedRange['sqref'], (string) $protectedRange['password'], true); |
|
1070 | } |
||
1071 | } |
||
1072 | } |
||
1073 | |||
1074 | 38 | if ($xmlSheet && $xmlSheet->autoFilter && !$this->readDataOnly) { |
|
1075 | $autoFilterRange = (string) $xmlSheet->autoFilter['ref']; |
||
1076 | if (strpos($autoFilterRange, ':') !== false) { |
||
1077 | $autoFilter = $docSheet->getAutoFilter(); |
||
1078 | $autoFilter->setRange($autoFilterRange); |
||
1079 | |||
1080 | foreach ($xmlSheet->autoFilter->filterColumn as $filterColumn) { |
||
1081 | $column = $autoFilter->getColumnByOffset((int) $filterColumn['colId']); |
||
1082 | // Check for standard filters |
||
1083 | if ($filterColumn->filters) { |
||
1084 | $column->setFilterType(Column::AUTOFILTER_FILTERTYPE_FILTER); |
||
1085 | $filters = $filterColumn->filters; |
||
1086 | if ((isset($filters['blank'])) && ($filters['blank'] == 1)) { |
||
1087 | // Operator is undefined, but always treated as EQUAL |
||
1088 | $column->createRule()->setRule(null, '')->setRuleType(Column\Rule::AUTOFILTER_RULETYPE_FILTER); |
||
1089 | } |
||
1090 | // Standard filters are always an OR join, so no join rule needs to be set |
||
1091 | // Entries can be either filter elements |
||
1092 | foreach ($filters->filter as $filterRule) { |
||
1093 | // Operator is undefined, but always treated as EQUAL |
||
1094 | $column->createRule()->setRule(null, (string) $filterRule['val'])->setRuleType(Column\Rule::AUTOFILTER_RULETYPE_FILTER); |
||
1095 | } |
||
1096 | // Or Date Group elements |
||
1097 | foreach ($filters->dateGroupItem as $dateGroupItem) { |
||
1098 | // Operator is undefined, but always treated as EQUAL |
||
1099 | $column->createRule()->setRule( |
||
1100 | null, |
||
1101 | [ |
||
1102 | 'year' => (string) $dateGroupItem['year'], |
||
1103 | 'month' => (string) $dateGroupItem['month'], |
||
1104 | 'day' => (string) $dateGroupItem['day'], |
||
1105 | 'hour' => (string) $dateGroupItem['hour'], |
||
1106 | 'minute' => (string) $dateGroupItem['minute'], |
||
1107 | 'second' => (string) $dateGroupItem['second'], |
||
1108 | ], |
||
1109 | (string) $dateGroupItem['dateTimeGrouping'] |
||
1110 | ) |
||
1111 | ->setRuleType(Column\Rule::AUTOFILTER_RULETYPE_DATEGROUP); |
||
1112 | } |
||
1113 | } |
||
1114 | // Check for custom filters |
||
1115 | if ($filterColumn->customFilters) { |
||
1116 | $column->setFilterType(Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER); |
||
1117 | $customFilters = $filterColumn->customFilters; |
||
1118 | // Custom filters can an AND or an OR join; |
||
1119 | // and there should only ever be one or two entries |
||
1120 | if ((isset($customFilters['and'])) && ($customFilters['and'] == 1)) { |
||
1121 | $column->setJoin(Column::AUTOFILTER_COLUMN_JOIN_AND); |
||
1122 | } |
||
1123 | foreach ($customFilters->customFilter as $filterRule) { |
||
1124 | $column->createRule()->setRule( |
||
1125 | (string) $filterRule['operator'], |
||
1126 | (string) $filterRule['val'] |
||
1127 | ) |
||
1128 | ->setRuleType(Column\Rule::AUTOFILTER_RULETYPE_CUSTOMFILTER); |
||
1129 | } |
||
1130 | } |
||
1131 | // Check for dynamic filters |
||
1132 | if ($filterColumn->dynamicFilter) { |
||
1133 | $column->setFilterType(Column::AUTOFILTER_FILTERTYPE_DYNAMICFILTER); |
||
1134 | // We should only ever have one dynamic filter |
||
1135 | foreach ($filterColumn->dynamicFilter as $filterRule) { |
||
1136 | // Operator is undefined, but always treated as EQUAL |
||
1137 | $column->createRule()->setRule( |
||
1138 | null, |
||
1139 | (string) $filterRule['val'], |
||
1140 | (string) $filterRule['type'] |
||
1141 | ) |
||
1142 | ->setRuleType(Column\Rule::AUTOFILTER_RULETYPE_DYNAMICFILTER); |
||
1143 | if (isset($filterRule['val'])) { |
||
1144 | $column->setAttribute('val', (string) $filterRule['val']); |
||
1145 | } |
||
1146 | if (isset($filterRule['maxVal'])) { |
||
1147 | $column->setAttribute('maxVal', (string) $filterRule['maxVal']); |
||
1148 | } |
||
1149 | } |
||
1150 | } |
||
1151 | // Check for dynamic filters |
||
1152 | if ($filterColumn->top10) { |
||
1153 | $column->setFilterType(Column::AUTOFILTER_FILTERTYPE_TOPTENFILTER); |
||
1154 | // We should only ever have one top10 filter |
||
1155 | foreach ($filterColumn->top10 as $filterRule) { |
||
1156 | $column->createRule()->setRule( |
||
1157 | (((isset($filterRule['percent'])) && ($filterRule['percent'] == 1)) |
||
1158 | ? Column\Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_PERCENT |
||
1159 | : Column\Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_BY_VALUE |
||
1160 | ), |
||
1161 | (string) $filterRule['val'], |
||
1162 | (((isset($filterRule['top'])) && ($filterRule['top'] == 1)) |
||
1163 | ? Column\Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_TOP |
||
1164 | : Column\Rule::AUTOFILTER_COLUMN_RULE_TOPTEN_BOTTOM |
||
1165 | ) |
||
1166 | ) |
||
1167 | ->setRuleType(Column\Rule::AUTOFILTER_RULETYPE_TOPTENFILTER); |
||
1168 | } |
||
1169 | } |
||
1170 | } |
||
1171 | } |
||
1172 | } |
||
1173 | |||
1174 | 38 | if ($xmlSheet && $xmlSheet->mergeCells && $xmlSheet->mergeCells->mergeCell && !$this->readDataOnly) { |
|
1175 | 6 | foreach ($xmlSheet->mergeCells->mergeCell as $mergeCell) { |
|
1176 | 6 | $mergeRef = (string) $mergeCell['ref']; |
|
1177 | 6 | if (strpos($mergeRef, ':') !== false) { |
|
1178 | 6 | $docSheet->mergeCells((string) $mergeCell['ref']); |
|
1179 | } |
||
1180 | } |
||
1181 | } |
||
1182 | |||
1183 | 38 | if ($xmlSheet && $xmlSheet->pageMargins && !$this->readDataOnly) { |
|
1184 | 37 | $docPageMargins = $docSheet->getPageMargins(); |
|
1185 | 37 | $docPageMargins->setLeft((float) ($xmlSheet->pageMargins['left'])); |
|
1186 | 37 | $docPageMargins->setRight((float) ($xmlSheet->pageMargins['right'])); |
|
1187 | 37 | $docPageMargins->setTop((float) ($xmlSheet->pageMargins['top'])); |
|
1188 | 37 | $docPageMargins->setBottom((float) ($xmlSheet->pageMargins['bottom'])); |
|
1189 | 37 | $docPageMargins->setHeader((float) ($xmlSheet->pageMargins['header'])); |
|
1190 | 37 | $docPageMargins->setFooter((float) ($xmlSheet->pageMargins['footer'])); |
|
1191 | } |
||
1192 | |||
1193 | 38 | if ($xmlSheet && $xmlSheet->pageSetup && !$this->readDataOnly) { |
|
1194 | 37 | $docPageSetup = $docSheet->getPageSetup(); |
|
1195 | |||
1196 | 37 | if (isset($xmlSheet->pageSetup['orientation'])) { |
|
1197 | 37 | $docPageSetup->setOrientation((string) $xmlSheet->pageSetup['orientation']); |
|
1198 | } |
||
1199 | 37 | if (isset($xmlSheet->pageSetup['paperSize'])) { |
|
1200 | 34 | $docPageSetup->setPaperSize((int) ($xmlSheet->pageSetup['paperSize'])); |
|
1201 | } |
||
1202 | 37 | if (isset($xmlSheet->pageSetup['scale'])) { |
|
1203 | 30 | $docPageSetup->setScale((int) ($xmlSheet->pageSetup['scale']), false); |
|
1204 | } |
||
1205 | 37 | if (isset($xmlSheet->pageSetup['fitToHeight']) && (int) ($xmlSheet->pageSetup['fitToHeight']) >= 0) { |
|
1206 | 30 | $docPageSetup->setFitToHeight((int) ($xmlSheet->pageSetup['fitToHeight']), false); |
|
1207 | } |
||
1208 | 37 | if (isset($xmlSheet->pageSetup['fitToWidth']) && (int) ($xmlSheet->pageSetup['fitToWidth']) >= 0) { |
|
1209 | 30 | $docPageSetup->setFitToWidth((int) ($xmlSheet->pageSetup['fitToWidth']), false); |
|
1210 | } |
||
1211 | 37 | if (isset($xmlSheet->pageSetup['firstPageNumber'], $xmlSheet->pageSetup['useFirstPageNumber']) && |
|
1212 | 37 | self::boolean((string) $xmlSheet->pageSetup['useFirstPageNumber'])) { |
|
1213 | $docPageSetup->setFirstPageNumber((int) ($xmlSheet->pageSetup['firstPageNumber'])); |
||
1214 | } |
||
1215 | |||
1216 | 37 | $relAttributes = $xmlSheet->pageSetup->attributes('http://schemas.openxmlformats.org/officeDocument/2006/relationships'); |
|
1217 | 37 | if (isset($relAttributes['id'])) { |
|
1218 | 7 | $unparsedLoadedData['sheets'][$docSheet->getCodeName()]['pageSetupRelId'] = (string) $relAttributes['id']; |
|
1219 | } |
||
1220 | } |
||
1221 | |||
1222 | 38 | if ($xmlSheet && $xmlSheet->headerFooter && !$this->readDataOnly) { |
|
1223 | 32 | $docHeaderFooter = $docSheet->getHeaderFooter(); |
|
1224 | |||
1225 | 32 | if (isset($xmlSheet->headerFooter['differentOddEven']) && |
|
1226 | 32 | self::boolean((string) $xmlSheet->headerFooter['differentOddEven'])) { |
|
1227 | $docHeaderFooter->setDifferentOddEven(true); |
||
1228 | } else { |
||
1229 | 32 | $docHeaderFooter->setDifferentOddEven(false); |
|
1230 | } |
||
1231 | 32 | if (isset($xmlSheet->headerFooter['differentFirst']) && |
|
1232 | 32 | self::boolean((string) $xmlSheet->headerFooter['differentFirst'])) { |
|
1233 | $docHeaderFooter->setDifferentFirst(true); |
||
1234 | } else { |
||
1235 | 32 | $docHeaderFooter->setDifferentFirst(false); |
|
1236 | } |
||
1237 | 32 | if (isset($xmlSheet->headerFooter['scaleWithDoc']) && |
|
1238 | 32 | !self::boolean((string) $xmlSheet->headerFooter['scaleWithDoc'])) { |
|
1239 | $docHeaderFooter->setScaleWithDocument(false); |
||
1240 | } else { |
||
1241 | 32 | $docHeaderFooter->setScaleWithDocument(true); |
|
1242 | } |
||
1243 | 32 | if (isset($xmlSheet->headerFooter['alignWithMargins']) && |
|
1244 | 32 | !self::boolean((string) $xmlSheet->headerFooter['alignWithMargins'])) { |
|
1245 | 3 | $docHeaderFooter->setAlignWithMargins(false); |
|
1246 | } else { |
||
1247 | 29 | $docHeaderFooter->setAlignWithMargins(true); |
|
1248 | } |
||
1249 | |||
1250 | 32 | $docHeaderFooter->setOddHeader((string) $xmlSheet->headerFooter->oddHeader); |
|
1251 | 32 | $docHeaderFooter->setOddFooter((string) $xmlSheet->headerFooter->oddFooter); |
|
1252 | 32 | $docHeaderFooter->setEvenHeader((string) $xmlSheet->headerFooter->evenHeader); |
|
1253 | 32 | $docHeaderFooter->setEvenFooter((string) $xmlSheet->headerFooter->evenFooter); |
|
1254 | 32 | $docHeaderFooter->setFirstHeader((string) $xmlSheet->headerFooter->firstHeader); |
|
1255 | 32 | $docHeaderFooter->setFirstFooter((string) $xmlSheet->headerFooter->firstFooter); |
|
1256 | } |
||
1257 | |||
1258 | 38 | if ($xmlSheet && $xmlSheet->rowBreaks && $xmlSheet->rowBreaks->brk && !$this->readDataOnly) { |
|
1259 | foreach ($xmlSheet->rowBreaks->brk as $brk) { |
||
1260 | if ($brk['man']) { |
||
1261 | $docSheet->setBreak("A$brk[id]", Worksheet::BREAK_ROW); |
||
1262 | } |
||
1263 | } |
||
1264 | } |
||
1265 | 38 | if ($xmlSheet && $xmlSheet->colBreaks && $xmlSheet->colBreaks->brk && !$this->readDataOnly) { |
|
1266 | foreach ($xmlSheet->colBreaks->brk as $brk) { |
||
1267 | if ($brk['man']) { |
||
1268 | $docSheet->setBreak(Coordinate::stringFromColumnIndex((string) $brk['id'] + 1) . '1', Worksheet::BREAK_COLUMN); |
||
1269 | } |
||
1270 | } |
||
1271 | } |
||
1272 | |||
1273 | 38 | if ($xmlSheet && $xmlSheet->dataValidations && !$this->readDataOnly) { |
|
1274 | foreach ($xmlSheet->dataValidations->dataValidation as $dataValidation) { |
||
1275 | // Uppercase coordinate |
||
1276 | $range = strtoupper($dataValidation['sqref']); |
||
1277 | $rangeSet = explode(' ', $range); |
||
1278 | foreach ($rangeSet as $range) { |
||
1279 | $stRange = $docSheet->shrinkRangeToFit($range); |
||
1280 | |||
1281 | // Extract all cell references in $range |
||
1282 | foreach (Coordinate::extractAllCellReferencesInRange($stRange) as $reference) { |
||
1283 | // Create validation |
||
1284 | $docValidation = $docSheet->getCell($reference)->getDataValidation(); |
||
1285 | $docValidation->setType((string) $dataValidation['type']); |
||
1286 | $docValidation->setErrorStyle((string) $dataValidation['errorStyle']); |
||
1287 | $docValidation->setOperator((string) $dataValidation['operator']); |
||
1288 | $docValidation->setAllowBlank($dataValidation['allowBlank'] != 0); |
||
1289 | $docValidation->setShowDropDown($dataValidation['showDropDown'] == 0); |
||
1290 | $docValidation->setShowInputMessage($dataValidation['showInputMessage'] != 0); |
||
1291 | $docValidation->setShowErrorMessage($dataValidation['showErrorMessage'] != 0); |
||
1292 | $docValidation->setErrorTitle((string) $dataValidation['errorTitle']); |
||
1293 | $docValidation->setError((string) $dataValidation['error']); |
||
1294 | $docValidation->setPromptTitle((string) $dataValidation['promptTitle']); |
||
1295 | $docValidation->setPrompt((string) $dataValidation['prompt']); |
||
1296 | $docValidation->setFormula1((string) $dataValidation->formula1); |
||
1297 | $docValidation->setFormula2((string) $dataValidation->formula2); |
||
1298 | } |
||
1299 | } |
||
1300 | } |
||
1301 | } |
||
1302 | |||
1303 | // unparsed sheet AlternateContent |
||
1304 | 38 | if ($xmlSheet && !$this->readDataOnly) { |
|
1305 | 38 | $mc = $xmlSheet->children('http://schemas.openxmlformats.org/markup-compatibility/2006'); |
|
1306 | 38 | if ($mc->AlternateContent) { |
|
1307 | 1 | foreach ($mc->AlternateContent as $alternateContent) { |
|
1308 | 1 | $unparsedLoadedData['sheets'][$docSheet->getCodeName()]['AlternateContents'][] = $alternateContent->asXML(); |
|
1309 | } |
||
1310 | } |
||
1311 | } |
||
1312 | |||
1313 | // Add hyperlinks |
||
1314 | 38 | $hyperlinks = []; |
|
1315 | 38 | if (!$this->readDataOnly) { |
|
1316 | // Locate hyperlink relations |
||
1317 | 38 | if ($zip->locateName(dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels')) { |
|
1318 | //~ http://schemas.openxmlformats.org/package/2006/relationships" |
||
1319 | 37 | $relsWorksheet = simplexml_load_string( |
|
1320 | 37 | $this->securityScanner->scan( |
|
1321 | 37 | $this->getFromZipArchive($zip, dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels') |
|
1322 | ), |
||
1323 | 37 | 'SimpleXMLElement', |
|
1324 | 37 | Settings::getLibXmlLoaderOptions() |
|
1325 | ); |
||
1326 | 37 | foreach ($relsWorksheet->Relationship as $ele) { |
|
1327 | 12 | if ($ele['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink') { |
|
1328 | 12 | $hyperlinks[(string) $ele['Id']] = (string) $ele['Target']; |
|
1329 | } |
||
1330 | } |
||
1331 | } |
||
1332 | |||
1333 | // Loop through hyperlinks |
||
1334 | 38 | if ($xmlSheet && $xmlSheet->hyperlinks) { |
|
1335 | /** @var SimpleXMLElement $hyperlink */ |
||
1336 | 2 | foreach ($xmlSheet->hyperlinks->hyperlink as $hyperlink) { |
|
1337 | // Link url |
||
1338 | 2 | $linkRel = $hyperlink->attributes('http://schemas.openxmlformats.org/officeDocument/2006/relationships'); |
|
1339 | |||
1340 | 2 | foreach (Coordinate::extractAllCellReferencesInRange($hyperlink['ref']) as $cellReference) { |
|
1341 | 2 | $cell = $docSheet->getCell($cellReference); |
|
1342 | 2 | if (isset($linkRel['id'])) { |
|
1343 | 2 | $hyperlinkUrl = $hyperlinks[(string) $linkRel['id']]; |
|
1344 | 2 | if (isset($hyperlink['location'])) { |
|
1345 | $hyperlinkUrl .= '#' . (string) $hyperlink['location']; |
||
1346 | } |
||
1347 | 2 | $cell->getHyperlink()->setUrl($hyperlinkUrl); |
|
1348 | 2 | } elseif (isset($hyperlink['location'])) { |
|
1349 | 2 | $cell->getHyperlink()->setUrl('sheet://' . (string) $hyperlink['location']); |
|
1350 | } |
||
1351 | |||
1352 | // Tooltip |
||
1353 | 2 | if (isset($hyperlink['tooltip'])) { |
|
1354 | 2 | $cell->getHyperlink()->setTooltip((string) $hyperlink['tooltip']); |
|
1355 | } |
||
1356 | } |
||
1357 | } |
||
1358 | } |
||
1359 | } |
||
1360 | |||
1361 | // Add comments |
||
1362 | 38 | $comments = []; |
|
1363 | 38 | $vmlComments = []; |
|
1364 | 38 | if (!$this->readDataOnly) { |
|
1365 | // Locate comment relations |
||
1366 | 38 | if ($zip->locateName(dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels')) { |
|
1367 | //~ http://schemas.openxmlformats.org/package/2006/relationships" |
||
1368 | 37 | $relsWorksheet = simplexml_load_string( |
|
1369 | 37 | $this->securityScanner->scan( |
|
1370 | 37 | $this->getFromZipArchive($zip, dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels') |
|
1371 | ), |
||
1372 | 37 | 'SimpleXMLElement', |
|
1373 | 37 | Settings::getLibXmlLoaderOptions() |
|
1374 | ); |
||
1375 | 37 | foreach ($relsWorksheet->Relationship as $ele) { |
|
1376 | 12 | if ($ele['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments') { |
|
1377 | 3 | $comments[(string) $ele['Id']] = (string) $ele['Target']; |
|
1378 | } |
||
1379 | 12 | if ($ele['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing') { |
|
1380 | 12 | $vmlComments[(string) $ele['Id']] = (string) $ele['Target']; |
|
1381 | } |
||
1382 | } |
||
1383 | } |
||
1384 | |||
1385 | // Loop through comments |
||
1386 | 38 | foreach ($comments as $relName => $relPath) { |
|
1387 | // Load comments file |
||
1388 | 3 | $relPath = File::realpath(dirname("$dir/$fileWorksheet") . '/' . $relPath); |
|
1389 | 3 | $commentsFile = simplexml_load_string( |
|
1390 | 3 | $this->securityScanner->scan($this->getFromZipArchive($zip, $relPath)), |
|
1391 | 3 | 'SimpleXMLElement', |
|
1392 | 3 | Settings::getLibXmlLoaderOptions() |
|
1393 | ); |
||
1394 | |||
1395 | // Utility variables |
||
1396 | 3 | $authors = []; |
|
1397 | |||
1398 | // Loop through authors |
||
1399 | 3 | foreach ($commentsFile->authors->author as $author) { |
|
1400 | 3 | $authors[] = (string) $author; |
|
1401 | } |
||
1402 | |||
1403 | // Loop through contents |
||
1404 | 3 | foreach ($commentsFile->commentList->comment as $comment) { |
|
1405 | 3 | if (!empty($comment['authorId'])) { |
|
1406 | $docSheet->getComment((string) $comment['ref'])->setAuthor($authors[(string) $comment['authorId']]); |
||
1407 | } |
||
1408 | 3 | $docSheet->getComment((string) $comment['ref'])->setText($this->parseRichText($comment->text)); |
|
1409 | } |
||
1410 | } |
||
1411 | |||
1412 | // later we will remove from it real vmlComments |
||
1413 | 38 | $unparsedVmlDrawings = $vmlComments; |
|
1414 | |||
1415 | // Loop through VML comments |
||
1416 | 38 | foreach ($vmlComments as $relName => $relPath) { |
|
1417 | // Load VML comments file |
||
1418 | 4 | $relPath = File::realpath(dirname("$dir/$fileWorksheet") . '/' . $relPath); |
|
1419 | 4 | $vmlCommentsFile = simplexml_load_string( |
|
1420 | 4 | $this->securityScanner->scan($this->getFromZipArchive($zip, $relPath)), |
|
1421 | 4 | 'SimpleXMLElement', |
|
1422 | 4 | Settings::getLibXmlLoaderOptions() |
|
1423 | ); |
||
1424 | 4 | $vmlCommentsFile->registerXPathNamespace('v', 'urn:schemas-microsoft-com:vml'); |
|
1425 | |||
1426 | 4 | $shapes = $vmlCommentsFile->xpath('//v:shape'); |
|
1427 | 4 | foreach ($shapes as $shape) { |
|
1428 | 4 | $shape->registerXPathNamespace('v', 'urn:schemas-microsoft-com:vml'); |
|
1429 | |||
1430 | 4 | if (isset($shape['style'])) { |
|
1431 | 4 | $style = (string) $shape['style']; |
|
1432 | 4 | $fillColor = strtoupper(substr((string) $shape['fillcolor'], 1)); |
|
1433 | 4 | $column = null; |
|
1434 | 4 | $row = null; |
|
1435 | |||
1436 | 4 | $clientData = $shape->xpath('.//x:ClientData'); |
|
1437 | 4 | if (is_array($clientData) && !empty($clientData)) { |
|
1438 | 4 | $clientData = $clientData[0]; |
|
1439 | |||
1440 | 4 | if (isset($clientData['ObjectType']) && (string) $clientData['ObjectType'] == 'Note') { |
|
1441 | 3 | $temp = $clientData->xpath('.//x:Row'); |
|
1442 | 3 | if (is_array($temp)) { |
|
1443 | 3 | $row = $temp[0]; |
|
1444 | } |
||
1445 | |||
1446 | 3 | $temp = $clientData->xpath('.//x:Column'); |
|
1447 | 3 | if (is_array($temp)) { |
|
1448 | 3 | $column = $temp[0]; |
|
1449 | } |
||
1450 | } |
||
1451 | } |
||
1452 | |||
1453 | 4 | if (($column !== null) && ($row !== null)) { |
|
1454 | // Set comment properties |
||
1455 | 3 | $comment = $docSheet->getCommentByColumnAndRow($column + 1, $row + 1); |
|
1456 | 3 | $comment->getFillColor()->setRGB($fillColor); |
|
1457 | |||
1458 | // Parse style |
||
1459 | 3 | $styleArray = explode(';', str_replace(' ', '', $style)); |
|
1460 | 3 | foreach ($styleArray as $stylePair) { |
|
1461 | 3 | $stylePair = explode(':', $stylePair); |
|
1462 | |||
1463 | 3 | if ($stylePair[0] == 'margin-left') { |
|
1464 | 3 | $comment->setMarginLeft($stylePair[1]); |
|
1465 | } |
||
1466 | 3 | if ($stylePair[0] == 'margin-top') { |
|
1467 | 3 | $comment->setMarginTop($stylePair[1]); |
|
1468 | } |
||
1469 | 3 | if ($stylePair[0] == 'width') { |
|
1470 | 3 | $comment->setWidth($stylePair[1]); |
|
1471 | } |
||
1472 | 3 | if ($stylePair[0] == 'height') { |
|
1473 | 3 | $comment->setHeight($stylePair[1]); |
|
1474 | } |
||
1475 | 3 | if ($stylePair[0] == 'visibility') { |
|
1476 | 3 | $comment->setVisible($stylePair[1] == 'visible'); |
|
1477 | } |
||
1478 | } |
||
1479 | |||
1480 | 4 | unset($unparsedVmlDrawings[$relName]); |
|
1481 | } |
||
1482 | } |
||
1483 | } |
||
1484 | } |
||
1485 | |||
1486 | // unparsed vmlDrawing |
||
1487 | 38 | if ($unparsedVmlDrawings) { |
|
1488 | 1 | foreach ($unparsedVmlDrawings as $rId => $relPath) { |
|
1489 | 1 | $rId = substr($rId, 3); // rIdXXX |
|
1490 | 1 | $unparsedVmlDrawing = &$unparsedLoadedData['sheets'][$docSheet->getCodeName()]['vmlDrawings']; |
|
1491 | 1 | $unparsedVmlDrawing[$rId] = []; |
|
1492 | 1 | $unparsedVmlDrawing[$rId]['filePath'] = self::dirAdd("$dir/$fileWorksheet", $relPath); |
|
1493 | 1 | $unparsedVmlDrawing[$rId]['relFilePath'] = $relPath; |
|
1494 | 1 | $unparsedVmlDrawing[$rId]['content'] = $this->securityScanner->scan($this->getFromZipArchive($zip, $unparsedVmlDrawing[$rId]['filePath'])); |
|
1495 | 1 | unset($unparsedVmlDrawing); |
|
1496 | } |
||
1497 | } |
||
1498 | |||
1499 | // Header/footer images |
||
1500 | 38 | if ($xmlSheet && $xmlSheet->legacyDrawingHF && !$this->readDataOnly) { |
|
1501 | if ($zip->locateName(dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels')) { |
||
1502 | //~ http://schemas.openxmlformats.org/package/2006/relationships" |
||
1503 | $relsWorksheet = simplexml_load_string( |
||
1504 | $this->securityScanner->scan( |
||
1505 | $this->getFromZipArchive($zip, dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels') |
||
1506 | ), |
||
1507 | 'SimpleXMLElement', |
||
1508 | Settings::getLibXmlLoaderOptions() |
||
1509 | ); |
||
1510 | $vmlRelationship = ''; |
||
1511 | |||
1512 | foreach ($relsWorksheet->Relationship as $ele) { |
||
1513 | if ($ele['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing') { |
||
1514 | $vmlRelationship = self::dirAdd("$dir/$fileWorksheet", $ele['Target']); |
||
1515 | } |
||
1516 | } |
||
1517 | |||
1518 | if ($vmlRelationship != '') { |
||
1519 | // Fetch linked images |
||
1520 | //~ http://schemas.openxmlformats.org/package/2006/relationships" |
||
1521 | $relsVML = simplexml_load_string( |
||
1522 | $this->securityScanner->scan( |
||
1523 | $this->getFromZipArchive($zip, dirname($vmlRelationship) . '/_rels/' . basename($vmlRelationship) . '.rels') |
||
1524 | ), |
||
1525 | 'SimpleXMLElement', |
||
1526 | Settings::getLibXmlLoaderOptions() |
||
1527 | ); |
||
1528 | $drawings = []; |
||
1529 | foreach ($relsVML->Relationship as $ele) { |
||
1530 | if ($ele['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image') { |
||
1531 | $drawings[(string) $ele['Id']] = self::dirAdd($vmlRelationship, $ele['Target']); |
||
1532 | } |
||
1533 | } |
||
1534 | |||
1535 | // Fetch VML document |
||
1536 | $vmlDrawing = simplexml_load_string( |
||
1537 | $this->securityScanner->scan($this->getFromZipArchive($zip, $vmlRelationship)), |
||
1538 | 'SimpleXMLElement', |
||
1539 | Settings::getLibXmlLoaderOptions() |
||
1540 | ); |
||
1541 | $vmlDrawing->registerXPathNamespace('v', 'urn:schemas-microsoft-com:vml'); |
||
1542 | |||
1543 | $hfImages = []; |
||
1544 | |||
1545 | $shapes = $vmlDrawing->xpath('//v:shape'); |
||
1546 | foreach ($shapes as $idx => $shape) { |
||
1547 | $shape->registerXPathNamespace('v', 'urn:schemas-microsoft-com:vml'); |
||
1548 | $imageData = $shape->xpath('//v:imagedata'); |
||
1549 | |||
1550 | if (!$imageData) { |
||
1551 | continue; |
||
1552 | } |
||
1553 | |||
1554 | $imageData = $imageData[$idx]; |
||
1555 | |||
1556 | $imageData = $imageData->attributes('urn:schemas-microsoft-com:office:office'); |
||
1557 | $style = self::toCSSArray((string) $shape['style']); |
||
1558 | |||
1559 | $hfImages[(string) $shape['id']] = new HeaderFooterDrawing(); |
||
1560 | if (isset($imageData['title'])) { |
||
1561 | $hfImages[(string) $shape['id']]->setName((string) $imageData['title']); |
||
1562 | } |
||
1563 | |||
1564 | $hfImages[(string) $shape['id']]->setPath('zip://' . File::realpath($pFilename) . '#' . $drawings[(string) $imageData['relid']], false); |
||
1565 | $hfImages[(string) $shape['id']]->setResizeProportional(false); |
||
1566 | $hfImages[(string) $shape['id']]->setWidth($style['width']); |
||
1567 | $hfImages[(string) $shape['id']]->setHeight($style['height']); |
||
1568 | if (isset($style['margin-left'])) { |
||
1569 | $hfImages[(string) $shape['id']]->setOffsetX($style['margin-left']); |
||
1570 | } |
||
1571 | $hfImages[(string) $shape['id']]->setOffsetY($style['margin-top']); |
||
1572 | $hfImages[(string) $shape['id']]->setResizeProportional(true); |
||
1573 | } |
||
1574 | |||
1575 | $docSheet->getHeaderFooter()->setImages($hfImages); |
||
1576 | } |
||
1577 | } |
||
1578 | } |
||
1579 | } |
||
1580 | |||
1581 | // TODO: Autoshapes from twoCellAnchors! |
||
1582 | 38 | if ($zip->locateName(dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels')) { |
|
1583 | //~ http://schemas.openxmlformats.org/package/2006/relationships" |
||
1584 | 37 | $relsWorksheet = simplexml_load_string( |
|
1585 | 37 | $this->securityScanner->scan( |
|
1586 | 37 | $this->getFromZipArchive($zip, dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels') |
|
1587 | ), |
||
1588 | 37 | 'SimpleXMLElement', |
|
1589 | 37 | Settings::getLibXmlLoaderOptions() |
|
1590 | ); |
||
1591 | 37 | $drawings = []; |
|
1592 | 37 | foreach ($relsWorksheet->Relationship as $ele) { |
|
1593 | 12 | if ($ele['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing') { |
|
1594 | 12 | $drawings[(string) $ele['Id']] = self::dirAdd("$dir/$fileWorksheet", $ele['Target']); |
|
1595 | } |
||
1596 | } |
||
1597 | 37 | if ($xmlSheet->drawing && !$this->readDataOnly) { |
|
1598 | 7 | foreach ($xmlSheet->drawing as $drawing) { |
|
1599 | 7 | $fileDrawing = $drawings[(string) self::getArrayItem($drawing->attributes('http://schemas.openxmlformats.org/officeDocument/2006/relationships'), 'id')]; |
|
1600 | //~ http://schemas.openxmlformats.org/package/2006/relationships" |
||
1601 | 7 | $relsDrawing = simplexml_load_string( |
|
1602 | 7 | $this->securityScanner->scan( |
|
1603 | 7 | $this->getFromZipArchive($zip, dirname($fileDrawing) . '/_rels/' . basename($fileDrawing) . '.rels') |
|
1604 | ), |
||
1605 | 7 | 'SimpleXMLElement', |
|
1606 | 7 | Settings::getLibXmlLoaderOptions() |
|
1607 | ); |
||
1608 | 7 | $images = []; |
|
1609 | 7 | $hyperlinks = []; |
|
1610 | 7 | if ($relsDrawing && $relsDrawing->Relationship) { |
|
1611 | 6 | foreach ($relsDrawing->Relationship as $ele) { |
|
1612 | 6 | if ($ele['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink') { |
|
1613 | 2 | $hyperlinks[(string) $ele['Id']] = (string) $ele['Target']; |
|
1614 | } |
||
1615 | 6 | if ($ele['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image') { |
|
1616 | 6 | $images[(string) $ele['Id']] = self::dirAdd($fileDrawing, $ele['Target']); |
|
1617 | 4 | } elseif ($ele['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart') { |
|
1618 | 2 | if ($this->includeCharts) { |
|
1619 | 2 | $charts[self::dirAdd($fileDrawing, $ele['Target'])] = [ |
|
1620 | 2 | 'id' => (string) $ele['Id'], |
|
1621 | 6 | 'sheet' => $docSheet->getTitle(), |
|
1622 | ]; |
||
1623 | } |
||
1624 | } |
||
1625 | } |
||
1626 | } |
||
1627 | 7 | $xmlDrawing = simplexml_load_string( |
|
1628 | 7 | $this->securityScanner->scan($this->getFromZipArchive($zip, $fileDrawing)), |
|
1629 | 7 | 'SimpleXMLElement', |
|
1630 | 7 | Settings::getLibXmlLoaderOptions() |
|
1631 | 7 | )->children('http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing'); |
|
1632 | |||
1633 | 7 | if ($xmlDrawing->oneCellAnchor) { |
|
1634 | 4 | foreach ($xmlDrawing->oneCellAnchor as $oneCellAnchor) { |
|
1635 | 4 | if ($oneCellAnchor->pic->blipFill) { |
|
1636 | /** @var SimpleXMLElement $blip */ |
||
1637 | 4 | $blip = $oneCellAnchor->pic->blipFill->children('http://schemas.openxmlformats.org/drawingml/2006/main')->blip; |
|
1638 | /** @var SimpleXMLElement $xfrm */ |
||
1639 | 4 | $xfrm = $oneCellAnchor->pic->spPr->children('http://schemas.openxmlformats.org/drawingml/2006/main')->xfrm; |
|
1640 | /** @var SimpleXMLElement $outerShdw */ |
||
1641 | 4 | $outerShdw = $oneCellAnchor->pic->spPr->children('http://schemas.openxmlformats.org/drawingml/2006/main')->effectLst->outerShdw; |
|
1642 | /** @var \SimpleXMLElement $hlinkClick */ |
||
1643 | 4 | $hlinkClick = $oneCellAnchor->pic->nvPicPr->cNvPr->children('http://schemas.openxmlformats.org/drawingml/2006/main')->hlinkClick; |
|
1644 | |||
1645 | 4 | $objDrawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing(); |
|
1646 | 4 | $objDrawing->setName((string) self::getArrayItem($oneCellAnchor->pic->nvPicPr->cNvPr->attributes(), 'name')); |
|
1647 | 4 | $objDrawing->setDescription((string) self::getArrayItem($oneCellAnchor->pic->nvPicPr->cNvPr->attributes(), 'descr')); |
|
1648 | 4 | $objDrawing->setPath( |
|
1649 | 4 | 'zip://' . File::realpath($pFilename) . '#' . |
|
1650 | 4 | $images[(string) self::getArrayItem( |
|
1651 | 4 | $blip->attributes('http://schemas.openxmlformats.org/officeDocument/2006/relationships'), |
|
1652 | 4 | 'embed' |
|
1653 | )], |
||
1654 | 4 | false |
|
1655 | ); |
||
1656 | 4 | $objDrawing->setCoordinates(Coordinate::stringFromColumnIndex(((string) $oneCellAnchor->from->col) + 1) . ($oneCellAnchor->from->row + 1)); |
|
1657 | 4 | $objDrawing->setOffsetX(Drawing::EMUToPixels($oneCellAnchor->from->colOff)); |
|
1658 | 4 | $objDrawing->setOffsetY(Drawing::EMUToPixels($oneCellAnchor->from->rowOff)); |
|
1659 | 4 | $objDrawing->setResizeProportional(false); |
|
1660 | 4 | $objDrawing->setWidth(Drawing::EMUToPixels(self::getArrayItem($oneCellAnchor->ext->attributes(), 'cx'))); |
|
1661 | 4 | $objDrawing->setHeight(Drawing::EMUToPixels(self::getArrayItem($oneCellAnchor->ext->attributes(), 'cy'))); |
|
1662 | 4 | if ($xfrm) { |
|
1663 | 4 | $objDrawing->setRotation(Drawing::angleToDegrees(self::getArrayItem($xfrm->attributes(), 'rot'))); |
|
1 ignored issue
–
show
|
|||
1664 | } |
||
1665 | 4 | if ($outerShdw) { |
|
1666 | 2 | $shadow = $objDrawing->getShadow(); |
|
1667 | 2 | $shadow->setVisible(true); |
|
1668 | 2 | $shadow->setBlurRadius(Drawing::EMUTopixels(self::getArrayItem($outerShdw->attributes(), 'blurRad'))); |
|
1 ignored issue
–
show
|
|||
1669 | 2 | $shadow->setDistance(Drawing::EMUTopixels(self::getArrayItem($outerShdw->attributes(), 'dist'))); |
|
1670 | 2 | $shadow->setDirection(Drawing::angleToDegrees(self::getArrayItem($outerShdw->attributes(), 'dir'))); |
|
1671 | 2 | $shadow->setAlignment((string) self::getArrayItem($outerShdw->attributes(), 'algn')); |
|
1672 | 2 | $shadow->getColor()->setRGB(self::getArrayItem($outerShdw->srgbClr->attributes(), 'val')); |
|
1673 | 2 | $shadow->setAlpha(self::getArrayItem($outerShdw->srgbClr->alpha->attributes(), 'val') / 1000); |
|
1674 | } |
||
1675 | |||
1676 | 4 | $this->readHyperLinkDrawing($objDrawing, $oneCellAnchor, $hyperlinks); |
|
1677 | |||
1678 | 4 | $objDrawing->setWorksheet($docSheet); |
|
1679 | } else { |
||
1680 | // ? Can charts be positioned with a oneCellAnchor ? |
||
1681 | $coordinates = Coordinate::stringFromColumnIndex(((string) $oneCellAnchor->from->col) + 1) . ($oneCellAnchor->from->row + 1); |
||
1682 | $offsetX = Drawing::EMUToPixels($oneCellAnchor->from->colOff); |
||
1683 | $offsetY = Drawing::EMUToPixels($oneCellAnchor->from->rowOff); |
||
1684 | $width = Drawing::EMUToPixels(self::getArrayItem($oneCellAnchor->ext->attributes(), 'cx')); |
||
1685 | 4 | $height = Drawing::EMUToPixels(self::getArrayItem($oneCellAnchor->ext->attributes(), 'cy')); |
|
1686 | } |
||
1687 | } |
||
1688 | } |
||
1689 | 7 | if ($xmlDrawing->twoCellAnchor) { |
|
1690 | 2 | foreach ($xmlDrawing->twoCellAnchor as $twoCellAnchor) { |
|
1691 | 2 | if ($twoCellAnchor->pic->blipFill) { |
|
1692 | 2 | $blip = $twoCellAnchor->pic->blipFill->children('http://schemas.openxmlformats.org/drawingml/2006/main')->blip; |
|
1693 | 2 | $xfrm = $twoCellAnchor->pic->spPr->children('http://schemas.openxmlformats.org/drawingml/2006/main')->xfrm; |
|
1694 | 2 | $outerShdw = $twoCellAnchor->pic->spPr->children('http://schemas.openxmlformats.org/drawingml/2006/main')->effectLst->outerShdw; |
|
1695 | 2 | $hlinkClick = $twoCellAnchor->pic->nvPicPr->cNvPr->children('http://schemas.openxmlformats.org/drawingml/2006/main')->hlinkClick; |
|
1696 | 2 | $objDrawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing(); |
|
1697 | 2 | $objDrawing->setName((string) self::getArrayItem($twoCellAnchor->pic->nvPicPr->cNvPr->attributes(), 'name')); |
|
1698 | 2 | $objDrawing->setDescription((string) self::getArrayItem($twoCellAnchor->pic->nvPicPr->cNvPr->attributes(), 'descr')); |
|
1699 | 2 | $objDrawing->setPath( |
|
1700 | 2 | 'zip://' . File::realpath($pFilename) . '#' . |
|
1701 | 2 | $images[(string) self::getArrayItem( |
|
1702 | 2 | $blip->attributes('http://schemas.openxmlformats.org/officeDocument/2006/relationships'), |
|
1703 | 2 | 'embed' |
|
1704 | )], |
||
1705 | 2 | false |
|
1706 | ); |
||
1707 | 2 | $objDrawing->setCoordinates(Coordinate::stringFromColumnIndex(((string) $twoCellAnchor->from->col) + 1) . ($twoCellAnchor->from->row + 1)); |
|
1708 | 2 | $objDrawing->setOffsetX(Drawing::EMUToPixels($twoCellAnchor->from->colOff)); |
|
1709 | 2 | $objDrawing->setOffsetY(Drawing::EMUToPixels($twoCellAnchor->from->rowOff)); |
|
1710 | 2 | $objDrawing->setResizeProportional(false); |
|
1711 | |||
1712 | 2 | if ($xfrm) { |
|
1713 | 2 | $objDrawing->setWidth(Drawing::EMUToPixels(self::getArrayItem($xfrm->ext->attributes(), 'cx'))); |
|
1714 | 2 | $objDrawing->setHeight(Drawing::EMUToPixels(self::getArrayItem($xfrm->ext->attributes(), 'cy'))); |
|
1715 | 2 | $objDrawing->setRotation(Drawing::angleToDegrees(self::getArrayItem($xfrm->attributes(), 'rot'))); |
|
1716 | } |
||
1717 | 2 | if ($outerShdw) { |
|
1718 | $shadow = $objDrawing->getShadow(); |
||
1719 | $shadow->setVisible(true); |
||
1720 | $shadow->setBlurRadius(Drawing::EMUTopixels(self::getArrayItem($outerShdw->attributes(), 'blurRad'))); |
||
1721 | $shadow->setDistance(Drawing::EMUTopixels(self::getArrayItem($outerShdw->attributes(), 'dist'))); |
||
1722 | $shadow->setDirection(Drawing::angleToDegrees(self::getArrayItem($outerShdw->attributes(), 'dir'))); |
||
1723 | $shadow->setAlignment((string) self::getArrayItem($outerShdw->attributes(), 'algn')); |
||
1724 | $shadow->getColor()->setRGB(self::getArrayItem($outerShdw->srgbClr->attributes(), 'val')); |
||
1725 | $shadow->setAlpha(self::getArrayItem($outerShdw->srgbClr->alpha->attributes(), 'val') / 1000); |
||
1726 | } |
||
1727 | |||
1728 | 2 | $this->readHyperLinkDrawing($objDrawing, $twoCellAnchor, $hyperlinks); |
|
1729 | |||
1730 | 2 | $objDrawing->setWorksheet($docSheet); |
|
1731 | 2 | } elseif (($this->includeCharts) && ($twoCellAnchor->graphicFrame)) { |
|
1732 | 2 | $fromCoordinate = Coordinate::stringFromColumnIndex(((string) $twoCellAnchor->from->col) + 1) . ($twoCellAnchor->from->row + 1); |
|
1733 | 2 | $fromOffsetX = Drawing::EMUToPixels($twoCellAnchor->from->colOff); |
|
1734 | 2 | $fromOffsetY = Drawing::EMUToPixels($twoCellAnchor->from->rowOff); |
|
1735 | 2 | $toCoordinate = Coordinate::stringFromColumnIndex(((string) $twoCellAnchor->to->col) + 1) . ($twoCellAnchor->to->row + 1); |
|
1736 | 2 | $toOffsetX = Drawing::EMUToPixels($twoCellAnchor->to->colOff); |
|
1737 | 2 | $toOffsetY = Drawing::EMUToPixels($twoCellAnchor->to->rowOff); |
|
1738 | 2 | $graphic = $twoCellAnchor->graphicFrame->children('http://schemas.openxmlformats.org/drawingml/2006/main')->graphic; |
|
1739 | /** @var SimpleXMLElement $chartRef */ |
||
1740 | 2 | $chartRef = $graphic->graphicData->children('http://schemas.openxmlformats.org/drawingml/2006/chart')->chart; |
|
1741 | 2 | $thisChart = (string) $chartRef->attributes('http://schemas.openxmlformats.org/officeDocument/2006/relationships'); |
|
1742 | |||
1743 | 2 | $chartDetails[$docSheet->getTitle() . '!' . $thisChart] = [ |
|
1744 | 2 | 'fromCoordinate' => $fromCoordinate, |
|
1745 | 2 | 'fromOffsetX' => $fromOffsetX, |
|
1746 | 2 | 'fromOffsetY' => $fromOffsetY, |
|
1747 | 2 | 'toCoordinate' => $toCoordinate, |
|
1748 | 2 | 'toOffsetX' => $toOffsetX, |
|
1749 | 2 | 'toOffsetY' => $toOffsetY, |
|
1750 | 7 | 'worksheetTitle' => $docSheet->getTitle(), |
|
1751 | ]; |
||
1752 | } |
||
1753 | } |
||
1754 | } |
||
1755 | } |
||
1756 | |||
1757 | // store original rId of drawing files |
||
1758 | 7 | $unparsedLoadedData['sheets'][$docSheet->getCodeName()]['drawingOriginalIds'] = []; |
|
1759 | 7 | foreach ($relsWorksheet->Relationship as $ele) { |
|
1760 | 7 | if ($ele['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing') { |
|
1761 | 7 | $unparsedLoadedData['sheets'][$docSheet->getCodeName()]['drawingOriginalIds'][(string) $ele['Target']] = (string) $ele['Id']; |
|
1762 | } |
||
1763 | } |
||
1764 | |||
1765 | // unparsed drawing AlternateContent |
||
1766 | 7 | $xmlAltDrawing = simplexml_load_string( |
|
1767 | 7 | $this->securityScanner->scan($this->getFromZipArchive($zip, $fileDrawing)), |
|
1768 | 7 | 'SimpleXMLElement', |
|
1769 | 7 | Settings::getLibXmlLoaderOptions() |
|
1770 | 7 | )->children('http://schemas.openxmlformats.org/markup-compatibility/2006'); |
|
1771 | |||
1772 | 7 | if ($xmlAltDrawing->AlternateContent) { |
|
1773 | 1 | foreach ($xmlAltDrawing->AlternateContent as $alternateContent) { |
|
1774 | 1 | $unparsedLoadedData['sheets'][$docSheet->getCodeName()]['drawingAlternateContents'][] = $alternateContent->asXML(); |
|
1775 | } |
||
1776 | } |
||
1777 | } |
||
1778 | } |
||
1779 | |||
1780 | 38 | $this->readFormControlProperties($excel, $zip, $dir, $fileWorksheet, $docSheet, $unparsedLoadedData); |
|
1781 | 38 | $this->readPrinterSettings($excel, $zip, $dir, $fileWorksheet, $docSheet, $unparsedLoadedData); |
|
1782 | |||
1783 | // Loop through definedNames |
||
1784 | 38 | if ($xmlWorkbook->definedNames) { |
|
1785 | 30 | foreach ($xmlWorkbook->definedNames->definedName as $definedName) { |
|
1786 | // Extract range |
||
1787 | 2 | $extractedRange = (string) $definedName; |
|
1788 | 2 | if (($spos = strpos($extractedRange, '!')) !== false) { |
|
1789 | 2 | $extractedRange = substr($extractedRange, 0, $spos) . str_replace('$', '', substr($extractedRange, $spos)); |
|
1790 | } else { |
||
1791 | $extractedRange = str_replace('$', '', $extractedRange); |
||
1792 | } |
||
1793 | |||
1794 | // Valid range? |
||
1795 | 2 | if (stripos((string) $definedName, '#REF!') !== false || $extractedRange == '') { |
|
1796 | continue; |
||
1797 | } |
||
1798 | |||
1799 | // Some definedNames are only applicable if we are on the same sheet... |
||
1800 | 2 | if ((string) $definedName['localSheetId'] != '' && (string) $definedName['localSheetId'] == $oldSheetId) { |
|
1801 | // Switch on type |
||
1802 | 2 | switch ((string) $definedName['name']) { |
|
1803 | 2 | case '_xlnm._FilterDatabase': |
|
1804 | if ((string) $definedName['hidden'] !== '1') { |
||
1805 | $extractedRange = explode(',', $extractedRange); |
||
1806 | foreach ($extractedRange as $range) { |
||
1807 | $autoFilterRange = $range; |
||
1808 | if (strpos($autoFilterRange, ':') !== false) { |
||
1809 | $docSheet->getAutoFilter()->setRange($autoFilterRange); |
||
1810 | } |
||
1811 | } |
||
1812 | } |
||
1813 | |||
1814 | break; |
||
1815 | 2 | case '_xlnm.Print_Titles': |
|
1816 | // Split $extractedRange |
||
1817 | 1 | $extractedRange = explode(',', $extractedRange); |
|
1818 | |||
1819 | // Set print titles |
||
1820 | 1 | foreach ($extractedRange as $range) { |
|
1821 | 1 | $matches = []; |
|
1822 | 1 | $range = str_replace('$', '', $range); |
|
1823 | |||
1824 | // check for repeating columns, e g. 'A:A' or 'A:D' |
||
1825 | 1 | if (preg_match('/!?([A-Z]+)\:([A-Z]+)$/', $range, $matches)) { |
|
1826 | $docSheet->getPageSetup()->setColumnsToRepeatAtLeft([$matches[1], $matches[2]]); |
||
1827 | 1 | } elseif (preg_match('/!?(\d+)\:(\d+)$/', $range, $matches)) { |
|
1828 | // check for repeating rows, e.g. '1:1' or '1:5' |
||
1829 | 1 | $docSheet->getPageSetup()->setRowsToRepeatAtTop([$matches[1], $matches[2]]); |
|
1830 | } |
||
1831 | } |
||
1832 | |||
1833 | 1 | break; |
|
1834 | 1 | case '_xlnm.Print_Area': |
|
1835 | 1 | $rangeSets = preg_split("/('?(?:.*?)'?(?:![A-Z0-9]+:[A-Z0-9]+)),?/", $extractedRange, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); |
|
1836 | 1 | $newRangeSets = []; |
|
1837 | 1 | foreach ($rangeSets as $rangeSet) { |
|
1838 | 1 | list($sheetName, $rangeSet) = Worksheet::extractSheetTitle($rangeSet, true); |
|
1839 | 1 | if (strpos($rangeSet, ':') === false) { |
|
1840 | $rangeSet = $rangeSet . ':' . $rangeSet; |
||
1841 | } |
||
1842 | 1 | $newRangeSets[] = str_replace('$', '', $rangeSet); |
|
1843 | } |
||
1844 | 1 | $docSheet->getPageSetup()->setPrintArea(implode(',', $newRangeSets)); |
|
1845 | |||
1846 | 1 | break; |
|
1847 | default: |
||
1848 | 2 | break; |
|
1849 | } |
||
1850 | } |
||
1851 | } |
||
1852 | } |
||
1853 | |||
1854 | // Next sheet id |
||
1855 | 38 | ++$sheetId; |
|
1856 | } |
||
1857 | |||
1858 | // Loop through definedNames |
||
1859 | 38 | if ($xmlWorkbook->definedNames) { |
|
1860 | 30 | foreach ($xmlWorkbook->definedNames->definedName as $definedName) { |
|
1861 | // Extract range |
||
1862 | 2 | $extractedRange = (string) $definedName; |
|
1863 | 2 | if (($spos = strpos($extractedRange, '!')) !== false) { |
|
1864 | 2 | $extractedRange = substr($extractedRange, 0, $spos) . str_replace('$', '', substr($extractedRange, $spos)); |
|
1865 | } else { |
||
1866 | $extractedRange = str_replace('$', '', $extractedRange); |
||
1867 | } |
||
1868 | |||
1869 | // Valid range? |
||
1870 | 2 | if (stripos((string) $definedName, '#REF!') !== false || $extractedRange == '') { |
|
1871 | continue; |
||
1872 | } |
||
1873 | |||
1874 | // Some definedNames are only applicable if we are on the same sheet... |
||
1875 | 2 | if ((string) $definedName['localSheetId'] != '') { |
|
1876 | // Local defined name |
||
1877 | // Switch on type |
||
1878 | 2 | switch ((string) $definedName['name']) { |
|
1879 | 2 | case '_xlnm._FilterDatabase': |
|
1880 | 2 | case '_xlnm.Print_Titles': |
|
1881 | 1 | case '_xlnm.Print_Area': |
|
1882 | 2 | break; |
|
1883 | default: |
||
1884 | if ($mapSheetId[(int) $definedName['localSheetId']] !== null) { |
||
1885 | if (strpos((string) $definedName, '!') !== false) { |
||
1886 | $range = Worksheet::extractSheetTitle((string) $definedName, true); |
||
1887 | $range[0] = str_replace("''", "'", $range[0]); |
||
1888 | $range[0] = str_replace("'", '', $range[0]); |
||
1889 | if ($worksheet = $docSheet->getParent()->getSheetByName($range[0])) { |
||
1890 | $extractedRange = str_replace('$', '', $range[1]); |
||
1891 | $scope = $docSheet->getParent()->getSheet($mapSheetId[(int) $definedName['localSheetId']]); |
||
1892 | $excel->addNamedRange(new NamedRange((string) $definedName['name'], $worksheet, $extractedRange, true, $scope)); |
||
1893 | } |
||
1894 | } |
||
1895 | } |
||
1896 | |||
1897 | 2 | break; |
|
1898 | } |
||
1899 | } elseif (!isset($definedName['localSheetId'])) { |
||
1900 | // "Global" definedNames |
||
1901 | $locatedSheet = null; |
||
1902 | $extractedSheetName = ''; |
||
1903 | if (strpos((string) $definedName, '!') !== false) { |
||
1904 | // Extract sheet name |
||
1905 | $extractedSheetName = Worksheet::extractSheetTitle((string) $definedName, true); |
||
1906 | $extractedSheetName = $extractedSheetName[0]; |
||
1907 | |||
1908 | // Locate sheet |
||
1909 | $locatedSheet = $excel->getSheetByName($extractedSheetName); |
||
1910 | |||
1911 | // Modify range |
||
1912 | list($worksheetName, $extractedRange) = Worksheet::extractSheetTitle($extractedRange, true); |
||
1913 | } |
||
1914 | |||
1915 | if ($locatedSheet !== null) { |
||
1916 | 2 | $excel->addNamedRange(new NamedRange((string) $definedName['name'], $locatedSheet, $extractedRange, false)); |
|
1917 | } |
||
1918 | } |
||
1919 | } |
||
1920 | } |
||
1921 | } |
||
1922 | |||
1923 | 38 | if ((!$this->readDataOnly) || (!empty($this->loadSheetsOnly))) { |
|
1924 | 38 | $workbookView = $xmlWorkbook->bookViews->workbookView; |
|
1925 | |||
1926 | // active sheet index |
||
1927 | 38 | $activeTab = (int) ($workbookView['activeTab']); // refers to old sheet index |
|
1928 | |||
1929 | // keep active sheet index if sheet is still loaded, else first sheet is set as the active |
||
1930 | 38 | if (isset($mapSheetId[$activeTab]) && $mapSheetId[$activeTab] !== null) { |
|
1931 | 38 | $excel->setActiveSheetIndex($mapSheetId[$activeTab]); |
|
1932 | } else { |
||
1933 | if ($excel->getSheetCount() == 0) { |
||
1934 | $excel->createSheet(); |
||
1935 | } |
||
1936 | $excel->setActiveSheetIndex(0); |
||
1937 | } |
||
1938 | |||
1939 | 38 | if (isset($workbookView['showHorizontalScroll'])) { |
|
1940 | 30 | $showHorizontalScroll = (string) $workbookView['showHorizontalScroll']; |
|
1941 | 30 | $excel->setShowHorizontalScroll($this->castXsdBooleanToBool($showHorizontalScroll)); |
|
1942 | } |
||
1943 | |||
1944 | 38 | if (isset($workbookView['showVerticalScroll'])) { |
|
1945 | 30 | $showVerticalScroll = (string) $workbookView['showVerticalScroll']; |
|
1946 | 30 | $excel->setShowVerticalScroll($this->castXsdBooleanToBool($showVerticalScroll)); |
|
1947 | } |
||
1948 | |||
1949 | 38 | if (isset($workbookView['showSheetTabs'])) { |
|
1950 | 30 | $showSheetTabs = (string) $workbookView['showSheetTabs']; |
|
1951 | 30 | $excel->setShowSheetTabs($this->castXsdBooleanToBool($showSheetTabs)); |
|
1952 | } |
||
1953 | |||
1954 | 38 | if (isset($workbookView['minimized'])) { |
|
1955 | 30 | $minimized = (string) $workbookView['minimized']; |
|
1956 | 30 | $excel->setMinimized($this->castXsdBooleanToBool($minimized)); |
|
1957 | } |
||
1958 | |||
1959 | 38 | if (isset($workbookView['autoFilterDateGrouping'])) { |
|
1960 | 30 | $autoFilterDateGrouping = (string) $workbookView['autoFilterDateGrouping']; |
|
1961 | 30 | $excel->setAutoFilterDateGrouping($this->castXsdBooleanToBool($autoFilterDateGrouping)); |
|
1962 | } |
||
1963 | |||
1964 | 38 | if (isset($workbookView['firstSheet'])) { |
|
1965 | 30 | $firstSheet = (string) $workbookView['firstSheet']; |
|
1966 | 30 | $excel->setFirstSheetIndex((int) $firstSheet); |
|
1967 | } |
||
1968 | |||
1969 | 38 | if (isset($workbookView['visibility'])) { |
|
1970 | 30 | $visibility = (string) $workbookView['visibility']; |
|
1971 | 30 | $excel->setVisibility($visibility); |
|
1972 | } |
||
1973 | |||
1974 | 38 | if (isset($workbookView['tabRatio'])) { |
|
1975 | 30 | $tabRatio = (string) $workbookView['tabRatio']; |
|
1976 | 30 | $excel->setTabRatio((int) $tabRatio); |
|
1977 | } |
||
1978 | } |
||
1979 | |||
1980 | 38 | break; |
|
1981 | } |
||
1982 | } |
||
1983 | |||
1984 | 38 | if (!$this->readDataOnly) { |
|
1985 | 38 | $contentTypes = simplexml_load_string( |
|
1986 | 38 | $this->securityScanner->scan( |
|
1987 | 38 | $this->getFromZipArchive($zip, '[Content_Types].xml') |
|
1988 | ), |
||
1989 | 38 | 'SimpleXMLElement', |
|
1990 | 38 | Settings::getLibXmlLoaderOptions() |
|
1991 | ); |
||
1992 | |||
1993 | // Default content types |
||
1994 | 38 | foreach ($contentTypes->Default as $contentType) { |
|
1995 | 38 | switch ($contentType['ContentType']) { |
|
1996 | 38 | case 'application/vnd.openxmlformats-officedocument.spreadsheetml.printerSettings': |
|
1997 | 8 | $unparsedLoadedData['default_content_types'][(string) $contentType['Extension']] = (string) $contentType['ContentType']; |
|
1998 | |||
1999 | 38 | break; |
|
2000 | } |
||
2001 | } |
||
2002 | |||
2003 | // Override content types |
||
2004 | 38 | foreach ($contentTypes->Override as $contentType) { |
|
2005 | 38 | switch ($contentType['ContentType']) { |
|
2006 | 38 | case 'application/vnd.openxmlformats-officedocument.drawingml.chart+xml': |
|
2007 | 2 | if ($this->includeCharts) { |
|
2008 | 2 | $chartEntryRef = ltrim($contentType['PartName'], '/'); |
|
2009 | 2 | $chartElements = simplexml_load_string( |
|
2010 | 2 | $this->securityScanner->scan( |
|
2011 | 2 | $this->getFromZipArchive($zip, $chartEntryRef) |
|
2012 | ), |
||
2013 | 2 | 'SimpleXMLElement', |
|
2014 | 2 | Settings::getLibXmlLoaderOptions() |
|
2015 | ); |
||
2016 | 2 | $objChart = Chart::readChart($chartElements, basename($chartEntryRef, '.xml')); |
|
1 ignored issue
–
show
|
|||
2017 | |||
2018 | 2 | if (isset($charts[$chartEntryRef])) { |
|
2019 | 2 | $chartPositionRef = $charts[$chartEntryRef]['sheet'] . '!' . $charts[$chartEntryRef]['id']; |
|
2020 | 2 | if (isset($chartDetails[$chartPositionRef])) { |
|
2021 | 2 | $excel->getSheetByName($charts[$chartEntryRef]['sheet'])->addChart($objChart); |
|
2022 | 2 | $objChart->setWorksheet($excel->getSheetByName($charts[$chartEntryRef]['sheet'])); |
|
2023 | 2 | $objChart->setTopLeftPosition($chartDetails[$chartPositionRef]['fromCoordinate'], $chartDetails[$chartPositionRef]['fromOffsetX'], $chartDetails[$chartPositionRef]['fromOffsetY']); |
|
2024 | 2 | $objChart->setBottomRightPosition($chartDetails[$chartPositionRef]['toCoordinate'], $chartDetails[$chartPositionRef]['toOffsetX'], $chartDetails[$chartPositionRef]['toOffsetY']); |
|
2025 | } |
||
2026 | } |
||
2027 | } |
||
2028 | |||
2029 | 2 | break; |
|
2030 | |||
2031 | // unparsed |
||
2032 | 38 | case 'application/vnd.ms-excel.controlproperties+xml': |
|
2033 | 1 | $unparsedLoadedData['override_content_types'][(string) $contentType['PartName']] = (string) $contentType['ContentType']; |
|
2034 | |||
2035 | 38 | break; |
|
2036 | } |
||
2037 | } |
||
2038 | } |
||
2039 | |||
2040 | 38 | $excel->setUnparsedLoadedData($unparsedLoadedData); |
|
2041 | |||
2042 | 38 | $zip->close(); |
|
2043 | |||
2044 | 38 | return $excel; |
|
2045 | } |
||
2046 | |||
2047 | 38 | private static function readColor($color, $background = false) |
|
2048 | { |
||
2049 | 38 | if (isset($color['rgb'])) { |
|
2050 | 33 | return (string) $color['rgb']; |
|
2051 | 10 | } elseif (isset($color['indexed'])) { |
|
2052 | 7 | return Color::indexedColor($color['indexed'] - 7, $background)->getARGB(); |
|
2053 | 7 | } elseif (isset($color['theme'])) { |
|
2054 | 6 | if (self::$theme !== null) { |
|
2055 | 6 | $returnColour = self::$theme->getColourByIndex((int) $color['theme']); |
|
2056 | 6 | if (isset($color['tint'])) { |
|
2057 | 1 | $tintAdjust = (float) $color['tint']; |
|
2058 | 1 | $returnColour = Color::changeBrightness($returnColour, $tintAdjust); |
|
2059 | } |
||
2060 | |||
2061 | 6 | return 'FF' . $returnColour; |
|
2062 | } |
||
2063 | } |
||
2064 | |||
2065 | 2 | if ($background) { |
|
2066 | return 'FFFFFFFF'; |
||
2067 | } |
||
2068 | |||
2069 | 2 | return 'FF000000'; |
|
2070 | } |
||
2071 | |||
2072 | /** |
||
2073 | * @param Style $docStyle |
||
2074 | * @param SimpleXMLElement|\stdClass $style |
||
2075 | */ |
||
2076 | 38 | private static function readStyle(Style $docStyle, $style) |
|
2077 | { |
||
2078 | 38 | $docStyle->getNumberFormat()->setFormatCode($style->numFmt); |
|
2079 | |||
2080 | // font |
||
2081 | 38 | if (isset($style->font)) { |
|
2082 | 38 | $docStyle->getFont()->setName((string) $style->font->name['val']); |
|
2083 | 38 | $docStyle->getFont()->setSize((string) $style->font->sz['val']); |
|
2084 | 38 | if (isset($style->font->b)) { |
|
2085 | 34 | $docStyle->getFont()->setBold(!isset($style->font->b['val']) || self::boolean((string) $style->font->b['val'])); |
|
2086 | } |
||
2087 | 38 | if (isset($style->font->i)) { |
|
2088 | 31 | $docStyle->getFont()->setItalic(!isset($style->font->i['val']) || self::boolean((string) $style->font->i['val'])); |
|
2089 | } |
||
2090 | 38 | if (isset($style->font->strike)) { |
|
2091 | 30 | $docStyle->getFont()->setStrikethrough(!isset($style->font->strike['val']) || self::boolean((string) $style->font->strike['val'])); |
|
2092 | } |
||
2093 | 38 | $docStyle->getFont()->getColor()->setARGB(self::readColor($style->font->color)); |
|
2094 | |||
2095 | 38 | if (isset($style->font->u) && !isset($style->font->u['val'])) { |
|
2096 | $docStyle->getFont()->setUnderline(\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_SINGLE); |
||
2097 | 38 | } elseif (isset($style->font->u, $style->font->u['val'])) { |
|
2098 | 30 | $docStyle->getFont()->setUnderline((string) $style->font->u['val']); |
|
2099 | } |
||
2100 | |||
2101 | 38 | if (isset($style->font->vertAlign, $style->font->vertAlign['val'])) { |
|
2102 | $vertAlign = strtolower((string) $style->font->vertAlign['val']); |
||
2103 | if ($vertAlign == 'superscript') { |
||
2104 | $docStyle->getFont()->setSuperscript(true); |
||
2105 | } |
||
2106 | if ($vertAlign == 'subscript') { |
||
2107 | $docStyle->getFont()->setSubscript(true); |
||
2108 | } |
||
2109 | } |
||
2110 | } |
||
2111 | |||
2112 | // fill |
||
2113 | 38 | if (isset($style->fill)) { |
|
2114 | 38 | if ($style->fill->gradientFill) { |
|
2115 | /** @var SimpleXMLElement $gradientFill */ |
||
2116 | 2 | $gradientFill = $style->fill->gradientFill[0]; |
|
2117 | 2 | if (!empty($gradientFill['type'])) { |
|
2118 | 2 | $docStyle->getFill()->setFillType((string) $gradientFill['type']); |
|
2119 | } |
||
2120 | 2 | $docStyle->getFill()->setRotation((float) ($gradientFill['degree'])); |
|
2121 | 2 | $gradientFill->registerXPathNamespace('sml', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main'); |
|
2122 | 2 | $docStyle->getFill()->getStartColor()->setARGB(self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=0]'))->color)); |
|
2123 | 2 | $docStyle->getFill()->getEndColor()->setARGB(self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=1]'))->color)); |
|
2124 | 38 | } elseif ($style->fill->patternFill) { |
|
2125 | 38 | $patternType = (string) $style->fill->patternFill['patternType'] != '' ? (string) $style->fill->patternFill['patternType'] : 'solid'; |
|
2126 | 38 | $docStyle->getFill()->setFillType($patternType); |
|
2127 | 38 | if ($style->fill->patternFill->fgColor) { |
|
2128 | 4 | $docStyle->getFill()->getStartColor()->setARGB(self::readColor($style->fill->patternFill->fgColor, true)); |
|
2129 | } else { |
||
2130 | 38 | $docStyle->getFill()->getStartColor()->setARGB('FF000000'); |
|
2131 | } |
||
2132 | 38 | if ($style->fill->patternFill->bgColor) { |
|
2133 | 5 | $docStyle->getFill()->getEndColor()->setARGB(self::readColor($style->fill->patternFill->bgColor, true)); |
|
2134 | } |
||
2135 | } |
||
2136 | } |
||
2137 | |||
2138 | // border |
||
2139 | 38 | if (isset($style->border)) { |
|
2140 | 38 | $diagonalUp = self::boolean((string) $style->border['diagonalUp']); |
|
2141 | 38 | $diagonalDown = self::boolean((string) $style->border['diagonalDown']); |
|
2142 | 38 | if (!$diagonalUp && !$diagonalDown) { |
|
2143 | 38 | $docStyle->getBorders()->setDiagonalDirection(Borders::DIAGONAL_NONE); |
|
2144 | } elseif ($diagonalUp && !$diagonalDown) { |
||
2145 | $docStyle->getBorders()->setDiagonalDirection(Borders::DIAGONAL_UP); |
||
2146 | } elseif (!$diagonalUp && $diagonalDown) { |
||
2147 | $docStyle->getBorders()->setDiagonalDirection(Borders::DIAGONAL_DOWN); |
||
2148 | } else { |
||
2149 | $docStyle->getBorders()->setDiagonalDirection(Borders::DIAGONAL_BOTH); |
||
2150 | } |
||
2151 | 38 | self::readBorder($docStyle->getBorders()->getLeft(), $style->border->left); |
|
2152 | 38 | self::readBorder($docStyle->getBorders()->getRight(), $style->border->right); |
|
2153 | 38 | self::readBorder($docStyle->getBorders()->getTop(), $style->border->top); |
|
2154 | 38 | self::readBorder($docStyle->getBorders()->getBottom(), $style->border->bottom); |
|
2155 | 38 | self::readBorder($docStyle->getBorders()->getDiagonal(), $style->border->diagonal); |
|
2156 | } |
||
2157 | |||
2158 | // alignment |
||
2159 | 38 | if (isset($style->alignment)) { |
|
2160 | 38 | $docStyle->getAlignment()->setHorizontal((string) $style->alignment['horizontal']); |
|
2161 | 38 | $docStyle->getAlignment()->setVertical((string) $style->alignment['vertical']); |
|
2162 | |||
2163 | 38 | $textRotation = 0; |
|
2164 | 38 | if ((int) $style->alignment['textRotation'] <= 90) { |
|
2165 | 38 | $textRotation = (int) $style->alignment['textRotation']; |
|
2166 | } elseif ((int) $style->alignment['textRotation'] > 90) { |
||
2167 | $textRotation = 90 - (int) $style->alignment['textRotation']; |
||
2168 | } |
||
2169 | |||
2170 | 38 | $docStyle->getAlignment()->setTextRotation((int) $textRotation); |
|
2171 | 38 | $docStyle->getAlignment()->setWrapText(self::boolean((string) $style->alignment['wrapText'])); |
|
2172 | 38 | $docStyle->getAlignment()->setShrinkToFit(self::boolean((string) $style->alignment['shrinkToFit'])); |
|
2173 | 38 | $docStyle->getAlignment()->setIndent((int) ((string) $style->alignment['indent']) > 0 ? (int) ((string) $style->alignment['indent']) : 0); |
|
2174 | 38 | $docStyle->getAlignment()->setReadOrder((int) ((string) $style->alignment['readingOrder']) > 0 ? (int) ((string) $style->alignment['readingOrder']) : 0); |
|
2175 | } |
||
2176 | |||
2177 | // protection |
||
2178 | 38 | if (isset($style->protection)) { |
|
2179 | 38 | if (isset($style->protection['locked'])) { |
|
2180 | 2 | if (self::boolean((string) $style->protection['locked'])) { |
|
2181 | $docStyle->getProtection()->setLocked(Protection::PROTECTION_PROTECTED); |
||
2182 | } else { |
||
2183 | 2 | $docStyle->getProtection()->setLocked(Protection::PROTECTION_UNPROTECTED); |
|
2184 | } |
||
2185 | } |
||
2186 | |||
2187 | 38 | if (isset($style->protection['hidden'])) { |
|
2188 | if (self::boolean((string) $style->protection['hidden'])) { |
||
2189 | $docStyle->getProtection()->setHidden(Protection::PROTECTION_PROTECTED); |
||
2190 | } else { |
||
2191 | $docStyle->getProtection()->setHidden(Protection::PROTECTION_UNPROTECTED); |
||
2192 | } |
||
2193 | } |
||
2194 | } |
||
2195 | |||
2196 | // top-level style settings |
||
2197 | 38 | if (isset($style->quotePrefix)) { |
|
2198 | 38 | $docStyle->setQuotePrefix($style->quotePrefix); |
|
2199 | } |
||
2200 | 38 | } |
|
2201 | |||
2202 | /** |
||
2203 | * @param Border $docBorder |
||
2204 | * @param SimpleXMLElement $eleBorder |
||
2205 | */ |
||
2206 | 38 | private static function readBorder(Border $docBorder, $eleBorder) |
|
2207 | { |
||
2208 | 38 | if (isset($eleBorder['style'])) { |
|
2209 | 3 | $docBorder->setBorderStyle((string) $eleBorder['style']); |
|
2210 | } |
||
2211 | 38 | if (isset($eleBorder->color)) { |
|
2212 | 3 | $docBorder->getColor()->setARGB(self::readColor($eleBorder->color)); |
|
2213 | } |
||
2214 | 38 | } |
|
2215 | |||
2216 | /** |
||
2217 | * @param SimpleXMLElement | null $is |
||
2218 | * |
||
2219 | * @return RichText |
||
2220 | */ |
||
2221 | 4 | private function parseRichText($is) |
|
2222 | { |
||
2223 | 4 | $value = new RichText(); |
|
2224 | |||
2225 | 4 | if (isset($is->t)) { |
|
2226 | $value->createText(StringHelper::controlCharacterOOXML2PHP((string) $is->t)); |
||
2227 | } else { |
||
2228 | 4 | if (is_object($is->r)) { |
|
2229 | 4 | foreach ($is->r as $run) { |
|
2230 | 4 | if (!isset($run->rPr)) { |
|
2231 | 4 | $value->createText(StringHelper::controlCharacterOOXML2PHP((string) $run->t)); |
|
2232 | } else { |
||
2233 | 3 | $objText = $value->createTextRun(StringHelper::controlCharacterOOXML2PHP((string) $run->t)); |
|
2234 | |||
2235 | 3 | if (isset($run->rPr->rFont['val'])) { |
|
2236 | 3 | $objText->getFont()->setName((string) $run->rPr->rFont['val']); |
|
2237 | } |
||
2238 | 3 | if (isset($run->rPr->sz['val'])) { |
|
2239 | 3 | $objText->getFont()->setSize((float) $run->rPr->sz['val']); |
|
2240 | } |
||
2241 | 3 | if (isset($run->rPr->color)) { |
|
2242 | 3 | $objText->getFont()->setColor(new Color(self::readColor($run->rPr->color))); |
|
2243 | } |
||
2244 | 3 | if ((isset($run->rPr->b['val']) && self::boolean((string) $run->rPr->b['val'])) || |
|
2245 | 3 | (isset($run->rPr->b) && !isset($run->rPr->b['val']))) { |
|
2246 | 3 | $objText->getFont()->setBold(true); |
|
2247 | } |
||
2248 | 3 | if ((isset($run->rPr->i['val']) && self::boolean((string) $run->rPr->i['val'])) || |
|
2249 | 3 | (isset($run->rPr->i) && !isset($run->rPr->i['val']))) { |
|
2250 | 2 | $objText->getFont()->setItalic(true); |
|
2251 | } |
||
2252 | 3 | if (isset($run->rPr->vertAlign, $run->rPr->vertAlign['val'])) { |
|
2253 | $vertAlign = strtolower((string) $run->rPr->vertAlign['val']); |
||
2254 | if ($vertAlign == 'superscript') { |
||
2255 | $objText->getFont()->setSuperscript(true); |
||
2256 | } |
||
2257 | if ($vertAlign == 'subscript') { |
||
2258 | $objText->getFont()->setSubscript(true); |
||
2259 | } |
||
2260 | } |
||
2261 | 3 | if (isset($run->rPr->u) && !isset($run->rPr->u['val'])) { |
|
2262 | $objText->getFont()->setUnderline(\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_SINGLE); |
||
2263 | 3 | } elseif (isset($run->rPr->u, $run->rPr->u['val'])) { |
|
2264 | 2 | $objText->getFont()->setUnderline((string) $run->rPr->u['val']); |
|
2265 | } |
||
2266 | 3 | if ((isset($run->rPr->strike['val']) && self::boolean((string) $run->rPr->strike['val'])) || |
|
2267 | 3 | (isset($run->rPr->strike) && !isset($run->rPr->strike['val']))) { |
|
2268 | 4 | $objText->getFont()->setStrikethrough(true); |
|
2269 | } |
||
2270 | } |
||
2271 | } |
||
2272 | } |
||
2273 | } |
||
2274 | |||
2275 | 4 | return $value; |
|
2276 | } |
||
2277 | |||
2278 | /** |
||
2279 | * @param Spreadsheet $excel |
||
2280 | * @param mixed $customUITarget |
||
2281 | * @param mixed $zip |
||
2282 | */ |
||
2283 | private function readRibbon(Spreadsheet $excel, $customUITarget, $zip) |
||
2284 | { |
||
2285 | $baseDir = dirname($customUITarget); |
||
2286 | $nameCustomUI = basename($customUITarget); |
||
2287 | // get the xml file (ribbon) |
||
2288 | $localRibbon = $this->getFromZipArchive($zip, $customUITarget); |
||
2289 | $customUIImagesNames = []; |
||
2290 | $customUIImagesBinaries = []; |
||
2291 | // something like customUI/_rels/customUI.xml.rels |
||
2292 | $pathRels = $baseDir . '/_rels/' . $nameCustomUI . '.rels'; |
||
2293 | $dataRels = $this->getFromZipArchive($zip, $pathRels); |
||
2294 | if ($dataRels) { |
||
2295 | // exists and not empty if the ribbon have some pictures (other than internal MSO) |
||
2296 | $UIRels = simplexml_load_string( |
||
2297 | $this->securityScanner->scan($dataRels), |
||
2298 | 'SimpleXMLElement', |
||
2299 | Settings::getLibXmlLoaderOptions() |
||
2300 | ); |
||
2301 | if (false !== $UIRels) { |
||
2302 | // we need to save id and target to avoid parsing customUI.xml and "guess" if it's a pseudo callback who load the image |
||
2303 | foreach ($UIRels->Relationship as $ele) { |
||
2304 | if ($ele['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image') { |
||
2305 | // an image ? |
||
2306 | $customUIImagesNames[(string) $ele['Id']] = (string) $ele['Target']; |
||
2307 | $customUIImagesBinaries[(string) $ele['Target']] = $this->getFromZipArchive($zip, $baseDir . '/' . (string) $ele['Target']); |
||
2308 | } |
||
2309 | } |
||
2310 | } |
||
2311 | } |
||
2312 | if ($localRibbon) { |
||
2313 | $excel->setRibbonXMLData($customUITarget, $localRibbon); |
||
2314 | if (count($customUIImagesNames) > 0 && count($customUIImagesBinaries) > 0) { |
||
2315 | $excel->setRibbonBinObjects($customUIImagesNames, $customUIImagesBinaries); |
||
2316 | } else { |
||
2317 | $excel->setRibbonBinObjects(null, null); |
||
2318 | } |
||
2319 | } else { |
||
2320 | $excel->setRibbonXMLData(null, null); |
||
2321 | $excel->setRibbonBinObjects(null, null); |
||
2322 | } |
||
2323 | } |
||
2324 | |||
2325 | 39 | private static function getArrayItem($array, $key = 0) |
|
2326 | { |
||
2327 | 39 | return isset($array[$key]) ? $array[$key] : null; |
|
2328 | } |
||
2329 | |||
2330 | 11 | private static function dirAdd($base, $add) |
|
2331 | { |
||
2332 | 11 | return preg_replace('~[^/]+/\.\./~', '', dirname($base) . "/$add"); |
|
2333 | } |
||
2334 | |||
2335 | private static function toCSSArray($style) |
||
2336 | { |
||
2337 | $style = trim(str_replace(["\r", "\n"], '', $style), ';'); |
||
2338 | |||
2339 | $temp = explode(';', $style); |
||
2340 | $style = []; |
||
2341 | foreach ($temp as $item) { |
||
2342 | $item = explode(':', $item); |
||
2343 | |||
2344 | if (strpos($item[1], 'px') !== false) { |
||
2345 | $item[1] = str_replace('px', '', $item[1]); |
||
2346 | } |
||
2347 | if (strpos($item[1], 'pt') !== false) { |
||
2348 | $item[1] = str_replace('pt', '', $item[1]); |
||
2349 | $item[1] = Font::fontSizeToPixels($item[1]); |
||
2350 | } |
||
2351 | if (strpos($item[1], 'in') !== false) { |
||
2352 | $item[1] = str_replace('in', '', $item[1]); |
||
2353 | $item[1] = Font::inchSizeToPixels($item[1]); |
||
2354 | } |
||
2355 | if (strpos($item[1], 'cm') !== false) { |
||
2356 | $item[1] = str_replace('cm', '', $item[1]); |
||
2357 | $item[1] = Font::centimeterSizeToPixels($item[1]); |
||
2358 | } |
||
2359 | |||
2360 | $style[$item[0]] = $item[1]; |
||
2361 | } |
||
2362 | |||
2363 | return $style; |
||
2364 | } |
||
2365 | |||
2366 | 38 | private static function boolean($value) |
|
2376 | } |
||
2377 | |||
2378 | /** |
||
2379 | * @param \PhpOffice\PhpSpreadsheet\Worksheet\Drawing $objDrawing |
||
2380 | * @param \SimpleXMLElement $cellAnchor |
||
2381 | * @param array $hyperlinks |
||
2382 | */ |
||
2383 | 6 | private function readHyperLinkDrawing($objDrawing, $cellAnchor, $hyperlinks) |
|
2384 | { |
||
2385 | 6 | $hlinkClick = $cellAnchor->pic->nvPicPr->cNvPr->children('http://schemas.openxmlformats.org/drawingml/2006/main')->hlinkClick; |
|
2386 | |||
2387 | 6 | if ($hlinkClick->count() === 0) { |
|
2388 | 4 | return; |
|
2389 | } |
||
2390 | |||
2391 | 2 | $hlinkId = (string) $hlinkClick->attributes('http://schemas.openxmlformats.org/officeDocument/2006/relationships')['id']; |
|
2392 | 2 | $hyperlink = new Hyperlink( |
|
2393 | 2 | $hyperlinks[$hlinkId], |
|
2394 | 2 | (string) self::getArrayItem($cellAnchor->pic->nvPicPr->cNvPr->attributes(), 'name') |
|
2395 | ); |
||
2396 | 2 | $objDrawing->setHyperlink($hyperlink); |
|
2397 | 2 | } |
|
2398 | |||
2399 | 38 | private function readProtection(Spreadsheet $excel, SimpleXMLElement $xmlWorkbook) |
|
2400 | { |
||
2401 | 38 | if (!$xmlWorkbook->workbookProtection) { |
|
2402 | 37 | return; |
|
2403 | } |
||
2404 | |||
2405 | 1 | if ($xmlWorkbook->workbookProtection['lockRevision']) { |
|
2406 | $excel->getSecurity()->setLockRevision((bool) $xmlWorkbook->workbookProtection['lockRevision']); |
||
2407 | } |
||
2408 | |||
2409 | 1 | if ($xmlWorkbook->workbookProtection['lockStructure']) { |
|
2410 | 1 | $excel->getSecurity()->setLockStructure((bool) $xmlWorkbook->workbookProtection['lockStructure']); |
|
2411 | } |
||
2412 | |||
2413 | 1 | if ($xmlWorkbook->workbookProtection['lockWindows']) { |
|
2414 | $excel->getSecurity()->setLockWindows((bool) $xmlWorkbook->workbookProtection['lockWindows']); |
||
2415 | } |
||
2416 | |||
2417 | 1 | if ($xmlWorkbook->workbookProtection['revisionsPassword']) { |
|
2418 | $excel->getSecurity()->setRevisionsPassword((string) $xmlWorkbook->workbookProtection['revisionsPassword'], true); |
||
2419 | } |
||
2420 | |||
2421 | 1 | if ($xmlWorkbook->workbookProtection['workbookPassword']) { |
|
2422 | 1 | $excel->getSecurity()->setWorkbookPassword((string) $xmlWorkbook->workbookProtection['workbookPassword'], true); |
|
2423 | } |
||
2424 | 1 | } |
|
2425 | |||
2426 | 38 | private function readFormControlProperties(Spreadsheet $excel, ZipArchive $zip, $dir, $fileWorksheet, $docSheet, array &$unparsedLoadedData) |
|
2427 | { |
||
2428 | 38 | if (!$zip->locateName(dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels')) { |
|
2429 | 4 | return; |
|
2430 | } |
||
2431 | |||
2432 | //~ http://schemas.openxmlformats.org/package/2006/relationships" |
||
2433 | 37 | $relsWorksheet = simplexml_load_string( |
|
2434 | 37 | $this->securityScanner->scan( |
|
2435 | 37 | $this->getFromZipArchive($zip, dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels') |
|
2436 | ), |
||
2437 | 37 | 'SimpleXMLElement', |
|
2438 | 37 | Settings::getLibXmlLoaderOptions() |
|
2439 | ); |
||
2440 | 37 | $ctrlProps = []; |
|
2441 | 37 | foreach ($relsWorksheet->Relationship as $ele) { |
|
2442 | 12 | if ($ele['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/ctrlProp') { |
|
2443 | 12 | $ctrlProps[(string) $ele['Id']] = $ele; |
|
2444 | } |
||
2445 | } |
||
2446 | |||
2447 | 37 | $unparsedCtrlProps = &$unparsedLoadedData['sheets'][$docSheet->getCodeName()]['ctrlProps']; |
|
2448 | 37 | foreach ($ctrlProps as $rId => $ctrlProp) { |
|
2449 | 1 | $rId = substr($rId, 3); // rIdXXX |
|
2450 | 1 | $unparsedCtrlProps[$rId] = []; |
|
2451 | 1 | $unparsedCtrlProps[$rId]['filePath'] = self::dirAdd("$dir/$fileWorksheet", $ctrlProp['Target']); |
|
2452 | 1 | $unparsedCtrlProps[$rId]['relFilePath'] = (string) $ctrlProp['Target']; |
|
2453 | 1 | $unparsedCtrlProps[$rId]['content'] = $this->securityScanner->scan($this->getFromZipArchive($zip, $unparsedCtrlProps[$rId]['filePath'])); |
|
2454 | } |
||
2455 | 37 | unset($unparsedCtrlProps); |
|
2456 | 37 | } |
|
2457 | |||
2458 | 38 | private function readPrinterSettings(Spreadsheet $excel, ZipArchive $zip, $dir, $fileWorksheet, $docSheet, array &$unparsedLoadedData) |
|
2459 | { |
||
2460 | 38 | if (!$zip->locateName(dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels')) { |
|
2461 | 4 | return; |
|
2462 | } |
||
2463 | |||
2464 | //~ http://schemas.openxmlformats.org/package/2006/relationships" |
||
2465 | 37 | $relsWorksheet = simplexml_load_string( |
|
2466 | 37 | $this->securityScanner->scan( |
|
2467 | 37 | $this->getFromZipArchive($zip, dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels') |
|
2468 | ), |
||
2469 | 37 | 'SimpleXMLElement', |
|
2470 | 37 | Settings::getLibXmlLoaderOptions() |
|
2471 | ); |
||
2472 | 37 | $sheetPrinterSettings = []; |
|
2473 | 37 | foreach ($relsWorksheet->Relationship as $ele) { |
|
2474 | 12 | if ($ele['Type'] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/printerSettings') { |
|
2475 | 12 | $sheetPrinterSettings[(string) $ele['Id']] = $ele; |
|
2476 | } |
||
2477 | } |
||
2478 | |||
2479 | 37 | $unparsedPrinterSettings = &$unparsedLoadedData['sheets'][$docSheet->getCodeName()]['printerSettings']; |
|
2480 | 37 | foreach ($sheetPrinterSettings as $rId => $printerSettings) { |
|
2481 | 7 | $rId = substr($rId, 3); // rIdXXX |
|
2482 | 7 | $unparsedPrinterSettings[$rId] = []; |
|
2483 | 7 | $unparsedPrinterSettings[$rId]['filePath'] = self::dirAdd("$dir/$fileWorksheet", $printerSettings['Target']); |
|
2484 | 7 | $unparsedPrinterSettings[$rId]['relFilePath'] = (string) $printerSettings['Target']; |
|
2485 | 7 | $unparsedPrinterSettings[$rId]['content'] = $this->securityScanner->scan($this->getFromZipArchive($zip, $unparsedPrinterSettings[$rId]['filePath'])); |
|
2486 | } |
||
2487 | 37 | unset($unparsedPrinterSettings); |
|
2488 | 37 | } |
|
2489 | |||
2490 | /** |
||
2491 | * Convert an 'xsd:boolean' XML value to a PHP boolean value. |
||
2492 | * A valid 'xsd:boolean' XML value can be one of the following |
||
2493 | * four values: 'true', 'false', '1', '0'. It is case sensitive. |
||
2494 | * |
||
2495 | * Note that just doing '(bool) $xsdBoolean' is not safe, |
||
2496 | * since '(bool) "false"' returns true. |
||
2497 | * |
||
2498 | * @see https://www.w3.org/TR/xmlschema11-2/#boolean |
||
2499 | * |
||
2500 | * @param string $xsdBoolean An XML string value of type 'xsd:boolean' |
||
2501 | * |
||
2502 | * @return bool Boolean value |
||
2503 | */ |
||
2504 | 30 | private function castXsdBooleanToBool($xsdBoolean) |
|
2511 | } |
||
2512 | |||
2513 | /** |
||
2514 | * Read columns and rows attributes from XML and set them on the worksheet. |
||
2515 | * |
||
2516 | * @param SimpleXMLElement $xmlSheet |
||
2517 | * @param Worksheet $docSheet |
||
2518 | */ |
||
2519 | 38 | private function readColumnsAndRowsAttributes(SimpleXMLElement $xmlSheet, Worksheet $docSheet) |
|
2520 | { |
||
2521 | 38 | $columnsAttributes = []; |
|
2522 | 38 | $rowsAttributes = []; |
|
2523 | 38 | if (isset($xmlSheet->cols) && !$this->readDataOnly) { |
|
2524 | 9 | foreach ($xmlSheet->cols->col as $col) { |
|
2525 | 9 | for ($i = (int) ($col['min']); $i <= (int) ($col['max']); ++$i) { |
|
2526 | 9 | if ($col['style'] && !$this->readDataOnly) { |
|
2527 | 4 | $columnsAttributes[Coordinate::stringFromColumnIndex($i)]['xfIndex'] = (int) $col['style']; |
|
2528 | } |
||
2529 | 9 | if (self::boolean($col['hidden'])) { |
|
2530 | 1 | $columnsAttributes[Coordinate::stringFromColumnIndex($i)]['visible'] = false; |
|
2531 | } |
||
2532 | 9 | if (self::boolean($col['collapsed'])) { |
|
2533 | 1 | $columnsAttributes[Coordinate::stringFromColumnIndex($i)]['collapsed'] = true; |
|
2534 | } |
||
2603 |