de832d2efb7695da08f132aa097f5f16f8d0a012
[lhc/web/wiklou.git] / includes / specials / SpecialResetpass.php
1 <?php
2 /**
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 */
19
20 /**
21 * @file
22 * @ingroup SpecialPage
23 */
24
25 /**
26 * Let users recover their password.
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
50 $this->setHeaders();
51 $this->outputHeader();
52 $wgOut->disallowUserJs();
53
54 if( !$wgAuth->allowPasswordChange() ) {
55 $this->error( wfMsg( 'resetpass_forbidden' ) );
56 return;
57 }
58
59 if( !$wgRequest->wasPosted() && !$wgUser->isLoggedIn() ) {
60 $this->error( wfMsg( 'resetpass-no-info' ) );
61 return;
62 }
63
64 if( $wgRequest->wasPosted() && $wgRequest->getBool( 'wpCancel' ) ) {
65 $this->doReturnTo();
66 return;
67 }
68
69 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getVal('token') ) ) {
70 try {
71 $this->attemptReset( $this->mNewpass, $this->mRetype );
72 $wgOut->addWikiMsg( 'resetpass_success' );
73 if( !$wgUser->isLoggedIn() ) {
74 $data = array(
75 'action' => 'submitlogin',
76 'wpName' => $this->mUserName,
77 'wpPassword' => $this->mNewpass,
78 'returnto' => $wgRequest->getVal( 'returnto' ),
79 );
80 if( $wgRequest->getCheck( 'wpRemember' ) ) {
81 $data['wpRemember'] = 1;
82 }
83 $login = new LoginForm( new FauxRequest( $data, true ) );
84 $login->execute();
85 }
86 $this->doReturnTo();
87 } catch( PasswordError $e ) {
88 $this->error( $e->getMessage() );
89 }
90 }
91 $this->showForm();
92 }
93
94 function doReturnTo() {
95 global $wgRequest, $wgOut;
96 $titleObj = Title::newFromText( $wgRequest->getVal( 'returnto' ) );
97 if ( !$titleObj instanceof Title ) {
98 $titleObj = Title::newMainPage();
99 }
100 $wgOut->redirect( $titleObj->getFullURL() );
101 }
102
103 function error( $msg ) {
104 global $wgOut;
105 $wgOut->addHTML( Xml::element('p', array( 'class' => 'error' ), $msg ) );
106 }
107
108 function showForm() {
109 global $wgOut, $wgUser, $wgRequest, $wgLivePasswordStrengthChecks;
110
111 if ( $wgLivePasswordStrengthChecks ) {
112 $wgOut->addPasswordSecurity( 'wpNewPassword', 'wpRetype' );
113 }
114 $self = $this->getTitle();
115 if ( !$this->mUserName ) {
116 $this->mUserName = $wgUser->getName();
117 }
118 $rememberMe = '';
119 if ( !$wgUser->isLoggedIn() ) {
120 global $wgCookieExpiration, $wgLang;
121 $rememberMe = '<tr>' .
122 '<td></td>' .
123 '<td class="mw-input">' .
124 Xml::checkLabel(
125 wfMsgExt( 'remembermypassword', 'parsemag', $wgLang->formatNum( ceil( $wgCookieExpiration / ( 3600 * 24 ) ) ) ),
126 'wpRemember', 'wpRemember',
127 $wgRequest->getCheck( 'wpRemember' ) ) .
128 '</td>' .
129 '</tr>';
130 $submitMsg = 'resetpass_submit';
131 $oldpassMsg = 'resetpass-temp-password';
132 } else {
133 $oldpassMsg = 'oldpassword';
134 $submitMsg = 'resetpass-submit-loggedin';
135 }
136 $wgOut->addHTML(
137 Xml::fieldset( wfMsg( 'resetpass_header' ) ) .
138 Xml::openElement( 'form',
139 array(
140 'method' => 'post',
141 'action' => $self->getLocalUrl(),
142 'id' => 'mw-resetpass-form' ) ) . "\n" .
143 Xml::hidden( 'token', $wgUser->editToken() ) . "\n" .
144 Xml::hidden( 'wpName', $this->mUserName ) . "\n" .
145 Xml::hidden( 'returnto', $wgRequest->getVal( 'returnto' ) ) . "\n" .
146 wfMsgExt( 'resetpass_text', array( 'parse' ) ) . "\n" .
147 Xml::openElement( 'table', array( 'id' => 'mw-resetpass-table' ) ) . "\n" .
148 $this->pretty( array(
149 array( 'wpName', 'username', 'text', $this->mUserName, '' ),
150 array( 'wpPassword', $oldpassMsg, 'password', $this->mOldpass, '' ),
151 array( 'wpNewPassword', 'newpassword', 'password', null, '<div id="password-strength"></div>' ),
152 array( 'wpRetype', 'retypenew', 'password', null, '<div id="password-retype"></div>' ),
153 ) ) . "\n" .
154 $rememberMe .
155 "<tr>\n" .
156 "<td></td>\n" .
157 '<td class="mw-input">' .
158 Xml::submitButton( wfMsg( $submitMsg ) ) .
159 Xml::submitButton( wfMsg( 'resetpass-submit-cancel' ), array( 'name' => 'wpCancel' ) ) .
160 "</td>\n" .
161 "</tr>\n" .
162 Xml::closeElement( 'table' ) .
163 Xml::closeElement( 'form' ) .
164 Xml::closeElement( 'fieldset' ) . "\n"
165 );
166 }
167
168 function pretty( $fields ) {
169 $out = '';
170 foreach ( $fields as $list ) {
171 list( $name, $label, $type, $value, $extra ) = $list;
172 if( $type == 'text' ) {
173 $field = htmlspecialchars( $value );
174 } else {
175 $attribs = array( 'id' => $name );
176 if ( $name == 'wpNewPassword' || $name == 'wpRetype' ) {
177 $attribs = array_merge( $attribs,
178 User::passwordChangeInputAttribs() );
179 }
180 if ( $name == 'wpPassword' ) {
181 $attribs[] = 'autofocus';
182 }
183 $field = Html::input( $name, $value, $type, $attribs );
184 }
185 $out .= "<tr>\n";
186 $out .= "\t<td class='mw-label'>";
187 if ( $type != 'text' )
188 $out .= Xml::label( wfMsg( $label ), $name );
189 else
190 $out .= wfMsgHtml( $label );
191 $out .= "</td>\n";
192 $out .= "\t<td class='mw-input'>$field</td>\n";
193 $out .= "\t<td>$extra</td>\n";
194 $out .= "</tr>";
195 }
196 return $out;
197 }
198
199 /**
200 * @throws PasswordError when cannot set the new password because requirements not met.
201 */
202 protected function attemptReset( $newpass, $retype ) {
203 $user = User::newFromName( $this->mUserName );
204 if( !$user || $user->isAnon() ) {
205 throw new PasswordError( 'no such user' );
206 }
207
208 if( $newpass !== $retype ) {
209 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
210 throw new PasswordError( wfMsg( 'badretype' ) );
211 }
212
213 if( !$user->checkTemporaryPassword($this->mOldpass) && !$user->checkPassword($this->mOldpass) ) {
214 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
215 throw new PasswordError( wfMsg( 'resetpass-wrong-oldpass' ) );
216 }
217
218 try {
219 $user->setPassword( $this->mNewpass );
220 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
221 $this->mNewpass = $this->mOldpass = $this->mRetypePass = '';
222 } catch( PasswordError $e ) {
223 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
224 throw new PasswordError( $e->getMessage() );
225 return;
226 }
227
228 $user->setCookies();
229 $user->saveSettings();
230 }
231 }