Project

General

Profile

Download (4.14 KB) Statistics
| Branch: | Tag: | Revision:

root / drupal-module / ProxyHandler.class.php @ 0bb989f9

1
<?php
2
/*
3
 * Initially based on php5rp <http://code.google.com/p/php5rp/>, originally
4
 * published under the (new) BSD license,
5
 * http://www.opensource.org/licenses/bsd-license.php
6
 */
7

    
8
class ProxyHandler
9
{
10
    private $url;
11
    private $proxy_url;
12
    private $remote_base_url;
13
    private $translated_url;
14
    private $curl_handler;
15
    private $client_headers=array();
16

    
17
    public $result;
18
    public $http_code;
19

    
20
    function __construct($local_base_url, $remote_base_url, $proxied_url) {
21
        $this->result = '';
22
        $this->location = '';
23

    
24
        $this->url = $local_url;
25
        $this->remote_base_url = $remote_base_url;
26
        if ($this->remote_base_url[strlen($this->remote_base_url)-1] != '/') {
27
             $this->remote_base_url .= '/';
28
        }
29
        $this->proxy_url =  $remote_base_url . $proxied_url;
30

    
31
        $this->translated_url = $this->proxy_url;
32

    
33
        $this->curl_handler = curl_init($this->translated_url);
34

    
35
        // Set various options
36
        $this->setCurlOption(CURLOPT_RETURNTRANSFER, true);
37
        $this->setCurlOption(CURLOPT_VERBOSE, false);
38
        $this->setCurlOption(CURLOPT_BINARYTRANSFER, true); // For images, etc.
39
        $this->setCurlOption(CURLOPT_ENCODING, 'identity');
40
        $this->setCurlOption(CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
41
        $this->setCurlOption(CURLOPT_WRITEFUNCTION, array($this,'readResponse'));
42
        $this->setCurlOption(CURLOPT_HEADERFUNCTION, array($this,'readHeaders'));
43

    
44
        // Process post data.
45
        if (count($_POST)) {
46
            // Empty the post data
47
            $post=array();
48

    
49
            // Set the post data
50
            $this->setCurlOption(CURLOPT_POST, true);
51

    
52
            // Encode and form the post data
53
            foreach($_POST as $key=>$value) {
54
                $post[$key] = $value;
55
            }
56

    
57
            $this->setCurlOption(CURLOPT_POSTFIELDS, $post);
58

    
59
            unset($post);
60
        }
61
        elseif ($_SERVER['REQUEST_METHOD'] !== 'GET') // Default request method is 'get'
62
        {
63
            // Set the request method
64
            $this->setCurlOption(CURLOPT_CUSTOMREQUEST, $_SERVER['REQUEST_METHOD']);
65
        }
66

    
67
        // Handle the client headers.
68
        $this->handleClientHeaders();
69

    
70
    }
71

    
72
    public function setClientHeader($header) {
73
        $this->client_headers[] = $header;
74
    }
75

    
76
    // Executes the proxy.
77
    public function execute() {
78
        $this->setCurlOption(CURLOPT_HTTPHEADER, $this->client_headers);
79
        curl_exec($this->curl_handler);
80
    }
81

    
82
    public function close() {
83
        $info = curl_getinfo($this->curl_handler);
84
        $this->http_code = $info['http_code'];
85
        if ($this->http_code == 301 || $this->http_code == 302 || $this->http_code == 303) {
86
            $this->result = 'redirect to ' . $this->location;
87
        }
88
        curl_close($this->curl_handler);
89
    }
90

    
91
    public function getCurlInfo() {
92
        return curl_getinfo($this->curl_handler);
93
    }
94

    
95
    public function setCurlOption($option, $value) {
96
        curl_setopt($this->curl_handler, $option, $value);
97
    }
98

    
99
    protected function readHeaders(&$cu, $string) {
100
        $length = strlen($string);
101
        if (preg_match(',^Location:,', $string)) {
102
            $string = str_replace($this->remote_base_url, $this->url, $string);
103
            if (! preg_match(',http,', $string)) {
104
                $string = str_replace('//', '/', $string);
105
            }
106
            $this->location = $string;
107
            return $length;
108
        }
109
        if (preg_match(',^Set-Cookie:,', $string)) {
110
            header(rtrim($string));
111
        }
112
        return $length;
113
    }
114

    
115
    protected function handleClientHeaders() {
116
        $headers = apache_request_headers();
117

    
118
        foreach ($headers as $header => $value) {
119
            switch($header) {
120
                case 'Host':
121
                    break;
122
                case 'Content-Length':
123
                    break;
124
                default:
125
                    $this->setClientHeader(sprintf('%s: %s', $header, $value));
126
                    break;
127
            }
128
        }
129
    }
130

    
131
    protected function readResponse(&$cu, $string) {
132
        $length = strlen($string);
133
        $this->result .= $string;
134
        return $length;
135
    }
136
}
137

    
138
?>
(2-2/9)