<?php
$pathinfo = '';
handleRequest("[URL unfurl="true"]www.google.co.uk");[/URL] //this is the default url for proxying
function rewriteLinks($output){
//a VERY crude link rewriter
$patterns = array (
'/(<.*?href=)["\'](.*?)["\']/i',
'/(<.*?action=)["\'](.*?)["\']/i',
'/(<.*?src=)["\'](.*?)["\']/i');
foreach ($patterns as $pattern){
$output = preg_replace_callback($pattern,'rewriteMe', $output);
}
return $output;
}
function rewriteMe($matches){
//need the pathinfo to make partial links into absolute uri's
//simpler to do it now than later
global $pathinfo;
$base = $pathinfo['scheme'].'://'.$pathinfo['host'];
//$matches[2] contains the url and the query string
//get rid of trailing spaces
$encodeURL = trim($matches[2]);
$curAddress = '[URL unfurl="true"]http://'.$_SERVER[/URL]['HTTP_HOST'].$_SERVER['PHP_SELF'];
//test for absolute uri
//and return an absolute version
$lower = strtolower(substr($encodeURL, 0, 4));
if ($lower === 'http'){
return $matches[1]."\"$curAddress?proxy=$encodeURL\"";
} else {
return $matches[1]."\"$curAddress?proxy=$base$encodeURL\"";
}
}
function parseIncomingProxyRequest($url=NULL){
//this just returns the desired url
if (isset($_GET['proxy'])){
return (trim($_GET['proxy']));
}else{
return $url;
}
}
function getPostVars(){
//used to encode post variables and format them for the cURL request
if ( count($_POST) >0 ){
foreach ($_POST as $key=>$var){
$return[] = urlencode(trim($key)) ."=" .urlencode(trim($var));
}
}
if (empty($return)){
return '';
} else {
return implode($return, '&');
}
}
function handleRequest($url=NULL){
//everything hangs from this function
global $pathinfo;
//get the url
$url = parseIncomingProxyRequest($url);
if (empty($url)){
die ("no address to proxy");
}
//need the segmented url later
$pathinfo = parse_url($url);
//check for post
$postData = getPostVars();
//grab the page contents
$pageContents = makeRequest($url, $postData);
//rewrite the links
$pageContents = rewriteLinks($pageContents, $pathinfo);
//bash it out to the user
echo $pageContents;
}
function makeRequest($url, $postData=null){
//star the cURL session
$ch = curl_init($url);
$cookiejar = "cookiejar.txt";
$cookiefile = "cookies.txt";
//set options
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //into varaible
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //follow redirects
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); //send user information in case needed
curl_setopt($ch, CURLOPT_USERPWD, 'username:password'); //parameters
if (!empty($postData)){ //check for post data. if present switch method to post and attach the posted data
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDs, $postData);
}
//handle cookies and make a windows fix
//from php manual.
//only neeeded if the file addresses for the cookie files at the top of this function are changed
if (substr(PHP_OS, 0, 3) == 'WIN'){
$cookies = str_replace('\\','/', getcwd().'/'.$cookies);
curl_setopt($ch, CURLOPT_CRLF, TRUE); //convert for windows
}
//set the cookie options
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiejar);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
//make the request
$r = curl_exec($ch);
//close the cURL instance
curl_close($ch);
//return the incoming web page
return $r;
}
?>