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
{
38 * Users new email address
43 public function __construct() {
44 parent
::__construct( 'ChangeEmail' );
53 return $wgAuth->allowPropChange( 'emailaddress' );
57 * Main execution point
59 function execute( $par ) {
63 $this->outputHeader();
65 $out = $this->getOutput();
66 $out->disallowUserJs();
67 $out->addModules( 'mediawiki.special.changeemail' );
69 if ( !$wgAuth->allowPropChange( 'emailaddress' ) ) {
70 $this->error( 'cannotchangeemail' );
75 $user = $this->getUser();
76 $request = $this->getRequest();
78 if ( !$request->wasPosted() && !$user->isLoggedIn() ) {
79 $this->error( 'changeemail-no-info' );
84 if ( $request->wasPosted() && $request->getBool( 'wpCancel' ) ) {
90 $this->checkReadOnly();
92 $this->mPassword
= $request->getVal( 'wpPassword' );
93 $this->mNewEmail
= $request->getVal( 'wpNewEmail' );
95 if ( $request->wasPosted()
96 && $user->matchEditToken( $request->getVal( 'token' ) )
98 $info = $this->attemptChange( $user, $this->mPassword
, $this->mNewEmail
);
99 if ( $info === true ) {
101 } elseif ( $info === 'eauth' ) {
102 # Notify user that a confirmation email has been sent...
103 $out->wrapWikiMsg( "<div class='error' style='clear: both;'>\n$1\n</div>",
104 'eauthentsent', $user->getName() );
105 $this->doReturnTo( 'soft' ); // just show the link to go back
114 * @param $type string
116 protected function doReturnTo( $type = 'hard' ) {
117 $titleObj = Title
::newFromText( $this->getRequest()->getVal( 'returnto' ) );
118 if ( !$titleObj instanceof Title
) {
119 $titleObj = Title
::newMainPage();
121 if ( $type == 'hard' ) {
122 $this->getOutput()->redirect( $titleObj->getFullURL() );
124 $this->getOutput()->addReturnTo( $titleObj );
131 protected function error( $msg ) {
132 $this->getOutput()->wrapWikiMsg( "<p class='error'>\n$1\n</p>", $msg );
135 protected function showForm() {
136 global $wgRequirePasswordforEmailChange;
137 $user = $this->getUser();
139 $oldEmailText = $user->getEmail()
141 : $this->msg( 'changeemail-none' )->text();
143 $this->getOutput()->addHTML(
144 Xml
::fieldset( $this->msg( 'changeemail-header' )->text() ) .
145 Xml
::openElement( 'form',
148 'action' => $this->getTitle()->getLocalURL(),
149 'id' => 'mw-changeemail-form' ) ) . "\n" .
150 Html
::hidden( 'token', $user->getEditToken() ) . "\n" .
151 Html
::hidden( 'returnto', $this->getRequest()->getVal( 'returnto' ) ) . "\n" .
152 $this->msg( 'changeemail-text' )->parseAsBlock() . "\n" .
153 Xml
::openElement( 'table', array( 'id' => 'mw-changeemail-table' ) ) . "\n"
156 array( 'wpName', 'username', 'text', $user->getName() ),
157 array( 'wpOldEmail', 'changeemail-oldemail', 'text', $oldEmailText ),
158 array( 'wpNewEmail', 'changeemail-newemail', 'email', $this->mNewEmail
),
160 if ( $wgRequirePasswordforEmailChange ) {
161 $items[] = array( 'wpPassword', 'changeemail-password', 'password', $this->mPassword
);
164 $this->getOutput()->addHTML(
165 $this->pretty( $items ) .
169 '<td class="mw-input">' .
170 Xml
::submitButton( $this->msg( 'changeemail-submit' )->text() ) .
171 Xml
::submitButton( $this->msg( 'changeemail-cancel' )->text(), array( 'name' => 'wpCancel' ) ) .
174 Xml
::closeElement( 'table' ) .
175 Xml
::closeElement( 'form' ) .
176 Xml
::closeElement( 'fieldset' ) . "\n"
181 * @param $fields array
184 protected function pretty( $fields ) {
186 foreach ( $fields as $list ) {
187 list( $name, $label, $type, $value ) = $list;
188 if ( $type == 'text' ) {
189 $field = htmlspecialchars( $value );
191 $attribs = array( 'id' => $name );
192 if ( $name == 'wpPassword' ) {
193 $attribs[] = 'autofocus';
195 $field = Html
::input( $name, $value, $type, $attribs );
198 $out .= "\t<td class='mw-label'>";
199 if ( $type != 'text' ) {
200 $out .= Xml
::label( $this->msg( $label )->text(), $name );
202 $out .= $this->msg( $label )->escaped();
205 $out .= "\t<td class='mw-input'>";
216 * @param $pass string
217 * @param $newaddr string
218 * @return bool|string true or string on success, false on failure
220 protected function attemptChange( User
$user, $pass, $newaddr ) {
223 if ( $newaddr != '' && !Sanitizer
::validateEmail( $newaddr ) ) {
224 $this->error( 'invalidemailaddress' );
229 $throttleCount = LoginForm
::incLoginThrottle( $user->getName() );
230 if ( $throttleCount === true ) {
231 $this->error( 'login-throttled' );
236 global $wgRequirePasswordforEmailChange;
237 if ( $wgRequirePasswordforEmailChange && !$user->checkTemporaryPassword( $pass ) && !$user->checkPassword( $pass ) ) {
238 $this->error( 'wrongpassword' );
243 if ( $throttleCount ) {
244 LoginForm
::clearLoginThrottle( $user->getName() );
247 $oldaddr = $user->getEmail();
248 $status = $user->setEmailWithConfirmation( $newaddr );
249 if ( !$status->isGood() ) {
250 $this->getOutput()->addHTML(
251 '<p class="error">' .
252 $this->getOutput()->parseInline( $status->getWikiText( 'mailerror' ) ) .
258 wfRunHooks( 'PrefsEmailAudit', array( $user, $oldaddr, $newaddr ) );
260 $user->saveSettings();
262 $wgAuth->updateExternalDB( $user );
264 return $status->value
;
267 protected function getGroupName() {