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();
71 $this->mPassword
= $request->getVal( 'wpPassword' );
72 $this->mNewEmail
= $request->getVal( 'wpNewEmail' );
74 if ( $request->wasPosted()
75 && $user->matchEditToken( $request->getVal( 'token' ) ) )
77 $info = $this->attemptChange( $user, $this->mPassword
, $this->mNewEmail
);
78 if ( $info === true ) {
80 } elseif ( $info === 'eauth' ) {
81 # Notify user that a confirmation email has been sent...
82 $out->wrapWikiMsg( "<div class='error' style='clear: both;'>\n$1\n</div>",
83 'eauthentsent', $user->getName() );
84 $this->doReturnTo( 'soft' ); // just show the link to go back
92 protected function doReturnTo( $type = 'hard' ) {
93 $titleObj = Title
::newFromText( $this->getRequest()->getVal( 'returnto' ) );
94 if ( !$titleObj instanceof Title
) {
95 $titleObj = Title
::newMainPage();
97 if ( $type == 'hard' ) {
98 $this->getOutput()->redirect( $titleObj->getFullURL() );
100 $this->getOutput()->addReturnTo( $titleObj );
104 protected function error( $msg ) {
105 $this->getOutput()->wrapWikiMsg( "<p class='error'>\n$1\n</p>", $msg );
108 protected function showForm() {
109 $user = $this->getUser();
111 $oldEmailText = $user->getEmail()
113 : $this->msg( 'changeemail-none' )->text();
115 $this->getOutput()->addHTML(
116 Xml
::fieldset( $this->msg( 'changeemail-header' )->text() ) .
117 Xml
::openElement( 'form',
120 'action' => $this->getTitle()->getLocalUrl(),
121 'id' => 'mw-changeemail-form' ) ) . "\n" .
122 Html
::hidden( 'token', $user->getEditToken() ) . "\n" .
123 Html
::hidden( 'returnto', $this->getRequest()->getVal( 'returnto' ) ) . "\n" .
124 $this->msg( 'changeemail-text' )->parseAsBlock() . "\n" .
125 Xml
::openElement( 'table', array( 'id' => 'mw-changeemail-table' ) ) . "\n" .
126 $this->pretty( array(
127 array( 'wpName', 'username', 'text', $user->getName() ),
128 array( 'wpOldEmail', 'changeemail-oldemail', 'text', $oldEmailText ),
129 array( 'wpNewEmail', 'changeemail-newemail', 'input', $this->mNewEmail
),
130 array( 'wpPassword', 'yourpassword', 'password', $this->mPassword
),
134 '<td class="mw-input">' .
135 Xml
::submitButton( $this->msg( 'changeemail-submit' )->text() ) .
136 Xml
::submitButton( $this->msg( 'changeemail-cancel' )->text(), array( 'name' => 'wpCancel' ) ) .
139 Xml
::closeElement( 'table' ) .
140 Xml
::closeElement( 'form' ) .
141 Xml
::closeElement( 'fieldset' ) . "\n"
145 protected function pretty( $fields ) {
147 foreach ( $fields as $list ) {
148 list( $name, $label, $type, $value ) = $list;
149 if( $type == 'text' ) {
150 $field = htmlspecialchars( $value );
152 $attribs = array( 'id' => $name );
153 if ( $name == 'wpPassword' ) {
154 $attribs[] = 'autofocus';
156 $field = Html
::input( $name, $value, $type, $attribs );
159 $out .= "\t<td class='mw-label'>";
160 if ( $type != 'text' ) {
161 $out .= Xml
::label( $this->msg( $label )->text(), $name );
163 $out .= $this->msg( $label )->escaped();
166 $out .= "\t<td class='mw-input'>";
175 * @return bool|string true or string on success, false on failure
177 protected function attemptChange( User
$user, $pass, $newaddr ) {
178 if ( $newaddr != '' && !Sanitizer
::validateEmail( $newaddr ) ) {
179 $this->error( 'invalidemailaddress' );
183 $throttleCount = LoginForm
::incLoginThrottle( $user->getName() );
184 if ( $throttleCount === true ) {
185 $this->error( 'login-throttled' );
189 if ( !$user->checkTemporaryPassword( $pass ) && !$user->checkPassword( $pass ) ) {
190 $this->error( 'wrongpassword' );
194 if ( $throttleCount ) {
195 LoginForm
::clearLoginThrottle( $user->getName() );
198 list( $status, $info ) = Preferences
::trySetUserEmail( $user, $newaddr );
199 if ( $status !== true ) {
200 if ( $status instanceof Status
) {
201 $this->getOutput()->addHTML(
202 '<p class="error">' .
203 $this->getOutput()->parseInline( $status->getWikiText( $info ) ) .
209 $user->saveSettings();
210 return $info ?
$info : true;