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