Translated using Weblate (Chinese (Traditional))
[phpmyadmin.git] / libraries / VersionInformation.php
blob960f9618ad86a09d7035ba4666a15a294191984f
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Responsible for retrieving version information and notifiying about latest version
6 * @package PhpMyAdmin
7 */
8 namespace PMA\libraries;
10 use PMA\libraries\Util;
11 use stdClass;
13 if (!defined('PHPMYADMIN')) {
14 exit;
17 /**
18 * Responsible for retrieving version information and notifiying about latest version
20 * @package PhpMyAdmin
23 class VersionInformation
25 /**
26 * Returns information with latest version from phpmyadmin.net
28 * @return object JSON decoded object with the data
30 public function getLatestVersion()
32 if (!$GLOBALS['cfg']['VersionCheck']) {
33 return null;
36 // Get response text from phpmyadmin.net or from the session
37 // Update cache every 6 hours
38 if (isset($_SESSION['cache']['version_check'])
39 && time() < $_SESSION['cache']['version_check']['timestamp'] + 3600 * 6
40 ) {
41 $save = false;
42 $response = $_SESSION['cache']['version_check']['response'];
43 } else {
44 $save = true;
45 $file = 'https://www.phpmyadmin.net/home_page/version.json';
46 $response = Util::httpRequest($file, "GET");
48 $response = $response ? $response : '{}';
49 /* Parse response */
50 $data = json_decode($response);
52 /* Basic sanity checking */
53 if (! is_object($data)
54 || empty($data->version)
55 || empty($data->releases)
56 || empty($data->date)
57 ) {
58 return null;
61 if ($save) {
62 $_SESSION['cache']['version_check'] = array(
63 'response' => $response,
64 'timestamp' => time()
67 return $data;
70 /**
71 * Calculates numerical equivalent of phpMyAdmin version string
73 * @param string $version version
75 * @return mixed false on failure, integer on success
77 public function versionToInt($version)
79 $parts = explode('-', $version);
80 if (count($parts) > 1) {
81 $suffix = $parts[1];
82 } else {
83 $suffix = '';
85 $parts = explode('.', $parts[0]);
87 $result = 0;
89 if (count($parts) >= 1 && is_numeric($parts[0])) {
90 $result += 1000000 * $parts[0];
93 if (count($parts) >= 2 && is_numeric($parts[1])) {
94 $result += 10000 * $parts[1];
97 if (count($parts) >= 3 && is_numeric($parts[2])) {
98 $result += 100 * $parts[2];
101 if (count($parts) >= 4 && is_numeric($parts[3])) {
102 $result += 1 * $parts[3];
105 if (!empty($suffix)) {
106 $matches = array();
107 if (preg_match('/^(\D+)(\d+)$/', $suffix, $matches)) {
108 $suffix = $matches[1];
109 $result += intval($matches[2]);
111 switch ($suffix) {
112 case 'pl':
113 $result += 60;
114 break;
115 case 'rc':
116 $result += 30;
117 break;
118 case 'beta':
119 $result += 20;
120 break;
121 case 'alpha':
122 $result += 10;
123 break;
124 case 'dev':
125 $result += 0;
126 break;
128 } else {
129 $result += 50; // for final
132 return $result;
136 * Returns the version and date of the latest phpMyAdmin version compatible
137 * with the available PHP and MySQL versions
139 * @param array $releases array of information related to each version
141 * @return array containing the version and date of latest compatible version
143 public function getLatestCompatibleVersion($releases)
145 foreach ($releases as $release) {
146 $phpVersions = $release->php_versions;
147 $phpConditions = explode(",", $phpVersions);
148 foreach ($phpConditions as $phpCondition) {
149 if (! $this->evaluateVersionCondition("PHP", $phpCondition)) {
150 continue 2;
154 // We evalute MySQL version constraint if there are only
155 // one server configured.
156 if (count($GLOBALS['cfg']['Servers']) == 1) {
157 $mysqlVersions = $release->mysql_versions;
158 $mysqlConditions = explode(",", $mysqlVersions);
159 foreach ($mysqlConditions as $mysqlCondition) {
160 if (!$this->evaluateVersionCondition('MySQL', $mysqlCondition)) {
161 continue 2;
166 return array(
167 'version' => $release->version,
168 'date' => $release->date,
172 // no compatible version
173 return null;
177 * Checks whether PHP or MySQL version meets supplied version condition
179 * @param string $type PHP or MySQL
180 * @param string $condition version condition
182 * @return boolean whether the condition is met
184 public function evaluateVersionCondition($type, $condition)
186 $operator = null;
187 $operators = array("<=", ">=", "!=", "<>", "<", ">", "="); // preserve order
188 foreach ($operators as $oneOperator) {
189 if (strpos($condition, $oneOperator) === 0) {
190 $operator = $oneOperator;
191 $version = substr($condition, strlen($oneOperator));
192 break;
196 $myVersion = null;
197 if ($type == 'PHP') {
198 $myVersion = $this->getPHPVersion();
199 } elseif ($type == 'MySQL') {
200 $myVersion = $this->getMySQLVersion();
203 if ($myVersion != null && $operator != null) {
204 return version_compare($myVersion, $version, $operator);
206 return false;
210 * Returns the PHP version
212 * @return string PHP version
214 protected function getPHPVersion()
216 return PHP_VERSION;
220 * Returns the MySQL version
222 * @return string MySQL version
224 protected function getMySQLVersion()
226 return PMA_MYSQL_STR_VERSION;