2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * displays and handles the form where the user can change his password
5 * linked from index.php
11 * Gets some core libraries
13 require_once './libraries/common.inc.php';
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');
26 * Displays an error message and exits if the user isn't allowed to use this
29 if (! $GLOBALS['cfg']['ShowChgPassword']) {
30 $GLOBALS['cfg']['ShowChgPassword'] = $GLOBALS['dbi']->selectDb('mysql');
32 if ($cfg['Server']['auth_type'] == 'config' ||
! $cfg['ShowChgPassword']) {
34 __('You don\'t have sufficient privileges to be here right now!')
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') {
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);
54 PMA_getChangePassMessage($change_password_message);
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
69 require_once './libraries/display_change_password.lib.php';
71 echo PMA_getHtmlForChangePassword($username, $hostname);
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
82 function PMA_getChangePassMessage($change_password_message, $sql_query = '')
84 if ($GLOBALS['is_ajax_request'] == true) {
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);
93 $sql_query = PMA_Util
::getMessage(
94 $change_password_message['msg'],
98 $response->addJSON('message', $sql_query);
105 * Generate the message
107 * @return array error value and message
109 function PMA_setChangePasswordMsg()
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!'));
118 } elseif ($_REQUEST['pma_pw'] != $_REQUEST['pma_pw2']) {
119 $message = PMA_Message
::error(__('The passwords aren\'t the same!'));
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
135 function PMA_changePassword($password, $message, $change_password_message)
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 ($serverType === 'MySQL'
150 && PMA_MYSQL_INT_VERSION
>= 50706
153 if (isset($_REQUEST['authentication_plugin'])
154 && ! empty($_REQUEST['authentication_plugin'])
156 $orig_auth_plugin = $_REQUEST['authentication_plugin'];
158 $orig_auth_plugin = PMA_getCurrentAuthenticationPlugin(
159 'change', $username, $hostname
163 $sql_query = 'ALTER USER \'' . $username . '\'@\'' . $hostname
164 . '\' IDENTIFIED WITH ' . $orig_auth_plugin . ' BY '
165 . (($password == '') ?
'\'\'' : '\'***\'');
167 // For MySQL versions 5.5.7+ and MariaDB versions 5.2+,
168 // explicitly set value of `old_passwords` so that
169 // it does not give an error while using
170 // the PASSWORD() function
171 if (($serverType == 'MySQL'
172 && PMA_MYSQL_INT_VERSION
>= 50507)
173 ||
($serverType == 'MariaDB'
174 && PMA_MYSQL_INT_VERSION
>= 50200)
176 $orig_auth_plugin = PMA_getCurrentAuthenticationPlugin(
177 'change', $username, $hostname
179 if ($orig_auth_plugin == 'sha256_password') {
184 $GLOBALS['dbi']->tryQuery('SET `old_passwords` = ' . $value . ';');
186 $sql_query = 'SET password = '
187 . (($password == '') ?
'\'\'' : $hashing_function . '(\'***\')');
189 PMA_changePassUrlParamsAndSubmitQuery(
190 $username, $hostname, $password,
191 $sql_query, $hashing_function, $orig_auth_plugin
194 $auth_plugin->handlePasswordChange($password);
195 PMA_getChangePassMessage($change_password_message, $sql_query);
196 PMA_changePassDisplayPage($message, $sql_query);
200 * Generate the hashing function
202 * @return string $hashing_function
204 function PMA_changePassHashingFunction()
206 if (PMA_isValid($_REQUEST['pw_hash'], 'identical', 'old')) {
207 $hashing_function = 'OLD_PASSWORD';
209 $hashing_function = 'PASSWORD';
211 return $hashing_function;
215 * Generate the error url and submit the query
217 * @param string $username Username
218 * @param string $hostname Hostname
219 * @param string $password Password
220 * @param string $sql_query SQL query
221 * @param string $hashing_function Hashing function
222 * @param string $auth_plugin Authentication Plugin
226 function PMA_changePassUrlParamsAndSubmitQuery(
227 $username, $hostname, $password, $sql_query, $hashing_function, $auth_plugin
229 $err_url = 'user_password.php' . PMA_URL_getCommon();
230 if (PMA_Util
::getServerType() === 'MySQL' && PMA_MYSQL_INT_VERSION
>= 50706) {
231 $local_query = 'ALTER USER \'' . $username . '\'@\'' . $hostname . '\''
232 . ' IDENTIFIED with ' . $auth_plugin . ' BY '
235 : '\'' . PMA_Util
::sqlAddSlashes($password) . '\'');
237 $local_query = 'SET password = ' . (($password == '')
239 : $hashing_function . '(\'' . PMA_Util
::sqlAddSlashes($password)
242 if (! @$GLOBALS['dbi']->tryQuery($local_query)) {
243 PMA_Util
::mysqlDie($GLOBALS['dbi']->getError(), $sql_query, false, $err_url);
250 * @param string $message Message
251 * @param string $sql_query SQL query
255 function PMA_changePassDisplayPage($message, $sql_query)
257 echo '<h1>' . __('Change password') . '</h1>' . "\n\n";
258 echo PMA_Util
::getMessage(
259 $message, $sql_query, 'success'
261 echo '<a href="index.php' . PMA_URL_getCommon()
262 . ' target="_parent">' . "\n"
263 . '<strong>' . __('Back') . '</strong></a>';