Implement extension registration from an extension.json file
[mediawiki.git] / includes / specials / SpecialChangeEmail.php
blob06ede617de55dc0cb7502e0943fef184297b15cb
1 <?php
2 /**
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * Let users change their email address.
27 * @ingroup SpecialPage
29 class SpecialChangeEmail extends FormSpecialPage {
30 /**
31 * @var Status
33 private $status;
35 public function __construct() {
36 parent::__construct( 'ChangeEmail', 'editmyprivateinfo' );
39 /**
40 * @return bool
42 public function isListed() {
43 global $wgAuth;
45 return $wgAuth->allowPropChange( 'emailaddress' );
48 /**
49 * Main execution point
50 * @param string $par
52 function execute( $par ) {
53 $out = $this->getOutput();
54 $out->disallowUserJs();
55 $out->addModules( 'mediawiki.special.changeemail' );
57 parent::execute( $par );
60 protected function checkExecutePermissions( User $user ) {
61 global $wgAuth;
63 if ( !$wgAuth->allowPropChange( 'emailaddress' ) ) {
64 throw new ErrorPageError( 'changeemail', 'cannotchangeemail' );
67 $this->requireLogin( 'changeemail-no-info' );
69 // This could also let someone check the current email address, so
70 // require both permissions.
71 if ( !$this->getUser()->isAllowed( 'viewmyprivateinfo' ) ) {
72 throw new PermissionsError( 'viewmyprivateinfo' );
75 parent::checkExecutePermissions( $user );
78 protected function getFormFields() {
79 $user = $this->getUser();
81 $fields = array(
82 'Name' => array(
83 'type' => 'info',
84 'label-message' => 'username',
85 'default' => $user->getName(),
87 'OldEmail' => array(
88 'type' => 'info',
89 'label-message' => 'changeemail-oldemail',
90 'default' => $user->getEmail() ?: $this->msg( 'changeemail-none' )->text(),
92 'NewEmail' => array(
93 'type' => 'email',
94 'label-message' => 'changeemail-newemail',
98 if ( $this->getConfig()->get( 'RequirePasswordforEmailChange' ) ) {
99 $fields['Password'] = array(
100 'type' => 'password',
101 'label-message' => 'changeemail-password',
102 'autofocus' => true,
106 return $fields;
109 protected function alterForm( HTMLForm $form ) {
110 $form->setDisplayFormat( 'vform' );
111 $form->setId( 'mw-changeemail-form' );
112 $form->setTableId( 'mw-changeemail-table' );
113 $form->setWrapperLegend( false );
114 $form->setSubmitTextMsg( 'changeemail-submit' );
115 $form->addHiddenFields( $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
118 public function onSubmit( array $data ) {
119 $password = isset( $data['Password'] ) ? $data['Password'] : null;
120 $status = $this->attemptChange( $this->getUser(), $password, $data['NewEmail'] );
122 $this->status = $status;
124 return $status;
127 public function onSuccess() {
128 $request = $this->getRequest();
130 $titleObj = Title::newFromText( $request->getVal( 'returnto' ) );
131 if ( !$titleObj instanceof Title ) {
132 $titleObj = Title::newMainPage();
134 $query = $request->getVal( 'returntoquery' );
136 if ( $this->status->value === true ) {
137 $this->getOutput()->redirect( $titleObj->getFullURL( $query ) );
138 } elseif ( $this->status->value === 'eauth' ) {
139 # Notify user that a confirmation email has been sent...
140 $this->getOutput()->wrapWikiMsg( "<div class='error' style='clear: both;'>\n$1\n</div>",
141 'eauthentsent', $this->getUser()->getName() );
142 $this->getOutput()->addReturnTo( $titleObj, wfCgiToArray( $query ) ); // just show the link to go back
147 * @param User $user
148 * @param string $pass
149 * @param string $newaddr
150 * @return Status
152 private function attemptChange( User $user, $pass, $newaddr ) {
153 global $wgAuth;
155 if ( $newaddr != '' && !Sanitizer::validateEmail( $newaddr ) ) {
156 return Status::newFatal( 'invalidemailaddress' );
159 $throttleCount = LoginForm::incLoginThrottle( $user->getName() );
160 if ( $throttleCount === true ) {
161 $lang = $this->getLanguage();
162 $throttleInfo = $this->getConfig()->get( 'PasswordAttemptThrottle' );
163 return Status::newFatal(
164 'changeemail-throttled',
165 $lang->formatDuration( $throttleInfo['seconds'] )
169 if ( $this->getConfig()->get( 'RequirePasswordforEmailChange' )
170 && !$user->checkTemporaryPassword( $pass )
171 && !$user->checkPassword( $pass )
173 return Status::newFatal( 'wrongpassword' );
176 if ( $throttleCount ) {
177 LoginForm::clearLoginThrottle( $user->getName() );
180 $oldaddr = $user->getEmail();
181 $status = $user->setEmailWithConfirmation( $newaddr );
182 if ( !$status->isGood() ) {
183 return $status;
186 Hooks::run( 'PrefsEmailAudit', array( $user, $oldaddr, $newaddr ) );
188 $user->saveSettings();
190 $wgAuth->updateExternalDB( $user );
192 return $status;
195 public function requiresUnblock() {
196 return false;
199 protected function getGroupName() {
200 return 'users';