256cc903944944e7e6f25f796035d662aa0e6b49
[lhc/web/wiklou.git] / includes / specials / SpecialResetpass.php
1 <?php
2 /**
3 * Implements Special:Resetpass
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 SpecialResetpass extends SpecialPage {
30 public function __construct() {
31 parent::__construct( 'Resetpass' );
32 }
33
34 /**
35 * Main execution point
36 */
37 function execute( $par ) {
38 global $wgUser, $wgAuth, $wgOut, $wgRequest;
39
40 if ( wfReadOnly() ) {
41 $wgOut->readOnlyPage();
42 return;
43 }
44
45 $this->mUserName = $wgRequest->getVal( 'wpName' );
46 $this->mOldpass = $wgRequest->getVal( 'wpPassword' );
47 $this->mNewpass = $wgRequest->getVal( 'wpNewPassword' );
48 $this->mRetype = $wgRequest->getVal( 'wpRetype' );
49 $this->mDomain = $wgRequest->getVal( 'wpDomain' );
50
51 $this->setHeaders();
52 $this->outputHeader();
53 $wgOut->disallowUserJs();
54
55 if( !$wgRequest->wasPosted() && !$wgUser->isLoggedIn() ) {
56 $this->error( wfMsg( 'resetpass-no-info' ) );
57 return;
58 }
59
60 if( $wgRequest->wasPosted() && $wgRequest->getBool( 'wpCancel' ) ) {
61 $this->doReturnTo();
62 return;
63 }
64
65 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getVal( 'token' ) ) ) {
66 try {
67 $wgAuth->setDomain( $this->mDomain );
68 if( !$wgAuth->allowPasswordChange() ) {
69 $this->error( wfMsg( 'resetpass_forbidden' ) );
70 return;
71 }
72
73 $this->attemptReset( $this->mNewpass, $this->mRetype );
74 $wgOut->addWikiMsg( 'resetpass_success' );
75 if( !$wgUser->isLoggedIn() ) {
76 LoginForm::setLoginToken();
77 $token = LoginForm::getLoginToken();
78 $data = array(
79 'action' => 'submitlogin',
80 'wpName' => $this->mUserName,
81 'wpDomain' => $this->mDomain,
82 'wpLoginToken' => $token,
83 'wpPassword' => $this->mNewpass,
84 'returnto' => $wgRequest->getVal( 'returnto' ),
85 );
86 if( $wgRequest->getCheck( 'wpRemember' ) ) {
87 $data['wpRemember'] = 1;
88 }
89 $login = new LoginForm( new FauxRequest( $data, true ) );
90 $login->execute( null );
91 }
92 $this->doReturnTo();
93 } catch( PasswordError $e ) {
94 $this->error( $e->getMessage() );
95 }
96 }
97 $this->showForm();
98 }
99
100 function doReturnTo() {
101 global $wgRequest, $wgOut;
102 $titleObj = Title::newFromText( $wgRequest->getVal( 'returnto' ) );
103 if ( !$titleObj instanceof Title ) {
104 $titleObj = Title::newMainPage();
105 }
106 $wgOut->redirect( $titleObj->getFullURL() );
107 }
108
109 function error( $msg ) {
110 global $wgOut;
111 $wgOut->addHTML( Xml::element('p', array( 'class' => 'error' ), $msg ) );
112 }
113
114 function showForm() {
115 global $wgOut, $wgUser, $wgRequest, $wgLivePasswordStrengthChecks;
116
117 if ( $wgLivePasswordStrengthChecks ) {
118 $wgOut->addPasswordSecurity( 'wpNewPassword', 'wpRetype' );
119 }
120 $self = $this->getTitle();
121 if ( !$this->mUserName ) {
122 $this->mUserName = $wgUser->getName();
123 }
124 $rememberMe = '';
125 if ( !$wgUser->isLoggedIn() ) {
126 global $wgCookieExpiration, $wgLang;
127 $rememberMe = '<tr>' .
128 '<td></td>' .
129 '<td class="mw-input">' .
130 Xml::checkLabel(
131 wfMsgExt( 'remembermypassword', 'parsemag', $wgLang->formatNum( ceil( $wgCookieExpiration / ( 3600 * 24 ) ) ) ),
132 'wpRemember', 'wpRemember',
133 $wgRequest->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 $wgOut->addHTML(
143 Xml::fieldset( wfMsg( 'resetpass_header' ) ) .
144 Xml::openElement( 'form',
145 array(
146 'method' => 'post',
147 'action' => $self->getLocalUrl(),
148 'id' => 'mw-resetpass-form' ) ) . "\n" .
149 Html::hidden( 'token', $wgUser->editToken() ) . "\n" .
150 Html::hidden( 'wpName', $this->mUserName ) . "\n" .
151 Html::hidden( 'wpDomain', $this->mDomain ) . "\n" .
152 Html::hidden( 'returnto', $wgRequest->getVal( 'returnto' ) ) . "\n" .
153 wfMsgExt( 'resetpass_text', array( 'parse' ) ) . "\n" .
154 Xml::openElement( 'table', array( 'id' => 'mw-resetpass-table' ) ) . "\n" .
155 $this->pretty( array(
156 array( 'wpName', 'username', 'text', $this->mUserName, '' ),
157 array( 'wpPassword', $oldpassMsg, 'password', $this->mOldpass, '' ),
158 array( 'wpNewPassword', 'newpassword', 'password', null, '<div id="password-strength"></div>' ),
159 array( 'wpRetype', 'retypenew', 'password', null, '<div id="password-retype"></div>' ),
160 ) ) . "\n" .
161 $rememberMe .
162 "<tr>\n" .
163 "<td></td>\n" .
164 '<td class="mw-input">' .
165 Xml::submitButton( wfMsg( $submitMsg ) ) .
166 Xml::submitButton( wfMsg( 'resetpass-submit-cancel' ), array( 'name' => 'wpCancel' ) ) .
167 "</td>\n" .
168 "</tr>\n" .
169 Xml::closeElement( 'table' ) .
170 Xml::closeElement( 'form' ) .
171 Xml::closeElement( 'fieldset' ) . "\n"
172 );
173 }
174
175 function pretty( $fields ) {
176 $out = '';
177 foreach ( $fields as $list ) {
178 list( $name, $label, $type, $value, $extra ) = $list;
179 if( $type == 'text' ) {
180 $field = htmlspecialchars( $value );
181 } else {
182 $attribs = array( 'id' => $name );
183 if ( $name == 'wpNewPassword' || $name == 'wpRetype' ) {
184 $attribs = array_merge( $attribs,
185 User::passwordChangeInputAttribs() );
186 }
187 if ( $name == 'wpPassword' ) {
188 $attribs[] = 'autofocus';
189 }
190 $field = Html::input( $name, $value, $type, $attribs );
191 }
192 $out .= "<tr>\n";
193 $out .= "\t<td class='mw-label'>";
194 if ( $type != 'text' )
195 $out .= Xml::label( wfMsg( $label ), $name );
196 else
197 $out .= wfMsgHtml( $label );
198 $out .= "</td>\n";
199 $out .= "\t<td class='mw-input'>$field</td>\n";
200 $out .= "\t<td>$extra</td>\n";
201 $out .= "</tr>";
202 }
203 return $out;
204 }
205
206 /**
207 * @throws PasswordError when cannot set the new password because requirements not met.
208 */
209 protected function attemptReset( $newpass, $retype ) {
210 $user = User::newFromName( $this->mUserName );
211 if( !$user || $user->isAnon() ) {
212 throw new PasswordError( 'no such user' );
213 }
214
215 if( $newpass !== $retype ) {
216 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
217 throw new PasswordError( wfMsg( 'badretype' ) );
218 }
219
220 if( !$user->checkTemporaryPassword($this->mOldpass) && !$user->checkPassword($this->mOldpass) ) {
221 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
222 throw new PasswordError( wfMsg( 'resetpass-wrong-oldpass' ) );
223 }
224
225 try {
226 $user->setPassword( $this->mNewpass );
227 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
228 $this->mNewpass = $this->mOldpass = $this->mRetypePass = '';
229 } catch( PasswordError $e ) {
230 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
231 throw new PasswordError( $e->getMessage() );
232 }
233
234 $user->setCookies();
235 $user->saveSettings();
236 }
237 }