Translated using Weblate (Belarusian)
[phpmyadmin.git] / user_password.php
blobbbdcc6af28b763971130e9132350e6213f9ce3e0
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 */
9 use PMA\libraries\URL;
10 use PMA\libraries\Response;
12 /**
13 * Gets some core libraries
15 require_once './libraries/common.inc.php';
17 /**
18 * Libraries needed for some functions
20 require_once './libraries/server_privileges.lib.php';
22 $response = Response::getInstance();
23 $header = $response->getHeader();
24 $scripts = $header->getScripts();
25 $scripts->addFile('server_privileges.js');
26 $scripts->addFile('zxcvbn.js');
28 /**
29 * Displays an error message and exits if the user isn't allowed to use this
30 * script
32 if (! $GLOBALS['cfg']['ShowChgPassword']) {
33 $GLOBALS['cfg']['ShowChgPassword'] = $GLOBALS['dbi']->selectDb('mysql');
35 if ($cfg['Server']['auth_type'] == 'config' || ! $cfg['ShowChgPassword']) {
36 PMA\libraries\Message::error(
37 __('You don\'t have sufficient privileges to be here right now!')
38 )->display();
39 exit;
40 } // end if
42 /**
43 * If the "change password" form has been submitted, checks for valid values
44 * and submit the query or logout
46 if (isset($_REQUEST['nopass'])) {
47 if ($_REQUEST['nopass'] == '1') {
48 $password = '';
49 } else {
50 $password = $_REQUEST['pma_pw'];
52 $change_password_message = PMA_setChangePasswordMsg();
53 $msg = $change_password_message['msg'];
54 if (! $change_password_message['error']) {
55 PMA_changePassword($password, $msg, $change_password_message);
56 } else {
57 PMA_getChangePassMessage($change_password_message);
61 /**
62 * If the "change password" form hasn't been submitted or the values submitted
63 * aren't valid -> displays the form
66 // Displays an error message if required
67 if (isset($msg)) {
68 $msg->display();
69 unset($msg);
72 require_once './libraries/display_change_password.lib.php';
74 echo PMA_getHtmlForChangePassword('change_pw', $username, $hostname);
75 exit;
77 /**
78 * Send the message as an ajax request
80 * @param array $change_password_message Message to display
81 * @param string $sql_query SQL query executed
83 * @return void
85 function PMA_getChangePassMessage($change_password_message, $sql_query = '')
87 $response = Response::getInstance();
88 if ($response->isAjax()) {
89 /**
90 * If in an Ajax request, we don't need to show the rest of the page
92 $response = Response::getInstance();
93 if ($change_password_message['error']) {
94 $response->addJSON('message', $change_password_message['msg']);
95 $response->setRequestStatus(false);
96 } else {
97 $sql_query = PMA\libraries\Util::getMessage(
98 $change_password_message['msg'],
99 $sql_query,
100 'success'
102 $response->addJSON('message', $sql_query);
104 exit;
109 * Generate the message
111 * @return array error value and message
113 function PMA_setChangePasswordMsg()
115 $error = false;
116 $message = PMA\libraries\Message::success(__('The profile has been updated.'));
118 if (($_REQUEST['nopass'] != '1')) {
119 if (strlen($_REQUEST['pma_pw']) === 0 || strlen($_REQUEST['pma_pw2']) === 0) {
120 $message = PMA\libraries\Message::error(__('The password is empty!'));
121 $error = true;
122 } elseif ($_REQUEST['pma_pw'] !== $_REQUEST['pma_pw2']) {
123 $message = PMA\libraries\Message::error(
124 __('The passwords aren\'t the same!')
126 $error = true;
127 } elseif (strlen($_REQUEST['pma_pw']) > 256) {
128 $message = PMA_Message::error(__('Password is too long!'));
129 $error = true;
132 return array('error' => $error, 'msg' => $message);
136 * Change the password
138 * @param string $password New password
139 * @param string $message Message
140 * @param array $change_password_message Message to show
142 * @return void
144 function PMA_changePassword($password, $message, $change_password_message)
146 global $auth_plugin;
148 $hashing_function = PMA_changePassHashingFunction();
150 list($username, $hostname) = $GLOBALS['dbi']->getCurrentUserAndHost();
152 $serverType = PMA\libraries\Util::getServerType();
154 if (isset($_REQUEST['authentication_plugin'])
155 && ! empty($_REQUEST['authentication_plugin'])
157 $orig_auth_plugin = $_REQUEST['authentication_plugin'];
158 } else {
159 $orig_auth_plugin = PMA_getCurrentAuthenticationPlugin(
160 'change', $username, $hostname
164 $sql_query = 'SET password = '
165 . (($password == '') ? '\'\'' : $hashing_function . '(\'***\')');
167 if ($serverType == 'MySQL'
168 && PMA_MYSQL_INT_VERSION >= 50706
170 $sql_query = 'ALTER USER \'' . $username . '\'@\'' . $hostname
171 . '\' IDENTIFIED WITH ' . $orig_auth_plugin . ' BY '
172 . (($password == '') ? '\'\'' : '\'***\'');
173 } else if (($serverType == 'MySQL'
174 && PMA_MYSQL_INT_VERSION >= 50507)
175 || ($serverType == 'MariaDB'
176 && PMA_MYSQL_INT_VERSION >= 50200)
178 // For MySQL versions 5.5.7+ and MariaDB versions 5.2+,
179 // explicitly set value of `old_passwords` so that
180 // it does not give an error while using
181 // the PASSWORD() function
182 if ($orig_auth_plugin == 'sha256_password') {
183 $value = 2;
184 } else {
185 $value = 0;
187 $GLOBALS['dbi']->tryQuery('SET `old_passwords` = ' . $value . ';');
190 PMA_changePassUrlParamsAndSubmitQuery(
191 $username, $hostname, $password,
192 $sql_query, $hashing_function, $orig_auth_plugin
195 $auth_plugin->handlePasswordChange($password);
196 PMA_getChangePassMessage($change_password_message, $sql_query);
197 PMA_changePassDisplayPage($message, $sql_query);
201 * Generate the hashing function
203 * @return string $hashing_function
205 function PMA_changePassHashingFunction()
207 if (PMA_isValid(
208 $_REQUEST['authentication_plugin'], 'identical', 'mysql_old_password'
209 )) {
210 $hashing_function = 'OLD_PASSWORD';
211 } else {
212 $hashing_function = 'PASSWORD';
214 return $hashing_function;
218 * Changes password for a user
220 * @param string $username Username
221 * @param string $hostname Hostname
222 * @param string $password Password
223 * @param string $sql_query SQL query
224 * @param string $hashing_function Hashing function
225 * @param string $orig_auth_plugin Original Authentication Plugin
227 * @return void
229 function PMA_changePassUrlParamsAndSubmitQuery(
230 $username, $hostname, $password, $sql_query, $hashing_function, $orig_auth_plugin
232 $err_url = 'user_password.php' . URL::getCommon();
234 $serverType = PMA\libraries\Util::getServerType();
236 if ($serverType == 'MySQL' && PMA_MYSQL_INT_VERSION >= 50706) {
237 $local_query = 'ALTER USER \'' . $username . '\'@\'' . $hostname . '\''
238 . ' IDENTIFIED with ' . $orig_auth_plugin . ' BY '
239 . (($password == '')
240 ? '\'\''
241 : '\'' . $GLOBALS['dbi']->escapeString($password) . '\'');
242 } else if ($serverType == 'MariaDB'
243 && PMA_MYSQL_INT_VERSION >= 50200
244 && PMA_MYSQL_INT_VERSION < 100100
245 && $orig_auth_plugin !== ''
247 if ($orig_auth_plugin == 'mysql_native_password') {
248 // Set the hashing method used by PASSWORD()
249 // to be 'mysql_native_password' type
250 $GLOBALS['dbi']->tryQuery('SET old_passwords = 0;');
251 } else if ($orig_auth_plugin == 'sha256_password') {
252 // Set the hashing method used by PASSWORD()
253 // to be 'sha256_password' type
254 $GLOBALS['dbi']->tryQuery('SET `old_passwords` = 2;');
257 $hashedPassword = PMA_getHashedPassword($_POST['pma_pw']);
259 $local_query = "UPDATE `mysql`.`user` SET"
260 . " `authentication_string` = '" . $hashedPassword
261 . "', `Password` = '', "
262 . " `plugin` = '" . $orig_auth_plugin . "'"
263 . " WHERE `User` = '" . $username . "' AND Host = '"
264 . $hostname . "';";
265 } else {
266 $local_query = 'SET password = ' . (($password == '')
267 ? '\'\''
268 : $hashing_function . '(\''
269 . $GLOBALS['dbi']->escapeString($password) . '\')');
271 if (! @$GLOBALS['dbi']->tryQuery($local_query)) {
272 PMA\libraries\Util::mysqlDie(
273 $GLOBALS['dbi']->getError(),
274 $sql_query,
275 false,
276 $err_url
280 // Flush privileges after successful password change
281 $GLOBALS['dbi']->tryQuery("FLUSH PRIVILEGES;");
285 * Display the page
287 * @param string $message Message
288 * @param string $sql_query SQL query
290 * @return void
292 function PMA_changePassDisplayPage($message, $sql_query)
294 echo '<h1>' , __('Change password') , '</h1>' , "\n\n";
295 echo PMA\libraries\Util::getMessage(
296 $message, $sql_query, 'success'
298 echo '<a href="index.php' , URL::getCommon()
299 , ' target="_parent">' , "\n"
300 , '<strong>' , __('Back') , '</strong></a>';
301 exit;