Simple PHP Proxy

<?php
    function download($url) {
        $curl = curl_init($url);
        curl_setopt_array($curl, [
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_RETURNTRANSFER => true
        ]);
        $data = curl_exec($curl);
        curl_close($curl);
        return $data;
    }
    
    function getFileName($url) {
        $fileName = basename(parse_url($url, PHP_URL_PATH));
        return empty($fileName) ? "data" : $fileName;
    }
    
    $url = trim($_GET["url"]);
    if (!empty($url)) {
        $data = gzencode(download($url));
        header("Content-Type: application/gzip");
        header(sprintf("Content-Length: %d", strlen($data)));
        header(sprintf("Content-Disposition: attachment; filename=%s.gz", getFileName($url)));
        print($data);
        return;
    }
?>

<!DOCTYPE html>
<html>
    <head>
        <title>Proxy</title>
    </head>
    <body>
        <form action="<?= $_SERVER["SCRIPT_NAME"] ?>" method="get">
            <label>
                URL:
                <input type="text" name="url" size="50" />
            </label>
            <input type="submit" value="Download" />
        </form>
    </body>
</html>

Leave a Reply

Your email address will not be published.