<?php
    require_once __DIR__ . "/../vendor/autoload.php";


    $json = [];
    try {
        $params = [
            "host" => $_SERVER['HTTP_HOST'] ?? null,
        ];
        $ip = \Intgrtn\Lib\Common::getClientIP();
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://' . \Intgrtn\Config::instance()->getServerHost() . '/api/v1/projects/robotstxt.php?' . http_build_query($params));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("X_FORWARDED_FOR: $ip", "X-Forwarded-For: $ip"));
        curl_setopt($ch, CURLOPT_REFERER, $_SERVER['HTTP_REFERER'] ?? null);
        $output = curl_exec($ch);

        if (curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200) {
            throw new ErrorException($output);
        }
        curl_close($ch);
        $output = json_decode($output);

        if (empty($output->data->content)) {
            throw new ErrorException("Missing content");
        }
        header("Content-Type: text/plain");
        echo $output->data->content;
    } catch (ErrorException $exc) {
        http_response_code(400);
        echo $exc->getMessage();
    }

