Page 1 of 1

PHP code needs an expert viewing please

Posted: Sun Apr 05, 2015 7:38 pm
by dbsalop52
Hi Guys :-)
First time poster here and a total coding n00b (I'll admit that straight away). Im trying to get this proxy script to work and ultimately use it for unblocking isp blocked websites.
The code below is pieced together from other peoples work and some of it is about 4 or 5 years old.
It works. but it is painfully slow.
There are probably some errors that will stand out to most of you, so rather than spend the next few days banging my head against a wall Im here for your expert help.
Thank you in advance to anyone that takes the time to scan though the code.
Dan

index.php

Code: Select all

<?php

session_start();
ob_start();

/* config settings */
$base = "ANY-WEBSITE-URL.COM";  
$ckfile = '/tmp/simpleproxy-cookie-'.session_id();  


//work out cookie domain
$cookiedomain = str_replace("http://www.","",$base);
$cookiedomain = str_replace("https://www.","",$cookiedomain);
$cookiedomain = str_replace("www.","",$cookiedomain);

$url = $base . $_SERVER['REQUEST_URI'];

if($_SERVER['HTTPS'] == 'on'){
	$mydomain = 'https://'.$_SERVER['HTTP_HOST'];
} else {
	$mydomain = 'http://'.$_SERVER['HTTP_HOST'];
}

// Open the cURL session
$curlSession = curl_init();

curl_setopt ($curlSession, CURLOPT_URL, $url);
curl_setopt ($curlSession, CURLOPT_HEADER, 1);


if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $postinfo = '';
        foreach($_POST as $key=>$value) {
                $postinfo .= $key.'='.urlencode($value).'&';
        }
        rtrim($postinfo,'&');


        curl_setopt ($curlSession, CURLOPT_POST, 1);
        curl_setopt ($curlSession, CURLOPT_POSTFIELDS, $postinfo);
}

curl_setopt($curlSession, CURLOPT_RETURNTRANSFER,1);
curl_setopt($curlSession, CURLOPT_TIMEOUT,30);
curl_setopt($curlSession, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt ($curlSession, CURLOPT_COOKIEJAR, $ckfile); 
curl_setopt ($curlSession, CURLOPT_COOKIEFILE, $ckfile);

//handle other cookies cookies
foreach($_COOKIE as $k=>$v){
	if(is_array($v)){
		$v = serialize($v);
	}
	curl_setopt($curlSession,CURLOPT_COOKIE,"$k=$v; domain=.$cookiedomain ; path=/");
}

//Send the request and store the result in an array
$response = curl_exec ($curlSession);

// Check that a connection was made
if (curl_error($curlSession)){
        // If it wasn't...
        print curl_error($curlSession);
} else {

	//clean duplicate header that seems to appear on fastcgi with output buffer on some servers!!
	$response = str_replace("HTTP/1.1 100 Continue\r\n\r\n","",$response);

	$ar = explode("\r\n\r\n", $response, 2); 


	$header = $ar[0];
	$body = $ar[1];

	//handle headers - simply re-outputing them
	$header_ar = split(chr(10),$header); 
	foreach($header_ar as $k=>$v){
		if(!preg_match("/^Transfer-Encoding/",$v)){
			$v = str_replace($base,$mydomain,$v); //header rewrite if needed
			header(trim($v));
		}
	}

  //rewrite all hard coded urls to ensure the links still work!
	$body = str_replace($base,$mydomain,$body);

  //code change
  //code change
  //REMOVE AD IFRAMES
  $body = str_replace("<iframe","<!--<iframe", $body);
  $body = str_replace("</iframe>","</iframe> -->", $body);
  //REMOVE AD IFRAMES
  //code change
  //code change


	print $body;

}

curl_close ($curlSession);


?>
htaccess

Code: Select all

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php [L]

Re: PHP code needs an expert viewing please

Posted: Mon Apr 06, 2015 10:59 am
by Christopher
Other than that str_replace() can take an array and you could use http_build_query(), the script looks fairly simple. The main issue is that you say it is slow. My guess is that is mostly the time it takes to do the cURL request. And why are you using output buffering? Everything is printed on one line, and the ob_end_flush() is missing to the shutdown has to do it.