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 ) {
44 $this->checkReadOnly();
46 $request = $this->getRequest();
47 $this->mPassword
= $request->getVal( 'wpPassword' );
48 $this->mNewEmail
= $request->getVal( 'wpNewEmail' );
51 $this->outputHeader();
53 $out = $this->getOutput();
54 $out->disallowUserJs();
56 $user = $this->getUser();
58 if ( !$wgAuth->allowPropChange( 'emailaddress' ) ) {
59 $this->error( wfMsgExt( 'cannotchangeemail', 'parseinline' ) );
63 if ( !$request->wasPosted() && !$user->isLoggedIn() ) {
64 $this->error( wfMsg( 'changeemail-no-info' ) );
68 if ( $request->wasPosted() && $request->getBool( 'wpCancel' ) ) {
73 if ( $request->wasPosted()
74 && $user->matchEditToken( $request->getVal( 'token' ) ) )
76 $info = $this->attemptChange( $user, $this->mPassword
, $this->mNewEmail
);
77 if ( $info === true ) {
79 } elseif ( $info === 'eauth' ) {
80 # Notify user that a confirmation email has been sent...
81 $out->wrapWikiMsg( "<div class='error' style='clear: both;'>\n$1\n</div>",
82 'eauthentsent', $user->getName() );
83 $this->doReturnTo( 'soft' ); // just show the link to go back
91 protected function doReturnTo( $type = 'hard' ) {
92 $titleObj = Title
::newFromText( $this->getRequest()->getVal( 'returnto' ) );
93 if ( !$titleObj instanceof Title
) {
94 $titleObj = Title
::newMainPage();
96 if ( $type == 'hard' ) {
97 $this->getOutput()->redirect( $titleObj->getFullURL() );
99 $this->getOutput()->addReturnTo( $titleObj );
103 protected function error( $msg ) {
104 $this->getOutput()->addHTML( Xml
::element('p', array( 'class' => 'error' ), $msg ) );
107 protected function showForm() {
108 $user = $this->getUser();
110 $oldEmailText = $user->getEmail()
112 : wfMsg( 'changeemail-none' );
114 $this->getOutput()->addHTML(
115 Xml
::fieldset( wfMsg( 'changeemail-header' ) ) .
116 Xml
::openElement( 'form',
119 'action' => $this->getTitle()->getLocalUrl(),
120 'id' => 'mw-changeemail-form' ) ) . "\n" .
121 Html
::hidden( 'token', $user->getEditToken() ) . "\n" .
122 Html
::hidden( 'returnto', $this->getRequest()->getVal( 'returnto' ) ) . "\n" .
123 wfMsgExt( 'changeemail-text', array( 'parse' ) ) . "\n" .
124 Xml
::openElement( 'table', array( 'id' => 'mw-changeemail-table' ) ) . "\n" .
125 $this->pretty( array(
126 array( 'wpName', 'username', 'text', $user->getName() ),
127 array( 'wpOldEmail', 'changeemail-oldemail', 'text', $oldEmailText ),
128 array( 'wpNewEmail', 'changeemail-newemail', 'input', $this->mNewEmail
),
129 array( 'wpPassword', 'yourpassword', 'password', $this->mPassword
),
133 '<td class="mw-input">' .
134 Xml
::submitButton( wfMsg( 'changeemail-submit' ) ) .
135 Xml
::submitButton( wfMsg( 'changeemail-cancel' ), array( 'name' => 'wpCancel' ) ) .
138 Xml
::closeElement( 'table' ) .
139 Xml
::closeElement( 'form' ) .
140 Xml
::closeElement( 'fieldset' ) . "\n"
144 protected function pretty( $fields ) {
146 foreach ( $fields as $list ) {
147 list( $name, $label, $type, $value ) = $list;
148 if( $type == 'text' ) {
149 $field = htmlspecialchars( $value );
151 $attribs = array( 'id' => $name );
152 if ( $name == 'wpPassword' ) {
153 $attribs[] = 'autofocus';
155 $field = Html
::input( $name, $value, $type, $attribs );
158 $out .= "\t<td class='mw-label'>";
159 if ( $type != 'text' ) {
160 $out .= Xml
::label( wfMsg( $label ), $name );
162 $out .= wfMsgHtml( $label );
165 $out .= "\t<td class='mw-input'>";
174 * @return bool|string true or string on success, false on failure
176 protected function attemptChange( User
$user, $pass, $newaddr ) {
177 if ( $newaddr != '' && !Sanitizer
::validateEmail( $newaddr ) ) {
178 $this->error( wfMsgExt( 'invalidemailaddress', 'parseinline' ) );
182 $throttleCount = LoginForm
::incLoginThrottle( $user->getName() );
183 if ( $throttleCount === true ) {
184 $this->error( wfMsgHtml( 'login-throttled' ) );
188 if ( !$user->checkTemporaryPassword( $pass ) && !$user->checkPassword( $pass ) ) {
189 $this->error( wfMsgHtml( 'wrongpassword' ) );
193 if ( $throttleCount ) {
194 LoginForm
::clearLoginThrottle( $user->getName() );
197 list( $status, $info ) = Preferences
::trySetUserEmail( $user, $newaddr );
198 if ( $status !== true ) {
199 if ( $status instanceof Status
) {
200 $this->getOutput()->addHTML(
201 '<p class="error">' .
202 $this->getOutput()->parseInline( $status->getWikiText( $info ) ) .
208 $user->saveSettings();
209 return $info ?
$info : true;