Whitelist the <wbr> element.
[mediawiki.git] / includes / specials / SpecialChangeEmail.php
blob816938d6e8f0279f132f962e4da74b191ef9f6e1
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
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();
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 ) {
107 $this->doReturnTo();
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
113 return; // skip form
117 $this->showForm();
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() );
130 } else {
131 $this->getOutput()->addReturnTo( $titleObj );
136 * @param $msg string
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()
147 ? $user->getEmail()
148 : $this->msg( 'changeemail-none' )->text();
150 $this->getOutput()->addHTML(
151 Xml::fieldset( $this->msg( 'changeemail-header' )->text() ) .
152 Xml::openElement( 'form',
153 array(
154 'method' => 'post',
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"
162 $items = array(
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 ) .
173 "\n" .
174 "<tr>\n" .
175 "<td></td>\n" .
176 '<td class="mw-input">' .
177 Xml::submitButton( $this->msg( 'changeemail-submit' )->text() ) .
178 Xml::submitButton( $this->msg( 'changeemail-cancel' )->text(), array( 'name' => 'wpCancel' ) ) .
179 "</td>\n" .
180 "</tr>\n" .
181 Xml::closeElement( 'table' ) .
182 Xml::closeElement( 'form' ) .
183 Xml::closeElement( 'fieldset' ) . "\n"
188 * @param $fields array
189 * @return string
191 protected function pretty( $fields ) {
192 $out = '';
193 foreach ( $fields as $list ) {
194 list( $name, $label, $type, $value ) = $list;
195 if ( $type == 'text' ) {
196 $field = htmlspecialchars( $value );
197 } else {
198 $attribs = array( 'id' => $name );
199 if ( $name == 'wpPassword' ) {
200 $attribs[] = 'autofocus';
202 $field = Html::input( $name, $value, $type, $attribs );
204 $out .= "<tr>\n";
205 $out .= "\t<td class='mw-label'>";
206 if ( $type != 'text' ) {
207 $out .= Xml::label( $this->msg( $label )->text(), $name );
208 } else {
209 $out .= $this->msg( $label )->escaped();
211 $out .= "</td>\n";
212 $out .= "\t<td class='mw-input'>";
213 $out .= $field;
214 $out .= "</td>\n";
215 $out .= "</tr>";
218 return $out;
222 * @param $user User
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;
230 if ( $newaddr != '' && !Sanitizer::validateEmail( $newaddr ) ) {
231 $this->error( 'invalidemailaddress' );
233 return false;
236 $throttleCount = LoginForm::incLoginThrottle( $user->getName() );
237 if ( $throttleCount === true ) {
238 $this->error( 'login-throttled' );
240 return false;
243 global $wgRequirePasswordforEmailChange;
244 if ( $wgRequirePasswordforEmailChange && !$user->checkTemporaryPassword( $pass ) && !$user->checkPassword( $pass ) ) {
245 $this->error( 'wrongpassword' );
247 return false;
250 if ( $throttleCount ) {
251 LoginForm::clearLoginThrottle( $user->getName() );
254 $oldaddr = $user->getEmail();
255 $status = $user->setEmailWithConfirmation( $newaddr );
256 if ( !$status->isGood() ) {
257 $this->getOutput()->addHTML(
258 '<p class="error">' .
259 $this->getOutput()->parseInline( $status->getWikiText( 'mailerror' ) ) .
260 '</p>' );
262 return false;
265 wfRunHooks( 'PrefsEmailAudit', array( $user, $oldaddr, $newaddr ) );
267 $user->saveSettings();
269 $wgAuth->updateExternalDB( $user );
271 return $status->value;
274 protected function getGroupName() {
275 return 'users';