Translated using Weblate (Slovenian)
[phpmyadmin.git] / user_password.php
blob1c647036593832b853c8b0732ebcd8ab022145ec
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_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_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_Response::getInstance();
89 if ($change_password_message['error']) {
90 $response->addJSON('message', $change_password_message['msg']);
91 $response->isSuccess(false);
92 } else {
93 $sql_query = PMA_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_Message::success(__('The profile has been updated.'));
114 if (($_REQUEST['nopass'] != '1')) {
115 if (empty($_REQUEST['pma_pw']) || empty($_REQUEST['pma_pw2'])) {
116 $message = PMA_Message::error(__('The password is empty!'));
117 $error = true;
118 } elseif ($_REQUEST['pma_pw'] != $_REQUEST['pma_pw2']) {
119 $message = PMA_Message::error(__('The passwords aren\'t the same!'));
120 $error = true;
123 return array('error' => $error, 'msg' => $message);
127 * Change the password
129 * @param string $password New password
130 * @param string $message Message
131 * @param array $change_password_message Message to show
133 * @return void
135 function PMA_changePassword($password, $message, $change_password_message)
137 global $auth_plugin;
139 $hashing_function = PMA_changePassHashingFunction();
141 $orig_auth_plugin = null;
143 $row = $GLOBALS['dbi']->fetchSingleRow('SELECT CURRENT_USER() as user');
144 $curr_user = $row['user'];
145 list($username, $hostname) = explode('@', $curr_user);
147 $serverType = PMA_Util::getServerType();
149 if (isset($_REQUEST['authentication_plugin'])
150 && ! empty($_REQUEST['authentication_plugin'])
152 $orig_auth_plugin = $_REQUEST['authentication_plugin'];
153 } else {
154 $orig_auth_plugin = PMA_getCurrentAuthenticationPlugin(
155 'change', $username, $hostname
159 if ($serverType == 'MySQL'
160 && PMA_MYSQL_INT_VERSION >= 50706
162 $sql_query = 'ALTER USER \'' . $username . '\'@\'' . $hostname
163 . '\' IDENTIFIED WITH ' . $orig_auth_plugin . ' BY '
164 . (($password == '') ? '\'\'' : '\'***\'');
165 } else if (($serverType == 'MySQL'
166 && PMA_MYSQL_INT_VERSION >= 50507)
167 || ($serverType == 'MariaDB'
168 && PMA_MYSQL_INT_VERSION >= 50200)
170 // For MySQL versions 5.5.7+ and MariaDB versions 5.2+,
171 // explicitly set value of `old_passwords` so that
172 // it does not give an error while using
173 // the PASSWORD() function
174 if ($orig_auth_plugin == 'sha256_password') {
175 $value = 2;
176 } else {
177 $value = 0;
179 $GLOBALS['dbi']->tryQuery('SET `old_passwords` = ' . $value . ';');
181 $sql_query = 'SET password = '
182 . (($password == '') ? '\'\'' : $hashing_function . '(\'***\')');
183 PMA_changePassUrlParamsAndSubmitQuery(
184 $username, $hostname, $password,
185 $sql_query, $hashing_function, $orig_auth_plugin
188 $auth_plugin->handlePasswordChange($password);
189 PMA_getChangePassMessage($change_password_message, $sql_query);
190 PMA_changePassDisplayPage($message, $sql_query);
194 * Generate the hashing function
196 * @return string $hashing_function
198 function PMA_changePassHashingFunction()
200 if (PMA_isValid(
201 $_REQUEST['authentication_plugin'], 'identical', 'mysql_old_password'
202 )) {
203 $hashing_function = 'OLD_PASSWORD';
204 } else {
205 $hashing_function = 'PASSWORD';
207 return $hashing_function;
211 * Generate the error url and submit the query
213 * @param string $username Username
214 * @param string $hostname Hostname
215 * @param string $password Password
216 * @param string $sql_query SQL query
217 * @param string $hashing_function Hashing function
218 * @param string $orig_auth_plugin Original Authentication Plugin
220 * @return void
222 function PMA_changePassUrlParamsAndSubmitQuery(
223 $username, $hostname, $password, $sql_query, $hashing_function, $orig_auth_plugin
225 $err_url = 'user_password.php' . PMA_URL_getCommon();
226 $serverType = PMA_Util::getServerType();
228 if ($serverType == 'MySQL' && PMA_MYSQL_INT_VERSION >= 50706) {
229 $local_query = 'ALTER USER \'' . $username . '\'@\'' . $hostname . '\''
230 . ' IDENTIFIED with ' . $orig_auth_plugin . ' BY '
231 . (($password == '')
232 ? '\'\''
233 : '\'' . PMA_Util::sqlAddSlashes($password) . '\'');
234 } else if ($serverType == 'MariaDB'
235 && PMA_MYSQL_INT_VERSION >= 50200
237 if ($orig_auth_plugin == 'mysql_native_password') {
238 // Set the hashing method used by PASSWORD()
239 // to be 'mysql_native_password' type
240 $GLOBALS['dbi']->tryQuery('SET old_passwords = 0;');
241 } else if ($orig_auth_plugin == 'sha256_password') {
242 // Set the hashing method used by PASSWORD()
243 // to be 'sha256_password' type
244 $GLOBALS['dbi']->tryQuery('SET `old_passwords` = 2;');
247 $hashedPassword = PMA_getHashedPassword($_POST['pma_pw']);
249 $local_query = "UPDATE `mysql`.`user` SET"
250 . " `authentication_string` = '" . $hashedPassword
251 . "', `Password` = '', "
252 . " `plugin` = '" . $orig_auth_plugin . "'"
253 . " WHERE `User` = '" . $username . "' AND Host = '"
254 . $hostname . "';";
256 $GLOBALS['dbi']->tryQuery("FLUSH PRIVILEGES;");
257 } else {
258 $local_query = 'SET password = ' . (($password == '')
259 ? '\'\''
260 : $hashing_function . '(\'' . PMA_Util::sqlAddSlashes($password)
261 . '\')');
263 if (! @$GLOBALS['dbi']->tryQuery($local_query)) {
264 PMA_Util::mysqlDie($GLOBALS['dbi']->getError(), $sql_query, false, $err_url);
269 * Display the page
271 * @param string $message Message
272 * @param string $sql_query SQL query
274 * @return void
276 function PMA_changePassDisplayPage($message, $sql_query)
278 echo '<h1>' . __('Change password') . '</h1>' . "\n\n";
279 echo PMA_Util::getMessage(
280 $message, $sql_query, 'success'
282 echo '<a href="index.php' . PMA_URL_getCommon()
283 . ' target="_parent">' . "\n"
284 . '<strong>' . __('Back') . '</strong></a>';
285 exit;