Skip to content

Instantly share code, notes, and snippets.

@bmvakili
Created January 31, 2012 02:54
Show Gist options
  • Save bmvakili/1708421 to your computer and use it in GitHub Desktop.
Save bmvakili/1708421 to your computer and use it in GitHub Desktop.
this PHP script lets your apache server act as a proxy
<?
// limitation is that this script only works for http right now; also, login forms may/not work
// this PHP script lets your apache server act as a proxy
// 1. simply add a .htaccess directive as follows
// ErrorDocument 404 /proxy.php
// 2. set the proxy_url and actual_url parameters belom
// proxy_url is the address to your PHP server, e.g. www.example.com
// actual_url is the address to your other server, e.g. 10.10.10.10
error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
$proxy_url = 'www.example.com'; // the domain name where users will access the site
$actual_url = '10.10.10.10'; // the server's IP address where content resides
$url = "http://" . $actual_url . $_SERVER['REDIRECT_URL'];
if (isset($_SERVER['REDIRECT_QUERY_STRING'])) {
$url = "http://" . $actual_url . $_SERVER['REDIRECT_URL'] . "?" . $_SERVER['REDIRECT_QUERY_STRING'];
}
//var_dump($_SERVER);
$timeout=15;
$ch = curl_init();
$type_js = false;
$type_css = false;
$type_png = false;
$type_gif = false;
$current_headers = "";
//var_dump($_POST);
setType2();
//emu_getallheaders();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, true);
if (isset($_SERVER['HTTP_COOKIE'])) {
curl_setopt( $ch, CURLOPT_COOKIE, $_SERVER['HTTP_COOKIE']);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $ch, CURLOPT_ENCODING, "" );
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($_POST));
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
$redirects = 5;
$method = $_SERVER['REQUEST_METHOD'];
$request = explode("/", substr(@$_SERVER['PATH_INFO'], 1));
switch ($method) {
case 'PUT':
break;
case 'POST':
echo "TEST";
$result = post_request($url, $_POST);
list(,,$contents) = $result;
break;
case 'GET':
$contents = curl_redirect_exec($ch, $redirects, false, $actual_url, $proxy_url);
break;
case 'HEAD':
break;
case 'DELETE':
break;
case 'OPTIONS':
break;
default:
break;
}
if (!$contents) {
}else {
$headers = curl_getinfo($ch);
//emu_getallheaders2($headers);
//foreach ($headers as $name => $value) {
// header($name. ': ' . $value,true, 200);
//}
//header("Content-Type: ". "text/css", true, 200);
if (isset($headers['set_cookies'])) {
header("Set-Cookie: " . $headers['set_cookies'], true, 200);
}
if (isset($headers['content_type'])) {
header("Content-Type: " . $headers['content_type'], true, 200);
$current_headers = $headers['content_type'];
} else
if ($type_css) {
header("Content-Type: ". "text/css", true, 200);
$current_headers = "css";
} else
if ($type_js) {
header("Content-Type: ". "text/javascript", true, 200);
$current_headers = "js";
} else
if ($type_png) {
header("Content-Type: ". "image/ping", true, 200);
$current_headers = "png";
} else
if ($type_gif) {
header("Content-Type: ". "image/gif", true, 200);
$current_headers = "gif";
} else
if (strpos($url, "/html/js/barebone.jsp") !== false | strpos($url, "/html/themes/classic/js/main.js") !== false | strpos($url, "/js/main.jsp") !== false | strpos($url, "/js/main.js") !== false ){
header("Content-Type: ". "text/javascript", true, 200);
$current_headers = "js";
} else
if (strpos($url, "/html/portal/css.jsp") !== false | strpos($url, "/html/themes/classic/css/main.css") !== false | strpos($url, "/css/main.jsp") !== false | strpos($url, "/css/main.css") !== false){
header("Content-Type: ". "text/css", true, 200);
$current_headers = "css";
}
curl_close ($ch); // $contents = curl_exec ($ch);
$contents = preg_replace( '/' . $actual_url . '/', $proxy_url . '', $contents );
$contents = preg_replace( '/' . $actual_url . '/', $proxy_url . '', $contents );
$contents = preg_replace( '/http:..' . $actual_url . '/', 'http://' . $proxy_url . '', $contents );
$contents = preg_replace( '/https:..' . $actual_url . '/', 'http://' . $proxy_url . '', $contents );
#echo $url;
#echo $current_headers.$contents;
echo $contents;
}
function curl_redirect_exec($ch, &$redirects, $curlopt_header = false, $actual_url, $proxy_url) {
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 301 || $http_code == 302) {
list($header) = explode("\r\n\r\n", $data, 2);
$matches = array();
preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
$url = trim(array_pop($matches));
$url = preg_replace('/https:..' . $actual_url . '/','http://' . $proxy_url . '',$url);
$url = preg_replace('/http:..' . $actual_url . '/','http://' . $proxy_url . '',$url);
preg_match('/(Set-Cookie:)(.*?)\n/', $header, $matches);
$cookies = trim(array_pop($matches));
if ($cookies) {
header('Set-Cookie: ' . $cookies);
}
//header('Location: ' . $url);
//var_dump($data);
//return false;
$url_parsed = parse_url($url);
//$url = trim(array_pop($matches));
if (isset($url_parsed)) {
curl_setopt($ch, CURLOPT_URL, $url);
$redirects++;
return curl_redirect_exec($ch, $redirects, $curlopt_header, $actual_url, $proxy_url);
}
}
if ($curlopt_header)
return $data;
else {
// var_dump(explode("\r\n\r\n", $data,1));
//var_dump (curl_getinfo($ch));
$matches = array();
list($header) = explode("\r\n\r\n", $data, 2);
preg_match('/(Set-Cookie:)(.*?)\n/', $header, $matches);
$cookies = trim(array_pop($matches));
if ($cookies) {
header('Set-Cookie: ' . $cookies);
}
$tmp_var = explode("\r\n\r\n", $data, 2);
$body = $data;
//if (count($tmp_var) > 1) {
list(,$body) = $tmp_var;
//}
return $body;
}
}
function setType2() {
if ($_SERVER["HTTP_ACCEPT"]) {
if ( strrpos($_SERVER["HTTP_ACCEPT"], "css")) {
$type_css = true;
} else if ( strrpos($_SERVER["HTTP_ACCEPT"], "javascript")) {
$type_js = true;
} else if ( strrpos($_SERVER["HTTP_ACCEPT"], "png")) {
$type_png = true;
} else if ( strrpos($_SERVER["HTTP_ACCEPT"], "gif")) {
$type_gif = true;
} else if ($_SERVER["HTTP_ACCEPT"] == "*/*") {
if (strrpos($_SERVER['REDIRECT_URL'], "main.js")) {
$type_js=true;
}
}
}
return;
}
function post_request($url, $data, $referer='') {
// Convert the data array into URL Parameters like a=b&foo=bar etc.
$data = http_build_query($data);
// parse the given URL
$url = parse_url($url);
if ($url['scheme'] != 'http') {
die('Error: Only HTTP request are supported !');
}
// extract host and path:
$host = $url['host'];
$path = $url['path'];
// open a socket connection on port 80 - timeout: 30 sec
$fp = fsockopen($host, 80, $errno, $errstr, 30);
if ($fp){
// send the request headers:
fputs($fp, "POST $path HTTP/1.1\r\n");
fputs($fp, "Host: $host\r\n");
if (isset($_SERVER['HTTP_COOKIE'])) {
fputs($fp, "Cookies:" . $_SERVER['HTTP_COOKIE'] . "\r\n");
}
if ($referer != '')
fputs($fp, "Referer: $referer\r\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
fputs($fp, "Content-length: ". strlen($data) ."\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $data);
$result = '';
while(!feof($fp)) {
// receive the results of the request
$result .= fgets($fp, 128);
}
}
else {
return array(
'status' => 'err',
'error' => "$errstr ($errno)"
);
}
// close the socket connection:
fclose($fp);
// split the result header from the content
$result = explode("\r\n\r\n", $result, 2);
$header = isset($result[0]) ? $result[0] : '';
$content = isset($result[1]) ? $result[1] : '';
// return as structured array:
return array(
'status' => 'ok',
'header' => $header,
'content' => $content
);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment