<?php

/*
Plugin Name: Kronet connect
Plugin URI: http://www.irontec.com
Description: Kronet connect adapter fo satellite sites.
Version: 0.1
Author: Irontec
Author URI: http://www.irontec.com
License: GNU General Public License 2.0 (GPL) http://www.gnu.org/licenses/gpl.html
*/

// Define plugin version and plugin db version
define( 'KRONET_CONNECT', '0.1' );
define( 'KRONET_CONNECT_DB_VERSION', '0.1' );

// Activation hook
register_activation_hook( __FILE__, 'kronet_connect_activation' );
// Deactivation hook
register_deactivation_hook( __FILE__, 'kronet_connect_deactivation' );

/*
Activation of the plugin.
*/
function kronet_connect_activation() {

    update_option( "kconnect_host", "http://www.kronikoensarea.net/" );

    if (get_option( "kconnect_currentkey") == "") {

        update_option( "kconnect_currentkey", "" );
    }

    if (! is_numeric(get_option( "kconnect_status"))) {

        update_option( "kconnect_status", "0");
    }

	return true;
}

/*
Deactivation of the plugin.
*/
function kronet_connect_deactivation() {

    update_option( "kconnect_enabled", 0 );
	return true;
}


/****************************************************
 ****************** Admin Page **********************
 ****************************************************/

/*
 * Tell WP about the Admin page
 */

function kronet_admin_page()
{
    add_options_page("kronet connect Options", 'kronet connect', 'administrator', "wp-kronet-connect", 'kronet_connect_admin_page');
}
add_action('admin_menu', 'kronet_admin_page');

/**
  * Link to Settings on Plugins page
  */
add_filter('plugin_action_links', 'kronet_connect_add_plugin_links', 10, 2);
function kronet_connect_add_plugin_links($links, $file)
{
    if( dirname(plugin_basename( __FILE__ )) == dirname($file) )
        $links[] = '<a href="options-general.php?page=' . "wp-kronet-connect" .'">' . __('Settings','sitemap') . '</a>';
    return $links;
}

/*
 * Output the Admin page
 */
function kronet_connect_admin_page()
{
    ?>
    <div class="wrap">
     <h2><?php echo __("kronikoensarea connect Options","budypress");?></h2>
    <?php

        if(version_compare('5', PHP_VERSION, ">"))
        {
            ?><div class="error"><p><?php echo __("Sorry,  kronikoensarea connect requires PHP5."); ?></p></div><?php
            die();
        }

        if ( isset($_POST["enabled"]) and $_POST["enabled"] != "") {

            update_option( "kconnect_enabled", $_POST["enabled"] );
        }

        $status = get_option("kconnect_status");

        switch ($status) {

            case 0:

                $status = "Pendiente de validación remota";
                break;

            case 1:

                $status = "Activo";
                break;

        }
    ?>
   <p>Estado: <strong><?php echo $status; ?></strong></p>

   </div><?php
}

/***************************************************************************
 *************************** Filters and actions ***************************
 ***************************************************************************/
function setRemoteSessionCookie ($data1, $data2) {

    if (count($_POST) > 0) {

        setcookie("justConnected", 1, time()+3600);
    }
}
add_action('wp_authenticate', 'setRemoteSessionCookie', 10, 2);

function startRemoteSession () {

    if (isset($_COOKIE["justConnected"]) and $_COOKIE["justConnected"] == 1) {

        $status = get_option("kconnect_status");
        if (is_numeric($status) and $status > 0) {

            global $current_user;

            if (!isset($current_user->ID) or $current_user->ID == 0) {

                return;
            }

            $data = array(
                "satId" => get_option("kconnect_sid"),
                "id" => $current_user->ID,
                "login" => $current_user->user_login,
                "email" => $current_user->user_email ,
                "date" => $current_user->user_registered
            );

            require("utilities.php");

            $token = $data["id"]."_".$data["satId"]."_";
            $token .= cryptToken($data["login"].$data["email"].$data["date"]);

            $host = get_option("kconnect_host");
            $scriptSrc = $host."wp-content/plugins/irontec-kronet-connect/session.php";
            $html = '<script type="text/javascript" src="%s?token=%s"></script>';
            echo sprintf($html, $scriptSrc, $token);
        }

    } else if (!isset($_COOKIE["justConnected"])) {

            global $current_user;

            if (isset($current_user->ID) and is_numeric($current_user->ID)) {
                setcookie("justConnected", 1, time()+3600);
            }
    }

}

add_action('wp_head', 'startRemoteSession', 10, 2);
