user_password.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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'] = $GLOBALS['dbi']->selectDb('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 Message to display
  64. * @param string $sql_query SQL query executed
  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. }
  106. }
  107. return array('error' => $error, 'msg' => $message);
  108. }
  109. /**
  110. * Change the password
  111. *
  112. * @param string $password New password
  113. * @param string $message Message
  114. * @param array $change_password_message Message to show
  115. *
  116. * @return void
  117. */
  118. function PMA_changePassword($password, $message, $change_password_message)
  119. {
  120. // Defines the url to return to in case of error in the sql statement
  121. $_url_params = array();
  122. $hashing_function = PMA_changePassHashingFunction();
  123. $sql_query = 'SET password = '
  124. . (($password == '') ? '\'\'' : $hashing_function . '(\'***\')');
  125. PMA_changePassUrlParamsAndSubmitQuery(
  126. $password, $_url_params, $sql_query, $hashing_function
  127. );
  128. $new_url_params = PMA_changePassAuthType($_url_params, $password);
  129. PMA_getChangePassMessage($change_password_message, $sql_query);
  130. PMA_changePassDisplayPage($message, $sql_query, $new_url_params);
  131. }
  132. /**
  133. * Generate the hashing function
  134. *
  135. * @return string $hashing_function
  136. */
  137. function PMA_changePassHashingFunction()
  138. {
  139. if (PMA_isValid($_REQUEST['pw_hash'], 'identical', 'old')) {
  140. $hashing_function = 'OLD_PASSWORD';
  141. } else {
  142. $hashing_function = 'PASSWORD';
  143. }
  144. return $hashing_function;
  145. }
  146. /**
  147. * Generate the error url and submit the query
  148. *
  149. * @param string $password Password
  150. * @param array $_url_params URL parameters
  151. * @param string $sql_query SQL query
  152. * @param string $hashing_function Hashing function
  153. *
  154. * @return void
  155. */
  156. function PMA_changePassUrlParamsAndSubmitQuery(
  157. $password, $_url_params, $sql_query, $hashing_function
  158. ) {
  159. $err_url = 'user_password.php' . PMA_URL_getCommon($_url_params);
  160. $local_query = 'SET password = ' . (($password == '')
  161. ? '\'\''
  162. : $hashing_function . '(\'' . PMA_Util::sqlAddSlashes($password) . '\')');
  163. if (! @$GLOBALS['dbi']->tryQuery($local_query)) {
  164. PMA_Util::mysqlDie($GLOBALS['dbi']->getError(), $sql_query, false, $err_url);
  165. }
  166. }
  167. /**
  168. * Change password authentication type
  169. *
  170. * @param array $_url_params URL parameters
  171. * @param string $password Password
  172. *
  173. * @return array $_url_params
  174. */
  175. function PMA_changePassAuthType($_url_params, $password)
  176. {
  177. /**
  178. * Changes password cookie if required
  179. * Duration = till the browser is closed for password
  180. * (we don't want this to be saved)
  181. */
  182. // include_once "libraries/plugins/auth/AuthenticationCookie.class.php";
  183. // $auth_plugin = new AuthenticationCookie();
  184. // the $auth_plugin is already defined in common.inc.php when this is used
  185. global $auth_plugin;
  186. if ($GLOBALS['cfg']['Server']['auth_type'] == 'cookie') {
  187. $GLOBALS['PMA_Config']->setCookie(
  188. 'pmaPass-' . $GLOBALS['server'],
  189. $auth_plugin->blowfishEncrypt(
  190. $password,
  191. $GLOBALS['cfg']['blowfish_secret']
  192. )
  193. );
  194. }
  195. /**
  196. * For http auth. mode, the "back" link will also enforce new
  197. * authentication
  198. */
  199. if ($GLOBALS['cfg']['Server']['auth_type'] == 'http') {
  200. $_url_params['old_usr'] = 'relog';
  201. }
  202. return $_url_params;
  203. }
  204. /**
  205. * Display the page
  206. *
  207. * @param string $message Message
  208. * @param string $sql_query SQL query
  209. * @param array $_url_params URL parameters
  210. *
  211. * @return void
  212. */
  213. function PMA_changePassDisplayPage($message, $sql_query, $_url_params)
  214. {
  215. echo '<h1>' . __('Change password') . '</h1>' . "\n\n";
  216. echo PMA_Util::getMessage(
  217. $message, $sql_query, 'success'
  218. );
  219. echo '<a href="index.php' . PMA_URL_getCommon($_url_params)
  220. . ' target="_parent">' . "\n"
  221. . '<strong>' . __('Back') . '</strong></a>';
  222. exit;
  223. }
  224. ?>