VersionInformation.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Responsile for retrieving version information and notifiying about latest version
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. * Responsile for retrieving version information and notifiying about latest version
  13. *
  14. * @package PhpMyAdmin
  15. *
  16. */
  17. class VersionInformation
  18. {
  19. /**
  20. * Returns information with latest version from phpmyadmin.net
  21. *
  22. * @return object JSON decoded object with the data
  23. */
  24. public function getLatestVersion()
  25. {
  26. if (!$GLOBALS['cfg']['VersionCheck']) {
  27. return null;
  28. }
  29. // wait 3s at most for server response, it's enough to get information
  30. // from a working server
  31. $connection_timeout = 3;
  32. $response = '{}';
  33. // Get response text from phpmyadmin.net or from the session
  34. // Update cache every 6 hours
  35. if (isset($_SESSION['cache']['version_check'])
  36. && time() < $_SESSION['cache']['version_check']['timestamp'] + 3600 * 6
  37. ) {
  38. $save = false;
  39. $response = $_SESSION['cache']['version_check']['response'];
  40. } else {
  41. $save = true;
  42. $file = 'https://www.phpmyadmin.net/home_page/version.json';
  43. if (ini_get('allow_url_fopen')) {
  44. $context = array(
  45. 'http' => array(
  46. 'request_fulluri' => true,
  47. 'timeout' => $connection_timeout,
  48. )
  49. );
  50. $context = PMA_Util::handleContext($context);
  51. if (! defined('TESTSUITE')) {
  52. session_write_close();
  53. }
  54. $response = file_get_contents(
  55. $file,
  56. false,
  57. stream_context_create($context)
  58. );
  59. } else if (function_exists('curl_init')) {
  60. $curl_handle = curl_init($file);
  61. if ($curl_handle === false) {
  62. return null;
  63. }
  64. $curl_handle = PMA_Util::configureCurl($curl_handle);
  65. curl_setopt(
  66. $curl_handle,
  67. CURLOPT_HEADER,
  68. false
  69. );
  70. curl_setopt(
  71. $curl_handle,
  72. CURLOPT_RETURNTRANSFER,
  73. true
  74. );
  75. curl_setopt(
  76. $curl_handle,
  77. CURLOPT_TIMEOUT,
  78. $connection_timeout
  79. );
  80. if (! defined('TESTSUITE')) {
  81. session_write_close();
  82. }
  83. $response = curl_exec($curl_handle);
  84. }
  85. }
  86. $data = json_decode($response);
  87. if (is_object($data)
  88. && ! empty($data->version)
  89. && ! empty($data->date)
  90. && $save
  91. ) {
  92. if (! isset($_SESSION) && ! defined('TESTSUITE')) {
  93. ini_set('session.use_only_cookies', 'false');
  94. ini_set('session.use_cookies', 'false');
  95. ini_set('session.use_trans_sid', 'false');
  96. ini_set('session.cache_limiter', 'nocache');
  97. session_start();
  98. }
  99. $_SESSION['cache']['version_check'] = array(
  100. 'response' => $response,
  101. 'timestamp' => time()
  102. );
  103. }
  104. return $data;
  105. }
  106. /**
  107. * Calculates numerical equivalent of phpMyAdmin version string
  108. *
  109. * @param string $version version
  110. *
  111. * @return mixed false on failure, integer on success
  112. */
  113. public function versionToInt($version)
  114. {
  115. $parts = explode('-', $version);
  116. if (count($parts) > 1) {
  117. $suffix = $parts[1];
  118. } else {
  119. $suffix = '';
  120. }
  121. $parts = explode('.', $parts[0]);
  122. $result = 0;
  123. if (count($parts) >= 1 && is_numeric($parts[0])) {
  124. $result += 1000000 * $parts[0];
  125. }
  126. if (count($parts) >= 2 && is_numeric($parts[1])) {
  127. $result += 10000 * $parts[1];
  128. }
  129. if (count($parts) >= 3 && is_numeric($parts[2])) {
  130. $result += 100 * $parts[2];
  131. }
  132. if (count($parts) >= 4 && is_numeric($parts[3])) {
  133. $result += 1 * $parts[3];
  134. }
  135. if (!empty($suffix)) {
  136. $matches = array();
  137. if (preg_match('/^(\D+)(\d+)$/', $suffix, $matches)) {
  138. $suffix = $matches[1];
  139. $result += intval($matches[2]);
  140. }
  141. switch ($suffix) {
  142. case 'pl':
  143. $result += 60;
  144. break;
  145. case 'rc':
  146. $result += 30;
  147. break;
  148. case 'beta':
  149. $result += 20;
  150. break;
  151. case 'alpha':
  152. $result += 10;
  153. break;
  154. case 'dev':
  155. $result += 0;
  156. break;
  157. }
  158. } else {
  159. $result += 50; // for final
  160. }
  161. return $result;
  162. }
  163. /**
  164. * Returns the version and date of the latest phpMyAdmin version compatible
  165. * with avilable PHP and MySQL versions
  166. *
  167. * @param array $releases array of information related to each version
  168. *
  169. * @return array containing the version and date of latest compatibel version
  170. */
  171. public function getLatestCompatibleVersion($releases)
  172. {
  173. foreach ($releases as $release) {
  174. $phpVersions = $release->php_versions;
  175. $phpConditions = explode(",", $phpVersions);
  176. foreach ($phpConditions as $phpCondition) {
  177. if (! $this->evaluateVersionCondition("PHP", $phpCondition)) {
  178. continue 2;
  179. }
  180. }
  181. // We evalute MySQL version constraint if there are only
  182. // one server configured.
  183. if (count($GLOBALS['cfg']['Servers']) == 1) {
  184. $mysqlVersions = $release->mysql_versions;
  185. $mysqlConditions = explode(",", $mysqlVersions);
  186. foreach ($mysqlConditions as $mysqlCondition) {
  187. if (! $this->evaluateVersionCondition('MySQL', $mysqlCondition)) {
  188. continue 2;
  189. }
  190. }
  191. }
  192. return array(
  193. 'version' => $release->version,
  194. 'date' => $release->date,
  195. );
  196. }
  197. // no compatible version
  198. return null;
  199. }
  200. /**
  201. * Checks whether PHP or MySQL version meets supplied version condition
  202. *
  203. * @param string $type PHP or MySQL
  204. * @param string $condition version condition
  205. *
  206. * @return boolean whether the condition is met
  207. */
  208. public function evaluateVersionCondition($type, $condition)
  209. {
  210. $operator = null;
  211. $operators = array("<=", ">=", "!=", "<>", "<", ">", "="); // preserve order
  212. foreach ($operators as $oneOperator) {
  213. if (strpos($condition, $oneOperator) === 0) {
  214. $operator = $oneOperator;
  215. $version = substr($condition, strlen($oneOperator));
  216. break;
  217. }
  218. }
  219. $myVersion = null;
  220. if ($type == 'PHP') {
  221. $myVersion = $this->getPHPVersion();
  222. } elseif ($type == 'MySQL') {
  223. $myVersion = $this->getMySQLVersion();
  224. }
  225. if ($myVersion != null && $operator != null) {
  226. return version_compare($myVersion, $version, $operator);
  227. }
  228. return false;
  229. }
  230. /**
  231. * Returns the PHP version
  232. *
  233. * @return string PHP version
  234. */
  235. protected function getPHPVersion()
  236. {
  237. return PHP_VERSION;
  238. }
  239. /**
  240. * Returns the MySQL version
  241. *
  242. * @return string MySQL version
  243. */
  244. protected function getMySQLVersion()
  245. {
  246. return PMA_Util::cacheGet('PMA_MYSQL_STR_VERSION');
  247. }
  248. }
  249. ?>