191832b69bd6150c023d324d8c465decff6829a5
[lhc/web/wiklou.git] / includes / specials / SpecialPasswordReset.php
1 <?php
2 /**
3 * Implements Special:Blankpage
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 * Special page for requesting a password reset email
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialPasswordReset extends FormSpecialPage {
30
31 public function __construct() {
32 parent::__construct( 'PasswordReset' );
33 }
34
35 public function userCanExecute( User $user ) {
36 global $wgPasswordResetRoutes, $wgAuth;
37
38 $error = $this->canChangePassword( $user );
39 if ( is_string( $error ) ) {
40 throw new ErrorPageError( 'internalerror', $error );
41 } else if ( !$error ) {
42 throw new ErrorPageError( 'internalerror', 'resetpass_forbidden' );
43 }
44
45 return parent::userCanExecute( $user );
46 }
47
48 protected function getFormFields() {
49 global $wgPasswordResetRoutes;
50 $a = array();
51 if ( isset( $wgPasswordResetRoutes['username'] ) && $wgPasswordResetRoutes['username'] ) {
52 $a['Username'] = array(
53 'type' => 'text',
54 'label-message' => 'passwordreset-username',
55 );
56 }
57
58 if ( isset( $wgPasswordResetRoutes['email'] ) && $wgPasswordResetRoutes['email'] ) {
59 $a['Email'] = array(
60 'type' => 'email',
61 'label-message' => 'passwordreset-email',
62 );
63 }
64
65 return $a;
66 }
67
68 public function alterForm( HTMLForm $form ) {
69 $form->setSubmitText( wfMessage( "mailmypassword" ) );
70 }
71
72 protected function preText() {
73 global $wgPasswordResetRoutes;
74 $i = 0;
75 if ( isset( $wgPasswordResetRoutes['username'] ) && $wgPasswordResetRoutes['username'] ) {
76 $i++;
77 }
78 if ( isset( $wgPasswordResetRoutes['email'] ) && $wgPasswordResetRoutes['email'] ) {
79 $i++;
80 }
81 return wfMessage( 'passwordreset-pretext', $i )->parseAsBlock();
82 }
83
84 /**
85 * Process the form. At this point we know that the user passes all the criteria in
86 * userCanExecute(), and if the data array contains 'Username', etc, then Username
87 * resets are allowed.
88 * @param $data array
89 * @return Bool|Array
90 */
91 public function onSubmit( array $data ) {
92
93 if ( isset( $data['Username'] ) && $data['Username'] !== '' ) {
94 $method = 'username';
95 $users = array( User::newFromName( $data['Username'] ) );
96 } elseif ( isset( $data['Email'] )
97 && $data['Email'] !== ''
98 && Sanitizer::validateEmail( $data['Email'] ) )
99 {
100 $method = 'email';
101 $res = wfGetDB( DB_SLAVE )->select(
102 'user',
103 '*',
104 array( 'user_email' => $data['Email'] ),
105 __METHOD__
106 );
107 if ( $res ) {
108 $users = array();
109 foreach( $res as $row ){
110 $users[] = User::newFromRow( $row );
111 }
112 } else {
113 // Some sort of database error, probably unreachable
114 throw new MWException( 'Unknown database error in ' . __METHOD__ );
115 }
116 } else {
117 // The user didn't supply any data
118 return false;
119 }
120
121 // Check for hooks (captcha etc), and allow them to modify the users list
122 $error = array();
123 if ( !wfRunHooks( 'SpecialPasswordResetOnSubmit', array( &$users, $data, &$error ) ) ) {
124 return array( $error );
125 }
126
127 if( count( $users ) == 0 ){
128 if( $method == 'email' ){
129 // Don't reveal whether or not an email address is in use
130 return true;
131 } else {
132 return array( 'noname' );
133 }
134 }
135
136 $firstUser = $users[0];
137
138 if ( !$firstUser instanceof User || !$firstUser->getID() ) {
139 return array( array( 'nosuchuser', $data['Username'] ) );
140 }
141
142 // Check against the rate limiter
143 if ( $this->getUser()->pingLimiter( 'mailpassword' ) ) {
144 throw new ThrottledError;
145 }
146
147 // Check against password throttle
148 foreach ( $users as $user ) {
149 if ( $user->isPasswordReminderThrottled() ) {
150 global $wgPasswordReminderResendTime;
151 # Round the time in hours to 3 d.p., in case someone is specifying
152 # minutes or seconds.
153 return array( array( 'throttled-mailpassword', round( $wgPasswordReminderResendTime, 3 ) ) );
154 }
155 }
156
157 global $wgServer, $wgScript, $wgNewPasswordExpiry;
158
159 // All the users will have the same email address
160 if ( $firstUser->getEmail() == '' ) {
161 // This won't be reachable from the email route, so safe to expose the username
162 return array( array( 'noemail', $firstUser->getName() ) );
163 }
164
165 // We need to have a valid IP address for the hook, but per bug 18347, we should
166 // send the user's name if they're logged in.
167 $ip = wfGetIP();
168 if ( !$ip ) {
169 return array( 'badipaddress' );
170 }
171 $caller = $this->getUser();
172 wfRunHooks( 'User::mailPasswordInternal', array( &$caller, &$ip, &$firstUser ) );
173 $username = $caller->getName();
174 $msg = IP::isValid( $username )
175 ? 'passwordreset-emailtext-ip'
176 : 'passwordreset-emailtext-user';
177
178 $passwords = array();
179 foreach ( $users as $user ) {
180 $password = $user->randomPassword();
181 $user->setNewpassword( $password );
182 $user->saveSettings();
183 $passwords[] = wfMessage( 'passwordreset-emailelement', $user->getName(), $password );
184 }
185 $passwordBlock = implode( "\n\n", $passwords );
186
187 // Send in the user's language; which should hopefully be the same
188 $userLanguage = $firstUser->getOption( 'language' );
189
190 $body = wfMessage( $msg )->inLanguage( $userLanguage );
191 $body->params(
192 $username,
193 $passwordBlock,
194 count( $passwords ),
195 $wgServer . $wgScript,
196 round( $wgNewPasswordExpiry / 86400 )
197 );
198
199 $title = wfMessage( 'passwordreset-emailtitle' );
200
201 $result = $firstUser->sendMail( $title->text(), $body->text() );
202
203 if ( $result->isGood() ) {
204 return true;
205 } else {
206 // @todo FIXME: The email didn't send, but we have already set the password throttle
207 // timestamp, so they won't be able to try again until it expires... :(
208 return array( array( 'mailerror', $result->getMessage() ) );
209 }
210 }
211
212 public function onSuccess() {
213 $this->getOutput()->addWikiMsg( 'passwordreset-emailsent' );
214 $this->getOutput()->returnToMain();
215 }
216
217 function canChangePassword(User $user) {
218 global $wgPasswordResetRoutes, $wgAuth;
219
220 // Maybe password resets are disabled, or there are no allowable routes
221 if ( !is_array( $wgPasswordResetRoutes ) ||
222 !in_array( true, array_values( $wgPasswordResetRoutes ) ) ) {
223 return 'passwordreset-disabled';
224 }
225
226 // Maybe the external auth plugin won't allow local password changes
227 if ( !$wgAuth->allowPasswordChange() ) {
228 return 'resetpass_forbidden';
229 }
230
231 // Maybe the user is blocked (check this here rather than relying on the parent
232 // method as we have a more specific error message to use here
233 if ( $user->isBlocked() ) {
234 return 'blocked-mailpassword';
235 }
236
237 return true;
238 }
239
240
241 /**
242 * Hide the password reset page if resets are disabled.
243 * @return Bool
244 */
245 function isListed() {
246 global $wgPasswordResetRoutes, $wgAuth, $wgUser;
247
248 if ( $this->canChangePassword( $wgUser ) === true ) {
249 return parent::isListed();
250 }
251
252 return false;
253 }
254 }