Index.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * Various checks and message functions used on index page.
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Setup;
  7. use PhpMyAdmin\Sanitize;
  8. use PhpMyAdmin\VersionInformation;
  9. use function htmlspecialchars;
  10. use function is_array;
  11. use function sprintf;
  12. use function uniqid;
  13. /**
  14. * PhpMyAdmin\Setup\Index class
  15. *
  16. * Various checks and message functions used on index page.
  17. */
  18. class Index
  19. {
  20. /**
  21. * Initializes message list
  22. *
  23. * @return void
  24. */
  25. public static function messagesBegin()
  26. {
  27. if (! isset($_SESSION['messages']) || ! is_array($_SESSION['messages'])) {
  28. $_SESSION['messages'] = [
  29. 'error' => [],
  30. 'notice' => [],
  31. ];
  32. } else {
  33. // reset message states
  34. foreach ($_SESSION['messages'] as &$messages) {
  35. foreach ($messages as &$msg) {
  36. $msg['fresh'] = false;
  37. $msg['active'] = false;
  38. }
  39. }
  40. }
  41. }
  42. /**
  43. * Adds a new message to message list
  44. *
  45. * @param string $type one of: notice, error
  46. * @param string $msgId unique message identifier
  47. * @param string $title language string id (in $str array)
  48. * @param string $message message text
  49. *
  50. * @return void
  51. */
  52. public static function messagesSet($type, $msgId, $title, $message)
  53. {
  54. $fresh = ! isset($_SESSION['messages'][$type][$msgId]);
  55. $_SESSION['messages'][$type][$msgId] = [
  56. 'fresh' => $fresh,
  57. 'active' => true,
  58. 'title' => $title,
  59. 'message' => $message,
  60. ];
  61. }
  62. /**
  63. * Cleans up message list
  64. *
  65. * @return void
  66. */
  67. public static function messagesEnd()
  68. {
  69. foreach ($_SESSION['messages'] as &$messages) {
  70. $remove_ids = [];
  71. foreach ($messages as $id => &$msg) {
  72. if ($msg['active'] != false) {
  73. continue;
  74. }
  75. $remove_ids[] = $id;
  76. }
  77. foreach ($remove_ids as $id) {
  78. unset($messages[$id]);
  79. }
  80. }
  81. }
  82. /**
  83. * Prints message list, must be called after self::messagesEnd()
  84. *
  85. * @return array
  86. */
  87. public static function messagesShowHtml()
  88. {
  89. $return = [];
  90. foreach ($_SESSION['messages'] as $type => $messages) {
  91. foreach ($messages as $id => $msg) {
  92. $return[] = [
  93. 'id' => $id,
  94. 'title' => $msg['title'],
  95. 'type' => $type,
  96. 'message' => $msg['message'],
  97. 'is_hidden' => ! $msg['fresh'] && $type !== 'error',
  98. ];
  99. }
  100. }
  101. return $return;
  102. }
  103. /**
  104. * Checks for newest phpMyAdmin version and sets result as a new notice
  105. *
  106. * @return void
  107. */
  108. public static function versionCheck()
  109. {
  110. // version check messages should always be visible so let's make
  111. // a unique message id each time we run it
  112. $message_id = uniqid('version_check');
  113. // Fetch data
  114. $versionInformation = new VersionInformation();
  115. $version_data = $versionInformation->getLatestVersion();
  116. if (empty($version_data)) {
  117. self::messagesSet(
  118. 'error',
  119. $message_id,
  120. __('Version check'),
  121. __(
  122. 'Reading of version failed. '
  123. . 'Maybe you\'re offline or the upgrade server does not respond.'
  124. )
  125. );
  126. return;
  127. }
  128. $releases = $version_data->releases;
  129. $latestCompatible = $versionInformation->getLatestCompatibleVersion($releases);
  130. if ($latestCompatible == null) {
  131. return;
  132. }
  133. $version = $latestCompatible['version'];
  134. $date = $latestCompatible['date'];
  135. $version_upstream = $versionInformation->versionToInt($version);
  136. if ($version_upstream === false) {
  137. self::messagesSet(
  138. 'error',
  139. $message_id,
  140. __('Version check'),
  141. __('Got invalid version string from server')
  142. );
  143. return;
  144. }
  145. $version_local = $versionInformation->versionToInt(
  146. $GLOBALS['PMA_Config']->get('PMA_VERSION')
  147. );
  148. if ($version_local === false) {
  149. self::messagesSet(
  150. 'error',
  151. $message_id,
  152. __('Version check'),
  153. __('Unparsable version string')
  154. );
  155. return;
  156. }
  157. if ($version_upstream > $version_local) {
  158. $version = htmlspecialchars($version);
  159. $date = htmlspecialchars($date);
  160. self::messagesSet(
  161. 'notice',
  162. $message_id,
  163. __('Version check'),
  164. sprintf(__('A newer version of phpMyAdmin is available and you should consider upgrading.'
  165. . ' The newest version is %s, released on %s.'), $version, $date)
  166. );
  167. } else {
  168. if ($version_local % 100 == 0) {
  169. self::messagesSet(
  170. 'notice',
  171. $message_id,
  172. __('Version check'),
  173. Sanitize::sanitizeMessage(sprintf(__('You are using Git version, run [kbd]git pull[/kbd]'
  174. . ' :-)[br]The latest stable version is %s, released on %s.'), $version, $date))
  175. );
  176. } else {
  177. self::messagesSet(
  178. 'notice',
  179. $message_id,
  180. __('Version check'),
  181. __('No newer stable version is available')
  182. );
  183. }
  184. }
  185. }
  186. }