Index.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Various checks and message functions used on index page.
  5. *
  6. * @package PhpMyAdmin-Setup
  7. */
  8. declare(strict_types=1);
  9. namespace PhpMyAdmin\Setup;
  10. use PhpMyAdmin\Sanitize;
  11. use PhpMyAdmin\VersionInformation;
  12. /**
  13. * PhpMyAdmin\Setup\Index class
  14. *
  15. * Various checks and message functions used on index page.
  16. *
  17. * @package PhpMyAdmin-Setup
  18. */
  19. class Index
  20. {
  21. /**
  22. * Initializes message list
  23. *
  24. * @return void
  25. */
  26. public static function messagesBegin()
  27. {
  28. if (! isset($_SESSION['messages']) || ! is_array($_SESSION['messages'])) {
  29. $_SESSION['messages'] = [
  30. 'error' => [],
  31. 'notice' => [],
  32. ];
  33. } else {
  34. // reset message states
  35. foreach ($_SESSION['messages'] as &$messages) {
  36. foreach ($messages as &$msg) {
  37. $msg['fresh'] = false;
  38. $msg['active'] = false;
  39. }
  40. }
  41. }
  42. }
  43. /**
  44. * Adds a new message to message list
  45. *
  46. * @param string $type one of: notice, error
  47. * @param string $msgId unique message identifier
  48. * @param string $title language string id (in $str array)
  49. * @param string $message message text
  50. *
  51. * @return void
  52. */
  53. public static function messagesSet($type, $msgId, $title, $message)
  54. {
  55. $fresh = ! isset($_SESSION['messages'][$type][$msgId]);
  56. $_SESSION['messages'][$type][$msgId] = [
  57. 'fresh' => $fresh,
  58. 'active' => true,
  59. 'title' => $title,
  60. 'message' => $message,
  61. ];
  62. }
  63. /**
  64. * Cleans up message list
  65. *
  66. * @return void
  67. */
  68. public static function messagesEnd()
  69. {
  70. foreach ($_SESSION['messages'] as &$messages) {
  71. $remove_ids = [];
  72. foreach ($messages as $id => &$msg) {
  73. if ($msg['active'] == false) {
  74. $remove_ids[] = $id;
  75. }
  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. $version = $latestCompatible['version'];
  132. $date = $latestCompatible['date'];
  133. } else {
  134. return;
  135. }
  136. $version_upstream = $versionInformation->versionToInt($version);
  137. if ($version_upstream === false) {
  138. self::messagesSet(
  139. 'error',
  140. $message_id,
  141. __('Version check'),
  142. __('Got invalid version string from server')
  143. );
  144. return;
  145. }
  146. $version_local = $versionInformation->versionToInt(
  147. $GLOBALS['PMA_Config']->get('PMA_VERSION')
  148. );
  149. if ($version_local === false) {
  150. self::messagesSet(
  151. 'error',
  152. $message_id,
  153. __('Version check'),
  154. __('Unparsable version string')
  155. );
  156. return;
  157. }
  158. if ($version_upstream > $version_local) {
  159. $version = htmlspecialchars($version);
  160. $date = htmlspecialchars($date);
  161. self::messagesSet(
  162. 'notice',
  163. $message_id,
  164. __('Version check'),
  165. sprintf(__('A newer version of phpMyAdmin is available and you should consider upgrading. 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] :-)[br]The latest stable version is %s, released on %s.'), $version, $date))
  174. );
  175. } else {
  176. self::messagesSet(
  177. 'notice',
  178. $message_id,
  179. __('Version check'),
  180. __('No newer stable version is available')
  181. );
  182. }
  183. }
  184. }
  185. }