root/trunk/wp-login.php

Revision 1398, 18.7 kB (checked in by donncha, 2 months ago)

id -> ID, fixes #690

  • Property svn:eol-style set to native
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, $current_site;
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://' . $current_site->domain . $current_site->path ); ?>" title="<?php echo apply_filters('login_headertitle', $current_site->site_name ); ?>"><span class="hide"><?php bloginfo('name'); ?></span></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, $current_site;
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 .= 'http://' . trailingslashit( $current_site->domain . $current_site->path ) . "\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 .= 'http://' . trailingslashit( $current_site->domain . $current_site->path ) . "wp-login.php?action=rp&key=$key\r\n";
152
153     if ( !wp_mail($user_email, sprintf(__('[%s] Password Reset'), $current_site->site_name), $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, $current_site;
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'), $current_site->site_name), $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     return true;
194 }
195
196 /**
197  * register_new_user() - Handles registering a new user
198  *
199  * {@internal Missing Long Description}}
200  *
201  * @param string $user_login User's username for logging in
202  * @param string $user_email User's email address to send password and add
203  * @return int|WP_Error Either user's ID or error on failure.
204  */
205 function register_new_user($user_login, $user_email) {
206     $errors = new WP_Error();
207
208     $user_login = sanitize_user( $user_login );
209     $user_email = apply_filters( 'user_registration_email', $user_email );
210
211     // Check the username
212     if ( $user_login == '' )
213         $errors->add('empty_username', __('<strong>ERROR</strong>: Please enter a username.'));
214     elseif ( !validate_username( $user_login ) ) {
215         $errors->add('invalid_username', __('<strong>ERROR</strong>: This username is invalid.  Please enter a valid username.'));
216         $user_login = '';
217     } elseif ( username_exists( $user_login ) )
218         $errors->add('username_exists', __('<strong>ERROR</strong>: This username is already registered, please choose another one.'));
219
220     // Check the e-mail address
221     if ($user_email == '') {
222         $errors->add('empty_email', __('<strong>ERROR</strong>: Please type your e-mail address.'));
223     } elseif ( !is_email( $user_email ) ) {
224         $errors->add('invalid_email', __('<strong>ERROR</strong>: The email address isn&#8217;t correct.'));
225         $user_email = '';
226     } elseif ( email_exists( $user_email ) )
227         $errors->add('email_exists', __('<strong>ERROR</strong>: This email is already registered, please choose another one.'));
228
229     do_action('register_post', $user_login, $user_email, $errors);
230
231     $errors = apply_filters( 'registration_errors', $errors );
232
233     if ( $errors->get_error_code() )
234         return $errors;
235
236     $user_pass = wp_generate_password();
237     $user_id = wp_create_user( $user_login, $user_pass, $user_email );
238     if ( !$user_id ) {
239         $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')));
240         return $errors;
241     }
242
243     wp_new_user_notification($user_id, $user_pass);
244
245     return $user_id;
246 }
247
248 //
249 // Main
250 //
251
252 $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
253 $errors = new WP_Error();
254
255 if ( isset($_GET['key']) )
256     $action = 'resetpass';
257
258 nocache_headers();
259
260 header('Content-Type: '.get_bloginfo('html_type').'; charset='.get_bloginfo('charset'));
261
262 if ( defined('RELOCATE') ) { // Move flag is set
263     if ( isset( $_SERVER['PATH_INFO'] ) && ($_SERVER['PATH_INFO'] != $_SERVER['PHP_SELF']) )
264         $_SERVER['PHP_SELF'] = str_replace( $_SERVER['PATH_INFO'], '', $_SERVER['PHP_SELF'] );
265
266     $schema = ( isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https://' : 'http://';
267     if ( dirname($schema . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']) != get_option('siteurl') )
268         update_option('siteurl', dirname($schema . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']) );
269 }
270
271 //Set a cookie now to see if they are supported by the browser.
272 setcookie(TEST_COOKIE, 'WP Cookie check', 0, COOKIEPATH, COOKIE_DOMAIN);
273 if ( SITECOOKIEPATH != COOKIEPATH )
274     setcookie(TEST_COOKIE, 'WP Cookie check', 0, SITECOOKIEPATH, COOKIE_DOMAIN);
275
276 $http_post = ('POST' == $_SERVER['REQUEST_METHOD']);
277 switch ($action) {
278
279 case 'logout' :
280
281     wp_logout();
282
283     $redirect_to = 'wp-login.php?loggedout=true';
284     if ( isset( $_REQUEST['redirect_to'] ) )
285         $redirect_to = $_REQUEST['redirect_to'];
286
287     wp_safe_redirect($redirect_to);
288     exit();
289
290 break;
291
292 case 'lostpassword' :
293 case 'retrievepassword' :
294     if ( $http_post ) {
295         $errors = retrieve_password();
296         if ( !is_wp_error($errors) ) {
297             wp_redirect('wp-login.php?checkemail=confirm');
298             exit();
299         }
300     }
301
302     if ( 'invalidkey' == $_GET['error'] ) $errors->add('invalidkey', __('Sorry, that key does not appear to be valid.'));
303
304     do_action('lost_password');
305     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);
306 ?>
307
308 <form name="lostpasswordform" id="lostpasswordform" action="<?php echo site_url('wp-login.php?action=lostpassword', 'login_post') ?>" method="post">
309     <p>
310         <label><?php _e('Username or E-mail:') ?><br />
311         <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>
312     </p>
313 <?php do_action('lostpassword_form'); ?>
314     <p class="submit"><input type="submit" name="wp-submit" id="wp-submit" value="<?php _e('Get New Password'); ?>" tabindex="100" /></p>
315 </form>
316
317 <p id="nav">
318 <?php if (get_option('users_can_register')) : ?>
319 <a href="<?php echo site_url('wp-login.php', 'login') ?>"><?php _e('Log in') ?></a> |
320 <a href="<?php echo site_url('wp-login.php?action=register', 'login') ?>"><?php _e('Register') ?></a>
321 <?php else : ?>
322 <a href="<?php echo site_url('wp-login.php', 'login') ?>"><?php _e('Log in') ?></a>
323 <?php endif; ?>
324 </p>
325
326 </div>
327
328 <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>
329
330 <script type="text/javascript">
331 try{document.getElementById('user_login').focus();}catch(e){}
332 </script>
333 </body>
334 </html>
335 <?php
336 break;
337
338 case 'resetpass' :
339 case 'rp' :
340     $errors = reset_password($_GET['key']);
341
342     if ( ! is_wp_error($errors) ) {
343         wp_redirect('wp-login.php?checkemail=newpass');
344         exit();
345     }
346
347     wp_redirect('wp-login.php?action=lostpassword&error=invalidkey');
348     exit();
349
350 break;
351
352 case 'register' :
353     // WPMU doesn't use this
354     wp_redirect( get_bloginfo('wpurl') . '/wp-signup.php' );
355     exit;
356     if ( !get_option('users_can_register') ) {
357         wp_redirect('wp-login.php?registration=disabled');
358         exit();
359     }
360
361     $user_login = '';
362     $user_email = '';
363     if ( $http_post ) {
364         require_once( ABSPATH . WPINC . '/registration.php');
365
366         $user_login = $_POST['user_login'];
367         $user_email = $_POST['user_email'];
368         $errors = register_new_user($user_login, $user_email);
369         if ( !is_wp_error($errors) ) {
370             wp_redirect('wp-login.php?checkemail=registered');
371             exit();
372         }
373     }
374
375     login_header(__('Registration Form'), '<p class="message register">' . __('Register For This Site') . '</p>', $errors);
376 ?>
377
378 <form name="registerform" id="registerform" action="<?php echo site_url('wp-login.php?action=register', 'login_post') ?>" method="post">
379     <p>
380         <label><?php _e('Username') ?><br />
381         <input type="text" name="user_login" id="user_login" class="input" value="<?php echo attribute_escape(stripslashes($user_login)); ?>" size="20" tabindex="10" /></label>
382     </p>
383     <p>
384         <label><?php _e('E-mail') ?><br />
385         <input type="text" name="user_email" id="user_email" class="input" value="<?php echo attribute_escape(stripslashes($user_email)); ?>" size="25" tabindex="20" /></label>
386     </p>
387 <?php do_action('register_form'); ?>
388     <p id="reg_passmail"><?php _e('A password will be e-mailed to you.') ?></p>
389     <p class="submit"><input type="submit" name="wp-submit" id="wp-submit" value="<?php _e('Register'); ?>" tabindex="100" /></p>
390 </form>
391
392 <p id="nav">
393 <a href="<?php echo site_url('wp-login.php', 'login') ?>"><?php _e('Log in') ?></a> |
394 <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>
395 </p>
396
397 </div>
398
399 <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>
400
401 <script type="text/javascript">
402 try{document.getElementById('user_login').focus();}catch(e){}
403 </script>
404 </body>
405 </html>
406 <?php
407 break;
408
409 case 'login' :
410 default:
411     if ( isset( $_REQUEST['redirect_to'] ) )
412         $redirect_to = $_REQUEST['redirect_to'];
413     else
414         $redirect_to = admin_url();
415
416     if ( is_ssl() && force_ssl_login() && !force_ssl_admin() && ( 0 !== strpos($redirect_to, 'https') ) && ( 0 === strpos($redirect_to, 'http') ) )
417         $secure_cookie = false;
418     else
419         $secure_cookie = '';
420
421     $user = wp_signon('', $secure_cookie);
422
423     if ( !is_wp_error($user) ) {
424         // If the user can't edit posts, send them to their profile.
425         if ( !$user->has_cap('edit_posts') && ( empty( $redirect_to ) || $redirect_to == 'wp-admin/' ) )
426             $redirect_to = admin_url('profile.php');
427         wp_safe_redirect($redirect_to);
428         exit();
429     }
430
431     $errors = $user;
432     // Clear errors if loggedout is set.
433     if ( !empty($_GET['loggedout']) )
434         $errors = new WP_Error();
435
436     // If cookies are disabled we can't log in even with a valid user+pass
437     if ( isset($_POST['testcookie']) && empty($_COOKIE[TEST_COOKIE]) )
438         $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."));
439
440     // Some parts of this script use the main login form to display a message
441     if        ( isset($_GET['loggedout']) && TRUE == $_GET['loggedout'] )            $errors->add('loggedout', __('You are now logged out.'), 'message');
442     elseif    ( isset($_GET['registration']) && 'disabled' == $_GET['registration'] )    $errors->add('registerdiabled', __('User registration is currently not allowed.'));
443     elseif    ( isset($_GET['checkemail']) && 'confirm' == $_GET['checkemail'] )    $errors->add('confirm', __('Check your e-mail for the confirmation link.'), 'message');
444     elseif    ( isset($_GET['checkemail']) && 'newpass' == $_GET['checkemail'] )    $errors->add('newpass', __('Check your e-mail for your new password.'), 'message');
445     elseif    ( isset($_GET['checkemail']) && 'registered' == $_GET['checkemail'] )    $errors->add('registered', __('Registration complete. Please check your e-mail.'), 'message');
446
447     login_header(__('Login'), '', $errors);
448 ?>
449
450 <form name="loginform" id="loginform" action="<?php echo site_url('wp-login.php', 'login_post') ?>" method="post">
451 <?php if ( !isset($_GET['checkemail']) || !in_array( $_GET['checkemail'], array('confirm', 'newpass') ) ) : ?>
452     <p>
453         <label><?php _e('Username') ?><br />
454         <input type="text" name="log" id="user_login" class="input" value="<?php echo attribute_escape(stripslashes($user_login)); ?>" size="20" tabindex="10" /></label>
455     </p>
456     <p>
457         <label><?php _e('Password') ?><br />
458         <input type="password" name="pwd" id="user_pass" class="input" value="" size="20" tabindex="20" /></label>
459     </p>
460 <?php do_action('login_form'); ?>
461     <p class="forgetmenot"><label><input name="rememberme" type="checkbox" id="rememberme" value="forever" tabindex="90" /> <?php _e('Remember Me'); ?></label></p>
462     <p class="submit">
463         <input type="submit" name="wp-submit" id="wp-submit" value="<?php _e('Log In'); ?>" tabindex="100" />
464         <input type="hidden" name="redirect_to" value="<?php echo attribute_escape($redirect_to); ?>" />
465         <input type="hidden" name="testcookie" value="1" />
466     </p>
467 <?php else : ?>
468     <p>&nbsp;</p>
469 <?php endif; ?>
470 </form>
471
472 <p id="nav">
473 <?php if ( isset($_GET['checkemail']) && in_array( $_GET['checkemail'], array('confirm', 'newpass') ) ) : ?>
474 <?php elseif (get_option('users_can_register')) : ?>
475 <a href="<?php echo site_url('wp-login.php?action=register', 'login') ?>"><?php _e('Register') ?></a> |
476 <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>
477 <?php else : ?>
478 <a href="<?php echo site_url('wp-login.php?action=lostpassword', 'login') ?>" title="<?php _e('Password Lost and Found')