PageRenderTime 22ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 1ms

/libraries/joomla/updater/adapters/collection.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 264 lines | 137 code | 23 blank | 104 comment | 21 complexity | be6f25ee96a079c52e2815111c3ba755 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. * Collection Update Adapter Class
  13. *
  14. * @package Joomla.Platform
  15. * @subpackage Updater
  16. * @since 11.1
  17. */
  18. class JUpdaterCollection extends JUpdateAdapter
  19. {
  20. /**
  21. * Root of the tree
  22. *
  23. * @var object
  24. * @since 11.1
  25. */
  26. protected $base;
  27. /**
  28. * Tree of objects
  29. *
  30. * @var array
  31. * @since 11.1
  32. */
  33. protected $parent = array(0);
  34. /**
  35. * Used to control if an item has a child or not
  36. *
  37. * @var boolean
  38. * @since 11.1
  39. */
  40. protected $pop_parent = 0;
  41. /**
  42. * @var array A list of discovered update sites
  43. */
  44. protected $update_sites;
  45. /**
  46. * A list of discovered updates
  47. *
  48. * @var array
  49. */
  50. protected $updates;
  51. /**
  52. * Gets the reference to the current direct parent
  53. *
  54. * @return object
  55. *
  56. * @since 11.1
  57. */
  58. protected function _getStackLocation()
  59. {
  60. return implode('->', $this->stack);
  61. }
  62. /**
  63. * Get the parent tag
  64. *
  65. * @return string parent
  66. *
  67. * @since 11.1
  68. */
  69. protected function _getParent()
  70. {
  71. return end($this->parent);
  72. }
  73. /**
  74. * Opening an XML element
  75. *
  76. * @param object $parser Parser object
  77. * @param string $name Name of element that is opened
  78. * @param array $attrs Array of attributes for the element
  79. *
  80. * @return void
  81. *
  82. * @since 11.1
  83. */
  84. public function _startElement($parser, $name, $attrs = array())
  85. {
  86. array_push($this->stack, $name);
  87. $tag = $this->_getStackLocation();
  88. // Reset the data
  89. if (isset($this->$tag))
  90. {
  91. $this->$tag->_data = "";
  92. }
  93. switch ($name)
  94. {
  95. case 'CATEGORY':
  96. if (isset($attrs['REF']))
  97. {
  98. $this->update_sites[] = array('type' => 'collection', 'location' => $attrs['REF'], 'update_site_id' => $this->updateSiteId);
  99. }
  100. else
  101. {
  102. // This item will have children, so prepare to attach them
  103. $this->pop_parent = 1;
  104. }
  105. break;
  106. case 'EXTENSION':
  107. $update = JTable::getInstance('update');
  108. $update->set('update_site_id', $this->updateSiteId);
  109. foreach ($this->updatecols as $col)
  110. {
  111. // Reset the values if it doesn't exist
  112. if (!array_key_exists($col, $attrs))
  113. {
  114. $attrs[$col] = '';
  115. if ($col == 'CLIENT')
  116. {
  117. $attrs[$col] = 'site';
  118. }
  119. }
  120. }
  121. $client = JApplicationHelper::getClientInfo($attrs['CLIENT'], 1);
  122. if (isset($client->id))
  123. {
  124. $attrs['CLIENT_ID'] = $client->id;
  125. }
  126. // Lower case all of the fields
  127. foreach ($attrs as $key => $attr)
  128. {
  129. $values[strtolower($key)] = $attr;
  130. }
  131. // Only add the update if it is on the same platform and release as we are
  132. $ver = new JVersion;
  133. // Lower case and remove the exclamation mark
  134. $product = strtolower(JFilterInput::getInstance()->clean($ver->PRODUCT, 'cmd'));
  135. /*
  136. * Set defaults, the extension file should clarify in case but it may be only available in one version
  137. * This allows an update site to specify a targetplatform
  138. * targetplatformversion can be a regexp, so 1.[56] would be valid for an extension that supports 1.5 and 1.6
  139. * Note: Whilst the version is a regexp here, the targetplatform is not (new extension per platform)
  140. * Additionally, the version is a regexp here and it may also be in an extension file if the extension is
  141. * compatible against multiple versions of the same platform (e.g. a library)
  142. */
  143. if (!isset($values['targetplatform']))
  144. {
  145. $values['targetplatform'] = $product;
  146. }
  147. // Set this to ourself as a default
  148. if (!isset($values['targetplatformversion']))
  149. {
  150. $values['targetplatformversion'] = $ver->RELEASE;
  151. }
  152. // Set this to ourself as a default
  153. // validate that we can install the extension
  154. if ($product == $values['targetplatform'] && preg_match('/' . $values['targetplatformversion'] . '/', $ver->RELEASE))
  155. {
  156. $update->bind($values);
  157. $this->updates[] = $update;
  158. }
  159. break;
  160. }
  161. }
  162. /**
  163. * Closing an XML element
  164. * Note: This is a protected function though has to be exposed externally as a callback
  165. *
  166. * @param object $parser Parser object
  167. * @param string $name Name of the element closing
  168. *
  169. * @return void
  170. *
  171. * @since 11.1
  172. */
  173. protected function _endElement($parser, $name)
  174. {
  175. array_pop($this->stack);
  176. switch ($name)
  177. {
  178. case 'CATEGORY':
  179. if ($this->pop_parent)
  180. {
  181. $this->pop_parent = 0;
  182. array_pop($this->parent);
  183. }
  184. break;
  185. }
  186. }
  187. // Note: we don't care about char data in collection because there should be none
  188. /**
  189. * Finds an update
  190. *
  191. * @param array $options Options to use: update_site_id: the unique ID of the update site to look at
  192. *
  193. * @return array Update_sites and updates discovered
  194. *
  195. * @since 11.1
  196. */
  197. public function findUpdate($options)
  198. {
  199. $url = $options['location'];
  200. $this->updateSiteId = $options['update_site_id'];
  201. if (substr($url, -4) != '.xml')
  202. {
  203. if (substr($url, -1) != '/')
  204. {
  205. $url .= '/';
  206. }
  207. $url .= 'update.xml';
  208. }
  209. $this->base = new stdClass;
  210. $this->update_sites = array();
  211. $this->updates = array();
  212. $db = $this->parent->getDBO();
  213. $http = JHttpFactory::getHttp();
  214. $response = $http->get($url);
  215. if (200 != $response->code)
  216. {
  217. $query = $db->getQuery(true)
  218. ->update('#__update_sites')
  219. ->set('enabled = 0')
  220. ->where('update_site_id = ' . $this->updateSiteId);
  221. $db->setQuery($query);
  222. $db->execute();
  223. JLog::add("Error parsing url: " . $url, JLog::WARNING, 'updater');
  224. $app = JFactory::getApplication();
  225. $app->enqueueMessage(JText::sprintf('JLIB_UPDATER_ERROR_COLLECTION_OPEN_URL', $url), 'warning');
  226. return false;
  227. }
  228. $this->xmlParser = xml_parser_create('');
  229. xml_set_object($this->xmlParser, $this);
  230. xml_set_element_handler($this->xmlParser, '_startElement', '_endElement');
  231. if (!xml_parse($this->xmlParser, $response->body))
  232. {
  233. JLog::add("Error parsing url: " . $url, JLog::WARNING, 'updater');
  234. $app = JFactory::getApplication();
  235. $app->enqueueMessage(JText::sprintf('JLIB_UPDATER_ERROR_COLLECTION_PARSE_URL', $url), 'warning');
  236. return false;
  237. }
  238. // TODO: Decrement the bad counter if non-zero
  239. return array('update_sites' => $this->update_sites, 'updates' => $this->updates);
  240. }
  241. }