Translated using Weblate (Estonian)
[phpmyadmin.git] / libraries / classes / VersionInformation.php
blobe6f42dbfad5b14a3763fcd51eb76eb9dfbe99cb4
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 declare(strict_types=1);
10 namespace PhpMyAdmin;
12 use PhpMyAdmin\Utils\HttpRequest;
13 use \stdClass;
15 /**
16 * Responsible for retrieving version information and notifiying about latest version
18 * @package PhpMyAdmin
21 class VersionInformation
23 /**
24 * Returns information with latest version from phpmyadmin.net
26 * @return stdClass|null JSON decoded object with the data
28 public function getLatestVersion(): ?stdClass
30 if (! $GLOBALS['cfg']['VersionCheck']) {
31 return null;
34 // Get response text from phpmyadmin.net or from the session
35 // Update cache every 6 hours
36 if (isset($_SESSION['cache']['version_check'])
37 && time() < $_SESSION['cache']['version_check']['timestamp'] + 3600 * 6
38 ) {
39 $save = false;
40 $response = $_SESSION['cache']['version_check']['response'];
41 } else {
42 $save = true;
43 $file = 'https://www.phpmyadmin.net/home_page/version.json';
44 $httpRequest = new HttpRequest();
45 $response = $httpRequest->create($file, 'GET');
47 $response = $response ?: '{}';
48 /* Parse response */
49 $data = json_decode($response);
51 /* Basic sanity checking */
52 if (! is_object($data)
53 || empty($data->version)
54 || empty($data->releases)
55 || empty($data->date)
56 ) {
57 return null;
60 if ($save) {
61 $_SESSION['cache']['version_check'] = [
62 'response' => $response,
63 'timestamp' => time(),
66 return $data;
69 /**
70 * Calculates numerical equivalent of phpMyAdmin version string
72 * @param string $version version
74 * @return mixed false on failure, integer on success
76 public function versionToInt($version)
78 $parts = explode('-', $version);
79 if (count($parts) > 1) {
80 $suffix = $parts[1];
81 } else {
82 $suffix = '';
84 $parts = explode('.', $parts[0]);
86 $result = 0;
88 if (count($parts) >= 1 && is_numeric($parts[0])) {
89 $result += 1000000 * (int) $parts[0];
92 if (count($parts) >= 2 && is_numeric($parts[1])) {
93 $result += 10000 * (int) $parts[1];
96 if (count($parts) >= 3 && is_numeric($parts[2])) {
97 $result += 100 * (int) $parts[2];
100 if (count($parts) >= 4 && is_numeric($parts[3])) {
101 $result += 1 * (int) $parts[3];
104 if (! empty($suffix)) {
105 $matches = [];
106 if (preg_match('/^(\D+)(\d+)$/', $suffix, $matches)) {
107 $suffix = $matches[1];
108 $result += intval($matches[2]);
110 switch ($suffix) {
111 case 'pl':
112 $result += 60;
113 break;
114 case 'rc':
115 $result += 30;
116 break;
117 case 'beta':
118 $result += 20;
119 break;
120 case 'alpha':
121 $result += 10;
122 break;
123 case 'dev':
124 $result += 0;
125 break;
127 } else {
128 $result += 50; // for final
131 return $result;
135 * Returns the version and date of the latest phpMyAdmin version compatible
136 * with the available PHP and MySQL versions
138 * @param array $releases array of information related to each version
140 * @return array|null containing the version and date of latest compatible version
142 public function getLatestCompatibleVersion(array $releases)
144 foreach ($releases as $release) {
145 $phpVersions = $release->php_versions;
146 $phpConditions = explode(",", $phpVersions);
147 foreach ($phpConditions as $phpCondition) {
148 if (! $this->evaluateVersionCondition('PHP', $phpCondition)) {
149 continue 2;
153 // We evalute MySQL version constraint if there are only
154 // one server configured.
155 if (count($GLOBALS['cfg']['Servers']) === 1) {
156 $mysqlVersions = $release->mysql_versions;
157 $mysqlConditions = explode(",", $mysqlVersions);
158 foreach ($mysqlConditions as $mysqlCondition) {
159 if (! $this->evaluateVersionCondition('MySQL', $mysqlCondition)) {
160 continue 2;
165 return [
166 'version' => $release->version,
167 'date' => $release->date,
171 // no compatible version
172 return null;
176 * Checks whether PHP or MySQL version meets supplied version condition
178 * @param string $type PHP or MySQL
179 * @param string $condition version condition
181 * @return boolean whether the condition is met
183 public function evaluateVersionCondition(string $type, string $condition)
185 $operator = null;
186 $version = null;
187 $operators = [
188 "<=",
189 ">=",
190 "!=",
191 "<>",
192 "<",
193 ">",
194 "=",
195 ]; // preserve order
196 foreach ($operators as $oneOperator) {
197 if (strpos($condition, $oneOperator) === 0) {
198 $operator = $oneOperator;
199 $version = substr($condition, strlen($oneOperator));
200 break;
204 $myVersion = null;
205 if ($type == 'PHP') {
206 $myVersion = $this->getPHPVersion();
207 } elseif ($type == 'MySQL') {
208 $myVersion = $this->getMySQLVersion();
211 if ($myVersion !== null && $operator !== null) {
212 return version_compare($myVersion, $version, $operator);
214 return false;
218 * Returns the PHP version
220 * @return string PHP version
222 protected function getPHPVersion()
224 return PHP_VERSION;
228 * Returns the MySQL version if connected to a database
230 * @return string|null MySQL version
232 protected function getMySQLVersion()
234 if (isset($GLOBALS['dbi'])) {
235 return $GLOBALS['dbi']->getVersionString();
237 return null;