Removing old documentation
[openemr.git] / phpmyadmin / libraries / VersionInformation.php
blob555ddded892a70bb693852511896cecca89cf2ec
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Responsile for retrieving version information and notifiying about latest version
6 * @package PhpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
13 * Responsile for retrieving version information and notifiying about latest version
15 * @package PhpMyAdmin
18 class VersionInformation
20 /**
21 * Returns information with latest version from phpmyadmin.net
23 * @return object JSON decoded object with the data
25 public function getLatestVersion()
27 if (!$GLOBALS['cfg']['VersionCheck']) {
28 return null;
31 // wait 3s at most for server response, it's enough to get information
32 // from a working server
33 $connection_timeout = 3;
35 $response = '{}';
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 if (ini_get('allow_url_fopen')) {
47 $context = array(
48 'http' => array(
49 'request_fulluri' => true,
50 'timeout' => $connection_timeout,
53 $context = PMA_Util::handleContext($context);
54 if (! defined('TESTSUITE')) {
55 session_write_close();
57 $response = file_get_contents(
58 $file,
59 false,
60 stream_context_create($context)
62 } else if (function_exists('curl_init')) {
63 $curl_handle = curl_init($file);
64 if ($curl_handle === false) {
65 return null;
67 $curl_handle = PMA_Util::configureCurl($curl_handle);
68 curl_setopt(
69 $curl_handle,
70 CURLOPT_HEADER,
71 false
73 curl_setopt(
74 $curl_handle,
75 CURLOPT_RETURNTRANSFER,
76 true
78 curl_setopt(
79 $curl_handle,
80 CURLOPT_TIMEOUT,
81 $connection_timeout
83 if (! defined('TESTSUITE')) {
84 session_write_close();
86 $response = curl_exec($curl_handle);
90 $data = json_decode($response);
91 if (is_object($data)
92 && ! empty($data->version)
93 && ! empty($data->date)
94 && $save
95 ) {
96 if (! isset($_SESSION) && ! defined('TESTSUITE')) {
97 ini_set('session.use_only_cookies', 'false');
98 ini_set('session.use_cookies', 'false');
99 ini_set('session.use_trans_sid', 'false');
100 ini_set('session.cache_limiter', 'nocache');
101 session_start();
103 $_SESSION['cache']['version_check'] = array(
104 'response' => $response,
105 'timestamp' => time()
108 return $data;
112 * Calculates numerical equivalent of phpMyAdmin version string
114 * @param string $version version
116 * @return mixed false on failure, integer on success
118 public function versionToInt($version)
120 $parts = explode('-', $version);
121 if (count($parts) > 1) {
122 $suffix = $parts[1];
123 } else {
124 $suffix = '';
126 $parts = explode('.', $parts[0]);
128 $result = 0;
130 if (count($parts) >= 1 && is_numeric($parts[0])) {
131 $result += 1000000 * $parts[0];
134 if (count($parts) >= 2 && is_numeric($parts[1])) {
135 $result += 10000 * $parts[1];
138 if (count($parts) >= 3 && is_numeric($parts[2])) {
139 $result += 100 * $parts[2];
142 if (count($parts) >= 4 && is_numeric($parts[3])) {
143 $result += 1 * $parts[3];
146 if (!empty($suffix)) {
147 $matches = array();
148 if (preg_match('/^(\D+)(\d+)$/', $suffix, $matches)) {
149 $suffix = $matches[1];
150 $result += intval($matches[2]);
152 switch ($suffix) {
153 case 'pl':
154 $result += 60;
155 break;
156 case 'rc':
157 $result += 30;
158 break;
159 case 'beta':
160 $result += 20;
161 break;
162 case 'alpha':
163 $result += 10;
164 break;
165 case 'dev':
166 $result += 0;
167 break;
169 } else {
170 $result += 50; // for final
173 return $result;
177 * Returns the version and date of the latest phpMyAdmin version compatible
178 * with avilable PHP and MySQL versions
180 * @param array $releases array of information related to each version
182 * @return array containing the version and date of latest compatibel version
184 public function getLatestCompatibleVersion($releases)
186 foreach ($releases as $release) {
187 $phpVersions = $release->php_versions;
188 $phpConditions = explode(",", $phpVersions);
189 foreach ($phpConditions as $phpCondition) {
190 if (! $this->evaluateVersionCondition("PHP", $phpCondition)) {
191 continue 2;
195 // We evalute MySQL version constraint if there are only
196 // one server configured.
197 if (count($GLOBALS['cfg']['Servers']) == 1) {
198 $mysqlVersions = $release->mysql_versions;
199 $mysqlConditions = explode(",", $mysqlVersions);
200 foreach ($mysqlConditions as $mysqlCondition) {
201 if (! $this->evaluateVersionCondition('MySQL', $mysqlCondition)) {
202 continue 2;
207 return array(
208 'version' => $release->version,
209 'date' => $release->date,
213 // no compatible version
214 return null;
218 * Checks whether PHP or MySQL version meets supplied version condition
220 * @param string $type PHP or MySQL
221 * @param string $condition version condition
223 * @return boolean whether the condition is met
225 public function evaluateVersionCondition($type, $condition)
227 $operator = null;
228 $operators = array("<=", ">=", "!=", "<>", "<", ">", "="); // preserve order
229 foreach ($operators as $oneOperator) {
230 if (strpos($condition, $oneOperator) === 0) {
231 $operator = $oneOperator;
232 $version = substr($condition, strlen($oneOperator));
233 break;
237 $myVersion = null;
238 if ($type == 'PHP') {
239 $myVersion = $this->getPHPVersion();
240 } elseif ($type == 'MySQL') {
241 $myVersion = $this->getMySQLVersion();
244 if ($myVersion != null && $operator != null) {
245 return version_compare($myVersion, $version, $operator);
247 return false;
251 * Returns the PHP version
253 * @return string PHP version
255 protected function getPHPVersion()
257 return PHP_VERSION;
261 * Returns the MySQL version
263 * @return string MySQL version
265 protected function getMySQLVersion()
267 return PMA_Util::cacheGet('PMA_MYSQL_STR_VERSION');