PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/pastor399/newcastleunifc
PHP | 232 lines | 146 code | 17 blank | 69 comment | 29 complexity | 351dd3a4da124eaaa01dc8546e187b72 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Updater
  5. *
  6. * @copyright Copyright (C) 2005 - 2013 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. * Start element parser callback.
  22. *
  23. * @param object $parser The parser object.
  24. * @param string $name The name of the element.
  25. * @param array $attrs The attributes of the element.
  26. *
  27. * @return void
  28. *
  29. * @since 11.1
  30. */
  31. protected function _startElement($parser, $name, $attrs = array())
  32. {
  33. array_push($this->stack, $name);
  34. $tag = $this->_getStackLocation();
  35. // Reset the data
  36. if (isset($this->$tag))
  37. {
  38. $this->$tag->_data = "";
  39. }
  40. switch ($name)
  41. {
  42. case 'UPDATE':
  43. $this->current_update = JTable::getInstance('update');
  44. $this->current_update->update_site_id = $this->updateSiteId;
  45. $this->current_update->detailsurl = $this->_url;
  46. $this->current_update->folder = "";
  47. $this->current_update->client_id = 1;
  48. break;
  49. // Don't do anything
  50. case 'UPDATES':
  51. break;
  52. default:
  53. if (in_array($name, $this->updatecols))
  54. {
  55. $name = strtolower($name);
  56. $this->current_update->$name = '';
  57. }
  58. if ($name == 'TARGETPLATFORM')
  59. {
  60. $this->current_update->targetplatform = $attrs;
  61. }
  62. break;
  63. }
  64. }
  65. /**
  66. * Character Parser Function
  67. *
  68. * @param object $parser Parser object.
  69. * @param object $name The name of the element.
  70. *
  71. * @return void
  72. *
  73. * @since 11.1
  74. */
  75. protected function _endElement($parser, $name)
  76. {
  77. array_pop($this->stack);
  78. // @todo remove code: echo 'Closing: '. $name .'<br />';
  79. switch ($name)
  80. {
  81. case 'UPDATE':
  82. $ver = new JVersion;
  83. // Lower case and remove the exclamation mark
  84. $product = strtolower(JFilterInput::getInstance()->clean($ver->PRODUCT, 'cmd'));
  85. // Check that the product matches and that the version matches (optionally a regexp)
  86. // Check for optional min_dev_level and max_dev_level attributes to further specify targetplatform (e.g., 3.0.1)
  87. if ($product == $this->current_update->targetplatform['NAME']
  88. && preg_match('/' . $this->currentUpdate->targetplatform->version . '/', $ver->RELEASE)
  89. && ((!isset($this->currentUpdate->targetplatform->min_dev_level)) || $ver->DEV_LEVEL >= $this->currentUpdate->targetplatform->min_dev_level)
  90. && ((!isset($this->currentUpdate->targetplatform->max_dev_level)) || $ver->DEV_LEVEL <= $this->currentUpdate->targetplatform->max_dev_level))
  91. {
  92. // Target platform isn't a valid field in the update table so unset it to prevent J! from trying to store it
  93. unset($this->current_update->targetplatform);
  94. if (isset($this->latest))
  95. {
  96. if (version_compare($this->current_update->version, $this->latest->version, '>') == 1)
  97. {
  98. $this->latest = $this->current_update;
  99. }
  100. }
  101. else
  102. {
  103. $this->latest = $this->current_update;
  104. }
  105. }
  106. break;
  107. case 'UPDATES':
  108. // :D
  109. break;
  110. }
  111. }
  112. /**
  113. * Character Parser Function
  114. *
  115. * @param object $parser Parser object.
  116. * @param object $data The data.
  117. *
  118. * @return void
  119. *
  120. * @note This is public because its called externally.
  121. * @since 11.1
  122. */
  123. protected function _characterData($parser, $data)
  124. {
  125. $tag = $this->_getLastTag();
  126. /**
  127. * @todo remove code
  128. * if(!isset($this->$tag->_data)) $this->$tag->_data = '';
  129. * $this->$tag->_data .= $data;
  130. */
  131. if (in_array($tag, $this->updatecols))
  132. {
  133. $tag = strtolower($tag);
  134. $this->current_update->$tag .= $data;
  135. }
  136. }
  137. /**
  138. * Finds an update.
  139. *
  140. * @param array $options Update options.
  141. *
  142. * @return array Array containing the array of update sites and array of updates
  143. *
  144. * @since 11.1
  145. */
  146. public function findUpdate($options)
  147. {
  148. $url = $options['location'];
  149. $this->_url = &$url;
  150. $this->updateSiteId = $options['update_site_id'];
  151. if (substr($url, -4) != '.xml')
  152. {
  153. if (substr($url, -1) != '/')
  154. {
  155. $url .= '/';
  156. }
  157. $url .= 'extension.xml';
  158. }
  159. $db = $this->parent->getDBO();
  160. $http = JHttpFactory::getHttp();
  161. $response = $http->get($url);
  162. if (!empty($response->code) && 200 != $response->code)
  163. {
  164. $query = $db->getQuery(true)
  165. ->update('#__update_sites')
  166. ->set('enabled = 0')
  167. ->where('update_site_id = ' . $this->updateSiteId);
  168. $db->setQuery($query);
  169. $db->execute();
  170. JLog::add("Error opening url: " . $url, JLog::WARNING, 'updater');
  171. $app = JFactory::getApplication();
  172. $app->enqueueMessage(JText::sprintf('JLIB_UPDATER_ERROR_EXTENSION_OPEN_URL', $url), 'warning');
  173. return false;
  174. }
  175. $this->xmlParser = xml_parser_create('');
  176. xml_set_object($this->xmlParser, $this);
  177. xml_set_element_handler($this->xmlParser, '_startElement', '_endElement');
  178. xml_set_character_data_handler($this->xmlParser, '_characterData');
  179. if (!xml_parse($this->xmlParser, $response->body))
  180. {
  181. JLog::add("Error parsing url: " . $url, JLog::WARNING, 'updater');
  182. $app = JFactory::getApplication();
  183. $app->enqueueMessage(JText::sprintf('JLIB_UPDATER_ERROR_EXTENSION_PARSE_URL', $url), 'warning');
  184. return false;
  185. }
  186. xml_parser_free($this->xmlParser);
  187. if (isset($this->latest))
  188. {
  189. if (isset($this->latest->client) && strlen($this->latest->client))
  190. {
  191. if (is_numeric($this->latest->client))
  192. {
  193. $byName = false;
  194. // <client> has to be 'administrator' or 'site', numeric values are depreceated. See http://docs.joomla.org/Design_of_JUpdate
  195. JLog::add(
  196. 'Using numeric values for <client> in the updater xml is deprecated. Use \'administrator\' or \'site\' instead.',
  197. JLog::WARNING, 'deprecated'
  198. );
  199. }
  200. else
  201. {
  202. $byName = true;
  203. }
  204. $this->latest->client_id = JApplicationHelper::getClientInfo($this->latest->client, $byName)->id;
  205. unset($this->latest->client);
  206. }
  207. $updates = array($this->latest);
  208. }
  209. else
  210. {
  211. $updates = array();
  212. }
  213. return array('update_sites' => array(), 'updates' => $updates);
  214. }
  215. }