VersionInformation.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Responsible for retrieving version information and notifiying about latest version
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. namespace PhpMyAdmin;
  9. use PhpMyAdmin\Util;
  10. use PhpMyAdmin\Utils\HttpRequest;
  11. /**
  12. * Responsible 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. // Get response text from phpmyadmin.net or from the session
  30. // Update cache every 6 hours
  31. if (isset($_SESSION['cache']['version_check'])
  32. && time() < $_SESSION['cache']['version_check']['timestamp'] + 3600 * 6
  33. ) {
  34. $save = false;
  35. $response = $_SESSION['cache']['version_check']['response'];
  36. } else {
  37. $save = true;
  38. $file = 'https://www.phpmyadmin.net/home_page/version.json';
  39. $httpRequest = new HttpRequest();
  40. $response = $httpRequest->create($file, 'GET');
  41. }
  42. $response = $response ? $response : '{}';
  43. /* Parse response */
  44. $data = json_decode($response);
  45. /* Basic sanity checking */
  46. if (! is_object($data)
  47. || empty($data->version)
  48. || empty($data->releases)
  49. || empty($data->date)
  50. ) {
  51. return null;
  52. }
  53. if ($save) {
  54. $_SESSION['cache']['version_check'] = array(
  55. 'response' => $response,
  56. 'timestamp' => time()
  57. );
  58. }
  59. return $data;
  60. }
  61. /**
  62. * Calculates numerical equivalent of phpMyAdmin version string
  63. *
  64. * @param string $version version
  65. *
  66. * @return mixed false on failure, integer on success
  67. */
  68. public function versionToInt($version)
  69. {
  70. $parts = explode('-', $version);
  71. if (count($parts) > 1) {
  72. $suffix = $parts[1];
  73. } else {
  74. $suffix = '';
  75. }
  76. $parts = explode('.', $parts[0]);
  77. $result = 0;
  78. if (count($parts) >= 1 && is_numeric($parts[0])) {
  79. $result += 1000000 * $parts[0];
  80. }
  81. if (count($parts) >= 2 && is_numeric($parts[1])) {
  82. $result += 10000 * $parts[1];
  83. }
  84. if (count($parts) >= 3 && is_numeric($parts[2])) {
  85. $result += 100 * $parts[2];
  86. }
  87. if (count($parts) >= 4 && is_numeric($parts[3])) {
  88. $result += 1 * $parts[3];
  89. }
  90. if (!empty($suffix)) {
  91. $matches = array();
  92. if (preg_match('/^(\D+)(\d+)$/', $suffix, $matches)) {
  93. $suffix = $matches[1];
  94. $result += intval($matches[2]);
  95. }
  96. switch ($suffix) {
  97. case 'pl':
  98. $result += 60;
  99. break;
  100. case 'rc':
  101. $result += 30;
  102. break;
  103. case 'beta':
  104. $result += 20;
  105. break;
  106. case 'alpha':
  107. $result += 10;
  108. break;
  109. case 'dev':
  110. $result += 0;
  111. break;
  112. }
  113. } else {
  114. $result += 50; // for final
  115. }
  116. return $result;
  117. }
  118. /**
  119. * Returns the version and date of the latest phpMyAdmin version compatible
  120. * with the available PHP and MySQL versions
  121. *
  122. * @param array $releases array of information related to each version
  123. *
  124. * @return array containing the version and date of latest compatible version
  125. */
  126. public function getLatestCompatibleVersion(array $releases)
  127. {
  128. foreach ($releases as $release) {
  129. $phpVersions = $release->php_versions;
  130. $phpConditions = explode(",", $phpVersions);
  131. foreach ($phpConditions as $phpCondition) {
  132. if (! $this->evaluateVersionCondition("PHP", $phpCondition)) {
  133. continue 2;
  134. }
  135. }
  136. // We evalute MySQL version constraint if there are only
  137. // one server configured.
  138. if (count($GLOBALS['cfg']['Servers']) == 1) {
  139. $mysqlVersions = $release->mysql_versions;
  140. $mysqlConditions = explode(",", $mysqlVersions);
  141. foreach ($mysqlConditions as $mysqlCondition) {
  142. if (!$this->evaluateVersionCondition('MySQL', $mysqlCondition)) {
  143. continue 2;
  144. }
  145. }
  146. }
  147. return array(
  148. 'version' => $release->version,
  149. 'date' => $release->date,
  150. );
  151. }
  152. // no compatible version
  153. return null;
  154. }
  155. /**
  156. * Checks whether PHP or MySQL version meets supplied version condition
  157. *
  158. * @param string $type PHP or MySQL
  159. * @param string $condition version condition
  160. *
  161. * @return boolean whether the condition is met
  162. */
  163. public function evaluateVersionCondition($type, $condition)
  164. {
  165. $operator = null;
  166. $operators = array("<=", ">=", "!=", "<>", "<", ">", "="); // preserve order
  167. foreach ($operators as $oneOperator) {
  168. if (strpos($condition, $oneOperator) === 0) {
  169. $operator = $oneOperator;
  170. $version = substr($condition, strlen($oneOperator));
  171. break;
  172. }
  173. }
  174. $myVersion = null;
  175. if ($type == 'PHP') {
  176. $myVersion = $this->getPHPVersion();
  177. } elseif ($type == 'MySQL') {
  178. $myVersion = $this->getMySQLVersion();
  179. }
  180. if ($myVersion != null && $operator != null) {
  181. return version_compare($myVersion, $version, $operator);
  182. }
  183. return false;
  184. }
  185. /**
  186. * Returns the PHP version
  187. *
  188. * @return string PHP version
  189. */
  190. protected function getPHPVersion()
  191. {
  192. return PHP_VERSION;
  193. }
  194. /**
  195. * Returns the MySQL version if connected to a database
  196. *
  197. * @return string MySQL version
  198. */
  199. protected function getMySQLVersion()
  200. {
  201. if (isset($GLOBALS['dbi'])) {
  202. return $GLOBALS['dbi']->getVersionString();
  203. }
  204. return null;
  205. }
  206. }