<?php
/*
Plugin Name: Registered Only
Plugin URI: http://blog.carthik.net
Description: Use this plugin to make your entire weblog password protected. Once this plugin is activated, only registered users who are logged in will be able to view the blog. Based on the original auth.php for WordPress 1.2 credited to  <a href="http://boren.nu">Ryan Boren</a>.
Version: 0.1
Author: Carthik Sharma
Author URI: http://blog.carthik.net
*/ 
/*  Copyright 2004 Carthik A. Sharma (email : mail at carthik dot net)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
require_once(dirname(__FILE__).'/../../'.'wp-config.php');
//require_once('wp-config.php');
/* Checking login & pass in the database */
function verify_login() {
    global 
$cookiehash;
    global 
$tableusers$wpdb;

    if (!empty(
$_COOKIE['wordpressuser_' $cookiehash])) {
        
$user_login $_COOKIE['wordpressuser_' $cookiehash];
        
$user_pass_md5 $_COOKIE['wordpresspass_' $cookiehash];
    } else {
        return 
false;
    }

    if (
'' == $user_login)
        return 
false;
    if (!
$user_pass_md5)
        return 
false;

    
$login $wpdb->get_row("SELECT user_login, user_pass FROM $tableusers WHERE user_login = '$user_login'");

    if (!
$login) {
        return 
false;

    } else {
        if (
$login->user_login == $user_login && md5($login->user_pass) == $user_pass_md5) {
            return 
true;
        } else {
            return 
false;
        }
    }
}

function 
reject_unverified() {
echo 
'<meta name="robots" content="noindex,follow" />'//Comment this line if you want your blog to be indexed by search engines
verify_login();
if ( !
verify_login() ) {
    
header('Expires: Wed, 11 Jan 1984 05:00:00 GMT');
    
header('Last-Modified: ' gmdate('D, d M Y H:i:s') . ' GMT');
    
header('Cache-Control: no-cache, must-revalidate');
    
header('Pragma: no-cache');
    if (!empty(
$_COOKIE['wordpressuser_' $cookiehash])) {
        
$error__("<strong>Error</strong>: wrong login or password.");
    }

    
$redir 'Location: ' get_settings('siteurl') . '/wp-login.php?redirect_to=' urlencode($_SERVER['REQUEST_URI']);
    
header($redir);
    exit();
}
}

add_action(wp_headreject_unverified);
?>