TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
ExtensionXmlPushParser.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 {
34  protected $element = null;
35 
39  public function __construct()
40  {
41  $this->requiredPhpExtensions = 'xml';
42  }
43 
49  protected function createParser()
50  {
51  $this->objXml = xml_parser_create();
52  xml_set_object($this->objXml, $this);
53  }
54 
62  public function parseXml($file)
63  {
64  $this->createParser();
65  if (!is_resource($this->objXml)) {
66  throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Unable to create XML parser.', 1342640663);
67  }
68  // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
69  $previousValueOfEntityLoader = libxml_disable_entity_loader(true);
70  // keep original character case of XML document
71  xml_parser_set_option($this->objXml, XML_OPTION_CASE_FOLDING, false);
72  xml_parser_set_option($this->objXml, XML_OPTION_SKIP_WHITE, false);
73  xml_parser_set_option($this->objXml, XML_OPTION_TARGET_ENCODING, 'utf-8');
74  xml_set_element_handler($this->objXml, 'startElement', 'endElement');
75  xml_set_character_data_handler($this->objXml, 'characterData');
76  if (!($fp = fopen($file, 'r'))) {
77  throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(sprintf('Unable to open file resource %s.', $file), 1342640689);
78  }
79  while ($data = fread($fp, 4096)) {
80  if (!xml_parse($this->objXml, $data, feof($fp))) {
81  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), 1342640703);
82  }
83  }
84  libxml_disable_entity_loader($previousValueOfEntityLoader);
85  xml_parser_free($this->objXml);
86  }
87 
96  protected function startElement($parser, $elementName, $attrs)
97  {
98  switch ($elementName) {
99  case 'extension':
100  $this->extensionKey = $attrs['extensionkey'];
101  break;
102  case 'version':
103  $this->version = $attrs['version'];
104  break;
105  default:
106  $this->element = $elementName;
107  }
108  }
109 
117  protected function endElement($parser, $elementName)
118  {
119  switch ($elementName) {
120  case 'extension':
121  $this->resetProperties(true);
122  break;
123  case 'version':
124  $this->notify();
125  $this->resetProperties();
126  break;
127  default:
128  $this->element = null;
129  }
130  }
131 
139  protected function characterData($parser, $data)
140  {
141  if (isset($this->element)) {
142  switch ($this->element) {
143  case 'downloadcounter':
144  // downloadcounter could be a child node of
145  // extension or version
146  if ($this->version == null) {
147  $this->extensionDownloadCounter = $data;
148  } else {
149  $this->versionDownloadCounter = $data;
150  }
151  break;
152  case 'title':
153  $this->title = $data;
154  break;
155  case 'description':
156  $this->description .= $data;
157  break;
158  case 'state':
159  $this->state = $data;
160  break;
161  case 'reviewstate':
162  $this->reviewstate = $data;
163  break;
164  case 'category':
165  $this->category = $data;
166  break;
167  case 'lastuploaddate':
168  $this->lastuploaddate = $data;
169  break;
170  case 'uploadcomment':
171  $this->uploadcomment .= $data;
172  break;
173  case 'dependencies':
174  $this->dependencies = $this->convertDependencies($data);
175  break;
176  case 'authorname':
177  $this->authorname = $data;
178  break;
179  case 'authoremail':
180  $this->authoremail = $data;
181  break;
182  case 'authorcompany':
183  $this->authorcompany = $data;
184  break;
185  case 'ownerusername':
186  $this->ownerusername = $data;
187  break;
188  case 't3xfilemd5':
189  $this->t3xfilemd5 = $data;
190  break;
191  }
192  }
193  }
194 }