‪TYPO3CMS  10.4
MirrorXmlPushParser.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
19 
32 {
36  protected ‪$element;
37 
41  public function ‪__construct()
42  {
43  $this->requiredPhpExtensions = 'xml';
44  }
45 
49  protected function ‪createParser()
50  {
51  $this->objXml = xml_parser_create();
52  xml_set_object($this->objXml, $this);
53  }
54 
61  public function ‪parseXml($file)
62  {
63  $this->‪createParser();
64  if (!is_resource($this->objXml)) {
65  throw new ‪ExtensionManagerException('Unable to create XML parser.', 1342641009);
66  }
67  // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
68  $previousValueOfEntityLoader = null;
69  if (PHP_MAJOR_VERSION < 8) {
70  $previousValueOfEntityLoader = libxml_disable_entity_loader(true);
71  }
72  // keep original character case of XML document
73  xml_parser_set_option($this->objXml, XML_OPTION_CASE_FOLDING, false);
74  xml_parser_set_option($this->objXml, XML_OPTION_SKIP_WHITE, false);
75  xml_parser_set_option($this->objXml, XML_OPTION_TARGET_ENCODING, 'utf-8');
76  xml_set_element_handler($this->objXml, [$this, 'startElement'], [$this, 'endElement']);
77  xml_set_character_data_handler($this->objXml, [$this, 'characterData']);
78  if (!($fp = fopen($file, 'r'))) {
79  throw new ‪ExtensionManagerException(sprintf('Unable to open file resource %s.', $file), 1342641010);
80  }
81  while ($data = fread($fp, 4096)) {
82  if (!xml_parse($this->objXml, $data, feof($fp))) {
83  throw new ‪ExtensionManagerException(sprintf('XML error %s in line %u of file resource %s.', xml_error_string(xml_get_error_code($this->objXml)), xml_get_current_line_number($this->objXml), $file), 1342641011);
84  }
85  }
86  if (PHP_MAJOR_VERSION < 8) {
87  libxml_disable_entity_loader($previousValueOfEntityLoader);
88  }
89  xml_parser_free($this->objXml);
90  }
91 
99  protected function ‪startElement(‪$parser, $elementName, $attrs)
100  {
101  switch ($elementName) {
102  default:
103  $this->element = $elementName;
104  }
105  }
106 
116  protected function ‪endElement(‪$parser, $elementName)
117  {
118  switch ($elementName) {
119  case 'mirror':
120  $this->‪notify();
121  $this->‪resetProperties();
122  break;
123  default:
124  $this->element = null;
125  }
126  }
127 
137  protected function ‪characterData(‪$parser, $data)
138  {
139  if (isset($this->element)) {
140  switch ($this->element) {
141  case 'title':
142  $this->title = $data;
143  break;
144  case 'host':
145  $this->host = $data;
146  break;
147  case 'path':
148  $this->path = $data;
149  break;
150  case 'country':
151  $this->country = $data;
152  break;
153  default:
154  // Do nothing
155  }
156  }
157  }
158 }
‪TYPO3\CMS\Extensionmanager\Utility\Parser\MirrorXmlPushParser
Definition: MirrorXmlPushParser.php:32
‪TYPO3\CMS\Extensionmanager\Utility\Parser\AbstractMirrorXmlParser\resetProperties
‪resetProperties()
Definition: AbstractMirrorXmlParser.php:113
‪TYPO3\CMS\Extensionmanager\Utility\Parser
Definition: AbstractExtensionXmlParser.php:16
‪TYPO3\CMS\Extensionmanager\Utility\Parser\MirrorXmlPushParser\characterData
‪characterData($parser, $data)
Definition: MirrorXmlPushParser.php:136
‪TYPO3\CMS\Extensionmanager\Utility\Parser\MirrorXmlPushParser\createParser
‪createParser()
Definition: MirrorXmlPushParser.php:48
‪TYPO3\CMS\Extensionmanager\Utility\Parser\MirrorXmlPushParser\parseXml
‪parseXml($file)
Definition: MirrorXmlPushParser.php:60
‪TYPO3\CMS\Extensionmanager\Utility\Parser\MirrorXmlPushParser\startElement
‪startElement($parser, $elementName, $attrs)
Definition: MirrorXmlPushParser.php:98
‪TYPO3\CMS\Extensionmanager\Utility\Parser\MirrorXmlPushParser\endElement
‪endElement($parser, $elementName)
Definition: MirrorXmlPushParser.php:115
‪$parser
‪$parser
Definition: annotationChecker.php:108
‪TYPO3\CMS\Extensionmanager\Utility\Parser\AbstractXmlParser\notify
‪notify()
Definition: AbstractXmlParser.php:76
‪TYPO3\CMS\Extensionmanager\Utility\Parser\MirrorXmlPushParser\__construct
‪__construct()
Definition: MirrorXmlPushParser.php:40
‪TYPO3\CMS\Extensionmanager\Utility\Parser\MirrorXmlPushParser\$element
‪string $element
Definition: MirrorXmlPushParser.php:35
‪TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
Definition: ExtensionManagerException.php:24
‪TYPO3\CMS\Extensionmanager\Utility\Parser\AbstractMirrorXmlParser
Definition: AbstractMirrorXmlParser.php:23