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