TYPO3 CMS  TYPO3_6-2
MirrorXmlPushParser.php
Go to the documentation of this file.
1 <?php
3 
31 
35  protected $element;
36 
40  public function __construct() {
41  $this->requiredPhpExtensions = 'xml';
42  }
43 
49  protected function createParser() {
50  $this->objXml = xml_parser_create();
51  xml_set_object($this->objXml, $this);
52  }
53 
61  public function parseXml($file) {
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.', htmlspecialchars($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), htmlspecialchars($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  switch ($elementName) {
96  default:
97  $this->element = $elementName;
98  }
99  }
100 
111  protected function endElement($parser, $elementName) {
112  switch ($elementName) {
113  case 'mirror':
114  $this->notify();
115  $this->resetProperties();
116  break;
117  default:
118  $this->element = NULL;
119  }
120  }
121 
132  protected function characterData($parser, $data) {
133  if (isset($this->element)) {
134  switch ($this->element) {
135  case 'title':
136  $this->title = $data;
137  break;
138  case 'host':
139  $this->host = $data;
140  break;
141  case 'path':
142  $this->path = $data;
143  break;
144  case 'country':
145  $this->country = $data;
146  break;
147  case 'name':
148  $this->sponsorname = $data;
149  break;
150  case 'link':
151  $this->sponsorlink = $data;
152  break;
153  case 'logo':
154  $this->sponsorlogo = $data;
155  break;
156  default:
157  // Do nothing
158  }
159  }
160  }
161 }