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