DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Proxy Transparent Cross-domain

02.02.2011
| 6027 views |
  • submit to reddit
        
<?php
/**
* @filename: proxy.php
*
* Proxy transparent cross-domain
* tranport cookie and headers
*    @example
*       http://frameinoves/proxy.php?url=http://frameinoves/ola.php"
*
*
* website: http://inov.es
* @autor Steven Koch<steven.koch@sapo.pt>
*/

/*
* Permission url (whitelist)
*    Only urls that match with list are permitted!
*    - try short prefixOfCookie, how:
* @example: $whiteListURLs = array('_iPxFb'=>'http://facebook.com','_iPxGm'=>'http://mygame.com',etc..);
*/
$whiteListURLs = array('prefixOfCookie'=>'http://frameinoves/ola.php');




//----NO MORE EDITABLE----
//check permission
foreach($whiteListURLs as $prefix=>$checkUrl){
	if(strpos($checkUrl, urldecode($_GET['url']))==0){
		define( 'PREFIX_COOKIE', $prefix );
		define( 'URL', urldecode($_GET['url']) );
	}
}

//make resource
$ch =  curl_init();
curl_setopt($ch, CURLOPT_URL, URL);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, getHeadersFromServer(true) );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);

//get contents
$response = curl_exec( $ch );
list($response_headers, $response_body) = explode("\r\n\r\n", $response, 2);

//send your header
$ary_headers = explode("\n", $response_headers );

foreach($ary_headers as $hdr) {

	if(strpos($hdr, 'Set-Cookie:')!==false){
		$hdr = str_replace('Set-Cookie: ', '', $hdr);
		$ahdr = explode("\n", $hdr);
		foreach($ahdr as $k=>$v){
			$ahdr[$k] = PREFIX_COOKIE.$v;
		}
		header('Set-Cookie: '. implode("\n", $ahdr));
	}else{
		header($hdr);
	}
}
echo $response_body;
exit(0);

//---
function getHeadersFromServer($onlyCookie=false)
{
    $headers = array();
	if($onlyCookie){
        $headers[] = 'Cookie: '.preg_replace('/'.PREFIX_COOKIE.'(.+=)?/', '$1', $_SERVER['HTTP_COOKIE']);
	}else{
	    foreach ($_SERVER as $k => $v)
	    {		
			if (substr($k, 0, 5) == "HTTP_")
		    {
		        $k = str_replace('_', ' ', substr($k, 5));
		        $k = str_replace(' ', '-', ucwords(strtolower($k)));
				if($k=='Cookie')
        			$headers[] = 'Cookie: '.preg_replace('@'.PREFIX_COOKIE.'(.+=)?@', '${1}', $v);
				else
		        	$headers[] = $k.': '.$v;
		    }
		}
    }
    return $headers;
}