]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-login.php
Wordpress 3.1.4
[autoinstalls/wordpress.git] / wp-login.php
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 run 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  * Outputs the header for the login page.
27  *
28  * @uses do_action() Calls the 'login_head' for outputting HTML in the Log In
29  *              header.
30  * @uses apply_filters() Calls 'login_headerurl' for the top login link.
31  * @uses apply_filters() Calls 'login_headertitle' for the top login title.
32  * @uses apply_filters() Calls 'login_message' on the message to display in the
33  *              header.
34  * @uses $error The error global, which is checked for displaying errors.
35  *
36  * @param string $title Optional. WordPress Log In Page title to display in
37  *              <title/> element.
38  * @param string $message Optional. Message to display in header.
39  * @param WP_Error $wp_error Optional. WordPress Error Object
40  */
41 function login_header($title = 'Log In', $message = '', $wp_error = '') {
42         global $error, $is_iphone, $interim_login, $current_site;
43
44         // Don't index any of these forms
45         add_filter( 'pre_option_blog_public', '__return_zero' );
46         add_action( 'login_head', 'noindex' );
47
48         if ( empty($wp_error) )
49                 $wp_error = new WP_Error();
50
51         // Shake it!
52         $shake_error_codes = array( 'empty_password', 'empty_email', 'invalid_email', 'invalidcombo', 'empty_username', 'invalid_username', 'incorrect_password' );
53         $shake_error_codes = apply_filters( 'shake_error_codes', $shake_error_codes );
54
55         if ( $shake_error_codes && $wp_error->get_error_code() && in_array( $wp_error->get_error_code(), $shake_error_codes ) )
56                 add_action( 'login_head', 'wp_shake_js', 12 );
57
58         ?>
59 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
60 <html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>
61 <head>
62         <title><?php bloginfo('name'); ?> &rsaquo; <?php echo $title; ?></title>
63         <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
64 <?php
65         wp_admin_css( 'login', true );
66         wp_admin_css( 'colors-fresh', true );
67
68         if ( $is_iphone ) { ?>
69         <meta name="viewport" content="width=320; initial-scale=0.9; maximum-scale=1.0; user-scalable=0;" />
70         <style type="text/css" media="screen">
71         form { margin-left: 0px; }
72         #login { margin-top: 20px; }
73         </style>
74 <?php
75         } elseif ( isset($interim_login) && $interim_login ) { ?>
76         <style type="text/css" media="all">
77         .login #login { margin: 20px auto; }
78         </style>
79 <?php
80         }
81
82         do_action( 'login_enqueue_scripts' );
83         do_action( 'login_head' ); ?>
84 </head>
85 <body class="login">
86 <?php   if ( !is_multisite() ) { ?>
87 <div id="login"><h1><a href="<?php echo apply_filters('login_headerurl', 'http://wordpress.org/'); ?>" title="<?php echo apply_filters('login_headertitle', esc_attr__('Powered by WordPress')); ?>"><?php bloginfo('name'); ?></a></h1>
88 <?php   } else { ?>
89 <div id="login"><h1><a href="<?php echo apply_filters('login_headerurl', network_home_url() ); ?>" title="<?php echo apply_filters('login_headertitle', esc_attr($current_site->site_name) ); ?>"><span class="hide"><?php bloginfo('name'); ?></span></a></h1>
90 <?php   }
91
92         $message = apply_filters('login_message', $message);
93         if ( !empty( $message ) ) echo $message . "\n";
94
95         // Incase a plugin uses $error rather than the $errors object
96         if ( !empty( $error ) ) {
97                 $wp_error->add('error', $error);
98                 unset($error);
99         }
100
101         if ( $wp_error->get_error_code() ) {
102                 $errors = '';
103                 $messages = '';
104                 foreach ( $wp_error->get_error_codes() as $code ) {
105                         $severity = $wp_error->get_error_data($code);
106                         foreach ( $wp_error->get_error_messages($code) as $error ) {
107                                 if ( 'message' == $severity )
108                                         $messages .= '  ' . $error . "<br />\n";
109                                 else
110                                         $errors .= '    ' . $error . "<br />\n";
111                         }
112                 }
113                 if ( !empty($errors) )
114                         echo '<div id="login_error">' . apply_filters('login_errors', $errors) . "</div>\n";
115                 if ( !empty($messages) )
116                         echo '<p class="message">' . apply_filters('login_messages', $messages) . "</p>\n";
117         }
118 } // End of login_header()
119
120 /**
121  * Outputs the footer for the login page.
122  *
123  * @param string $input_id Which input to auto-focus
124  */
125 function login_footer($input_id = '') {
126         echo "</div>\n";
127
128         if ( !empty($input_id) ) {
129 ?>
130 <script type="text/javascript">
131 try{document.getElementById('<?php echo $input_id; ?>').focus();}catch(e){}
132 if(typeof wpOnload=='function')wpOnload();
133 </script>
134 <?php
135         }
136 ?>
137 <p id="backtoblog"><a href="<?php bloginfo('url'); ?>/" title="<?php esc_attr_e('Are you lost?') ?>"><?php printf(__('&larr; Back to %s'), get_bloginfo('title', 'display' )); ?></a></p>
138 <?php do_action('login_footer'); ?>
139 </body>
140 </html>
141 <?php
142 }
143
144 function wp_shake_js() {
145         global $is_iphone;
146         if ( $is_iphone )
147                 return;
148 ?>
149 <script type="text/javascript">
150 addLoadEvent = function(func){if(typeof jQuery!="undefined")jQuery(document).ready(func);else if(typeof wpOnload!='function'){wpOnload=func;}else{var oldonload=wpOnload;wpOnload=function(){oldonload();func();}}};
151 function s(id,pos){g(id).left=pos+'px';}
152 function g(id){return document.getElementById(id).style;}
153 function shake(id,a,d){c=a.shift();s(id,c);if(a.length>0){setTimeout(function(){shake(id,a,d);},d);}else{try{g(id).position='static';wp_attempt_focus();}catch(e){}}}
154 addLoadEvent(function(){ var p=new Array(15,30,15,0,-15,-30,-15,0);p=p.concat(p.concat(p));var i=document.forms[0].id;g(i).position='relative';shake(i,p,20);});
155 </script>
156 <?php
157 }
158
159 /**
160  * Handles sending password retrieval email to user.
161  *
162  * @uses $wpdb WordPress Database object
163  *
164  * @return bool|WP_Error True: when finish. WP_Error on error
165  */
166 function retrieve_password() {
167         global $wpdb, $current_site;
168
169         $errors = new WP_Error();
170
171         if ( empty( $_POST['user_login'] ) && empty( $_POST['user_email'] ) )
172                 $errors->add('empty_username', __('<strong>ERROR</strong>: Enter a username or e-mail address.'));
173
174         if ( strpos($_POST['user_login'], '@') ) {
175                 $user_data = get_user_by_email(trim($_POST['user_login']));
176                 if ( empty($user_data) )
177                         $errors->add('invalid_email', __('<strong>ERROR</strong>: There is no user registered with that email address.'));
178         } else {
179                 $login = trim($_POST['user_login']);
180                 $user_data = get_userdatabylogin($login);
181         }
182
183         do_action('lostpassword_post');
184
185         if ( $errors->get_error_code() )
186                 return $errors;
187
188         if ( !$user_data ) {
189                 $errors->add('invalidcombo', __('<strong>ERROR</strong>: Invalid username or e-mail.'));
190                 return $errors;
191         }
192
193         // redefining user_login ensures we return the right case in the email
194         $user_login = $user_data->user_login;
195         $user_email = $user_data->user_email;
196
197         do_action('retreive_password', $user_login);  // Misspelled and deprecated
198         do_action('retrieve_password', $user_login);
199
200         $allow = apply_filters('allow_password_reset', true, $user_data->ID);
201
202         if ( ! $allow )
203                 return new WP_Error('no_password_reset', __('Password reset is not allowed for this user'));
204         else if ( is_wp_error($allow) )
205                 return $allow;
206
207         $key = $wpdb->get_var($wpdb->prepare("SELECT user_activation_key FROM $wpdb->users WHERE user_login = %s", $user_login));
208         if ( empty($key) ) {
209                 // Generate something random for a key...
210                 $key = wp_generate_password(20, false);
211                 do_action('retrieve_password_key', $user_login, $key);
212                 // Now insert the new md5 key into the db
213                 $wpdb->update($wpdb->users, array('user_activation_key' => $key), array('user_login' => $user_login));
214         }
215         $message = __('Someone requested that the password be reset for the following account:') . "\r\n\r\n";
216         $message .= network_site_url() . "\r\n\r\n";
217         $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
218         $message .= __('If this was a mistake, just ignore this email and nothing will happen.') . "\r\n\r\n";
219         $message .= __('To reset your password, visit the following address:') . "\r\n\r\n";
220         $message .= '<' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login') . ">\r\n";
221
222         if ( is_multisite() )
223                 $blogname = $GLOBALS['current_site']->site_name;
224         else
225                 // The blogname option is escaped with esc_html on the way into the database in sanitize_option
226                 // we want to reverse this for the plain text arena of emails.
227                 $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
228
229         $title = sprintf( __('[%s] Password Reset'), $blogname );
230
231         $title = apply_filters('retrieve_password_title', $title);
232         $message = apply_filters('retrieve_password_message', $message, $key);
233
234         if ( $message && !wp_mail($user_email, $title, $message) )
235                 wp_die( __('The e-mail could not be sent.') . "<br />\n" . __('Possible reason: your host may have disabled the mail() function...') );
236
237         return true;
238 }
239
240 /**
241  * Retrieves a user row based on password reset key and login
242  *
243  * @uses $wpdb WordPress Database object
244  *
245  * @param string $key Hash to validate sending user's password
246  * @param string $login The user login
247  *
248  * @return object|WP_Error
249  */
250 function check_password_reset_key($key, $login) {
251         global $wpdb;
252
253         $key = preg_replace('/[^a-z0-9]/i', '', $key);
254
255         if ( empty( $key ) || !is_string( $key ) )
256                 return new WP_Error('invalid_key', __('Invalid key'));
257
258         if ( empty($login) || !is_string($login) )
259                 return new WP_Error('invalid_key', __('Invalid key'));
260
261         $user = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->users WHERE user_activation_key = %s AND user_login = %s", $key, $login));
262
263         if ( empty( $user ) )
264                 return new WP_Error('invalid_key', __('Invalid key'));
265
266         return $user;
267 }
268
269 /**
270  * Handles resetting the user's password.
271  *
272  * @uses $wpdb WordPress Database object
273  *
274  * @param string $key Hash to validate sending user's password
275  */
276 function reset_password($user, $new_pass) {
277         do_action('password_reset', $user, $new_pass);
278
279         wp_set_password($new_pass, $user->ID);
280
281         wp_password_change_notification($user);
282 }
283
284 /**
285  * Handles registering a new user.
286  *
287  * @param string $user_login User's username for logging in
288  * @param string $user_email User's email address to send password and add
289  * @return int|WP_Error Either user's ID or error on failure.
290  */
291 function register_new_user( $user_login, $user_email ) {
292         $errors = new WP_Error();
293
294         $sanitized_user_login = sanitize_user( $user_login );
295         $user_email = apply_filters( 'user_registration_email', $user_email );
296
297         // Check the username
298         if ( $sanitized_user_login == '' ) {
299                 $errors->add( 'empty_username', __( '<strong>ERROR</strong>: Please enter a username.' ) );
300         } elseif ( ! validate_username( $user_login ) ) {
301                 $errors->add( 'invalid_username', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' ) );
302                 $sanitized_user_login = '';
303         } elseif ( username_exists( $sanitized_user_login ) ) {
304                 $errors->add( 'username_exists', __( '<strong>ERROR</strong>: This username is already registered, please choose another one.' ) );
305         }
306
307         // Check the e-mail address
308         if ( $user_email == '' ) {
309                 $errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please type your e-mail address.' ) );
310         } elseif ( ! is_email( $user_email ) ) {
311                 $errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The email address isn&#8217;t correct.' ) );
312                 $user_email = '';
313         } elseif ( email_exists( $user_email ) ) {
314                 $errors->add( 'email_exists', __( '<strong>ERROR</strong>: This email is already registered, please choose another one.' ) );
315         }
316
317         do_action( 'register_post', $sanitized_user_login, $user_email, $errors );
318
319         $errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email );
320
321         if ( $errors->get_error_code() )
322                 return $errors;
323
324         $user_pass = wp_generate_password( 12, false);
325         $user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email );
326         if ( ! $user_id ) {
327                 $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' ) ) );
328                 return $errors;
329         }
330
331         update_user_option( $user_id, 'default_password_nag', true, true ); //Set up the Password change nag.
332
333         wp_new_user_notification( $user_id, $user_pass );
334
335         return $user_id;
336 }
337
338 //
339 // Main
340 //
341
342 $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'login';
343 $errors = new WP_Error();
344
345 if ( isset($_GET['key']) )
346         $action = 'resetpass';
347
348 // validate action so as to default to the login screen
349 if ( !in_array($action, array('logout', 'lostpassword', 'retrievepassword', 'resetpass', 'rp', 'register', 'login'), true) && false === has_filter('login_form_' . $action) )
350         $action = 'login';
351
352 nocache_headers();
353
354 header('Content-Type: '.get_bloginfo('html_type').'; charset='.get_bloginfo('charset'));
355
356 if ( defined('RELOCATE') ) { // Move flag is set
357         if ( isset( $_SERVER['PATH_INFO'] ) && ($_SERVER['PATH_INFO'] != $_SERVER['PHP_SELF']) )
358                 $_SERVER['PHP_SELF'] = str_replace( $_SERVER['PATH_INFO'], '', $_SERVER['PHP_SELF'] );
359
360         $schema = is_ssl() ? 'https://' : 'http://';
361         if ( dirname($schema . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']) != get_option('siteurl') )
362                 update_option('siteurl', dirname($schema . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']) );
363 }
364
365 //Set a cookie now to see if they are supported by the browser.
366 setcookie(TEST_COOKIE, 'WP Cookie check', 0, COOKIEPATH, COOKIE_DOMAIN);
367 if ( SITECOOKIEPATH != COOKIEPATH )
368         setcookie(TEST_COOKIE, 'WP Cookie check', 0, SITECOOKIEPATH, COOKIE_DOMAIN);
369
370 // allow plugins to override the default actions, and to add extra actions if they want
371 do_action( 'login_init' );
372 do_action( 'login_form_' . $action );
373
374 $http_post = ('POST' == $_SERVER['REQUEST_METHOD']);
375 switch ($action) {
376
377 case 'logout' :
378         check_admin_referer('log-out');
379         wp_logout();
380
381         $redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : 'wp-login.php?loggedout=true';
382         wp_safe_redirect( $redirect_to );
383         exit();
384
385 break;
386
387 case 'lostpassword' :
388 case 'retrievepassword' :
389
390         if ( $http_post ) {
391                 $errors = retrieve_password();
392                 if ( !is_wp_error($errors) ) {
393                         $redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : 'wp-login.php?checkemail=confirm';
394                         wp_safe_redirect( $redirect_to );
395                         exit();
396                 }
397         }
398
399         if ( isset($_GET['error']) && 'invalidkey' == $_GET['error'] ) $errors->add('invalidkey', __('Sorry, that key does not appear to be valid.'));
400         $redirect_to = apply_filters( 'lostpassword_redirect', !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '' );
401
402         do_action('lost_password');
403         login_header(__('Lost Password'), '<p class="message">' . __('Please enter your username or email address. You will receive a link to create a new password via email.') . '</p>', $errors);
404
405         $user_login = isset($_POST['user_login']) ? stripslashes($_POST['user_login']) : '';
406
407 ?>
408
409 <form name="lostpasswordform" id="lostpasswordform" action="<?php echo site_url('wp-login.php?action=lostpassword', 'login_post') ?>" method="post">
410         <p>
411                 <label><?php _e('Username or E-mail:') ?><br />
412                 <input type="text" name="user_login" id="user_login" class="input" value="<?php echo esc_attr($user_login); ?>" size="20" tabindex="10" /></label>
413         </p>
414 <?php do_action('lostpassword_form'); ?>
415         <input type="hidden" name="redirect_to" value="<?php echo esc_attr( $redirect_to ); ?>" />
416         <p class="submit"><input type="submit" name="wp-submit" id="wp-submit" class="button-primary" value="<?php esc_attr_e('Get New Password'); ?>" tabindex="100" /></p>
417 </form>
418
419 <p id="nav">
420 <a href="<?php echo site_url('wp-login.php', 'login') ?>"><?php _e('Log in') ?></a>
421 <?php if (get_option('users_can_register')) : ?>
422  | <a href="<?php echo site_url('wp-login.php?action=register', 'login') ?>"><?php _e('Register') ?></a>
423 <?php endif; ?>
424 </p>
425
426 <?php
427 login_footer('user_login');
428 break;
429
430 case 'resetpass' :
431 case 'rp' :
432         $user = check_password_reset_key($_GET['key'], $_GET['login']);
433
434         if ( is_wp_error($user) ) {
435                 wp_redirect( site_url('wp-login.php?action=lostpassword&error=invalidkey') );
436                 exit;
437         }
438
439         $errors = '';
440
441         if ( isset($_POST['pass1']) && $_POST['pass1'] != $_POST['pass2'] ) {
442                 $errors = new WP_Error('password_reset_mismatch', __('The passwords do not match.'));
443         } elseif ( isset($_POST['pass1']) && !empty($_POST['pass1']) ) {
444                 reset_password($user, $_POST['pass1']);
445                 login_header(__('Password Reset'), '<p class="message reset-pass">' . __('Your password has been reset.') . ' <a href="' . site_url('wp-login.php', 'login') . '">' . __('Log in') . '</a></p>');
446                 login_footer();
447                 exit;
448         }
449
450         wp_enqueue_script('utils');
451         wp_enqueue_script('user-profile');
452
453         login_header(__('Reset Password'), '<p class="message reset-pass">' . __('Enter your new password below.') . '</p>', $errors );
454
455 ?>
456 <form name="resetpassform" id="resetpassform" action="<?php echo site_url('wp-login.php?action=resetpass&key=' . urlencode($_GET['key']) . '&login=' . urlencode($_GET['login']), 'login_post') ?>" method="post">
457         <input type="hidden" id="user_login" value="<?php echo esc_attr( $_GET['login'] ); ?>" autocomplete="off" />
458
459         <p>
460                 <label><?php _e('New password') ?><br />
461                 <input type="password" name="pass1" id="pass1" class="input" size="20" value="" autocomplete="off" /></label>
462         </p>
463         <p>
464                 <label><?php _e('Confirm new password') ?><br />
465                 <input type="password" name="pass2" id="pass2" class="input" size="20" value="" autocomplete="off" /></label>
466         </p>
467
468         <div id="pass-strength-result" class="hide-if-no-js"><?php _e('Strength indicator'); ?></div>
469         <p class="description indicator-hint"><?php _e('Hint: The password should be at least seven characters long. To make it stronger, use upper and lower case letters, numbers and symbols like ! " ? $ % ^ &amp; ).'); ?></p>
470
471         <br class="clear" />
472         <p class="submit"><input type="submit" name="wp-submit" id="wp-submit" class="button-primary" value="<?php esc_attr_e('Reset Password'); ?>" tabindex="100" /></p>
473 </form>
474
475 <p id="nav">
476 <a href="<?php echo site_url('wp-login.php', 'login') ?>"><?php _e('Log in') ?></a>
477 <?php if (get_option('users_can_register')) : ?>
478  | <a href="<?php echo site_url('wp-login.php?action=register', 'login') ?>"><?php _e('Register') ?></a>
479 <?php endif; ?>
480 </p>
481
482 <?php
483 login_footer('user_pass');
484 break;
485
486 case 'register' :
487         if ( is_multisite() ) {
488                 // Multisite uses wp-signup.php
489                 wp_redirect( apply_filters( 'wp_signup_location', site_url('wp-signup.php') ) );
490                 exit;
491         }
492
493         if ( !get_option('users_can_register') ) {
494                 wp_redirect( site_url('wp-login.php?registration=disabled') );
495                 exit();
496         }
497
498         $user_login = '';
499         $user_email = '';
500         if ( $http_post ) {
501                 $user_login = $_POST['user_login'];
502                 $user_email = $_POST['user_email'];
503                 $errors = register_new_user($user_login, $user_email);
504                 if ( !is_wp_error($errors) ) {
505                         $redirect_to = !empty( $_POST['redirect_to'] ) ? $_POST['redirect_to'] : 'wp-login.php?checkemail=registered';
506                         wp_safe_redirect( $redirect_to );
507                         exit();
508                 }
509         }
510
511         $redirect_to = apply_filters( 'registration_redirect', !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '' );
512         login_header(__('Registration Form'), '<p class="message register">' . __('Register For This Site') . '</p>', $errors);
513 ?>
514
515 <form name="registerform" id="registerform" action="<?php echo site_url('wp-login.php?action=register', 'login_post') ?>" method="post">
516         <p>
517                 <label><?php _e('Username') ?><br />
518                 <input type="text" name="user_login" id="user_login" class="input" value="<?php echo esc_attr(stripslashes($user_login)); ?>" size="20" tabindex="10" /></label>
519         </p>
520         <p>
521                 <label><?php _e('E-mail') ?><br />
522                 <input type="text" name="user_email" id="user_email" class="input" value="<?php echo esc_attr(stripslashes($user_email)); ?>" size="25" tabindex="20" /></label>
523         </p>
524 <?php do_action('register_form'); ?>
525         <p id="reg_passmail"><?php _e('A password will be e-mailed to you.') ?></p>
526         <br class="clear" />
527         <input type="hidden" name="redirect_to" value="<?php echo esc_attr( $redirect_to ); ?>" />
528         <p class="submit"><input type="submit" name="wp-submit" id="wp-submit" class="button-primary" value="<?php esc_attr_e('Register'); ?>" tabindex="100" /></p>
529 </form>
530
531 <p id="nav">
532 <a href="<?php echo site_url('wp-login.php', 'login') ?>"><?php _e('Log in') ?></a> |
533 <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>
534 </p>
535
536 <?php
537 login_footer('user_login');
538 break;
539
540 case 'login' :
541 default:
542         $secure_cookie = '';
543         $interim_login = isset($_REQUEST['interim-login']);
544
545         // If the user wants ssl but the session is not ssl, force a secure cookie.
546         if ( !empty($_POST['log']) && !force_ssl_admin() ) {
547                 $user_name = sanitize_user($_POST['log']);
548                 if ( $user = get_userdatabylogin($user_name) ) {
549                         if ( get_user_option('use_ssl', $user->ID) ) {
550                                 $secure_cookie = true;
551                                 force_ssl_admin(true);
552                         }
553                 }
554         }
555
556         if ( isset( $_REQUEST['redirect_to'] ) ) {
557                 $redirect_to = $_REQUEST['redirect_to'];
558                 // Redirect to https if user wants ssl
559                 if ( $secure_cookie && false !== strpos($redirect_to, 'wp-admin') )
560                         $redirect_to = preg_replace('|^http://|', 'https://', $redirect_to);
561         } else {
562                 $redirect_to = admin_url();
563         }
564
565         $reauth = empty($_REQUEST['reauth']) ? false : true;
566
567         // If the user was redirected to a secure login form from a non-secure admin page, and secure login is required but secure admin is not, then don't use a secure
568         // cookie and redirect back to the referring non-secure admin page.  This allows logins to always be POSTed over SSL while allowing the user to choose visiting
569         // the admin via http or https.
570         if ( !$secure_cookie && is_ssl() && force_ssl_login() && !force_ssl_admin() && ( 0 !== strpos($redirect_to, 'https') ) && ( 0 === strpos($redirect_to, 'http') ) )
571                 $secure_cookie = false;
572
573         $user = wp_signon('', $secure_cookie);
574
575         $redirect_to = apply_filters('login_redirect', $redirect_to, isset( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '', $user);
576
577         if ( !is_wp_error($user) && !$reauth ) {
578                 if ( $interim_login ) {
579                         $message = '<p class="message">' . __('You have logged in successfully.') . '</p>';
580                         login_header( '', $message ); ?>
581                         <script type="text/javascript">setTimeout( function(){window.close()}, 8000);</script>
582                         <p class="alignright">
583                         <input type="button" class="button-primary" value="<?php esc_attr_e('Close'); ?>" onclick="window.close()" /></p>
584                         </div></body></html>
585 <?php           exit;
586                 }
587
588                 if ( ( empty( $redirect_to ) || $redirect_to == 'wp-admin/' || $redirect_to == admin_url() ) ) {
589                         // If the user doesn't belong to a blog, send them to user admin. If the user can't edit posts, send them to their profile.
590                         if ( is_multisite() && !get_active_blog_for_user($user->id) )
591                                 $redirect_to = user_admin_url();
592                         elseif ( is_multisite() && !$user->has_cap('read') )
593                                 $redirect_to = get_dashboard_url( $user->id );
594                         elseif ( !$user->has_cap('edit_posts') )
595                                 $redirect_to = admin_url('profile.php');
596                 }
597                 wp_safe_redirect($redirect_to);
598                 exit();
599         }
600
601         $errors = $user;
602         // Clear errors if loggedout is set.
603         if ( !empty($_GET['loggedout']) || $reauth )
604                 $errors = new WP_Error();
605
606         // If cookies are disabled we can't log in even with a valid user+pass
607         if ( isset($_POST['testcookie']) && empty($_COOKIE[TEST_COOKIE]) )
608                 $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."));
609
610         // Some parts of this script use the main login form to display a message
611         if              ( isset($_GET['loggedout']) && TRUE == $_GET['loggedout'] )
612                 $errors->add('loggedout', __('You are now logged out.'), 'message');
613         elseif  ( isset($_GET['registration']) && 'disabled' == $_GET['registration'] )
614                 $errors->add('registerdisabled', __('User registration is currently not allowed.'));
615         elseif  ( isset($_GET['checkemail']) && 'confirm' == $_GET['checkemail'] )
616                 $errors->add('confirm', __('Check your e-mail for the confirmation link.'), 'message');
617         elseif  ( isset($_GET['checkemail']) && 'newpass' == $_GET['checkemail'] )
618                 $errors->add('newpass', __('Check your e-mail for your new password.'), 'message');
619         elseif  ( isset($_GET['checkemail']) && 'registered' == $_GET['checkemail'] )
620                 $errors->add('registered', __('Registration complete. Please check your e-mail.'), 'message');
621         elseif  ( $interim_login )
622                 $errors->add('expired', __('Your session has expired. Please log-in again.'), 'message');
623
624         // Clear any stale cookies.
625         if ( $reauth )
626                 wp_clear_auth_cookie();
627
628         login_header(__('Log In'), '', $errors);
629
630         if ( isset($_POST['log']) )
631                 $user_login = ( 'incorrect_password' == $errors->get_error_code() || 'empty_password' == $errors->get_error_code() ) ? esc_attr(stripslashes($_POST['log'])) : '';
632         $rememberme = ! empty( $_POST['rememberme'] );
633 ?>
634
635 <form name="loginform" id="loginform" action="<?php echo site_url('wp-login.php', 'login_post') ?>" method="post">
636         <p>
637                 <label><?php _e('Username') ?><br />
638                 <input type="text" name="log" id="user_login" class="input" value="<?php echo esc_attr($user_login); ?>" size="20" tabindex="10" /></label>
639         </p>
640         <p>
641                 <label><?php _e('Password') ?><br />
642                 <input type="password" name="pwd" id="user_pass" class="input" value="" size="20" tabindex="20" /></label>
643         </p>
644 <?php do_action('login_form'); ?>
645         <p class="forgetmenot"><label><input name="rememberme" type="checkbox" id="rememberme" value="forever" tabindex="90"<?php checked( $rememberme ); ?> /> <?php esc_attr_e('Remember Me'); ?></label></p>
646         <p class="submit">
647                 <input type="submit" name="wp-submit" id="wp-submit" class="button-primary" value="<?php esc_attr_e('Log In'); ?>" tabindex="100" />
648 <?php   if ( $interim_login ) { ?>
649                 <input type="hidden" name="interim-login" value="1" />
650 <?php   } else { ?>
651                 <input type="hidden" name="redirect_to" value="<?php echo esc_attr($redirect_to); ?>" />
652 <?php   } ?>
653                 <input type="hidden" name="testcookie" value="1" />
654         </p>
655 </form>
656
657 <?php if ( !$interim_login ) { ?>
658 <p id="nav">
659 <?php if ( isset($_GET['checkemail']) && in_array( $_GET['checkemail'], array('confirm', 'newpass') ) ) : ?>
660 <?php elseif ( get_option('users_can_register') ) : ?>
661 <a href="<?php echo site_url('wp-login.php?action=register', 'login') ?>"><?php _e('Register') ?></a> |
662 <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>
663 <?php else : ?>
664 <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>
665 <?php endif; ?>
666 </p>
667 </div>
668 <p id="backtoblog"><a href="<?php bloginfo('url'); ?>/" title="<?php esc_attr_e('Are you lost?') ?>"><?php printf(__('&larr; Back to %s'), get_bloginfo('title', 'display' )); ?></a></p>
669 <?php } else { ?>
670 </div>
671 <?php } ?>
672
673 <script type="text/javascript">
674 function wp_attempt_focus(){
675 setTimeout( function(){ try{
676 <?php if ( $user_login || $interim_login ) { ?>
677 d = document.getElementById('user_pass');
678 d.value = '';
679 <?php } else { ?>
680 d = document.getElementById('user_login');
681 <?php if ( 'invalid_username' == $errors->get_error_code() ) { ?>
682 if( d.value != '' )
683 d.value = '';
684 <?php
685 }
686 }?>
687 d.focus();
688 d.select();
689 } catch(e){}
690 }, 200);
691 }
692
693 <?php if ( !$error ) { ?>
694 wp_attempt_focus();
695 <?php } ?>
696 if(typeof wpOnload=='function')wpOnload();
697 </script>
698 <?php do_action( 'login_footer' ); ?>
699 </body>
700 </html>
701 <?php
702
703 break;
704 } // end action switch
705 ?>