diff options
author | Nathan Guse <nathaniel.guse@gmail.com> | 2014-02-19 16:11:40 -0600 |
---|---|---|
committer | Nathan Guse <nathaniel.guse@gmail.com> | 2014-02-19 16:13:23 -0600 |
commit | 6c8589775b8df2b6fbeffbe594d9279ae90e85ba (patch) | |
tree | 170b26bd86dce0eeb7d6c447115f6810ced34bc2 | |
parent | bd8951cfbec799f685eb89f14a83933cee647112 (diff) | |
download | forums-6c8589775b8df2b6fbeffbe594d9279ae90e85ba.tar forums-6c8589775b8df2b6fbeffbe594d9279ae90e85ba.tar.gz forums-6c8589775b8df2b6fbeffbe594d9279ae90e85ba.tar.bz2 forums-6c8589775b8df2b6fbeffbe594d9279ae90e85ba.tar.xz forums-6c8589775b8df2b6fbeffbe594d9279ae90e85ba.zip |
[ticket/9871] Update version check file to use json format
PHPBB3-9871
-rw-r--r-- | phpBB/adm/style/acp_main.html | 1 | ||||
-rw-r--r-- | phpBB/adm/style/acp_update.html | 68 | ||||
-rw-r--r-- | phpBB/config/services.yml | 7 | ||||
-rw-r--r-- | phpBB/includes/acp/acp_main.php | 18 | ||||
-rw-r--r-- | phpBB/includes/acp/acp_update.php | 60 | ||||
-rw-r--r-- | phpBB/includes/functions.php | 14 | ||||
-rw-r--r-- | phpBB/includes/functions_admin.php | 105 | ||||
-rw-r--r-- | phpBB/includes/functions_compatibility.php | 68 | ||||
-rw-r--r-- | phpBB/install/install_update.php | 50 | ||||
-rw-r--r-- | phpBB/language/en/install.php | 12 | ||||
-rw-r--r-- | phpBB/phpbb/version_helper.php | 249 | ||||
-rw-r--r-- | tests/functions/get_remote_file_test.php | 75 | ||||
-rw-r--r-- | tests/version/version_test.php | 333 |
13 files changed, 739 insertions, 321 deletions
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 @@ <!-- IF S_VERSIONCHECK_FAIL --> <div class="errorbox notice"> <p>{L_VERSIONCHECK_FAIL}</p> + <p>{VERSIONCHECK_FAIL_REASON}</p> <p><a href="{U_VERSIONCHECK_FORCE}">{L_VERSIONCHECK_FORCE_UPDATE}</a> · <a href="{U_VERSIONCHECK}">{L_MORE_INFORMATION}</a></p> </div> <!-- ELSEIF not S_VERSION_UP_TO_DATE --> 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 @@ <!-- INCLUDE overall_header.html --> <a id="maincontent"></a> - -<!-- IF S_VERSION_CHECK --> - <h1>{L_VERSION_CHECK}</h1> +<h1>{L_VERSION_CHECK}</h1> - <p>{L_VERSION_CHECK_EXPLAIN}</p> +<p>{L_VERSION_CHECK_EXPLAIN}</p> - <!-- IF S_UP_TO_DATE and S_UP_TO_DATE_AUTO --> - <div class="successbox"> - <p>{L_VERSION_UP_TO_DATE_ACP} - <a href="{U_VERSIONCHECK_FORCE}">{L_VERSIONCHECK_FORCE_UPDATE}</a></p> - </div> - <!-- ELSE --> - <div class="errorbox"> - <p>{L_VERSION_NOT_UP_TO_DATE_ACP} - <a href="{U_VERSIONCHECK_FORCE}">{L_VERSIONCHECK_FORCE_UPDATE}</a></p> - </div> - <!-- ENDIF --> - - <!-- IF NEXT_FEATURE_VERSION --> - <div class="errorbox"> - <p>{UPGRADE_INSTRUCTIONS}</p> - </div> - <!-- ENDIF --> +<!-- IF S_UP_TO_DATE --> + <div class="successbox"> + <p>{L_VERSION_UP_TO_DATE_ACP} - <a href="{U_VERSIONCHECK_FORCE}">{L_VERSIONCHECK_FORCE_UPDATE}</a></p> + </div> +<!-- ELSE --> + <div class="errorbox"> + <p>{L_VERSION_NOT_UP_TO_DATE_ACP} - <a href="{U_VERSIONCHECK_FORCE}">{L_VERSIONCHECK_FORCE_UPDATE}</a></p> + </div> +<!-- ENDIF --> - <fieldset> - <legend></legend> +<fieldset> + <legend></legend> <dl> <dt><label>{L_CURRENT_VERSION}</label></dt> - <dd><strong><!-- IF S_UP_TO_DATE and not S_UP_TO_DATE_AUTO -->{AUTO_VERSION}<!-- ELSE -->{CURRENT_VERSION}<!-- ENDIF --></strong></dd> - </dl> - <dl> - <dt><label>{L_LATEST_VERSION}</label></dt> - <dd><strong>{LATEST_VERSION}</strong></dd> + <dd><strong>{CURRENT_VERSION}</strong></dd> </dl> - </fieldset> +</fieldset> - <!-- IF S_UP_TO_DATE and not S_UP_TO_DATE_AUTO --> - {L_UPDATE_INSTRUCTIONS_INCOMPLETE} - <br /><br /> - {UPDATE_INSTRUCTIONS} - <br /><br /> - <!-- ENDIF --> - <!-- IF not S_UP_TO_DATE --> - {UPDATE_INSTRUCTIONS} - <br /><br /> - <!-- ENDIF --> +<!-- BEGIN updates_available --> + <fieldset> + <legend></legend> + <dl> + <dt><label>{L_LATEST_VERSION}</label></dt> + <dd><strong>{updates_available.current}</strong></dd> + </dl> + <dl> + <dt><label>{L_RELEASE_ANNOUNCEMENT}</label></dt> + <dd><strong><a href="{updates_available.announcement}">{updates_available.announcement}</a></strong></dd> + </dl> + </fieldset> +<!-- END updates_available --> +<!-- IF not S_UP_TO_DATE --> + {UPDATE_INSTRUCTIONS} + <br /><br /> <!-- ENDIF --> <!-- INCLUDE overall_footer.html --> 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); + $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)); } - else + catch (\RuntimeException $e) { - $latest_version_info = explode("\n", $latest_version_info); - $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); - $info = explode("\n", $info); - $latest_version = trim($info[0]); - - $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'); + $updates_available = array(); + } - // next feature release - $next_feature_version = $next_feature_announcement_url = false; - if (isset($info[2]) && trim($info[2]) !== '') + foreach ($updates_available as $branch => $version_data) { - $next_feature_version = trim($info[2]); - $next_feature_announcement_url = trim($info[3]); + $template->assign_block_vars('updates_available', $version_data); } - // 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']; + $update_link = append_sid($phpbb_root_path . 'install/index.' . $phpEx, 'mode=update'); $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 @@ -2925,72 +2925,6 @@ 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 @@ -3100,45 +3034,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. * * @param string $table_name The table to update 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( <h1>Release announcement</h1> - <p>Please read <a href="%1$s" title="%1$s"><strong>the release announcement for the latest version</strong></a> before you continue your update process, it may contain useful information. It also contains full download links as well as the change log.</p> + <p>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.</p> <br /> @@ -547,17 +548,11 @@ $lang = array_merge($lang, array( </ul> <p>Once uploaded your board will be offline for normal users due to the install directory you uploaded now present.<br /><br /> - <strong><a href="%2$s" title="%2$s">Now start the update process by pointing your browser to the install folder</a>.</strong><br /> + <strong><a href="%1$s" title="%1$s">Now start the update process by pointing your browser to the install folder</a>.</strong><br /> <br /> You will then be guided through the update process. You will be notified once the update is complete. </p> ', - 'UPDATE_INSTRUCTIONS_INCOMPLETE' => ' - - <h1>Incomplete update detected</h1> - - <p>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.</p> - ', '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 <strong>%1$s</strong> is available. Please read <a href="%2$s" title="%2$s"><strong>the release announcement</strong></a> 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 @@ +<?php +/** +* +* @package phpBB3 +* @copyright (c) 2014 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +namespace phpbb; + +/** + * Class to handle version checking and comparison + */ +class version_helper +{ + protected $cache; + protected $config; + protected $user; + + public function __construct($cache, $config, $user) + { + $this->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 @@ -<?php -/** -* -* @package testing -* @copyright (c) 2013 phpBB Group -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 -* -*/ - -require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; -require_once dirname(__FILE__) . '/../../phpBB/includes/functions_admin.php'; - -/** -* @group slow -*/ -class phpbb_functions_get_remote_file extends phpbb_test_case -{ - public function test_version_phpbb_com() - { - $hostname = 'version.phpbb.com'; - - if (!phpbb_checkdnsrr($hostname, 'A')) - { - $this->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 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2014 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +class phpbb_version_helper_test extends phpbb_test_case +{ + public function setUp() + { + parent::setUp(); + + $this->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); + } +} |