5 * Handles authentication, registering, resetting passwords, forgot password,
6 * and other user handling.
11 /** Make sure that the WordPress bootstrap has ran before continuing. */
12 require( dirname(__FILE__) . '/wp-load.php' );
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']));
20 wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
26 * login_header() - Outputs the header for the login page
29 * @uses do_action() Calls the 'login_head' for outputting HTML in the Login
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
35 * @uses $error The error global, which is checked for displaying errors.
37 * @param string $title Optional. WordPress Login Page title to display in
39 * @param string $message Optional. Message to display in header.
40 * @param WP_Error $wp_error Optional. WordPress Error Object
42 function login_header($title = 'Login', $message = '', $wp_error = '') {
45 if ( empty($wp_error) )
46 $wp_error = new WP_Error();
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(); ?>>
51 <title><?php bloginfo('name'); ?> › <?php echo $title; ?></title>
52 <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
54 wp_admin_css( 'login', true );
55 wp_admin_css( 'colors-fresh', true );
56 do_action('login_head'); ?>
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>
62 if ( !empty( $message ) ) echo apply_filters('login_message', $message) . "\n";
64 // Incase a plugin uses $error rather than the $errors object
65 if ( !empty( $error ) ) {
66 $wp_error->add('error', $error);
70 if ( $wp_error->get_error_code() ) {
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";
79 $errors .= ' ' . $error . "<br />\n";
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";
87 } // End of login_header()
90 * retrieve_password() - Handles sending password retrieval email to user
92 * {@internal Missing Long Description}}
94 * @uses $wpdb WordPress Database object
96 * @return bool|WP_Error True: when finish. WP_Error on error
98 function retrieve_password() {
101 $errors = new WP_Error();
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.'));
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.'));
111 $login = trim($_POST['user_login']);
112 $user_data = get_userdatabylogin($login);
115 do_action('lostpassword_post');
117 if ( $errors->get_error_code() )
121 $errors->add('invalidcombo', __('<strong>ERROR</strong>: Invalid username or e-mail.'));
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;
129 do_action('retreive_password', $user_login); // Misspelled and deprecated
130 do_action('retrieve_password', $user_login);
132 $allow = apply_filters('allow_password_reset', true, $user_data->ID);
135 return new WP_Error('no_password_reset', __('Password reset is not allowed for this user'));
136 else if ( is_wp_error($allow) )
139 $key = $wpdb->get_var($wpdb->prepare("SELECT user_activation_key FROM $wpdb->users WHERE user_login = %s", $user_login));
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));
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";
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>');
160 * reset_password() - Handles resetting the user's password
162 * {@internal Missing Long Description}}
164 * @uses $wpdb WordPress Database object
166 * @param string $key Hash to validate sending user's password
167 * @return bool|WP_Error
169 function reset_password($key) {
172 $key = preg_replace('/[^a-z0-9]/i', '', $key);
175 return new WP_Error('invalid_key', __('Invalid key'));
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'));
181 do_action('password_reset', $user);
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";
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>');
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);
204 * register_new_user() - Handles registering a new user
206 * {@internal Missing Long Description}}
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.
212 function register_new_user($user_login, $user_email) {
213 $errors = new WP_Error();
215 $user_login = sanitize_user( $user_login );
216 $user_email = apply_filters( 'user_registration_email', $user_email );
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.'));
224 } elseif ( username_exists( $user_login ) )
225 $errors->add('username_exists', __('<strong>ERROR</strong>: This username is already registered, please choose another one.'));
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’t correct.'));
233 } elseif ( email_exists( $user_email ) )
234 $errors->add('email_exists', __('<strong>ERROR</strong>: This email is already registered, please choose another one.'));
236 do_action('register_post', $user_login, $user_email, $errors);
238 $errors = apply_filters( 'registration_errors', $errors );
240 if ( $errors->get_error_code() )
243 $user_pass = wp_generate_password();
244 $user_id = wp_create_user( $user_login, $user_pass, $user_email );
246 $errors->add('registerfail', sprintf(__('<strong>ERROR</strong>: Couldn’t register you... please contact the <a href="mailto:%s">webmaster</a> !'), get_option('admin_email')));
250 wp_new_user_notification($user_id, $user_pass);
259 $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
260 $errors = new WP_Error();
262 if ( isset($_GET['key']) )
263 $action = 'resetpass';
267 header('Content-Type: '.get_bloginfo('html_type').'; charset='.get_bloginfo('charset'));
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'] );
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']) );
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);
283 $http_post = ('POST' == $_SERVER['REQUEST_METHOD']);
290 $redirect_to = 'wp-login.php?loggedout=true';
291 if ( isset( $_REQUEST['redirect_to'] ) )
292 $redirect_to = $_REQUEST['redirect_to'];
294 wp_safe_redirect($redirect_to);
299 case 'lostpassword' :
300 case 'retrievepassword' :
302 $errors = retrieve_password();
303 if ( !is_wp_error($errors) ) {
304 wp_redirect('wp-login.php?checkemail=confirm');
309 if ( 'invalidkey' == $_GET['error'] ) $errors->add('invalidkey', __('Sorry, that key does not appear to be valid.'));
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);
315 <form name="lostpasswordform" id="lostpasswordform" action="<?php echo site_url('wp-login.php?action=lostpassword', 'login_post') ?>" method="post">
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>
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>
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>
329 <a href="<?php echo site_url('wp-login.php', 'login') ?>"><?php _e('Log in') ?></a>
335 <p id="backtoblog"><a href="<?php bloginfo('url'); ?>/" title="<?php _e('Are you lost?') ?>"><?php printf(__('« Back to %s'), get_bloginfo('title', 'display' )); ?></a></p>
337 <script type="text/javascript">
338 try{document.getElementById('user_login').focus();}catch(e){}
347 $errors = reset_password($_GET['key']);
349 if ( ! is_wp_error($errors) ) {
350 wp_redirect('wp-login.php?checkemail=newpass');
354 wp_redirect('wp-login.php?action=lostpassword&error=invalidkey');
360 if ( !get_option('users_can_register') ) {
361 wp_redirect('wp-login.php?registration=disabled');
368 require_once( ABSPATH . WPINC . '/registration.php');
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');
379 login_header(__('Registration Form'), '<p class="message register">' . __('Register For This Site') . '</p>', $errors);
382 <form name="registerform" id="registerform" action="<?php echo site_url('wp-login.php?action=register', 'login_post') ?>" method="post">
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>
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>
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>
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>
403 <p id="backtoblog"><a href="<?php bloginfo('url'); ?>/" title="<?php _e('Are you lost?') ?>"><?php printf(__('« Back to %s'), get_bloginfo('title', 'display' )); ?></a></p>
405 <script type="text/javascript">
406 try{document.getElementById('user_login').focus();}catch(e){}
415 if ( isset( $_REQUEST['redirect_to'] ) )
416 $redirect_to = $_REQUEST['redirect_to'];
418 $redirect_to = admin_url();
420 if ( is_ssl() && force_ssl_login() && !force_ssl_admin() && ( 0 !== strpos($redirect_to, 'https') ) && ( 0 === strpos($redirect_to, 'http') ) )
421 $secure_cookie = false;
425 $user = wp_signon('', $secure_cookie);
427 $redirect_to = apply_filters('login_redirect', $redirect_to, isset( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '', $user);
429 if ( !is_wp_error($user) ) {
430 // If the user can't edit posts, send them to their profile.
431 if ( !$user->has_cap('edit_posts') && ( empty( $redirect_to ) || $redirect_to == 'wp-admin/' ) )
432 $redirect_to = admin_url('profile.php');
433 wp_safe_redirect($redirect_to);
438 // Clear errors if loggedout is set.
439 if ( !empty($_GET['loggedout']) )
440 $errors = new WP_Error();
442 // If cookies are disabled we can't log in even with a valid user+pass
443 if ( isset($_POST['testcookie']) && empty($_COOKIE[TEST_COOKIE]) )
444 $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."));
446 // Some parts of this script use the main login form to display a message
447 if ( isset($_GET['loggedout']) && TRUE == $_GET['loggedout'] ) $errors->add('loggedout', __('You are now logged out.'), 'message');
448 elseif ( isset($_GET['registration']) && 'disabled' == $_GET['registration'] ) $errors->add('registerdiabled', __('User registration is currently not allowed.'));
449 elseif ( isset($_GET['checkemail']) && 'confirm' == $_GET['checkemail'] ) $errors->add('confirm', __('Check your e-mail for the confirmation link.'), 'message');
450 elseif ( isset($_GET['checkemail']) && 'newpass' == $_GET['checkemail'] ) $errors->add('newpass', __('Check your e-mail for your new password.'), 'message');
451 elseif ( isset($_GET['checkemail']) && 'registered' == $_GET['checkemail'] ) $errors->add('registered', __('Registration complete. Please check your e-mail.'), 'message');
453 login_header(__('Login'), '', $errors);
456 <form name="loginform" id="loginform" action="<?php echo site_url('wp-login.php', 'login_post') ?>" method="post">
457 <?php if ( !isset($_GET['checkemail']) || !in_array( $_GET['checkemail'], array('confirm', 'newpass') ) ) : ?>
459 <label><?php _e('Username') ?><br />
460 <input type="text" name="log" id="user_login" class="input" value="<?php echo attribute_escape(stripslashes($user_login)); ?>" size="20" tabindex="10" /></label>
463 <label><?php _e('Password') ?><br />
464 <input type="password" name="pwd" id="user_pass" class="input" value="" size="20" tabindex="20" /></label>
466 <?php do_action('login_form'); ?>
467 <p class="forgetmenot"><label><input name="rememberme" type="checkbox" id="rememberme" value="forever" tabindex="90" /> <?php _e('Remember Me'); ?></label></p>
469 <input type="submit" name="wp-submit" id="wp-submit" value="<?php _e('Log In'); ?>" tabindex="100" />
470 <input type="hidden" name="redirect_to" value="<?php echo attribute_escape($redirect_to); ?>" />
471 <input type="hidden" name="testcookie" value="1" />
479 <?php if ( isset($_GET['checkemail']) && in_array( $_GET['checkemail'], array('confirm', 'newpass') ) ) : ?>
480 <?php elseif (get_option('users_can_register')) : ?>
481 <a href="<?php echo site_url('wp-login.php?action=register', 'login') ?>"><?php _e('Register') ?></a> |
482 <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>
484 <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>
490 <p id="backtoblog"><a href="<?php bloginfo('url'); ?>/" title="<?php _e('Are you lost?') ?>"><?php printf(__('« Back to %s'), get_bloginfo('title', 'display' )); ?></a></p>
492 <script type="text/javascript">
493 try{document.getElementById('user_login').focus();}catch(e){}
500 } // end action switch