| 1441 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace PhpOffice\PhpSpreadsheet\Reader\Xlsx;
|
|
|
4 |
|
|
|
5 |
use PhpOffice\PhpSpreadsheet\Document\Properties as DocumentProperties;
|
|
|
6 |
use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner;
|
|
|
7 |
use SimpleXMLElement;
|
|
|
8 |
|
|
|
9 |
class Properties
|
|
|
10 |
{
|
|
|
11 |
private XmlScanner $securityScanner;
|
|
|
12 |
|
|
|
13 |
private DocumentProperties $docProps;
|
|
|
14 |
|
|
|
15 |
public function __construct(XmlScanner $securityScanner, DocumentProperties $docProps)
|
|
|
16 |
{
|
|
|
17 |
$this->securityScanner = $securityScanner;
|
|
|
18 |
$this->docProps = $docProps;
|
|
|
19 |
}
|
|
|
20 |
|
|
|
21 |
private function extractPropertyData(string $propertyData): ?SimpleXMLElement
|
|
|
22 |
{
|
|
|
23 |
// okay to omit namespace because everything will be processed by xpath
|
|
|
24 |
$obj = simplexml_load_string(
|
|
|
25 |
$this->securityScanner->scan($propertyData)
|
|
|
26 |
);
|
|
|
27 |
|
|
|
28 |
return $obj === false ? null : $obj;
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
public function readCoreProperties(string $propertyData): void
|
|
|
32 |
{
|
|
|
33 |
$xmlCore = $this->extractPropertyData($propertyData);
|
|
|
34 |
|
|
|
35 |
if (is_object($xmlCore)) {
|
|
|
36 |
$xmlCore->registerXPathNamespace('dc', Namespaces::DC_ELEMENTS);
|
|
|
37 |
$xmlCore->registerXPathNamespace('dcterms', Namespaces::DC_TERMS);
|
|
|
38 |
$xmlCore->registerXPathNamespace('cp', Namespaces::CORE_PROPERTIES2);
|
|
|
39 |
|
|
|
40 |
$this->docProps->setCreator($this->getArrayItem($xmlCore->xpath('dc:creator')));
|
|
|
41 |
$this->docProps->setLastModifiedBy($this->getArrayItem($xmlCore->xpath('cp:lastModifiedBy')));
|
|
|
42 |
$this->docProps->setCreated($this->getArrayItem($xmlCore->xpath('dcterms:created'))); //! respect xsi:type
|
|
|
43 |
$this->docProps->setModified($this->getArrayItem($xmlCore->xpath('dcterms:modified'))); //! respect xsi:type
|
|
|
44 |
$this->docProps->setTitle($this->getArrayItem($xmlCore->xpath('dc:title')));
|
|
|
45 |
$this->docProps->setDescription($this->getArrayItem($xmlCore->xpath('dc:description')));
|
|
|
46 |
$this->docProps->setSubject($this->getArrayItem($xmlCore->xpath('dc:subject')));
|
|
|
47 |
$this->docProps->setKeywords($this->getArrayItem($xmlCore->xpath('cp:keywords')));
|
|
|
48 |
$this->docProps->setCategory($this->getArrayItem($xmlCore->xpath('cp:category')));
|
|
|
49 |
}
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
public function readExtendedProperties(string $propertyData): void
|
|
|
53 |
{
|
|
|
54 |
$xmlCore = $this->extractPropertyData($propertyData);
|
|
|
55 |
|
|
|
56 |
if (is_object($xmlCore)) {
|
|
|
57 |
if (isset($xmlCore->Company)) {
|
|
|
58 |
$this->docProps->setCompany((string) $xmlCore->Company);
|
|
|
59 |
}
|
|
|
60 |
if (isset($xmlCore->Manager)) {
|
|
|
61 |
$this->docProps->setManager((string) $xmlCore->Manager);
|
|
|
62 |
}
|
|
|
63 |
if (isset($xmlCore->HyperlinkBase)) {
|
|
|
64 |
$this->docProps->setHyperlinkBase((string) $xmlCore->HyperlinkBase);
|
|
|
65 |
}
|
|
|
66 |
}
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
public function readCustomProperties(string $propertyData): void
|
|
|
70 |
{
|
|
|
71 |
$xmlCore = $this->extractPropertyData($propertyData);
|
|
|
72 |
|
|
|
73 |
if (is_object($xmlCore)) {
|
|
|
74 |
foreach ($xmlCore as $xmlProperty) {
|
|
|
75 |
/** @var SimpleXMLElement $xmlProperty */
|
|
|
76 |
$cellDataOfficeAttributes = $xmlProperty->attributes();
|
|
|
77 |
if (isset($cellDataOfficeAttributes['name'])) {
|
|
|
78 |
$propertyName = (string) $cellDataOfficeAttributes['name'];
|
|
|
79 |
$cellDataOfficeChildren = $xmlProperty->children('http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes');
|
|
|
80 |
|
|
|
81 |
$attributeType = $cellDataOfficeChildren->getName();
|
|
|
82 |
$attributeValue = (string) $cellDataOfficeChildren->{$attributeType};
|
|
|
83 |
$attributeValue = DocumentProperties::convertProperty($attributeValue, $attributeType);
|
|
|
84 |
$attributeType = DocumentProperties::convertPropertyType($attributeType);
|
|
|
85 |
$this->docProps->setCustomProperty($propertyName, $attributeValue, $attributeType);
|
|
|
86 |
}
|
|
|
87 |
}
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
private function getArrayItem(null|array|false $array): string
|
|
|
92 |
{
|
|
|
93 |
return is_array($array) ? (string) ($array[0] ?? '') : '';
|
|
|
94 |
}
|
|
|
95 |
}
|