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