merged master
[lhc/web/wiklou.git] / includes / specials / SpecialChangePassword.php
1 <?php
2 /**
3 * Implements Special:ChangePassword
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * Let users recover their password.
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialChangePassword extends UnlistedSpecialPage {
30 public function __construct() {
31 parent::__construct( 'ChangePassword' );
32 }
33
34 /**
35 * Main execution point
36 */
37 function execute( $par ) {
38 global $wgAuth;
39
40 $this->checkReadOnly();
41
42 $request = $this->getRequest();
43 $this->mUserName = trim( $request->getVal( 'wpName' ) );
44 $this->mOldpass = $request->getVal( 'wpPassword' );
45 $this->mNewpass = $request->getVal( 'wpNewPassword' );
46 $this->mRetype = $request->getVal( 'wpRetype' );
47 $this->mDomain = $request->getVal( 'wpDomain' );
48
49 $this->setHeaders();
50 $this->outputHeader();
51 $this->getOutput()->disallowUserJs();
52
53 $user = $this->getUser();
54 if( !$request->wasPosted() && !$user->isLoggedIn() ) {
55 $this->error( $this->msg( 'resetpass-no-info' )->text() );
56 return;
57 }
58
59 if( $request->wasPosted() && $request->getBool( 'wpCancel' ) ) {
60 $this->doReturnTo();
61 return;
62 }
63
64 if( $request->wasPosted() && $user->matchEditToken( $request->getVal( 'token' ) ) ) {
65 try {
66 $this->mDomain = $wgAuth->getDomain();
67 if( !$wgAuth->allowPasswordChange() ) {
68 $this->error( $this->msg( 'resetpass_forbidden' )->text() );
69 return;
70 }
71
72 $this->attemptReset( $this->mNewpass, $this->mRetype );
73 $this->getOutput()->addWikiMsg( 'resetpass_success' );
74 if( !$user->isLoggedIn() ) {
75 LoginForm::setLoginToken();
76 $token = LoginForm::getLoginToken();
77 $data = array(
78 'action' => 'submitlogin',
79 'wpName' => $this->mUserName,
80 'wpDomain' => $this->mDomain,
81 'wpLoginToken' => $token,
82 'wpPassword' => $this->mNewpass,
83 'returnto' => $request->getVal( 'returnto' ),
84 );
85 if( $request->getCheck( 'wpRemember' ) ) {
86 $data['wpRemember'] = 1;
87 }
88 $login = new LoginForm( new FauxRequest( $data, true ) );
89 $login->setContext( $this->getContext() );
90 $login->execute( null );
91 }
92 $this->doReturnTo();
93 } catch( PasswordError $e ) {
94 $this->error( $e->getMessage() );
95 }
96 }
97 $this->showForm();
98 }
99
100 function doReturnTo() {
101 $titleObj = Title::newFromText( $this->getRequest()->getVal( 'returnto' ) );
102 if ( !$titleObj instanceof Title ) {
103 $titleObj = Title::newMainPage();
104 }
105 $this->getOutput()->redirect( $titleObj->getFullURL() );
106 }
107
108 function error( $msg ) {
109 $this->getOutput()->addHTML( Xml::element('p', array( 'class' => 'error' ), $msg ) );
110 }
111
112 function showForm() {
113 global $wgCookieExpiration;
114
115 $user = $this->getUser();
116 if ( !$this->mUserName ) {
117 $this->mUserName = $user->getName();
118 }
119 $rememberMe = '';
120 if ( !$user->isLoggedIn() ) {
121 $rememberMe = '<tr>' .
122 '<td></td>' .
123 '<td class="mw-input">' .
124 Xml::checkLabel(
125 $this->msg( 'remembermypassword' )->numParams( ceil( $wgCookieExpiration / ( 3600 * 24 ) ) )->text(),
126 'wpRemember', 'wpRemember',
127 $this->getRequest()->getCheck( 'wpRemember' ) ) .
128 '</td>' .
129 '</tr>';
130 $submitMsg = 'resetpass_submit';
131 $oldpassMsg = 'resetpass-temp-password';
132 } else {
133 $oldpassMsg = 'oldpassword';
134 $submitMsg = 'resetpass-submit-loggedin';
135 }
136 $extraFields = array();
137 wfRunHooks( 'ChangePasswordForm', array( &$extraFields ) );
138 $prettyFields = array(
139 array( 'wpName', 'username', 'text', $this->mUserName ),
140 array( 'wpPassword', $oldpassMsg, 'password', $this->mOldpass ),
141 array( 'wpNewPassword', 'newpassword', 'password', null ),
142 array( 'wpRetype', 'retypenew', 'password', null ),
143 );
144 $prettyFields = array_merge( $prettyFields, $extraFields );
145 $this->getOutput()->addHTML(
146 Xml::fieldset( $this->msg( 'resetpass_header' )->text() ) .
147 Xml::openElement( 'form',
148 array(
149 'method' => 'post',
150 'action' => $this->getTitle()->getLocalUrl(),
151 'id' => 'mw-resetpass-form' ) ) . "\n" .
152 Html::hidden( 'token', $user->getEditToken() ) . "\n" .
153 Html::hidden( 'wpName', $this->mUserName ) . "\n" .
154 Html::hidden( 'wpDomain', $this->mDomain ) . "\n" .
155 Html::hidden( 'returnto', $this->getRequest()->getVal( 'returnto' ) ) . "\n" .
156 $this->msg( 'resetpass_text' )->parseAsBlock() . "\n" .
157 Xml::openElement( 'table', array( 'id' => 'mw-resetpass-table' ) ) . "\n" .
158 $this->pretty( $prettyFields ) . "\n" .
159 $rememberMe .
160 "<tr>\n" .
161 "<td></td>\n" .
162 '<td class="mw-input">' .
163 Xml::submitButton( $this->msg( $submitMsg )->text() ) .
164 Xml::submitButton( $this->msg( 'resetpass-submit-cancel' )->text(), array( 'name' => 'wpCancel' ) ) .
165 "</td>\n" .
166 "</tr>\n" .
167 Xml::closeElement( 'table' ) .
168 Xml::closeElement( 'form' ) .
169 Xml::closeElement( 'fieldset' ) . "\n"
170 );
171 }
172
173 function pretty( $fields ) {
174 $out = '';
175 foreach ( $fields as $list ) {
176 list( $name, $label, $type, $value ) = $list;
177 if( $type == 'text' ) {
178 $field = htmlspecialchars( $value );
179 } else {
180 $attribs = array( 'id' => $name );
181 if ( $name == 'wpNewPassword' || $name == 'wpRetype' ) {
182 $attribs = array_merge( $attribs,
183 User::passwordChangeInputAttribs() );
184 }
185 if ( $name == 'wpPassword' ) {
186 $attribs[] = 'autofocus';
187 }
188 $field = Html::input( $name, $value, $type, $attribs );
189 }
190 $out .= "<tr>\n";
191 $out .= "\t<td class='mw-label'>";
192 if ( $type != 'text' )
193 $out .= Xml::label( $this->msg( $label )->text(), $name );
194 else
195 $out .= $this->msg( $label )->escaped();
196 $out .= "</td>\n";
197 $out .= "\t<td class='mw-input'>";
198 $out .= $field;
199 $out .= "</td>\n";
200 $out .= "</tr>";
201 }
202 return $out;
203 }
204
205 /**
206 * @throws PasswordError when cannot set the new password because requirements not met.
207 */
208 protected function attemptReset( $newpass, $retype ) {
209 $user = User::newFromName( $this->mUserName );
210 if( !$user || $user->isAnon() ) {
211 throw new PasswordError( $this->msg( 'nosuchusershort', $this->mUserName )->text() );
212 }
213
214 if( $newpass !== $retype ) {
215 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
216 throw new PasswordError( $this->msg( 'badretype' )->text() );
217 }
218
219 $throttleCount = LoginForm::incLoginThrottle( $this->mUserName );
220 if ( $throttleCount === true ) {
221 throw new PasswordError( $this->msg( 'login-throttled' )->text() );
222 }
223
224 if( !$user->checkTemporaryPassword($this->mOldpass) && !$user->checkPassword($this->mOldpass) ) {
225 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
226 throw new PasswordError( $this->msg( 'resetpass-wrong-oldpass' )->text() );
227 }
228
229 // Please reset throttle for successful logins, thanks!
230 if ( $throttleCount ) {
231 LoginForm::clearLoginThrottle( $this->mUserName );
232 }
233
234 try {
235 $user->setPassword( $this->mNewpass );
236 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
237 $this->mNewpass = $this->mOldpass = $this->mRetypePass = '';
238 } catch( PasswordError $e ) {
239 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
240 throw new PasswordError( $e->getMessage() );
241 }
242
243 $user->setCookies();
244 $user->saveSettings();
245 }
246 }