35c3a7d024bac690e23fce4218015b777e717608
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Utility / Parser / MirrorXmlPushParser.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Utility\Parser;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2013 Marcus Krause <marcus#exp2010@t3sec.info>
8 * Steffen Kamper <info@sk-typo3.de>
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Parser for TYPO3's mirrors.xml file.
29 *
30 * Depends on PHP ext/xml which should be available
31 * with PHP 4+. This is the parser used in TYPO3
32 * Core <= 4.3 (without the "collect all data in one
33 * array" behaviour).
34 * Notice: ext/xml has proven to be buggy with entities.
35 * Use at least PHP 5.2.9+ and libxml2 2.7.3+!
36 *
37 * @author Marcus Krause <marcus#exp2010@t3sec.info>
38 * @author Steffen Kamper <info@sk-typo3.de>
39 * @since 2010-11-17
40 */
41 class MirrorXmlPushParser extends AbstractMirrorXmlParser {
42
43 /**
44 * @var string
45 */
46 protected $element;
47
48 /**
49 * Class constructor.
50 */
51 public function __construct() {
52 $this->requiredPhpExtensions = 'xml';
53 }
54
55 /**
56 * Create required parser
57 *
58 * @return void
59 */
60 protected function createParser() {
61 $this->objXml = xml_parser_create();
62 xml_set_object($this->objXml, $this);
63 }
64
65 /**
66 * Method parses a mirror.xml file.
67 *
68 * @param string $file GZIP stream resource
69 * @return void
70 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException in case of XML parser errors
71 */
72 public function parseXml($file) {
73 $this->createParser();
74 if (!is_resource($this->objXml)) {
75 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Unable to create XML parser.', 1342641009);
76 }
77 // keep original character case of XML document
78 xml_parser_set_option($this->objXml, XML_OPTION_CASE_FOLDING, FALSE);
79 xml_parser_set_option($this->objXml, XML_OPTION_SKIP_WHITE, FALSE);
80 xml_parser_set_option($this->objXml, XML_OPTION_TARGET_ENCODING, 'utf-8');
81 xml_set_element_handler($this->objXml, 'startElement', 'endElement');
82 xml_set_character_data_handler($this->objXml, 'characterData');
83 if (!($fp = fopen($file, 'r'))) {
84 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(sprintf('Unable to open file resource %s.', htmlspecialchars($file)), 1342641010);
85 }
86 while ($data = fread($fp, 4096)) {
87 if (!xml_parse($this->objXml, $data, feof($fp))) {
88 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);
89 }
90 }
91 xml_parser_free($this->objXml);
92 }
93
94 /**
95 * Method is invoked when parser accesses start tag of an element.
96 *
97 * @param resource $parser parser resource
98 * @param string $elementName element name at parser's current position
99 * @param array $attrs array of an element's attributes if available
100 * @return void
101 */
102 protected function startElement($parser, $elementName, $attrs) {
103 switch ($elementName) {
104 default:
105 $this->element = $elementName;
106 }
107 }
108
109 /**
110 * Method is invoked when parser accesses end tag of an element.
111 * Although the first parameter seems unused, it needs to be there for
112 * adherence to the API of xml_set_element_handler
113 *
114 * @see xml_set_element_handler
115 * @param resource $parser parser resource
116 * @param string $elementName element name at parser's current position
117 * @return void
118 */
119 protected function endElement($parser, $elementName) {
120 switch ($elementName) {
121 case 'mirror':
122 $this->notify();
123 $this->resetProperties();
124 break;
125 default:
126 $this->element = NULL;
127 }
128 }
129
130 /**
131 * Method is invoked when parser accesses any character other than elements.
132 * Although the first parameter seems unused, it needs to be there for
133 * adherence to the API of xml_set_character_data_handler
134 *
135 * @see xml_set_character_data_handler
136 * @param resource $parser parser resource
137 * @param string $data an element's value
138 * @return void
139 */
140 protected function characterData($parser, $data) {
141 if (isset($this->element)) {
142 switch ($this->element) {
143 case 'title':
144 $this->title = $data;
145 break;
146 case 'host':
147 $this->host = $data;
148 break;
149 case 'path':
150 $this->path = $data;
151 break;
152 case 'country':
153 $this->country = $data;
154 break;
155 case 'name':
156 $this->sponsorname = $data;
157 break;
158 case 'link':
159 $this->sponsorlink = $data;
160 break;
161 case 'logo':
162 $this->sponsorlogo = $data;
163 break;
164 default:
165 // Do nothing
166 }
167 }
168 }
169 }