revert r106095, fix apparently not this simple
[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 public function __construct() {
31 parent::__construct( 'ChangeEmail' );
32 }
33
34 function isListed() {
35 global $wgAuth;
36 return $wgAuth->allowPropChange( 'emailaddress' );
37 }
38
39 /**
40 * Main execution point
41 */
42 function execute( $par ) {
43 global $wgAuth;
44
45 $this->checkReadOnly();
46
47 $this->setHeaders();
48 $this->outputHeader();
49
50 if ( !$wgAuth->allowPropChange( 'emailaddress' ) ) {
51 $this->error( 'cannotchangeemail' );
52 return;
53 }
54
55 $user = $this->getUser();
56 $request = $this->getRequest();
57
58 if ( !$request->wasPosted() && !$user->isLoggedIn() ) {
59 $this->error( 'changeemail-no-info' );
60 return;
61 }
62
63 if ( $request->wasPosted() && $request->getBool( 'wpCancel' ) ) {
64 $this->doReturnTo();
65 return;
66 }
67
68 $out = $this->getOutput();
69 $out->disallowUserJs();
70
71 $this->mPassword = $request->getVal( 'wpPassword' );
72 $this->mNewEmail = $request->getVal( 'wpNewEmail' );
73
74 if ( $request->wasPosted()
75 && $user->matchEditToken( $request->getVal( 'token' ) ) )
76 {
77 $info = $this->attemptChange( $user, $this->mPassword, $this->mNewEmail );
78 if ( $info === true ) {
79 $this->doReturnTo();
80 } elseif ( $info === 'eauth' ) {
81 # Notify user that a confirmation email has been sent...
82 $out->wrapWikiMsg( "<div class='error' style='clear: both;'>\n$1\n</div>",
83 'eauthentsent', $user->getName() );
84 $this->doReturnTo( 'soft' ); // just show the link to go back
85 return; // skip form
86 }
87 }
88
89 $this->showForm();
90 }
91
92 protected function doReturnTo( $type = 'hard' ) {
93 $titleObj = Title::newFromText( $this->getRequest()->getVal( 'returnto' ) );
94 if ( !$titleObj instanceof Title ) {
95 $titleObj = Title::newMainPage();
96 }
97 if ( $type == 'hard' ) {
98 $this->getOutput()->redirect( $titleObj->getFullURL() );
99 } else {
100 $this->getOutput()->addReturnTo( $titleObj );
101 }
102 }
103
104 protected function error( $msg ) {
105 $this->getOutput()->wrapWikiMsg( "<p class='error'>\n$1\n</p>", $msg );
106 }
107
108 protected function showForm() {
109 $user = $this->getUser();
110
111 $oldEmailText = $user->getEmail()
112 ? $user->getEmail()
113 : $this->msg( 'changeemail-none' )->text();
114
115 $this->getOutput()->addHTML(
116 Xml::fieldset( $this->msg( 'changeemail-header' )->text() ) .
117 Xml::openElement( 'form',
118 array(
119 'method' => 'post',
120 'action' => $this->getTitle()->getLocalUrl(),
121 'id' => 'mw-changeemail-form' ) ) . "\n" .
122 Html::hidden( 'token', $user->getEditToken() ) . "\n" .
123 Html::hidden( 'returnto', $this->getRequest()->getVal( 'returnto' ) ) . "\n" .
124 $this->msg( 'changeemail-text' )->parseAsBlock() . "\n" .
125 Xml::openElement( 'table', array( 'id' => 'mw-changeemail-table' ) ) . "\n" .
126 $this->pretty( array(
127 array( 'wpName', 'username', 'text', $user->getName() ),
128 array( 'wpOldEmail', 'changeemail-oldemail', 'text', $oldEmailText ),
129 array( 'wpNewEmail', 'changeemail-newemail', 'input', $this->mNewEmail ),
130 array( 'wpPassword', 'yourpassword', 'password', $this->mPassword ),
131 ) ) . "\n" .
132 "<tr>\n" .
133 "<td></td>\n" .
134 '<td class="mw-input">' .
135 Xml::submitButton( $this->msg( 'changeemail-submit' )->text() ) .
136 Xml::submitButton( $this->msg( 'changeemail-cancel' )->text(), array( 'name' => 'wpCancel' ) ) .
137 "</td>\n" .
138 "</tr>\n" .
139 Xml::closeElement( 'table' ) .
140 Xml::closeElement( 'form' ) .
141 Xml::closeElement( 'fieldset' ) . "\n"
142 );
143 }
144
145 protected function pretty( $fields ) {
146 $out = '';
147 foreach ( $fields as $list ) {
148 list( $name, $label, $type, $value ) = $list;
149 if( $type == 'text' ) {
150 $field = htmlspecialchars( $value );
151 } else {
152 $attribs = array( 'id' => $name );
153 if ( $name == 'wpPassword' ) {
154 $attribs[] = 'autofocus';
155 }
156 $field = Html::input( $name, $value, $type, $attribs );
157 }
158 $out .= "<tr>\n";
159 $out .= "\t<td class='mw-label'>";
160 if ( $type != 'text' ) {
161 $out .= Xml::label( $this->msg( $label )->text(), $name );
162 } else {
163 $out .= $this->msg( $label )->escaped();
164 }
165 $out .= "</td>\n";
166 $out .= "\t<td class='mw-input'>";
167 $out .= $field;
168 $out .= "</td>\n";
169 $out .= "</tr>";
170 }
171 return $out;
172 }
173
174 /**
175 * @return bool|string true or string on success, false on failure
176 */
177 protected function attemptChange( User $user, $pass, $newaddr ) {
178 if ( $newaddr != '' && !Sanitizer::validateEmail( $newaddr ) ) {
179 $this->error( 'invalidemailaddress' );
180 return false;
181 }
182
183 $throttleCount = LoginForm::incLoginThrottle( $user->getName() );
184 if ( $throttleCount === true ) {
185 $this->error( 'login-throttled' );
186 return false;
187 }
188
189 if ( !$user->checkTemporaryPassword( $pass ) && !$user->checkPassword( $pass ) ) {
190 $this->error( 'wrongpassword' );
191 return false;
192 }
193
194 if ( $throttleCount ) {
195 LoginForm::clearLoginThrottle( $user->getName() );
196 }
197
198 list( $status, $info ) = Preferences::trySetUserEmail( $user, $newaddr );
199 if ( $status !== true ) {
200 if ( $status instanceof Status ) {
201 $this->getOutput()->addHTML(
202 '<p class="error">' .
203 $this->getOutput()->parseInline( $status->getWikiText( $info ) ) .
204 '</p>' );
205 }
206 return false;
207 }
208
209 $user->saveSettings();
210 return $info ? $info : true;
211 }
212 }