]> code.delx.au - cgiproxy/blob - php/proxy.php
Ruby proxy
[cgiproxy] / php / proxy.php
1 <?php
2
3 function debugAndDie() {
4 print "<pre>";
5 print_r($_GET);
6 print_r($_POST);
7 print_r($_COOKIE);
8 print_r($_SERVER);
9 print_r(apache_request_headers());
10 print "</pre>";
11 die();
12 }
13
14 function debug($msg) {
15 $fp = fopen("/tmp/debug.log", "a");
16 fwrite($fp, "===\n");
17 fwrite($fp, $msg);
18 fwrite($fp, "\n");
19 fclose($fp);
20 }
21
22 function getUrl($url) {
23 if(isset($_SERVER["PATH_INFO"])) {
24 $url .= $_SERVER["PATH_INFO"];
25 }
26 $url = parse_url($url);
27 if(!isset($url["port"])) {
28 $url["port"] = 80;
29 if($url["scheme"] == "https") {
30 $url["port"] = 443;
31 }
32 }
33 if(strlen($_SERVER["QUERY_STRING"]) > 0) {
34 $url["path"] = $url["path"] . "?" . $_SERVER["QUERY_STRING"];
35 }
36 return $url;
37 }
38
39 function generateRequest($rmeth, $host, $path) {
40 $crlf = "\r\n";
41
42 $req = $rmeth . " " . $path . " HTTP/1.0" . $crlf;
43
44 $headers = apache_request_headers();
45 if(isset($headers["Host"])) {
46 $req .= "Host: " . $headers["Host"] . $crlf;
47 }
48 if(isset($headers["Cookie"])) {
49 $req .= "Cookie: " . $headers["Cookie"] . $crlf;
50 }
51 if(isset($headers["Cookie2"])) {
52 $req .= "Cookie2: " . $headers["Cookie2"] . $crlf;
53 }
54 if(isset($headers["Referer"])) {
55 $req .= "Referer: " . $headers["Referer"] . $crlf;
56 }
57 if(isset($headers["Content-Length"])) {
58 $req .= "Content-Length: " . $headers["Content-Length"] . $crlf;
59 }
60 if(isset($headers["Content-Type"])) {
61 $val = $headers["Content-Type"];
62 $val = str_replace("PHPHACK", "", $val);
63 $req .= "Content-Type: " . $headers["Content-Type"] . $crlf;
64 }
65
66 $req .= $crlf;
67
68 return $req;
69 }
70
71 function doProxy($scheme, $rmeth, $host, $port, $path)
72 {
73 $req = generateRequest($rmeth, $host, $path);
74
75 // Make the HTTP request
76 $fp = fsockopen(($scheme == 'https' ? 'ssl://' : '') . $host, $port);
77 if(!$fp) {
78 die("Could not connect to internal server!");
79 }
80 fwrite($fp, $req);
81 if($rmeth == "POST") {
82 $fpi = fopen("php://input", "rb");
83 while(!feof($fpi)) {
84 fwrite($fp, fread($fpi, 1024));
85 }
86 fclose($fpi);
87 }
88
89 $headers = array();
90 $mode = 0;
91 while(is_resource($fp) && $fp && !feof($fp)) {
92 // Parsing headers
93 if($mode == 0) {
94 // Grab a line
95 $line = fgets($fp);
96
97 if(strlen(trim($line)) != 0) {
98 array_push($headers, $line);
99 } else {
100 $mode = 1;
101 foreach($headers as $header) {
102 header($header);
103 }
104 }
105 }
106 // Body data!
107 else {
108 myPassThru($fp);
109 }
110 }
111
112 fclose($fp);
113 }
114
115 function myPassThru($fp) {
116 $data = "";
117 while(!feof($fp)) {
118 $data .= fread($fp, 1024);
119 }
120 $data = str_replace("enctype=\"multipart/form-data\"", "enctype=\"PHPHACKmultipart/form-data\"", $data);
121 print $data;
122 }
123
124 function main($basePath) {
125 ///debugAndDie();
126
127 $u = getUrl($basePath);
128 $rmeth = $_SERVER["REQUEST_METHOD"];
129 doProxy($u["scheme"], $rmeth, $u["host"], $u["port"], $u["path"]);
130 }
131
132 // Run!
133 if(!isset($PROXY)) {
134 die("No forwarding available!");
135 }
136
137 main($PROXY);
138
139 ?>