Make it show email as required if you choose to email a random password.
[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
31 protected $mUserName, $mOldpass, $mNewpass, $mRetype, $mDomain;
32
33 public function __construct() {
34 parent::__construct( 'ChangePassword' );
35 }
36
37 /**
38 * Main execution point
39 */
40 function execute( $par ) {
41 global $wgAuth;
42
43 $this->setHeaders();
44 $this->outputHeader();
45 $this->getOutput()->disallowUserJs();
46
47 $request = $this->getRequest();
48 $this->mUserName = trim( $request->getVal( 'wpName' ) );
49 $this->mOldpass = $request->getVal( 'wpPassword' );
50 $this->mNewpass = $request->getVal( 'wpNewPassword' );
51 $this->mRetype = $request->getVal( 'wpRetype' );
52 $this->mDomain = $request->getVal( 'wpDomain' );
53
54 $user = $this->getUser();
55 if ( !$request->wasPosted() && !$user->isLoggedIn() ) {
56 $this->error( $this->msg( 'resetpass-no-info' )->text() );
57
58 return;
59 }
60
61 if ( $request->wasPosted() && $request->getBool( 'wpCancel' ) ) {
62 $this->doReturnTo();
63
64 return;
65 }
66
67 $this->checkReadOnly();
68
69 if ( $request->wasPosted() && $user->matchEditToken( $request->getVal( 'token' ) ) ) {
70 try {
71 $this->mDomain = $wgAuth->getDomain();
72 if ( !$wgAuth->allowPasswordChange() ) {
73 $this->error( $this->msg( 'resetpass_forbidden' )->text() );
74
75 return;
76 }
77
78 $this->attemptReset( $this->mNewpass, $this->mRetype );
79
80 if ( $user->isLoggedIn() ) {
81 $this->doReturnTo();
82 } else {
83 LoginForm::setLoginToken();
84 $token = LoginForm::getLoginToken();
85 $data = array(
86 'action' => 'submitlogin',
87 'wpName' => $this->mUserName,
88 'wpDomain' => $this->mDomain,
89 'wpLoginToken' => $token,
90 'wpPassword' => $request->getVal( 'wpNewPassword' ),
91 ) + $request->getValues( 'wpRemember', 'returnto', 'returntoquery' );
92 $login = new LoginForm( new FauxRequest( $data, true ) );
93 $login->setContext( $this->getContext() );
94 $login->execute( null );
95 }
96
97 return;
98 } catch ( PasswordError $e ) {
99 $this->error( $e->getMessage() );
100 }
101 }
102 $this->showForm();
103 }
104
105 function doReturnTo() {
106 $request = $this->getRequest();
107 $titleObj = Title::newFromText( $request->getVal( 'returnto' ) );
108 if ( !$titleObj instanceof Title ) {
109 $titleObj = Title::newMainPage();
110 }
111 $query = $request->getVal( 'returntoquery' );
112 $this->getOutput()->redirect( $titleObj->getFullURL( $query ) );
113 }
114
115 /**
116 * @param $msg string
117 */
118 function error( $msg ) {
119 $this->getOutput()->addHTML( Xml::element( 'p', array( 'class' => 'error' ), $msg ) );
120 }
121
122 function showForm() {
123 global $wgCookieExpiration;
124
125 $user = $this->getUser();
126 if ( !$this->mUserName ) {
127 $this->mUserName = $user->getName();
128 }
129 $rememberMe = '';
130 if ( !$user->isLoggedIn() ) {
131 $rememberMe = '<tr>' .
132 '<td></td>' .
133 '<td class="mw-input">' .
134 Xml::checkLabel(
135 $this->msg( 'remembermypassword' )->numParams( ceil( $wgCookieExpiration / ( 3600 * 24 ) ) )->text(),
136 'wpRemember', 'wpRemember',
137 $this->getRequest()->getCheck( 'wpRemember' ) ) .
138 '</td>' .
139 '</tr>';
140 $submitMsg = 'resetpass_submit';
141 $oldpassMsg = 'resetpass-temp-password';
142 } else {
143 $oldpassMsg = 'oldpassword';
144 $submitMsg = 'resetpass-submit-loggedin';
145 }
146 $extraFields = array();
147 wfRunHooks( 'ChangePasswordForm', array( &$extraFields ) );
148 $prettyFields = array(
149 array( 'wpName', 'username', 'text', $this->mUserName ),
150 array( 'wpPassword', $oldpassMsg, 'password', $this->mOldpass ),
151 array( 'wpNewPassword', 'newpassword', 'password', null ),
152 array( 'wpRetype', 'retypenew', 'password', null ),
153 );
154 $prettyFields = array_merge( $prettyFields, $extraFields );
155 $hiddenFields = array(
156 'token' => $user->getEditToken(),
157 'wpName' => $this->mUserName,
158 'wpDomain' => $this->mDomain,
159 ) + $this->getRequest()->getValues( 'returnto', 'returntoquery' );
160 $hiddenFieldsStr = '';
161 foreach ( $hiddenFields as $fieldname => $fieldvalue ) {
162 $hiddenFieldsStr .= Html::hidden( $fieldname, $fieldvalue ) . "\n";
163 }
164 $this->getOutput()->addHTML(
165 Xml::fieldset( $this->msg( 'resetpass_header' )->text() ) .
166 Xml::openElement( 'form',
167 array(
168 'method' => 'post',
169 'action' => $this->getTitle()->getLocalURL(),
170 'id' => 'mw-resetpass-form' ) ) . "\n" .
171 $hiddenFieldsStr .
172 $this->msg( 'resetpass_text' )->parseAsBlock() . "\n" .
173 Xml::openElement( 'table', array( 'id' => 'mw-resetpass-table' ) ) . "\n" .
174 $this->pretty( $prettyFields ) . "\n" .
175 $rememberMe .
176 "<tr>\n" .
177 "<td></td>\n" .
178 '<td class="mw-input">' .
179 Xml::submitButton( $this->msg( $submitMsg )->text() ) .
180 Xml::submitButton( $this->msg( 'resetpass-submit-cancel' )->text(), array( 'name' => 'wpCancel' ) ) .
181 "</td>\n" .
182 "</tr>\n" .
183 Xml::closeElement( 'table' ) .
184 Xml::closeElement( 'form' ) .
185 Xml::closeElement( 'fieldset' ) . "\n"
186 );
187 }
188
189 /**
190 * @param $fields array
191 * @return string
192 */
193 function pretty( $fields ) {
194 $out = '';
195 foreach ( $fields as $list ) {
196 list( $name, $label, $type, $value ) = $list;
197 if ( $type == 'text' ) {
198 $field = htmlspecialchars( $value );
199 } else {
200 $attribs = array( 'id' => $name );
201 if ( $name == 'wpNewPassword' || $name == 'wpRetype' ) {
202 $attribs = array_merge( $attribs,
203 User::passwordChangeInputAttribs() );
204 }
205 if ( $name == 'wpPassword' ) {
206 $attribs[] = 'autofocus';
207 }
208 $field = Html::input( $name, $value, $type, $attribs );
209 }
210 $out .= "<tr>\n";
211 $out .= "\t<td class='mw-label'>";
212
213 if ( $type != 'text' ) {
214 $out .= Xml::label( $this->msg( $label )->text(), $name );
215 } else {
216 $out .= $this->msg( $label )->escaped();
217 }
218
219 $out .= "</td>\n";
220 $out .= "\t<td class='mw-input'>";
221 $out .= $field;
222 $out .= "</td>\n";
223 $out .= "</tr>";
224 }
225
226 return $out;
227 }
228
229 /**
230 * @throws PasswordError when cannot set the new password because requirements not met.
231 */
232 protected function attemptReset( $newpass, $retype ) {
233 $isSelf = ( $this->mUserName === $this->getUser()->getName() );
234 if ( $isSelf ) {
235 $user = $this->getUser();
236 } else {
237 $user = User::newFromName( $this->mUserName );
238 }
239
240 if ( !$user || $user->isAnon() ) {
241 throw new PasswordError( $this->msg( 'nosuchusershort', $this->mUserName )->text() );
242 }
243
244 if ( $newpass !== $retype ) {
245 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
246 throw new PasswordError( $this->msg( 'badretype' )->text() );
247 }
248
249 $throttleCount = LoginForm::incLoginThrottle( $this->mUserName );
250 if ( $throttleCount === true ) {
251 throw new PasswordError( $this->msg( 'login-throttled' )->text() );
252 }
253
254 $abortMsg = 'resetpass-abort-generic';
255 if ( !wfRunHooks( 'AbortChangePassword', array( $user, $this->mOldpass, $newpass, &$abortMsg ) ) ) {
256 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'abortreset' ) );
257 throw new PasswordError( $this->msg( $abortMsg )->text() );
258 }
259
260 if ( !$user->checkTemporaryPassword( $this->mOldpass ) && !$user->checkPassword( $this->mOldpass ) ) {
261 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
262 throw new PasswordError( $this->msg( 'resetpass-wrong-oldpass' )->text() );
263 }
264
265 // Please reset throttle for successful logins, thanks!
266 if ( $throttleCount ) {
267 LoginForm::clearLoginThrottle( $this->mUserName );
268 }
269
270 try {
271 $user->setPassword( $this->mNewpass );
272 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
273 $this->mNewpass = $this->mOldpass = $this->mRetype = '';
274 } catch ( PasswordError $e ) {
275 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
276 throw new PasswordError( $e->getMessage() );
277 }
278
279 if ( $isSelf ) {
280 // This is needed to keep the user connected since
281 // changing the password also modifies the user's token.
282 $user->setCookies();
283 }
284
285 $user->saveSettings();
286 }
287
288 protected function getGroupName() {
289 return 'users';
290 }
291 }