Merge "Added ORMIterator interface which can be used for type hinting (in particular...
[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 if ( isset( $_SESSION['wsDomain'] ) ) {
67 $this->mDomain = $_SESSION['wsDomain'];
68 }
69 $wgAuth->setDomain( $this->mDomain );
70 if( !$wgAuth->allowPasswordChange() ) {
71 $this->error( $this->msg( 'resetpass_forbidden' )->text() );
72 return;
73 }
74
75 $this->attemptReset( $this->mNewpass, $this->mRetype );
76 $this->getOutput()->addWikiMsg( 'resetpass_success' );
77 if( !$user->isLoggedIn() ) {
78 LoginForm::setLoginToken();
79 $token = LoginForm::getLoginToken();
80 $data = array(
81 'action' => 'submitlogin',
82 'wpName' => $this->mUserName,
83 'wpDomain' => $this->mDomain,
84 'wpLoginToken' => $token,
85 'wpPassword' => $this->mNewpass,
86 'returnto' => $request->getVal( 'returnto' ),
87 );
88 if( $request->getCheck( 'wpRemember' ) ) {
89 $data['wpRemember'] = 1;
90 }
91 $login = new LoginForm( new FauxRequest( $data, true ) );
92 $login->setContext( $this->getContext() );
93 $login->execute( null );
94 }
95 $this->doReturnTo();
96 } catch( PasswordError $e ) {
97 $this->error( $e->getMessage() );
98 }
99 }
100 $this->showForm();
101 }
102
103 function doReturnTo() {
104 $titleObj = Title::newFromText( $this->getRequest()->getVal( 'returnto' ) );
105 if ( !$titleObj instanceof Title ) {
106 $titleObj = Title::newMainPage();
107 }
108 $this->getOutput()->redirect( $titleObj->getFullURL() );
109 }
110
111 function error( $msg ) {
112 $this->getOutput()->addHTML( Xml::element('p', array( 'class' => 'error' ), $msg ) );
113 }
114
115 function showForm() {
116 global $wgCookieExpiration;
117
118 $user = $this->getUser();
119 if ( !$this->mUserName ) {
120 $this->mUserName = $user->getName();
121 }
122 $rememberMe = '';
123 if ( !$user->isLoggedIn() ) {
124 $rememberMe = '<tr>' .
125 '<td></td>' .
126 '<td class="mw-input">' .
127 Xml::checkLabel(
128 $this->msg( 'remembermypassword' )->numParams( ceil( $wgCookieExpiration / ( 3600 * 24 ) ) )->text(),
129 'wpRemember', 'wpRemember',
130 $this->getRequest()->getCheck( 'wpRemember' ) ) .
131 '</td>' .
132 '</tr>';
133 $submitMsg = 'resetpass_submit';
134 $oldpassMsg = 'resetpass-temp-password';
135 } else {
136 $oldpassMsg = 'oldpassword';
137 $submitMsg = 'resetpass-submit-loggedin';
138 }
139 $extraFields = array();
140 wfRunHooks( 'ChangePasswordForm', array( &$extraFields ) );
141 $prettyFields = array(
142 array( 'wpName', 'username', 'text', $this->mUserName ),
143 array( 'wpPassword', $oldpassMsg, 'password', $this->mOldpass ),
144 array( 'wpNewPassword', 'newpassword', 'password', null ),
145 array( 'wpRetype', 'retypenew', 'password', null ),
146 );
147 $prettyFields = array_merge( $prettyFields, $extraFields );
148 $this->getOutput()->addHTML(
149 Xml::fieldset( $this->msg( 'resetpass_header' )->text() ) .
150 Xml::openElement( 'form',
151 array(
152 'method' => 'post',
153 'action' => $this->getTitle()->getLocalUrl(),
154 'id' => 'mw-resetpass-form' ) ) . "\n" .
155 Html::hidden( 'token', $user->getEditToken() ) . "\n" .
156 Html::hidden( 'wpName', $this->mUserName ) . "\n" .
157 Html::hidden( 'wpDomain', $this->mDomain ) . "\n" .
158 Html::hidden( 'returnto', $this->getRequest()->getVal( 'returnto' ) ) . "\n" .
159 $this->msg( 'resetpass_text' )->parseAsBlock() . "\n" .
160 Xml::openElement( 'table', array( 'id' => 'mw-resetpass-table' ) ) . "\n" .
161 $this->pretty( $prettyFields ) . "\n" .
162 $rememberMe .
163 "<tr>\n" .
164 "<td></td>\n" .
165 '<td class="mw-input">' .
166 Xml::submitButton( $this->msg( $submitMsg )->text() ) .
167 Xml::submitButton( $this->msg( 'resetpass-submit-cancel' )->text(), array( 'name' => 'wpCancel' ) ) .
168 "</td>\n" .
169 "</tr>\n" .
170 Xml::closeElement( 'table' ) .
171 Xml::closeElement( 'form' ) .
172 Xml::closeElement( 'fieldset' ) . "\n"
173 );
174 }
175
176 function pretty( $fields ) {
177 $out = '';
178 foreach ( $fields as $list ) {
179 list( $name, $label, $type, $value ) = $list;
180 if( $type == 'text' ) {
181 $field = htmlspecialchars( $value );
182 } else {
183 $attribs = array( 'id' => $name );
184 if ( $name == 'wpNewPassword' || $name == 'wpRetype' ) {
185 $attribs = array_merge( $attribs,
186 User::passwordChangeInputAttribs() );
187 }
188 if ( $name == 'wpPassword' ) {
189 $attribs[] = 'autofocus';
190 }
191 $field = Html::input( $name, $value, $type, $attribs );
192 }
193 $out .= "<tr>\n";
194 $out .= "\t<td class='mw-label'>";
195 if ( $type != 'text' )
196 $out .= Xml::label( $this->msg( $label )->text(), $name );
197 else
198 $out .= $this->msg( $label )->escaped();
199 $out .= "</td>\n";
200 $out .= "\t<td class='mw-input'>";
201 $out .= $field;
202 $out .= "</td>\n";
203 $out .= "</tr>";
204 }
205 return $out;
206 }
207
208 /**
209 * @throws PasswordError when cannot set the new password because requirements not met.
210 */
211 protected function attemptReset( $newpass, $retype ) {
212 $user = User::newFromName( $this->mUserName );
213 if( !$user || $user->isAnon() ) {
214 throw new PasswordError( $this->msg( 'nosuchusershort', $this->mUserName )->text() );
215 }
216
217 if( $newpass !== $retype ) {
218 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
219 throw new PasswordError( $this->msg( 'badretype' )->text() );
220 }
221
222 $throttleCount = LoginForm::incLoginThrottle( $this->mUserName );
223 if ( $throttleCount === true ) {
224 throw new PasswordError( $this->msg( 'login-throttled' )->text() );
225 }
226
227 if( !$user->checkTemporaryPassword($this->mOldpass) && !$user->checkPassword($this->mOldpass) ) {
228 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
229 throw new PasswordError( $this->msg( 'resetpass-wrong-oldpass' )->text() );
230 }
231
232 // Please reset throttle for successful logins, thanks!
233 if ( $throttleCount ) {
234 LoginForm::clearLoginThrottle( $this->mUserName );
235 }
236
237 try {
238 $user->setPassword( $this->mNewpass );
239 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
240 $this->mNewpass = $this->mOldpass = $this->mRetypePass = '';
241 } catch( PasswordError $e ) {
242 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
243 throw new PasswordError( $e->getMessage() );
244 }
245
246 $user->setCookies();
247 $user->saveSettings();
248 }
249 }