root/trunk/wp-login.php

Revision 8411, 18.8 kB (checked in by ryan, 2 days ago)

Fix case. fixes #7344 for trunk

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 /**
3  * WordPress User Page
4  *
5  * Handles authentication, registering, resetting passwords, forgot password,
6  * and other user handling.
7  *
8  * @package WordPress
9  */
10
11 /** Make sure that the WordPress bootstrap has ran before continuing. */
12 require( dirname(__FILE__) . '/wp-load.php' );
13
14 // Redirect to https login if forced to use SSL
15 if ( force_ssl_admin() && !is_ssl() ) {
16     if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) {
17         wp_redirect(preg_replace('|^http://|', 'https://', $_SERVER['REQUEST_URI']));
18         exit();
19     } else {
20         wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
21         exit();           
22     }
23 }
24
25 /**
26  * login_header() - Outputs the header for the login page
27  *
28  * @package WordPress
29  * @uses do_action() Calls the 'login_head' for outputting HTML in the Login
30  *        header.
31  * @uses apply_filters() Calls 'login_headerurl' for the top login link.
32  * @uses apply_filters() Calls 'login_headertitle' for the top login title.
33  * @uses apply_filters() Calls 'login_message' on the message to display in the
34  *        header.
35  * @uses $error The error global, which is checked for displaying errors.
36  *
37  * @param string $title Optional. WordPress Login Page title to display in
38  *        <title/> element.
39  * @param string $message Optional. Message to display in header.
40  * @param WP_Error $wp_error Optional. WordPress Error Object
41  */
42 function login_header($title = 'Login', $message = '', $wp_error = '') {
43     global $error;
44
45     if ( empty($wp_error) )
46         $wp_error = new WP_Error();
47     ?>
48 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
49 <html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>
50 <head>
51     <title><?php bloginfo('name'); ?> &rsaquo; <?php echo $title; ?></title>
52     <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
53     <?php
54     wp_admin_css( 'login', true );
55     wp_admin_css( 'colors-fresh', true );
56     do_action('login_head'); ?>
57 </head>
58 <body class="login">
59
60 <div id="login"><h1><a href="<?php echo apply_filters('login_headerurl', 'http://wordpress.org/'); ?>" title="<?php echo apply_filters('login_headertitle', __('Powered by WordPress')); ?>"><?php bloginfo('name'); ?></a></h1>
61 <?php
62     if ( !empty( $message ) ) echo apply_filters('login_message', $message) . "\n";
63
64     // Incase a plugin uses $error rather than the $errors object
65     if ( !empty( $error ) ) {
66         $wp_error->add('error', $error);
67         unset($error);
68     }
69
70     if ( $wp_error->get_error_code() ) {
71         $errors = '';
72         $messages = '';
73         foreach ( $wp_error->get_error_codes() as $code ) {
74             $severity = $wp_error->get_error_data($code);
75             foreach ( $wp_error->get_error_messages($code) as $error ) {
76                 if ( 'message' == $severity )
77                     $messages .= '    ' . $error . "<br />\n";
78                 else
79                     $errors .= '    ' . $error . "<br />\n";
80             }
81         }
82         if ( !empty($errors) )
83             echo '<div id="login_error">' . apply_filters('login_errors', $errors) . "</div>\n";
84         if ( !empty($messages) )
85             echo '<p class="message">' . apply_filters('login_messages', $messages) . "</p>\n";
86     }
87 } // End of login_header()
88
89 /**
90  * retrieve_password() - Handles sending password retrieval email to user
91  *
92  * {@internal Missing Long Description}}
93  *
94  * @uses $wpdb WordPress Database object
95  *
96  * @return bool|WP_Error True: when finish. WP_Error on error
97  */
98 function retrieve_password() {
99     global $wpdb;
100
101     $errors = new WP_Error();
102
103     if ( empty( $_POST['user_login'] ) && empty( $_POST['user_email'] ) )
104         $errors->add('empty_username', __('<strong>ERROR</strong>: Enter a username or e-mail address.'));
105
106     if ( strstr($_POST['user_login'], '@') ) {
107         $user_data = get_user_by_email(trim($_POST['user_login']));
108         if ( empty($user_data) )
109             $errors->add('invalid_email', __('<strong>ERROR</strong>: There is no user registered with that email address.'));
110     } else {
111         $login = trim($_POST['user_login']);
112         $user_data = get_userdatabylogin($login);
113     }
114
115     do_action('lostpassword_post');
116
117     if ( $errors->get_error_code() )
118         return $errors;
119
120     if ( !$user_data ) {
121         $errors->add('invalidcombo', __('<strong>ERROR</strong>: Invalid username or e-mail.'));
122         return $errors;
123     }
124
125     // redefining user_login ensures we return the right case in the email
126     $user_login = $user_data->user_login;
127     $user_email = $user_data->user_email;
128
129     do_action('retreive_password', $user_login);  // Misspelled and deprecated
130     do_action('retrieve_password', $user_login);
131
132     $allow = apply_filters('allow_password_reset', true, $user_data->ID);
133
134     if ( ! $allow )
135         return new WP_Error('no_password_reset', __('Password reset is not allowed for this user'));
136     else if ( is_wp_error($allow) )
137         return $allow;
138         
139     $key = $wpdb->get_var($wpdb->prepare("SELECT user_activation_key FROM $wpdb->users WHERE user_login = %s", $user_login));
140     if ( empty($key) ) {
141         // Generate something random for a key...
142         $key = wp_generate_password(20, false);
143         do_action('retrieve_password_key', $user_login, $key);
144         // Now insert the new md5 key into the db
145         $wpdb->query($wpdb->prepare("UPDATE $wpdb->users SET user_activation_key = %s WHERE user_login = %s", $key, $user_login));
146     }
147     $message = __('Someone has asked to reset the password for the following site and username.') . "\r\n\r\n";
148     $message .= get_option('siteurl') . "\r\n\r\n";
149     $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
150     $message .= __('To reset your password visit the following address, otherwise just ignore this email and nothing will happen.') . "\r\n\r\n";
151     $message .= site_url("wp-login.php?action=rp&key=$key", 'login') . "\r\n";
152
153     if ( !wp_mail($user_email, sprintf(__('[%s] Password Reset'), get_option('blogname')), $message) )
154         die('<p>' . __('The e-mail could not be sent.') . "<br />\n" . __('Possible reason: your host may have disabled the mail() function...') . '</p>');
155
156     return true;
157 }
158
159 /**
160  * reset_password() - Handles resetting the user's password
161  *
162  * {@internal Missing Long Description}}
163  *
164  * @uses $wpdb WordPress Database object
165  *
166  * @param string $key Hash to validate sending user's password
167  * @return bool|WP_Error
168  */
169 function reset_password($key) {
170     global $wpdb;
171
172     $key = preg_replace('/[^a-z0-9]/i', '', $key);
173
174     if ( empty( $key ) )
175         return new WP_Error('invalid_key', __('Invalid key'));
176
177     $user = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->users WHERE user_activation_key = %s", $key));
178     if ( empty( $user ) )
179         return new WP_Error('invalid_key', __('Invalid key'));
180
181     do_action('password_reset', $user);
182
183     // Generate something random for a password...
184     $new_pass = wp_generate_password();
185     wp_set_password($new_pass, $user->ID);
186     $message  = sprintf(__('Username: %s'), $user->user_login) . "\r\n";
187     $message .= sprintf(__('Password: %s'), $new_pass) . "\r\n";
188     $message .= site_url('wp-login.php', 'login') . "\r\n";
189
190     if (  !wp_mail($user->user_email, sprintf(__('[%s] Your new password'), get_option('blogname')), $message) )
191         die('<p>' . __('The e-mail could not be sent.') . "<br />\n" . __('Possible reason: your host may have disabled the mail() function...') . '</p>');
192
193     // send a copy of password change notification to the admin
194     // but check to see if it's the admin whose password we're changing, and skip this
195     if ( $user->user_email != get_option('admin_email') ) {
196         $message = sprintf(__('Password Lost and Changed for user: %s'), $user->user_login) . "\r\n";
197         wp_mail(get_option('admin_email'), sprintf(__('[%s] Password Lost/Changed'), get_option('blogname')), $message);
198     }
199
200     return true;
201 }
202
203 /**
204  * register_new_user() - Handles registering a new user
205  *
206  * {@internal Missing Long Description}}
207  *
208  * @param string $user_login User's username for logging in
209  * @param string $user_email User's email address to send password and add
210  * @return int|WP_Error Either user's ID or error on failure.
211  */
212 function register_new_user($user_login, $user_email) {
213     $errors = new WP_Error();
214
215     $user_login = sanitize_user( $user_login );
216     $user_email = apply_filters( 'user_registration_email', $user_email );
217
218     // Check the username
219     if ( $user_login == '' )
220         $errors->add('empty_username', __('<strong>ERROR</strong>: Please enter a username.'));
221     elseif ( !validate_username( $user_login ) ) {
222         $errors->add('invalid_username', __('<strong>ERROR</strong>: This username is invalid.  Please enter a valid username.'));
223         $user_login = '';
224     } elseif ( username_exists( $user_login ) )
225         $errors->add('username_exists', __('<strong>ERROR</strong>: This username is already registered, please choose another one.'));
226
227     // Check the e-mail address
228     if ($user_email == '') {
229         $errors->add('empty_email', __('<strong>ERROR</strong>: Please type your e-mail address.'));
230     } elseif ( !is_email( $user_email ) ) {
231         $errors->add('invalid_email', __('<strong>ERROR</strong>: The email address isn&#8217;t correct.'));
232         $user_email = '';
233     } elseif ( email_exists( $user_email ) )
234         $errors->add('email_exists', __('<strong>ERROR</strong>: This email is already registered, please choose another one.'));
235
236     do_action('register_post', $user_login, $user_email, $errors);
237
238     $errors = apply_filters( 'registration_errors', $errors );
239
240     if ( $errors->get_error_code() )
241         return $errors;
242
243     $user_pass = wp_generate_password();
244     $user_id = wp_create_user( $user_login, $user_pass, $user_email );
245     if ( !$user_id ) {
246         $errors->add('registerfail', sprintf(__('<strong>ERROR</strong>: Couldn&#8217;t register you... please contact the <a href="mailto:%s">webmaster</a> !'), get_option('admin_email')));
247         return $errors;
248     }
249
250     wp_new_user_notification($user_id, $user_pass);
251
252     return $user_id;
253 }
254
255 //
256 // Main
257 //
258
259 $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
260 $errors = new WP_Error();
261
262 if ( isset($_GET['key']) )
263     $action = 'resetpass';
264
265 nocache_headers();
266
267 header('Content-Type: '.get_bloginfo('html_type').'; charset='.get_bloginfo('charset'));
268
269 if ( defined('RELOCATE') ) { // Move flag is set
270     if ( isset( $_SERVER['PATH_INFO'] ) && ($_SERVER['PATH_INFO'] != $_SERVER['PHP_SELF']) )
271         $_SERVER['PHP_SELF'] = str_replace( $_SERVER['PATH_INFO'], '', $_SERVER['PHP_SELF'] );
272
273     $schema = ( isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https://' : 'http://';
274     if ( dirname($schema . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']) != get_option('siteurl') )
275         update_option('siteurl', dirname($schema . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']) );
276 }
277
278 //Set a cookie now to see if they are supported by the browser.
279 setcookie(TEST_COOKIE, 'WP Cookie check', 0, COOKIEPATH, COOKIE_DOMAIN);
280 if ( SITECOOKIEPATH != COOKIEPATH )
281     setcookie(TEST_COOKIE, 'WP Cookie check', 0, SITECOOKIEPATH, COOKIE_DOMAIN);
282
283 $http_post = ('POST' == $_SERVER['REQUEST_METHOD']);
284 switch ($action) {
285
286 case 'logout' :
287
288     wp_logout();
289
290     $redirect_to = 'wp-login.php?loggedout=true';
291     if ( isset( $_REQUEST['redirect_to'] ) )
292         $redirect_to = $_REQUEST['redirect_to'];
293
294     wp_safe_redirect($redirect_to);
295     exit();
296
297 break;
298
299 case 'lostpassword' :
300 case 'retrievepassword' :
301     if ( $http_post ) {
302         $errors = retrieve_password();
303         if ( !is_wp_error($errors) ) {
304             wp_redirect('wp-login.php?checkemail=confirm');
305             exit();
306         }
307     }
308
309     if ( 'invalidkey' == $_GET['error'] ) $errors->add('invalidkey', __('Sorry, that key does not appear to be valid.'));
310
311     do_action('lost_password');
312     login_header(__('Lost Password'), '<p class="message">' . __('Please enter your username or e-mail address. You will receive a new password via e-mail.') . '</p>', $errors);
313 ?>
314
315 <form name="lostpasswordform" id="lostpasswordform" action="<?php echo site_url('wp-login.php?action=lostpassword', 'login_post') ?>" method="post">
316     <p>
317         <label><?php _e('Username or E-mail:') ?><br />
318         <input type="text" name="user_login" id="user_login" class="input" value="<?php echo attribute_escape(stripslashes($_POST['user_login'])); ?>" size="20" tabindex="10" /></label>
319     </p>
320 <?php do_action('lostpassword_form'); ?>
321     <p class="submit"><input type="submit" name="wp-submit" id="wp-submit" value="<?php _e('Get New Password'); ?>" tabindex="100" /></p>
322 </form>
323
324 <p id="nav">
325 <?php if (get_option('users_can_register')) : ?>
326 <a href="<?php echo site_url('wp-login.php', 'login') ?>"><?php _e('Log in') ?></a> |
327 <a href="<?php echo site_url('wp-login.php?action=register', 'login') ?>"><?php _e('Register') ?></a>
328 <?php else : ?>
329 <a href="<?php echo site_url('wp-login.php', 'login') ?>"><?php _e('Log in') ?></a>
330 <?php endif; ?>
331 </p>
332
333 </div>
334
335 <p id="backtoblog"><a href="<?php bloginfo('url'); ?>/" title="<?php _e('Are you lost?') ?>"><?php printf(__('&laquo; Back to %s'), get_bloginfo('title', 'display' )); ?></a></p>
336
337 <script type="text/javascript">
338 try{document.getElementById('user_login').focus();}catch(e){}
339 </script>
340 </body>
341 </html>
342 <?php
343 break;
344
345 case 'resetpass' :
346 case 'rp' :
347     $errors = reset_password($_GET['key']);
348
349     if ( ! is_wp_error($errors) ) {
350         wp_redirect('wp-login.php?checkemail=newpass');
351         exit();
352     }
353
354     wp_redirect('wp-login.php?action=lostpassword&error=invalidkey');
355     exit();
356
357 break;
358
359 case 'register' :
360     if ( !get_option('users_can_register') ) {
361         wp_redirect('wp-login.php?registration=disabled');
362         exit();
363     }
364
365     $user_login = '';
366     $user_email = '';
367     if ( $http_post ) {
368         require_once( ABSPATH . WPINC . '/registration.php');
369
370         $user_login = $_POST['user_login'];
371         $user_email = $_POST['user_email'];
372         $errors = register_new_user($user_login, $user_email);
373         if ( !is_wp_error($errors) ) {
374             wp_redirect('wp-login.php?checkemail=registered');
375             exit();
376         }
377     }
378
379     login_header(__('Registration Form'), '<p class="message register">' . __('Register For This Site') . '</p>', $errors);
380 ?>
381
382 <form name="registerform" id="registerform" action="<?php echo site_url('wp-login.php?action=register', 'login_post') ?>" method="post">
383     <p>
384         <label><?php _e('Username') ?><br />
385         <input type="text" name="user_login" id="user_login" class="input" value="<?php echo attribute_escape(stripslashes($user_login)); ?>" size="20" tabindex="10" /></label>
386     </p>
387     <p>
388         <label><?php _e('E-mail') ?><br />
389         <input type="text" name="user_email" id="user_email" class="input" value="<?php echo attribute_escape(stripslashes($user_email)); ?>" size="25" tabindex="20" /></label>
390     </p>
391 <?php do_action('register_form'); ?>
392     <p id="reg_passmail"><?php _e('A password will be e-mailed to you.') ?></p>
393     <p class="submit"><input type="submit" name="wp-submit" id="wp-submit" value="<?php _e('Register'); ?>" tabindex="100" /></p>
394 </form>
395
396 <p id="nav">
397 <a href="<?php echo site_url('wp-login.php', 'login') ?>"><?php _e('Log in') ?></a> |
398 <a href="<?php echo site_url('wp-login.php?action=lostpassword', 'login') ?>" title="<?php _e('Password Lost and Found') ?>"><?php _e('Lost your password?') ?></a>
399 </p>
400
401 </div>
402
403 <p id="backtoblog"><a href="<?php bloginfo('url'); ?>/" title="<?php _e('Are you lost?') ?>"><?php printf(__('&laquo; Back to %s'), get_bloginfo('title', 'display' )); ?></a></p>
404
405 <script type="text/javascript">
406 try{document.getElementById('user_login').focus();}catch(e){}
407 </script>
408 </body>
409 </html>
410 <?php
411 break;
412
413 case 'login' :
414 default:
415     if ( isset( $_REQUEST['redirect_to'] ) )
416         $redirect_to = $_REQUEST['redirect_to'];
417     else
418         $redirect_to = admin_url();
419
420     if ( is_ssl() && force_ssl_login() && !force_ssl_admin() && ( 0 !== strpos($redirect_to, 'https') ) && ( 0 === strpos($redirect_to, 'http') ) )
421         $secure_cookie = false;
422     else
423         $secure_cookie = '';
424
425     $user = wp_signon('', $secure_cookie);
426
427     if ( !is_wp_error($user) ) {
428         // If the user can't edit posts, send them to their profile.
429         if ( !$user->has_cap('edit_posts') && ( empty( $redirect_to ) || $redirect_to == 'wp-admin/' ) )
430             $redirect_to = admin_url('profile.php');
431         wp_safe_redirect($redirect_to);
432         exit();
433     }
434
435     $errors = $user;
436     // Clear errors if loggedout is set.
437     if ( !empty($_GET['loggedout']) )
438         $errors = new WP_Error();
439
440     // If cookies are disabled we can't log in even with a valid user+pass
441     if ( isset($_POST['testcookie']) && empty($_COOKIE[TEST_COOKIE]) )
442         $errors->add('test_cookie', __("<strong>ERROR</strong>: Cookies are blocked or not supported by your browser. You must <a href='http://www.google.com/cookies.html'>enable cookies</a> to use WordPress."));
443
444     // Some parts of this script use the main login form to display a message
445     if        ( isset($_GET['loggedout']) && TRUE == $_GET['loggedout'] )            $errors->add('loggedout', __('You are now logged out.'), 'message');
446     elseif    ( isset($_GET['registration']) && 'disabled' == $_GET['registration'] )    $errors->add('registerdiabled', __('User registration is currently not allowed.'));
447     elseif    ( isset($_GET['checkemail']) && 'confirm' == $_GET['checkemail'] )    $errors->add('confirm', __('Check your e-mail for the confirmation link.'), 'message');
448     elseif    ( isset($_GET['checkemail']) && 'newpass' == $_GET['checkemail'] )    $errors->add('newpass', __('Check your e-mail for your new password.'), 'message');
449     elseif    ( isset($_GET['checkemail']) && 'registered' == $_GET['checkemail'] )    $errors->add('registered', __('Registration complete. Please check your e-mail.'), 'message');
450
451     login_header(__('Login'), '', $errors);
452 ?>
453
454 <form name="loginform" id="loginform" action="<?php echo site_url('wp-login.php', 'login_post') ?>" method="post">
455 <?php if ( !isset($_GET['checkemail']) || !in_array( $_GET['checkemail'], array('confirm', 'newpass') ) ) : ?>
456     <p>
457         <label><?php _e('Username') ?><br />
458         <input type="text" name="log" id="user_login" class="input" value="<?php echo attribute_escape(stripslashes($user_login)); ?>" size="20" tabindex="10" /></label>
459     </p>
460     <p>
461         <label><?php _e('Password') ?><br />
462         <input type="password" name="pwd" id="user_pass" class="input" value="" size="20" tabindex="20" /></label>
463     </p>
464 <?php do_action('login_form'); ?>
465     <p class="forgetmenot"><label><input name="rememberme" type="checkbox" id="rememberme" value="forever" tabindex="90" /> <?php _e('Remember Me'); ?></label></p>
466     <p class="submit">
467         <input type="submit" name="wp-submit" id="wp-submit" value="<?php _e('Log In'); ?>" tabindex="100" />
468         <input type="hidden" name="redirect_to" value="<?php echo attribute_escape($redirect_to); ?>" />
469         <input type="hidden" name="testcookie" value="1" />
470     </p>
471 <?php else : ?>
472     <p>&nbsp;</p>
473 <?php endif; ?>
474 </form>
475
476 <p id="nav">
477 <?php if ( isset($_GET['checkemail']) && in_array( $_GET['checkemail'], array('confirm', 'newpass') ) ) : ?>
478 <?php elseif (get_option('users_can_register')) : ?>
479 <a href="<?php echo site_url('wp-login.php?action=register', 'login') ?>"><?php _e('Register') ?></a> |
480 <a href="<?php echo site_url('wp-login.php?action=lostpassword', 'login') ?>" title="<?php _e('Password Lost and Found') ?>"><?php _e('Lost your password?') </