/libraries/joomla/updater/adapters/extension.php

https://github.com/mathc/joomla-cms · PHP · 159 lines · 100 code · 10 blank · 49 comment · 20 complexity · c951f33de79f985db05c18affd0d4507 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Updater
  5. *
  6. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. jimport('joomla.updater.updateadapter');
  11. /**
  12. * Extension class for updater
  13. *
  14. * @package Joomla.Platform
  15. * @subpackage Updater
  16. * @since 11.1
  17. * */
  18. class JUpdaterExtension extends JUpdateAdapter
  19. {
  20. /**
  21. *
  22. *
  23. * @param $parser
  24. * @param string $name
  25. * @param array $attrs
  26. *
  27. * @since 11.1
  28. */
  29. protected function _startElement($parser, $name, $attrs = array()) {
  30. array_push($this->_stack, $name);
  31. $tag = $this->_getStackLocation();
  32. // reset the data
  33. eval('$this->'. $tag .'->_data = "";');
  34. switch($name) {
  35. case 'UPDATE':
  36. $this->current_update = JTable::getInstance('update');
  37. $this->current_update->update_site_id = $this->_update_site_id;
  38. $this->current_update->detailsurl = $this->_url;
  39. break;
  40. // Don't do anything
  41. case 'UPDATES':
  42. break;
  43. default:
  44. if(in_array($name, $this->_updatecols)) {
  45. $name = strtolower($name);
  46. $this->current_update->$name = '';
  47. }
  48. if($name == 'TARGETPLATFORM') {
  49. $this->current_update->targetplatform = $attrs;
  50. }
  51. break;
  52. }
  53. }
  54. /**
  55. *
  56. *
  57. * @param $parser
  58. * @param string $name
  59. *
  60. * @since 11.1
  61. */
  62. protected function _endElement($parser, $name)
  63. {
  64. array_pop($this->_stack);
  65. //echo 'Closing: '. $name .'<br />';
  66. switch($name) {
  67. case 'UPDATE':
  68. $ver = new JVersion;
  69. $product = strtolower(JFilterInput::getInstance()->clean($ver->PRODUCT, 'cmd')); // lower case and remove the exclamation mark
  70. // Check that the product matches and that the version matches (optionally a regexp)
  71. if($product == $this->current_update->targetplatform['NAME'] && preg_match('/'.$this->current_update->targetplatform['VERSION'].'/',$ver->RELEASE)) {
  72. // Target platform isn't a valid field in the update table so unset it to prevent J! from trying to store it
  73. unset($this->current_update->targetplatform);
  74. if(isset($this->latest)) {
  75. if(version_compare($this->current_update->version, $this->latest->version, '>') == 1) {
  76. $this->latest = $this->current_update;
  77. }
  78. } else {
  79. $this->latest = $this->current_update;
  80. }
  81. }
  82. break;
  83. case 'UPDATES':
  84. // :D
  85. break;
  86. }
  87. }
  88. protected function _characterData($parser, $data)
  89. {
  90. $tag = $this->_getLastTag();
  91. //if(!isset($this->$tag->_data)) $this->$tag->_data = '';
  92. //$this->$tag->_data .= $data;
  93. if(in_array($tag, $this->_updatecols)) {
  94. $tag = strtolower($tag);
  95. $this->current_update->$tag .= $data;
  96. }
  97. }
  98. /**
  99. *
  100. *
  101. * @param array $options
  102. *
  103. * @return array Array containing the array of update sites and array of updates
  104. *
  105. * @since 11.1
  106. */
  107. public function findUpdate($options)
  108. {
  109. $url = $options['location'];
  110. $this->_url =& $url;
  111. $this->_update_site_id = $options['update_site_id'];
  112. //echo '<p>Find update for extension run on <a href="'. $url .'">'. $url .'</a></p>';
  113. if(substr($url, -4) != '.xml') {
  114. if(substr($url, -1) != '/') {
  115. $url .= '/';
  116. }
  117. $url .= 'extension.xml';
  118. }
  119. $dbo = $this->parent->getDBO();
  120. if (!($fp = @fopen($url, "r"))) {
  121. $query = $dbo->getQuery(true);
  122. $query->update('#__update_sites');
  123. $query->set('enabled = 0');
  124. $query->where('update_site_id = '. $this->_update_site_id);
  125. $dbo->setQuery($query);
  126. $dbo->Query();
  127. JError::raiseWarning('101', JText::sprintf('JLIB_UPDATER_ERROR_EXTENSION_OPEN_URL', $url));
  128. return false;
  129. }
  130. $this->xml_parser = xml_parser_create('');
  131. xml_set_object($this->xml_parser, $this);
  132. xml_set_element_handler($this->xml_parser, '_startElement', '_endElement');
  133. xml_set_character_data_handler($this->xml_parser, '_characterData');
  134. while ($data = fread($fp, 8192)) {
  135. if (!xml_parse($this->xml_parser, $data, feof($fp))) {
  136. die(sprintf("XML error: %s at line %d",
  137. xml_error_string(xml_get_error_code($this->xml_parser)),
  138. xml_get_current_line_number($this->xml_parser)));
  139. }
  140. }
  141. xml_parser_free($this->xml_parser);
  142. if(isset($this->latest)) {
  143. $updates = Array($this->latest);
  144. } else {
  145. $updates = Array();
  146. }
  147. return Array('update_sites'=>Array(),'updates'=>$updates);
  148. }
  149. }