From 6c8589775b8df2b6fbeffbe594d9279ae90e85ba Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Wed, 19 Feb 2014 16:11:40 -0600 Subject: [PATCH 01/14] [ticket/9871] Update version check file to use json format PHPBB3-9871 --- phpBB/adm/style/acp_main.html | 1 + phpBB/adm/style/acp_update.html | 68 ++--- phpBB/config/services.yml | 7 + phpBB/includes/acp/acp_main.php | 20 +- phpBB/includes/acp/acp_update.php | 64 ++-- phpBB/includes/functions.php | 14 +- phpBB/includes/functions_admin.php | 105 ------- phpBB/includes/functions_compatibility.php | 68 +++++ phpBB/install/install_update.php | 50 +--- phpBB/language/en/install.php | 12 +- phpBB/phpbb/version_helper.php | 249 +++++++++++++++ tests/functions/get_remote_file_test.php | 75 ----- tests/version/version_test.php | 333 +++++++++++++++++++++ 13 files changed, 742 insertions(+), 324 deletions(-) create mode 100644 phpBB/phpbb/version_helper.php delete mode 100644 tests/functions/get_remote_file_test.php create mode 100644 tests/version/version_test.php diff --git a/phpBB/adm/style/acp_main.html b/phpBB/adm/style/acp_main.html index 6e28c7a0cc..9a9b3ff2b9 100644 --- a/phpBB/adm/style/acp_main.html +++ b/phpBB/adm/style/acp_main.html @@ -17,6 +17,7 @@

{L_VERSIONCHECK_FAIL}

+

{VERSIONCHECK_FAIL_REASON}

{L_VERSIONCHECK_FORCE_UPDATE} · {L_MORE_INFORMATION}

diff --git a/phpBB/adm/style/acp_update.html b/phpBB/adm/style/acp_update.html index 00d37515b3..0cc995959b 100644 --- a/phpBB/adm/style/acp_update.html +++ b/phpBB/adm/style/acp_update.html @@ -1,52 +1,46 @@ - - -

{L_VERSION_CHECK}

+

{L_VERSION_CHECK}

-

{L_VERSION_CHECK_EXPLAIN}

+

{L_VERSION_CHECK_EXPLAIN}

- -
-

{L_VERSION_UP_TO_DATE_ACP} - {L_VERSIONCHECK_FORCE_UPDATE}

-
- -
-

{L_VERSION_NOT_UP_TO_DATE_ACP} - {L_VERSIONCHECK_FORCE_UPDATE}

-
- + +
+

{L_VERSION_UP_TO_DATE_ACP} - {L_VERSIONCHECK_FORCE_UPDATE}

+
+ +
+

{L_VERSION_NOT_UP_TO_DATE_ACP} - {L_VERSIONCHECK_FORCE_UPDATE}

+
+ - -
-

{UPGRADE_INSTRUCTIONS}

-
- - -
- +
+
-
{AUTO_VERSION}{CURRENT_VERSION}
-
-
-
-
{LATEST_VERSION}
+
{CURRENT_VERSION}
+
+ + +
+ +
+
+
{updates_available.current}
+
+
+
+
{updates_available.announcement}
+
+ - - {L_UPDATE_INSTRUCTIONS_INCOMPLETE} -

- {UPDATE_INSTRUCTIONS} -

- - - {UPDATE_INSTRUCTIONS} -

- - + + {UPDATE_INSTRUCTIONS} +

diff --git a/phpBB/config/services.yml b/phpBB/config/services.yml index 735626810f..ad70927522 100644 --- a/phpBB/config/services.yml +++ b/phpBB/config/services.yml @@ -315,3 +315,10 @@ services: - %core.root_path% - %core.php_ext% - %tables.users% + + version_helper: + class: phpbb\version_helper + arguments: + - @cache + - @config + - @user diff --git a/phpBB/includes/acp/acp_main.php b/phpBB/includes/acp/acp_main.php index f01cba0bcc..579317d294 100644 --- a/phpBB/includes/acp/acp_main.php +++ b/phpBB/includes/acp/acp_main.php @@ -25,7 +25,7 @@ class acp_main function main($id, $mode) { global $config, $db, $cache, $user, $auth, $template, $request; - global $phpbb_root_path, $phpbb_admin_path, $phpEx; + global $phpbb_root_path, $phpbb_admin_path, $phpEx, $phpbb_container; // Show restore permissions notice if ($user->data['user_perm_from'] && $auth->acl_get('a_switchperm')) @@ -432,17 +432,19 @@ class acp_main )); } - $latest_version_info = false; - if (($latest_version_info = obtain_latest_version_info(request_var('versioncheck_force', false))) === false) + $version_helper = $phpbb_container->get('version_helper'); + try { - $template->assign_var('S_VERSIONCHECK_FAIL', true); - } - else - { - $latest_version_info = explode("\n", $latest_version_info); + $recheck = request_var('versioncheck_force', false); + $updates_available = $version_helper->get_suggested_updates($recheck); + $template->assign_var('S_VERSION_UP_TO_DATE', empty($updates_available)); + } + catch (\RuntimeException $e) + { $template->assign_vars(array( - 'S_VERSION_UP_TO_DATE' => phpbb_version_compare(trim($latest_version_info[0]), $config['version'], '<='), + 'S_VERSIONCHECK_FAIL' => true, + 'VERSIONCHECK_FAIL_REASON' => ($e->getMessage() !== $user->lang('VERSIONCHECK_FAIL')) ? $e->getMessage() : '', )); } diff --git a/phpBB/includes/acp/acp_update.php b/phpBB/includes/acp/acp_update.php index 6b5407067d..e8f5c6f288 100644 --- a/phpBB/includes/acp/acp_update.php +++ b/phpBB/includes/acp/acp_update.php @@ -24,64 +24,42 @@ class acp_update function main($id, $mode) { - global $config, $db, $user, $auth, $template, $cache; - global $phpbb_root_path, $phpbb_admin_path, $phpEx; + global $config, $user, $template; + global $phpbb_root_path, $phpEx, $phpbb_container; $user->add_lang('install'); $this->tpl_name = 'acp_update'; $this->page_title = 'ACP_VERSION_CHECK'; - // Get current and latest version - $errstr = ''; - $errno = 0; - - $info = obtain_latest_version_info(request_var('versioncheck_force', false)); - - if (empty($info)) + $version_helper = $phpbb_container->get('version_helper'); + try { - trigger_error('VERSIONCHECK_FAIL', E_USER_WARNING); + $recheck = request_var('versioncheck_force', false); + $updates_available = $version_helper->get_suggested_updates($recheck); + } + catch (\RuntimeException $e) + { + $template->assign_var('S_VERSIONCHECK_FAIL', true); + + $updates_available = array(); } - $info = explode("\n", $info); - $latest_version = trim($info[0]); + foreach ($updates_available as $branch => $version_data) + { + $template->assign_block_vars('updates_available', $version_data); + } - $announcement_url = trim($info[1]); - $announcement_url = (strpos($announcement_url, '&') === false) ? str_replace('&', '&', $announcement_url) : $announcement_url; $update_link = append_sid($phpbb_root_path . 'install/index.' . $phpEx, 'mode=update'); - // next feature release - $next_feature_version = $next_feature_announcement_url = false; - if (isset($info[2]) && trim($info[2]) !== '') - { - $next_feature_version = trim($info[2]); - $next_feature_announcement_url = trim($info[3]); - } - - // Determine automatic update... - $sql = 'SELECT config_value - FROM ' . CONFIG_TABLE . " - WHERE config_name = 'version_update_from'"; - $result = $db->sql_query($sql); - $version_update_from = (string) $db->sql_fetchfield('config_value'); - $db->sql_freeresult($result); - - $current_version = (!empty($version_update_from)) ? $version_update_from : $config['version']; - $template->assign_vars(array( - 'S_UP_TO_DATE' => phpbb_version_compare($latest_version, $config['version'], '<='), - 'S_UP_TO_DATE_AUTO' => phpbb_version_compare($latest_version, $current_version, '<='), - 'S_VERSION_CHECK' => true, - 'U_ACTION' => $this->u_action, - 'U_VERSIONCHECK_FORCE' => append_sid($this->u_action . '&versioncheck_force=1'), + 'S_UP_TO_DATE' => empty($updates_available), + 'U_ACTION' => $this->u_action, + 'U_VERSIONCHECK_FORCE' => append_sid($this->u_action . '&versioncheck_force=1'), - 'LATEST_VERSION' => $latest_version, - 'CURRENT_VERSION' => $config['version'], - 'AUTO_VERSION' => $version_update_from, - 'NEXT_FEATURE_VERSION' => $next_feature_version, + 'CURRENT_VERSION' => $config['version'], - 'UPDATE_INSTRUCTIONS' => sprintf($user->lang['UPDATE_INSTRUCTIONS'], $announcement_url, $update_link), - 'UPGRADE_INSTRUCTIONS' => $next_feature_version ? $user->lang('UPGRADE_INSTRUCTIONS', $next_feature_version, $next_feature_announcement_url) : false, + 'UPDATE_INSTRUCTIONS' => sprintf($user->lang['UPDATE_INSTRUCTIONS'], $update_link), )); } } diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 689a682de3..97e35a930b 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -429,17 +429,11 @@ function phpbb_email_hash($email) */ function phpbb_version_compare($version1, $version2, $operator = null) { - $version1 = strtolower($version1); - $version2 = strtolower($version2); + global $phpbb_container; - if (is_null($operator)) - { - return version_compare($version1, $version2); - } - else - { - return version_compare($version1, $version2, $operator); - } + $version_helper = $phpbb_container->get('version_helper'); + + return $version_helper->compare($version1, $version2, $operator); } /** diff --git a/phpBB/includes/functions_admin.php b/phpBB/includes/functions_admin.php index 722d3c9c67..7c4f589700 100644 --- a/phpBB/includes/functions_admin.php +++ b/phpBB/includes/functions_admin.php @@ -2924,72 +2924,6 @@ function get_database_size() return $database_size; } -/** -* Retrieve contents from remotely stored file -*/ -function get_remote_file($host, $directory, $filename, &$errstr, &$errno, $port = 80, $timeout = 6) -{ - global $user; - - 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) - { - $errstr = $user->lang['FILE_NOT_FOUND'] . ': ' . $filename; - return false; - } - } - - $stream_meta_data = stream_get_meta_data($fsock); - - if (!empty($stream_meta_data['timed_out']) || time() >= $timer_stop) - { - $errstr = $user->lang['FSOCK_TIMEOUT']; - return false; - } - } - @fclose($fsock); - } - else - { - if ($errstr) - { - $errstr = utf8_convert_message($errstr); - return false; - } - else - { - $errstr = $user->lang['FSOCK_DISABLED']; - return false; - } - } - - return $file_info; -} - /** * Tidy Warnings * Remove all warnings which have now expired from the database @@ -3099,45 +3033,6 @@ function add_permission_language() } } -/** - * Obtains the latest version information - * - * @param bool $force_update Ignores cached data. Defaults to false. - * @param bool $warn_fail Trigger a warning if obtaining the latest version information fails. Defaults to false. - * @param int $ttl Cache version information for $ttl seconds. Defaults to 86400 (24 hours). - * - * @return string | false Version info on success, false on failure. - */ -function obtain_latest_version_info($force_update = false, $warn_fail = false, $ttl = 86400) -{ - global $cache; - - $info = $cache->get('versioncheck'); - - if ($info === false || $force_update) - { - $errstr = ''; - $errno = 0; - - $info = get_remote_file('version.phpbb.com', '/phpbb', - ((defined('PHPBB_QA')) ? '30x_qa.txt' : '30x.txt'), $errstr, $errno); - - if (empty($info)) - { - $cache->destroy('versioncheck'); - if ($warn_fail) - { - trigger_error($errstr, E_USER_WARNING); - } - return false; - } - - $cache->put('versioncheck', $info, $ttl); - } - - return $info; -} - /** * Enables a particular flag in a bitfield column of a given table. * diff --git a/phpBB/includes/functions_compatibility.php b/phpBB/includes/functions_compatibility.php index 2197815087..2bd812efe0 100644 --- a/phpBB/includes/functions_compatibility.php +++ b/phpBB/includes/functions_compatibility.php @@ -48,3 +48,71 @@ function get_user_avatar($avatar, $avatar_type, $avatar_width, $avatar_height, $ return phpbb_get_avatar($row, $alt, $ignore_config); } + +/** + * Retrieve contents from remotely stored file + * + * @deprecated 3.1.0-a4 (To be removed: 3.3.0) + */ +function get_remote_file($host, $directory, $filename, &$errstr, &$errno, $port = 80, $timeout = 6) +{ + global $user; + + 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) + { + $errstr = $user->lang['FILE_NOT_FOUND'] . ': ' . $filename; + return false; + } + } + + $stream_meta_data = stream_get_meta_data($fsock); + + if (!empty($stream_meta_data['timed_out']) || time() >= $timer_stop) + { + $errstr = $user->lang['FSOCK_TIMEOUT']; + return false; + } + } + @fclose($fsock); + } + else + { + if ($errstr) + { + $errstr = utf8_convert_message($errstr); + return false; + } + else + { + $errstr = $user->lang['FSOCK_DISABLED']; + return false; + } + } + + return $file_info; +} diff --git a/phpBB/install/install_update.php b/phpBB/install/install_update.php index dc6e57c851..5891efb375 100644 --- a/phpBB/install/install_update.php +++ b/phpBB/install/install_update.php @@ -154,14 +154,23 @@ class install_update extends module )); // Get current and latest version - if (($latest_version = $cache->get('_version_info')) === false) + $version_helper = $phpbb_container->get('version_helper'); + try { - $this->latest_version = $this->get_file('version_info'); - $cache->put('_version_info', $this->latest_version); + $this->latest_version = $version_helper->get_latest_on_current_branch(true); } - else + catch (\RuntimeException $e) { - $this->latest_version = $latest_version; + $this->latest_version = false; + + $update_info = array(); + include($phpbb_root_path . 'install/update/index.' . $phpEx); + $info = (empty($update_info) || !is_array($update_info)) ? false : $update_info; + + if ($info !== false) + { + $this->latest_version = (!empty($info['version']['to'])) ? trim($info['version']['to']) : false; + } } // For the current version we trick a bit. ;) @@ -1606,37 +1615,6 @@ class install_update extends module switch ($mode) { - case 'version_info': - global $phpbb_root_path, $phpEx; - - $info = get_remote_file('version.phpbb.com', '/phpbb', - ((defined('PHPBB_QA')) ? '30x_qa.txt' : '30x.txt'), $errstr, $errno); - - if ($info !== false) - { - $info = explode("\n", $info); - $info = trim($info[0]); - } - - if ($this->test_update !== false) - { - $info = $this->test_update; - } - - // If info is false the fsockopen function may not be working. Instead get the latest version from our update file (and pray it is up-to-date) - if ($info === false) - { - $update_info = array(); - include($phpbb_root_path . 'install/update/index.' . $phpEx); - $info = (empty($update_info) || !is_array($update_info)) ? false : $update_info; - - if ($info !== false) - { - $info = (!empty($info['version']['to'])) ? trim($info['version']['to']) : false; - } - } - break; - case 'update_info': global $phpbb_root_path, $phpEx; diff --git a/phpBB/language/en/install.php b/phpBB/language/en/install.php index 03c9562983..081361f661 100644 --- a/phpBB/language/en/install.php +++ b/phpBB/language/en/install.php @@ -490,6 +490,7 @@ $lang = array_merge($lang, array( 'PREVIOUS_VERSION' => 'Previous version', 'PROGRESS' => 'Progress', + 'RELEASE_ANNOUNCEMENT' => 'Announcement', 'RESULT' => 'Result', 'RUN_DATABASE_SCRIPT' => 'Update my database now', @@ -532,7 +533,7 @@ $lang = array_merge($lang, array(

Release announcement

-

Please read the release announcement for the latest version before you continue your update process, it may contain useful information. It also contains full download links as well as the change log.

+

Please read the release announcement for the latest version before you continue your update process, it may contain useful information. It also contains full download links as well as the change log.


@@ -547,17 +548,11 @@ $lang = array_merge($lang, array(

Once uploaded your board will be offline for normal users due to the install directory you uploaded now present.

- Now start the update process by pointing your browser to the install folder.
+ Now start the update process by pointing your browser to the install folder.

You will then be guided through the update process. You will be notified once the update is complete.

', - 'UPDATE_INSTRUCTIONS_INCOMPLETE' => ' - -

Incomplete update detected

- -

phpBB detected an incomplete automatic update. Please make sure you followed every step within the automatic update tool. Below you will find the link again, or go directly to your install directory.

- ', 'UPDATE_METHOD' => 'Update method', 'UPDATE_METHOD_EXPLAIN' => 'You are now able to choose your preferred update method. Using the FTP upload will present you with a form you need to enter your FTP account details into. With this method the files will be automatically moved to the new location and backups of the old files being created by appending .bak to the filename. If you choose to download the modified files you are able to unpack and upload them to their correct location manually later.', 'UPDATE_REQUIRES_FILE' => 'The updater requires that the following file is present: %s', @@ -567,7 +562,6 @@ $lang = array_merge($lang, array( 'UPDATING_DATA' => 'Updating data', 'UPDATING_TO_LATEST_STABLE' => 'Updating database to latest stable release', 'UPDATED_VERSION' => 'Updated version', - 'UPGRADE_INSTRUCTIONS' => 'A new feature release %1$s is available. Please read the release announcement to learn about what it has to offer, and how to upgrade.', 'UPLOAD_METHOD' => 'Upload method', 'UPDATE_DB_SUCCESS' => 'Database update was successful.', 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; + } +} diff --git a/tests/functions/get_remote_file_test.php b/tests/functions/get_remote_file_test.php deleted file mode 100644 index 4032ca5b58..0000000000 --- a/tests/functions/get_remote_file_test.php +++ /dev/null @@ -1,75 +0,0 @@ -markTestSkipped(sprintf( - 'Could not find a DNS record for hostname %s. ' . - 'Assuming network is down.', - $hostname - )); - } - - $errstr = $errno = null; - $file = get_remote_file($hostname, '/phpbb', '30x.txt', $errstr, $errno); - - $this->assertNotEquals( - 0, - strlen($file), - 'Failed asserting that the response is not empty.' - ); - - $this->assertSame( - '', - $errstr, - 'Failed asserting that the error string is empty.' - ); - - $this->assertSame( - 0, - $errno, - 'Failed asserting that the error number is 0 (i.e. no error occurred).' - ); - - $lines = explode("\n", $file); - - $this->assertGreaterThanOrEqual( - 2, - sizeof($lines), - 'Failed asserting that the version file has at least two lines.' - ); - - $this->assertStringStartsWith( - '3.', - $lines[0], - "Failed asserting that the first line of the version file starts with '3.'" - ); - - $this->assertNotSame( - false, - filter_var($lines[1], FILTER_VALIDATE_URL), - 'Failed asserting that the second line of the version file is a valid URL.' - ); - - $this->assertContains('http', $lines[1]); - $this->assertContains('phpbb.com', $lines[1], '', true); - } -} diff --git a/tests/version/version_test.php b/tests/version/version_test.php new file mode 100644 index 0000000000..e7df30634c --- /dev/null +++ b/tests/version/version_test.php @@ -0,0 +1,333 @@ +version_helper = new \phpbb\version_helper( + new phpbb_mock_null_cache(), + new \phpbb\config\config(array( + 'version' => '3.1.0', + )), + new \phpbb\user() + ); + } + + public function is_stable_data() + { + return array( + array( + '3.0.0-a1', + false, + ), + array( + '3.0.0-b1', + false, + ), + array( + '3.0.0-rc1', + false, + ), + array( + '3.0.0-RC1', + false, + ), + array( + '3.0.0', + true, + ), + array( + '3.0.0-pl1', + true, + ), + array( + '3.0.0.1-pl1', + true, + ), + array( + '3.1-dev', + false, + ), + array( + 'foobar', + false, + ), + ); + } + + /** + * @dataProvider is_stable_data + */ + public function test_is_stable($version, $expected) + { + $this->assertSame($expected, $this->version_helper->is_stable($version)); + } + + public function get_suggested_updates_data() + { + return array( + array( + '1.0.0', + array( + '1.0' => array( + 'current' => '1.0.1', + ), + '1.1' => array( + 'current' => '1.1.1', + ), + ), + array( + '1.0' => array( + 'current' => '1.0.1', + ), + '1.1' => array( + 'current' => '1.1.1', + ), + ), + ), + array( + '1.0.1', + array( + '1.0' => array( + 'current' => '1.0.1', + ), + '1.1' => array( + 'current' => '1.1.1', + ), + ), + array( + '1.1' => array( + 'current' => '1.1.1', + ), + ), + ), + array( + '1.0.1-a1', + array( + '1.0' => array( + 'current' => '1.0.1-a2', + ), + '1.1' => array( + 'current' => '1.1.0', + ), + ), + array( + '1.0' => array( + 'current' => '1.0.1-a2', + ), + '1.1' => array( + 'current' => '1.1.0', + ), + ), + ), + array( + '1.1.0', + array( + '1.0' => array( + 'current' => '1.0.1', + ), + '1.1' => array( + 'current' => '1.1.1', + ), + ), + array( + '1.1' => array( + 'current' => '1.1.1', + ), + ), + ), + array( + '1.1.1', + array( + '1.0' => array( + 'current' => '1.0.1', + ), + '1.1' => array( + 'current' => '1.1.1', + ), + ), + array(), + ), + array( + '1.1.0-a1', + array( + '1.0' => array( + 'current' => '1.0.1', + ), + '1.1' => array( + 'current' => '1.1.0-a2', + ), + ), + array( + '1.1' => array( + 'current' => '1.1.0-a2', + ), + ), + ), + ); + } + + /** + * @dataProvider get_suggested_updates_data + */ + public function test_get_suggested_updates($current_version, $versions, $expected) + { + $version_helper = $this + ->getMockBuilder('\phpbb\version_helper') + ->setMethods(array( + 'get_versions_matching_stability', + )) + ->setConstructorArgs(array( + new phpbb_mock_null_cache(), + new \phpbb\config\config(array( + 'version' => $current_version, + )), + new \phpbb\user(), + )) + ->getMock() + ; + + $version_helper->expects($this->any()) + ->method('get_versions_matching_stability') + ->will($this->returnValue($versions)); + + $this->assertSame($expected, $version_helper->get_suggested_updates()); + } + + public function get_latest_on_current_branch_data() + { + return array( + array( + '1.0.0', + array( + '1.0' => array( + 'current' => '1.0.1', + ), + '1.1' => array( + 'current' => '1.1.1', + ), + ), + '1.0.1', + ), + array( + '1.0.1', + array( + '1.0' => array( + 'current' => '1.0.1', + ), + '1.1' => array( + 'current' => '1.1.1', + ), + ), + '1.0.1', + ), + array( + '1.0.1-a1', + array( + '1.0' => array( + 'current' => '1.0.1-a2', + ), + '1.1' => array( + 'current' => '1.1.0', + ), + ), + '1.0.1-a2', + ), + array( + '1.1.0', + array( + '1.0' => array( + 'current' => '1.0.1', + ), + '1.1' => array( + 'current' => '1.1.1', + ), + ), + '1.1.1', + ), + array( + '1.1.1', + array( + '1.0' => array( + 'current' => '1.0.1', + ), + '1.1' => array( + 'current' => '1.1.1', + ), + ), + '1.1.1', + ), + array( + '1.1.0-a1', + array( + '1.0' => array( + 'current' => '1.0.1', + ), + '1.1' => array( + 'current' => '1.1.0-a2', + ), + ), + '1.1.0-a2', + ), + ); + } + + /** + * @dataProvider get_latest_on_current_branch_data + */ + public function test_get_latest_on_current_branch($current_version, $versions, $expected) + { + $version_helper = $this + ->getMockBuilder('\phpbb\version_helper') + ->setMethods(array( + 'get_versions_matching_stability', + )) + ->setConstructorArgs(array( + new phpbb_mock_null_cache(), + new \phpbb\config\config(array( + 'version' => $current_version, + )), + new \phpbb\user(), + )) + ->getMock() + ; + + $version_helper->expects($this->any()) + ->method('get_versions_matching_stability') + ->will($this->returnValue($versions)); + + $this->assertSame($expected, $version_helper->get_latest_on_current_branch()); + } + + public function test_version_phpbb_com() + { + global $phpbb_root_path, $phpEx; + include_once($phpbb_root_path . 'includes/functions.' . $phpEx); + + if (!phpbb_checkdnsrr('version.phpbb.com', 'A')) + { + $this->markTestSkipped(sprintf( + 'Could not find a DNS record for hostname %s. ' . + 'Assuming network is down.', + 'version.phpbb.com' + )); + } + + $this->version_helper->get_versions(); + + // get_versions checks to make sure we got a valid versions file or + // throws an exception if we did not. We don't need to test anything + // here, but adding an assertion so we do not get a warning about no + // assertions in this test + $this->assertSame(true, true); + } +} From 6a8f110b5a28338ac4e48e75c763ea7435192cc7 Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Wed, 5 Mar 2014 19:00:11 -0600 Subject: [PATCH 02/14] [ticket/9871] Use $request->variable instead of request_var PHPBB3-9871 --- phpBB/includes/acp/acp_main.php | 2 +- phpBB/includes/acp/acp_update.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/phpBB/includes/acp/acp_main.php b/phpBB/includes/acp/acp_main.php index 579317d294..fd45027b49 100644 --- a/phpBB/includes/acp/acp_main.php +++ b/phpBB/includes/acp/acp_main.php @@ -435,7 +435,7 @@ class acp_main $version_helper = $phpbb_container->get('version_helper'); try { - $recheck = request_var('versioncheck_force', false); + $recheck = $request->variable('versioncheck_force', false); $updates_available = $version_helper->get_suggested_updates($recheck); $template->assign_var('S_VERSION_UP_TO_DATE', empty($updates_available)); diff --git a/phpBB/includes/acp/acp_update.php b/phpBB/includes/acp/acp_update.php index e8f5c6f288..e50409bd37 100644 --- a/phpBB/includes/acp/acp_update.php +++ b/phpBB/includes/acp/acp_update.php @@ -24,7 +24,7 @@ class acp_update function main($id, $mode) { - global $config, $user, $template; + global $config, $user, $template, $request; global $phpbb_root_path, $phpEx, $phpbb_container; $user->add_lang('install'); @@ -35,7 +35,7 @@ class acp_update $version_helper = $phpbb_container->get('version_helper'); try { - $recheck = request_var('versioncheck_force', false); + $recheck = $request->variable('versioncheck_force', false); $updates_available = $version_helper->get_suggested_updates($recheck); } catch (\RuntimeException $e) From 8748032866293129b881f9b7f3f0173c1e3a646b Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Wed, 5 Mar 2014 19:07:39 -0600 Subject: [PATCH 03/14] [ticket/9871] Restore get_remote_file PHPBB3-9871 --- phpBB/includes/functions_admin.php | 66 ++++++++++++++++++ phpBB/includes/functions_compatibility.php | 68 ------------------- phpBB/phpbb/version_helper.php | 78 ++-------------------- 3 files changed, 73 insertions(+), 139 deletions(-) diff --git a/phpBB/includes/functions_admin.php b/phpBB/includes/functions_admin.php index 7c4f589700..a422d38f67 100644 --- a/phpBB/includes/functions_admin.php +++ b/phpBB/includes/functions_admin.php @@ -2925,6 +2925,72 @@ function get_database_size() } /** + * Retrieve contents from remotely stored file + */ +function get_remote_file($host, $directory, $filename, &$errstr, &$errno, $port = 80, $timeout = 6) +{ + global $user; + + 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) + { + $errstr = $user->lang['FILE_NOT_FOUND'] . ': ' . $filename; + return false; + } + } + + $stream_meta_data = stream_get_meta_data($fsock); + + if (!empty($stream_meta_data['timed_out']) || time() >= $timer_stop) + { + $errstr = $user->lang['FSOCK_TIMEOUT']; + return false; + } + } + @fclose($fsock); + } + else + { + if ($errstr) + { + $errstr = utf8_convert_message($errstr); + return false; + } + else + { + $errstr = $user->lang['FSOCK_DISABLED']; + return false; + } + } + + return $file_info; +} + +/* * Tidy Warnings * Remove all warnings which have now expired from the database * The duration of a warning can be defined by the administrator diff --git a/phpBB/includes/functions_compatibility.php b/phpBB/includes/functions_compatibility.php index 2bd812efe0..2197815087 100644 --- a/phpBB/includes/functions_compatibility.php +++ b/phpBB/includes/functions_compatibility.php @@ -48,71 +48,3 @@ function get_user_avatar($avatar, $avatar_type, $avatar_width, $avatar_height, $ return phpbb_get_avatar($row, $alt, $ignore_config); } - -/** - * Retrieve contents from remotely stored file - * - * @deprecated 3.1.0-a4 (To be removed: 3.3.0) - */ -function get_remote_file($host, $directory, $filename, &$errstr, &$errno, $port = 80, $timeout = 6) -{ - global $user; - - 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) - { - $errstr = $user->lang['FILE_NOT_FOUND'] . ': ' . $filename; - return false; - } - } - - $stream_meta_data = stream_get_meta_data($fsock); - - if (!empty($stream_meta_data['timed_out']) || time() >= $timer_stop) - { - $errstr = $user->lang['FSOCK_TIMEOUT']; - return false; - } - } - @fclose($fsock); - } - else - { - if ($errstr) - { - $errstr = utf8_convert_message($errstr); - return false; - } - else - { - $errstr = $user->lang['FSOCK_DISABLED']; - return false; - } - } - - return $file_info; -} 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; - } } From 43074a1b6938f3efb2b9282a25cf7beb3b1e14a6 Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Wed, 5 Mar 2014 19:09:47 -0600 Subject: [PATCH 04/14] [ticket/9871] Restore phpbb_version_compare PHPBB3-9871 --- phpBB/includes/functions.php | 14 ++++++++++---- phpBB/phpbb/version_helper.php | 12 +----------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 97e35a930b..689a682de3 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -429,11 +429,17 @@ function phpbb_email_hash($email) */ function phpbb_version_compare($version1, $version2, $operator = null) { - global $phpbb_container; + $version1 = strtolower($version1); + $version2 = strtolower($version2); - $version_helper = $phpbb_container->get('version_helper'); - - return $version_helper->compare($version1, $version2, $operator); + if (is_null($operator)) + { + return version_compare($version1, $version2); + } + else + { + return version_compare($version1, $version2, $operator); + } } /** 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); } /** From dc5a34eb880974c47c2b3bcc8468c57cd6169612 Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Wed, 5 Mar 2014 19:18:23 -0600 Subject: [PATCH 05/14] [ticket/9871] Mark test as slow PHPBB3-9871 --- tests/version/version_test.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/version/version_test.php b/tests/version/version_test.php index e7df30634c..3ac2b46b0d 100644 --- a/tests/version/version_test.php +++ b/tests/version/version_test.php @@ -7,6 +7,9 @@ * */ +/* +* @group slow +*/ class phpbb_version_helper_test extends phpbb_test_case { public function setUp() From 98542547e2b0416f0f11bc1d75e5d999ae0d9f2b Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Wed, 5 Mar 2014 19:18:54 -0600 Subject: [PATCH 06/14] [ticket/9871] Typehint and comment on var types PHPBB3-9871 --- phpBB/phpbb/version_helper.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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; From c655757aa635ced42db1f00023aadf423d37bbff Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Sat, 8 Mar 2014 17:43:58 -0600 Subject: [PATCH 07/14] [ticket/9871] Fix tests PHPBB3-9871 --- tests/version/version_test.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/tests/version/version_test.php b/tests/version/version_test.php index 3ac2b46b0d..87922d50b4 100644 --- a/tests/version/version_test.php +++ b/tests/version/version_test.php @@ -16,8 +16,16 @@ class phpbb_version_helper_test extends phpbb_test_case { parent::setUp(); + global $phpbb_root_path, $phpEx; + + include_once($phpbb_root_path . 'includes/functions.' . $phpEx); + + $this->cache = $this->getMockBuilder('\phpbb\cache\service') + ->disableOriginalConstructor() + ->getMock(); + $this->version_helper = new \phpbb\version_helper( - new phpbb_mock_null_cache(), + $this->cache, new \phpbb\config\config(array( 'version' => '3.1.0', )), @@ -190,7 +198,7 @@ class phpbb_version_helper_test extends phpbb_test_case 'get_versions_matching_stability', )) ->setConstructorArgs(array( - new phpbb_mock_null_cache(), + $this->cache, new \phpbb\config\config(array( 'version' => $current_version, )), @@ -295,7 +303,7 @@ class phpbb_version_helper_test extends phpbb_test_case 'get_versions_matching_stability', )) ->setConstructorArgs(array( - new phpbb_mock_null_cache(), + $this->cache, new \phpbb\config\config(array( 'version' => $current_version, )), From 1f7c891da2590cd2d70f4e7278cf4d6119a820a5 Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Mon, 10 Mar 2014 20:57:20 -0500 Subject: [PATCH 08/14] [ticket/9871] Restore get_remote_file_test PHPBB3-9871 --- tests/functions/get_remote_file_test.php | 75 ++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 tests/functions/get_remote_file_test.php diff --git a/tests/functions/get_remote_file_test.php b/tests/functions/get_remote_file_test.php new file mode 100644 index 0000000000..4032ca5b58 --- /dev/null +++ b/tests/functions/get_remote_file_test.php @@ -0,0 +1,75 @@ +markTestSkipped(sprintf( + 'Could not find a DNS record for hostname %s. ' . + 'Assuming network is down.', + $hostname + )); + } + + $errstr = $errno = null; + $file = get_remote_file($hostname, '/phpbb', '30x.txt', $errstr, $errno); + + $this->assertNotEquals( + 0, + strlen($file), + 'Failed asserting that the response is not empty.' + ); + + $this->assertSame( + '', + $errstr, + 'Failed asserting that the error string is empty.' + ); + + $this->assertSame( + 0, + $errno, + 'Failed asserting that the error number is 0 (i.e. no error occurred).' + ); + + $lines = explode("\n", $file); + + $this->assertGreaterThanOrEqual( + 2, + sizeof($lines), + 'Failed asserting that the version file has at least two lines.' + ); + + $this->assertStringStartsWith( + '3.', + $lines[0], + "Failed asserting that the first line of the version file starts with '3.'" + ); + + $this->assertNotSame( + false, + filter_var($lines[1], FILTER_VALIDATE_URL), + 'Failed asserting that the second line of the version file is a valid URL.' + ); + + $this->assertContains('http', $lines[1]); + $this->assertContains('phpbb.com', $lines[1], '', true); + } +} From 00d86a4af1adc4d34955d0432ef514d8c25942c9 Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Mon, 10 Mar 2014 21:26:46 -0500 Subject: [PATCH 09/14] [ticket/9871] Allow setting the host/file to load for the version class PHPBB3-9871 --- phpBB/config/services.yml | 1 + phpBB/phpbb/version_helper.php | 47 +++++++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/phpBB/config/services.yml b/phpBB/config/services.yml index ad70927522..6e9672fcb5 100644 --- a/phpBB/config/services.yml +++ b/phpBB/config/services.yml @@ -318,6 +318,7 @@ services: version_helper: class: phpbb\version_helper + scope: prototype arguments: - @cache - @config 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; From feed1441add9582d987c7480b92cc38946eedf15 Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Tue, 11 Mar 2014 19:15:50 -0500 Subject: [PATCH 10/14] [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(-) 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']; } /** From fb6b350c21f5f63991099ae2ef9f007c9786cb61 Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Thu, 13 Mar 2014 19:38:20 -0500 Subject: [PATCH 11/14] [ticket/9871] Split fast and slow version tests PHPBB3-9871 --- tests/version/version_fetch_test.php | 58 ++++++++++++++++++++++++++++ tests/version/version_test.php | 26 ------------- 2 files changed, 58 insertions(+), 26 deletions(-) create mode 100644 tests/version/version_fetch_test.php diff --git a/tests/version/version_fetch_test.php b/tests/version/version_fetch_test.php new file mode 100644 index 0000000000..b4912861f4 --- /dev/null +++ b/tests/version/version_fetch_test.php @@ -0,0 +1,58 @@ +cache = $this->getMockBuilder('\phpbb\cache\service') + ->disableOriginalConstructor() + ->getMock(); + + $this->version_helper = new \phpbb\version_helper( + $this->cache, + new \phpbb\config\config(array( + 'version' => '3.1.0', + )), + new \phpbb\user() + ); + } + + public function test_version_phpbb_com() + { + global $phpbb_root_path, $phpEx; + include_once($phpbb_root_path . 'includes/functions.' . $phpEx); + + if (!phpbb_checkdnsrr('version.phpbb.com', 'A')) + { + $this->markTestSkipped(sprintf( + 'Could not find a DNS record for hostname %s. ' . + 'Assuming network is down.', + 'version.phpbb.com' + )); + } + + $this->version_helper->get_versions(); + + // get_versions checks to make sure we got a valid versions file or + // throws an exception if we did not. We don't need to test anything + // here, but adding an assertion so we do not get a warning about no + // assertions in this test + $this->assertSame(true, true); + } +} diff --git a/tests/version/version_test.php b/tests/version/version_test.php index 87922d50b4..2e2398bd45 100644 --- a/tests/version/version_test.php +++ b/tests/version/version_test.php @@ -7,9 +7,6 @@ * */ -/* -* @group slow -*/ class phpbb_version_helper_test extends phpbb_test_case { public function setUp() @@ -318,27 +315,4 @@ class phpbb_version_helper_test extends phpbb_test_case $this->assertSame($expected, $version_helper->get_latest_on_current_branch()); } - - public function test_version_phpbb_com() - { - global $phpbb_root_path, $phpEx; - include_once($phpbb_root_path . 'includes/functions.' . $phpEx); - - if (!phpbb_checkdnsrr('version.phpbb.com', 'A')) - { - $this->markTestSkipped(sprintf( - 'Could not find a DNS record for hostname %s. ' . - 'Assuming network is down.', - 'version.phpbb.com' - )); - } - - $this->version_helper->get_versions(); - - // get_versions checks to make sure we got a valid versions file or - // throws an exception if we did not. We don't need to test anything - // here, but adding an assertion so we do not get a warning about no - // assertions in this test - $this->assertSame(true, true); - } } From 23289c89a19ef8c262c46d2761b75a2506354d4e Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Fri, 14 Mar 2014 07:02:25 -0500 Subject: [PATCH 12/14] [ticket/9871] Fix test name PHPBB3-9871 --- tests/version/version_fetch_test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/version/version_fetch_test.php b/tests/version/version_fetch_test.php index b4912861f4..7b3ba5e717 100644 --- a/tests/version/version_fetch_test.php +++ b/tests/version/version_fetch_test.php @@ -10,7 +10,7 @@ /* * @group slow */ -class phpbb_version_helper_test extends phpbb_test_case +class phpbb_version_helper_fetch_test extends phpbb_test_case { public function setUp() { From 0c40cd5f137167d3a7a022ac951a238cbc885305 Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Fri, 14 Mar 2014 07:03:37 -0500 Subject: [PATCH 13/14] [ticket/9871] Fix indentation on comment PHPBB3-9871 --- phpBB/includes/functions_admin.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpBB/includes/functions_admin.php b/phpBB/includes/functions_admin.php index a422d38f67..432d4dd477 100644 --- a/phpBB/includes/functions_admin.php +++ b/phpBB/includes/functions_admin.php @@ -2925,8 +2925,8 @@ function get_database_size() } /** - * Retrieve contents from remotely stored file - */ +* Retrieve contents from remotely stored file +*/ function get_remote_file($host, $directory, $filename, &$errstr, &$errno, $port = 80, $timeout = 6) { global $user; From 60d2c1f4006894f2bb4fa310372734d02565e9ca Mon Sep 17 00:00:00 2001 From: Nathan Guse Date: Fri, 14 Mar 2014 07:07:26 -0500 Subject: [PATCH 14/14] [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(-) 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']; } /**