Index.php 5.7 KB

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