Revert r55842 "Can now pass in element attributes other than just id to buildTable...
[lhc/web/wiklou.git] / includes / specials / SpecialResetpass.php
1 <?php
2 /**
3 * @file
4 * @ingroup SpecialPage
5 */
6
7 /**
8 * Let users recover their password.
9 * @ingroup SpecialPage
10 */
11 class SpecialResetpass extends SpecialPage {
12 public function __construct() {
13 parent::__construct( 'Resetpass' );
14 }
15
16 /**
17 * Main execution point
18 */
19 function execute( $par ) {
20 global $wgUser, $wgAuth, $wgOut, $wgRequest;
21
22 $this->mUserName = $wgRequest->getVal( 'wpName' );
23 $this->mOldpass = $wgRequest->getVal( 'wpPassword' );
24 $this->mNewpass = $wgRequest->getVal( 'wpNewPassword' );
25 $this->mRetype = $wgRequest->getVal( 'wpRetype' );
26
27 $this->setHeaders();
28 $this->outputHeader();
29
30 if( !$wgAuth->allowPasswordChange() ) {
31 $this->error( wfMsg( 'resetpass_forbidden' ) );
32 return;
33 }
34
35 if( !$wgRequest->wasPosted() && !$wgUser->isLoggedIn() ) {
36 $this->error( wfMsg( 'resetpass-no-info' ) );
37 return;
38 }
39
40 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getVal('token') ) ) {
41 try {
42 $this->attemptReset( $this->mNewpass, $this->mRetype );
43 $wgOut->addWikiMsg( 'resetpass_success' );
44 if( !$wgUser->isLoggedIn() ) {
45 $data = array(
46 'wpName' => $this->mUserName,
47 'wpPassword' => $this->mNewpass,
48 'returnto' => $wgRequest->getVal( 'returnto' ),
49 );
50 if( $wgRequest->getCheck( 'wpRemember' ) ) {
51 $data['wpRemember'] = 1;
52 }
53 $login = new LoginForm( new FauxRequest( $data, true ) );
54 $login->attemptLogin();
55 }
56 $titleObj = Title::newFromText( $wgRequest->getVal( 'returnto' ) );
57 if ( !$titleObj instanceof Title ) {
58 $titleObj = Title::newMainPage();
59 }
60 $wgOut->redirect( $titleObj->getFullURL() );
61 } catch( PasswordError $e ) {
62 $this->error( $e->getMessage() );
63 }
64 }
65 $this->showForm();
66 }
67
68 function error( $msg ) {
69 global $wgOut;
70 $wgOut->addHTML( Xml::element('p', array( 'class' => 'error' ), $msg ) );
71 }
72
73 function showForm() {
74 global $wgOut, $wgUser, $wgRequest;
75
76 $wgOut->disallowUserJs();
77
78 $self = SpecialPage::getTitleFor( 'Resetpass' );
79 if ( !$this->mUserName ) {
80 $this->mUserName = $wgUser->getName();
81 }
82 $rememberMe = '';
83 if ( !$wgUser->isLoggedIn() ) {
84 $rememberMe = '<tr>' .
85 '<td></td>' .
86 '<td class="mw-input">' .
87 Xml::checkLabel( wfMsg( 'remembermypassword' ),
88 'wpRemember', 'wpRemember',
89 $wgRequest->getCheck( 'wpRemember' ) ) .
90 '</td>' .
91 '</tr>';
92 $submitMsg = 'resetpass_submit';
93 $oldpassMsg = 'resetpass-temp-password';
94 } else {
95 $oldpassMsg = 'oldpassword';
96 $submitMsg = 'resetpass-submit-loggedin';
97 }
98 $wgOut->addHTML(
99 Xml::fieldset( wfMsg( 'resetpass_header' ) ) .
100 Xml::openElement( 'form',
101 array(
102 'method' => 'post',
103 'action' => $self->getLocalUrl(),
104 'id' => 'mw-resetpass-form' ) ) . "\n" .
105 Xml::hidden( 'token', $wgUser->editToken() ) . "\n" .
106 Xml::hidden( 'wpName', $this->mUserName ) . "\n" .
107 Xml::hidden( 'returnto', $wgRequest->getVal( 'returnto' ) ) . "\n" .
108 wfMsgExt( 'resetpass_text', array( 'parse' ) ) . "\n" .
109 Xml::openElement( 'table', array( 'id' => 'mw-resetpass-table' ) ) . "\n" .
110 $this->pretty( array(
111 array( 'wpName', 'username', 'text', $this->mUserName ),
112 array( 'wpPassword', $oldpassMsg, 'password', $this->mOldpass ),
113 array( 'wpNewPassword', 'newpassword', 'password', null ),
114 array( 'wpRetype', 'retypenew', 'password', null ),
115 ) ) . "\n" .
116 $rememberMe .
117 "<tr>\n" .
118 "<td></td>\n" .
119 '<td class="mw-input">' .
120 Xml::submitButton( wfMsg( $submitMsg ) ) .
121 "</td>\n" .
122 "</tr>\n" .
123 Xml::closeElement( 'table' ) .
124 Xml::closeElement( 'form' ) .
125 Xml::closeElement( 'fieldset' ) . "\n"
126 );
127 }
128
129 function pretty( $fields ) {
130 $out = '';
131 foreach ( $fields as $list ) {
132 list( $name, $label, $type, $value ) = $list;
133 if( $type == 'text' ) {
134 $field = htmlspecialchars( $value );
135 } else {
136 $attribs = array( 'id' => $name );
137 if ( $name == 'wpNewPassword' || $name == 'wpRetype' ) {
138 $attribs = array_merge( $attribs,
139 User::passwordChangeInputAttribs() );
140 }
141 if ( $name == 'wpPassword' ) {
142 $attribs[] = 'autofocus';
143 }
144 $field = Html::input( $name, $value, $type, $attribs );
145 }
146 $out .= "<tr>\n";
147 $out .= "\t<td class='mw-label'>";
148 if ( $type != 'text' )
149 $out .= Xml::label( wfMsg( $label ), $name );
150 else
151 $out .= wfMsgHtml( $label );
152 $out .= "</td>\n";
153 $out .= "\t<td class='mw-input'>";
154 $out .= $field;
155 $out .= "</td>\n";
156 $out .= "</tr>";
157 }
158 return $out;
159 }
160
161 /**
162 * @throws PasswordError when cannot set the new password because requirements not met.
163 */
164 protected function attemptReset( $newpass, $retype ) {
165 $user = User::newFromName( $this->mUserName );
166 if( !$user || $user->isAnon() ) {
167 throw new PasswordError( 'no such user' );
168 }
169
170 if( $newpass !== $retype ) {
171 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
172 throw new PasswordError( wfMsg( 'badretype' ) );
173 }
174
175 if( !$user->checkTemporaryPassword($this->mOldpass) && !$user->checkPassword($this->mOldpass) ) {
176 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
177 throw new PasswordError( wfMsg( 'resetpass-wrong-oldpass' ) );
178 }
179
180 try {
181 $user->setPassword( $this->mNewpass );
182 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
183 $this->mNewpass = $this->mOldpass = $this->mRetypePass = '';
184 } catch( PasswordError $e ) {
185 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
186 throw new PasswordError( $e->getMessage() );
187 return;
188 }
189
190 $user->setCookies();
191 $user->saveSettings();
192 }
193 }