SpecialResetpass.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * @file
  4. * @ingroup SpecialPage
  5. */
  6. /**
  7. * Let users recover their password.
  8. * @ingroup SpecialPage
  9. */
  10. class SpecialResetpass extends SpecialPage {
  11. public function __construct() {
  12. parent::__construct( 'Resetpass' );
  13. }
  14. /**
  15. * Main execution point
  16. */
  17. function execute( $par ) {
  18. global $wgUser, $wgAuth, $wgOut, $wgRequest;
  19. $this->mUserName = $wgRequest->getVal( 'wpName' );
  20. $this->mOldpass = $wgRequest->getVal( 'wpPassword' );
  21. $this->mNewpass = $wgRequest->getVal( 'wpNewPassword' );
  22. $this->mRetype = $wgRequest->getVal( 'wpRetype' );
  23. $this->setHeaders();
  24. $this->outputHeader();
  25. if( !$wgAuth->allowPasswordChange() ) {
  26. $this->error( wfMsg( 'resetpass_forbidden' ) );
  27. return;
  28. }
  29. if( !$wgRequest->wasPosted() && !$wgUser->isLoggedIn() ) {
  30. $this->error( wfMsg( 'resetpass-no-info' ) );
  31. return;
  32. }
  33. if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getVal('token') ) ) {
  34. try {
  35. $this->attemptReset( $this->mNewpass, $this->mRetype );
  36. $wgOut->addWikiMsg( 'resetpass_success' );
  37. if( !$wgUser->isLoggedIn() ) {
  38. $data = array(
  39. 'action' => 'submitlogin',
  40. 'wpName' => $this->mUserName,
  41. 'wpPassword' => $this->mNewpass,
  42. 'returnto' => $wgRequest->getVal( 'returnto' ),
  43. );
  44. if( $wgRequest->getCheck( 'wpRemember' ) ) {
  45. $data['wpRemember'] = 1;
  46. }
  47. $login = new LoginForm( new FauxRequest( $data, true ) );
  48. $login->execute();
  49. }
  50. $titleObj = Title::newFromText( $wgRequest->getVal( 'returnto' ) );
  51. if ( !$titleObj instanceof Title ) {
  52. $titleObj = Title::newMainPage();
  53. }
  54. $wgOut->redirect( $titleObj->getFullURL() );
  55. } catch( PasswordError $e ) {
  56. $this->error( $e->getMessage() );
  57. }
  58. }
  59. $this->showForm();
  60. }
  61. function error( $msg ) {
  62. global $wgOut;
  63. $wgOut->addHTML( Xml::element('p', array( 'class' => 'error' ), $msg ) );
  64. }
  65. function showForm() {
  66. global $wgOut, $wgUser, $wgRequest;
  67. $wgOut->disallowUserJs();
  68. $self = SpecialPage::getTitleFor( 'Resetpass' );
  69. if ( !$this->mUserName ) {
  70. $this->mUserName = $wgUser->getName();
  71. }
  72. $rememberMe = '';
  73. if ( !$wgUser->isLoggedIn() ) {
  74. $rememberMe = '<tr>' .
  75. '<td></td>' .
  76. '<td class="mw-input">' .
  77. Xml::checkLabel( wfMsg( 'remembermypassword' ),
  78. 'wpRemember', 'wpRemember',
  79. $wgRequest->getCheck( 'wpRemember' ) ) .
  80. '</td>' .
  81. '</tr>';
  82. $submitMsg = 'resetpass_submit';
  83. $oldpassMsg = 'resetpass-temp-password';
  84. } else {
  85. $oldpassMsg = 'oldpassword';
  86. $submitMsg = 'resetpass-submit-loggedin';
  87. }
  88. $wgOut->addHTML(
  89. Xml::fieldset( wfMsg( 'resetpass_header' ) ) .
  90. Xml::openElement( 'form',
  91. array(
  92. 'method' => 'post',
  93. 'action' => $self->getLocalUrl(),
  94. 'id' => 'mw-resetpass-form' ) ) .
  95. Xml::hidden( 'token', $wgUser->editToken() ) .
  96. Xml::hidden( 'wpName', $this->mUserName ) .
  97. Xml::hidden( 'returnto', $wgRequest->getVal( 'returnto' ) ) .
  98. wfMsgExt( 'resetpass_text', array( 'parse' ) ) .
  99. Xml::openElement( 'table', array( 'id' => 'mw-resetpass-table' ) ) .
  100. $this->pretty( array(
  101. array( 'wpName', 'username', 'text', $this->mUserName ),
  102. array( 'wpPassword', $oldpassMsg, 'password', $this->mOldpass ),
  103. array( 'wpNewPassword', 'newpassword', 'password', '' ),
  104. array( 'wpRetype', 'retypenew', 'password', '' ),
  105. ) ) .
  106. $rememberMe .
  107. '<tr>' .
  108. '<td></td>' .
  109. '<td class="mw-input">' .
  110. Xml::submitButton( wfMsg( $submitMsg ) ) .
  111. '</td>' .
  112. '</tr>' .
  113. Xml::closeElement( 'table' ) .
  114. Xml::closeElement( 'form' ) .
  115. Xml::closeElement( 'fieldset' )
  116. );
  117. }
  118. function pretty( $fields ) {
  119. $out = '';
  120. foreach( $fields as $list ) {
  121. list( $name, $label, $type, $value ) = $list;
  122. if( $type == 'text' ) {
  123. $field = htmlspecialchars( $value );
  124. } else {
  125. $field = Xml::input( $name, 20, $value,
  126. array( 'id' => $name, 'type' => $type ) );
  127. }
  128. $out .= '<tr>';
  129. $out .= "<td class='mw-label'>";
  130. if ( $type != 'text' )
  131. $out .= Xml::label( wfMsg( $label ), $name );
  132. else
  133. $out .= wfMsg( $label );
  134. $out .= '</td>';
  135. $out .= "<td class='mw-input'>";
  136. $out .= $field;
  137. $out .= '</td>';
  138. $out .= '</tr>';
  139. }
  140. return $out;
  141. }
  142. /**
  143. * @throws PasswordError when cannot set the new password because requirements not met.
  144. */
  145. protected function attemptReset( $newpass, $retype ) {
  146. $user = User::newFromName( $this->mUserName );
  147. if( !$user || $user->isAnon() ) {
  148. throw new PasswordError( 'no such user' );
  149. }
  150. if( $newpass !== $retype ) {
  151. wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
  152. throw new PasswordError( wfMsg( 'badretype' ) );
  153. }
  154. if( !$user->checkTemporaryPassword($this->mOldpass) && !$user->checkPassword($this->mOldpass) ) {
  155. wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
  156. throw new PasswordError( wfMsg( 'resetpass-wrong-oldpass' ) );
  157. }
  158. try {
  159. $user->setPassword( $this->mNewpass );
  160. wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
  161. $this->mNewpass = $this->mOldpass = $this->mRetypePass = '';
  162. } catch( PasswordError $e ) {
  163. wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
  164. throw new PasswordError( $e->getMessage() );
  165. return;
  166. }
  167. $user->setCookies();
  168. $user->saveSettings();
  169. }
  170. }