Translated using Weblate (Slovenian)
[phpmyadmin.git] / user_password.php
blobb6d9445459c77cf2f6ff59261a78848baaa2ec3d
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;
11 /**
12 * Gets some core libraries
14 require_once './libraries/common.inc.php';
16 /**
17 * Libraries needed for some functions
19 require_once './libraries/server_privileges.lib.php';
21 $response = PMA\libraries\Response::getInstance();
22 $header = $response->getHeader();
23 $scripts = $header->getScripts();
24 $scripts->addFile('server_privileges.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 PMA\libraries\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 if ($GLOBALS['is_ajax_request'] == true) {
86 /**
87 * If in an Ajax request, we don't need to show the rest of the page
89 $response = PMA\libraries\Response::getInstance();
90 if ($change_password_message['error']) {
91 $response->addJSON('message', $change_password_message['msg']);
92 $response->setRequestStatus(false);
93 } else {
94 $sql_query = PMA\libraries\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 = PMA\libraries\Message::success(__('The profile has been updated.'));
115 if (($_REQUEST['nopass'] != '1')) {
116 if (empty($_REQUEST['pma_pw']) || empty($_REQUEST['pma_pw2'])) {
117 $message = PMA\libraries\Message::error(__('The password is empty!'));
118 $error = true;
119 } elseif ($_REQUEST['pma_pw'] != $_REQUEST['pma_pw2']) {
120 $message = PMA\libraries\Message::error(
121 __('The passwords aren\'t the same!')
123 $error = true;
126 return array('error' => $error, 'msg' => $message);
130 * Change the password
132 * @param string $password New password
133 * @param string $message Message
134 * @param array $change_password_message Message to show
136 * @return void
138 function PMA_changePassword($password, $message, $change_password_message)
140 global $auth_plugin;
142 $hashing_function = PMA_changePassHashingFunction();
144 $row = $GLOBALS['dbi']->fetchSingleRow('SELECT CURRENT_USER() as user');
145 $curr_user = $row['user'];
146 list($username, $hostname) = explode('@', $curr_user);
148 $serverType = PMA\libraries\Util::getServerType();
150 if (isset($_REQUEST['authentication_plugin'])
151 && ! empty($_REQUEST['authentication_plugin'])
153 $orig_auth_plugin = $_REQUEST['authentication_plugin'];
154 } else {
155 $orig_auth_plugin = PMA_getCurrentAuthenticationPlugin(
156 'change', $username, $hostname
160 $sql_query = 'SET password = '
161 . (($password == '') ? '\'\'' : $hashing_function . '(\'***\')');
163 if ($serverType == 'MySQL'
164 && PMA_MYSQL_INT_VERSION >= 50706
166 $sql_query = 'ALTER USER \'' . $username . '\'@\'' . $hostname
167 . '\' IDENTIFIED WITH ' . $orig_auth_plugin . ' BY '
168 . (($password == '') ? '\'\'' : '\'***\'');
169 } else if (($serverType == 'MySQL'
170 && PMA_MYSQL_INT_VERSION >= 50507)
171 || ($serverType == 'MariaDB'
172 && PMA_MYSQL_INT_VERSION >= 50200)
174 // For MySQL versions 5.5.7+ and MariaDB versions 5.2+,
175 // explicitly set value of `old_passwords` so that
176 // it does not give an error while using
177 // the PASSWORD() function
178 if ($orig_auth_plugin == 'sha256_password') {
179 $value = 2;
180 } else {
181 $value = 0;
183 $GLOBALS['dbi']->tryQuery('SET `old_passwords` = ' . $value . ';');
186 PMA_changePassUrlParamsAndSubmitQuery(
187 $username, $hostname, $password,
188 $sql_query, $hashing_function, $orig_auth_plugin
191 $auth_plugin->handlePasswordChange($password);
192 PMA_getChangePassMessage($change_password_message, $sql_query);
193 PMA_changePassDisplayPage($message, $sql_query);
197 * Generate the hashing function
199 * @return string $hashing_function
201 function PMA_changePassHashingFunction()
203 if (PMA_isValid(
204 $_REQUEST['authentication_plugin'], 'identical', 'mysql_old_password'
205 )) {
206 $hashing_function = 'OLD_PASSWORD';
207 } else {
208 $hashing_function = 'PASSWORD';
210 return $hashing_function;
214 * Changes password for a user
216 * @param string $username Username
217 * @param string $hostname Hostname
218 * @param string $password Password
219 * @param string $sql_query SQL query
220 * @param string $hashing_function Hashing function
221 * @param string $orig_auth_plugin Original Authentication Plugin
223 * @return void
225 function PMA_changePassUrlParamsAndSubmitQuery(
226 $username, $hostname, $password, $sql_query, $hashing_function, $orig_auth_plugin
228 $err_url = 'user_password.php' . URL::getCommon();
230 $serverType = PMA\libraries\Util::getServerType();
232 if ($serverType == 'MySQL' && PMA_MYSQL_INT_VERSION >= 50706) {
233 $local_query = 'ALTER USER \'' . $username . '\'@\'' . $hostname . '\''
234 . ' IDENTIFIED with ' . $orig_auth_plugin . ' BY '
235 . (($password == '')
236 ? '\'\''
237 : '\'' . PMA\libraries\Util::sqlAddSlashes($password) . '\'');
238 } else if ($serverType == 'MariaDB'
239 && PMA_MYSQL_INT_VERSION >= 50200
240 && PMA_MYSQL_INT_VERSION < 100100
242 if ($orig_auth_plugin == 'mysql_native_password') {
243 // Set the hashing method used by PASSWORD()
244 // to be 'mysql_native_password' type
245 $GLOBALS['dbi']->tryQuery('SET old_passwords = 0;');
246 } else if ($orig_auth_plugin == 'sha256_password') {
247 // Set the hashing method used by PASSWORD()
248 // to be 'sha256_password' type
249 $GLOBALS['dbi']->tryQuery('SET `old_passwords` = 2;');
252 $hashedPassword = PMA_getHashedPassword($_POST['pma_pw']);
254 $local_query = "UPDATE `mysql`.`user` SET"
255 . " `authentication_string` = '" . $hashedPassword
256 . "', `Password` = '', "
257 . " `plugin` = '" . $orig_auth_plugin . "'"
258 . " WHERE `User` = '" . $username . "' AND Host = '"
259 . $hostname . "';";
261 $GLOBALS['dbi']->tryQuery("FLUSH PRIVILEGES;");
262 } else {
263 $local_query = 'SET password = ' . (($password == '')
264 ? '\'\''
265 : $hashing_function . '(\''
266 . PMA\libraries\Util::sqlAddSlashes($password) . '\')');
268 if (! @$GLOBALS['dbi']->tryQuery($local_query)) {
269 PMA\libraries\Util::mysqlDie(
270 $GLOBALS['dbi']->getError(),
271 $sql_query,
272 false,
273 $err_url
279 * Display the page
281 * @param string $message Message
282 * @param string $sql_query SQL query
284 * @return void
286 function PMA_changePassDisplayPage($message, $sql_query)
288 echo '<h1>' , __('Change password') , '</h1>' , "\n\n";
289 echo PMA\libraries\Util::getMessage(
290 $message, $sql_query, 'success'
292 echo '<a href="index.php' , URL::getCommon()
293 , ' target="_parent">' , "\n"
294 , '<strong>' , __('Back') , '</strong></a>';
295 exit;