Remove useless comments from search output
[mediawiki.git] / includes / specials / SpecialChangeEmail.php
blob07a2a862779662a235c8870cfaf3199419435b47
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', 'editmyprivateinfo' );
47 /**
48 * @return bool
50 function isListed() {
51 global $wgAuth;
53 return $wgAuth->allowPropChange( 'emailaddress' );
56 /**
57 * Main execution point
58 * @param string $par
60 function execute( $par ) {
61 global $wgAuth;
63 $this->setHeaders();
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' );
73 return;
76 $user = $this->getUser();
77 $request = $this->getRequest();
79 $this->requireLogin( 'changeemail-no-info' );
81 if ( $request->wasPosted() && $request->getBool( 'wpCancel' ) ) {
82 $this->doReturnTo();
84 return;
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 ) {
104 $this->doReturnTo();
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
110 return; // skip form
114 $this->showForm();
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() );
127 } else {
128 $this->getOutput()->addReturnTo( $titleObj );
133 * @param string $msg
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()
144 ? $user->getEmail()
145 : $this->msg( 'changeemail-none' )->text();
147 $this->getOutput()->addHTML(
148 Xml::fieldset( $this->msg( 'changeemail-header' )->text() ) .
149 Xml::openElement( 'form',
150 array(
151 'method' => 'post',
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"
159 $items = array(
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 ) .
170 "\n" .
171 "<tr>\n" .
172 "<td></td>\n" .
173 '<td class="mw-input">' .
174 Xml::submitButton( $this->msg( 'changeemail-submit' )->text() ) .
175 Xml::submitButton( $this->msg( 'changeemail-cancel' )->text(), array( 'name' => 'wpCancel' ) ) .
176 "</td>\n" .
177 "</tr>\n" .
178 Xml::closeElement( 'table' ) .
179 Xml::closeElement( 'form' ) .
180 Xml::closeElement( 'fieldset' ) . "\n"
185 * @param array $fields
186 * @return string
188 protected function pretty( $fields ) {
189 $out = '';
190 foreach ( $fields as $list ) {
191 list( $name, $label, $type, $value ) = $list;
192 if ( $type == 'text' ) {
193 $field = htmlspecialchars( $value );
194 } else {
195 $attribs = array( 'id' => $name );
196 if ( $name == 'wpPassword' ) {
197 $attribs[] = 'autofocus';
199 $field = Html::input( $name, $value, $type, $attribs );
201 $out .= "<tr>\n";
202 $out .= "\t<td class='mw-label'>";
203 if ( $type != 'text' ) {
204 $out .= Xml::label( $this->msg( $label )->text(), $name );
205 } else {
206 $out .= $this->msg( $label )->escaped();
208 $out .= "</td>\n";
209 $out .= "\t<td class='mw-input'>";
210 $out .= $field;
211 $out .= "</td>\n";
212 $out .= "</tr>";
215 return $out;
219 * @param User $user
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' );
230 return false;
233 $throttleCount = LoginForm::incLoginThrottle( $user->getName() );
234 if ( $throttleCount === true ) {
235 $lang = $this->getLanguage();
236 $this->error( array( 'changeemail-throttled', $lang->formatDuration( $wgPasswordAttemptThrottle['seconds'] ) ) );
238 return false;
241 global $wgRequirePasswordforEmailChange;
242 if ( $wgRequirePasswordforEmailChange && !$user->checkTemporaryPassword( $pass ) && !$user->checkPassword( $pass ) ) {
243 $this->error( 'wrongpassword' );
245 return false;
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' ) ) .
258 '</p>' );
260 return false;
263 wfRunHooks( 'PrefsEmailAudit', array( $user, $oldaddr, $newaddr ) );
265 $user->saveSettings();
267 $wgAuth->updateExternalDB( $user );
269 return $status->value;
272 protected function getGroupName() {
273 return 'users';