/libraries/joomla/updater/update.php

https://github.com/felix4/joomla-platform · PHP · 306 lines · 124 code · 28 blank · 154 comment · 14 complexity · 5672526427df60ac409d025ad5789d69 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. /**
  11. * Update class.
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage Updater
  15. * @since 11.1
  16. */
  17. class JUpdate extends JObject
  18. {
  19. /**
  20. * @var string
  21. * @since 11.1
  22. */
  23. protected $name;
  24. /**
  25. * @var string
  26. * @since 11.1
  27. */
  28. protected $description;
  29. /**
  30. * @var string
  31. * @since 11.1
  32. */
  33. protected $element;
  34. /**
  35. * @var string
  36. * @since 11.1
  37. */
  38. protected $type;
  39. /**
  40. * @var string
  41. * @since 11.1
  42. */
  43. protected $version;
  44. /**
  45. * @var string
  46. * @since 11.1
  47. */
  48. protected $infourl;
  49. /**
  50. * @var string
  51. * @since 11.1
  52. */
  53. protected $client;
  54. /**
  55. * @var string
  56. * @since 11.1
  57. */
  58. protected $group;
  59. /**
  60. * @var string
  61. * @since 11.1
  62. */
  63. protected $downloads;
  64. /**
  65. * @var string
  66. * @since 11.1
  67. */
  68. protected $tags;
  69. /**
  70. * @var string
  71. * @since 11.1
  72. */
  73. protected $maintainer;
  74. /**
  75. * @var string
  76. * @since 11.1
  77. */
  78. protected $maintainerurl;
  79. /**
  80. * @var string
  81. * @since 11.1
  82. */
  83. protected $category;
  84. /**
  85. * @var string
  86. * @since 11.1
  87. */
  88. protected $relationships;
  89. /**
  90. * @var string
  91. * @since 11.1
  92. */
  93. protected $targetplatform;
  94. /**
  95. * @var string
  96. * @since 11.1
  97. */
  98. protected $_xml_parser;
  99. /**
  100. * @var array
  101. * @since 11.1
  102. */
  103. protected $_stack = Array('base');
  104. /**
  105. * @var array
  106. * @since 11.1
  107. */
  108. protected $_state_store = Array();
  109. /**
  110. * Gets the reference to the current direct parent
  111. *
  112. * @return object
  113. *
  114. * @since 11.1
  115. */
  116. protected function _getStackLocation()
  117. {
  118. return implode('->', $this->_stack);
  119. }
  120. /**
  121. * Get the last position in stack count
  122. *
  123. * @return string
  124. *
  125. * @since 11.1
  126. */
  127. protected function _getLastTag()
  128. {
  129. return $this->_stack[count($this->_stack) - 1];
  130. }
  131. /**
  132. * XML Start Element callback
  133. *
  134. * @param object $parser Parser object
  135. * @param string $name Name of the tag found
  136. * @param array $attrs Attributes of the tag
  137. *
  138. * @return void
  139. *
  140. * @note This is public because it is called externally
  141. * @since 11.1
  142. */
  143. public function _startElement($parser, $name, $attrs = Array())
  144. {
  145. array_push($this->_stack, $name);
  146. $tag = $this->_getStackLocation();
  147. // Reset the data
  148. eval('$this->' . $tag . '->_data = "";');
  149. switch ($name)
  150. {
  151. // This is a new update; create a current update
  152. case 'UPDATE':
  153. $this->_current_update = new stdClass;
  154. break;
  155. // Don't do anything
  156. case 'UPDATES':
  157. break;
  158. // For everything else there's...the default!
  159. default:
  160. $name = strtolower($name);
  161. $this->_current_update->$name->_data = '';
  162. foreach ($attrs as $key => $data)
  163. {
  164. $key = strtolower($key);
  165. $this->_current_update->$name->$key = $data;
  166. }
  167. break;
  168. }
  169. }
  170. /**
  171. * Callback for closing the element
  172. *
  173. * @param object $parser Parser object
  174. * @param string $name Name of element that was closed
  175. *
  176. * @return void
  177. *
  178. * @note This is public because it is called externally
  179. * @since 11.1
  180. */
  181. public function _endElement($parser, $name)
  182. {
  183. array_pop($this->_stack);
  184. switch ($name)
  185. {
  186. // Closing update, find the latest version and check
  187. case 'UPDATE':
  188. $ver = new JVersion;
  189. $product = strtolower(JFilterInput::getInstance()->clean($ver->PRODUCT, 'cmd'));
  190. if ($product == $this->_current_update->targetplatform->name
  191. && preg_match('/' . $this->_current_update->targetplatform->version . '/', $ver->RELEASE))
  192. {
  193. if (isset($this->_latest))
  194. {
  195. if (version_compare($this->_current_update->version->_data, $this->_latest->version->_data, '>') == 1)
  196. {
  197. $this->_latest = $this->_current_update;
  198. }
  199. }
  200. else
  201. {
  202. $this->_latest = $this->_current_update;
  203. }
  204. }
  205. break;
  206. case 'UPDATES':
  207. // If the latest item is set then we transfer it to where we want to
  208. if (isset($this->_latest))
  209. {
  210. foreach (get_object_vars($this->_latest) as $key => $val)
  211. {
  212. $this->$key = $val;
  213. }
  214. unset($this->_latest);
  215. unset($this->_current_update);
  216. }
  217. else if (isset($this->_current_update))
  218. {
  219. // The update might be for an older version of j!
  220. unset($this->_current_update);
  221. }
  222. break;
  223. }
  224. }
  225. /**
  226. * Character Parser Function
  227. *
  228. * @param object $parser Parser object.
  229. * @param object $data The data.
  230. *
  231. * @return void
  232. *
  233. * @note This is public because its called externally.
  234. * @since 11.1
  235. */
  236. public function _characterData($parser, $data)
  237. {
  238. $tag = $this->_getLastTag();
  239. //if(!isset($this->$tag->_data)) $this->$tag->_data = '';
  240. //$this->$tag->_data .= $data;
  241. // Throw the data for this item together
  242. $tag = strtolower($tag);
  243. $this->_current_update->$tag->_data .= $data;
  244. }
  245. /**
  246. * Loads an XML file from a URL.
  247. *
  248. * @param string $url The URL.
  249. *
  250. * @return boolean True on success
  251. *
  252. * @since 11.1
  253. */
  254. public function loadFromXML($url)
  255. {
  256. if (!($fp = @fopen($url, 'r')))
  257. {
  258. // TODO: Add a 'mark bad' setting here somehow
  259. JError::raiseWarning('101', JText::sprintf('JLIB_UPDATER_ERROR_EXTENSION_OPEN_URL', $url));
  260. return false;
  261. }
  262. $this->xml_parser = xml_parser_create('');
  263. xml_set_object($this->xml_parser, $this);
  264. xml_set_element_handler($this->xml_parser, '_startElement', '_endElement');
  265. xml_set_character_data_handler($this->xml_parser, '_characterData');
  266. while ($data = fread($fp, 8192))
  267. {
  268. if (!xml_parse($this->xml_parser, $data, feof($fp)))
  269. {
  270. die(
  271. sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->xml_parser)),
  272. xml_get_current_line_number($this->xml_parser)));
  273. }
  274. }
  275. xml_parser_free($this->xml_parser);
  276. return true;
  277. }
  278. }