[ Index ]

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


Generated: Sun Feb 19 19:47:08 2017 Cross-referenced by PHPXref 0.7.1