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