Back out unreviewed commit 17d23282dd6d8b1bf6b1b80f97cf02265cbec59d
[lhc/web/wiklou.git] / includes / specials / SpecialChangeEmail.php
1 <?php
2 /**
3 * Implements Special:ChangeEmail
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * Let users change their email address.
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialChangeEmail extends UnlistedSpecialPage {
30 public function __construct() {
31 parent::__construct( 'ChangeEmail' );
32 }
33
34 function isListed() {
35 global $wgAuth;
36 return $wgAuth->allowPropChange( 'emailaddress' );
37 }
38
39 /**
40 * Main execution point
41 */
42 function execute( $par ) {
43 global $wgAuth;
44
45 $this->checkReadOnly();
46
47 $this->setHeaders();
48 $this->outputHeader();
49
50 if ( !$wgAuth->allowPropChange( 'emailaddress' ) ) {
51 $this->error( 'cannotchangeemail' );
52 return;
53 }
54
55 $user = $this->getUser();
56 $request = $this->getRequest();
57
58 if ( !$request->wasPosted() && !$user->isLoggedIn() ) {
59 $this->error( 'changeemail-no-info' );
60 return;
61 }
62
63 if ( $request->wasPosted() && $request->getBool( 'wpCancel' ) ) {
64 $this->doReturnTo();
65 return;
66 }
67
68 $out = $this->getOutput();
69 $out->disallowUserJs();
70 $out->addModules( 'mediawiki.special.changeemail' );
71
72 $this->mPassword = $request->getVal( 'wpPassword' );
73 $this->mNewEmail = $request->getVal( 'wpNewEmail' );
74
75 if ( $request->wasPosted()
76 && $user->matchEditToken( $request->getVal( 'token' ) ) )
77 {
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
87 }
88 }
89
90 $this->showForm();
91 }
92
93 protected function doReturnTo( $type = 'hard' ) {
94 $titleObj = Title::newFromText( $this->getRequest()->getVal( 'returnto' ) );
95 if ( !$titleObj instanceof Title ) {
96 $titleObj = Title::newMainPage();
97 }
98 if ( $type == 'hard' ) {
99 $this->getOutput()->redirect( $titleObj->getFullURL() );
100 } else {
101 $this->getOutput()->addReturnTo( $titleObj );
102 }
103 }
104
105 protected function error( $msg ) {
106 $this->getOutput()->wrapWikiMsg( "<p class='error'>\n$1\n</p>", $msg );
107 }
108
109 protected function showForm() {
110 $user = $this->getUser();
111
112 $oldEmailText = $user->getEmail()
113 ? $user->getEmail()
114 : $this->msg( 'changeemail-none' )->text();
115
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"
143 );
144 }
145
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';
156 }
157 $field = Html::input( $name, $value, $type, $attribs );
158 }
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();
165 }
166 $out .= "</td>\n";
167 $out .= "\t<td class='mw-input'>";
168 $out .= $field;
169 $out .= "</td>\n";
170 $out .= "</tr>";
171 }
172 return $out;
173 }
174
175 /**
176 * @return bool|string true or string on success, false on failure
177 */
178 protected function attemptChange( User $user, $pass, $newaddr ) {
179 if ( $newaddr != '' && !Sanitizer::validateEmail( $newaddr ) ) {
180 $this->error( 'invalidemailaddress' );
181 return false;
182 }
183
184 $throttleCount = LoginForm::incLoginThrottle( $user->getName() );
185 if ( $throttleCount === true ) {
186 $this->error( 'login-throttled' );
187 return false;
188 }
189
190 if ( !$user->checkTemporaryPassword( $pass ) && !$user->checkPassword( $pass ) ) {
191 $this->error( 'wrongpassword' );
192 return false;
193 }
194
195 if ( $throttleCount ) {
196 LoginForm::clearLoginThrottle( $user->getName() );
197 }
198
199 $oldaddr = $user->getEmail();
200 $status = $user->setEmailWithConfirmation( $newaddr );
201 if ( !$status->isGood() ) {
202 $this->getOutput()->addHTML(
203 '<p class="error">' .
204 $this->getOutput()->parseInline( $status->getWikiText( $info ) ) .
205 '</p>' );
206 return false;
207 }
208
209 wfRunHooks( 'PrefsEmailAudit', array( $user, $oldaddr, $newaddr ) );
210
211 $user->saveSettings();
212
213 return $status->value;
214 }
215 }