Snippets

RuWeb.net Site-Moved-Proxy

Created by Andrey Chesnakov last modified
<?php
//
// Site-Moved-Proxy v.0.2 (c) 2015 RuWeb.net
//
// Put theese 4 lines into .htaccess :
//    RewriteEngine On
//    RewriteCond %{REQUEST_FILENAME} !-f [OR]
//    RewriteCond %{REQUEST_FILENAME} !\.(gif|png|jpg|jpeg|bmp|ico|css|js|zip|rar|gz|tgz|bz2|txt|pdf|doc|xls|docx|xlsx|exe)$ 
//    RewriteRule !^.site-moved.php$ .site-moved.php [L]
//
// and specify new IP of your website in variable below:
$ip='127.0.0.1';

$myver='0.2';
$myname='Site-Moved-Proxy';

$timeout=30;
$timeout_connect=5;

if (($t=ini_get('max_execution_time')) && $timeout>=$t) $timeout=$t-1;

if ($_SERVER['SERVER_ADDR']==$ip) { echo "ERROR: new IP same as this server address<br>\n"; exit; }

$_SERVER['PATH_INFO']=$_SERVER['REDIRECT_URL'];

$fp = fsockopen ($ip, 80, $errno, $errstr, $timeout_connect);
if (!$fp) {
	echo "$errstr ($errno)<br>\n";
} else {
	stream_set_timeout($fp, $timeout);
	$path=$_SERVER['PATH_INFO'].($_SERVER['QUERY_STRING']?"?$_SERVER[QUERY_STRING]":'');
	$path=preg_replace('/ /','%20',$path);
	fputs ($fp, "$_SERVER[REQUEST_METHOD] $path HTTP/1.0\r\n");
	$headers = getallheaders();
	foreach ($headers as $header => $value) {
#		$logheaders.="$header: $value\r\n";
		if (preg_match('/^Via$/i',$header)) {
			if (preg_match("/ $myname/", $value)) { echo "ERROR: loop detected<br>\n"; exit; }
			$value.="$value, $myver $myname";
			$via=1;
		} elseif (preg_match('/^X-Forwarded-For$/i',$header)) {
			$value.="$value, $_SERVER[REMOTE_ADDR]";
			$xff=1;
		} elseif (preg_match('/^(Proxy-Connection|Content-Length|Connection)$/i',$header)) $header='';
		if ($header) fputs ($fp, "$header: $value\r\n");
	}
	if (!$via) fputs ($fp, "Via: $myver $myname\r\n");
	if (!$xff) fputs ($fp, "X-Forwarded-For: $_SERVER[REMOTE_ADDR]\r\n");
	fputs ($fp, "Connection: close\r\n");

	if ($_SERVER[REQUEST_METHOD]=='POST') {
		if (!preg_match('#multipart/form-data;\s+boundary=([\w\-]+)#i',$_SERVER[CONTENT_TYPE],$marr)){
			if ($_SERVER[CONTENT_LENGTH]) fputs ($fp, "Content-Length: $_SERVER[CONTENT_LENGTH]\r\n");
			fputs ($fp, "\r\n");
			$input = fopen('php://input','r');
			while (!feof($input)) fputs ($fp,fread($input, 10240));
			fclose($input);
		} else {
			$postdata='';
			$boundary=$marr[1];
			foreach($_POST as $name=>$data) {
				$postdata .= '--'.$boundary."\r\n"
					.'Content-Disposition: form-data; name="'.$name.'"'."\r\n\r\n"
					.$data."\r\n"; // FIXME: If $data is an array, handle that correctly.
			}
			foreach($_FILES as $name=>$data) {
				if (!is_uploaded_file($data['tmp_name'])) continue;

				$postdata .= '--'.$boundary."\r\n"
				.'Content-Disposition: form-data; name="'.$name.'";'
				.' filename="'.$data['name'].'"'."\r\n";
				$postdata .= 'Content-Type: '.$data['type']."\r\n\r\n".file_get_contents($data['tmp_name'])."\r\n";
			}
			$postdata .= '--'.$boundary."--\r\n";
			fputs ($fp, "Content-Length: ".strlen($postdata)."\r\n");
			fputs ($fp,"\r\n".$postdata);
		}
		fflush($fp);
	} else {
		if ($_SERVER[CONTENT_LENGTH]) fputs ($fp, "Content-Length: $_SERVER[CONTENT_LENGTH]\r\n");
		fputs ($fp, "\r\n");
	}
	while ($line=fgets($fp)) {
		if (trim($line)=='') break;
		if (preg_match("#^HTTP/1.\d\s+(\d+)#i",$line,$arr)) $status=$arr[1];
		elseif (preg_match("/^Content-Encoding:/i", $line)) ini_set('zlib.output_compression', 'Off');
		elseif (preg_match("/^Content-Length:/i",$line)) $line='';
		elseif (preg_match("/^(Pragma|Cache-Control):/i", $line))
			if ($status<300 && preg_match("/no-cache/i", $line)) $line='';
#			else $line=preg_replace("/max-age=65536/","max-age=604800", $line);
		elseif (preg_match("#^Content-Type:\s+([^/]+)/(\S+)#i", $line,$content_type)) {
			if ($content_type[1]!='text') ini_set('zlib.output_compression', 'Off');
		}
		if ($line) {
			header($line);
#			$logheaders.=$line;
		}
	}
	while (!feof($fp)) echo fread($fp,10240);
	fclose ($fp);
}
#echo "<!-- proxy\n$logheaders\n\n".($postdata?$postdata:file_get_contents('php://input'))."\n-->";
?>

Comments (1)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.