Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

      (Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

version_helper.php

Zuletzt modifiziert: 09.10.2024, 12:51 - Dateigröße: 7.65 KiB


 001  <?php
 002  /**
 003  *
 004  * This file is part of the phpBB Forum Software package.
 005  *
 006  * @copyright (c) phpBB Limited <https://www.phpbb.com>
 007  * @license GNU General Public License, version 2 (GPL-2.0)
 008  *
 009  * For full copyright and license information, please see
 010  * the docs/CREDITS.txt file.
 011  *
 012  */
 013   
 014  namespace phpbb;
 015   
 016  use phpbb\exception\version_check_exception;
 017   
 018  /**
 019   * Class to handle version checking and comparison
 020   */
 021  class version_helper
 022  {
 023      /**
 024       * @var string Host
 025       */
 026      protected $host = 'version.phpbb.com';
 027   
 028      /**
 029       * @var string Path to file
 030       */
 031      protected $path = '/phpbb';
 032   
 033      /**
 034       * @var string File name
 035       */
 036      protected $file = 'versions.json';
 037   
 038      /**
 039       * @var bool Use SSL or not
 040       */
 041      protected $use_ssl = false;
 042   
 043      /**
 044       * @var string Current version installed
 045       */
 046      protected $current_version;
 047   
 048      /**
 049       * @var null|string Null to not force stability, 'unstable' or 'stable' to
 050       *                    force the corresponding stability
 051       */
 052      protected $force_stability;
 053   
 054      /** @var \phpbb\cache\service */
 055      protected $cache;
 056   
 057      /** @var \phpbb\config\config */
 058      protected $config;
 059   
 060      /** @var \phpbb\file_downloader */
 061      protected $file_downloader;
 062   
 063      /**
 064       * Constructor
 065       *
 066       * @param \phpbb\cache\service $cache
 067       * @param \phpbb\config\config $config
 068       * @param \phpbb\file_downloader $file_downloader
 069       */
 070      public function __construct(\phpbb\cache\service $cache, \phpbb\config\config $config, \phpbb\file_downloader $file_downloader)
 071      {
 072          $this->cache = $cache;
 073          $this->config = $config;
 074          $this->file_downloader = $file_downloader;
 075   
 076          if (defined('PHPBB_QA'))
 077          {
 078              $this->force_stability = 'unstable';
 079          }
 080   
 081          $this->current_version = $this->config['version'];
 082      }
 083   
 084      /**
 085       * Set location to the file
 086       *
 087       * @param string $host Host (e.g. version.phpbb.com)
 088       * @param string $path Path to file (e.g. /phpbb)
 089       * @param string $file File name (Default: versions.json)
 090       * @param bool $use_ssl Use SSL or not (Default: false)
 091       * @return version_helper
 092       */
 093      public function set_file_location($host, $path, $file = 'versions.json', $use_ssl = false)
 094      {
 095          $this->host = $host;
 096          $this->path = $path;
 097          $this->file = $file;
 098          $this->use_ssl = $use_ssl;
 099   
 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  }
 295