another minor fix to prior commit
[openemr.git] / phpmyadmin / user_password.php
blob93149947bb2dc49c0d231720a8a51b81bb89aba0
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 $sql_query = 'SET password = '
160 . (($password == '') ? '\'\'' : $hashing_function . '(\'***\')');
162 if ($serverType == 'MySQL'
163 && PMA_MYSQL_INT_VERSION >= 50706
165 $sql_query = 'ALTER USER \'' . $username . '\'@\'' . $hostname
166 . '\' IDENTIFIED WITH ' . $orig_auth_plugin . ' BY '
167 . (($password == '') ? '\'\'' : '\'***\'');
168 } else if (($serverType == 'MySQL'
169 && PMA_MYSQL_INT_VERSION >= 50507)
170 || ($serverType == 'MariaDB'
171 && PMA_MYSQL_INT_VERSION >= 50200)
173 // For MySQL versions 5.5.7+ and MariaDB versions 5.2+,
174 // explicitly set value of `old_passwords` so that
175 // it does not give an error while using
176 // the PASSWORD() function
177 if ($orig_auth_plugin == 'sha256_password') {
178 $value = 2;
179 } else {
180 $value = 0;
182 $GLOBALS['dbi']->tryQuery('SET `old_passwords` = ' . $value . ';');
185 PMA_changePassUrlParamsAndSubmitQuery(
186 $username, $hostname, $password,
187 $sql_query, $hashing_function, $orig_auth_plugin
190 $auth_plugin->handlePasswordChange($password);
191 PMA_getChangePassMessage($change_password_message, $sql_query);
192 PMA_changePassDisplayPage($message, $sql_query);
196 * Generate the hashing function
198 * @return string $hashing_function
200 function PMA_changePassHashingFunction()
202 if (PMA_isValid(
203 $_REQUEST['authentication_plugin'], 'identical', 'mysql_old_password'
204 )) {
205 $hashing_function = 'OLD_PASSWORD';
206 } else {
207 $hashing_function = 'PASSWORD';
209 return $hashing_function;
213 * Generate the error url and submit the query
215 * @param string $username Username
216 * @param string $hostname Hostname
217 * @param string $password Password
218 * @param string $sql_query SQL query
219 * @param string $hashing_function Hashing function
220 * @param string $orig_auth_plugin Original Authentication Plugin
222 * @return void
224 function PMA_changePassUrlParamsAndSubmitQuery(
225 $username, $hostname, $password, $sql_query, $hashing_function, $orig_auth_plugin
227 $err_url = 'user_password.php' . PMA_URL_getCommon();
228 $serverType = PMA_Util::getServerType();
230 if ($serverType == 'MySQL' && PMA_MYSQL_INT_VERSION >= 50706) {
231 $local_query = 'ALTER USER \'' . $username . '\'@\'' . $hostname . '\''
232 . ' IDENTIFIED with ' . $orig_auth_plugin . ' BY '
233 . (($password == '')
234 ? '\'\''
235 : '\'' . PMA_Util::sqlAddSlashes($password) . '\'');
236 } else if ($serverType == 'MariaDB'
237 && PMA_MYSQL_INT_VERSION >= 50200
239 if ($orig_auth_plugin == 'mysql_native_password') {
240 // Set the hashing method used by PASSWORD()
241 // to be 'mysql_native_password' type
242 $GLOBALS['dbi']->tryQuery('SET old_passwords = 0;');
243 } else if ($orig_auth_plugin == 'sha256_password') {
244 // Set the hashing method used by PASSWORD()
245 // to be 'sha256_password' type
246 $GLOBALS['dbi']->tryQuery('SET `old_passwords` = 2;');
249 $hashedPassword = PMA_getHashedPassword($_POST['pma_pw']);
251 $local_query = "UPDATE `mysql`.`user` SET"
252 . " `authentication_string` = '" . $hashedPassword
253 . "', `Password` = '', "
254 . " `plugin` = '" . $orig_auth_plugin . "'"
255 . " WHERE `User` = '" . $username . "' AND Host = '"
256 . $hostname . "';";
258 $GLOBALS['dbi']->tryQuery("FLUSH PRIVILEGES;");
259 } else {
260 $local_query = 'SET password = ' . (($password == '')
261 ? '\'\''
262 : $hashing_function . '(\'' . PMA_Util::sqlAddSlashes($password)
263 . '\')');
265 if (! @$GLOBALS['dbi']->tryQuery($local_query)) {
266 PMA_Util::mysqlDie($GLOBALS['dbi']->getError(), $sql_query, false, $err_url);
271 * Display the page
273 * @param string $message Message
274 * @param string $sql_query SQL query
276 * @return void
278 function PMA_changePassDisplayPage($message, $sql_query)
280 echo '<h1>' . __('Change password') . '</h1>' . "\n\n";
281 echo PMA_Util::getMessage(
282 $message, $sql_query, 'success'
284 echo '<a href="index.php' . PMA_URL_getCommon()
285 . ' target="_parent">' . "\n"
286 . '<strong>' . __('Back') . '</strong></a>';
287 exit;