2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Responsible for retrieving version information and notifiying about latest version
8 declare(strict_types
=1);
12 use PhpMyAdmin\Utils\HttpRequest
;
16 * Responsible for retrieving version information and notifiying about latest version
21 class VersionInformation
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']) {
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
40 $response = $_SESSION['cache']['version_check']['response'];
43 $file = 'https://www.phpmyadmin.net/home_page/version.json';
44 $httpRequest = new HttpRequest();
45 $response = $httpRequest->create($file, 'GET');
47 $response = $response ?
: '{}';
49 $data = json_decode($response);
51 /* Basic sanity checking */
52 if (! is_object($data)
53 ||
empty($data->version
)
54 ||
empty($data->releases
)
61 $_SESSION['cache']['version_check'] = [
62 'response' => $response,
63 'timestamp' => time(),
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) {
84 $parts = explode('.', $parts[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)) {
106 if (preg_match('/^(\D+)(\d+)$/', $suffix, $matches)) {
107 $suffix = $matches[1];
108 $result +
= intval($matches[2]);
128 $result +
= 50; // for final
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)) {
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)) {
166 'version' => $release->version
,
167 'date' => $release->date
,
171 // no compatible version
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)
196 foreach ($operators as $oneOperator) {
197 if (strpos($condition, $oneOperator) === 0) {
198 $operator = $oneOperator;
199 $version = substr($condition, strlen($oneOperator));
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);
218 * Returns the PHP version
220 * @return string PHP version
222 protected function getPHPVersion()
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();