Translated using Weblate (Indonesian)
[phpmyadmin.git] / user_password.php
blob1654d33c9a6a3e7345e32813f745b3aadcd83b35
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
7 * @package PhpMyAdmin
8 */
10 use PhpMyAdmin\Core;
11 use PhpMyAdmin\Url;
12 use PhpMyAdmin\Response;
13 use PhpMyAdmin\Server\Privileges;
15 /**
16 * Gets some core libraries
18 require_once './libraries/common.inc.php';
20 $response = Response::getInstance();
21 $header = $response->getHeader();
22 $scripts = $header->getScripts();
23 $scripts->addFile('server_privileges.js');
24 $scripts->addFile('vendor/zxcvbn.js');
26 /**
27 * Displays an error message and exits if the user isn't allowed to use this
28 * script
30 if (! $GLOBALS['cfg']['ShowChgPassword']) {
31 $GLOBALS['cfg']['ShowChgPassword'] = $GLOBALS['dbi']->selectDb('mysql');
33 if ($cfg['Server']['auth_type'] == 'config' || ! $cfg['ShowChgPassword']) {
34 PhpMyAdmin\Message::error(
35 __('You don\'t have sufficient privileges to be here right now!')
36 )->display();
37 exit;
38 } // end if
40 /**
41 * If the "change password" form has been submitted, checks for valid values
42 * and submit the query or logout
44 if (isset($_REQUEST['nopass'])) {
45 if ($_REQUEST['nopass'] == '1') {
46 $password = '';
47 } else {
48 $password = $_REQUEST['pma_pw'];
50 $change_password_message = PMA_setChangePasswordMsg();
51 $msg = $change_password_message['msg'];
52 if (! $change_password_message['error']) {
53 PMA_changePassword($password, $msg, $change_password_message);
54 } else {
55 PMA_getChangePassMessage($change_password_message);
59 /**
60 * If the "change password" form hasn't been submitted or the values submitted
61 * aren't valid -> displays the form
64 // Displays an error message if required
65 if (isset($msg)) {
66 $msg->display();
67 unset($msg);
70 require_once './libraries/display_change_password.lib.php';
72 echo PMA_getHtmlForChangePassword('change_pw', $username, $hostname);
73 exit;
75 /**
76 * Send the message as an ajax request
78 * @param array $change_password_message Message to display
79 * @param string $sql_query SQL query executed
81 * @return void
83 function PMA_getChangePassMessage($change_password_message, $sql_query = '')
85 $response = Response::getInstance();
86 if ($response->isAjax()) {
87 /**
88 * If in an Ajax request, we don't need to show the rest of the page
90 if ($change_password_message['error']) {
91 $response->addJSON('message', $change_password_message['msg']);
92 $response->setRequestStatus(false);
93 } else {
94 $sql_query = PhpMyAdmin\Util::getMessage(
95 $change_password_message['msg'],
96 $sql_query,
97 'success'
99 $response->addJSON('message', $sql_query);
101 exit;
106 * Generate the message
108 * @return array error value and message
110 function PMA_setChangePasswordMsg()
112 $error = false;
113 $message = PhpMyAdmin\Message::success(__('The profile has been updated.'));
115 if (($_REQUEST['nopass'] != '1')) {
116 if (strlen($_REQUEST['pma_pw']) === 0 || strlen($_REQUEST['pma_pw2']) === 0) {
117 $message = PhpMyAdmin\Message::error(__('The password is empty!'));
118 $error = true;
119 } elseif ($_REQUEST['pma_pw'] !== $_REQUEST['pma_pw2']) {
120 $message = PhpMyAdmin\Message::error(
121 __('The passwords aren\'t the same!')
123 $error = true;
124 } elseif (strlen($_REQUEST['pma_pw']) > 256) {
125 $message = PMA_Message::error(__('Password is too long!'));
126 $error = true;
129 return array('error' => $error, 'msg' => $message);
133 * Change the password
135 * @param string $password New password
136 * @param string $message Message
137 * @param array $change_password_message Message to show
139 * @return void
141 function PMA_changePassword($password, $message, $change_password_message)
143 global $auth_plugin;
145 $hashing_function = PMA_changePassHashingFunction();
147 list($username, $hostname) = $GLOBALS['dbi']->getCurrentUserAndHost();
149 $serverType = PhpMyAdmin\Util::getServerType();
150 $serverVersion = $GLOBALS['dbi']->getVersion();
152 if (isset($_REQUEST['authentication_plugin'])
153 && ! empty($_REQUEST['authentication_plugin'])
155 $orig_auth_plugin = $_REQUEST['authentication_plugin'];
156 } else {
157 $orig_auth_plugin = Privileges::getCurrentAuthenticationPlugin(
158 'change', $username, $hostname
162 $sql_query = 'SET password = '
163 . (($password == '') ? '\'\'' : $hashing_function . '(\'***\')');
165 if ($serverType == 'MySQL'
166 && $serverVersion >= 50706
168 $sql_query = 'ALTER USER \'' . $username . '\'@\'' . $hostname
169 . '\' IDENTIFIED WITH ' . $orig_auth_plugin . ' BY '
170 . (($password == '') ? '\'\'' : '\'***\'');
171 } else if (($serverType == 'MySQL'
172 && $serverVersion >= 50507)
173 || ($serverType == 'MariaDB'
174 && $serverVersion >= 50200)
176 // For MySQL versions 5.5.7+ and MariaDB versions 5.2+,
177 // explicitly set value of `old_passwords` so that
178 // it does not give an error while using
179 // the PASSWORD() function
180 if ($orig_auth_plugin == 'sha256_password') {
181 $value = 2;
182 } else {
183 $value = 0;
185 $GLOBALS['dbi']->tryQuery('SET `old_passwords` = ' . $value . ';');
188 PMA_changePassUrlParamsAndSubmitQuery(
189 $username, $hostname, $password,
190 $sql_query, $hashing_function, $orig_auth_plugin
193 $auth_plugin->handlePasswordChange($password);
194 PMA_getChangePassMessage($change_password_message, $sql_query);
195 PMA_changePassDisplayPage($message, $sql_query);
199 * Generate the hashing function
201 * @return string $hashing_function
203 function PMA_changePassHashingFunction()
205 if (Core::isValid(
206 $_REQUEST['authentication_plugin'], 'identical', 'mysql_old_password'
207 )) {
208 $hashing_function = 'OLD_PASSWORD';
209 } else {
210 $hashing_function = 'PASSWORD';
212 return $hashing_function;
216 * Changes password for a user
218 * @param string $username Username
219 * @param string $hostname Hostname
220 * @param string $password Password
221 * @param string $sql_query SQL query
222 * @param string $hashing_function Hashing function
223 * @param string $orig_auth_plugin Original Authentication Plugin
225 * @return void
227 function PMA_changePassUrlParamsAndSubmitQuery(
228 $username, $hostname, $password, $sql_query, $hashing_function, $orig_auth_plugin
230 $err_url = 'user_password.php' . Url::getCommon();
232 $serverType = PhpMyAdmin\Util::getServerType();
233 $serverVersion = $GLOBALS['dbi']->getVersion();
235 if ($serverType == 'MySQL' && $serverVersion >= 50706) {
236 $local_query = 'ALTER USER \'' . $username . '\'@\'' . $hostname . '\''
237 . ' IDENTIFIED with ' . $orig_auth_plugin . ' BY '
238 . (($password == '')
239 ? '\'\''
240 : '\'' . $GLOBALS['dbi']->escapeString($password) . '\'');
241 } else if ($serverType == 'MariaDB'
242 && $serverVersion >= 50200
243 && $serverVersion < 100100
244 && $orig_auth_plugin !== ''
246 if ($orig_auth_plugin == 'mysql_native_password') {
247 // Set the hashing method used by PASSWORD()
248 // to be 'mysql_native_password' type
249 $GLOBALS['dbi']->tryQuery('SET old_passwords = 0;');
250 } else if ($orig_auth_plugin == 'sha256_password') {
251 // Set the hashing method used by PASSWORD()
252 // to be 'sha256_password' type
253 $GLOBALS['dbi']->tryQuery('SET `old_passwords` = 2;');
256 $hashedPassword = Privileges::getHashedPassword($_POST['pma_pw']);
258 $local_query = "UPDATE `mysql`.`user` SET"
259 . " `authentication_string` = '" . $hashedPassword
260 . "', `Password` = '', "
261 . " `plugin` = '" . $orig_auth_plugin . "'"
262 . " WHERE `User` = '" . $username . "' AND Host = '"
263 . $hostname . "';";
264 } else {
265 $local_query = 'SET password = ' . (($password == '')
266 ? '\'\''
267 : $hashing_function . '(\''
268 . $GLOBALS['dbi']->escapeString($password) . '\')');
270 if (! @$GLOBALS['dbi']->tryQuery($local_query)) {
271 PhpMyAdmin\Util::mysqlDie(
272 $GLOBALS['dbi']->getError(),
273 $sql_query,
274 false,
275 $err_url
279 // Flush privileges after successful password change
280 $GLOBALS['dbi']->tryQuery("FLUSH PRIVILEGES;");
284 * Display the page
286 * @param string $message Message
287 * @param string $sql_query SQL query
289 * @return void
291 function PMA_changePassDisplayPage($message, $sql_query)
293 echo '<h1>' , __('Change password') , '</h1>' , "\n\n";
294 echo PhpMyAdmin\Util::getMessage(
295 $message, $sql_query, 'success'
297 echo '<a href="index.php' , Url::getCommon()
298 , ' target="_parent">' , "\n"
299 , '<strong>' , __('Back') , '</strong></a>';
300 exit;