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