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