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