<?php
    if (isset($_GET["token"])) {

        require("../../../wp-load.php");

        $data = $tokenSegments = explode("_", $_GET["token"], 3);
        list($uid, $sid, $token) = explode("_", $_GET["token"], 3);

        /***
         * TODO Comprobar que el id satelite esta registrado y conincide con $_SERVER["REMOTE_ADDR"] y $_SERVER["HTTP_REFERER"]
         */

        /***
         * TODO Cargar código blowfish para cifrado bidirecional de la cookie
         */
        require("utilities.php");

        global $wpdb;
        $query = "Select * from `".$wpdb->prefix."kconnect_satellites` where id='".$sid."'";
        $site = array_shift($wpdb->get_results($query));

        if (isset($site->id) and $site->id == $sid) {

            $blowfish = new Blowfish();
            $blowfish->key = $site->Key;
            $cookieValue = base64_encode($blowfish->encrypt($uid."_".$token));

            if (setcookie("kc".$sid, $cookieValue, time()+4500, "/")) {

                require("session.js");
                ?>
                setCookie("justConnected", 2, 1);
                <?php
            }
        }
    }
