TYPO3 CMS  TYPO3_7-6
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 
29 {
33  protected $element;
34 
38  public function __construct()
39  {
40  $this->requiredPhpExtensions = 'xml';
41  }
42 
48  protected function createParser()
49  {
50  $this->objXml = xml_parser_create();
51  xml_set_object($this->objXml, $this);
52  }
53 
61  public function parseXml($file)
62  {
63  $this->createParser();
64  if (!is_resource($this->objXml)) {
65  throw new \TYPO3\CMS\Extensionmanager\Exception\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 = libxml_disable_entity_loader(true);
69  // keep original character case of XML document
70  xml_parser_set_option($this->objXml, XML_OPTION_CASE_FOLDING, false);
71  xml_parser_set_option($this->objXml, XML_OPTION_SKIP_WHITE, false);
72  xml_parser_set_option($this->objXml, XML_OPTION_TARGET_ENCODING, 'utf-8');
73  xml_set_element_handler($this->objXml, 'startElement', 'endElement');
74  xml_set_character_data_handler($this->objXml, 'characterData');
75  if (!($fp = fopen($file, 'r'))) {
76  throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(sprintf('Unable to open file resource %s.', $file), 1342641010);
77  }
78  while ($data = fread($fp, 4096)) {
79  if (!xml_parse($this->objXml, $data, feof($fp))) {
80  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);
81  }
82  }
83  libxml_disable_entity_loader($previousValueOfEntityLoader);
84  xml_parser_free($this->objXml);
85  }
86 
95  protected function startElement($parser, $elementName, $attrs)
96  {
97  switch ($elementName) {
98  default:
99  $this->element = $elementName;
100  }
101  }
102 
113  protected function endElement($parser, $elementName)
114  {
115  switch ($elementName) {
116  case 'mirror':
117  $this->notify();
118  $this->resetProperties();
119  break;
120  default:
121  $this->element = null;
122  }
123  }
124 
135  protected function characterData($parser, $data)
136  {
137  if (isset($this->element)) {
138  switch ($this->element) {
139  case 'title':
140  $this->title = $data;
141  break;
142  case 'host':
143  $this->host = $data;
144  break;
145  case 'path':
146  $this->path = $data;
147  break;
148  case 'country':
149  $this->country = $data;
150  break;
151  case 'name':
152  $this->sponsorname = $data;
153  break;
154  case 'link':
155  $this->sponsorlink = $data;
156  break;
157  case 'logo':
158  $this->sponsorlogo = $data;
159  break;
160  default:
161  // Do nothing
162  }
163  }
164  }
165 }