(bug 35565) Special:Log/patrol doesn't indicate whether patrolling was automatic
[mediawiki.git] / includes / specials / SpecialChangeEmail.php
blob0f85f516b8c3d72f9a6aab5540e3059f5e0d2cba
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 {
30 public function __construct() {
31 parent::__construct( 'ChangeEmail' );
34 function isListed() {
35 global $wgAuth;
36 return $wgAuth->allowPropChange( 'emailaddress' );
39 /**
40 * Main execution point
42 function execute( $par ) {
43 global $wgAuth;
45 $this->checkReadOnly();
47 $this->setHeaders();
48 $this->outputHeader();
50 if ( !$wgAuth->allowPropChange( 'emailaddress' ) ) {
51 $this->error( 'cannotchangeemail' );
52 return;
55 $user = $this->getUser();
56 $request = $this->getRequest();
58 if ( !$request->wasPosted() && !$user->isLoggedIn() ) {
59 $this->error( 'changeemail-no-info' );
60 return;
63 if ( $request->wasPosted() && $request->getBool( 'wpCancel' ) ) {
64 $this->doReturnTo();
65 return;
68 $out = $this->getOutput();
69 $out->disallowUserJs();
70 $out->addModules( 'mediawiki.special.changeemail' );
72 $this->mPassword = $request->getVal( 'wpPassword' );
73 $this->mNewEmail = $request->getVal( 'wpNewEmail' );
75 if ( $request->wasPosted()
76 && $user->matchEditToken( $request->getVal( 'token' ) ) )
78 $info = $this->attemptChange( $user, $this->mPassword, $this->mNewEmail );
79 if ( $info === true ) {
80 $this->doReturnTo();
81 } elseif ( $info === 'eauth' ) {
82 # Notify user that a confirmation email has been sent...
83 $out->wrapWikiMsg( "<div class='error' style='clear: both;'>\n$1\n</div>",
84 'eauthentsent', $user->getName() );
85 $this->doReturnTo( 'soft' ); // just show the link to go back
86 return; // skip form
90 $this->showForm();
93 protected function doReturnTo( $type = 'hard' ) {
94 $titleObj = Title::newFromText( $this->getRequest()->getVal( 'returnto' ) );
95 if ( !$titleObj instanceof Title ) {
96 $titleObj = Title::newMainPage();
98 if ( $type == 'hard' ) {
99 $this->getOutput()->redirect( $titleObj->getFullURL() );
100 } else {
101 $this->getOutput()->addReturnTo( $titleObj );
105 protected function error( $msg ) {
106 $this->getOutput()->wrapWikiMsg( "<p class='error'>\n$1\n</p>", $msg );
109 protected function showForm() {
110 $user = $this->getUser();
112 $oldEmailText = $user->getEmail()
113 ? $user->getEmail()
114 : $this->msg( 'changeemail-none' )->text();
116 $this->getOutput()->addHTML(
117 Xml::fieldset( $this->msg( 'changeemail-header' )->text() ) .
118 Xml::openElement( 'form',
119 array(
120 'method' => 'post',
121 'action' => $this->getTitle()->getLocalUrl(),
122 'id' => 'mw-changeemail-form' ) ) . "\n" .
123 Html::hidden( 'token', $user->getEditToken() ) . "\n" .
124 Html::hidden( 'returnto', $this->getRequest()->getVal( 'returnto' ) ) . "\n" .
125 $this->msg( 'changeemail-text' )->parseAsBlock() . "\n" .
126 Xml::openElement( 'table', array( 'id' => 'mw-changeemail-table' ) ) . "\n" .
127 $this->pretty( array(
128 array( 'wpName', 'username', 'text', $user->getName() ),
129 array( 'wpOldEmail', 'changeemail-oldemail', 'text', $oldEmailText ),
130 array( 'wpNewEmail', 'changeemail-newemail', 'input', $this->mNewEmail ),
131 array( 'wpPassword', 'yourpassword', 'password', $this->mPassword ),
132 ) ) . "\n" .
133 "<tr>\n" .
134 "<td></td>\n" .
135 '<td class="mw-input">' .
136 Xml::submitButton( $this->msg( 'changeemail-submit' )->text() ) .
137 Xml::submitButton( $this->msg( 'changeemail-cancel' )->text(), array( 'name' => 'wpCancel' ) ) .
138 "</td>\n" .
139 "</tr>\n" .
140 Xml::closeElement( 'table' ) .
141 Xml::closeElement( 'form' ) .
142 Xml::closeElement( 'fieldset' ) . "\n"
146 protected function pretty( $fields ) {
147 $out = '';
148 foreach ( $fields as $list ) {
149 list( $name, $label, $type, $value ) = $list;
150 if( $type == 'text' ) {
151 $field = htmlspecialchars( $value );
152 } else {
153 $attribs = array( 'id' => $name );
154 if ( $name == 'wpPassword' ) {
155 $attribs[] = 'autofocus';
157 $field = Html::input( $name, $value, $type, $attribs );
159 $out .= "<tr>\n";
160 $out .= "\t<td class='mw-label'>";
161 if ( $type != 'text' ) {
162 $out .= Xml::label( $this->msg( $label )->text(), $name );
163 } else {
164 $out .= $this->msg( $label )->escaped();
166 $out .= "</td>\n";
167 $out .= "\t<td class='mw-input'>";
168 $out .= $field;
169 $out .= "</td>\n";
170 $out .= "</tr>";
172 return $out;
176 * @return bool|string true or string on success, false on failure
178 protected function attemptChange( User $user, $pass, $newaddr ) {
179 if ( $newaddr != '' && !Sanitizer::validateEmail( $newaddr ) ) {
180 $this->error( 'invalidemailaddress' );
181 return false;
184 $throttleCount = LoginForm::incLoginThrottle( $user->getName() );
185 if ( $throttleCount === true ) {
186 $this->error( 'login-throttled' );
187 return false;
190 if ( !$user->checkTemporaryPassword( $pass ) && !$user->checkPassword( $pass ) ) {
191 $this->error( 'wrongpassword' );
192 return false;
195 if ( $throttleCount ) {
196 LoginForm::clearLoginThrottle( $user->getName() );
199 list( $status, $info ) = Preferences::trySetUserEmail( $user, $newaddr );
200 if ( $status !== true ) {
201 if ( $status instanceof Status ) {
202 $this->getOutput()->addHTML(
203 '<p class="error">' .
204 $this->getOutput()->parseInline( $status->getWikiText( $info ) ) .
205 '</p>' );
207 return false;
210 $user->saveSettings();
211 return $info ? $info : true;