VersionInformation.php 6.9 KB

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