* Made IndexPager extend ContextSource
[mediawiki.git] / includes / specials / SpecialChangeEmail.php
blob71e7cbdb049d2ecc4e34bc0b1004335ba1735978
1 <?php
2 /**
3 * Implements Special:ChangeEmail
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.
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.
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * Let users change their email address.
27 * @ingroup SpecialPage
29 class SpecialChangeEmail extends UnlistedSpecialPage {
30 public function __construct() {
31 parent::__construct( 'ChangeEmail' );
34 /**
35 * Main execution point
37 function execute( $par ) {
38 if ( wfReadOnly() ) {
39 throw new ReadOnlyError;
42 $request = $this->getRequest();
43 $this->mPassword = $request->getVal( 'wpPassword' );
44 $this->mNewEmail = $request->getVal( 'wpNewEmail' );
46 $this->setHeaders();
47 $this->outputHeader();
49 $out = $this->getOutput();
50 $out->disallowUserJs();
52 $user = $this->getUser();
54 if ( !$request->wasPosted() && !$user->isLoggedIn() ) {
55 $this->error( wfMsg( 'changeemail-no-info' ) );
56 return;
59 if ( $request->wasPosted() && $request->getBool( 'wpCancel' ) ) {
60 $this->doReturnTo();
61 return;
64 if ( $request->wasPosted()
65 && $user->matchEditToken( $request->getVal( 'token' ) ) )
67 $info = $this->attemptChange( $user, $this->mPassword, $this->mNewEmail );
68 if ( $info === true ) {
69 $this->doReturnTo();
70 } elseif ( $info === 'eauth' ) {
71 # Notify user that a confirmation email has been sent...
72 $out->wrapWikiMsg( "<div class='error' style='clear: both;'>\n$1\n</div>",
73 'eauthentsent', $user->getName() );
74 $this->doReturnTo( 'soft' ); // just show the link to go back
75 return; // skip form
79 $this->showForm();
82 protected function doReturnTo( $type = 'hard' ) {
83 $titleObj = Title::newFromText( $this->getRequest()->getVal( 'returnto' ) );
84 if ( !$titleObj instanceof Title ) {
85 $titleObj = Title::newMainPage();
87 if ( $type == 'hard' ) {
88 $this->getOutput()->redirect( $titleObj->getFullURL() );
89 } else {
90 $this->getOutput()->addReturnTo( $titleObj );
94 protected function error( $msg ) {
95 $this->getOutput()->addHTML( Xml::element('p', array( 'class' => 'error' ), $msg ) );
98 protected function showForm() {
99 $user = $this->getUser();
101 $oldEmailText = $user->getEmail()
102 ? $user->getEmail()
103 : wfMsg( 'changeemail-none' );
105 $this->getOutput()->addHTML(
106 Xml::fieldset( wfMsg( 'changeemail-header' ) ) .
107 Xml::openElement( 'form',
108 array(
109 'method' => 'post',
110 'action' => $this->getTitle()->getLocalUrl(),
111 'id' => 'mw-changeemail-form' ) ) . "\n" .
112 Html::hidden( 'token', $user->editToken() ) . "\n" .
113 Html::hidden( 'returnto', $this->getRequest()->getVal( 'returnto' ) ) . "\n" .
114 wfMsgExt( 'changeemail-text', array( 'parse' ) ) . "\n" .
115 Xml::openElement( 'table', array( 'id' => 'mw-changeemail-table' ) ) . "\n" .
116 $this->pretty( array(
117 array( 'wpName', 'username', 'text', $user->getName() ),
118 array( 'wpOldEmail', 'changeemail-oldemail', 'text', $oldEmailText ),
119 array( 'wpNewEmail', 'changeemail-newemail', 'input', $this->mNewEmail ),
120 array( 'wpPassword', 'yourpassword', 'password', $this->mPassword ),
121 ) ) . "\n" .
122 "<tr>\n" .
123 "<td></td>\n" .
124 '<td class="mw-input">' .
125 Xml::submitButton( wfMsg( 'changeemail-submit' ) ) .
126 Xml::submitButton( wfMsg( 'changeemail-cancel' ), array( 'name' => 'wpCancel' ) ) .
127 "</td>\n" .
128 "</tr>\n" .
129 Xml::closeElement( 'table' ) .
130 Xml::closeElement( 'form' ) .
131 Xml::closeElement( 'fieldset' ) . "\n"
135 protected function pretty( $fields ) {
136 $out = '';
137 foreach ( $fields as $list ) {
138 list( $name, $label, $type, $value ) = $list;
139 if( $type == 'text' ) {
140 $field = htmlspecialchars( $value );
141 } else {
142 $attribs = array( 'id' => $name );
143 if ( $name == 'wpPassword' ) {
144 $attribs[] = 'autofocus';
146 $field = Html::input( $name, $value, $type, $attribs );
148 $out .= "<tr>\n";
149 $out .= "\t<td class='mw-label'>";
150 if ( $type != 'text' ) {
151 $out .= Xml::label( wfMsg( $label ), $name );
152 } else {
153 $out .= wfMsgHtml( $label );
155 $out .= "</td>\n";
156 $out .= "\t<td class='mw-input'>";
157 $out .= $field;
158 $out .= "</td>\n";
159 $out .= "</tr>";
161 return $out;
165 * @return bool|string true or string on success, false on failure
167 protected function attemptChange( User $user, $pass, $newaddr ) {
168 if ( $newaddr != '' && !Sanitizer::validateEmail( $newaddr ) ) {
169 $this->error( wfMsgExt( 'invalidemailaddress', 'parseinline' ) );
170 return false;
173 $throttleCount = LoginForm::incLoginThrottle( $user->getName() );
174 if ( $throttleCount === true ) {
175 $this->error( wfMsgHtml( 'login-throttled' ) );
176 return false;
179 if ( !$user->checkTemporaryPassword( $pass ) && !$user->checkPassword( $pass ) ) {
180 $this->error( wfMsgHtml( 'wrongpassword' ) );
181 return false;
184 if ( $throttleCount ) {
185 LoginForm::clearLoginThrottle( $user->getName() );
188 list( $status, $info ) = Preferences::trySetUserEmail( $user, $newaddr );
189 if ( $status !== true ) {
190 if ( $status instanceof Status ) {
191 $this->getOutput()->addHTML(
192 '<p class="error">' .
193 $this->getOutput()->parseInline( $status->getWikiText( $info ) ) .
194 '</p>' );
196 return false;
199 $user->saveSettings();
200 return $info ? $info : true;