user_password.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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
  6. *
  7. * @package PhpMyAdmin
  8. */
  9. /**
  10. * Gets some core libraries
  11. */
  12. require_once './libraries/common.inc.php';
  13. $response = PMA_Response::getInstance();
  14. $header = $response->getHeader();
  15. $scripts = $header->getScripts();
  16. $scripts->addFile('server_privileges.js');
  17. /**
  18. * Displays an error message and exits if the user isn't allowed to use this
  19. * script
  20. */
  21. if (! $cfg['ShowChgPassword']) {
  22. $cfg['ShowChgPassword'] = PMA_DBI_select_db('mysql');
  23. }
  24. if ($cfg['Server']['auth_type'] == 'config' || ! $cfg['ShowChgPassword']) {
  25. PMA_Message::error(
  26. __('You don\'t have sufficient privileges to be here right now!')
  27. )->display();
  28. exit;
  29. } // end if
  30. /**
  31. * If the "change password" form has been submitted, checks for valid values
  32. * and submit the query or logout
  33. */
  34. if (isset($_REQUEST['nopass'])) {
  35. if ($_REQUEST['nopass'] == '1') {
  36. $password = '';
  37. } else {
  38. $password = $_REQUEST['pma_pw'];
  39. }
  40. $change_password_message = PMA_setChangePasswordMsg();
  41. $msg = $change_password_message['msg'];
  42. if (! $change_password_message['error']) {
  43. PMA_changePassword($password, $msg, $change_password_message);
  44. } else {
  45. PMA_getChangePassMessage($change_password_message);
  46. }
  47. }
  48. /**
  49. * If the "change password" form hasn't been submitted or the values submitted
  50. * aren't valid -> displays the form
  51. */
  52. // Displays an error message if required
  53. if (isset($msg)) {
  54. $msg->display();
  55. unset($msg);
  56. }
  57. require_once './libraries/display_change_password.lib.php';
  58. echo PMA_getHtmlForChangePassword($username, $hostname);
  59. exit;
  60. /**
  61. * Send the message as an ajax request
  62. *
  63. * @param array $change_password_message
  64. * @param string $sql_query
  65. *
  66. * @return void
  67. */
  68. function PMA_getChangePassMessage($change_password_message, $sql_query = '')
  69. {
  70. if ($GLOBALS['is_ajax_request'] == true) {
  71. /**
  72. * If in an Ajax request, we don't need to show the rest of the page
  73. */
  74. $response = PMA_Response::getInstance();
  75. if ($change_password_message['error']) {
  76. $response->addJSON('message', $change_password_message['msg']);
  77. $response->isSuccess(false);
  78. } else {
  79. $sql_query = PMA_Util::getMessage(
  80. $change_password_message['msg'],
  81. $sql_query,
  82. 'success'
  83. );
  84. $response->addJSON('message', $sql_query);
  85. }
  86. exit;
  87. }
  88. }
  89. /**
  90. * Generate the message
  91. *
  92. * @return array error value and message
  93. */
  94. function PMA_setChangePasswordMsg()
  95. {
  96. $error = false;
  97. $message = PMA_Message::success(__('The profile has been updated.'));
  98. if (($_REQUEST['nopass'] != '1')) {
  99. if (empty($_REQUEST['pma_pw']) || empty($_REQUEST['pma_pw2'])) {
  100. $message = PMA_Message::error(__('The password is empty!'));
  101. $error = true;
  102. } elseif ($_REQUEST['pma_pw'] != $_REQUEST['pma_pw2']) {
  103. $message = PMA_Message::error(__('The passwords aren\'t the same!'));
  104. $error = true;
  105. } elseif (strlen($_REQUEST['pma_pw']) > 256) {
  106. $message = PMA_Message::error(__('Password is too long!'));
  107. $error = true;
  108. }
  109. }
  110. return array('error' => $error, 'msg' => $message);
  111. }
  112. /**
  113. * Change the password
  114. *
  115. * @param string $password
  116. * @param string $message
  117. * @param array $change_password_message
  118. *
  119. * @return void
  120. */
  121. function PMA_changePassword($password, $message, $change_password_message)
  122. {
  123. // Defines the url to return to in case of error in the sql statement
  124. $_url_params = array();
  125. $hashing_function = PMA_changePassHashingFunction();
  126. $sql_query = 'SET password = '
  127. . (($password == '') ? '\'\'' : $hashing_function . '(\'***\')');
  128. PMA_ChangePassUrlParamsAndSubmitQuery(
  129. $password, $_url_params, $sql_query, $hashing_function
  130. );
  131. $new_url_params = PMA_changePassAuthType($_url_params, $password);
  132. PMA_getChangePassMessage($change_password_message, $sql_query);
  133. PMA_changePassDisplayPage($message, $sql_query, $new_url_params);
  134. }
  135. /**
  136. * Generate the hashing function
  137. *
  138. * @return string $hashing_function
  139. */
  140. function PMA_changePassHashingFunction()
  141. {
  142. if (PMA_isValid($_REQUEST['pw_hash'], 'identical', 'old')) {
  143. $hashing_function = 'OLD_PASSWORD';
  144. } else {
  145. $hashing_function = 'PASSWORD';
  146. }
  147. return $hashing_function;
  148. }
  149. /**
  150. * Generate the error url and submit the query
  151. *
  152. * @param string $password
  153. * @param array $_url_params
  154. * @param string $sql_query
  155. * @param string $hashing_function
  156. *
  157. * @return void
  158. */
  159. function PMA_ChangePassUrlParamsAndSubmitQuery(
  160. $password, $_url_params, $sql_query, $hashing_function
  161. ) {
  162. $err_url = 'user_password.php' . PMA_generate_common_url($_url_params);
  163. $local_query = 'SET password = ' . (($password == '')
  164. ? '\'\''
  165. : $hashing_function . '(\'' . PMA_Util::sqlAddSlashes($password) . '\')');
  166. if (! @PMA_DBI_try_query($local_query)) {
  167. PMA_Util::mysqlDie(PMA_DBI_getError(), $sql_query, false, $err_url);
  168. }
  169. }
  170. /**
  171. * Change password authentication type
  172. *
  173. * @param array $_url_params
  174. * @param string $password
  175. *
  176. * @return array $_url_params
  177. */
  178. function PMA_changePassAuthType($_url_params, $password)
  179. {
  180. /**
  181. * Changes password cookie if required
  182. * Duration = till the browser is closed for password
  183. * (we don't want this to be saved)
  184. */
  185. // include_once "libraries/plugins/auth/AuthenticationCookie.class.php";
  186. // $auth_plugin = new AuthenticationCookie();
  187. // the $auth_plugin is already defined in common.inc.php when this is used
  188. global $auth_plugin;
  189. if ($GLOBALS['cfg']['Server']['auth_type'] == 'cookie') {
  190. $auth_plugin->storePasswordCookie($password);
  191. }
  192. /**
  193. * For http auth. mode, the "back" link will also enforce new
  194. * authentication
  195. */
  196. if ($GLOBALS['cfg']['Server']['auth_type'] == 'http') {
  197. $_url_params['old_usr'] = 'relog';
  198. }
  199. return $_url_params;
  200. }
  201. /**
  202. * Display the page
  203. *
  204. * @param string $message
  205. * @param string $sql_query
  206. * @param array $_url_params
  207. *
  208. * @return void
  209. */
  210. function PMA_changePassDisplayPage($message, $sql_query, $_url_params)
  211. {
  212. echo '<h1>' . __('Change password') . '</h1>' . "\n\n";
  213. echo PMA_Util::getMessage(
  214. $message, $sql_query, 'success'
  215. );
  216. echo '<a href="index.php'.PMA_generate_common_url($_url_params)
  217. .' target="_parent">'. "\n"
  218. .'<strong>'.__('Back').'</strong></a>';
  219. exit;
  220. }
  221. ?>