[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/libraries/joomla/updater/ -> updater.php (source)

   1  <?php
   2  /**
   3   * @package     Joomla.Platform
   4   * @subpackage  Updater
   5   *
   6   * @copyright   Copyright (C) 2005 - 2012 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.filesystem.file');
  13  jimport('joomla.filesystem.folder');
  14  jimport('joomla.filesystem.archive');
  15  jimport('joomla.filesystem.path');
  16  jimport('joomla.base.adapter');
  17  jimport('joomla.utilities.arrayhelper');
  18  
  19  /**
  20   * Updater Class
  21   *
  22   * @package     Joomla.Platform
  23   * @subpackage  Updater
  24   * @since       11.1
  25   */
  26  class JUpdater extends JAdapter
  27  {
  28      /**
  29       * @var    JUpdater  JUpdater instance container.
  30       * @since  11.3
  31       */
  32      protected static $instance;
  33  
  34      /**
  35       * Constructor
  36       *
  37       * @since   11.1
  38       */
  39  	public function __construct()
  40      {
  41          // Adapter base path, class prefix
  42          parent::__construct(dirname(__FILE__), 'JUpdater');
  43      }
  44  
  45      /**
  46       * Returns a reference to the global Installer object, only creating it
  47       * if it doesn't already exist.
  48       *
  49       * @return  object  An installer object
  50       *
  51       * @since   11.1
  52       */
  53      public static function &getInstance()
  54      {
  55          if (!isset(self::$instance))
  56          {
  57              self::$instance = new JUpdater;
  58          }
  59          return self::$instance;
  60      }
  61  
  62      /**
  63       * Finds an update for an extension
  64       *
  65       * @param   integer  $eid           Extension Identifier; if zero use all sites
  66       * @param   integer  $cacheTimeout  How many seconds to cache update information; if zero, force reload the update information
  67       *
  68       * @return  boolean True if there are updates
  69       *
  70       * @since   11.1
  71       */
  72  	public function findUpdates($eid = 0, $cacheTimeout = 0)
  73      {
  74          // Check if fopen is allowed
  75          $result = ini_get('allow_url_fopen');
  76          if (empty($result))
  77          {
  78              JError::raiseWarning('101', JText::_('JLIB_UPDATER_ERROR_COLLECTION_FOPEN'));
  79              return false;
  80          }
  81  
  82          $dbo = $this->getDBO();
  83          $retval = false;
  84          // Push it into an array
  85          if (!is_array($eid))
  86          {
  87              $query = 'SELECT DISTINCT update_site_id, type, location, last_check_timestamp FROM #__update_sites WHERE enabled = 1';
  88          }
  89          else
  90          {
  91              $query = 'SELECT DISTINCT update_site_id, type, location, last_check_timestamp FROM #__update_sites' .
  92                  ' WHERE update_site_id IN' .
  93                  '  (SELECT update_site_id FROM #__update_sites_extensions WHERE extension_id IN (' . implode(',', $eid) . '))';
  94          }
  95          $dbo->setQuery($query);
  96          $results = $dbo->loadAssocList();
  97          $result_count = count($results);
  98          $now = time();
  99          for ($i = 0; $i < $result_count; $i++)
 100          {
 101              $result = &$results[$i];
 102              $this->setAdapter($result['type']);
 103              if (!isset($this->_adapters[$result['type']]))
 104              {
 105                  // Ignore update sites requiring adapters we don't have installed
 106                  continue;
 107              }
 108              if ($cacheTimeout > 0)
 109              {
 110                  if (isset($result['last_check_timestamp']) && ($now - $result['last_check_timestamp'] <= $cacheTimeout))
 111                  {
 112                      // Ignore update sites whose information we have fetched within
 113                      // the cache time limit
 114                      $retval = true;
 115                      continue;
 116                  }
 117              }
 118              $update_result = $this->_adapters[$result['type']]->findUpdate($result);
 119              if (is_array($update_result))
 120              {
 121                  if (array_key_exists('update_sites', $update_result) && count($update_result['update_sites']))
 122                  {
 123                      $results = JArrayHelper::arrayUnique(array_merge($results, $update_result['update_sites']));
 124                      $result_count = count($results);
 125                  }
 126                  if (array_key_exists('updates', $update_result) && count($update_result['updates']))
 127                  {
 128                      for ($k = 0, $count = count($update_result['updates']); $k < $count; $k++)
 129                      {
 130                          $current_update = &$update_result['updates'][$k];
 131                          $update = JTable::getInstance('update');
 132                          $extension = JTable::getInstance('extension');
 133                          $uid = $update
 134                              ->find(
 135                              array(
 136                                  'element' => strtolower($current_update->get('element')), 'type' => strtolower($current_update->get('type')),
 137                                  'client_id' => strtolower($current_update->get('client_id')),
 138                                  'folder' => strtolower($current_update->get('folder'))
 139                              )
 140                          );
 141  
 142                          $eid = $extension
 143                              ->find(
 144                              array(
 145                                  'element' => strtolower($current_update->get('element')), 'type' => strtolower($current_update->get('type')),
 146                                  'client_id' => strtolower($current_update->get('client_id')),
 147                                  'folder' => strtolower($current_update->get('folder'))
 148                              )
 149                          );
 150                          if (!$uid)
 151                          {
 152                              // Set the extension id
 153                              if ($eid)
 154                              {
 155                                  // We have an installed extension, check the update is actually newer
 156                                  $extension->load($eid);
 157                                  $data = json_decode($extension->manifest_cache, true);
 158                                  if (version_compare($current_update->version, $data['version'], '>') == 1)
 159                                  {
 160                                      $current_update->extension_id = $eid;
 161                                      $current_update->store();
 162                                  }
 163                              }
 164                              else
 165                              {
 166                                  // A potentially new extension to be installed
 167                                  $current_update->store();
 168                              }
 169                          }
 170                          else
 171                          {
 172                              $update->load($uid);
 173                              // if there is an update, check that the version is newer then replaces
 174                              if (version_compare($current_update->version, $update->version, '>') == 1)
 175                              {
 176                                  $current_update->store();
 177                              }
 178                          }
 179                      }
 180                  }
 181                  $update_result = true;
 182              }
 183              elseif ($retval)
 184              {
 185                  $update_result = true;
 186              }
 187  
 188              // Finally, update the last update check timestamp
 189              $query = $dbo->getQuery(true);
 190              $query->update($dbo->quoteName('#__update_sites'));
 191              $query->set($dbo->quoteName('last_check_timestamp') . ' = ' . $dbo->quote($now));
 192              $query->where($dbo->quoteName('update_site_id') . ' = ' . $dbo->quote($result['update_site_id']));
 193              $dbo->setQuery($query);
 194              $dbo->query();
 195          }
 196          return $retval;
 197      }
 198  
 199      /**
 200       * Multidimensional array safe unique test
 201       *
 202       * @param   array  $myArray  The source array.
 203       *
 204       * @return  array
 205       *
 206       * @deprecated    12.1
 207       * @note    Use JArrayHelper::arrayUnique() instead.
 208       * @note    Borrowed from PHP.net
 209       * @see     http://au2.php.net/manual/en/function.array-unique.php
 210       * @since   11.1
 211       *
 212       */
 213  	public function arrayUnique($myArray)
 214      {
 215          JLog::add('JUpdater::arrayUnique() is deprecated. See JArrayHelper::arrayUnique() . ', JLog::WARNING, 'deprecated');
 216          return JArrayHelper::arrayUnique($myArray);
 217      }
 218  
 219      /**
 220       * Finds an update for an extension
 221       *
 222       * @param   integer  $id  Id of the extension
 223       *
 224       * @return  mixed
 225       *
 226       * @since   11.1
 227       */
 228  	public function update($id)
 229      {
 230          $updaterow = JTable::getInstance('update');
 231          $updaterow->load($id);
 232          $update = new JUpdate;
 233          if ($update->loadFromXML($updaterow->detailsurl))
 234          {
 235              return $update->install();
 236          }
 237          return false;
 238      }
 239  }


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1