[TASK] Re-work/simplify copyright header in PHP files - Part 9
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Utility / Parser / MirrorXmlPushParser.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Utility\Parser;
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 /**
17 * Parser for TYPO3's mirrors.xml file.
18 *
19 * Depends on PHP ext/xml which should be available
20 * with PHP 4+. This is the parser used in TYPO3
21 * Core <= 4.3 (without the "collect all data in one
22 * array" behaviour).
23 * Notice: ext/xml has proven to be buggy with entities.
24 * Use at least PHP 5.2.9+ and libxml2 2.7.3+!
25 *
26 * @author Marcus Krause <marcus#exp2010@t3sec.info>
27 * @author Steffen Kamper <info@sk-typo3.de>
28 * @since 2010-11-17
29 */
30 class MirrorXmlPushParser extends AbstractMirrorXmlParser {
31
32 /**
33 * @var string
34 */
35 protected $element;
36
37 /**
38 * Class constructor.
39 */
40 public function __construct() {
41 $this->requiredPhpExtensions = 'xml';
42 }
43
44 /**
45 * Create required parser
46 *
47 * @return void
48 */
49 protected function createParser() {
50 $this->objXml = xml_parser_create();
51 xml_set_object($this->objXml, $this);
52 }
53
54 /**
55 * Method parses a mirror.xml file.
56 *
57 * @param string $file GZIP stream resource
58 * @return void
59 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException in case of XML parser errors
60 */
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 // keep original character case of XML document
67 xml_parser_set_option($this->objXml, XML_OPTION_CASE_FOLDING, FALSE);
68 xml_parser_set_option($this->objXml, XML_OPTION_SKIP_WHITE, FALSE);
69 xml_parser_set_option($this->objXml, XML_OPTION_TARGET_ENCODING, 'utf-8');
70 xml_set_element_handler($this->objXml, 'startElement', 'endElement');
71 xml_set_character_data_handler($this->objXml, 'characterData');
72 if (!($fp = fopen($file, 'r'))) {
73 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(sprintf('Unable to open file resource %s.', htmlspecialchars($file)), 1342641010);
74 }
75 while ($data = fread($fp, 4096)) {
76 if (!xml_parse($this->objXml, $data, feof($fp))) {
77 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);
78 }
79 }
80 xml_parser_free($this->objXml);
81 }
82
83 /**
84 * Method is invoked when parser accesses start tag of an element.
85 *
86 * @param resource $parser parser resource
87 * @param string $elementName element name at parser's current position
88 * @param array $attrs array of an element's attributes if available
89 * @return void
90 */
91 protected function startElement($parser, $elementName, $attrs) {
92 switch ($elementName) {
93 default:
94 $this->element = $elementName;
95 }
96 }
97
98 /**
99 * Method is invoked when parser accesses end tag of an element.
100 * Although the first parameter seems unused, it needs to be there for
101 * adherence to the API of xml_set_element_handler
102 *
103 * @see xml_set_element_handler
104 * @param resource $parser parser resource
105 * @param string $elementName element name at parser's current position
106 * @return void
107 */
108 protected function endElement($parser, $elementName) {
109 switch ($elementName) {
110 case 'mirror':
111 $this->notify();
112 $this->resetProperties();
113 break;
114 default:
115 $this->element = NULL;
116 }
117 }
118
119 /**
120 * Method is invoked when parser accesses any character other than elements.
121 * Although the first parameter seems unused, it needs to be there for
122 * adherence to the API of xml_set_character_data_handler
123 *
124 * @see xml_set_character_data_handler
125 * @param resource $parser parser resource
126 * @param string $data an element's value
127 * @return void
128 */
129 protected function characterData($parser, $data) {
130 if (isset($this->element)) {
131 switch ($this->element) {
132 case 'title':
133 $this->title = $data;
134 break;
135 case 'host':
136 $this->host = $data;
137 break;
138 case 'path':
139 $this->path = $data;
140 break;
141 case 'country':
142 $this->country = $data;
143 break;
144 case 'name':
145 $this->sponsorname = $data;
146 break;
147 case 'link':
148 $this->sponsorlink = $data;
149 break;
150 case 'logo':
151 $this->sponsorlogo = $data;
152 break;
153 default:
154 // Do nothing
155 }
156 }
157 }
158 }