TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
MirrorXmlPushParser.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Utility\Parser;
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 
47  protected function createParser()
48  {
49  $this->objXml = xml_parser_create();
50  xml_set_object($this->objXml, $this);
51  }
52 
60  public function parseXml($file)
61  {
62  $this->createParser();
63  if (!is_resource($this->objXml)) {
64  throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Unable to create XML parser.', 1342641009);
65  }
66  // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
67  $previousValueOfEntityLoader = libxml_disable_entity_loader(true);
68  // keep original character case of XML document
69  xml_parser_set_option($this->objXml, XML_OPTION_CASE_FOLDING, false);
70  xml_parser_set_option($this->objXml, XML_OPTION_SKIP_WHITE, false);
71  xml_parser_set_option($this->objXml, XML_OPTION_TARGET_ENCODING, 'utf-8');
72  xml_set_element_handler($this->objXml, 'startElement', 'endElement');
73  xml_set_character_data_handler($this->objXml, 'characterData');
74  if (!($fp = fopen($file, 'r'))) {
75  throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(sprintf('Unable to open file resource %s.', $file), 1342641010);
76  }
77  while ($data = fread($fp, 4096)) {
78  if (!xml_parse($this->objXml, $data, feof($fp))) {
79  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);
80  }
81  }
82  libxml_disable_entity_loader($previousValueOfEntityLoader);
83  xml_parser_free($this->objXml);
84  }
85 
94  protected function startElement($parser, $elementName, $attrs)
95  {
96  switch ($elementName) {
97  default:
98  $this->element = $elementName;
99  }
100  }
101 
112  protected function endElement($parser, $elementName)
113  {
114  switch ($elementName) {
115  case 'mirror':
116  $this->notify();
117  $this->resetProperties();
118  break;
119  default:
120  $this->element = null;
121  }
122  }
123 
134  protected function characterData($parser, $data)
135  {
136  if (isset($this->element)) {
137  switch ($this->element) {
138  case 'title':
139  $this->title = $data;
140  break;
141  case 'host':
142  $this->host = $data;
143  break;
144  case 'path':
145  $this->path = $data;
146  break;
147  case 'country':
148  $this->country = $data;
149  break;
150  case 'name':
151  $this->sponsorname = $data;
152  break;
153  case 'link':
154  $this->sponsorlink = $data;
155  break;
156  case 'logo':
157  $this->sponsorlogo = $data;
158  break;
159  default:
160  // Do nothing
161  }
162  }
163  }
164 }