TYPO3 CMS  TYPO3_7-6
ExtensionXmlPushParser.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 
32 {
38  protected $elementData = '';
39 
43  public function __construct()
44  {
45  $this->requiredPhpExtensions = 'xml';
46  }
47 
53  protected function createParser()
54  {
55  $this->objXml = xml_parser_create();
56  xml_set_object($this->objXml, $this);
57  }
58 
66  public function parseXml($file)
67  {
68  $this->createParser();
69  if (!is_resource($this->objXml)) {
70  throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Unable to create XML parser.', 1342640663);
71  }
72  // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
73  $previousValueOfEntityLoader = libxml_disable_entity_loader(true);
74  // keep original character case of XML document
75  xml_parser_set_option($this->objXml, XML_OPTION_CASE_FOLDING, false);
76  xml_parser_set_option($this->objXml, XML_OPTION_SKIP_WHITE, false);
77  xml_parser_set_option($this->objXml, XML_OPTION_TARGET_ENCODING, 'utf-8');
78  xml_set_element_handler($this->objXml, 'startElement', 'endElement');
79  xml_set_character_data_handler($this->objXml, 'characterData');
80  if (!($fp = fopen($file, 'r'))) {
81  throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(sprintf('Unable to open file resource %s.', $file), 1342640689);
82  }
83  while ($data = fread($fp, 4096)) {
84  if (!xml_parse($this->objXml, $data, feof($fp))) {
85  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);
86  }
87  }
88  libxml_disable_entity_loader($previousValueOfEntityLoader);
89  xml_parser_free($this->objXml);
90  }
91 
100  protected function startElement($parser, $elementName, $attrs)
101  {
102  switch ($elementName) {
103  case 'extension':
104  $this->extensionKey = $attrs['extensionkey'];
105  break;
106  case 'version':
107  $this->version = $attrs['version'];
108  break;
109  default:
110  $this->elementData = '';
111  }
112  }
113 
121  protected function endElement($parser, $elementName)
122  {
123  switch ($elementName) {
124  case 'extension':
125  $this->resetProperties(true);
126  break;
127  case 'version':
128  $this->notify();
129  $this->resetProperties();
130  break;
131  case 'downloadcounter':
132  // downloadcounter could be a child node of
133  // extension or version
134  if ($this->version == null) {
135  $this->extensionDownloadCounter = $this->elementData;
136  } else {
137  $this->versionDownloadCounter = $this->elementData;
138  }
139  break;
140  case 'title':
141  $this->title = $this->elementData;
142  break;
143  case 'description':
144  $this->description = $this->elementData;
145  break;
146  case 'state':
147  $this->state = $this->elementData;
148  break;
149  case 'reviewstate':
150  $this->reviewstate = $this->elementData;
151  break;
152  case 'category':
153  $this->category = $this->elementData;
154  break;
155  case 'lastuploaddate':
156  $this->lastuploaddate = $this->elementData;
157  break;
158  case 'uploadcomment':
159  $this->uploadcomment = $this->elementData;
160  break;
161  case 'dependencies':
162  $this->dependencies = $this->convertDependencies($this->elementData);
163  break;
164  case 'authorname':
165  $this->authorname = $this->elementData;
166  break;
167  case 'authoremail':
168  $this->authoremail = $this->elementData;
169  break;
170  case 'authorcompany':
171  $this->authorcompany = $this->elementData;
172  break;
173  case 'ownerusername':
174  $this->ownerusername = $this->elementData;
175  break;
176  case 't3xfilemd5':
177  $this->t3xfilemd5 = $this->elementData;
178  break;
179  }
180  }
181 
189  protected function characterData($parser, $data)
190  {
191  $this->elementData .= $data;
192  }
193 }