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' );
52 return $wgAuth->allowPropChange( 'emailaddress' );
56 * Main execution point
58 function execute( $par ) {
62 $this->outputHeader();
64 $out = $this->getOutput();
65 $out->disallowUserJs();
66 $out->addModules( 'mediawiki.special.changeemail' );
68 if ( !$wgAuth->allowPropChange( 'emailaddress' ) ) {
69 $this->error( 'cannotchangeemail' );
73 $user = $this->getUser();
74 $request = $this->getRequest();
76 if ( !$request->wasPosted() && !$user->isLoggedIn() ) {
77 $this->error( 'changeemail-no-info' );
81 if ( $request->wasPosted() && $request->getBool( 'wpCancel' ) ) {
86 $this->checkReadOnly();
88 $this->mPassword
= $request->getVal( 'wpPassword' );
89 $this->mNewEmail
= $request->getVal( 'wpNewEmail' );
91 if ( $request->wasPosted()
92 && $user->matchEditToken( $request->getVal( 'token' ) )
94 $info = $this->attemptChange( $user, $this->mPassword
, $this->mNewEmail
);
95 if ( $info === true ) {
97 } elseif ( $info === 'eauth' ) {
98 # Notify user that a confirmation email has been sent...
99 $out->wrapWikiMsg( "<div class='error' style='clear: both;'>\n$1\n</div>",
100 'eauthentsent', $user->getName() );
101 $this->doReturnTo( 'soft' ); // just show the link to go back
110 * @param $type string
112 protected function doReturnTo( $type = 'hard' ) {
113 $titleObj = Title
::newFromText( $this->getRequest()->getVal( 'returnto' ) );
114 if ( !$titleObj instanceof Title
) {
115 $titleObj = Title
::newMainPage();
117 if ( $type == 'hard' ) {
118 $this->getOutput()->redirect( $titleObj->getFullURL() );
120 $this->getOutput()->addReturnTo( $titleObj );
127 protected function error( $msg ) {
128 $this->getOutput()->wrapWikiMsg( "<p class='error'>\n$1\n</p>", $msg );
131 protected function showForm() {
132 global $wgRequirePasswordforEmailChange;
133 $user = $this->getUser();
135 $oldEmailText = $user->getEmail()
137 : $this->msg( 'changeemail-none' )->text();
139 $this->getOutput()->addHTML(
140 Xml
::fieldset( $this->msg( 'changeemail-header' )->text() ) .
141 Xml
::openElement( 'form',
144 'action' => $this->getTitle()->getLocalURL(),
145 'id' => 'mw-changeemail-form' ) ) . "\n" .
146 Html
::hidden( 'token', $user->getEditToken() ) . "\n" .
147 Html
::hidden( 'returnto', $this->getRequest()->getVal( 'returnto' ) ) . "\n" .
148 $this->msg( 'changeemail-text' )->parseAsBlock() . "\n" .
149 Xml
::openElement( 'table', array( 'id' => 'mw-changeemail-table' ) ) . "\n"
152 array( 'wpName', 'username', 'text', $user->getName() ),
153 array( 'wpOldEmail', 'changeemail-oldemail', 'text', $oldEmailText ),
154 array( 'wpNewEmail', 'changeemail-newemail', 'email', $this->mNewEmail
),
156 if ( $wgRequirePasswordforEmailChange ) {
157 $items[] = array( 'wpPassword', 'changeemail-password', 'password', $this->mPassword
);
160 $this->getOutput()->addHTML(
161 $this->pretty( $items ) .
165 '<td class="mw-input">' .
166 Xml
::submitButton( $this->msg( 'changeemail-submit' )->text() ) .
167 Xml
::submitButton( $this->msg( 'changeemail-cancel' )->text(), array( 'name' => 'wpCancel' ) ) .
170 Xml
::closeElement( 'table' ) .
171 Xml
::closeElement( 'form' ) .
172 Xml
::closeElement( 'fieldset' ) . "\n"
177 * @param $fields array
180 protected function pretty( $fields ) {
182 foreach ( $fields as $list ) {
183 list( $name, $label, $type, $value ) = $list;
184 if ( $type == 'text' ) {
185 $field = htmlspecialchars( $value );
187 $attribs = array( 'id' => $name );
188 if ( $name == 'wpPassword' ) {
189 $attribs[] = 'autofocus';
191 $field = Html
::input( $name, $value, $type, $attribs );
194 $out .= "\t<td class='mw-label'>";
195 if ( $type != 'text' ) {
196 $out .= Xml
::label( $this->msg( $label )->text(), $name );
198 $out .= $this->msg( $label )->escaped();
201 $out .= "\t<td class='mw-input'>";
211 * @param $pass string
212 * @param $newaddr string
213 * @return bool|string true or string on success, false on failure
215 protected function attemptChange( User
$user, $pass, $newaddr ) {
218 if ( $newaddr != '' && !Sanitizer
::validateEmail( $newaddr ) ) {
219 $this->error( 'invalidemailaddress' );
223 $throttleCount = LoginForm
::incLoginThrottle( $user->getName() );
224 if ( $throttleCount === true ) {
225 $this->error( 'login-throttled' );
229 global $wgRequirePasswordforEmailChange;
230 if ( $wgRequirePasswordforEmailChange && !$user->checkTemporaryPassword( $pass ) && !$user->checkPassword( $pass ) ) {
231 $this->error( 'wrongpassword' );
235 if ( $throttleCount ) {
236 LoginForm
::clearLoginThrottle( $user->getName() );
239 $oldaddr = $user->getEmail();
240 $status = $user->setEmailWithConfirmation( $newaddr );
241 if ( !$status->isGood() ) {
242 $this->getOutput()->addHTML(
243 '<p class="error">' .
244 $this->getOutput()->parseInline( $status->getWikiText( 'mailerror' ) ) .
249 wfRunHooks( 'PrefsEmailAudit', array( $user, $oldaddr, $newaddr ) );
251 $user->saveSettings();
253 $wgAuth->updateExternalDB( $user );
255 return $status->value
;
258 protected function getGroupName() {