testPngNativetZtxt requires zlib extension
[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 $this->getOutput()->addWikiMsg( 'resetpass_success' );
77 if( !$user->isLoggedIn() ) {
78 LoginForm::setLoginToken();
79 $token = LoginForm::getLoginToken();
80 $data = array(
81 'action' => 'submitlogin',
82 'wpName' => $this->mUserName,
83 'wpDomain' => $this->mDomain,
84 'wpLoginToken' => $token,
85 'wpPassword' => $this->mNewpass,
86 'returnto' => $request->getVal( 'returnto' ),
87 );
88 if( $request->getCheck( 'wpRemember' ) ) {
89 $data['wpRemember'] = 1;
90 }
91 $login = new LoginForm( new FauxRequest( $data, true ) );
92 $login->setContext( $this->getContext() );
93 $login->execute( null );
94 }
95 $this->doReturnTo();
96 } catch( PasswordError $e ) {
97 $this->error( $e->getMessage() );
98 }
99 }
100 $this->showForm();
101 }
102
103 function doReturnTo() {
104 $titleObj = Title::newFromText( $this->getRequest()->getVal( 'returnto' ) );
105 if ( !$titleObj instanceof Title ) {
106 $titleObj = Title::newMainPage();
107 }
108 $this->getOutput()->redirect( $titleObj->getFullURL() );
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 $this->getOutput()->addHTML(
152 Xml::fieldset( $this->msg( 'resetpass_header' )->text() ) .
153 Xml::openElement( 'form',
154 array(
155 'method' => 'post',
156 'action' => $this->getTitle()->getLocalUrl(),
157 'id' => 'mw-resetpass-form' ) ) . "\n" .
158 Html::hidden( 'token', $user->getEditToken() ) . "\n" .
159 Html::hidden( 'wpName', $this->mUserName ) . "\n" .
160 Html::hidden( 'wpDomain', $this->mDomain ) . "\n" .
161 Html::hidden( 'returnto', $this->getRequest()->getVal( 'returnto' ) ) . "\n" .
162 $this->msg( 'resetpass_text' )->parseAsBlock() . "\n" .
163 Xml::openElement( 'table', array( 'id' => 'mw-resetpass-table' ) ) . "\n" .
164 $this->pretty( $prettyFields ) . "\n" .
165 $rememberMe .
166 "<tr>\n" .
167 "<td></td>\n" .
168 '<td class="mw-input">' .
169 Xml::submitButton( $this->msg( $submitMsg )->text() ) .
170 Xml::submitButton( $this->msg( 'resetpass-submit-cancel' )->text(), array( 'name' => 'wpCancel' ) ) .
171 "</td>\n" .
172 "</tr>\n" .
173 Xml::closeElement( 'table' ) .
174 Xml::closeElement( 'form' ) .
175 Xml::closeElement( 'fieldset' ) . "\n"
176 );
177 }
178
179 /**
180 * @param $fields array
181 * @return string
182 */
183 function pretty( $fields ) {
184 $out = '';
185 foreach ( $fields as $list ) {
186 list( $name, $label, $type, $value ) = $list;
187 if( $type == 'text' ) {
188 $field = htmlspecialchars( $value );
189 } else {
190 $attribs = array( 'id' => $name );
191 if ( $name == 'wpNewPassword' || $name == 'wpRetype' ) {
192 $attribs = array_merge( $attribs,
193 User::passwordChangeInputAttribs() );
194 }
195 if ( $name == 'wpPassword' ) {
196 $attribs[] = 'autofocus';
197 }
198 $field = Html::input( $name, $value, $type, $attribs );
199 }
200 $out .= "<tr>\n";
201 $out .= "\t<td class='mw-label'>";
202 if ( $type != 'text' )
203 $out .= Xml::label( $this->msg( $label )->text(), $name );
204 else
205 $out .= $this->msg( $label )->escaped();
206 $out .= "</td>\n";
207 $out .= "\t<td class='mw-input'>";
208 $out .= $field;
209 $out .= "</td>\n";
210 $out .= "</tr>";
211 }
212 return $out;
213 }
214
215 /**
216 * @throws PasswordError when cannot set the new password because requirements not met.
217 */
218 protected function attemptReset( $newpass, $retype ) {
219 $user = User::newFromName( $this->mUserName );
220 if( !$user || $user->isAnon() ) {
221 throw new PasswordError( $this->msg( 'nosuchusershort', $this->mUserName )->text() );
222 }
223
224 if( $newpass !== $retype ) {
225 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
226 throw new PasswordError( $this->msg( 'badretype' )->text() );
227 }
228
229 $throttleCount = LoginForm::incLoginThrottle( $this->mUserName );
230 if ( $throttleCount === true ) {
231 throw new PasswordError( $this->msg( 'login-throttled' )->text() );
232 }
233
234 if( !$user->checkTemporaryPassword($this->mOldpass) && !$user->checkPassword($this->mOldpass) ) {
235 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
236 throw new PasswordError( $this->msg( 'resetpass-wrong-oldpass' )->text() );
237 }
238
239 // Please reset throttle for successful logins, thanks!
240 if ( $throttleCount ) {
241 LoginForm::clearLoginThrottle( $this->mUserName );
242 }
243
244 try {
245 $user->setPassword( $this->mNewpass );
246 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
247 $this->mNewpass = $this->mOldpass = $this->mRetype = '';
248 } catch( PasswordError $e ) {
249 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
250 throw new PasswordError( $e->getMessage() );
251 }
252
253 $user->setCookies();
254 $user->saveSettings();
255 }
256 }