Translated using Weblate (Czech)
[phpmyadmin.git] / libraries / classes / VersionInformation.php
blob46334712742b01be1e6735b8e2c93c3c63d57ad4
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 PhpMyAdmin;
10 use PhpMyAdmin\Util;
11 use PhpMyAdmin\Utils\HttpRequest;
13 /**
14 * Responsible for retrieving version information and notifiying about latest version
16 * @package PhpMyAdmin
19 class VersionInformation
21 /**
22 * Returns information with latest version from phpmyadmin.net
24 * @return object JSON decoded object with the data
26 public function getLatestVersion()
28 if (!$GLOBALS['cfg']['VersionCheck']) {
29 return null;
32 // Get response text from phpmyadmin.net or from the session
33 // Update cache every 6 hours
34 if (isset($_SESSION['cache']['version_check'])
35 && time() < $_SESSION['cache']['version_check']['timestamp'] + 3600 * 6
36 ) {
37 $save = false;
38 $response = $_SESSION['cache']['version_check']['response'];
39 } else {
40 $save = true;
41 $file = 'https://www.phpmyadmin.net/home_page/version.json';
42 $httpRequest = new HttpRequest();
43 $response = $httpRequest->create($file, 'GET');
45 $response = $response ? $response : '{}';
46 /* Parse response */
47 $data = json_decode($response);
49 /* Basic sanity checking */
50 if (! is_object($data)
51 || empty($data->version)
52 || empty($data->releases)
53 || empty($data->date)
54 ) {
55 return null;
58 if ($save) {
59 $_SESSION['cache']['version_check'] = array(
60 'response' => $response,
61 'timestamp' => time()
64 return $data;
67 /**
68 * Calculates numerical equivalent of phpMyAdmin version string
70 * @param string $version version
72 * @return mixed false on failure, integer on success
74 public function versionToInt($version)
76 $parts = explode('-', $version);
77 if (count($parts) > 1) {
78 $suffix = $parts[1];
79 } else {
80 $suffix = '';
82 $parts = explode('.', $parts[0]);
84 $result = 0;
86 if (count($parts) >= 1 && is_numeric($parts[0])) {
87 $result += 1000000 * $parts[0];
90 if (count($parts) >= 2 && is_numeric($parts[1])) {
91 $result += 10000 * $parts[1];
94 if (count($parts) >= 3 && is_numeric($parts[2])) {
95 $result += 100 * $parts[2];
98 if (count($parts) >= 4 && is_numeric($parts[3])) {
99 $result += 1 * $parts[3];
102 if (!empty($suffix)) {
103 $matches = array();
104 if (preg_match('/^(\D+)(\d+)$/', $suffix, $matches)) {
105 $suffix = $matches[1];
106 $result += intval($matches[2]);
108 switch ($suffix) {
109 case 'pl':
110 $result += 60;
111 break;
112 case 'rc':
113 $result += 30;
114 break;
115 case 'beta':
116 $result += 20;
117 break;
118 case 'alpha':
119 $result += 10;
120 break;
121 case 'dev':
122 $result += 0;
123 break;
125 } else {
126 $result += 50; // for final
129 return $result;
133 * Returns the version and date of the latest phpMyAdmin version compatible
134 * with the available PHP and MySQL versions
136 * @param array $releases array of information related to each version
138 * @return array containing the version and date of latest compatible version
140 public function getLatestCompatibleVersion(array $releases)
142 foreach ($releases as $release) {
143 $phpVersions = $release->php_versions;
144 $phpConditions = explode(",", $phpVersions);
145 foreach ($phpConditions as $phpCondition) {
146 if (! $this->evaluateVersionCondition("PHP", $phpCondition)) {
147 continue 2;
151 // We evalute MySQL version constraint if there are only
152 // one server configured.
153 if (count($GLOBALS['cfg']['Servers']) == 1) {
154 $mysqlVersions = $release->mysql_versions;
155 $mysqlConditions = explode(",", $mysqlVersions);
156 foreach ($mysqlConditions as $mysqlCondition) {
157 if (!$this->evaluateVersionCondition('MySQL', $mysqlCondition)) {
158 continue 2;
163 return array(
164 'version' => $release->version,
165 'date' => $release->date,
169 // no compatible version
170 return null;
174 * Checks whether PHP or MySQL version meets supplied version condition
176 * @param string $type PHP or MySQL
177 * @param string $condition version condition
179 * @return boolean whether the condition is met
181 public function evaluateVersionCondition($type, $condition)
183 $operator = null;
184 $operators = array("<=", ">=", "!=", "<>", "<", ">", "="); // preserve order
185 foreach ($operators as $oneOperator) {
186 if (strpos($condition, $oneOperator) === 0) {
187 $operator = $oneOperator;
188 $version = substr($condition, strlen($oneOperator));
189 break;
193 $myVersion = null;
194 if ($type == 'PHP') {
195 $myVersion = $this->getPHPVersion();
196 } elseif ($type == 'MySQL') {
197 $myVersion = $this->getMySQLVersion();
200 if ($myVersion != null && $operator != null) {
201 return version_compare($myVersion, $version, $operator);
203 return false;
207 * Returns the PHP version
209 * @return string PHP version
211 protected function getPHPVersion()
213 return PHP_VERSION;
217 * Returns the MySQL version if connected to a database
219 * @return string MySQL version
221 protected function getMySQLVersion()
223 if (isset($GLOBALS['dbi'])) {
224 return $GLOBALS['dbi']->getVersionString();
226 return null;