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