Cleanup: Updated copyright comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / em / classes / parser / class.tx_em_parser_extensionxmlpushparser.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010-2011 Marcus Krause <marcus#exp2010@t3sec.info>
6 * Steffen Kamper <info@sk-typo3.de>
7 * All rights reserved
8 *
9 * This script is part of the TYPO3 project. The TYPO3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 *
18 * This script is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * This copyright notice MUST APPEAR in all copies of the script!
24 ***************************************************************/
25 /**
26 * Module: Extension manager - Extension.xml push-parser
27 *
28 * $Id: class.tx_em_parser_extensionxmlpushparser.php 1913 2010-02-21 15:47:37Z mkrause $
29 */
30
31
32 /**
33 * Parser for TYPO3's extension.xml file.
34 *
35 * Depends on PHP ext/xml which should be available
36 * with PHP 4+. This is the parser used in TYPO3
37 * Core <= 4.3 (without the "collect all data in one
38 * array" behaviour).
39 * Notice: ext/xml has proven to be buggy with entities.
40 * Use at least PHP 5.2.9+ and libxml2 2.7.3+!
41 *
42 * @author Marcus Krause <marcus#exp2010@t3sec.info>
43 * @author Steffen Kamper <info@sk-typo3.de>
44 *
45 * @since 2010-02-10
46 * @package TYPO3
47 * @subpackage EM
48 */
49 class tx_em_Parser_ExtensionXmlPushParser extends tx_em_Parser_ExtensionXmlAbstractParser implements SplSubject {
50
51
52 /**
53 * Keeps current element to process.
54 *
55 * @var string
56 */
57 protected $element = NULL;
58
59 /**
60 * Class constructor.
61 *
62 * @return void
63 */
64 function __construct() {
65 $this->requiredPHPExt = 'xml';
66
67 if ($this->isAvailable()) {
68 $this->objXML = xml_parser_create();
69 xml_set_object($this->objXML, $this);
70 }
71 }
72
73 /**
74 * Method parses an extensions.xml file.
75 *
76 * @param string $file: GZIP stream resource
77 * @return void
78 * @throws tx_em_ExtensionXmlException in case of XML parser errors
79 */
80 public function parseXML($file) {
81
82 if (!is_resource($this->objXML)) {
83 $this->throwException('Unable to create XML parser.');
84 }
85 // keep original character case of XML document
86 xml_parser_set_option($this->objXML, XML_OPTION_CASE_FOLDING, FALSE);
87 xml_parser_set_option($this->objXML, XML_OPTION_SKIP_WHITE, FALSE);
88 xml_parser_set_option($this->objXML, XML_OPTION_TARGET_ENCODING, 'utf-8');
89 xml_set_element_handler($this->objXML, 'startElement', 'endElement');
90 xml_set_character_data_handler($this->objXML, 'characterData');
91
92 if (!($fp = fopen($file, "r"))) {
93 $this->throwException(sprintf('Unable to open file ressource %s.', htmlspecialchars($file)));
94 }
95 while ($data = fread($fp, 4096)) {
96 if (!xml_parse($this->objXML, $data, feof($fp))) {
97 $this->throwException(sprintf('XML error %s in line %u of file ressource %s.',
98 xml_error_string(xml_get_error_code($this->objXML)),
99 xml_get_current_line_number($this->objXML),
100 htmlspecialchars($file)));
101 }
102 }
103 xml_parser_free($this->objXML);
104 }
105
106 /**
107 * Method is invoked when parser accesses start tag of an element.
108 *
109 * @param ressource $parser parser resource
110 * @param string $elementName: element name at parser's current position
111 * @param array $attrs: array of an element's attributes if available
112 * @return void
113 */
114 protected function startElement($parser, $elementName, $attrs) {
115 switch ($elementName) {
116 case 'extension':
117 $this->extensionKey = $attrs['extensionkey'];
118 break;
119 case 'version':
120 $this->version = $attrs['version'];
121 break;
122 default:
123 $this->element = $elementName;
124 }
125 }
126
127 /**
128 * Method is invoked when parser accesses end tag of an element.
129 *
130 * @param ressource $parser parser resource
131 * @param string $elementName: element name at parser's current position
132 * @return void
133 */
134 protected function endElement($parser, $elementName) {
135 switch ($elementName) {
136 case 'extension':
137 $this->resetProperties(TRUE);
138 break;
139 case 'version':
140 $this->notify();
141 $this->resetProperties();
142 break;
143 default:
144 $this->element = NULL;
145 }
146 }
147
148 /**
149 * Method is invoked when parser accesses any character other than elements.
150 *
151 * @param ressource $parser: parser ressource
152 * @param string $data: an element's value
153 * @return void
154 */
155 protected function characterData($parser, $data) {
156 if (isset($this->element)) {
157 switch ($this->element) {
158 case 'downloadcounter':
159 // downloadcounter could be a child node of
160 // extension or version
161 if ($this->version == NULL) {
162 $this->extensionDLCounter = $data;
163 } else {
164 $this->versionDLCounter = $data;
165 }
166 break;
167 case 'title':
168 $this->title = $data;
169 break;
170 case 'description':
171 $this->description = $data;
172 break;
173 case 'state':
174 $this->state = $data;
175 break;
176 case 'reviewstate':
177 $this->reviewstate = $data;
178 break;
179 case 'category':
180 $this->category = $data;
181 break;
182 case 'lastuploaddate':
183 $this->lastuploaddate = $data;
184 break;
185 case 'uploadcomment':
186 $this->uploadcomment = $data;
187 break;
188 case 'dependencies':
189 $this->dependencies = $data;
190 break;
191 case 'authorname':
192 $this->authorname = $data;
193 break;
194 case 'authoremail':
195 $this->authoremail = $data;
196 break;
197 case 'authorcompany':
198 $this->authorcompany = $data;
199 break;
200 case 'ownerusername':
201 $this->ownerusername = $data;
202 break;
203 case 't3xfilemd5':
204 $this->t3xfilemd5 = $data;
205 break;
206 }
207 }
208 }
209
210 /**
211 * Method attaches an observer.
212 *
213 * @param SplObserver $observer: an observer to attach
214 * @return void
215 */
216 public function attach(SplObserver $observer) {
217 $this->observers[] = $observer;
218 }
219
220 /**
221 * Method detaches an attached observer
222 *
223 * @param SplObserver $observer: an observer to detach
224 * @return void
225 */
226 public function detach(SplObserver $observer) {
227 $key = array_search($observer, $this->observers, true);
228 if (!($key === false)) {
229 unset($this->observers[$key]);
230 }
231 }
232
233 /**
234 * Method notifies attached observers.
235 *
236 * @return void
237 */
238 public function notify() {
239 foreach ($this->observers as $observer) {
240 $observer->update($this);
241 }
242 }
243 }
244
245 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/sysext/em/classes/parser/class.tx_em_parser_extensionxmlpushparser.php'])) {
246 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/sysext/em/classes/parser/class.tx_em_parser_extensionxmlpushparser.php']);
247 }
248
249 ?>