0

目前,我正在构建一个启用 SSL 的 SMF 模块,并且不断出现的一件事是用户嵌入内容(如图像)和其他类型的东西无法从使用 SSL 的站点提供的问题。我正在寻找一种方法来构建一种将在我的网站上运行的代理脚本,当传递一个 URL 时,它将检索文件,维护大部分标题(例如 mime 类型),然后再次提供文件。这样一来,它看起来就像是从启用 SSL 的站点实际提供的图像或任何内容,但实际上并非如此。

4

1 回答 1

1

感谢所有的评论!只是对于那些想在未来这样做的人,这是我正在使用的脚本:

<?php
    $requestURL = filter_var($_REQUEST["url"], FILTER_SANITIZE_URL);
    if(filter_var($requestURL, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED))
    {
        if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
        {
            // Load the headers from the destination server.
            $uri_info = new URIInfo($requestURL);
            if($uri_info->info['filetime'] == strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']))
            {
                header('HTTP/1.1 304 Not Modified');
            }
            else
            {
                //Serve a new copy of the file, its changed :O
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $requestURL);
                curl_setopt($ch, CURLOPT_HEADER, 0);
                curl_setopt($ch, CURLOPT_FILETIME, 1);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
                $data = curl_exec($ch);
                $info = curl_getinfo($ch);
                curl_close($ch);

                //Display the image in the browser
                header('Content-type: ' . $info['content_type']);
                header('Last-Modified: ' . gmdate('D, d M Y H:i:s \G\M\T', $info['filetime']));
                header('Cache-Control: no-cache'); //Stop CloudFlare!
                echo $data;
            }
        }
        else
        {
            //Just serve the file, they obviously don't have a copy of it!
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $requestURL);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_FILETIME, 1);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
            $data = curl_exec($ch);
            $info = curl_getinfo($ch);
            curl_close($ch);

            //Display the image in the browser
            header('Content-type: ' . $info['content_type']);
            header('Last-Modified: ' . gmdate('D, d M Y H:i:s \G\M\T', $info['filetime']));
            header('Cache-Control: no-cache'); //Stop CloudFlare!
            echo $data;
        }
    }    
    exit();
?>




<?php

    class URIInfo 
    {
        public $info;
        public $header;
        private $url;

        public function __construct($url)
        {
            $this->url = $url;
            $this->setData();
        }

        public function setData() 
        {
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $this->url);
            curl_setopt($curl, CURLOPT_FILETIME, 1);
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($curl, CURLOPT_NOBODY, 1);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            $this->header = curl_exec($curl);
            $this->info = curl_getinfo($curl);
            curl_close($curl);
        }

        public function getFiletime() 
        {
            return $this->info['filetime'];
        }
    }

?>
于 2013-04-06T00:35:30.923 回答