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