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', 'editmyprivateinfo' );
53 return $wgAuth->allowPropChange( 'emailaddress' );
57 * Main execution point
60 function execute( $par ) {
64 $this->outputHeader();
66 $out = $this->getOutput();
67 $out->disallowUserJs();
68 $out->addModules( 'mediawiki.special.changeemail' );
70 if ( !$wgAuth->allowPropChange( 'emailaddress' ) ) {
71 $this->error( 'cannotchangeemail' );
76 $user = $this->getUser();
77 $request = $this->getRequest();
79 $this->requireLogin( 'changeemail-no-info' );
81 if ( $request->wasPosted() && $request->getBool( 'wpCancel' ) ) {
87 $this->checkReadOnly();
88 $this->checkPermissions();
90 // This could also let someone check the current email address, so
91 // require both permissions.
92 if ( !$user->isAllowed( 'viewmyprivateinfo' ) ) {
93 throw new PermissionsError( 'viewmyprivateinfo' );
96 $this->mPassword
= $request->getVal( 'wpPassword' );
97 $this->mNewEmail
= $request->getVal( 'wpNewEmail' );
99 if ( $request->wasPosted()
100 && $user->matchEditToken( $request->getVal( 'token' ) )
102 $info = $this->attemptChange( $user, $this->mPassword
, $this->mNewEmail
);
103 if ( $info === true ) {
105 } elseif ( $info === 'eauth' ) {
106 # Notify user that a confirmation email has been sent...
107 $out->wrapWikiMsg( "<div class='error' style='clear: both;'>\n$1\n</div>",
108 'eauthentsent', $user->getName() );
109 $this->doReturnTo( 'soft' ); // just show the link to go back
118 * @param string $type
120 protected function doReturnTo( $type = 'hard' ) {
121 $titleObj = Title
::newFromText( $this->getRequest()->getVal( 'returnto' ) );
122 if ( !$titleObj instanceof Title
) {
123 $titleObj = Title
::newMainPage();
125 if ( $type == 'hard' ) {
126 $this->getOutput()->redirect( $titleObj->getFullURL() );
128 $this->getOutput()->addReturnTo( $titleObj );
135 protected function error( $msg ) {
136 $this->getOutput()->wrapWikiMsg( "<p class='error'>\n$1\n</p>", $msg );
139 protected function showForm() {
140 global $wgRequirePasswordforEmailChange;
141 $user = $this->getUser();
143 $oldEmailText = $user->getEmail()
145 : $this->msg( 'changeemail-none' )->text();
147 $this->getOutput()->addHTML(
148 Xml
::fieldset( $this->msg( 'changeemail-header' )->text() ) .
149 Xml
::openElement( 'form',
152 'action' => $this->getPageTitle()->getLocalURL(),
153 'id' => 'mw-changeemail-form' ) ) . "\n" .
154 Html
::hidden( 'token', $user->getEditToken() ) . "\n" .
155 Html
::hidden( 'returnto', $this->getRequest()->getVal( 'returnto' ) ) . "\n" .
156 $this->msg( 'changeemail-text' )->parseAsBlock() . "\n" .
157 Xml
::openElement( 'table', array( 'id' => 'mw-changeemail-table' ) ) . "\n"
160 array( 'wpName', 'username', 'text', $user->getName() ),
161 array( 'wpOldEmail', 'changeemail-oldemail', 'text', $oldEmailText ),
162 array( 'wpNewEmail', 'changeemail-newemail', 'email', $this->mNewEmail
),
164 if ( $wgRequirePasswordforEmailChange ) {
165 $items[] = array( 'wpPassword', 'changeemail-password', 'password', $this->mPassword
);
168 $this->getOutput()->addHTML(
169 $this->pretty( $items ) .
173 '<td class="mw-input">' .
174 Xml
::submitButton( $this->msg( 'changeemail-submit' )->text() ) .
175 Xml
::submitButton( $this->msg( 'changeemail-cancel' )->text(), array( 'name' => 'wpCancel' ) ) .
178 Xml
::closeElement( 'table' ) .
179 Xml
::closeElement( 'form' ) .
180 Xml
::closeElement( 'fieldset' ) . "\n"
185 * @param array $fields
188 protected function pretty( $fields ) {
190 foreach ( $fields as $list ) {
191 list( $name, $label, $type, $value ) = $list;
192 if ( $type == 'text' ) {
193 $field = htmlspecialchars( $value );
195 $attribs = array( 'id' => $name );
196 if ( $name == 'wpPassword' ) {
197 $attribs[] = 'autofocus';
199 $field = Html
::input( $name, $value, $type, $attribs );
202 $out .= "\t<td class='mw-label'>";
203 if ( $type != 'text' ) {
204 $out .= Xml
::label( $this->msg( $label )->text(), $name );
206 $out .= $this->msg( $label )->escaped();
209 $out .= "\t<td class='mw-input'>";
220 * @param string $pass
221 * @param string $newaddr
222 * @return bool|string True or string on success, false on failure
224 protected function attemptChange( User
$user, $pass, $newaddr ) {
225 global $wgAuth, $wgPasswordAttemptThrottle;
227 if ( $newaddr != '' && !Sanitizer
::validateEmail( $newaddr ) ) {
228 $this->error( 'invalidemailaddress' );
233 $throttleCount = LoginForm
::incLoginThrottle( $user->getName() );
234 if ( $throttleCount === true ) {
235 $lang = $this->getLanguage();
236 $this->error( array( 'changeemail-throttled', $lang->formatDuration( $wgPasswordAttemptThrottle['seconds'] ) ) );
241 global $wgRequirePasswordforEmailChange;
242 if ( $wgRequirePasswordforEmailChange && !$user->checkTemporaryPassword( $pass ) && !$user->checkPassword( $pass ) ) {
243 $this->error( 'wrongpassword' );
248 if ( $throttleCount ) {
249 LoginForm
::clearLoginThrottle( $user->getName() );
252 $oldaddr = $user->getEmail();
253 $status = $user->setEmailWithConfirmation( $newaddr );
254 if ( !$status->isGood() ) {
255 $this->getOutput()->addHTML(
256 '<p class="error">' .
257 $this->getOutput()->parseInline( $status->getWikiText( 'mailerror' ) ) .
263 wfRunHooks( 'PrefsEmailAudit', array( $user, $oldaddr, $newaddr ) );
265 $user->saveSettings();
267 $wgAuth->updateExternalDB( $user );
269 return $status->value
;
272 protected function getGroupName() {