Merge "Add WikiFilePage::getForeignCategories() method"
[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 if ( !$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 $this->checkPermissions();
92
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' );
97 }
98
99 $this->mPassword = $request->getVal( 'wpPassword' );
100 $this->mNewEmail = $request->getVal( 'wpNewEmail' );
101
102 if ( $request->wasPosted()
103 && $user->matchEditToken( $request->getVal( 'token' ) )
104 ) {
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
114 }
115 }
116
117 $this->showForm();
118 }
119
120 /**
121 * @param $type string
122 */
123 protected function doReturnTo( $type = 'hard' ) {
124 $titleObj = Title::newFromText( $this->getRequest()->getVal( 'returnto' ) );
125 if ( !$titleObj instanceof Title ) {
126 $titleObj = Title::newMainPage();
127 }
128 if ( $type == 'hard' ) {
129 $this->getOutput()->redirect( $titleObj->getFullURL() );
130 } else {
131 $this->getOutput()->addReturnTo( $titleObj );
132 }
133 }
134
135 /**
136 * @param $msg string
137 */
138 protected function error( $msg ) {
139 $this->getOutput()->wrapWikiMsg( "<p class='error'>\n$1\n</p>", $msg );
140 }
141
142 protected function showForm() {
143 global $wgRequirePasswordforEmailChange;
144 $user = $this->getUser();
145
146 $oldEmailText = $user->getEmail()
147 ? $user->getEmail()
148 : $this->msg( 'changeemail-none' )->text();
149
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"
161 );
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 ),
166 );
167 if ( $wgRequirePasswordforEmailChange ) {
168 $items[] = array( 'wpPassword', 'changeemail-password', 'password', $this->mPassword );
169 }
170
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"
184 );
185 }
186
187 /**
188 * @param $fields array
189 * @return string
190 */
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';
201 }
202 $field = Html::input( $name, $value, $type, $attribs );
203 }
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();
210 }
211 $out .= "</td>\n";
212 $out .= "\t<td class='mw-input'>";
213 $out .= $field;
214 $out .= "</td>\n";
215 $out .= "</tr>";
216 }
217
218 return $out;
219 }
220
221 /**
222 * @param $user User
223 * @param $pass string
224 * @param $newaddr string
225 * @return bool|string true or string on success, false on failure
226 */
227 protected function attemptChange( User $user, $pass, $newaddr ) {
228 global $wgAuth, $wgPasswordAttemptThrottle;
229
230 if ( $newaddr != '' && !Sanitizer::validateEmail( $newaddr ) ) {
231 $this->error( 'invalidemailaddress' );
232
233 return false;
234 }
235
236 $throttleCount = LoginForm::incLoginThrottle( $user->getName() );
237 if ( $throttleCount === true ) {
238 $lang = $this->getLanguage();
239 $this->error( array( 'login-throttled', $lang->formatDuration( $wgPasswordAttemptThrottle['seconds'] ) ) );
240
241 return false;
242 }
243
244 global $wgRequirePasswordforEmailChange;
245 if ( $wgRequirePasswordforEmailChange && !$user->checkTemporaryPassword( $pass ) && !$user->checkPassword( $pass ) ) {
246 $this->error( 'wrongpassword' );
247
248 return false;
249 }
250
251 if ( $throttleCount ) {
252 LoginForm::clearLoginThrottle( $user->getName() );
253 }
254
255 $oldaddr = $user->getEmail();
256 $status = $user->setEmailWithConfirmation( $newaddr );
257 if ( !$status->isGood() ) {
258 $this->getOutput()->addHTML(
259 '<p class="error">' .
260 $this->getOutput()->parseInline( $status->getWikiText( 'mailerror' ) ) .
261 '</p>' );
262
263 return false;
264 }
265
266 wfRunHooks( 'PrefsEmailAudit', array( $user, $oldaddr, $newaddr ) );
267
268 $user->saveSettings();
269
270 $wgAuth->updateExternalDB( $user );
271
272 return $status->value;
273 }
274
275 protected function getGroupName() {
276 return 'users';
277 }
278 }