Add a new searchmenu-new-nocreate message
[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 if ( wfReadOnly() ) {
23 $wgOut->readOnlyPage();
24 return;
25 }
26
27 $this->mUserName = $wgRequest->getVal( 'wpName' );
28 $this->mOldpass = $wgRequest->getVal( 'wpPassword' );
29 $this->mNewpass = $wgRequest->getVal( 'wpNewPassword' );
30 $this->mRetype = $wgRequest->getVal( 'wpRetype' );
31
32 $this->setHeaders();
33 $this->outputHeader();
34 $wgOut->disallowUserJs();
35
36 if( !$wgAuth->allowPasswordChange() ) {
37 $this->error( wfMsg( 'resetpass_forbidden' ) );
38 return;
39 }
40
41 if( !$wgRequest->wasPosted() && !$wgUser->isLoggedIn() ) {
42 $this->error( wfMsg( 'resetpass-no-info' ) );
43 return;
44 }
45
46 if( $wgRequest->wasPosted() && $wgRequest->getBool( 'wpCancel' ) ) {
47 $this->doReturnTo();
48 return;
49 }
50
51 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getVal('token') ) ) {
52 try {
53 $this->attemptReset( $this->mNewpass, $this->mRetype );
54 $wgOut->addWikiMsg( 'resetpass_success' );
55 if( !$wgUser->isLoggedIn() ) {
56 $data = array(
57 'action' => 'submitlogin',
58 'wpName' => $this->mUserName,
59 'wpPassword' => $this->mNewpass,
60 'returnto' => $wgRequest->getVal( 'returnto' ),
61 );
62 if( $wgRequest->getCheck( 'wpRemember' ) ) {
63 $data['wpRemember'] = 1;
64 }
65 $login = new LoginForm( new FauxRequest( $data, true ) );
66 $login->execute();
67 }
68 $this->doReturnTo();
69 } catch( PasswordError $e ) {
70 $this->error( $e->getMessage() );
71 }
72 }
73 $this->showForm();
74 }
75
76 function doReturnTo() {
77 global $wgRequest, $wgOut;
78 $titleObj = Title::newFromText( $wgRequest->getVal( 'returnto' ) );
79 if ( !$titleObj instanceof Title ) {
80 $titleObj = Title::newMainPage();
81 }
82 $wgOut->redirect( $titleObj->getFullURL() );
83 }
84
85 function error( $msg ) {
86 global $wgOut;
87 $wgOut->addHTML( Xml::element('p', array( 'class' => 'error' ), $msg ) );
88 }
89
90 function showForm() {
91 global $wgOut, $wgUser, $wgRequest;
92
93 $self = $this->getTitle();
94 if ( !$this->mUserName ) {
95 $this->mUserName = $wgUser->getName();
96 }
97 $rememberMe = '';
98 if ( !$wgUser->isLoggedIn() ) {
99 $rememberMe = '<tr>' .
100 '<td></td>' .
101 '<td class="mw-input">' .
102 Xml::checkLabel( wfMsg( 'remembermypassword' ),
103 'wpRemember', 'wpRemember',
104 $wgRequest->getCheck( 'wpRemember' ) ) .
105 '</td>' .
106 '</tr>';
107 $submitMsg = 'resetpass_submit';
108 $oldpassMsg = 'resetpass-temp-password';
109 } else {
110 $oldpassMsg = 'oldpassword';
111 $submitMsg = 'resetpass-submit-loggedin';
112 }
113 $wgOut->addHTML(
114 Xml::fieldset( wfMsg( 'resetpass_header' ) ) .
115 Xml::openElement( 'form',
116 array(
117 'method' => 'post',
118 'action' => $self->getLocalUrl(),
119 'id' => 'mw-resetpass-form' ) ) . "\n" .
120 Xml::hidden( 'token', $wgUser->editToken() ) . "\n" .
121 Xml::hidden( 'wpName', $this->mUserName ) . "\n" .
122 Xml::hidden( 'returnto', $wgRequest->getVal( 'returnto' ) ) . "\n" .
123 wfMsgExt( 'resetpass_text', array( 'parse' ) ) . "\n" .
124 Xml::openElement( 'table', array( 'id' => 'mw-resetpass-table' ) ) . "\n" .
125 $this->pretty( array(
126 array( 'wpName', 'username', 'text', $this->mUserName ),
127 array( 'wpPassword', $oldpassMsg, 'password', $this->mOldpass ),
128 array( 'wpNewPassword', 'newpassword', 'password', null ),
129 array( 'wpRetype', 'retypenew', 'password', null ),
130 ) ) . "\n" .
131 $rememberMe .
132 "<tr>\n" .
133 "<td></td>\n" .
134 '<td class="mw-input">' .
135 Xml::submitButton( wfMsg( $submitMsg ) ) .
136 Xml::submitButton( wfMsg( 'resetpass-submit-cancel' ), array( 'name' => 'wpCancel' ) ) .
137 "</td>\n" .
138 "</tr>\n" .
139 Xml::closeElement( 'table' ) .
140 Xml::closeElement( 'form' ) .
141 Xml::closeElement( 'fieldset' ) . "\n"
142 );
143 }
144
145 function pretty( $fields ) {
146 $out = '';
147 foreach ( $fields as $list ) {
148 list( $name, $label, $type, $value ) = $list;
149 if( $type == 'text' ) {
150 $field = htmlspecialchars( $value );
151 } else {
152 $attribs = array( 'id' => $name );
153 if ( $name == 'wpNewPassword' || $name == 'wpRetype' ) {
154 $attribs = array_merge( $attribs,
155 User::passwordChangeInputAttribs() );
156 }
157 if ( $name == 'wpPassword' ) {
158 $attribs[] = 'autofocus';
159 }
160 $field = Html::input( $name, $value, $type, $attribs );
161 }
162 $out .= "<tr>\n";
163 $out .= "\t<td class='mw-label'>";
164 if ( $type != 'text' )
165 $out .= Xml::label( wfMsg( $label ), $name );
166 else
167 $out .= wfMsgHtml( $label );
168 $out .= "</td>\n";
169 $out .= "\t<td class='mw-input'>";
170 $out .= $field;
171 $out .= "</td>\n";
172 $out .= "</tr>";
173 }
174 return $out;
175 }
176
177 /**
178 * @throws PasswordError when cannot set the new password because requirements not met.
179 */
180 protected function attemptReset( $newpass, $retype ) {
181 $user = User::newFromName( $this->mUserName );
182 if( !$user || $user->isAnon() ) {
183 throw new PasswordError( 'no such user' );
184 }
185
186 if( $newpass !== $retype ) {
187 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
188 throw new PasswordError( wfMsg( 'badretype' ) );
189 }
190
191 if( !$user->checkTemporaryPassword($this->mOldpass) && !$user->checkPassword($this->mOldpass) ) {
192 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
193 throw new PasswordError( wfMsg( 'resetpass-wrong-oldpass' ) );
194 }
195
196 try {
197 $user->setPassword( $this->mNewpass );
198 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
199 $this->mNewpass = $this->mOldpass = $this->mRetypePass = '';
200 } catch( PasswordError $e ) {
201 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
202 throw new PasswordError( $e->getMessage() );
203 return;
204 }
205
206 $user->setCookies();
207 $user->saveSettings();
208 }
209 }