Localisation updates for core messages from translatewiki.net (2009-02-20 19:30 UTC)
[mediawiki.git] / includes / specials / SpecialResetpass.php
blob49e2530d4bf32024cf885d623778131494ab0bbd
1 <?php
2 /**
3 * @file
4 * @ingroup SpecialPage
5 */
7 /**
8 * Let users recover their password.
9 * @ingroup SpecialPage
11 class SpecialResetpass extends SpecialPage {
13 private $mSelfChange = true; // Usually, but sometimes not :)
15 public function __construct() {
16 parent::__construct( 'Resetpass' );
19 /**
20 * Main execution point
22 function execute( $par ) {
23 global $wgUser, $wgAuth, $wgOut, $wgRequest;
25 $this->mUserName = $wgRequest->getVal( 'wpName', $par );
26 $this->mOldpass = $wgRequest->getVal( 'wpPassword' );
27 $this->mNewpass = $wgRequest->getVal( 'wpNewPassword' );
28 $this->mRetype = $wgRequest->getVal( 'wpRetype' );
30 $this->setHeaders();
31 $this->outputHeader();
33 if( !$wgAuth->allowPasswordChange() ) {
34 $this->error( wfMsg( 'resetpass_forbidden' ) );
35 return;
38 // Default to our own username when not given one
39 if ( !$this->mUserName ) {
40 $this->mUserName = $wgUser->getName();
43 // Are we changing our own?
44 if ( $wgUser->getName() != $this->mUserName ) {
45 $this->mSelfChange = false; // We're changing someone else
48 if( !$wgRequest->wasPosted() && !$wgUser->isLoggedIn() ) {
49 $this->error( wfMsg( 'resetpass-no-info' ) );
50 return;
53 if ( !$this->mSelfChange && !$wgUser->isAllowed( 'reset-passwords' ) ) {
54 $this->error( wfMsg( 'resetpass-no-others' ) );
55 return;
58 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getVal('token') ) ) {
59 try {
60 $this->attemptReset( $this->mNewpass, $this->mRetype );
61 $wgOut->addWikiMsg( 'resetpass_success' );
62 // Only attempt this login session if we're changing our own password
63 if( $this->mSelfChange && !$wgUser->isLoggedIn() ) {
64 $data = array(
65 'action' => 'submitlogin',
66 'wpName' => $this->mUserName,
67 'wpPassword' => $this->mNewpass,
68 'returnto' => $wgRequest->getVal( 'returnto' ),
70 if( $wgRequest->getCheck( 'wpRemember' ) ) {
71 $data['wpRemember'] = 1;
73 $login = new LoginForm( new FauxRequest( $data, true ) );
74 $login->execute();
76 $titleObj = Title::newFromText( $wgRequest->getVal( 'returnto' ) );
77 if ( !$titleObj instanceof Title ) {
78 $titleObj = Title::newMainPage();
80 $wgOut->redirect( $titleObj->getFullURL() );
81 } catch( PasswordError $e ) {
82 $this->error( $e->getMessage() );
85 $this->showForm();
88 function error( $msg ) {
89 global $wgOut;
90 $wgOut->addHTML( Xml::element('p', array( 'class' => 'error' ), $msg ) );
93 function showForm() {
94 global $wgOut, $wgUser, $wgRequest;
96 $wgOut->disallowUserJs();
98 $self = SpecialPage::getTitleFor( 'Resetpass' );
100 $rememberMe = '';
101 if ( !$wgUser->isLoggedIn() ) {
102 $rememberMe = '<tr>' .
103 '<td></td>' .
104 '<td class="mw-input">' .
105 Xml::checkLabel( wfMsg( 'remembermypassword' ),
106 'wpRemember', 'wpRemember',
107 $wgRequest->getCheck( 'wpRemember' ) ) .
108 '</td>' .
109 '</tr>';
110 $submitMsg = 'resetpass_submit';
111 $oldpassMsg = 'resetpass-temp-password';
112 } else {
113 $oldpassMsg = 'oldpassword';
114 $submitMsg = 'resetpass-submit-loggedin';
116 $wgOut->addHTML(
117 Xml::fieldset( wfMsg( 'resetpass_header' ) ) .
118 Xml::openElement( 'form',
119 array(
120 'method' => 'post',
121 'action' => $self->getLocalUrl(),
122 'id' => 'mw-resetpass-form' ) ) .
123 Xml::hidden( 'token', $wgUser->editToken() ) .
124 Xml::hidden( 'returnto', $wgRequest->getVal( 'returnto' ) ) .
125 wfMsgExt( 'resetpass_text', array( 'parse' ) ) .
126 Xml::openElement( 'table', array( 'id' => 'mw-resetpass-table' ) ) .
127 $this->pretty( array(
128 array( 'wpName', 'username', 'text', $this->mUserName, $wgUser->isAllowed( 'reset-passwords' ) ),
129 array( 'wpPassword', $oldpassMsg, 'password', $this->mOldpass, $this->mSelfChange ),
130 array( 'wpNewPassword', 'newpassword', 'password', '', true ),
131 array( 'wpRetype', 'retypenew', 'password', '', true ),
132 ) ) .
133 $rememberMe .
134 '<tr>' .
135 '<td></td>' .
136 '<td class="mw-input">' .
137 Xml::submitButton( wfMsg( $submitMsg ) ) .
138 '</td>' .
139 '</tr>' .
140 Xml::closeElement( 'table' ) .
141 Xml::closeElement( 'form' ) .
142 Xml::closeElement( 'fieldset' )
146 function pretty( $fields ) {
147 $out = '';
148 foreach( $fields as $list ) {
149 list( $name, $label, $type, $value, $enabled ) = $list;
150 $params = array( 'id' => $name, 'type' => $type );
151 if ( !$enabled )
152 $params['disabled'] = 'disabled';
153 $field = Xml::input( $name, 20, $value, $params );
154 $out .= '<tr>';
155 $out .= '<td class="mw-label">';
156 $out .= Xml::label( wfMsg( $label ), $name );
157 $out .= '</td>';
158 $out .= '<td class="mw-input">';
159 $out .= $field;
160 $out .= '</td>';
161 $out .= '</tr>';
163 return $out;
167 * @throws PasswordError when cannot set the new password because requirements not met.
169 protected function attemptReset( $newpass, $retype ) {
170 $user = User::newFromName( $this->mUserName );
171 if( !$user || $user->isAnon() ) {
172 throw new PasswordError( 'no such user' );
175 if( $newpass !== $retype ) {
176 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
177 throw new PasswordError( wfMsg( 'badretype' ) );
180 if ( $this->mSelfChange ) {
181 if( !$user->checkTemporaryPassword($this->mOldpass) && !$user->checkPassword($this->mOldpass) ) {
182 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
183 throw new PasswordError( wfMsg( 'resetpass-wrong-oldpass' ) );
187 try {
188 $user->setPassword( $this->mNewpass );
189 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
190 $this->mNewpass = $this->mOldpass = $this->mRetypePass = '';
191 } catch( PasswordError $e ) {
192 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
193 throw new PasswordError( $e->getMessage() );
194 return;
197 $user->setCookies();
198 $user->saveSettings();