Merge branch 'QA_4_4' into QA_4_5
[phpmyadmin.git] / user_password.php
blob71e5b4e3ce65d646d2fe3310987ee484dbb37125
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($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 if (PMA_Util::getServerType() === 'MySQL' && PMA_MYSQL_INT_VERSION >= 50706) {
149 if (isset($_REQUEST['pw_hash']) && ! empty($_REQUEST['pw_hash'])) {
150 $orig_auth_plugin = $_REQUEST['pw_hash'];
151 } else {
152 $orig_auth_plugin = PMA_getCurrentAuthenticationPlugin(
153 'change', $username, $hostname
157 $sql_query = 'ALTER USER \'' . $username . '\'@\'' . $hostname
158 . '\' IDENTIFIED WITH ' . $orig_auth_plugin . ' BY '
159 . (($password == '') ? '\'\'' : '\'***\'');
160 } else {
161 // For MySQL versions 5.6.6+,
162 // explicitly set value of `old_passwords` so that
163 // it does not give an error while using
164 // the PASSWORD() function
165 if (PMA_MYSQL_INT_VERSION >= 50606) {
166 $orig_auth_plugin = PMA_getCurrentAuthenticationPlugin(
167 'change', $username, $hostname
169 if ($orig_auth_plugin == 'sha256_password') {
170 $value = 2;
171 } else {
172 $value = 0;
174 $GLOBALS['dbi']->tryQuery('SET `old_passwords` = ' . $value . ';');
176 $sql_query = 'SET password = '
177 . (($password == '') ? '\'\'' : $hashing_function . '(\'***\')');
179 PMA_changePassUrlParamsAndSubmitQuery(
180 $username, $hostname, $password,
181 $sql_query, $hashing_function, $orig_auth_plugin
184 $auth_plugin->handlePasswordChange($password);
185 PMA_getChangePassMessage($change_password_message, $sql_query);
186 PMA_changePassDisplayPage($message, $sql_query);
190 * Generate the hashing function
192 * @return string $hashing_function
194 function PMA_changePassHashingFunction()
196 if (PMA_isValid($_REQUEST['pw_hash'], 'identical', 'old')) {
197 $hashing_function = 'OLD_PASSWORD';
198 } else {
199 $hashing_function = 'PASSWORD';
201 return $hashing_function;
205 * Generate the error url and submit the query
207 * @param string $username Username
208 * @param string $hostname Hostname
209 * @param string $password Password
210 * @param string $sql_query SQL query
211 * @param string $hashing_function Hashing function
212 * @param string $auth_plugin Authentication Plugin
214 * @return void
216 function PMA_changePassUrlParamsAndSubmitQuery(
217 $username, $hostname, $password, $sql_query, $hashing_function, $auth_plugin
219 $err_url = 'user_password.php' . PMA_URL_getCommon();
220 if (PMA_Util::getServerType() === 'MySQL' && PMA_MYSQL_INT_VERSION >= 50706) {
221 $local_query = 'ALTER USER \'' . $username . '\'@\'' . $hostname . '\''
222 . ' IDENTIFIED with ' . $auth_plugin . ' BY '
223 . (($password == '')
224 ? '\'\''
225 : '\'' . PMA_Util::sqlAddSlashes($password) . '\'');
226 } else {
227 $local_query = 'SET password = ' . (($password == '')
228 ? '\'\''
229 : $hashing_function . '(\'' . PMA_Util::sqlAddSlashes($password)
230 . '\')');
232 if (! @$GLOBALS['dbi']->tryQuery($local_query)) {
233 PMA_Util::mysqlDie($GLOBALS['dbi']->getError(), $sql_query, false, $err_url);
238 * Display the page
240 * @param string $message Message
241 * @param string $sql_query SQL query
243 * @return void
245 function PMA_changePassDisplayPage($message, $sql_query)
247 echo '<h1>' . __('Change password') . '</h1>' . "\n\n";
248 echo PMA_Util::getMessage(
249 $message, $sql_query, 'success'
251 echo '<a href="index.php' . PMA_URL_getCommon()
252 . ' target="_parent">' . "\n"
253 . '<strong>' . __('Back') . '</strong></a>';
254 exit;