PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/laravel/public/phpMyAdmin/libraries/VersionInformation.php

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