2 /* **************************************************************
5 * (c) 2006-2008 Karsten Dambekalns <karsten@typo3.org>
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
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.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
29 * XML handling class for the TYPO3 Extension Manager.
31 * It contains methods for handling the XML files involved with the EM,
32 * such as the list of extension mirrors and the list of available extensions.
34 * @author Karsten Dambekalns <karsten@typo3.org>
38 class SC_mod_tools_em_xmlhandler
{
41 * Enxtension Manager module
43 * @var SC_mod_tools_em_index
48 * Holds the parsed XML from extensions.xml.gz
49 * @see parseExtensionsXML()
53 var $extXMLResult = array();
54 var $extensionsXML = array();
55 var $reviewStates = null;
56 var $useUnchecked = false;
57 var $useObsolete = false;
60 * Reduces the entries in $this->extensionsXML to the latest version per extension and removes entries not matching the search parameter
62 * @param string $search The list of extensions is reduced to entries matching this. If empty, the full list is returned.
63 * @param string $owner If set only extensions of that user are fetched
64 * @param string $order A field to order the result by
65 * @param boolean $allExt If set also unreviewed and obsolete extensions are shown
66 * @param boolean $allVer If set returns all version of an extension, otherwise only the last
67 * @param integer $offset Offset to return result from (goes into LIMIT clause)
68 * @param integer $limit Maximum number of entries to return (goes into LIMIT clause)
71 function searchExtensionsXML($search, $owner='', $order='', $allExt=false, $allVer=false, $offset=0, $limit=500) {
74 $where.= ' AND extkey LIKE \'%'.$GLOBALS['TYPO3_DB']->quoteStr($GLOBALS['TYPO3_DB']->escapeStrForLike($search, 'cache_extensions'), 'cache_extensions').'%\'';
77 $where.= ' AND ownerusername='.$GLOBALS['TYPO3_DB']->fullQuoteStr($owner, 'cache_extensions');
79 if (strlen($owner) ||
$this->useUnchecked ||
$allExt) {
80 // show extensions without review or that have passed review
81 $where.= ' AND reviewstate >= 0';
83 // only display extensions that have passed review
84 $where.= ' AND reviewstate > 0';
86 if (!$this->useObsolete
&& !$allExt) {
87 $where.= ' AND state!=5'; // 5 == obsolete
90 case 'author_company':
91 $forder = 'authorname, authorcompany';
101 $order = $forder.', title';
103 if ($this->useUnchecked
) {
104 $where .= ' AND lastversion>0';
106 $where .= ' AND lastreviewedversion>0';
109 $this->catArr
= array();
111 foreach ($this->emObj
->defaultCategories
['cat'] as $catKey => $tmp) {
112 $this->catArr
[$idx] = $catKey;
115 $this->stateArr
= array();
117 foreach ($this->emObj
->states
as $state => $tmp) {
118 $this->stateArr
[$idx] = $state;
123 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('count(*) as cnt', 'cache_extensions', $where);
124 $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
125 $this->matchingCount
= $row['cnt'];
126 $GLOBALS['TYPO3_DB']->sql_free_result($res);
128 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'cache_extensions', $where, '', $order, $offset.','.$limit);
129 $this->extensionsXML
= array();
130 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
131 $row['category'] = $this->catArr
[$row['category']];
132 $row['state'] = $this->stateArr
[$row['state']];
134 if (!is_array($this->extensionsXML
[$row['extkey']])) {
135 $this->extensionsXML
[$row['extkey']] = array();
136 $this->extensionsXML
[$row['extkey']]['downloadcounter'] = $row['alldownloadcounter'];
138 if (!is_array($this->extensionsXML
[$row['extkey']]['versions'])) {
139 $this->extensionsXML
[$row['extkey']]['versions'] = array();
141 $row['dependencies'] = unserialize($row['dependencies']);
142 $this->extensionsXML
[$row['extkey']]['versions'][$row['version']] = $row;
144 $GLOBALS['TYPO3_DB']->sql_free_result($res);
147 function countExtensions() {
148 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('extkey', 'cache_extensions', '1=1', 'extkey');
149 $cnt = $GLOBALS['TYPO3_DB']->sql_num_rows($res);
150 $GLOBALS['TYPO3_DB']->sql_free_result($res);
155 * Loads the pre-parsed extension list
157 * @return boolean true on success, false on error
159 function loadExtensionsXML() {
160 $this->searchExtensionsXML('', '', '', true);
164 * Frees the pre-parsed extension list
168 function freeExtensionsXML() {
169 unset($this->extensionsXML
);
170 $this->extensionsXML
= array();
174 * Removes all extension with a certain state from the list
176 * @param array &$extensions The "versions" subpart of the extension list
179 function removeObsolete(&$extensions) {
180 if($this->useObsolete
) return;
183 while (list($version, $data) = each($extensions)) {
184 if($data['state']=='obsolete')
185 unset($extensions[$version]);
190 * Returns the reviewstate of a specific extension-key/version
192 * @param string $extKey
193 * @param string $version: ...
194 * @return integer Review state, if none is set 0 is returned as default.
196 function getReviewState($extKey, $version) {
197 $where = 'extkey='.$GLOBALS['TYPO3_DB']->fullQuoteStr($extKey, 'cache_extensions').' AND version='.$GLOBALS['TYPO3_DB']->fullQuoteStr($version, 'cache_extensions');
198 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('reviewstate', 'cache_extensions', $where);
199 if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
200 return $row['reviewstate'];
202 $GLOBALS['TYPO3_DB']->sql_free_result($res);
207 * Removes all extension versions from $extensions that have a reviewstate<1, unless explicitly allowed
209 * @param array &$extensions The "versions" subpart of the extension list
212 function checkReviewState(&$extensions) {
213 if ($this->useUnchecked
) return;
216 while (list($version, $data) = each($extensions)) {
217 if($data['reviewstate']<1)
218 unset($extensions[$version]);
223 * Removes all extension versions from the list of available extensions that have a reviewstate<1, unless explicitly allowed
227 function checkReviewStateGlobal() {
228 if($this->useUnchecked
) return;
230 reset($this->extensionsXML
);
231 while (list($extkey, $data) = each($this->extensionsXML
)) {
232 while (list($version, $vdata) = each($data['versions'])) {
233 if($vdata['reviewstate']<1) unset($this->extensionsXML
[$extkey]['versions'][$version]);
235 if(!count($this->extensionsXML
[$extkey]['versions'])) unset($this->extensionsXML
[$extkey]);
241 * ***************PARSING METHODS***********************
244 * Enter description here...
246 * @param unknown_type $parser
247 * @param unknown_type $name
248 * @param unknown_type $attrs
251 function startElement($parser, $name, $attrs) {
256 $this->currentExt
= $attrs['extensionkey'];
259 $this->currentVersion
= $attrs['version'];
260 $this->extXMLResult
[$this->currentExt
]['versions'][$this->currentVersion
] = array();
263 $this->currentTag
= $name;
268 * Enter description here...
270 * @param unknown_type $parser
271 * @param unknown_type $name
274 function endElement($parser, $name) {
277 unset($this->currentExt
);
280 unset($this->currentVersion
);
283 unset($this->currentTag
);
288 * Enter description here...
290 * @param unknown_type $parser
291 * @param unknown_type $data
294 function characterData($parser, $data) {
295 if(isset($this->currentTag
)) {
296 if(!isset($this->currentVersion
) && $this->currentTag
== 'downloadcounter') {
297 $this->extXMLResult
[$this->currentExt
]['downloadcounter'] = trim($data);
298 } elseif($this->currentTag
== 'dependencies') {
299 $data = @unserialize
($data);
300 if(is_array($data)) {
302 foreach($data as $v) {
303 $dep[$v['kind']][$v['extensionKey']] = $v['versionRange'];
305 $this->extXMLResult
[$this->currentExt
]['versions'][$this->currentVersion
]['dependencies'] = $dep;
307 } elseif($this->currentTag
== 'reviewstate') {
308 $this->reviewStates
[$this->currentExt
][$this->currentVersion
] = (int)trim($data);
309 $this->extXMLResult
[$this->currentExt
]['versions'][$this->currentVersion
]['reviewstate'] = (int)trim($data);
311 $this->extXMLResult
[$this->currentExt
]['versions'][$this->currentVersion
][$this->currentTag
] .= trim($data);
317 * Parses content of mirrors.xml into a suitable array
319 * @param string XML data file to parse
320 * @return string HTLML output informing about result
322 function parseExtensionsXML($filename) {
324 $parser = xml_parser_create();
325 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING
, 0);
326 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE
, 0);
327 xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING
, 'utf-8');
328 xml_set_element_handler($parser, array(&$this,'startElement'), array(&$this,'endElement'));
329 xml_set_character_data_handler($parser, array(&$this,'characterData'));
331 $fp = gzopen($filename, 'rb');
333 $content.= 'Error opening XML extension file "'.$filename.'"';
336 $string = gzread($fp, 0xffff); // Read 64KB
338 $this->revCatArr
= array();
340 foreach ($this->emObj
->defaultCategories
['cat'] as $catKey => $tmp) {
341 $this->revCatArr
[$catKey] = $idx++
;
344 $this->revStateArr
= array();
346 foreach ($this->emObj
->states
as $state => $tmp) {
347 $this->revStateArr
[$state] = $idx++
;
350 $GLOBALS['TYPO3_DB']->exec_DELETEquery('cache_extensions', '1=1');
353 @ini_set
('pcre.backtrack_limit', 500000);
355 if (preg_match('/.*(<extension\s+extensionkey="[^"]+">.*<\/extension>)/suU', $string, $match)) {
357 if (!xml_parse($parser, $match[0], 0)) {
358 $content.= 'Error in XML parser while decoding extensions XML file. Line '.xml_get_current_line_number($parser).': '.xml_error_string(xml_get_error_code($parser));
362 $this->storeXMLResult();
363 $this->extXMLResult
= array();
365 $string = substr($string, strlen($match[0]));
366 } elseif(function_exists('preg_last_error') && preg_last_error()) {
368 0 => 'PREG_NO_ERROR',
369 1 => 'PREG_INTERNAL_ERROR',
370 2 => 'PREG_BACKTRACK_LIMIT_ERROR',
371 3 => 'PREG_RECURSION_LIMIT_ERROR',
372 4 => 'PREG_BAD_UTF8_ERROR'
374 $content.= 'Error in regular expression matching, code: '.$errorcodes[preg_last_error()].'<br />See <a href="http://www.php.net/manual/en/function.preg-last-error.php" target="_blank">http://www.php.net/manual/en/function.preg-last-error.php</a>';
378 if(gzeof($fp)) break; // Nothing more can be read
379 $string .= gzread($fp, 0xffff); // Read another 64KB
383 xml_parser_free($parser);
387 $content.= '<p>The extensions list has been updated and now contains '.$extcount.' extension entries.</p>';
393 function storeXMLResult() {
394 foreach ($this->extXMLResult
as $extkey => $extArr) {
402 $useauthorcompany = '';
405 foreach ($extArr['versions'] as $version => $vArr) {
406 $iv = $this->emObj
->makeVersion($version, 'int');
407 if ($vArr['title']&&!$usetitle) {
408 $usetitle = $vArr['title'];
410 if ($vArr['state']&&!$usestate) {
411 $usestate = $vArr['state'];
413 if ($vArr['authorcompany']&&!$useauthorcompany) {
414 $useauthorcompany = $vArr['authorcompany'];
416 if ($vArr['authorname']&&!$useauthorname) {
417 $useauthorname = $vArr['authorname'];
419 $verArr[$version] = $iv;
423 if ($vArr['title']) {
424 $usetitle = $vArr['title'];
426 if ($vArr['state']) {
427 $usestate = $vArr['state'];
429 if ($vArr['authorcompany']) {
430 $useauthorcompany = $vArr['authorcompany'];
432 if ($vArr['authorname']) {
433 $useauthorname = $vArr['authorname'];
435 $usecat = $vArr['category'];
437 if ($vArr['reviewstate'] && ($iv>$maxrev)) {
442 if (!strlen($usecat)) {
443 $usecat = 4; // Extensions without a category end up in "misc"
445 if (isset($this->revCatArr
[$usecat])) {
446 $usecat = $this->revCatArr
[$usecat];
448 $usecat = 4; // Extensions without a category end up in "misc"
451 if (isset($this->revStateArr
[$usestate])) {
452 $usestate = $this->revCatArr
[$usestate];
454 $usestate = 999; // Extensions without a category end up in "misc"
456 foreach ($extArr['versions'] as $version => $vArr) {
457 $vArr['version'] = $version;
458 $vArr['intversion'] = $verArr[$version];
459 $vArr['extkey'] = $extkey;
460 $vArr['alldownloadcounter'] = $extArr['downloadcounter'];
461 $vArr['dependencies'] = serialize($vArr['dependencies']);
462 $vArr['category'] = $usecat;
463 $vArr['title'] = $usetitle;
464 if ($version==$last) {
465 $vArr['lastversion'] = 1;
467 if ($version==$lastrev) {
468 $vArr['lastreviewedversion'] = 1;
470 $vArr['state'] = isset($this->revStateArr
[$vArr['state']])?
$this->revStateArr
[$vArr['state']]:$usestate; // 999 = not set category
471 $GLOBALS['TYPO3_DB']->exec_INSERTquery('cache_extensions', $vArr);
477 * Parses content of mirrors.xml into a suitable array
479 * @param string $string: XML data to parse
480 * @return string HTLML output informing about result
482 function parseMirrorsXML($string) {
483 global $TYPO3_CONF_VARS;
486 $parser = xml_parser_create();
490 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING
, 0);
491 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE
, 0);
493 $preg_result = array();
494 preg_match('/^[[:space:]]*<\?xml[^>]*encoding[[:space:]]*=[[:space:]]*"([^"]*)"/',substr($string,0,200),$preg_result);
495 $theCharset = $preg_result[1] ?
$preg_result[1] : ($TYPO3_CONF_VARS['BE']['forceCharset'] ?
$TYPO3_CONF_VARS['BE']['forceCharset'] : 'iso-8859-1');
496 xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING
, $theCharset); // us-ascii / utf-8 / iso-8859-1
499 xml_parse_into_struct($parser, $string, $vals, $index);
501 // If error, return error message:
502 if (xml_get_error_code($parser)) {
503 $line = xml_get_current_line_number($parser);
504 $error = xml_error_string(xml_get_error_code($parser));
505 xml_parser_free($parser);
506 return 'Error in XML parser while decoding mirrors XML file. Line '.$line.': '.$error;
509 $stack = array(array());
516 // Traverse the parsed XML structure:
517 foreach($vals as $val) {
519 // First, process the tag-name (which is used in both cases, whether "complete" or "close")
520 $tagName = ($val['tag']=='mirror' && $val['type']=='open') ?
'__plh' : $val['tag'];
521 if (!$documentTag) $documentTag = $tagName;
523 // Setting tag-values, manage stack:
524 switch($val['type']) {
525 case 'open': // If open tag it means there is an array stored in sub-elements. Therefore increase the stackpointer and reset the accumulation array:
526 $current[$tagName] = array(); // Setting blank place holder
527 $stack[$stacktop++
] = $current;
530 case 'close': // If the tag is "close" then it is an array which is closing and we decrease the stack pointer.
531 $oldCurrent = $current;
532 $current = $stack[--$stacktop];
533 end($current); // Going to the end of array to get placeholder key, key($current), and fill in array next:
534 if($tagName=='mirror') {
535 unset($current['__plh']);
536 $current[$oldCurrent['host']] = $oldCurrent;
538 $current[key($current)] = $oldCurrent;
542 case 'complete': // If "complete", then it's a value. If the attribute "base64" is set, then decode the value, otherwise just set it.
543 $current[$tagName] = (string)$val['value']; // Had to cast it as a string - otherwise it would be evaluate false if tested with isset()!!
547 return $current[$tagName];
552 * Parses content of *-l10n.xml into a suitable array
554 * @param string $string: XML data to parse
555 * @return array Array representation of XML data
557 function parseL10nXML($string) {
559 $parser = xml_parser_create();
563 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING
, 0);
564 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE
, 0);
567 xml_parse_into_struct($parser, $string, $vals, $index);
569 // If error, return error message:
570 if (xml_get_error_code($parser)) {
571 $line = xml_get_current_line_number($parser);
572 $error = xml_error_string(xml_get_error_code($parser));
574 xml_parser_free($parser);
575 return 'Error in XML parser while decoding l10n XML file. Line '.$line.': '.$error;
578 $stack = array(array());
585 // Traverse the parsed XML structure:
586 foreach($vals as $val) {
588 // First, process the tag-name (which is used in both cases, whether "complete" or "close")
589 $tagName = ($val['tag']=='languagepack' && $val['type']=='open') ?
$val['attributes']['language'] : $val['tag'];
590 if (!$documentTag) $documentTag = $tagName;
592 // Setting tag-values, manage stack:
593 switch($val['type']) {
594 case 'open': // If open tag it means there is an array stored in sub-elements. Therefore increase the stackpointer and reset the accumulation array:
595 $current[$tagName] = array(); // Setting blank place holder
596 $stack[$stacktop++
] = $current;
599 case 'close': // If the tag is "close" then it is an array which is closing and we decrease the stack pointer.
600 $oldCurrent = $current;
601 $current = $stack[--$stacktop];
602 end($current); // Going to the end of array to get placeholder key, key($current), and fill in array next:
603 $current[key($current)] = $oldCurrent;
606 case 'complete': // If "complete", then it's a value. If the attribute "base64" is set, then decode the value, otherwise just set it.
607 $current[$tagName] = (string)$val['value']; // Had to cast it as a string - otherwise it would be evaluate false if tested with isset()!!
611 return $current[$tagName];