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
{
37 * Users new email address
42 public function __construct() {
43 parent
::__construct( 'ChangeEmail', 'editmyprivateinfo' );
52 return $wgAuth->allowPropChange( 'emailaddress' );
56 * 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 $this->requireLogin( 'changeemail-no-info' );
80 if ( $request->wasPosted() && $request->getBool( 'wpCancel' ) ) {
86 $this->checkReadOnly();
87 $this->checkPermissions();
89 // This could also let someone check the current email address, so
90 // require both permissions.
91 if ( !$user->isAllowed( 'viewmyprivateinfo' ) ) {
92 throw new PermissionsError( 'viewmyprivateinfo' );
95 $this->mPassword
= $request->getVal( 'wpPassword' );
96 $this->mNewEmail
= $request->getVal( 'wpNewEmail' );
98 if ( $request->wasPosted()
99 && $user->matchEditToken( $request->getVal( 'token' ) )
101 $info = $this->attemptChange( $user, $this->mPassword
, $this->mNewEmail
);
102 if ( $info === true ) {
104 } elseif ( $info === 'eauth' ) {
105 # Notify user that a confirmation email has been sent...
106 $out->wrapWikiMsg( "<div class='error' style='clear: both;'>\n$1\n</div>",
107 'eauthentsent', $user->getName() );
108 $this->doReturnTo( 'soft' ); // just show the link to go back
117 * @param string $type
119 protected function doReturnTo( $type = 'hard' ) {
120 $titleObj = Title
::newFromText( $this->getRequest()->getVal( 'returnto' ) );
121 if ( !$titleObj instanceof Title
) {
122 $titleObj = Title
::newMainPage();
124 if ( $type == 'hard' ) {
125 $this->getOutput()->redirect( $titleObj->getFullURL() );
127 $this->getOutput()->addReturnTo( $titleObj );
134 protected function error( $msg ) {
135 $this->getOutput()->wrapWikiMsg( "<p class='error'>\n$1\n</p>", $msg );
138 protected function showForm() {
139 global $wgRequirePasswordforEmailChange;
140 $user = $this->getUser();
142 $oldEmailText = $user->getEmail()
144 : $this->msg( 'changeemail-none' )->text();
146 $this->getOutput()->addHTML(
147 Xml
::fieldset( $this->msg( 'changeemail-header' )->text() ) .
148 Xml
::openElement( 'form',
151 'action' => $this->getPageTitle()->getLocalURL(),
152 'id' => 'mw-changeemail-form' ) ) . "\n" .
153 Html
::hidden( 'token', $user->getEditToken() ) . "\n" .
154 Html
::hidden( 'returnto', $this->getRequest()->getVal( 'returnto' ) ) . "\n" .
155 $this->msg( 'changeemail-text' )->parseAsBlock() . "\n" .
156 Xml
::openElement( 'table', array( 'id' => 'mw-changeemail-table' ) ) . "\n"
159 array( 'wpName', 'username', 'text', $user->getName() ),
160 array( 'wpOldEmail', 'changeemail-oldemail', 'text', $oldEmailText ),
161 array( 'wpNewEmail', 'changeemail-newemail', 'email', $this->mNewEmail
),
163 if ( $wgRequirePasswordforEmailChange ) {
164 $items[] = array( 'wpPassword', 'changeemail-password', 'password', $this->mPassword
);
167 $this->getOutput()->addHTML(
168 $this->pretty( $items ) .
172 '<td class="mw-input">' .
173 Xml
::submitButton( $this->msg( 'changeemail-submit' )->text() ) .
174 Xml
::submitButton( $this->msg( 'changeemail-cancel' )->text(), array( 'name' => 'wpCancel' ) ) .
177 Xml
::closeElement( 'table' ) .
178 Xml
::closeElement( 'form' ) .
179 Xml
::closeElement( 'fieldset' ) . "\n"
184 * @param array $fields
187 protected function pretty( $fields ) {
189 foreach ( $fields as $list ) {
190 list( $name, $label, $type, $value ) = $list;
191 if ( $type == 'text' ) {
192 $field = htmlspecialchars( $value );
194 $attribs = array( 'id' => $name );
195 if ( $name == 'wpPassword' ) {
196 $attribs[] = 'autofocus';
198 $field = Html
::input( $name, $value, $type, $attribs );
201 $out .= "\t<td class='mw-label'>";
202 if ( $type != 'text' ) {
203 $out .= Xml
::label( $this->msg( $label )->text(), $name );
205 $out .= $this->msg( $label )->escaped();
208 $out .= "\t<td class='mw-input'>";
219 * @param string $pass
220 * @param string $newaddr
221 * @return bool|string True or string on success, false on failure
223 protected function attemptChange( User
$user, $pass, $newaddr ) {
224 global $wgAuth, $wgPasswordAttemptThrottle;
226 if ( $newaddr != '' && !Sanitizer
::validateEmail( $newaddr ) ) {
227 $this->error( 'invalidemailaddress' );
232 $throttleCount = LoginForm
::incLoginThrottle( $user->getName() );
233 if ( $throttleCount === true ) {
234 $lang = $this->getLanguage();
236 'changeemail-throttled',
237 $lang->formatDuration( $wgPasswordAttemptThrottle['seconds'] )
243 global $wgRequirePasswordforEmailChange;
244 if ( $wgRequirePasswordforEmailChange
245 && !$user->checkTemporaryPassword( $pass )
246 && !$user->checkPassword( $pass )
248 $this->error( 'wrongpassword' );
253 if ( $throttleCount ) {
254 LoginForm
::clearLoginThrottle( $user->getName() );
257 $oldaddr = $user->getEmail();
258 $status = $user->setEmailWithConfirmation( $newaddr );
259 if ( !$status->isGood() ) {
260 $this->getOutput()->addHTML(
261 '<p class="error">' .
262 $this->getOutput()->parseInline( $status->getWikiText( 'mailerror' ) ) .
268 wfRunHooks( 'PrefsEmailAudit', array( $user, $oldaddr, $newaddr ) );
270 $user->saveSettings();
272 $wgAuth->updateExternalDB( $user );
274 return $status->value
;
277 protected function getGroupName() {