From 6c8589775b8df2b6fbeffbe594d9279ae90e85ba Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Wed, 19 Feb 2014 16:11:40 -0600 Subject: [ticket/9871] Update version check file to use json format PHPBB3-9871 --- phpBB/phpbb/version_helper.php | 249 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 249 insertions(+) create mode 100644 phpBB/phpbb/version_helper.php (limited to 'phpBB/phpbb') diff --git a/phpBB/phpbb/version_helper.php b/phpBB/phpbb/version_helper.php new file mode 100644 index 0000000000..df577c5dff --- /dev/null +++ b/phpBB/phpbb/version_helper.php @@ -0,0 +1,249 @@ +cache = $cache; + $this->config = $config; + $this->user = $user; + } + + /** + * Wrapper for version_compare() that allows using uppercase A and B + * for alpha and beta releases. + * + * See http://www.php.net/manual/en/function.version-compare.php + * + * @param string $version1 First version number + * @param string $version2 Second version number + * @param string $operator Comparison operator (optional) + * + * @return mixed Boolean (true, false) if comparison operator is specified. + * Integer (-1, 0, 1) otherwise. + */ + public function compare($version1, $version2, $operator = null) + { + $version1 = strtolower($version1); + $version2 = strtolower($version2); + + if (is_null($operator)) + { + return version_compare($version1, $version2); + } + else + { + return version_compare($version1, $version2, $operator); + } + } + + /** + * Check whether or not a version is "stable" + * + * Stable means only numbers OR a pl release + * + * @param string $version + * @return bool Bool true or false + */ + public function is_stable($version) + { + $matches = false; + preg_match('/^[\d\.]+/', $version, $matches); + + if (empty($matches[0])) + { + return false; + } + + return $this->compare($version, $matches[0], '>='); + } + + /** + * Gets the latest version for the current branch the user is on + * + * @param bool $force_update Ignores cached data. Defaults to false. + * @return string + * @throws \RuntimeException + */ + public function get_latest_on_current_branch($force_update = false) + { + $versions = $this->get_versions_matching_stability($force_update); + + $self = $this; + $current_version = $this->config['version']; + + // Filter out any versions less than to the current version + $versions = array_filter($versions, function($data) use ($self, $current_version) { + return $self->compare($data['current'], $current_version, '>='); + }); + + // Get the lowest version from the previous list. + return array_reduce($versions, function($value, $data) use ($self) { + if ($value === null || $self->compare($data['current'], $value, '<')) + { + return $data['current']; + } + + return $value; + }); + } + + /** + * Obtains the latest version information + * + * @param bool $force_update Ignores cached data. Defaults to false. + * @return string + * @throws \RuntimeException + */ + public function get_suggested_updates($force_update = false) + { + $versions = $this->get_versions_matching_stability($force_update); + + $self = $this; + $current_version = $this->config['version']; + + // Filter out any versions less than or equal to the current version + return array_filter($versions, function($data) use ($self, $current_version) { + return $self->compare($data['current'], $current_version, '>'); + }); + } + + /** + * Obtains the latest version information matching the stability of the current install + * + * @param bool $force_update Ignores cached data. Defaults to false. + * @return string Version info + * @throws \RuntimeException + */ + public function get_versions_matching_stability($force_update = false) + { + $info = $this->get_versions($force_update); + + return ($this->is_stable($this->config['version']) && !defined('PHPBB_QA')) ? $info['stable'] : $info['unstable']; + } + + /** + * Obtains the latest version information + * + * @param bool $force_update Ignores cached data. Defaults to false. + * @return string Version info, includes stable and unstable data + * @throws \RuntimeException + */ + public function get_versions($force_update = false) + { + $info = $this->cache->get('versioncheck'); + + if ($info === false || $force_update) + { + $info = $this->get_remote_file('version.phpbb.com', '/phpbb', 'versions.json'); + + $info = json_decode($info, true); + + if (empty($info['stable']) || empty($info['unstable'])) + { + $this->user->add_lang('acp/common'); + + throw new \RuntimeException($this->user->lang('VERSIONCHECK_FAIL')); + } + + // Replace & with & on announcement links + foreach ($info as $stability => $branches) + { + foreach ($branches as $branch => $branch_data) + { + $info[$stability][$branch]['announcement'] = str_replace('&', '&', $branch_data['announcement']); + } + } + + $this->cache->put('versioncheck', $info, 86400); // 24 hours + } + + return $info; + } + + /** + * Get remote file + * + * @param string $host Host, e.g. version.phpbb.com + * @param string $directory Directory, e.g. /phpbb + * @param string $filename Filename, e.g. versions.json + * @param int $port Port + * @param int $timeout Timeout (seconds) + * @return string Remote file contents + * @throws \RuntimeException + */ + public function get_remote_file($host, $directory, $filename, $port = 80, $timeout = 6) + { + $errstr = $errno = false; + + if ($fsock = @fsockopen($host, $port, $errno, $errstr, $timeout)) + { + @fputs($fsock, "GET $directory/$filename HTTP/1.0\r\n"); + @fputs($fsock, "HOST: $host\r\n"); + @fputs($fsock, "Connection: close\r\n\r\n"); + + $timer_stop = time() + $timeout; + stream_set_timeout($fsock, $timeout); + + $file_info = ''; + $get_info = false; + + while (!@feof($fsock)) + { + if ($get_info) + { + $file_info .= @fread($fsock, 1024); + } + else + { + $line = @fgets($fsock, 1024); + if ($line == "\r\n") + { + $get_info = true; + } + else if (stripos($line, '404 not found') !== false) + { + throw new \RuntimeException($this->user->lang('FILE_NOT_FOUND') . ': ' . $filename); + } + } + + $stream_meta_data = stream_get_meta_data($fsock); + + if (!empty($stream_meta_data['timed_out']) || time() >= $timer_stop) + { + throw new \RuntimeException($this->user->lang('FSOCK_TIMEOUT')); + } + } + @fclose($fsock); + } + else + { + if ($errstr) + { + throw new \RuntimeException(utf8_convert_message($errstr)); + } + else + { + throw new \RuntimeException($this->user->lang('FSOCK_DISABLED')); + } + } + + return $file_info; + } +} -- cgit v1.2.1 From 8748032866293129b881f9b7f3f0173c1e3a646b Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Wed, 5 Mar 2014 19:07:39 -0600 Subject: [ticket/9871] Restore get_remote_file PHPBB3-9871 --- phpBB/phpbb/version_helper.php | 78 ++++-------------------------------------- 1 file changed, 7 insertions(+), 71 deletions(-) (limited to 'phpBB/phpbb') diff --git a/phpBB/phpbb/version_helper.php b/phpBB/phpbb/version_helper.php index df577c5dff..f9db731415 100644 --- a/phpBB/phpbb/version_helper.php +++ b/phpBB/phpbb/version_helper.php @@ -151,7 +151,13 @@ class version_helper if ($info === false || $force_update) { - $info = $this->get_remote_file('version.phpbb.com', '/phpbb', 'versions.json'); + $errstr = $errno = ''; + $info = get_remote_file('version.phpbb.com', '/phpbb', 'versions.json', $errstr, $errno); + + if (!empty($errstr)) + { + throw new \RuntimeException($errstr); + } $info = json_decode($info, true); @@ -176,74 +182,4 @@ class version_helper return $info; } - - /** - * Get remote file - * - * @param string $host Host, e.g. version.phpbb.com - * @param string $directory Directory, e.g. /phpbb - * @param string $filename Filename, e.g. versions.json - * @param int $port Port - * @param int $timeout Timeout (seconds) - * @return string Remote file contents - * @throws \RuntimeException - */ - public function get_remote_file($host, $directory, $filename, $port = 80, $timeout = 6) - { - $errstr = $errno = false; - - if ($fsock = @fsockopen($host, $port, $errno, $errstr, $timeout)) - { - @fputs($fsock, "GET $directory/$filename HTTP/1.0\r\n"); - @fputs($fsock, "HOST: $host\r\n"); - @fputs($fsock, "Connection: close\r\n\r\n"); - - $timer_stop = time() + $timeout; - stream_set_timeout($fsock, $timeout); - - $file_info = ''; - $get_info = false; - - while (!@feof($fsock)) - { - if ($get_info) - { - $file_info .= @fread($fsock, 1024); - } - else - { - $line = @fgets($fsock, 1024); - if ($line == "\r\n") - { - $get_info = true; - } - else if (stripos($line, '404 not found') !== false) - { - throw new \RuntimeException($this->user->lang('FILE_NOT_FOUND') . ': ' . $filename); - } - } - - $stream_meta_data = stream_get_meta_data($fsock); - - if (!empty($stream_meta_data['timed_out']) || time() >= $timer_stop) - { - throw new \RuntimeException($this->user->lang('FSOCK_TIMEOUT')); - } - } - @fclose($fsock); - } - else - { - if ($errstr) - { - throw new \RuntimeException(utf8_convert_message($errstr)); - } - else - { - throw new \RuntimeException($this->user->lang('FSOCK_DISABLED')); - } - } - - return $file_info; - } } -- cgit v1.2.1 From 43074a1b6938f3efb2b9282a25cf7beb3b1e14a6 Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Wed, 5 Mar 2014 19:09:47 -0600 Subject: [ticket/9871] Restore phpbb_version_compare PHPBB3-9871 --- phpBB/phpbb/version_helper.php | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) (limited to 'phpBB/phpbb') diff --git a/phpBB/phpbb/version_helper.php b/phpBB/phpbb/version_helper.php index f9db731415..54c56a1b7f 100644 --- a/phpBB/phpbb/version_helper.php +++ b/phpBB/phpbb/version_helper.php @@ -40,17 +40,7 @@ class version_helper */ public function compare($version1, $version2, $operator = null) { - $version1 = strtolower($version1); - $version2 = strtolower($version2); - - if (is_null($operator)) - { - return version_compare($version1, $version2); - } - else - { - return version_compare($version1, $version2, $operator); - } + return phpbb_version_compare($version1, $version2, $operator); } /** -- cgit v1.2.1 From 98542547e2b0416f0f11bc1d75e5d999ae0d9f2b Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Wed, 5 Mar 2014 19:18:54 -0600 Subject: [ticket/9871] Typehint and comment on var types PHPBB3-9871 --- phpBB/phpbb/version_helper.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'phpBB/phpbb') diff --git a/phpBB/phpbb/version_helper.php b/phpBB/phpbb/version_helper.php index 54c56a1b7f..6f2fd0c732 100644 --- a/phpBB/phpbb/version_helper.php +++ b/phpBB/phpbb/version_helper.php @@ -14,11 +14,16 @@ namespace phpbb; */ class version_helper { + /** @var \phpbb\cache\service */ protected $cache; + + /** @var \phpbb\config\config */ protected $config; + + /** @var \phpbb\user */ protected $user; - public function __construct($cache, $config, $user) + public function __construct(\phpbb\cache\service $cache, \phpbb\config\config $config, \phpbb\user $user) { $this->cache = $cache; $this->config = $config; -- cgit v1.2.1 From 00d86a4af1adc4d34955d0432ef514d8c25942c9 Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Mon, 10 Mar 2014 21:26:46 -0500 Subject: [ticket/9871] Allow setting the host/file to load for the version class PHPBB3-9871 --- phpBB/phpbb/version_helper.php | 47 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) (limited to 'phpBB/phpbb') diff --git a/phpBB/phpbb/version_helper.php b/phpBB/phpbb/version_helper.php index 6f2fd0c732..d7bc09182e 100644 --- a/phpBB/phpbb/version_helper.php +++ b/phpBB/phpbb/version_helper.php @@ -14,6 +14,21 @@ namespace phpbb; */ class version_helper { + /** + * @var string Host + */ + protected $host = 'version.phpbb.com'; + + /** + * @var string Path to file + */ + protected $path = '/phpbb'; + + /** + * @var string File name + */ + protected $file = 'versions.json'; + /** @var \phpbb\cache\service */ protected $cache; @@ -23,6 +38,13 @@ class version_helper /** @var \phpbb\user */ protected $user; + /** + * Constructor + * + * @param \phpbb\cache\service $cache + * @param \phpbb\config\config $config + * @param \phpbb\user $user + */ public function __construct(\phpbb\cache\service $cache, \phpbb\config\config $config, \phpbb\user $user) { $this->cache = $cache; @@ -30,6 +52,23 @@ class version_helper $this->user = $user; } + /** + * Set location to the file + * + * @param string $host Host (e.g. version.phpbb.com) + * @param string $path Path to file (e.g. /phpbb) + * @param string $file File name (Default: versions.json) + * @return version_helper + */ + public function set_file_location($host, $path, $file = 'versions.json') + { + $this->host = $host; + $this->path = $path; + $this->file = $file; + + return $this; + } + /** * Wrapper for version_compare() that allows using uppercase A and B * for alpha and beta releases. @@ -142,12 +181,14 @@ class version_helper */ public function get_versions($force_update = false) { - $info = $this->cache->get('versioncheck'); + $cache_file = 'versioncheck_' . $this->host . $this->path . $this->file; + + $info = $this->cache->get($cache_file); if ($info === false || $force_update) { $errstr = $errno = ''; - $info = get_remote_file('version.phpbb.com', '/phpbb', 'versions.json', $errstr, $errno); + $info = get_remote_file($this->host, $this->path, $this->file, $errstr, $errno); if (!empty($errstr)) { @@ -172,7 +213,7 @@ class version_helper } } - $this->cache->put('versioncheck', $info, 86400); // 24 hours + $this->cache->put($cache_file, $info, 86400); // 24 hours } return $info; -- cgit v1.2.1 From feed1441add9582d987c7480b92cc38946eedf15 Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Tue, 11 Mar 2014 19:15:50 -0500 Subject: [ticket/9871] Option to force the stability when checking for updates PHPBB3-9871 --- phpBB/phpbb/version_helper.php | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'phpBB/phpbb') diff --git a/phpBB/phpbb/version_helper.php b/phpBB/phpbb/version_helper.php index d7bc09182e..b8f305111f 100644 --- a/phpBB/phpbb/version_helper.php +++ b/phpBB/phpbb/version_helper.php @@ -29,6 +29,12 @@ class version_helper */ protected $file = 'versions.json'; + /** + * @var null|string Null to not force stability, 'unstable' or 'stable' to + * force the corresponding stability + */ + protected $force_stability; + /** @var \phpbb\cache\service */ protected $cache; @@ -50,6 +56,11 @@ class version_helper $this->cache = $cache; $this->config = $config; $this->user = $user; + + if (defined('PHPBB_QA')) + { + $this->force_stability = 'unstable'; + } } /** @@ -69,6 +80,20 @@ class version_helper return $this; } + /** + * Over-ride the stability to force check to include unstable versions + * + * @param null|string Null to not force stability, 'unstable' or 'stable' to + * force the corresponding stability + * @return version_helper + */ + public function force_stability($stability) + { + $this->force_stability = $stability; + + return $this; + } + /** * Wrapper for version_compare() that allows using uppercase A and B * for alpha and beta releases. @@ -169,7 +194,12 @@ class version_helper { $info = $this->get_versions($force_update); - return ($this->is_stable($this->config['version']) && !defined('PHPBB_QA')) ? $info['stable'] : $info['unstable']; + if ($this->force_stability !== null) + { + return ($this->force_stability === 'unstable') ? $info['unstable'] : $info['stable']; + } + + return ($this->is_stable($this->config['version'])) ? $info['stable'] : $info['unstable']; } /** -- cgit v1.2.1 From 60d2c1f4006894f2bb4fa310372734d02565e9ca Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Fri, 14 Mar 2014 07:07:26 -0500 Subject: [ticket/9871] Can set current version to use instead of the phpBB version PHPBB3-9871 --- phpBB/phpbb/version_helper.php | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'phpBB/phpbb') diff --git a/phpBB/phpbb/version_helper.php b/phpBB/phpbb/version_helper.php index b8f305111f..e2fdf6ce63 100644 --- a/phpBB/phpbb/version_helper.php +++ b/phpBB/phpbb/version_helper.php @@ -29,6 +29,11 @@ class version_helper */ protected $file = 'versions.json'; + /** + * @var string Current version installed + */ + protected $current_version; + /** * @var null|string Null to not force stability, 'unstable' or 'stable' to * force the corresponding stability @@ -61,6 +66,8 @@ class version_helper { $this->force_stability = 'unstable'; } + + $this->current_version = $this->config['version']; } /** @@ -80,6 +87,19 @@ class version_helper return $this; } + /** + * Set current version + * + * @param string $version The current version + * @return version_helper + */ + public function set_current_version($version) + { + $this->current_version = $version; + + return $this; + } + /** * Over-ride the stability to force check to include unstable versions * @@ -145,7 +165,7 @@ class version_helper $versions = $this->get_versions_matching_stability($force_update); $self = $this; - $current_version = $this->config['version']; + $current_version = $this->current_version; // Filter out any versions less than to the current version $versions = array_filter($versions, function($data) use ($self, $current_version) { @@ -175,7 +195,7 @@ class version_helper $versions = $this->get_versions_matching_stability($force_update); $self = $this; - $current_version = $this->config['version']; + $current_version = $this->current_version; // Filter out any versions less than or equal to the current version return array_filter($versions, function($data) use ($self, $current_version) { @@ -199,7 +219,7 @@ class version_helper return ($this->force_stability === 'unstable') ? $info['unstable'] : $info['stable']; } - return ($this->is_stable($this->config['version'])) ? $info['stable'] : $info['unstable']; + return ($this->is_stable($this->current_version)) ? $info['stable'] : $info['unstable']; } /** -- cgit v1.2.1