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