<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

require "globales.php";
require "funciones.php";
//include "cloak1.php";

//add track
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    $requestPath = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
    if ($requestPath == "/") {
        $comdin = "";
        $uaa = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Unknown';
        $refe = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : "none";

        $datox["origen"] = get_client_ip();
        $datox["ua"] = $uaa;
        $datox["refe"] = $refe;

        $logger = new SimpleLogger();
        $sse = $logger->log($datox["ua"], $datox["refe"], $datox["origen"]);
    }
}

$args = $_REQUEST["args"];

$murl = get_url();

$argsSlashed = addslashesToSpecialChars($args);

$murlroot = $murl;

if (strlen($args) > 0) {
    $murlroot = preg_replace("/" . $argsSlashed . ".*/i", "", $murl);
}
define("URLROOT", $murlroot);

//$mRandomString = isset($_SESSION["grs"]) ? $_SESSION["grs"] : generateRandomString(10);
$mRandomString = generateRandomString(10);
if(isset($_SESSION["grs"])){
    $mRandomString = $_SESSION["grs"];
}else{
    $_SESSION["grs"] = $mRandomString;
}

define("URLROOTBASE", $murlroot.$mRandomString."/");

$mpath = str_replace($murlroot,"",$murl);

$esRaiz = strlen($mpath)<=0;

$initialCookieSet = isset($_COOKIE["AspNetSSID"]);

if(!$initialCookieSet){
    if(!$esRaiz){
        echo "<div style='color: red '>EN MANTENIMIENTO 005</div>";
        exit;
    }
    include "mvistas/cookiePage.php";
    exit;
}

$es_land = $mpath=="land";

if($es_land){
    if($solomobil){
        include "mvistas/landingPage.php";
        exit;
    }else{
        include "mvistas/redirectopersonas.php";
        exit;
    }
}

$patronEsMatch = "/^".$mRandomString."\/.+/i";

$esMatch = preg_match($patronEsMatch,$mpath);

if(!$esMatch){
    // http_response_code(400);
    // echo "<div style='color: red '>solicitud incorrecta</div>";
    //$_SESSION = [];
    //setcookie("AspNetSSID", "", time() - 3600);

    $ud = get_user_data();

    if($ud == null || !isset($ud["esta"])){}else{
        $ud["esta"] = "";
        guardar($ud);
    }
    

    redirectTo("$mRandomString/mi-espacio-bcp");

    exit;
}

$mpathBase = str_replace($mRandomString."/","",$mpath);

$esPublic = preg_match("/^public\/.+/i",$mpathBase);

if($esPublic){
    include "manejadores/staticRequestHandler.php";
    exit;
}else{
    include "manejadores/dinamycRequestHandler.php";
    exit;
}

