index.lib.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. if (!defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. * Initializes message list
  13. *
  14. * @return void
  15. */
  16. function PMA_messagesBegin()
  17. {
  18. if (! isset($_SESSION['messages']) || !is_array($_SESSION['messages'])) {
  19. $_SESSION['messages'] = array('error' => array(), 'notice' => array());
  20. } else {
  21. // reset message states
  22. foreach ($_SESSION['messages'] as &$messages) {
  23. foreach ($messages as &$msg) {
  24. $msg['fresh'] = false;
  25. $msg['active'] = false;
  26. }
  27. }
  28. }
  29. }
  30. /**
  31. * Adds a new message to message list
  32. *
  33. * @param string $type one of: notice, error
  34. * @param string $msgId unique message identifier
  35. * @param string $title language string id (in $str array)
  36. * @param string $message message text
  37. *
  38. * @return void
  39. */
  40. function PMA_messagesSet($type, $msgId, $title, $message)
  41. {
  42. $fresh = ! isset($_SESSION['messages'][$type][$msgId]);
  43. $_SESSION['messages'][$type][$msgId] = array(
  44. 'fresh' => $fresh,
  45. 'active' => true,
  46. 'title' => $title,
  47. 'message' => $message);
  48. }
  49. /**
  50. * Cleans up message list
  51. *
  52. * @return void
  53. */
  54. function PMA_messagesEnd()
  55. {
  56. foreach ($_SESSION['messages'] as &$messages) {
  57. $remove_ids = array();
  58. foreach ($messages as $id => &$msg) {
  59. if ($msg['active'] == false) {
  60. $remove_ids[] = $id;
  61. }
  62. }
  63. foreach ($remove_ids as $id) {
  64. unset($messages[$id]);
  65. }
  66. }
  67. }
  68. /**
  69. * Prints message list, must be called after PMA_messagesEnd()
  70. *
  71. * @return void
  72. */
  73. function PMA_messagesShowHtml()
  74. {
  75. $old_ids = array();
  76. foreach ($_SESSION['messages'] as $type => $messages) {
  77. foreach ($messages as $id => $msg) {
  78. echo '<div class="' . $type . '" id="' . $id . '">'
  79. . '<h4>' . $msg['title'] . '</h4>'
  80. . $msg['message'] . '</div>';
  81. if (!$msg['fresh'] && $type != 'error') {
  82. $old_ids[] = $id;
  83. }
  84. }
  85. }
  86. echo "\n" . '<script type="text/javascript">';
  87. foreach ($old_ids as $id) {
  88. echo "\nhiddenMessages.push('$id');";
  89. }
  90. echo "\n</script>\n";
  91. }
  92. /**
  93. * Checks for newest phpMyAdmin version and sets result as a new notice
  94. *
  95. * @return void
  96. */
  97. function PMA_versionCheck()
  98. {
  99. // version check messages should always be visible so let's make
  100. // a unique message id each time we run it
  101. $message_id = uniqid('version_check');
  102. // Fetch data
  103. $version_data = PMA_Util::getLatestVersion();
  104. if (empty($version_data)) {
  105. PMA_messagesSet(
  106. 'error',
  107. $message_id,
  108. __('Version check'),
  109. __('Reading of version failed. Maybe you\'re offline or the upgrade server does not respond.')
  110. );
  111. return;
  112. }
  113. $version = $version_data->version;
  114. $date = $version_data->date;
  115. $version_upstream = PMA_Util::versionToInt($version);
  116. if ($version_upstream === false) {
  117. PMA_messagesSet(
  118. 'error',
  119. $message_id,
  120. __('Version check'),
  121. __('Got invalid version string from server')
  122. );
  123. return;
  124. }
  125. $version_local = PMA_Util::versionToInt(
  126. $GLOBALS['PMA_Config']->get('PMA_VERSION')
  127. );
  128. if ($version_local === false) {
  129. PMA_messagesSet(
  130. 'error',
  131. $message_id,
  132. __('Version check'),
  133. __('Unparsable version string')
  134. );
  135. return;
  136. }
  137. if ($version_upstream > $version_local) {
  138. $version = htmlspecialchars($version);
  139. $date = htmlspecialchars($date);
  140. PMA_messagesSet(
  141. 'notice',
  142. $message_id,
  143. __('Version check'),
  144. sprintf(__('A newer version of phpMyAdmin is available and you should consider upgrading. The newest version is %s, released on %s.'), $version, $date)
  145. );
  146. } else {
  147. if ($version_local % 100 == 0) {
  148. PMA_messagesSet(
  149. 'notice',
  150. $message_id,
  151. __('Version check'),
  152. PMA_sanitize(sprintf(__('You are using Git version, run [kbd]git pull[/kbd] :-)[br]The latest stable version is %s, released on %s.'), $version, $date))
  153. );
  154. } else {
  155. PMA_messagesSet(
  156. 'notice',
  157. $message_id,
  158. __('Version check'),
  159. __('No newer stable version is available')
  160. );
  161. }
  162. }
  163. }
  164. /**
  165. * Checks whether config file is readable/writable
  166. *
  167. * @param bool &$is_readable whether the file is readable
  168. * @param bool &$is_writable whether the file is writable
  169. * @param bool &$file_exists whether the file exists
  170. *
  171. * @return void
  172. */
  173. function PMA_checkConfigRw(&$is_readable, &$is_writable, &$file_exists)
  174. {
  175. $file_path = $GLOBALS['ConfigFile']->getFilePath();
  176. $file_dir = dirname($file_path);
  177. $is_readable = true;
  178. $is_writable = is_dir($file_dir);
  179. if (SETUP_DIR_WRITABLE) {
  180. $is_writable = $is_writable && is_writable($file_dir);
  181. }
  182. $file_exists = file_exists($file_path);
  183. if ($file_exists) {
  184. $is_readable = is_readable($file_path);
  185. $is_writable = $is_writable && is_writable($file_path);
  186. }
  187. }