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' );
35 * Main execution point
37 function execute( $par ) {
40 $out = $this->getOutput();
46 $user = $this->getUser();
48 $this->mPassword
= $wgRequest->getVal( 'wpPassword' );
49 $this->mNewEmail
= $wgRequest->getVal( 'wpNewEmail' );
52 $this->outputHeader();
53 $out->disallowUserJs();
55 if ( !$wgRequest->wasPosted() && !$user->isLoggedIn() ) {
56 $this->error( wfMsg( 'changeemail-no-info' ) );
60 if ( $wgRequest->wasPosted() && $wgRequest->getBool( 'wpCancel' ) ) {
65 if ( $wgRequest->wasPosted()
66 && $user->matchEditToken( $wgRequest->getVal( 'token' ) ) )
68 $info = $this->attemptChange( $user, $this->mPassword
, $this->mNewEmail
);
69 if ( $info === true ) {
71 } elseif ( $info === 'eauth' ) {
72 # Notify user that a confirmation email has been sent...
73 $out->wrapWikiMsg( "<div class='error' style='clear: both;'>\n$1\n</div>",
74 'eauthentsent', $user->getName() );
75 $this->doReturnTo( 'soft' ); // just show the link to go back
83 protected function doReturnTo( $type = 'hard' ) {
85 $titleObj = Title
::newFromText( $wgRequest->getVal( 'returnto' ) );
86 if ( !$titleObj instanceof Title
) {
87 $titleObj = Title
::newMainPage();
89 if ( $type == 'hard' ) {
90 $this->getOutput()->redirect( $titleObj->getFullURL() );
92 $this->getOutput()->addReturnTo( $titleObj );
96 protected function error( $msg ) {
97 $this->getOutput()->addHTML( Xml
::element('p', array( 'class' => 'error' ), $msg ) );
100 protected function showForm() {
103 $user = $this->getUser();
105 $oldEmailText = $user->getEmail()
107 : wfMsg( 'changeemail-none' );
109 $this->getOutput()->addHTML(
110 Xml
::fieldset( wfMsg( 'changeemail-header' ) ) .
111 Xml
::openElement( 'form',
114 'action' => $this->getTitle()->getLocalUrl(),
115 'id' => 'mw-changeemail-form' ) ) . "\n" .
116 Html
::hidden( 'token', $user->editToken() ) . "\n" .
117 Html
::hidden( 'returnto', $wgRequest->getVal( 'returnto' ) ) . "\n" .
118 wfMsgExt( 'changeemail-text', array( 'parse' ) ) . "\n" .
119 Xml
::openElement( 'table', array( 'id' => 'mw-changeemail-table' ) ) . "\n" .
120 $this->pretty( array(
121 array( 'wpName', 'username', 'text', $user->getName() ),
122 array( 'wpOldEmail', 'changeemail-oldemail', 'text', $oldEmailText ),
123 array( 'wpNewEmail', 'changeemail-newemail', 'input', $this->mNewEmail
),
124 array( 'wpPassword', 'yourpassword', 'password', $this->mPassword
),
128 '<td class="mw-input">' .
129 Xml
::submitButton( wfMsg( 'changeemail-submit' ) ) .
130 Xml
::submitButton( wfMsg( 'changeemail-cancel' ), array( 'name' => 'wpCancel' ) ) .
133 Xml
::closeElement( 'table' ) .
134 Xml
::closeElement( 'form' ) .
135 Xml
::closeElement( 'fieldset' ) . "\n"
139 protected function pretty( $fields ) {
141 foreach ( $fields as $list ) {
142 list( $name, $label, $type, $value ) = $list;
143 if( $type == 'text' ) {
144 $field = htmlspecialchars( $value );
146 $attribs = array( 'id' => $name );
147 if ( $name == 'wpPassword' ) {
148 $attribs[] = 'autofocus';
150 $field = Html
::input( $name, $value, $type, $attribs );
153 $out .= "\t<td class='mw-label'>";
154 if ( $type != 'text' ) {
155 $out .= Xml
::label( wfMsg( $label ), $name );
157 $out .= wfMsgHtml( $label );
160 $out .= "\t<td class='mw-input'>";
169 * @return bool|string true or string on success, false on failure
171 protected function attemptChange( User
$user, $pass, $newaddr ) {
172 if ( $newaddr != '' && !Sanitizer
::validateEmail( $newaddr ) ) {
173 $this->error( wfMsgExt( 'invalidemailaddress', 'parseinline' ) );
177 $throttleCount = LoginForm
::incLoginThrottle( $user->getName() );
178 if ( $throttleCount === true ) {
179 $this->error( wfMsgHtml( 'login-throttled' ) );
183 if ( !$user->checkTemporaryPassword( $pass ) && !$user->checkPassword( $pass ) ) {
184 $this->error( wfMsgHtml( 'wrongpassword' ) );
188 if ( $throttleCount ) {
189 LoginForm
::clearLoginThrottle( $user->getName() );
192 list( $status, $info ) = Preferences
::trySetUserEmail( $user, $newaddr );
193 if ( $status !== true ) {
194 if ( $status instanceof Status
) {
195 $this->getOutput()->addHTML(
196 '<p class="error">' .
197 $this->getOutput()->parseInline( $status->getWikiText( $info ) ) .
203 $user->saveSettings();
204 return $info ?
$info : true;