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