UserPasswordController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Controllers;
  4. use PhpMyAdmin\DatabaseInterface;
  5. use PhpMyAdmin\Html\Generator;
  6. use PhpMyAdmin\Message;
  7. use PhpMyAdmin\Response;
  8. use PhpMyAdmin\Template;
  9. use PhpMyAdmin\UserPassword;
  10. /**
  11. * Displays and handles the form where the user can change their password.
  12. */
  13. class UserPasswordController extends AbstractController
  14. {
  15. /** @var UserPassword */
  16. private $userPassword;
  17. /** @var DatabaseInterface */
  18. private $dbi;
  19. /**
  20. * @param Response $response
  21. * @param DatabaseInterface $dbi
  22. */
  23. public function __construct($response, Template $template, UserPassword $userPassword, $dbi)
  24. {
  25. parent::__construct($response, $template);
  26. $this->userPassword = $userPassword;
  27. $this->dbi = $dbi;
  28. }
  29. public function index(): void
  30. {
  31. global $cfg, $hostname, $username, $password, $change_password_message, $msg;
  32. $this->addScriptFiles(['server/privileges.js', 'vendor/zxcvbn.js']);
  33. /**
  34. * Displays an error message and exits if the user isn't allowed to use this
  35. * script
  36. */
  37. if (! $cfg['ShowChgPassword']) {
  38. $cfg['ShowChgPassword'] = $this->dbi->selectDb('mysql');
  39. }
  40. if ($cfg['Server']['auth_type'] === 'config' || ! $cfg['ShowChgPassword']) {
  41. $this->response->addHTML(Message::error(
  42. __('You don\'t have sufficient privileges to be here right now!')
  43. )->getDisplay());
  44. return;
  45. }
  46. /**
  47. * If the "change password" form has been submitted, checks for valid values
  48. * and submit the query or logout
  49. */
  50. if (isset($_POST['nopass'])) {
  51. if ($_POST['nopass'] == '1') {
  52. $password = '';
  53. } else {
  54. $password = $_POST['pma_pw'];
  55. }
  56. $change_password_message = $this->userPassword->setChangePasswordMsg();
  57. $msg = $change_password_message['msg'];
  58. if (! $change_password_message['error']) {
  59. $sql_query = $this->userPassword->changePassword($password);
  60. if ($this->response->isAjax()) {
  61. $sql_query = Generator::getMessage($change_password_message['msg'], $sql_query, 'success');
  62. $this->response->addJSON('message', $sql_query);
  63. return;
  64. }
  65. $this->response->addHTML('<h1>' . __('Change password') . '</h1>' . "\n\n");
  66. $this->response->addHTML(Generator::getMessage($msg, $sql_query, 'success'));
  67. $this->render('user_password');
  68. return;
  69. }
  70. if ($this->response->isAjax()) {
  71. $this->response->addJSON('message', $change_password_message['msg']);
  72. $this->response->setRequestStatus(false);
  73. return;
  74. }
  75. }
  76. /**
  77. * If the "change password" form hasn't been submitted or the values submitted
  78. * aren't valid -> displays the form
  79. */
  80. // Displays an error message if required
  81. if (isset($msg)) {
  82. $this->response->addHTML($msg->getDisplay());
  83. }
  84. $this->response->addHTML($this->userPassword->getFormForChangePassword($username, $hostname));
  85. }
  86. }