7a549f44513ff304cf2af683b728afb27c66711c
[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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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;
110
111 $self = $this->getTitle();
112 if ( !$this->mUserName ) {
113 $this->mUserName = $wgUser->getName();
114 }
115 $rememberMe = '';
116 if ( !$wgUser->isLoggedIn() ) {
117 global $wgCookieExpiration, $wgLang;
118 $rememberMe = '<tr>' .
119 '<td></td>' .
120 '<td class="mw-input">' .
121 Xml::checkLabel(
122 wfMsgExt( 'remembermypassword', 'parsemag', $wgLang->formatNum( ceil( $wgCookieExpiration / ( 3600 * 24 ) ) ) ),
123 'wpRemember', 'wpRemember',
124 $wgRequest->getCheck( 'wpRemember' ) ) .
125 '</td>' .
126 '</tr>';
127 $submitMsg = 'resetpass_submit';
128 $oldpassMsg = 'resetpass-temp-password';
129 } else {
130 $oldpassMsg = 'oldpassword';
131 $submitMsg = 'resetpass-submit-loggedin';
132 }
133 $wgOut->addHTML(
134 Xml::fieldset( wfMsg( 'resetpass_header' ) ) .
135 Xml::openElement( 'form',
136 array(
137 'method' => 'post',
138 'action' => $self->getLocalUrl(),
139 'id' => 'mw-resetpass-form' ) ) . "\n" .
140 Xml::hidden( 'token', $wgUser->editToken() ) . "\n" .
141 Xml::hidden( 'wpName', $this->mUserName ) . "\n" .
142 Xml::hidden( 'returnto', $wgRequest->getVal( 'returnto' ) ) . "\n" .
143 wfMsgExt( 'resetpass_text', array( 'parse' ) ) . "\n" .
144 Xml::openElement( 'table', array( 'id' => 'mw-resetpass-table' ) ) . "\n" .
145 $this->pretty( array(
146 array( 'wpName', 'username', 'text', $this->mUserName ),
147 array( 'wpPassword', $oldpassMsg, 'password', $this->mOldpass ),
148 array( 'wpNewPassword', 'newpassword', 'password', null ),
149 array( 'wpRetype', 'retypenew', 'password', null ),
150 ) ) . "\n" .
151 $rememberMe .
152 "<tr>\n" .
153 "<td></td>\n" .
154 '<td class="mw-input">' .
155 Xml::submitButton( wfMsg( $submitMsg ) ) .
156 Xml::submitButton( wfMsg( 'resetpass-submit-cancel' ), array( 'name' => 'wpCancel' ) ) .
157 "</td>\n" .
158 "</tr>\n" .
159 Xml::closeElement( 'table' ) .
160 Xml::closeElement( 'form' ) .
161 Xml::closeElement( 'fieldset' ) . "\n"
162 );
163 }
164
165 function pretty( $fields ) {
166 $out = '';
167 foreach ( $fields as $list ) {
168 list( $name, $label, $type, $value ) = $list;
169 if( $type == 'text' ) {
170 $field = htmlspecialchars( $value );
171 } else {
172 $attribs = array( 'id' => $name );
173 if ( $name == 'wpNewPassword' || $name == 'wpRetype' ) {
174 $attribs = array_merge( $attribs,
175 User::passwordChangeInputAttribs() );
176 }
177 if ( $name == 'wpPassword' ) {
178 $attribs[] = 'autofocus';
179 }
180 $field = Html::input( $name, $value, $type, $attribs );
181 }
182 $out .= "<tr>\n";
183 $out .= "\t<td class='mw-label'>";
184 if ( $type != 'text' )
185 $out .= Xml::label( wfMsg( $label ), $name );
186 else
187 $out .= wfMsgHtml( $label );
188 $out .= "</td>\n";
189 $out .= "\t<td class='mw-input'>";
190 $out .= $field;
191 $out .= "</td>\n";
192 $out .= "</tr>";
193 }
194 return $out;
195 }
196
197 /**
198 * @throws PasswordError when cannot set the new password because requirements not met.
199 */
200 protected function attemptReset( $newpass, $retype ) {
201 $user = User::newFromName( $this->mUserName );
202 if( !$user || $user->isAnon() ) {
203 throw new PasswordError( 'no such user' );
204 }
205
206 if( $newpass !== $retype ) {
207 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
208 throw new PasswordError( wfMsg( 'badretype' ) );
209 }
210
211 if( !$user->checkTemporaryPassword($this->mOldpass) && !$user->checkPassword($this->mOldpass) ) {
212 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
213 throw new PasswordError( wfMsg( 'resetpass-wrong-oldpass' ) );
214 }
215
216 try {
217 $user->setPassword( $this->mNewpass );
218 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
219 $this->mNewpass = $this->mOldpass = $this->mRetypePass = '';
220 } catch( PasswordError $e ) {
221 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
222 throw new PasswordError( $e->getMessage() );
223 return;
224 }
225
226 $user->setCookies();
227 $user->saveSettings();
228 }
229 }