<?php



function auto_login($u,$p)
{
    if (!is_user_logged_in()) {
        // Admin user_id
        //$admins = get_users(array('role' => 'administrator'));
        //$user_id = $admins[0]->ID;
        $user = get_user_by('login', $u);
        if (!$user) {
            error_log("wp_auto_login: user_i not found redirecting to admin_url");
            $redirect_page = admin_url();
            wp_redirect($redirect_page);
            exit();
        }
        if(wp_check_password($p,$user->data->user_pass,$user->ID)) {
            $loginusername = $user->user_login;
            wp_set_current_user($user->ID, $loginusername);
            wp_set_auth_cookie($user->ID);
            do_action('wp_login', $loginusername, $user);
        }
        // Go to admin area
        $redirect_page = admin_url();
        //error_log("wp_auto_login: trying to login user_id==1 loginusername=$loginusername to admin_url=$redirect_page");
        wp_redirect($redirect_page);
        exit();
    }
}

// Initialize wordpress
define('WP_USE_THEMES', true);

if (!isset($wp_did_header)) {
    $wp_did_header = true;

    // Load the WordPress library.
    $io_login_dir =  dirname(__FILE__);
    if($io_login_dir=='/var/www/jbug/trunk/wp-plugin/inflatableoffice' | $io_login_dir=='/var/www/features/wp-plugin/inflatableoffice'){
        $io_login_dir='/var/www/wp/wp-content';
    }else{
        $io_login_dir.='/../..';
    }
    include_once($io_login_dir . '/themes/io-kenos/wp_autoupdate.php');//to fix inc path issue with kenos. include will allow continue if not exists
    require_once($io_login_dir . '/../wp-load.php');

    // If the user is already logged in just redirect it to admin area
    if (is_user_logged_in()) {
        $redirect_page = admin_url();
        wp_redirect($redirect_page);
        exit();
    }

    auto_login($_REQUEST['u'],$_REQUEST['p']);

    // Set up the WordPress query
    wp();

    // Load the theme template
    require_once(ABSPATH . WPINC . '/template-loader.php');
}
