[ Index ]

PHP Cross Reference of phpBB-3.1.10-deutsch

title

Body

[close]

/phpbb/ -> version_helper.php (source)

   1  <?php
   2  /**
   3  *
   4  * This file is part of the phpBB Forum Software package.
   5  *
   6  * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7  * @license GNU General Public License, version 2 (GPL-2.0)
   8  *
   9  * For full copyright and license information, please see
  10  * the docs/CREDITS.txt file.
  11  *
  12  */
  13  
  14  namespace phpbb;
  15  
  16  /**
  17   * Class to handle version checking and comparison
  18   */
  19  class version_helper
  20  {
  21      /**
  22       * @var string Host
  23       */
  24      protected $host = 'version.phpbb.com';
  25  
  26      /**
  27       * @var string Path to file
  28       */
  29      protected $path = '/phpbb';
  30  
  31      /**
  32       * @var string File name
  33       */
  34      protected $file = 'versions.json';
  35  
  36      /**
  37       * @var bool Use SSL or not
  38       */
  39      protected $use_ssl = false;
  40  
  41      /**
  42       * @var string Current version installed
  43       */
  44      protected $current_version;
  45  
  46      /**
  47       * @var null|string Null to not force stability, 'unstable' or 'stable' to
  48       *                    force the corresponding stability
  49       */
  50      protected $force_stability;
  51  
  52      /** @var \phpbb\cache\service */
  53      protected $cache;
  54  
  55      /** @var \phpbb\config\config */
  56      protected $config;
  57  
  58      /** @var \phpbb\file_downloader */
  59      protected $file_downloader;
  60  
  61      /** @var \phpbb\user */
  62      protected $user;
  63  
  64      /**
  65       * Constructor
  66       *
  67       * @param \phpbb\cache\service $cache
  68       * @param \phpbb\config\config $config
  69       * @param \phpbb\file_downloader $file_downloader
  70       * @param \phpbb\user $user
  71       */
  72  	public function __construct(\phpbb\cache\service $cache, \phpbb\config\config $config, \phpbb\file_downloader $file_downloader, \phpbb\user $user)
  73      {
  74          $this->cache = $cache;
  75          $this->config = $config;
  76          $this->file_downloader = $file_downloader;
  77          $this->user = $user;
  78  
  79          if (defined('PHPBB_QA'))
  80          {
  81              $this->force_stability = 'unstable';
  82          }
  83  
  84          $this->current_version = $this->config['version'];
  85      }
  86  
  87      /**
  88       * Set location to the file
  89       *
  90       * @param string $host Host (e.g. version.phpbb.com)
  91       * @param string $path Path to file (e.g. /phpbb)
  92       * @param string $file File name (Default: versions.json)
  93       * @param bool $use_ssl Use SSL or not (Default: false)
  94       * @return version_helper
  95       */
  96  	public function set_file_location($host, $path, $file = 'versions.json', $use_ssl = false)
  97      {
  98          $this->host = $host;
  99          $this->path = $path;
 100          $this->file = $file;
 101          $this->use_ssl = $use_ssl;
 102  
 103          return $this;
 104      }
 105  
 106      /**
 107       * Set current version
 108       *
 109       * @param string $version The current version
 110       * @return version_helper
 111       */
 112  	public function set_current_version($version)
 113      {
 114          $this->current_version = $version;
 115  
 116          return $this;
 117      }
 118  
 119      /**
 120       * Over-ride the stability to force check to include unstable versions
 121       *
 122       * @param null|string Null to not force stability, 'unstable' or 'stable' to
 123       *                         force the corresponding stability
 124       * @return version_helper
 125       */
 126  	public function force_stability($stability)
 127      {
 128          $this->force_stability = $stability;
 129  
 130          return $this;
 131      }
 132  
 133      /**
 134       * Wrapper for version_compare() that allows using uppercase A and B
 135       * for alpha and beta releases.
 136       *
 137       * See http://www.php.net/manual/en/function.version-compare.php
 138       *
 139       * @param string $version1        First version number
 140       * @param string $version2        Second version number
 141       * @param string $operator        Comparison operator (optional)
 142       *
 143       * @return mixed                Boolean (true, false) if comparison operator is specified.
 144       *                                Integer (-1, 0, 1) otherwise.
 145       */
 146  	public function compare($version1, $version2, $operator = null)
 147      {
 148          return phpbb_version_compare($version1, $version2, $operator);
 149      }
 150  
 151      /**
 152       * Check whether or not a version is "stable"
 153       *
 154       * Stable means only numbers OR a pl release
 155       *
 156       * @param string $version
 157       * @return bool Bool true or false
 158       */
 159  	public function is_stable($version)
 160      {
 161          $matches = false;
 162          preg_match('/^[\d\.]+/', $version, $matches);
 163  
 164          if (empty($matches[0]))
 165          {
 166              return false;
 167          }
 168  
 169          return $this->compare($version, $matches[0], '>=');
 170      }
 171  
 172      /**
 173      * Gets the latest version for the current branch the user is on
 174      *
 175      * @param bool $force_update Ignores cached data. Defaults to false.
 176      * @param bool $force_cache Force the use of the cache. Override $force_update.
 177      * @return string
 178      * @throws \RuntimeException
 179      */
 180  	public function get_latest_on_current_branch($force_update = false, $force_cache = false)
 181      {
 182          $versions = $this->get_versions_matching_stability($force_update, $force_cache);
 183  
 184          $self = $this;
 185          $current_version = $this->current_version;
 186  
 187          // Filter out any versions less than to the current version
 188          $versions = array_filter($versions, function($data) use ($self, $current_version) {
 189              return $self->compare($data['current'], $current_version, '>=');
 190          });
 191  
 192          // Get the lowest version from the previous list.
 193          return array_reduce($versions, function($value, $data) use ($self) {
 194              if ($value === null || $self->compare($data['current'], $value, '<'))
 195              {
 196                  return $data['current'];
 197              }
 198  
 199              return $value;
 200          });
 201      }
 202  
 203      /**
 204      * Obtains the latest version information
 205      *
 206      * @param bool $force_update Ignores cached data. Defaults to false.
 207      * @param bool $force_cache Force the use of the cache. Override $force_update.
 208      * @return string
 209      * @throws \RuntimeException
 210      */
 211  	public function get_suggested_updates($force_update = false, $force_cache = false)
 212      {
 213          $versions = $this->get_versions_matching_stability($force_update, $force_cache);
 214  
 215          $self = $this;
 216          $current_version = $this->current_version;
 217  
 218          // Filter out any versions less than or equal to the current version
 219          return array_filter($versions, function($data) use ($self, $current_version) {
 220              return $self->compare($data['current'], $current_version, '>');
 221          });
 222      }
 223  
 224      /**
 225      * Obtains the latest version information matching the stability of the current install
 226      *
 227      * @param bool $force_update Ignores cached data. Defaults to false.
 228      * @param bool $force_cache Force the use of the cache. Override $force_update.
 229      * @return string Version info
 230      * @throws \RuntimeException
 231      */
 232  	public function get_versions_matching_stability($force_update = false, $force_cache = false)
 233      {
 234          $info = $this->get_versions($force_update, $force_cache);
 235  
 236          if ($this->force_stability !== null)
 237          {
 238              return ($this->force_stability === 'unstable') ? $info['unstable'] : $info['stable'];
 239          }
 240  
 241          return ($this->is_stable($this->current_version)) ? $info['stable'] : $info['unstable'];
 242      }
 243  
 244      /**
 245      * Obtains the latest version information
 246      *
 247      * @param bool $force_update Ignores cached data. Defaults to false.
 248      * @param bool $force_cache Force the use of the cache. Override $force_update.
 249      * @return string Version info, includes stable and unstable data
 250      * @throws \RuntimeException
 251      */
 252  	public function get_versions($force_update = false, $force_cache = false)
 253      {
 254          $cache_file = '_versioncheck_' . $this->host . $this->path . $this->file . $this->use_ssl;
 255  
 256          $info = $this->cache->get($cache_file);
 257  
 258          if ($info === false && $force_cache)
 259          {
 260              throw new \RuntimeException($this->user->lang('VERSIONCHECK_FAIL'));
 261          }
 262          else if ($info === false || $force_update)
 263          {
 264              try {
 265                  $info = $this->file_downloader->get($this->host, $this->path, $this->file, $this->use_ssl ? 443 : 80);
 266              }
 267              catch (\phpbb\exception\runtime_exception $exception)
 268              {
 269                  $prepare_parameters = array_merge(array($exception->getMessage()), $exception->get_parameters());
 270                  throw new \RuntimeException(call_user_func_array(array($this->user, 'lang'), $prepare_parameters));
 271              }
 272              $error_string = $this->file_downloader->get_error_string();
 273  
 274              if (!empty($error_string))
 275              {
 276                  throw new \RuntimeException($error_string);
 277              }
 278  
 279              $info = json_decode($info, true);
 280  
 281              // Sanitize any data we retrieve from a server
 282              if (!empty($info))
 283              {
 284                  $json_sanitizer = function (&$value, $key) {
 285                      $type_cast_helper = new \phpbb\request\type_cast_helper();
 286                      $type_cast_helper->set_var($value, $value, gettype($value), true);
 287                  };
 288                  array_walk_recursive($info, $json_sanitizer);
 289              }
 290  
 291              if (empty($info['stable']) && empty($info['unstable']))
 292              {
 293                  $this->user->add_lang('acp/common');
 294  
 295                  throw new \RuntimeException($this->user->lang('VERSIONCHECK_FAIL'));
 296              }
 297  
 298              $info['stable'] = (empty($info['stable'])) ? array() : $info['stable'];
 299              $info['unstable'] = (empty($info['unstable'])) ? $info['stable'] : $info['unstable'];
 300  
 301              $this->cache->put($cache_file, $info, 86400); // 24 hours
 302          }
 303  
 304          return $info;
 305      }
 306  }


Generated: Sun Feb 19 19:52:41 2017 Cross-referenced by PHPXref 0.7.1