[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Updater/Adapter/ -> CollectionAdapter.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2008 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Updater\Adapter;
  11  
  12  use Joomla\CMS\Application\ApplicationHelper;
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Filter\InputFilter;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\CMS\Table\Table;
  17  use Joomla\CMS\Updater\UpdateAdapter;
  18  use Joomla\CMS\Version;
  19  
  20  // phpcs:disable PSR1.Files.SideEffects
  21  \defined('JPATH_PLATFORM') or die;
  22  // phpcs:enable PSR1.Files.SideEffects
  23  
  24  /**
  25   * Collection Update Adapter Class
  26   *
  27   * @since  1.7.0
  28   */
  29  class CollectionAdapter extends UpdateAdapter
  30  {
  31      /**
  32       * Root of the tree
  33       *
  34       * @var    object
  35       * @since  1.7.0
  36       */
  37      protected $base;
  38  
  39      /**
  40       * Tree of objects
  41       *
  42       * @var    array
  43       * @since  1.7.0
  44       */
  45      protected $parent = array(0);
  46  
  47      /**
  48       * Used to control if an item has a child or not
  49       *
  50       * @var    integer
  51       * @since  1.7.0
  52       */
  53      protected $pop_parent = 0;
  54  
  55      /**
  56       * A list of discovered update sites
  57       *
  58       * @var  array
  59       */
  60      protected $update_sites = array();
  61  
  62      /**
  63       * A list of discovered updates
  64       *
  65       * @var  array
  66       */
  67      protected $updates = array();
  68  
  69      /**
  70       * Gets the reference to the current direct parent
  71       *
  72       * @return  string
  73       *
  74       * @since   1.7.0
  75       */
  76      protected function _getStackLocation()
  77      {
  78          return implode('->', $this->stack);
  79      }
  80  
  81      /**
  82       * Get the parent tag
  83       *
  84       * @return  string   parent
  85       *
  86       * @since   1.7.0
  87       */
  88      protected function _getParent()
  89      {
  90          return end($this->parent);
  91      }
  92  
  93      /**
  94       * Opening an XML element
  95       *
  96       * @param   object  $parser  Parser object
  97       * @param   string  $name    Name of element that is opened
  98       * @param   array   $attrs   Array of attributes for the element
  99       *
 100       * @return  void
 101       *
 102       * @since   1.7.0
 103       */
 104      public function _startElement($parser, $name, $attrs = array())
 105      {
 106          $this->stack[] = $name;
 107          $tag           = $this->_getStackLocation();
 108  
 109          // Reset the data
 110          if (isset($this->$tag)) {
 111              $this->$tag->_data = '';
 112          }
 113  
 114          switch ($name) {
 115              case 'CATEGORY':
 116                  if (isset($attrs['REF'])) {
 117                      $this->update_sites[] = array('type' => 'collection', 'location' => $attrs['REF'], 'update_site_id' => $this->updateSiteId);
 118                  } else {
 119                      // This item will have children, so prepare to attach them
 120                      $this->pop_parent = 1;
 121                  }
 122                  break;
 123              case 'EXTENSION':
 124                  $update = Table::getInstance('update');
 125                  $update->set('update_site_id', $this->updateSiteId);
 126  
 127                  foreach ($this->updatecols as $col) {
 128                      // Reset the values if it doesn't exist
 129                      if (!\array_key_exists($col, $attrs)) {
 130                          $attrs[$col] = '';
 131  
 132                          if ($col === 'CLIENT') {
 133                              $attrs[$col] = 'site';
 134                          }
 135                      }
 136                  }
 137  
 138                  $client = ApplicationHelper::getClientInfo($attrs['CLIENT'], 1);
 139  
 140                  if (isset($client->id)) {
 141                      $attrs['CLIENT_ID'] = $client->id;
 142                  }
 143  
 144                  // Lower case all of the fields
 145                  foreach ($attrs as $key => $attr) {
 146                      $values[strtolower($key)] = $attr;
 147                  }
 148  
 149                  // Only add the update if it is on the same platform and release as we are
 150                  $ver = new Version();
 151  
 152                  // Lower case and remove the exclamation mark
 153                  $product = strtolower(InputFilter::getInstance()->clean($ver::PRODUCT, 'cmd'));
 154  
 155                  /*
 156                   * Set defaults, the extension file should clarify in case but it may be only available in one version
 157                   * This allows an update site to specify a targetplatform
 158                   * targetplatformversion can be a regexp, so 1.[56] would be valid for an extension that supports 1.5 and 1.6
 159                   * Note: Whilst the version is a regexp here, the targetplatform is not (new extension per platform)
 160                   * Additionally, the version is a regexp here and it may also be in an extension file if the extension is
 161                   * compatible against multiple versions of the same platform (e.g. a library)
 162                   */
 163                  if (!isset($values['targetplatform'])) {
 164                      $values['targetplatform'] = $product;
 165                  }
 166  
 167                  // Set this to ourself as a default
 168                  if (!isset($values['targetplatformversion'])) {
 169                      $values['targetplatformversion'] = $ver::MAJOR_VERSION . '.' . $ver::MINOR_VERSION;
 170                  }
 171  
 172                  // Set this to ourselves as a default
 173                  // validate that we can install the extension
 174                  if ($product == $values['targetplatform'] && preg_match('/^' . $values['targetplatformversion'] . '/', JVERSION)) {
 175                      $update->bind($values);
 176                      $this->updates[] = $update;
 177                  }
 178                  break;
 179          }
 180      }
 181  
 182      /**
 183       * Closing an XML element
 184       * Note: This is a protected function though has to be exposed externally as a callback
 185       *
 186       * @param   object  $parser  Parser object
 187       * @param   string  $name    Name of the element closing
 188       *
 189       * @return  void
 190       *
 191       * @since   1.7.0
 192       */
 193      protected function _endElement($parser, $name)
 194      {
 195          array_pop($this->stack);
 196  
 197          if ($name === 'CATEGORY' && $this->pop_parent) {
 198              $this->pop_parent = 0;
 199              array_pop($this->parent);
 200          }
 201      }
 202  
 203      // Note: we don't care about char data in collection because there should be none
 204  
 205      /**
 206       * Finds an update
 207       *
 208       * @param   array  $options  Options to use: update_site_id: the unique ID of the update site to look at
 209       *
 210       * @return  array|boolean  Update_sites and updates discovered. False on failure
 211       *
 212       * @since   1.7.0
 213       */
 214      public function findUpdate($options)
 215      {
 216          $response = $this->getUpdateSiteResponse($options);
 217  
 218          if ($response === false) {
 219              return false;
 220          }
 221  
 222          $this->xmlParser = xml_parser_create('');
 223          xml_set_object($this->xmlParser, $this);
 224          xml_set_element_handler($this->xmlParser, '_startElement', '_endElement');
 225  
 226          if (!xml_parse($this->xmlParser, $response->body)) {
 227              // If the URL is missing the .xml extension, try appending it and retry loading the update
 228              if (!$this->appendExtension && (substr($this->_url, -4) !== '.xml')) {
 229                  $options['append_extension'] = true;
 230  
 231                  return $this->findUpdate($options);
 232              }
 233  
 234              $app = Factory::getApplication();
 235              $app->getLogger()->warning("Error parsing url: {$this->_url}", array('category' => 'updater'));
 236              $app->enqueueMessage(Text::sprintf('JLIB_UPDATER_ERROR_COLLECTION_PARSE_URL', $this->_url), 'warning');
 237  
 238              return false;
 239          }
 240  
 241          // @todo: Decrement the bad counter if non-zero
 242          return array('update_sites' => $this->update_sites, 'updates' => $this->updates);
 243      }
 244  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer