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