PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/user_password.php

https://gitlab.com/trungthao379/phpmyadmin
PHP | 294 lines | 181 code | 29 blank | 84 comment | 57 complexity | cd5b6f86a526aed4489f8c008cee8b8b MD5 | raw file
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * displays and handles the form where the user can change his password
  5. * linked from index.php
  6. *
  7. * @package PhpMyAdmin
  8. */
  9. use PMA\libraries\URL;
  10. /**
  11. * Gets some core libraries
  12. */
  13. require_once './libraries/common.inc.php';
  14. /**
  15. * Libraries needed for some functions
  16. */
  17. require_once './libraries/server_privileges.lib.php';
  18. $response = PMA\libraries\Response::getInstance();
  19. $header = $response->getHeader();
  20. $scripts = $header->getScripts();
  21. $scripts->addFile('server_privileges.js');
  22. /**
  23. * Displays an error message and exits if the user isn't allowed to use this
  24. * script
  25. */
  26. if (! $GLOBALS['cfg']['ShowChgPassword']) {
  27. $GLOBALS['cfg']['ShowChgPassword'] = $GLOBALS['dbi']->selectDb('mysql');
  28. }
  29. if ($cfg['Server']['auth_type'] == 'config' || ! $cfg['ShowChgPassword']) {
  30. PMA\libraries\Message::error(
  31. __('You don\'t have sufficient privileges to be here right now!')
  32. )->display();
  33. exit;
  34. } // end if
  35. /**
  36. * If the "change password" form has been submitted, checks for valid values
  37. * and submit the query or logout
  38. */
  39. if (isset($_REQUEST['nopass'])) {
  40. if ($_REQUEST['nopass'] == '1') {
  41. $password = '';
  42. } else {
  43. $password = $_REQUEST['pma_pw'];
  44. }
  45. $change_password_message = PMA_setChangePasswordMsg();
  46. $msg = $change_password_message['msg'];
  47. if (! $change_password_message['error']) {
  48. PMA_changePassword($password, $msg, $change_password_message);
  49. } else {
  50. PMA_getChangePassMessage($change_password_message);
  51. }
  52. }
  53. /**
  54. * If the "change password" form hasn't been submitted or the values submitted
  55. * aren't valid -> displays the form
  56. */
  57. // Displays an error message if required
  58. if (isset($msg)) {
  59. $msg->display();
  60. unset($msg);
  61. }
  62. require_once './libraries/display_change_password.lib.php';
  63. echo PMA_getHtmlForChangePassword('change_pw', $username, $hostname);
  64. exit;
  65. /**
  66. * Send the message as an ajax request
  67. *
  68. * @param array $change_password_message Message to display
  69. * @param string $sql_query SQL query executed
  70. *
  71. * @return void
  72. */
  73. function PMA_getChangePassMessage($change_password_message, $sql_query = '')
  74. {
  75. if ($GLOBALS['is_ajax_request'] == true) {
  76. /**
  77. * If in an Ajax request, we don't need to show the rest of the page
  78. */
  79. $response = PMA\libraries\Response::getInstance();
  80. if ($change_password_message['error']) {
  81. $response->addJSON('message', $change_password_message['msg']);
  82. $response->setRequestStatus(false);
  83. } else {
  84. $sql_query = PMA\libraries\Util::getMessage(
  85. $change_password_message['msg'],
  86. $sql_query,
  87. 'success'
  88. );
  89. $response->addJSON('message', $sql_query);
  90. }
  91. exit;
  92. }
  93. }
  94. /**
  95. * Generate the message
  96. *
  97. * @return array error value and message
  98. */
  99. function PMA_setChangePasswordMsg()
  100. {
  101. $error = false;
  102. $message = PMA\libraries\Message::success(__('The profile has been updated.'));
  103. if (($_REQUEST['nopass'] != '1')) {
  104. if (empty($_REQUEST['pma_pw']) || empty($_REQUEST['pma_pw2'])) {
  105. $message = PMA\libraries\Message::error(__('The password is empty!'));
  106. $error = true;
  107. } elseif ($_REQUEST['pma_pw'] != $_REQUEST['pma_pw2']) {
  108. $message = PMA\libraries\Message::error(
  109. __('The passwords aren\'t the same!')
  110. );
  111. $error = true;
  112. }
  113. }
  114. return array('error' => $error, 'msg' => $message);
  115. }
  116. /**
  117. * Change the password
  118. *
  119. * @param string $password New password
  120. * @param string $message Message
  121. * @param array $change_password_message Message to show
  122. *
  123. * @return void
  124. */
  125. function PMA_changePassword($password, $message, $change_password_message)
  126. {
  127. global $auth_plugin;
  128. $hashing_function = PMA_changePassHashingFunction();
  129. list($username, $hostname) = $GLOBALS['dbi']->getCurrentUserAndHost();
  130. $serverType = PMA\libraries\Util::getServerType();
  131. if (isset($_REQUEST['authentication_plugin'])
  132. && ! empty($_REQUEST['authentication_plugin'])
  133. ) {
  134. $orig_auth_plugin = $_REQUEST['authentication_plugin'];
  135. } else {
  136. $orig_auth_plugin = PMA_getCurrentAuthenticationPlugin(
  137. 'change', $username, $hostname
  138. );
  139. }
  140. $sql_query = 'SET password = '
  141. . (($password == '') ? '\'\'' : $hashing_function . '(\'***\')');
  142. if ($serverType == 'MySQL'
  143. && PMA_MYSQL_INT_VERSION >= 50706
  144. ) {
  145. $sql_query = 'ALTER USER \'' . $username . '\'@\'' . $hostname
  146. . '\' IDENTIFIED WITH ' . $orig_auth_plugin . ' BY '
  147. . (($password == '') ? '\'\'' : '\'***\'');
  148. } else if (($serverType == 'MySQL'
  149. && PMA_MYSQL_INT_VERSION >= 50507)
  150. || ($serverType == 'MariaDB'
  151. && PMA_MYSQL_INT_VERSION >= 50200)
  152. ) {
  153. // For MySQL versions 5.5.7+ and MariaDB versions 5.2+,
  154. // explicitly set value of `old_passwords` so that
  155. // it does not give an error while using
  156. // the PASSWORD() function
  157. if ($orig_auth_plugin == 'sha256_password') {
  158. $value = 2;
  159. } else {
  160. $value = 0;
  161. }
  162. $GLOBALS['dbi']->tryQuery('SET `old_passwords` = ' . $value . ';');
  163. }
  164. PMA_changePassUrlParamsAndSubmitQuery(
  165. $username, $hostname, $password,
  166. $sql_query, $hashing_function, $orig_auth_plugin
  167. );
  168. $auth_plugin->handlePasswordChange($password);
  169. PMA_getChangePassMessage($change_password_message, $sql_query);
  170. PMA_changePassDisplayPage($message, $sql_query);
  171. }
  172. /**
  173. * Generate the hashing function
  174. *
  175. * @return string $hashing_function
  176. */
  177. function PMA_changePassHashingFunction()
  178. {
  179. if (PMA_isValid(
  180. $_REQUEST['authentication_plugin'], 'identical', 'mysql_old_password'
  181. )) {
  182. $hashing_function = 'OLD_PASSWORD';
  183. } else {
  184. $hashing_function = 'PASSWORD';
  185. }
  186. return $hashing_function;
  187. }
  188. /**
  189. * Changes password for a user
  190. *
  191. * @param string $username Username
  192. * @param string $hostname Hostname
  193. * @param string $password Password
  194. * @param string $sql_query SQL query
  195. * @param string $hashing_function Hashing function
  196. * @param string $orig_auth_plugin Original Authentication Plugin
  197. *
  198. * @return void
  199. */
  200. function PMA_changePassUrlParamsAndSubmitQuery(
  201. $username, $hostname, $password, $sql_query, $hashing_function, $orig_auth_plugin
  202. ) {
  203. $err_url = 'user_password.php' . URL::getCommon();
  204. $serverType = PMA\libraries\Util::getServerType();
  205. if ($serverType == 'MySQL' && PMA_MYSQL_INT_VERSION >= 50706) {
  206. $local_query = 'ALTER USER \'' . $username . '\'@\'' . $hostname . '\''
  207. . ' IDENTIFIED with ' . $orig_auth_plugin . ' BY '
  208. . (($password == '')
  209. ? '\'\''
  210. : '\'' . PMA\libraries\Util::sqlAddSlashes($password) . '\'');
  211. } else if ($serverType == 'MariaDB'
  212. && PMA_MYSQL_INT_VERSION >= 50200
  213. && PMA_MYSQL_INT_VERSION < 100100
  214. ) {
  215. if ($orig_auth_plugin == 'mysql_native_password') {
  216. // Set the hashing method used by PASSWORD()
  217. // to be 'mysql_native_password' type
  218. $GLOBALS['dbi']->tryQuery('SET old_passwords = 0;');
  219. } else if ($orig_auth_plugin == 'sha256_password') {
  220. // Set the hashing method used by PASSWORD()
  221. // to be 'sha256_password' type
  222. $GLOBALS['dbi']->tryQuery('SET `old_passwords` = 2;');
  223. }
  224. $hashedPassword = PMA_getHashedPassword($_POST['pma_pw']);
  225. $local_query = "UPDATE `mysql`.`user` SET"
  226. . " `authentication_string` = '" . $hashedPassword
  227. . "', `Password` = '', "
  228. . " `plugin` = '" . $orig_auth_plugin . "'"
  229. . " WHERE `User` = '" . $username . "' AND Host = '"
  230. . $hostname . "';";
  231. $GLOBALS['dbi']->tryQuery("FLUSH PRIVILEGES;");
  232. } else {
  233. $local_query = 'SET password = ' . (($password == '')
  234. ? '\'\''
  235. : $hashing_function . '(\''
  236. . PMA\libraries\Util::sqlAddSlashes($password) . '\')');
  237. }
  238. if (! @$GLOBALS['dbi']->tryQuery($local_query)) {
  239. PMA\libraries\Util::mysqlDie(
  240. $GLOBALS['dbi']->getError(),
  241. $sql_query,
  242. false,
  243. $err_url
  244. );
  245. }
  246. }
  247. /**
  248. * Display the page
  249. *
  250. * @param string $message Message
  251. * @param string $sql_query SQL query
  252. *
  253. * @return void
  254. */
  255. function PMA_changePassDisplayPage($message, $sql_query)
  256. {
  257. echo '<h1>' , __('Change password') , '</h1>' , "\n\n";
  258. echo PMA\libraries\Util::getMessage(
  259. $message, $sql_query, 'success'
  260. );
  261. echo '<a href="index.php' , URL::getCommon()
  262. , ' target="_parent">' , "\n"
  263. , '<strong>' , __('Back') , '</strong></a>';
  264. exit;
  265. }