Tidy up the class
[mediawiki.git] / includes / specials / SpecialResetpass.php
blob8c1eee708399037be8eb1595ed7bed996496ab40
1 <?php
2 /**
3 * @file
4 * @ingroup SpecialPage
5 */
7 /**
8 * Let users recover their password.
9 * @ingroup SpecialPage
11 class SpecialResetpass extends SpecialPage {
12 public function __construct() {
13 parent::__construct( 'Resetpass' );
16 /**
17 * Main execution point
19 function execute( $par ) {
20 global $wgUser, $wgAuth, $wgOut, $wgRequest;
22 if ( wfReadOnly() ) {
23 $wgOut->readOnlyPage();
24 return;
27 $this->mUserName = $wgRequest->getVal( 'wpName' );
28 $this->mOldpass = $wgRequest->getVal( 'wpPassword' );
29 $this->mNewpass = $wgRequest->getVal( 'wpNewPassword' );
30 $this->mRetype = $wgRequest->getVal( 'wpRetype' );
32 $this->setHeaders();
33 $this->outputHeader();
35 if( !$wgAuth->allowPasswordChange() ) {
36 $this->error( wfMsg( 'resetpass_forbidden' ) );
37 return;
40 if( !$wgRequest->wasPosted() && !$wgUser->isLoggedIn() ) {
41 $this->error( wfMsg( 'resetpass-no-info' ) );
42 return;
45 if( $wgRequest->wasPosted() && $wgRequest->getBool( 'wpCancel' ) ) {
46 $this->doReturnTo();
47 return;
50 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getVal('token') ) ) {
51 try {
52 $this->attemptReset( $this->mNewpass, $this->mRetype );
53 $wgOut->addWikiMsg( 'resetpass_success' );
54 if( !$wgUser->isLoggedIn() ) {
55 $data = array(
56 'action' => 'submitlogin',
57 'wpName' => $this->mUserName,
58 'wpPassword' => $this->mNewpass,
59 'returnto' => $wgRequest->getVal( 'returnto' ),
61 if( $wgRequest->getCheck( 'wpRemember' ) ) {
62 $data['wpRemember'] = 1;
64 $login = new LoginForm( new FauxRequest( $data, true ) );
65 $login->execute();
67 $this->doReturnTo();
68 } catch( PasswordError $e ) {
69 $this->error( $e->getMessage() );
72 $this->showForm();
75 function doReturnTo() {
76 global $wgRequest, $wgOut;
77 $titleObj = Title::newFromText( $wgRequest->getVal( 'returnto' ) );
78 if ( !$titleObj instanceof Title ) {
79 $titleObj = Title::newMainPage();
81 $wgOut->redirect( $titleObj->getFullURL() );
84 function error( $msg ) {
85 global $wgOut;
86 $wgOut->addHTML( Xml::element('p', array( 'class' => 'error' ), $msg ) );
89 function showForm() {
90 global $wgOut, $wgUser, $wgRequest;
92 $wgOut->disallowUserJs();
94 $self = SpecialPage::getTitleFor( 'Resetpass' );
95 if ( !$this->mUserName ) {
96 $this->mUserName = $wgUser->getName();
98 $rememberMe = '';
99 if ( !$wgUser->isLoggedIn() ) {
100 $rememberMe = '<tr>' .
101 '<td></td>' .
102 '<td class="mw-input">' .
103 Xml::checkLabel( wfMsg( 'remembermypassword' ),
104 'wpRemember', 'wpRemember',
105 $wgRequest->getCheck( 'wpRemember' ) ) .
106 '</td>' .
107 '</tr>';
108 $submitMsg = 'resetpass_submit';
109 $oldpassMsg = 'resetpass-temp-password';
110 } else {
111 $oldpassMsg = 'oldpassword';
112 $submitMsg = 'resetpass-submit-loggedin';
114 $wgOut->addHTML(
115 Xml::fieldset( wfMsg( 'resetpass_header' ) ) .
116 Xml::openElement( 'form',
117 array(
118 'method' => 'post',
119 'action' => $self->getLocalUrl(),
120 'id' => 'mw-resetpass-form' ) ) . "\n" .
121 Xml::hidden( 'token', $wgUser->editToken() ) . "\n" .
122 Xml::hidden( 'wpName', $this->mUserName ) . "\n" .
123 Xml::hidden( 'returnto', $wgRequest->getVal( 'returnto' ) ) . "\n" .
124 wfMsgExt( 'resetpass_text', array( 'parse' ) ) . "\n" .
125 Xml::openElement( 'table', array( 'id' => 'mw-resetpass-table' ) ) . "\n" .
126 $this->pretty( array(
127 array( 'wpName', 'username', 'text', $this->mUserName ),
128 array( 'wpPassword', $oldpassMsg, 'password', $this->mOldpass ),
129 array( 'wpNewPassword', 'newpassword', 'password', null ),
130 array( 'wpRetype', 'retypenew', 'password', null ),
131 ) ) . "\n" .
132 $rememberMe .
133 "<tr>\n" .
134 "<td></td>\n" .
135 '<td class="mw-input">' .
136 Xml::submitButton( wfMsg( $submitMsg ) ) .
137 Xml::submitButton( wfMsg( 'resetpass-submit-cancel' ), array( 'name' => 'wpCancel' ) ) .
138 "</td>\n" .
139 "</tr>\n" .
140 Xml::closeElement( 'table' ) .
141 Xml::closeElement( 'form' ) .
142 Xml::closeElement( 'fieldset' ) . "\n"
146 function pretty( $fields ) {
147 $out = '';
148 foreach ( $fields as $list ) {
149 list( $name, $label, $type, $value ) = $list;
150 if( $type == 'text' ) {
151 $field = htmlspecialchars( $value );
152 } else {
153 $attribs = array( 'id' => $name );
154 if ( $name == 'wpNewPassword' || $name == 'wpRetype' ) {
155 $attribs = array_merge( $attribs,
156 User::passwordChangeInputAttribs() );
158 if ( $name == 'wpPassword' ) {
159 $attribs[] = 'autofocus';
161 $field = Html::input( $name, $value, $type, $attribs );
163 $out .= "<tr>\n";
164 $out .= "\t<td class='mw-label'>";
165 if ( $type != 'text' )
166 $out .= Xml::label( wfMsg( $label ), $name );
167 else
168 $out .= wfMsgHtml( $label );
169 $out .= "</td>\n";
170 $out .= "\t<td class='mw-input'>";
171 $out .= $field;
172 $out .= "</td>\n";
173 $out .= "</tr>";
175 return $out;
179 * @throws PasswordError when cannot set the new password because requirements not met.
181 protected function attemptReset( $newpass, $retype ) {
182 $user = User::newFromName( $this->mUserName );
183 if( !$user || $user->isAnon() ) {
184 throw new PasswordError( 'no such user' );
187 if( $newpass !== $retype ) {
188 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
189 throw new PasswordError( wfMsg( 'badretype' ) );
192 if( !$user->checkTemporaryPassword($this->mOldpass) && !$user->checkPassword($this->mOldpass) ) {
193 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
194 throw new PasswordError( wfMsg( 'resetpass-wrong-oldpass' ) );
197 try {
198 $user->setPassword( $this->mNewpass );
199 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
200 $this->mNewpass = $this->mOldpass = $this->mRetypePass = '';
201 } catch( PasswordError $e ) {
202 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
203 throw new PasswordError( $e->getMessage() );
204 return;
207 $user->setCookies();
208 $user->saveSettings();