]> code.delx.au - cgiproxy/blobdiff - php/proxy.php
Ruby proxy
[cgiproxy] / php / proxy.php
diff --git a/php/proxy.php b/php/proxy.php
new file mode 100644 (file)
index 0000000..fdbf912
--- /dev/null
@@ -0,0 +1,139 @@
+<?php
+
+function debugAndDie() {
+       print "<pre>";
+       print_r($_GET);
+       print_r($_POST);
+       print_r($_COOKIE);
+       print_r($_SERVER);
+       print_r(apache_request_headers());
+       print "</pre>";
+       die();
+}
+
+function debug($msg) {
+       $fp = fopen("/tmp/debug.log", "a");
+       fwrite($fp, "===\n");
+       fwrite($fp, $msg);
+       fwrite($fp, "\n");
+       fclose($fp);
+}
+
+function getUrl($url) {
+       if(isset($_SERVER["PATH_INFO"])) {
+               $url .= $_SERVER["PATH_INFO"];
+       }
+       $url = parse_url($url);
+       if(!isset($url["port"])) {
+               $url["port"] = 80;
+               if($url["scheme"] == "https") {
+                       $url["port"] = 443;
+               }
+       }
+       if(strlen($_SERVER["QUERY_STRING"]) > 0) {
+               $url["path"] = $url["path"] . "?" . $_SERVER["QUERY_STRING"];
+       }
+       return $url;
+}
+
+function generateRequest($rmeth, $host, $path) {
+       $crlf = "\r\n";
+
+       $req = $rmeth . " " . $path . " HTTP/1.0" . $crlf;
+
+       $headers = apache_request_headers();
+       if(isset($headers["Host"])) {
+               $req .= "Host: " . $headers["Host"] . $crlf;
+       }
+       if(isset($headers["Cookie"])) {
+               $req .= "Cookie: " . $headers["Cookie"] . $crlf;
+       }
+       if(isset($headers["Cookie2"])) {
+               $req .= "Cookie2: " . $headers["Cookie2"] . $crlf;
+       }
+       if(isset($headers["Referer"])) {
+               $req .= "Referer: " . $headers["Referer"] . $crlf;
+       }
+       if(isset($headers["Content-Length"])) {
+               $req .= "Content-Length: " . $headers["Content-Length"] . $crlf;
+       }
+       if(isset($headers["Content-Type"])) {
+               $val = $headers["Content-Type"];
+               $val = str_replace("PHPHACK", "", $val);
+               $req .= "Content-Type: " . $headers["Content-Type"] . $crlf;
+       }
+
+       $req .= $crlf;
+
+       return $req;
+}
+
+function doProxy($scheme, $rmeth, $host, $port, $path)
+{
+       $req = generateRequest($rmeth, $host, $path);
+
+       // Make the HTTP request
+       $fp = fsockopen(($scheme == 'https' ? 'ssl://' : '') . $host, $port);
+       if(!$fp) {
+               die("Could not connect to internal server!");
+       }
+       fwrite($fp, $req);
+       if($rmeth == "POST") {
+               $fpi = fopen("php://input", "rb");
+               while(!feof($fpi)) {
+                       fwrite($fp, fread($fpi, 1024));
+               }
+               fclose($fpi);
+       }
+
+       $headers = array();
+       $mode = 0;
+       while(is_resource($fp) && $fp && !feof($fp)) {
+               // Parsing headers
+               if($mode == 0) {
+                       // Grab a line
+                       $line = fgets($fp);
+
+                       if(strlen(trim($line)) != 0) {
+                               array_push($headers, $line);
+                       } else {
+                               $mode = 1;
+                               foreach($headers as $header) {
+                                       header($header);
+                               }
+                       }
+               }
+               // Body data!
+               else {
+                       myPassThru($fp);
+               }
+       }
+
+       fclose($fp);
+}
+
+function myPassThru($fp) {
+       $data = "";
+       while(!feof($fp)) {
+               $data .= fread($fp, 1024);
+       }
+       $data = str_replace("enctype=\"multipart/form-data\"", "enctype=\"PHPHACKmultipart/form-data\"", $data);
+       print $data;
+}
+
+function main($basePath) {
+       ///debugAndDie();
+
+       $u = getUrl($basePath);
+       $rmeth = $_SERVER["REQUEST_METHOD"];
+       doProxy($u["scheme"], $rmeth, $u["host"], $u["port"], $u["path"]);
+}
+
+// Run!
+if(!isset($PROXY)) {
+       die("No forwarding available!");
+}
+
+main($PROXY);
+
+?>