Fixed spacing in db/debug/diff/externalstore/objectcache folder
[mediawiki.git] / includes / specials / SpecialChangeEmail.php
blob9e435fb374ebdb98110aaede941301c214f4440f
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 UnlistedSpecialPage {
31 /**
32 * Users password
33 * @var string
35 protected $mPassword;
37 /**
38 * Users new email address
39 * @var string
41 protected $mNewEmail;
43 public function __construct() {
44 parent::__construct( 'ChangeEmail' );
47 /**
48 * @return Bool
50 function isListed() {
51 global $wgAuth;
53 return $wgAuth->allowPropChange( 'emailaddress' );
56 /**
57 * Main execution point
59 function execute( $par ) {
60 global $wgAuth;
62 $this->setHeaders();
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' );
72 return;
75 $user = $this->getUser();
76 $request = $this->getRequest();
78 if ( !$request->wasPosted() && !$user->isLoggedIn() ) {
79 $this->error( 'changeemail-no-info' );
81 return;
84 if ( $request->wasPosted() && $request->getBool( 'wpCancel' ) ) {
85 $this->doReturnTo();
87 return;
90 $this->checkReadOnly();
92 $this->mPassword = $request->getVal( 'wpPassword' );
93 $this->mNewEmail = $request->getVal( 'wpNewEmail' );
95 if ( $request->wasPosted()
96 && $user->matchEditToken( $request->getVal( 'token' ) )
97 ) {
98 $info = $this->attemptChange( $user, $this->mPassword, $this->mNewEmail );
99 if ( $info === true ) {
100 $this->doReturnTo();
101 } elseif ( $info === 'eauth' ) {
102 # Notify user that a confirmation email has been sent...
103 $out->wrapWikiMsg( "<div class='error' style='clear: both;'>\n$1\n</div>",
104 'eauthentsent', $user->getName() );
105 $this->doReturnTo( 'soft' ); // just show the link to go back
106 return; // skip form
110 $this->showForm();
114 * @param $type string
116 protected function doReturnTo( $type = 'hard' ) {
117 $titleObj = Title::newFromText( $this->getRequest()->getVal( 'returnto' ) );
118 if ( !$titleObj instanceof Title ) {
119 $titleObj = Title::newMainPage();
121 if ( $type == 'hard' ) {
122 $this->getOutput()->redirect( $titleObj->getFullURL() );
123 } else {
124 $this->getOutput()->addReturnTo( $titleObj );
129 * @param $msg string
131 protected function error( $msg ) {
132 $this->getOutput()->wrapWikiMsg( "<p class='error'>\n$1\n</p>", $msg );
135 protected function showForm() {
136 global $wgRequirePasswordforEmailChange;
137 $user = $this->getUser();
139 $oldEmailText = $user->getEmail()
140 ? $user->getEmail()
141 : $this->msg( 'changeemail-none' )->text();
143 $this->getOutput()->addHTML(
144 Xml::fieldset( $this->msg( 'changeemail-header' )->text() ) .
145 Xml::openElement( 'form',
146 array(
147 'method' => 'post',
148 'action' => $this->getTitle()->getLocalURL(),
149 'id' => 'mw-changeemail-form' ) ) . "\n" .
150 Html::hidden( 'token', $user->getEditToken() ) . "\n" .
151 Html::hidden( 'returnto', $this->getRequest()->getVal( 'returnto' ) ) . "\n" .
152 $this->msg( 'changeemail-text' )->parseAsBlock() . "\n" .
153 Xml::openElement( 'table', array( 'id' => 'mw-changeemail-table' ) ) . "\n"
155 $items = array(
156 array( 'wpName', 'username', 'text', $user->getName() ),
157 array( 'wpOldEmail', 'changeemail-oldemail', 'text', $oldEmailText ),
158 array( 'wpNewEmail', 'changeemail-newemail', 'email', $this->mNewEmail ),
160 if ( $wgRequirePasswordforEmailChange ) {
161 $items[] = array( 'wpPassword', 'changeemail-password', 'password', $this->mPassword );
164 $this->getOutput()->addHTML(
165 $this->pretty( $items ) .
166 "\n" .
167 "<tr>\n" .
168 "<td></td>\n" .
169 '<td class="mw-input">' .
170 Xml::submitButton( $this->msg( 'changeemail-submit' )->text() ) .
171 Xml::submitButton( $this->msg( 'changeemail-cancel' )->text(), array( 'name' => 'wpCancel' ) ) .
172 "</td>\n" .
173 "</tr>\n" .
174 Xml::closeElement( 'table' ) .
175 Xml::closeElement( 'form' ) .
176 Xml::closeElement( 'fieldset' ) . "\n"
181 * @param $fields array
182 * @return string
184 protected function pretty( $fields ) {
185 $out = '';
186 foreach ( $fields as $list ) {
187 list( $name, $label, $type, $value ) = $list;
188 if ( $type == 'text' ) {
189 $field = htmlspecialchars( $value );
190 } else {
191 $attribs = array( 'id' => $name );
192 if ( $name == 'wpPassword' ) {
193 $attribs[] = 'autofocus';
195 $field = Html::input( $name, $value, $type, $attribs );
197 $out .= "<tr>\n";
198 $out .= "\t<td class='mw-label'>";
199 if ( $type != 'text' ) {
200 $out .= Xml::label( $this->msg( $label )->text(), $name );
201 } else {
202 $out .= $this->msg( $label )->escaped();
204 $out .= "</td>\n";
205 $out .= "\t<td class='mw-input'>";
206 $out .= $field;
207 $out .= "</td>\n";
208 $out .= "</tr>";
211 return $out;
215 * @param $user User
216 * @param $pass string
217 * @param $newaddr string
218 * @return bool|string true or string on success, false on failure
220 protected function attemptChange( User $user, $pass, $newaddr ) {
221 global $wgAuth;
223 if ( $newaddr != '' && !Sanitizer::validateEmail( $newaddr ) ) {
224 $this->error( 'invalidemailaddress' );
226 return false;
229 $throttleCount = LoginForm::incLoginThrottle( $user->getName() );
230 if ( $throttleCount === true ) {
231 $this->error( 'login-throttled' );
233 return false;
236 global $wgRequirePasswordforEmailChange;
237 if ( $wgRequirePasswordforEmailChange && !$user->checkTemporaryPassword( $pass ) && !$user->checkPassword( $pass ) ) {
238 $this->error( 'wrongpassword' );
240 return false;
243 if ( $throttleCount ) {
244 LoginForm::clearLoginThrottle( $user->getName() );
247 $oldaddr = $user->getEmail();
248 $status = $user->setEmailWithConfirmation( $newaddr );
249 if ( !$status->isGood() ) {
250 $this->getOutput()->addHTML(
251 '<p class="error">' .
252 $this->getOutput()->parseInline( $status->getWikiText( 'mailerror' ) ) .
253 '</p>' );
255 return false;
258 wfRunHooks( 'PrefsEmailAudit', array( $user, $oldaddr, $newaddr ) );
260 $user->saveSettings();
262 $wgAuth->updateExternalDB( $user );
264 return $status->value;
267 protected function getGroupName() {
268 return 'users';