Translated using Weblate (Slovenian)
[phpmyadmin.git] / user_password.php
blob5c0f6299bdfae13d82e167d46089753dfe20e83e
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 /**
11 * Gets some core libraries
13 require_once './libraries/common.inc.php';
15 /**
16 * Libraries needed for some functions
18 require_once './libraries/server_privileges.lib.php';
20 $response = PMA\libraries\Response::getInstance();
21 $header = $response->getHeader();
22 $scripts = $header->getScripts();
23 $scripts->addFile('server_privileges.js');
25 /**
26 * Displays an error message and exits if the user isn't allowed to use this
27 * script
29 if (! $GLOBALS['cfg']['ShowChgPassword']) {
30 $GLOBALS['cfg']['ShowChgPassword'] = $GLOBALS['dbi']->selectDb('mysql');
32 if ($cfg['Server']['auth_type'] == 'config' || ! $cfg['ShowChgPassword']) {
33 PMA\libraries\Message::error(
34 __('You don\'t have sufficient privileges to be here right now!')
35 )->display();
36 exit;
37 } // end if
39 /**
40 * If the "change password" form has been submitted, checks for valid values
41 * and submit the query or logout
43 if (isset($_REQUEST['nopass'])) {
44 if ($_REQUEST['nopass'] == '1') {
45 $password = '';
46 } else {
47 $password = $_REQUEST['pma_pw'];
49 $change_password_message = PMA_setChangePasswordMsg();
50 $msg = $change_password_message['msg'];
51 if (! $change_password_message['error']) {
52 PMA_changePassword($password, $msg, $change_password_message);
53 } else {
54 PMA_getChangePassMessage($change_password_message);
58 /**
59 * If the "change password" form hasn't been submitted or the values submitted
60 * aren't valid -> displays the form
63 // Displays an error message if required
64 if (isset($msg)) {
65 $msg->display();
66 unset($msg);
69 require_once './libraries/display_change_password.lib.php';
71 echo PMA_getHtmlForChangePassword('change_pw', $username, $hostname);
72 exit;
74 /**
75 * Send the message as an ajax request
77 * @param array $change_password_message Message to display
78 * @param string $sql_query SQL query executed
80 * @return void
82 function PMA_getChangePassMessage($change_password_message, $sql_query = '')
84 if ($GLOBALS['is_ajax_request'] == true) {
85 /**
86 * If in an Ajax request, we don't need to show the rest of the page
88 $response = PMA\libraries\Response::getInstance();
89 if ($change_password_message['error']) {
90 $response->addJSON('message', $change_password_message['msg']);
91 $response->setRequestStatus(false);
92 } else {
93 $sql_query = PMA\libraries\Util::getMessage(
94 $change_password_message['msg'],
95 $sql_query,
96 'success'
98 $response->addJSON('message', $sql_query);
100 exit;
105 * Generate the message
107 * @return array error value and message
109 function PMA_setChangePasswordMsg()
111 $error = false;
112 $message = PMA\libraries\Message::success(__('The profile has been updated.'));
114 if (($_REQUEST['nopass'] != '1')) {
115 if (strlen($_REQUEST['pma_pw']) === 0 || strlen($_REQUEST['pma_pw2']) === 0) {
116 $message = PMA\libraries\Message::error(__('The password is empty!'));
117 $error = true;
118 } elseif ($_REQUEST['pma_pw'] !== $_REQUEST['pma_pw2']) {
119 $message = PMA\libraries\Message::error(
120 __('The passwords aren\'t the same!')
122 $error = true;
123 } elseif (strlen($_REQUEST['pma_pw']) > 256) {
124 $message = PMA_Message::error(__('Password is too long!'));
125 $error = true;
128 return array('error' => $error, 'msg' => $message);
132 * Change the password
134 * @param string $password New password
135 * @param string $message Message
136 * @param array $change_password_message Message to show
138 * @return void
140 function PMA_changePassword($password, $message, $change_password_message)
142 global $auth_plugin;
144 $hashing_function = PMA_changePassHashingFunction();
146 $row = $GLOBALS['dbi']->fetchSingleRow('SELECT CURRENT_USER() as user');
147 $curr_user = $row['user'];
148 list($username, $hostname) = explode('@', $curr_user);
150 $serverType = PMA\libraries\Util::getServerType();
152 if (isset($_REQUEST['authentication_plugin'])
153 && ! empty($_REQUEST['authentication_plugin'])
155 $orig_auth_plugin = $_REQUEST['authentication_plugin'];
156 } else {
157 $orig_auth_plugin = PMA_getCurrentAuthenticationPlugin(
158 'change', $username, $hostname
162 $sql_query = 'SET password = '
163 . (($password == '') ? '\'\'' : $hashing_function . '(\'***\')');
165 if ($serverType == 'MySQL'
166 && PMA_MYSQL_INT_VERSION >= 50706
168 $sql_query = 'ALTER USER \'' . $username . '\'@\'' . $hostname
169 . '\' IDENTIFIED WITH ' . $orig_auth_plugin . ' BY '
170 . (($password == '') ? '\'\'' : '\'***\'');
171 } else if (($serverType == 'MySQL'
172 && PMA_MYSQL_INT_VERSION >= 50507)
173 || ($serverType == 'MariaDB'
174 && PMA_MYSQL_INT_VERSION >= 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 (PMA_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' . PMA_URL_getCommon();
232 $serverType = PMA\libraries\Util::getServerType();
234 if ($serverType == 'MySQL' && PMA_MYSQL_INT_VERSION >= 50706) {
235 $local_query = 'ALTER USER \'' . $username . '\'@\'' . $hostname . '\''
236 . ' IDENTIFIED with ' . $orig_auth_plugin . ' BY '
237 . (($password == '')
238 ? '\'\''
239 : '\'' . PMA\libraries\Util::sqlAddSlashes($password) . '\'');
240 } else if ($serverType == 'MariaDB'
241 && PMA_MYSQL_INT_VERSION >= 50200
242 && PMA_MYSQL_INT_VERSION < 100100
243 && $orig_auth_plugin !== ''
245 if ($orig_auth_plugin == 'mysql_native_password') {
246 // Set the hashing method used by PASSWORD()
247 // to be 'mysql_native_password' type
248 $GLOBALS['dbi']->tryQuery('SET old_passwords = 0;');
249 } else if ($orig_auth_plugin == 'sha256_password') {
250 // Set the hashing method used by PASSWORD()
251 // to be 'sha256_password' type
252 $GLOBALS['dbi']->tryQuery('SET `old_passwords` = 2;');
255 $hashedPassword = PMA_getHashedPassword($_POST['pma_pw']);
257 $local_query = "UPDATE `mysql`.`user` SET"
258 . " `authentication_string` = '" . $hashedPassword
259 . "', `Password` = '', "
260 . " `plugin` = '" . $orig_auth_plugin . "'"
261 . " WHERE `User` = '" . $username . "' AND Host = '"
262 . $hostname . "';";
263 } else {
264 $local_query = 'SET password = ' . (($password == '')
265 ? '\'\''
266 : $hashing_function . '(\''
267 . PMA\libraries\Util::sqlAddSlashes($password) . '\')');
269 if (! @$GLOBALS['dbi']->tryQuery($local_query)) {
270 PMA\libraries\Util::mysqlDie(
271 $GLOBALS['dbi']->getError(),
272 $sql_query,
273 false,
274 $err_url
278 // Flush privileges after successful password change
279 $GLOBALS['dbi']->tryQuery("FLUSH PRIVILEGES;");
283 * Display the page
285 * @param string $message Message
286 * @param string $sql_query SQL query
288 * @return void
290 function PMA_changePassDisplayPage($message, $sql_query)
292 echo '<h1>' , __('Change password') , '</h1>' , "\n\n";
293 echo PMA\libraries\Util::getMessage(
294 $message, $sql_query, 'success'
296 echo '<a href="index.php' , PMA_URL_getCommon()
297 , ' target="_parent">' , "\n"
298 , '<strong>' , __('Back') , '</strong></a>';
299 exit;