Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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 PhpOffice\PhpSpreadsheet\Settings;
   8  
   9  class Properties
  10  {
  11      private $securityScanner;
  12  
  13      private $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($propertyData)
  22      {
  23          return simplexml_load_string(
  24              $this->securityScanner->scan($propertyData),
  25              'SimpleXMLElement',
  26              Settings::getLibXmlLoaderOptions()
  27          );
  28      }
  29  
  30      public function readCoreProperties($propertyData)
  31      {
  32          $xmlCore = $this->extractPropertyData($propertyData);
  33  
  34          if (is_object($xmlCore)) {
  35              $xmlCore->registerXPathNamespace('dc', 'http://purl.org/dc/elements/1.1/');
  36              $xmlCore->registerXPathNamespace('dcterms', 'http://purl.org/dc/terms/');
  37              $xmlCore->registerXPathNamespace('cp', 'http://schemas.openxmlformats.org/package/2006/metadata/core-properties');
  38  
  39              $this->docProps->setCreator((string) self::getArrayItem($xmlCore->xpath('dc:creator')));
  40              $this->docProps->setLastModifiedBy((string) self::getArrayItem($xmlCore->xpath('cp:lastModifiedBy')));
  41              $this->docProps->setCreated(strtotime(self::getArrayItem($xmlCore->xpath('dcterms:created')))); //! respect xsi:type
  42              $this->docProps->setModified(strtotime(self::getArrayItem($xmlCore->xpath('dcterms:modified')))); //! respect xsi:type
  43              $this->docProps->setTitle((string) self::getArrayItem($xmlCore->xpath('dc:title')));
  44              $this->docProps->setDescription((string) self::getArrayItem($xmlCore->xpath('dc:description')));
  45              $this->docProps->setSubject((string) self::getArrayItem($xmlCore->xpath('dc:subject')));
  46              $this->docProps->setKeywords((string) self::getArrayItem($xmlCore->xpath('cp:keywords')));
  47              $this->docProps->setCategory((string) self::getArrayItem($xmlCore->xpath('cp:category')));
  48          }
  49      }
  50  
  51      public function readExtendedProperties($propertyData)
  52      {
  53          $xmlCore = $this->extractPropertyData($propertyData);
  54  
  55          if (is_object($xmlCore)) {
  56              if (isset($xmlCore->Company)) {
  57                  $this->docProps->setCompany((string) $xmlCore->Company);
  58              }
  59              if (isset($xmlCore->Manager)) {
  60                  $this->docProps->setManager((string) $xmlCore->Manager);
  61              }
  62          }
  63      }
  64  
  65      public function readCustomProperties($propertyData)
  66      {
  67          $xmlCore = $this->extractPropertyData($propertyData);
  68  
  69          if (is_object($xmlCore)) {
  70              foreach ($xmlCore as $xmlProperty) {
  71                  /** @var \SimpleXMLElement $xmlProperty */
  72                  $cellDataOfficeAttributes = $xmlProperty->attributes();
  73                  if (isset($cellDataOfficeAttributes['name'])) {
  74                      $propertyName = (string) $cellDataOfficeAttributes['name'];
  75                      $cellDataOfficeChildren = $xmlProperty->children('http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes');
  76  
  77                      $attributeType = $cellDataOfficeChildren->getName();
  78                      $attributeValue = (string) $cellDataOfficeChildren->{$attributeType};
  79                      $attributeValue = DocumentProperties::convertProperty($attributeValue, $attributeType);
  80                      $attributeType = DocumentProperties::convertPropertyType($attributeType);
  81                      $this->docProps->setCustomProperty($propertyName, $attributeValue, $attributeType);
  82                  }
  83              }
  84          }
  85      }
  86  
  87      private static function getArrayItem(array $array, $key = 0)
  88      {
  89          return $array[$key] ?? null;
  90      }
  91  }