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
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 ( !$user->isLoggedIn() ) {
79 $this->error( 'changeemail-no-info' );
84 if ( $request->wasPosted() && $request->getBool( 'wpCancel' ) ) {
90 $this->checkReadOnly();
91 $this->checkPermissions();
93 // This could also let someone check the current email address, so
94 // require both permissions.
95 if ( !$this->getUser()->isAllowed( 'viewmyprivateinfo' ) ) {
96 throw new PermissionsError( 'viewmyprivateinfo' );
99 $this->mPassword
= $request->getVal( 'wpPassword' );
100 $this->mNewEmail
= $request->getVal( 'wpNewEmail' );
102 if ( $request->wasPosted()
103 && $user->matchEditToken( $request->getVal( 'token' ) )
105 $info = $this->attemptChange( $user, $this->mPassword
, $this->mNewEmail
);
106 if ( $info === true ) {
108 } elseif ( $info === 'eauth' ) {
109 # Notify user that a confirmation email has been sent...
110 $out->wrapWikiMsg( "<div class='error' style='clear: both;'>\n$1\n</div>",
111 'eauthentsent', $user->getName() );
112 $this->doReturnTo( 'soft' ); // just show the link to go back
121 * @param $type string
123 protected function doReturnTo( $type = 'hard' ) {
124 $titleObj = Title
::newFromText( $this->getRequest()->getVal( 'returnto' ) );
125 if ( !$titleObj instanceof Title
) {
126 $titleObj = Title
::newMainPage();
128 if ( $type == 'hard' ) {
129 $this->getOutput()->redirect( $titleObj->getFullURL() );
131 $this->getOutput()->addReturnTo( $titleObj );
138 protected function error( $msg ) {
139 $this->getOutput()->wrapWikiMsg( "<p class='error'>\n$1\n</p>", $msg );
142 protected function showForm() {
143 global $wgRequirePasswordforEmailChange;
144 $user = $this->getUser();
146 $oldEmailText = $user->getEmail()
148 : $this->msg( 'changeemail-none' )->text();
150 $this->getOutput()->addHTML(
151 Xml
::fieldset( $this->msg( 'changeemail-header' )->text() ) .
152 Xml
::openElement( 'form',
155 'action' => $this->getTitle()->getLocalURL(),
156 'id' => 'mw-changeemail-form' ) ) . "\n" .
157 Html
::hidden( 'token', $user->getEditToken() ) . "\n" .
158 Html
::hidden( 'returnto', $this->getRequest()->getVal( 'returnto' ) ) . "\n" .
159 $this->msg( 'changeemail-text' )->parseAsBlock() . "\n" .
160 Xml
::openElement( 'table', array( 'id' => 'mw-changeemail-table' ) ) . "\n"
163 array( 'wpName', 'username', 'text', $user->getName() ),
164 array( 'wpOldEmail', 'changeemail-oldemail', 'text', $oldEmailText ),
165 array( 'wpNewEmail', 'changeemail-newemail', 'email', $this->mNewEmail
),
167 if ( $wgRequirePasswordforEmailChange ) {
168 $items[] = array( 'wpPassword', 'changeemail-password', 'password', $this->mPassword
);
171 $this->getOutput()->addHTML(
172 $this->pretty( $items ) .
176 '<td class="mw-input">' .
177 Xml
::submitButton( $this->msg( 'changeemail-submit' )->text() ) .
178 Xml
::submitButton( $this->msg( 'changeemail-cancel' )->text(), array( 'name' => 'wpCancel' ) ) .
181 Xml
::closeElement( 'table' ) .
182 Xml
::closeElement( 'form' ) .
183 Xml
::closeElement( 'fieldset' ) . "\n"
188 * @param $fields array
191 protected function pretty( $fields ) {
193 foreach ( $fields as $list ) {
194 list( $name, $label, $type, $value ) = $list;
195 if ( $type == 'text' ) {
196 $field = htmlspecialchars( $value );
198 $attribs = array( 'id' => $name );
199 if ( $name == 'wpPassword' ) {
200 $attribs[] = 'autofocus';
202 $field = Html
::input( $name, $value, $type, $attribs );
205 $out .= "\t<td class='mw-label'>";
206 if ( $type != 'text' ) {
207 $out .= Xml
::label( $this->msg( $label )->text(), $name );
209 $out .= $this->msg( $label )->escaped();
212 $out .= "\t<td class='mw-input'>";
223 * @param $pass string
224 * @param $newaddr string
225 * @return bool|string true or string on success, false on failure
227 protected function attemptChange( User
$user, $pass, $newaddr ) {
228 global $wgAuth, $wgPasswordAttemptThrottle;
230 if ( $newaddr != '' && !Sanitizer
::validateEmail( $newaddr ) ) {
231 $this->error( 'invalidemailaddress' );
236 $throttleCount = LoginForm
::incLoginThrottle( $user->getName() );
237 if ( $throttleCount === true ) {
238 $lang = $this->getLanguage();
239 $this->error( array( 'login-throttled', $lang->formatDuration( $wgPasswordAttemptThrottle['seconds'] ) ) );
244 global $wgRequirePasswordforEmailChange;
245 if ( $wgRequirePasswordforEmailChange && !$user->checkTemporaryPassword( $pass ) && !$user->checkPassword( $pass ) ) {
246 $this->error( 'wrongpassword' );
251 if ( $throttleCount ) {
252 LoginForm
::clearLoginThrottle( $user->getName() );
255 $oldaddr = $user->getEmail();
256 $status = $user->setEmailWithConfirmation( $newaddr );
257 if ( !$status->isGood() ) {
258 $this->getOutput()->addHTML(
259 '<p class="error">' .
260 $this->getOutput()->parseInline( $status->getWikiText( 'mailerror' ) ) .
266 wfRunHooks( 'PrefsEmailAudit', array( $user, $oldaddr, $newaddr ) );
268 $user->saveSettings();
270 $wgAuth->updateExternalDB( $user );
272 return $status->value
;
275 protected function getGroupName() {