Merge "Added a function to LoginForm to show the "return to" page."
[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
31 /**
32 * Users password
33 * @var string
34 */
35 protected $mPassword;
36
37 /**
38 * Users new email address
39 * @var string
40 */
41 protected $mNewEmail;
42
43 public function __construct() {
44 parent::__construct( 'ChangeEmail' );
45 }
46
47 /**
48 * @return Bool
49 */
50 function isListed() {
51 global $wgAuth;
52
53 return $wgAuth->allowPropChange( 'emailaddress' );
54 }
55
56 /**
57 * Main execution point
58 */
59 function execute( $par ) {
60 global $wgAuth;
61
62 $this->setHeaders();
63 $this->outputHeader();
64
65 $out = $this->getOutput();
66 $out->disallowUserJs();
67 $out->addModules( 'mediawiki.special.changeemail' );
68
69 if ( !$wgAuth->allowPropChange( 'emailaddress' ) ) {
70 $this->error( 'cannotchangeemail' );
71
72 return;
73 }
74
75 $user = $this->getUser();
76 $request = $this->getRequest();
77
78 if ( !$request->wasPosted() && !$user->isLoggedIn() ) {
79 $this->error( 'changeemail-no-info' );
80
81 return;
82 }
83
84 if ( $request->wasPosted() && $request->getBool( 'wpCancel' ) ) {
85 $this->doReturnTo();
86
87 return;
88 }
89
90 $this->checkReadOnly();
91
92 $this->mPassword = $request->getVal( 'wpPassword' );
93 $this->mNewEmail = $request->getVal( 'wpNewEmail' );
94
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
107 }
108 }
109
110 $this->showForm();
111 }
112
113 /**
114 * @param $type string
115 */
116 protected function doReturnTo( $type = 'hard' ) {
117 $titleObj = Title::newFromText( $this->getRequest()->getVal( 'returnto' ) );
118 if ( !$titleObj instanceof Title ) {
119 $titleObj = Title::newMainPage();
120 }
121 if ( $type == 'hard' ) {
122 $this->getOutput()->redirect( $titleObj->getFullURL() );
123 } else {
124 $this->getOutput()->addReturnTo( $titleObj );
125 }
126 }
127
128 /**
129 * @param $msg string
130 */
131 protected function error( $msg ) {
132 $this->getOutput()->wrapWikiMsg( "<p class='error'>\n$1\n</p>", $msg );
133 }
134
135 protected function showForm() {
136 global $wgRequirePasswordforEmailChange;
137 $user = $this->getUser();
138
139 $oldEmailText = $user->getEmail()
140 ? $user->getEmail()
141 : $this->msg( 'changeemail-none' )->text();
142
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"
154 );
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 ),
159 );
160 if ( $wgRequirePasswordforEmailChange ) {
161 $items[] = array( 'wpPassword', 'changeemail-password', 'password', $this->mPassword );
162 }
163
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"
177 );
178 }
179
180 /**
181 * @param $fields array
182 * @return string
183 */
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';
194 }
195 $field = Html::input( $name, $value, $type, $attribs );
196 }
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();
203 }
204 $out .= "</td>\n";
205 $out .= "\t<td class='mw-input'>";
206 $out .= $field;
207 $out .= "</td>\n";
208 $out .= "</tr>";
209 }
210
211 return $out;
212 }
213
214 /**
215 * @param $user User
216 * @param $pass string
217 * @param $newaddr string
218 * @return bool|string true or string on success, false on failure
219 */
220 protected function attemptChange( User $user, $pass, $newaddr ) {
221 global $wgAuth;
222
223 if ( $newaddr != '' && !Sanitizer::validateEmail( $newaddr ) ) {
224 $this->error( 'invalidemailaddress' );
225
226 return false;
227 }
228
229 $throttleCount = LoginForm::incLoginThrottle( $user->getName() );
230 if ( $throttleCount === true ) {
231 $this->error( 'login-throttled' );
232
233 return false;
234 }
235
236 global $wgRequirePasswordforEmailChange;
237 if ( $wgRequirePasswordforEmailChange && !$user->checkTemporaryPassword( $pass ) && !$user->checkPassword( $pass ) ) {
238 $this->error( 'wrongpassword' );
239
240 return false;
241 }
242
243 if ( $throttleCount ) {
244 LoginForm::clearLoginThrottle( $user->getName() );
245 }
246
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>' );
254
255 return false;
256 }
257
258 wfRunHooks( 'PrefsEmailAudit', array( $user, $oldaddr, $newaddr ) );
259
260 $user->saveSettings();
261
262 $wgAuth->updateExternalDB( $user );
263
264 return $status->value;
265 }
266
267 protected function getGroupName() {
268 return 'users';
269 }
270 }