Merge "Kill 'newmessageslink' and 'newmessagesdifflink' messages"
[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', 'editmyprivateinfo' );
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 $this->requireLogin( 'changeemail-no-info' );
79
80 if ( $request->wasPosted() && $request->getBool( 'wpCancel' ) ) {
81 $this->doReturnTo();
82
83 return;
84 }
85
86 $this->checkReadOnly();
87 $this->checkPermissions();
88
89 // This could also let someone check the current email address, so
90 // require both permissions.
91 if ( !$user->isAllowed( 'viewmyprivateinfo' ) ) {
92 throw new PermissionsError( 'viewmyprivateinfo' );
93 }
94
95 $this->mPassword = $request->getVal( 'wpPassword' );
96 $this->mNewEmail = $request->getVal( 'wpNewEmail' );
97
98 if ( $request->wasPosted()
99 && $user->matchEditToken( $request->getVal( 'token' ) )
100 ) {
101 $info = $this->attemptChange( $user, $this->mPassword, $this->mNewEmail );
102 if ( $info === true ) {
103 $this->doReturnTo();
104 } elseif ( $info === 'eauth' ) {
105 # Notify user that a confirmation email has been sent...
106 $out->wrapWikiMsg( "<div class='error' style='clear: both;'>\n$1\n</div>",
107 'eauthentsent', $user->getName() );
108 $this->doReturnTo( 'soft' ); // just show the link to go back
109 return; // skip form
110 }
111 }
112
113 $this->showForm();
114 }
115
116 /**
117 * @param $type string
118 */
119 protected function doReturnTo( $type = 'hard' ) {
120 $titleObj = Title::newFromText( $this->getRequest()->getVal( 'returnto' ) );
121 if ( !$titleObj instanceof Title ) {
122 $titleObj = Title::newMainPage();
123 }
124 if ( $type == 'hard' ) {
125 $this->getOutput()->redirect( $titleObj->getFullURL() );
126 } else {
127 $this->getOutput()->addReturnTo( $titleObj );
128 }
129 }
130
131 /**
132 * @param $msg string
133 */
134 protected function error( $msg ) {
135 $this->getOutput()->wrapWikiMsg( "<p class='error'>\n$1\n</p>", $msg );
136 }
137
138 protected function showForm() {
139 global $wgRequirePasswordforEmailChange;
140 $user = $this->getUser();
141
142 $oldEmailText = $user->getEmail()
143 ? $user->getEmail()
144 : $this->msg( 'changeemail-none' )->text();
145
146 $this->getOutput()->addHTML(
147 Xml::fieldset( $this->msg( 'changeemail-header' )->text() ) .
148 Xml::openElement( 'form',
149 array(
150 'method' => 'post',
151 'action' => $this->getTitle()->getLocalURL(),
152 'id' => 'mw-changeemail-form' ) ) . "\n" .
153 Html::hidden( 'token', $user->getEditToken() ) . "\n" .
154 Html::hidden( 'returnto', $this->getRequest()->getVal( 'returnto' ) ) . "\n" .
155 $this->msg( 'changeemail-text' )->parseAsBlock() . "\n" .
156 Xml::openElement( 'table', array( 'id' => 'mw-changeemail-table' ) ) . "\n"
157 );
158 $items = array(
159 array( 'wpName', 'username', 'text', $user->getName() ),
160 array( 'wpOldEmail', 'changeemail-oldemail', 'text', $oldEmailText ),
161 array( 'wpNewEmail', 'changeemail-newemail', 'email', $this->mNewEmail ),
162 );
163 if ( $wgRequirePasswordforEmailChange ) {
164 $items[] = array( 'wpPassword', 'changeemail-password', 'password', $this->mPassword );
165 }
166
167 $this->getOutput()->addHTML(
168 $this->pretty( $items ) .
169 "\n" .
170 "<tr>\n" .
171 "<td></td>\n" .
172 '<td class="mw-input">' .
173 Xml::submitButton( $this->msg( 'changeemail-submit' )->text() ) .
174 Xml::submitButton( $this->msg( 'changeemail-cancel' )->text(), array( 'name' => 'wpCancel' ) ) .
175 "</td>\n" .
176 "</tr>\n" .
177 Xml::closeElement( 'table' ) .
178 Xml::closeElement( 'form' ) .
179 Xml::closeElement( 'fieldset' ) . "\n"
180 );
181 }
182
183 /**
184 * @param $fields array
185 * @return string
186 */
187 protected function pretty( $fields ) {
188 $out = '';
189 foreach ( $fields as $list ) {
190 list( $name, $label, $type, $value ) = $list;
191 if ( $type == 'text' ) {
192 $field = htmlspecialchars( $value );
193 } else {
194 $attribs = array( 'id' => $name );
195 if ( $name == 'wpPassword' ) {
196 $attribs[] = 'autofocus';
197 }
198 $field = Html::input( $name, $value, $type, $attribs );
199 }
200 $out .= "<tr>\n";
201 $out .= "\t<td class='mw-label'>";
202 if ( $type != 'text' ) {
203 $out .= Xml::label( $this->msg( $label )->text(), $name );
204 } else {
205 $out .= $this->msg( $label )->escaped();
206 }
207 $out .= "</td>\n";
208 $out .= "\t<td class='mw-input'>";
209 $out .= $field;
210 $out .= "</td>\n";
211 $out .= "</tr>";
212 }
213
214 return $out;
215 }
216
217 /**
218 * @param $user User
219 * @param $pass string
220 * @param $newaddr string
221 * @return bool|string true or string on success, false on failure
222 */
223 protected function attemptChange( User $user, $pass, $newaddr ) {
224 global $wgAuth, $wgPasswordAttemptThrottle;
225
226 if ( $newaddr != '' && !Sanitizer::validateEmail( $newaddr ) ) {
227 $this->error( 'invalidemailaddress' );
228
229 return false;
230 }
231
232 $throttleCount = LoginForm::incLoginThrottle( $user->getName() );
233 if ( $throttleCount === true ) {
234 $lang = $this->getLanguage();
235 $this->error( array( 'login-throttled', $lang->formatDuration( $wgPasswordAttemptThrottle['seconds'] ) ) );
236
237 return false;
238 }
239
240 global $wgRequirePasswordforEmailChange;
241 if ( $wgRequirePasswordforEmailChange && !$user->checkTemporaryPassword( $pass ) && !$user->checkPassword( $pass ) ) {
242 $this->error( 'wrongpassword' );
243
244 return false;
245 }
246
247 if ( $throttleCount ) {
248 LoginForm::clearLoginThrottle( $user->getName() );
249 }
250
251 $oldaddr = $user->getEmail();
252 $status = $user->setEmailWithConfirmation( $newaddr );
253 if ( !$status->isGood() ) {
254 $this->getOutput()->addHTML(
255 '<p class="error">' .
256 $this->getOutput()->parseInline( $status->getWikiText( 'mailerror' ) ) .
257 '</p>' );
258
259 return false;
260 }
261
262 wfRunHooks( 'PrefsEmailAudit', array( $user, $oldaddr, $newaddr ) );
263
264 $user->saveSettings();
265
266 $wgAuth->updateExternalDB( $user );
267
268 return $status->value;
269 }
270
271 protected function getGroupName() {
272 return 'users';
273 }
274 }