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
21 * @ingroup SpecialPage
25 * Let users change their email address.
27 * @ingroup SpecialPage
29 class SpecialChangeEmail
extends UnlistedSpecialPage
{
30 public function __construct() {
31 parent
::__construct( 'ChangeEmail' );
36 return $wgAuth->allowPropChange( 'emailaddress' );
40 * Main execution point
42 function execute( $par ) {
45 $this->checkReadOnly();
48 $this->outputHeader();
50 if ( !$wgAuth->allowPropChange( 'emailaddress' ) ) {
51 $this->error( 'cannotchangeemail' );
55 $user = $this->getUser();
56 $request = $this->getRequest();
58 if ( !$request->wasPosted() && !$user->isLoggedIn() ) {
59 $this->error( 'changeemail-no-info' );
63 if ( $request->wasPosted() && $request->getBool( 'wpCancel' ) ) {
68 $out = $this->getOutput();
69 $out->disallowUserJs();
70 $out->addModules( 'mediawiki.special.changeemail' );
72 $this->mPassword
= $request->getVal( 'wpPassword' );
73 $this->mNewEmail
= $request->getVal( 'wpNewEmail' );
75 if ( $request->wasPosted()
76 && $user->matchEditToken( $request->getVal( 'token' ) ) )
78 $info = $this->attemptChange( $user, $this->mPassword
, $this->mNewEmail
);
79 if ( $info === true ) {
81 } elseif ( $info === 'eauth' ) {
82 # Notify user that a confirmation email has been sent...
83 $out->wrapWikiMsg( "<div class='error' style='clear: both;'>\n$1\n</div>",
84 'eauthentsent', $user->getName() );
85 $this->doReturnTo( 'soft' ); // just show the link to go back
93 protected function doReturnTo( $type = 'hard' ) {
94 $titleObj = Title
::newFromText( $this->getRequest()->getVal( 'returnto' ) );
95 if ( !$titleObj instanceof Title
) {
96 $titleObj = Title
::newMainPage();
98 if ( $type == 'hard' ) {
99 $this->getOutput()->redirect( $titleObj->getFullURL() );
101 $this->getOutput()->addReturnTo( $titleObj );
105 protected function error( $msg ) {
106 $this->getOutput()->wrapWikiMsg( "<p class='error'>\n$1\n</p>", $msg );
109 protected function showForm() {
110 $user = $this->getUser();
112 $oldEmailText = $user->getEmail()
114 : $this->msg( 'changeemail-none' )->text();
116 $this->getOutput()->addHTML(
117 Xml
::fieldset( $this->msg( 'changeemail-header' )->text() ) .
118 Xml
::openElement( 'form',
121 'action' => $this->getTitle()->getLocalUrl(),
122 'id' => 'mw-changeemail-form' ) ) . "\n" .
123 Html
::hidden( 'token', $user->getEditToken() ) . "\n" .
124 Html
::hidden( 'returnto', $this->getRequest()->getVal( 'returnto' ) ) . "\n" .
125 $this->msg( 'changeemail-text' )->parseAsBlock() . "\n" .
126 Xml
::openElement( 'table', array( 'id' => 'mw-changeemail-table' ) ) . "\n" .
127 $this->pretty( array(
128 array( 'wpName', 'username', 'text', $user->getName() ),
129 array( 'wpOldEmail', 'changeemail-oldemail', 'text', $oldEmailText ),
130 array( 'wpNewEmail', 'changeemail-newemail', 'input', $this->mNewEmail
),
131 array( 'wpPassword', 'yourpassword', 'password', $this->mPassword
),
135 '<td class="mw-input">' .
136 Xml
::submitButton( $this->msg( 'changeemail-submit' )->text() ) .
137 Xml
::submitButton( $this->msg( 'changeemail-cancel' )->text(), array( 'name' => 'wpCancel' ) ) .
140 Xml
::closeElement( 'table' ) .
141 Xml
::closeElement( 'form' ) .
142 Xml
::closeElement( 'fieldset' ) . "\n"
146 protected function pretty( $fields ) {
148 foreach ( $fields as $list ) {
149 list( $name, $label, $type, $value ) = $list;
150 if( $type == 'text' ) {
151 $field = htmlspecialchars( $value );
153 $attribs = array( 'id' => $name );
154 if ( $name == 'wpPassword' ) {
155 $attribs[] = 'autofocus';
157 $field = Html
::input( $name, $value, $type, $attribs );
160 $out .= "\t<td class='mw-label'>";
161 if ( $type != 'text' ) {
162 $out .= Xml
::label( $this->msg( $label )->text(), $name );
164 $out .= $this->msg( $label )->escaped();
167 $out .= "\t<td class='mw-input'>";
176 * @return bool|string true or string on success, false on failure
178 protected function attemptChange( User
$user, $pass, $newaddr ) {
179 if ( $newaddr != '' && !Sanitizer
::validateEmail( $newaddr ) ) {
180 $this->error( 'invalidemailaddress' );
184 $throttleCount = LoginForm
::incLoginThrottle( $user->getName() );
185 if ( $throttleCount === true ) {
186 $this->error( 'login-throttled' );
190 if ( !$user->checkTemporaryPassword( $pass ) && !$user->checkPassword( $pass ) ) {
191 $this->error( 'wrongpassword' );
195 if ( $throttleCount ) {
196 LoginForm
::clearLoginThrottle( $user->getName() );
199 list( $status, $info ) = Preferences
::trySetUserEmail( $user, $newaddr );
200 if ( $status !== true ) {
201 if ( $status instanceof Status
) {
202 $this->getOutput()->addHTML(
203 '<p class="error">' .
204 $this->getOutput()->parseInline( $status->getWikiText( $info ) ) .
210 $user->saveSettings();
211 return $info ?
$info : true;