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