TYPO3 CMS  TYPO3_8-7
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 
28 {
34  protected $elementData = '';
35 
39  public function __construct()
40  {
41  $this->requiredPhpExtensions = 'xml';
42  }
43 
47  protected function createParser()
48  {
49  $this->objXml = xml_parser_create();
50  xml_set_object($this->objXml, $this);
51  }
52 
59  public function parseXml($file)
60  {
61  $this->createParser();
62  if (!is_resource($this->objXml)) {
63  throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Unable to create XML parser.', 1342640663);
64  }
65  // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
66  $previousValueOfEntityLoader = libxml_disable_entity_loader(true);
67  // keep original character case of XML document
68  xml_parser_set_option($this->objXml, XML_OPTION_CASE_FOLDING, false);
69  xml_parser_set_option($this->objXml, XML_OPTION_SKIP_WHITE, false);
70  xml_parser_set_option($this->objXml, XML_OPTION_TARGET_ENCODING, 'utf-8');
71  xml_set_element_handler($this->objXml, 'startElement', 'endElement');
72  xml_set_character_data_handler($this->objXml, 'characterData');
73  if (!($fp = fopen($file, 'r'))) {
74  throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(sprintf('Unable to open file resource %s.', $file), 1342640689);
75  }
76  while ($data = fread($fp, 4096)) {
77  if (!xml_parse($this->objXml, $data, feof($fp))) {
78  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);
79  }
80  }
81  libxml_disable_entity_loader($previousValueOfEntityLoader);
82  xml_parser_free($this->objXml);
83  }
84 
92  protected function startElement($parser, $elementName, $attrs)
93  {
94  switch ($elementName) {
95  case 'extension':
96  $this->extensionKey = $attrs['extensionkey'];
97  break;
98  case 'version':
99  $this->version = $attrs['version'];
100  break;
101  default:
102  $this->elementData = '';
103  }
104  }
105 
112  protected function endElement($parser, $elementName)
113  {
114  switch ($elementName) {
115  case 'extension':
116  $this->resetProperties(true);
117  break;
118  case 'version':
119  $this->notify();
120  $this->resetProperties();
121  break;
122  case 'downloadcounter':
123  // downloadcounter could be a child node of
124  // extension or version
125  if ($this->version == null) {
126  $this->extensionDownloadCounter = $this->elementData;
127  } else {
128  $this->versionDownloadCounter = $this->elementData;
129  }
130  break;
131  case 'title':
132  $this->title = $this->elementData;
133  break;
134  case 'description':
135  $this->description = $this->elementData;
136  break;
137  case 'state':
138  $this->state = $this->elementData;
139  break;
140  case 'reviewstate':
141  $this->reviewstate = $this->elementData;
142  break;
143  case 'category':
144  $this->category = $this->elementData;
145  break;
146  case 'lastuploaddate':
147  $this->lastuploaddate = $this->elementData;
148  break;
149  case 'uploadcomment':
150  $this->uploadcomment = $this->elementData;
151  break;
152  case 'dependencies':
153  $this->dependencies = $this->convertDependencies($this->elementData);
154  break;
155  case 'authorname':
156  $this->authorname = $this->elementData;
157  break;
158  case 'authoremail':
159  $this->authoremail = $this->elementData;
160  break;
161  case 'authorcompany':
162  $this->authorcompany = $this->elementData;
163  break;
164  case 'ownerusername':
165  $this->ownerusername = $this->elementData;
166  break;
167  case 't3xfilemd5':
168  $this->t3xfilemd5 = $this->elementData;
169  break;
170  }
171  }
172 
179  protected function characterData($parser, $data)
180  {
181  $this->elementData .= $data;
182  }
183 }