root/branches/2.2/wp-login.php

Revision 5285, 15.6 kB (checked in by rob1n, 1 year ago)

Replace deprecated get_settings() calls with get_option(). Props johnbillion. fixes #4167

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 require( dirname(__FILE__) . '/wp-config.php' );
3
4 $action = $_REQUEST['action'];
5 $errors = array();
6
7 if ( isset($_GET['key']) )
8     $action = 'resetpass';
9
10 nocache_headers();
11
12 header('Content-Type: '.get_bloginfo('html_type').'; charset='.get_bloginfo('charset'));
13
14 if ( defined('RELOCATE') ) { // Move flag is set
15     if ( isset( $_SERVER['PATH_INFO'] ) && ($_SERVER['PATH_INFO'] != $_SERVER['PHP_SELF']) )
16         $_SERVER['PHP_SELF'] = str_replace( $_SERVER['PATH_INFO'], '', $_SERVER['PHP_SELF'] );
17
18     $schema = ( isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https://' : 'http://';
19     if ( dirname($schema . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']) != get_option('siteurl') )
20         update_option('siteurl', dirname($schema . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']) );
21 }
22
23
24 // Rather than duplicating this HTML all over the place, we'll stick it in function
25 function login_header($title = 'Login', $message = '') {
26     global $errors, $error, $wp_locale;
27
28     ?>
29 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
30 <html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>
31 <head>
32     <title><?php bloginfo('name'); ?> &rsaquo; <?php echo $title; ?></title>
33     <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
34     <link rel="stylesheet" href="<?php bloginfo('wpurl'); ?>/wp-admin/wp-admin.css?version=<?php bloginfo('version'); ?>" type="text/css" />
35 <?php if ( ('rtl' == $wp_locale->text_direction) ) : ?>
36     <link rel="stylesheet" href="<?php bloginfo('wpurl'); ?>/wp-admin/rtl.css?version=<?php bloginfo('version'); ?>" type="text/css" />
37 <?php endif; ?>
38     <!--[if IE]><style type="text/css">#login h1 a { margin-top: 35px; } #login #login_error { margin-bottom: 10px; }</style><![endif]--><!-- Curse you, IE! -->
39     <script type="text/javascript">
40         function focusit() {
41             document.getElementById('user_login').focus();
42         }
43         window.onload = focusit;
44     </script>
45 <?php do_action('login_head'); ?>
46 </head>
47 <body class="login">
48
49 <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>
50 <?php
51     if ( !empty( $message ) ) echo apply_filters('login_message', $message) . "\n";
52
53     // Incase a plugin uses $error rather than the $errors array
54     if ( !empty( $error ) ) {
55         $errors['error'] = $error;
56         unset($error);
57     }
58
59     if ( !empty( $errors ) ) {
60         if ( is_array( $errors ) ) {
61             $newerrors = "\n";
62             foreach ( $errors as $error ) $newerrors .= '    ' . $error . "<br />\n";
63             $errors = $newerrors;
64         }
65
66         echo '<div id="login_error">' . apply_filters('login_errors', $errors) . "</div>\n";
67     }
68 } // End of login_header()
69
70
71 switch ($action) {
72
73 case 'logout' :
74
75     wp_clearcookie();
76     do_action('wp_logout');
77
78     $redirect_to = 'wp-login.php?loggedout=true';
79     if ( isset( $_REQUEST['redirect_to'] ) )
80         $redirect_to = $_REQUEST['redirect_to'];
81
82     wp_redirect($redirect_to);
83     exit();
84
85 break;
86
87 case 'lostpassword' :
88 case 'retrievepassword' :
89     $user_login = '';
90     $user_pass = '';
91
92     if ( $_POST ) {
93         if ( empty( $_POST['user_login'] ) )
94             $errors['user_login'] = __('<strong>ERROR</strong>: The username field is empty.');
95         if ( empty( $_POST['user_email'] ) )
96             $errors['user_email'] = __('<strong>ERROR</strong>: The e-mail field is empty.');
97
98         do_action('lostpassword_post');
99
100         if ( empty( $errors ) ) {
101             $user_data = get_userdatabylogin(trim($_POST['user_login']));
102             // redefining user_login ensures we return the right case in the email
103             $user_login = $user_data->user_login;
104             $user_email = $user_data->user_email;
105
106             if (!$user_email || $user_email != $_POST['user_email']) {
107                 $errors['invalidcombo'] = __('<strong>ERROR</strong>: Invalid username / e-mail combination.');
108             } else {
109                 do_action('retreive_password', $user_login);  // Misspelled and deprecated
110                 do_action('retrieve_password', $user_login);
111
112                 // Generate something random for a password... md5'ing current time with a rand salt
113                 $key = substr( md5( uniqid( microtime() ) ), 0, 8);
114                 // Now insert the new pass md5'd into the db
115                 $wpdb->query("UPDATE $wpdb->users SET user_activation_key = '$key' WHERE user_login = '$user_login'");
116                 $message = __('Someone has asked to reset the password for the following site and username.') . "\r\n\r\n";
117                 $message .= get_option('siteurl') . "\r\n\r\n";
118                 $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
119                 $message .= __('To reset your password visit the following address, otherwise just ignore this email and nothing will happen.') . "\r\n\r\n";
120                 $message .= get_option('siteurl') . "/wp-login.php?action=rp&key=$key\r\n";
121
122                 if (FALSE == wp_mail($user_email, sprintf(__('[%s] Password Reset'), get_option('blogname')), $message)) {
123                     die('<p>' . __('The e-mail could not be sent.') . "<br />\n" . __('Possible reason: your host may have disabled the mail() function...') . '</p>');
124                 } else {
125                     wp_redirect('wp-login.php?checkemail=confirm');
126                     exit();
127                 }
128             }
129         }
130     }
131
132     if ( 'invalidkey' == $_GET['error'] ) $errors['invalidkey'] = __('Sorry, that key does not appear to be valid.');
133
134     do_action('lost_password');
135     login_header(__('Lost Password'), '<p class="message">' . __('Please enter your username and e-mail address. You will receive a new password via e-mail.') . '</p>');
136 ?>
137
138 <form name="lostpasswordform" id="lostpasswordform" action="wp-login.php?action=lostpassword" method="post">
139     <p>
140         <label><?php _e('Username:') ?><br />
141         <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>
142     </p>
143     <p>
144         <label><?php _e('E-mail:') ?><br />
145         <input type="text" name="user_email" id="user_email" class="input" value="<?php echo attribute_escape(stripslashes($_POST['user_email'])); ?>" size="25" tabindex="20" /></label>
146     </p>
147 <?php do_action('lostpassword_form'); ?>
148     <p class="submit"><input type="submit" name="wp-submit" id="wp-submit" value="<?php _e('Get New Password &raquo;'); ?>" tabindex="100" /></p>
149 </form>
150 </div>
151
152 <ul>
153 <?php if (get_option('users_can_register')) : ?>
154     <li><a href="<?php bloginfo('wpurl'); ?>/wp-login.php"><?php _e('Login') ?></a></li>
155     <li><a href="<?php bloginfo('wpurl'); ?>/wp-login.php?action=register"><?php _e('Register') ?></a></li>
156     <li><a href="<?php bloginfo('url'); ?>/" title="<?php _e('Are you lost?') ?>"><?php printf(__('Back to %s'), get_bloginfo('title')); ?></a></li>
157 <?php else : ?>
158     <li><a href="<?php bloginfo('url'); ?>/" title="<?php _e('Are you lost?') ?>"><?php printf(__('Back to %s'), get_bloginfo('title')); ?></a></li>
159     <li><a href="<?php bloginfo('wpurl'); ?>/wp-login.php"><?php _e('Login') ?></a></li>
160 <?php endif; ?>
161 </ul>
162
163 </body>
164 </html>
165 <?php
166 break;
167
168 case 'resetpass' :
169 case 'rp' :
170     $key = preg_replace('/[^a-z0-9]/i', '', $_GET['key']);
171     if ( empty( $key ) ) {
172         wp_redirect('wp-login.php?action=lostpassword&error=invalidkey');
173         exit();
174     }
175
176     $user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE user_activation_key = '$key'");
177     if ( empty( $user ) ) {
178         wp_redirect('wp-login.php?action=lostpassword&error=invalidkey');
179         exit();
180     }
181
182     do_action('password_reset');
183
184     // Generate something random for a password... md5'ing current time with a rand salt
185     $new_pass = substr( md5( uniqid( microtime() ) ), 0, 7);
186     $wpdb->query("UPDATE $wpdb->users SET user_pass = MD5('$new_pass'), user_activation_key = '' WHERE user_login = '$user->user_login'");
187     wp_cache_delete($user->ID, 'users');
188     wp_cache_delete($user->user_login, 'userlogins');
189     $message  = sprintf(__('Username: %s'), $user->user_login) . "\r\n";
190     $message .= sprintf(__('Password: %s'), $new_pass) . "\r\n";
191     $message .= get_option('siteurl') . "/wp-login.php\r\n";
192
193     if (FALSE == wp_mail($user->user_email, sprintf(__('[%s] Your new password'), get_option('blogname')), $message)) {
194         die('<p>' . __('The e-mail could not be sent.') . "<br />\n" . __('Possible reason: your host may have disabled the mail() function...') . '</p>');
195     } else {
196         // send a copy of password change notification to the admin
197         // but check to see if it's the admin whose password we're changing, and skip this
198         if ($user->user_email != get_option('admin_email')) {
199             $message = sprintf(__('Password Lost and Changed for user: %s'), $user->user_login) . "\r\n";
200             wp_mail(get_option('admin_email'), sprintf(__('[%s] Password Lost/Changed'), get_option('blogname')), $message);
201         }
202
203         wp_redirect('wp-login.php?checkemail=newpass');
204         exit();
205     }
206 break;
207
208 case 'register' :
209     if ( FALSE == get_option('users_can_register') ) {
210         wp_redirect('wp-login.php?registration=disabled');
211         exit();
212     }
213
214     if ( $_POST ) {
215         require_once( ABSPATH . WPINC . '/registration.php');
216
217         $user_login = sanitize_user( $_POST['user_login'] );
218         $user_email = apply_filters( 'user_registration_email', $_POST['user_email'] );
219
220         // Check the username
221         if ( $user_login == '' )
222             $errors['user_login'] = __('<strong>ERROR</strong>: Please enter a username.');
223         elseif ( !validate_username( $user_login ) ) {
224             $errors['user_login'] = __('<strong>ERROR</strong>: This username is invalid.  Please enter a valid username.');
225             $user_login = '';
226         } elseif ( username_exists( $user_login ) )
227             $errors['user_login'] = __('<strong>ERROR</strong>: This username is already registered, please choose another one.');
228
229         // Check the e-mail address
230         if ($user_email == '') {
231             $errors['user_email'] = __('<strong>ERROR</strong>: Please type your e-mail address.');
232         } elseif ( !is_email( $user_email ) ) {
233             $errors['user_email'] = __('<strong>ERROR</strong>: The email address isn&#8217;t correct.');
234             $user_email = '';
235         } elseif ( email_exists( $user_email ) )
236             $errors['user_email'] = __('<strong>ERROR</strong>: This email is already registered, please choose another one.');
237
238         do_action('register_post');
239
240         $errors = apply_filters( 'registration_errors', $errors );
241
242         if ( empty( $errors ) ) {
243             $user_pass = substr( md5( uniqid( microtime() ) ), 0, 7);
244
245             $user_id = wp_create_user( $user_login, $user_pass, $user_email );
246             if ( !$user_id )
247                 $errors['registerfail'] = sprintf(__('<strong>ERROR</strong>: Couldn&#8217;t register you... please contact the <a href="mailto:%s">webmaster</a> !'), get_option('admin_email'));
248             else {
249                 wp_new_user_notification($user_id, $user_pass);
250
251                 wp_redirect('wp-login.php?checkemail=registered');
252                 exit();
253             }
254         }
255     }
256
257     login_header(__('Registration Form'), '<p class="message register">' . __('Register For This Site') . '</p>');
258 ?>
259
260 <form name="registerform" id="registerform" action="wp-login.php?action=register" method="post">
261     <p>
262         <label><?php _e('Username:') ?><br />
263         <input type="text" name="user_login" id="user_login" class="input" value="<?php echo attribute_escape(stripslashes($user_login)); ?>" size="20" tabindex="10" /></label>
264     </p>
265     <p>
266         <label><?php _e('E-mail:') ?><br />
267         <input type="text" name="user_email" id="user_email" class="input" value="<?php echo attribute_escape(stripslashes($user_email)); ?>" size="25" tabindex="20" /></label>
268     </p>
269 <?php do_action('register_form'); ?>
270     <p id="reg_passmail"><?php _e('A password will be e-mailed to you.') ?></p>
271     <p class="submit"><input type="submit" name="wp-submit" id="wp-submit" value="<?php _e('Register &raquo;'); ?>" tabindex="100" /></p>
272 </form>
273 </div>
274
275 <ul>
276     <li><a href="<?php bloginfo('wpurl'); ?>/wp-login.php"><?php _e('Login') ?></a></li>
277     <li><a href="<?php bloginfo('wpurl'); ?>/wp-login.php?action=lostpassword" title="<?php _e('Password Lost and Found') ?>"><?php _e('Lost your password?') ?></a></li>
278     <li><a href="<?php bloginfo('url'); ?>/" title="<?php _e('Are you lost?') ?>"><?php printf(__('Back to %s'), get_bloginfo('title')); ?></a></li>
279 </ul>
280
281 </body>
282 </html>
283 <?php
284 break;
285
286 case 'login' :
287 default:
288     $user_login = '';
289     $user_pass = '';
290     $using_cookie = FALSE;
291
292     if ( !isset( $_REQUEST['redirect_to'] ) || is_user_logged_in() )
293         $redirect_to = 'wp-admin/';
294     else
295         $redirect_to = $_REQUEST['redirect_to'];
296
297     if ( $_POST ) {
298         $user_login = $_POST['log'];
299         $user_login = sanitize_user( $user_login );
300         $user_pass  = $_POST['pwd'];
301         $rememberme = $_POST['rememberme'];
302     } else {
303         $cookie_login = wp_get_cookie_login();
304         if ( ! empty($cookie_login) ) {
305             $using_cookie = true;
306             $user_login = $cookie_login['login'];
307             $user_pass = $cookie_login['password'];
308         }
309     }
310
311     do_action_ref_array('wp_authenticate', array(&$user_login, &$user_pass));
312
313     if ( $user_login && $user_pass && empty( $errors ) ) {
314         $user = new WP_User(0, $user_login);
315
316         // If the user can't edit posts, send them to their profile.
317         if ( !$user->has_cap('edit_posts') && ( empty( $redirect_to ) || $redirect_to == 'wp-admin/' ) )
318             $redirect_to = get_option('siteurl') . '/wp-admin/profile.php';
319
320         if ( wp_login($user_login, $user_pass, $using_cookie) ) {
321             if ( !$using_cookie )
322                 wp_setcookie($user_login, $user_pass, false, '', '', $rememberme);
323             do_action('wp_login', $user_login);
324             wp_redirect($redirect_to);
325             exit();
326         } else {
327             if ( $using_cookie )
328                 $errors['expiredsession'] = __('Your session has expired.');
329         }
330     }
331
332     if ( $_POST && empty( $user_login ) )
333         $errors['user_login'] = __('<strong>ERROR</strong>: The username field is empty.');
334     if ( $_POST && empty( $user_pass ) )
335         $errors['user_pass'] = __('<strong>ERROR</strong>: The password field is empty.');
336
337     // Some parts of this script use the main login form to display a message
338     if        ( TRUE == $_GET['loggedout'] )            $errors['loggedout']        = __('Successfully logged you out.');
339     elseif    ( 'disabled' == $_GET['registration'] )    $errors['registerdiabled']    = __('User registration is currently not allowed.');
340     elseif    ( 'confirm' == $_GET['checkemail'] )    $errors['confirm']            = __('Check your e-mail for the confirmation link.');
341     elseif    ( 'newpass' == $_GET['checkemail'] )    $errors['newpass']            = __('Check your e-mail for your new password.');
342     elseif    ( 'registered' == $_GET['checkemail'] )    $errors['registered']        = __('Registration complete. Please check your e-mail.');
343
344     login_header(__('Login'));
345 ?>
346
347 <form name="loginform" id="loginform" action="wp-login.php" method="post">
348     <p>
349         <label><?php _e('Username:') ?><br />
350         <input type="text" name="log" id="user_login" class="input" value="<?php echo attribute_escape(stripslashes($user_login)); ?>" size="20" tabindex="10" /></label>
351     </p>
352     <p>
353         <label><?php _e('Password:') ?><br />
354         <input type="password" name="pwd" id="user_pass" class="input" value="" size="20" tabindex="20" /></label>
355     </p>
356 <?php do_action('login_form'); ?>
357     <p><label><input name="rememberme" type="checkbox" id="rememberme" value="forever" tabindex="90" /> <?php _e('Remember me'); ?></label></p>
358     <p class="submit">
359         <input type="submit" name="wp-submit" id="wp-submit" value="<?php _e('Login'); ?> &raquo;" tabindex="100" />
360         <input type="hidden" name="redirect_to" value="<?php echo attribute_escape($redirect_to); ?>" />
361     </p>
362 </form>
363 </div>
364
365 <ul>
366 <?php if (get_option('users_can_register')) : ?>
367     <li><a href="<?php bloginfo('wpurl'); ?>/wp-login.php?action=register"><?php _e('Register') ?></a></li>
368     <li><a href="<?php bloginfo('wpurl'); ?>/wp-login.php?action=lostpassword" title="<?php _e('Password Lost and Found') ?>"><?php _e('Lost your password?') ?></a></li>
369     <li><a href="<?php bloginfo('url'); ?>/" title="<?php _e('Are you lost?') ?>"><?php printf(__('Back to %s'), get_bloginfo('title')); ?></a></li>
370 <?php else : ?>
371     <li><a href="<?php bloginfo('url'); ?>/" title="<?php _e('Are you lost?') ?>"><?php printf(__('Back to %s'), get_bloginfo('title')); ?></a></li>
372     <li><a href="<?php bloginfo('wpurl'); ?>/wp-login.php?action=lostpassword" title="<?php _e('Password Lost and Found') ?>"><?php _e('Lost your password?') ?></a></li>
373 <?php endif; ?>
374 </ul>
375
376
377 </body>
378 </html>
379 <?php
380
381 break;
382 } // end action switch
383 ?>
384
Note: See TracBrowser for help on using the browser.