AuthenticationConfig.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * Config Authentication plugin for phpMyAdmin
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Plugins\Auth;
  7. use PhpMyAdmin\Html\Generator;
  8. use PhpMyAdmin\Plugins\AuthenticationPlugin;
  9. use PhpMyAdmin\Response;
  10. use PhpMyAdmin\Server\Select;
  11. use PhpMyAdmin\Util;
  12. use const E_USER_NOTICE;
  13. use const E_USER_WARNING;
  14. use function count;
  15. use function defined;
  16. use function sprintf;
  17. use function trigger_error;
  18. /**
  19. * Handles the config authentication method
  20. */
  21. class AuthenticationConfig extends AuthenticationPlugin
  22. {
  23. /**
  24. * Displays authentication form
  25. *
  26. * @return bool always true
  27. */
  28. public function showLoginForm()
  29. {
  30. $response = Response::getInstance();
  31. if ($response->isAjax()) {
  32. $response->setRequestStatus(false);
  33. // reload_flag removes the token parameter from the URL and reloads
  34. $response->addJSON('reload_flag', '1');
  35. if (defined('TESTSUITE')) {
  36. return true;
  37. }
  38. exit;
  39. }
  40. return true;
  41. }
  42. /**
  43. * Gets authentication credentials
  44. *
  45. * @return bool always true
  46. */
  47. public function readCredentials()
  48. {
  49. if ($GLOBALS['token_provided'] && $GLOBALS['token_mismatch']) {
  50. return false;
  51. }
  52. $this->user = $GLOBALS['cfg']['Server']['user'];
  53. $this->password = $GLOBALS['cfg']['Server']['password'];
  54. return true;
  55. }
  56. /**
  57. * User is not allowed to login to MySQL -> authentication failed
  58. *
  59. * @param string $failure String describing why authentication has failed
  60. *
  61. * @return void
  62. */
  63. public function showFailure($failure)
  64. {
  65. global $dbi;
  66. parent::showFailure($failure);
  67. $conn_error = $dbi->getError();
  68. if (! $conn_error) {
  69. $conn_error = __('Cannot connect: invalid settings.');
  70. }
  71. /* HTML header */
  72. $response = Response::getInstance();
  73. $response->getFooter()
  74. ->setMinimal();
  75. $header = $response->getHeader();
  76. $header->setBodyId('loginform');
  77. $header->setTitle(__('Access denied!'));
  78. $header->disableMenuAndConsole();
  79. echo '<br><br>
  80. <div class="text-center">
  81. <h1>';
  82. echo sprintf(__('Welcome to %s'), ' phpMyAdmin ');
  83. echo '</h1>
  84. </div>
  85. <br>
  86. <table cellpadding="0" cellspacing="3" class= "pma-table auth_config_tbl" width="80%">
  87. <tr>
  88. <td>';
  89. if (isset($GLOBALS['allowDeny_forbidden'])
  90. && $GLOBALS['allowDeny_forbidden']
  91. ) {
  92. trigger_error(__('Access denied!'), E_USER_NOTICE);
  93. } else {
  94. // Check whether user has configured something
  95. if ($GLOBALS['PMA_Config']->sourceMtime == 0) {
  96. echo '<p>' , sprintf(
  97. __(
  98. 'You probably did not create a configuration file.'
  99. . ' You might want to use the %1$ssetup script%2$s to'
  100. . ' create one.'
  101. ),
  102. '<a href="setup/">',
  103. '</a>'
  104. ) , '</p>' , "\n";
  105. } elseif (! isset($GLOBALS['errno'])
  106. || (isset($GLOBALS['errno']) && $GLOBALS['errno'] != 2002)
  107. && $GLOBALS['errno'] != 2003
  108. ) {
  109. // if we display the "Server not responding" error, do not confuse
  110. // users by telling them they have a settings problem
  111. // (note: it's true that they could have a badly typed host name,
  112. // but anyway the current message tells that the server
  113. // rejected the connection, which is not really what happened)
  114. // 2002 is the error given by mysqli
  115. // 2003 is the error given by mysql
  116. trigger_error(
  117. __(
  118. 'phpMyAdmin tried to connect to the MySQL server, and the'
  119. . ' server rejected the connection. You should check the'
  120. . ' host, username and password in your configuration and'
  121. . ' make sure that they correspond to the information given'
  122. . ' by the administrator of the MySQL server.'
  123. ),
  124. E_USER_WARNING
  125. );
  126. }
  127. echo Generator::mysqlDie(
  128. $conn_error,
  129. '',
  130. true,
  131. '',
  132. false
  133. );
  134. }
  135. $GLOBALS['error_handler']->dispUserErrors();
  136. echo '</td>
  137. </tr>
  138. <tr>
  139. <td>' , "\n";
  140. echo '<a href="'
  141. , Util::getScriptNameForOption(
  142. $GLOBALS['cfg']['DefaultTabServer'],
  143. 'server'
  144. )
  145. , '" class="btn button mt-1 disableAjax">'
  146. , __('Retry to connect')
  147. , '</a>' , "\n";
  148. echo '</td>
  149. </tr>' , "\n";
  150. if (count($GLOBALS['cfg']['Servers']) > 1) {
  151. // offer a chance to login to other servers if the current one failed
  152. echo '<tr>' , "\n";
  153. echo ' <td>' , "\n";
  154. echo Select::render(true, true);
  155. echo ' </td>' , "\n";
  156. echo '</tr>' , "\n";
  157. }
  158. echo '</table>' , "\n";
  159. if (! defined('TESTSUITE')) {
  160. exit;
  161. }
  162. }
  163. }