TYPO3 CMS  TYPO3_8-7
MirrorXmlPushParser.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
28 {
32  protected $element;
33 
37  public function __construct()
38  {
39  $this->requiredPhpExtensions = 'xml';
40  }
41 
45  protected function createParser()
46  {
47  $this->objXml = xml_parser_create();
48  xml_set_object($this->objXml, $this);
49  }
50 
57  public function parseXml($file)
58  {
59  $this->createParser();
60  if (!is_resource($this->objXml)) {
61  throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Unable to create XML parser.', 1342641009);
62  }
63  // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
64  $previousValueOfEntityLoader = libxml_disable_entity_loader(true);
65  // keep original character case of XML document
66  xml_parser_set_option($this->objXml, XML_OPTION_CASE_FOLDING, false);
67  xml_parser_set_option($this->objXml, XML_OPTION_SKIP_WHITE, false);
68  xml_parser_set_option($this->objXml, XML_OPTION_TARGET_ENCODING, 'utf-8');
69  xml_set_element_handler($this->objXml, 'startElement', 'endElement');
70  xml_set_character_data_handler($this->objXml, 'characterData');
71  if (!($fp = fopen($file, 'r'))) {
72  throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(sprintf('Unable to open file resource %s.', $file), 1342641010);
73  }
74  while ($data = fread($fp, 4096)) {
75  if (!xml_parse($this->objXml, $data, feof($fp))) {
76  throw new \TYPO3\CMS\Extensionmanager\Exception\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);
77  }
78  }
79  libxml_disable_entity_loader($previousValueOfEntityLoader);
80  xml_parser_free($this->objXml);
81  }
82 
90  protected function startElement($parser, $elementName, $attrs)
91  {
92  switch ($elementName) {
93  default:
94  $this->element = $elementName;
95  }
96  }
97 
107  protected function endElement($parser, $elementName)
108  {
109  switch ($elementName) {
110  case 'mirror':
111  $this->notify();
112  $this->resetProperties();
113  break;
114  default:
115  $this->element = null;
116  }
117  }
118 
128  protected function characterData($parser, $data)
129  {
130  if (isset($this->element)) {
131  switch ($this->element) {
132  case 'title':
133  $this->title = $data;
134  break;
135  case 'host':
136  $this->host = $data;
137  break;
138  case 'path':
139  $this->path = $data;
140  break;
141  case 'country':
142  $this->country = $data;
143  break;
144  case 'name':
145  $this->sponsorname = $data;
146  break;
147  case 'link':
148  $this->sponsorlink = $data;
149  break;
150  case 'logo':
151  $this->sponsorlogo = $data;
152  break;
153  default:
154  // Do nothing
155  }
156  }
157  }
158 }