<?php
error_reporting(E_ALL);
require_once("httpconnection.class.php");
function spliturl($url)
{
$url_array=explode("://",$url);
if (count($url_array)>1)
{
$protocol=$url_array[0];
$url=$url_array[1];
}
else
{
$protocol="http";
$url=$url_array[0];
}
$url_array=explode("?",$url);
if (count($url_array)>1)
{
$parameters=$url_array[1];
}
else
{
$parameters="";
}
$url=$url_array[0];
$url_array=explode("/",$url);
if (count($url_array)>1)
{
$url=$url_array[0];
unset($url_array[0]);
$path=implode("/",$url_array);
}
else
{
$url=$url_array[0];
$path="";
}
$url_array=explode(":",$url);
if (count($url_array)>1)
{
$port=$url_array[1];
}
else
{
if ($protocol=="http")
{
$port=80;
}
elseif ($protocol=="https")
{
$port=443;
}
}
$host=$url_array[0];
$url=array("protocol"=>$protocol,"host"=>$host,"port"=>$port,"path"=>$path,"parameters"=>$parameters);
return $url;
}
function preparedata($data,$url)
{
//href
$data_array=array();
$replacements=array();
preg_match_all("/href=([\"]|['])[\S]*([\"]|['])/",$data,$data_array);
for ($x=0;$x<count($data_array[0]);$x++)
{
$extracted=substr($data_array[0][$x],6,strlen($data_array[0][$x])-7);
$replacement="href=\"http://localhost:8080/proxy/phpproxy.php?url=";
if (strpos($data_array[0][$x],$url['protocol'])==false)
{
$replacement.=$url['protocol']."://";
if (strpos($data_array[0][$x],$url['host'])==false)
{
$replacement.=$url['host'].":".$url['port'];
if (substr($extracted,0,1)!="/")
{
$replacement.="/";
}
}
}
$replacement.=$extracted."\"";
$replacements[]=$replacement;
}
$data=str_replace($data_array[0],$replacements,$data);
//src
$data_array=array();
$replacements=array();
preg_match_all("/src=([\"]|['])[\S]*([\"]|['])/",$data,$data_array);
for ($x=0;$x<count($data_array[0]);$x++)
{
$extracted=substr($data_array[0][$x],5,strlen($data_array[0][$x])-6);
$replacement="src=\"http://localhost:8080/proxy/phpproxy.php?url=";
if (strpos($data_array[0][$x],$url['protocol'])==false)
{
$replacement.=$url['protocol']."://";
if (strpos($data_array[0][$x],$url['host'])==false)
{
$replacement.=$url['host'].":".$url['port'];
if (substr($extracted,0,1)!="/")
{
$replacement.="/";
}
}
}
$replacement.=$extracted."\"";
$replacements[]=$replacement;
}
$data=str_replace($data_array[0],$replacements,$data);
//action
$data_array=array();
$replacements=array();
preg_match_all("/action=([\"]|['])[\S]*([\"]|['])/",$data,$data_array);
for ($x=0;$x<count($data_array[0]);$x++)
{
$extracted=substr($data_array[0][$x],8,strlen($data_array[0][$x])-9);
$replacement="action=\"http://localhost:8080/proxy/phpproxy.php?url=";
if (strpos($data_array[0][$x],$url['protocol'])==false)
{
$replacement.=$url['protocol']."://";
if (strpos($data_array[0][$x],$url['host'])==false)
{
$replacement.=$url['host'].":".$url['port'];
if (substr($extracted,0,1)!="/")
{
$replacement.="/";
}
}
}
$replacement.=$extracted."\"";
$replacements[]=$replacement;
}
$data=str_replace($data_array[0],$replacements,$data);
//for forms currently just post works, but not perfect
//url (css)
$data_array=array();
$replacements=array();
preg_match_all("/url\(([\"]|['])[\S]*([\"]|['])/",$data,$data_array);
for ($x=0;$x<count($data_array[0]);$x++)
{
$extracted=substr($data_array[0][$x],5,strlen($data_array[0][$x])-6);
$replacement="url(\"http://localhost:8080/proxy/phpproxy.php?url=";
if (strpos($data_array[0][$x],$url['protocol'])==false)
{
$replacement.=$url['protocol']."://";
if (strpos($data_array[0][$x],$url['host'])==false)
{
$replacement.=$url['host'].":".$url['port'];
if (substr($extracted,0,1)!="/")
{
$replacement.="/";
}
}
}
$replacement.=$extracted."\"";
$replacements[]=$replacement;
}
$data=str_replace($data_array[0],$replacements,$data);
return $data;
}
if (!empty($_GET['url']))
{
$url=spliturl($_GET['url']);
//echo $url['protocol'].' '.$url['host'].' '.$url['port'].' '.$url['path'].' '.$url['parameters'];
if ($url['protocol']=="https")
{
$http=new httpconnection($url['host'],$url['port'],true,$_SERVER['HTTP_USER_AGENT']);
}
else
{
$http=new httpconnection($url['host'],$url['port'],false,$_SERVER['HTTP_USER_AGENT']);
}
if (empty($_POST))
{
$data=$http->get($url['path'],$url['parameters']);
}
else
{
$filestring=false;
$mimestring=false;
if (!empty($_FILES))
{
$dir=md5(uniqid());
mkdir($dir);
$filekeys=array_keys($_FILES);
$fileitems=array();
$mimetypes=array();
for ($x=0;$x<count($filekeys);$x++)
{
if ($_FILES[$filekeys[$x]]['size']>0)
{
move_uploaded_file($_FILES[$filekeys[$x]]['tmp_name'],$dir."/".$_FILES[$filekeys[$x]]['name']);
$fileitems[]=$filekeys[$x]."=".$dir."/".$_FILES[$filekeys[$x]]['name'];
$mimetypes[]=$_FILES[$filekeys[$x]]['type'];
}
}
$filestring=implode("&",$fileitems);
$mimestring=implode(",",$mimetypes);
}
$postkeys=array_keys($_POST);
$postitems=array();
for ($x=0;$x<count($postkeys);$x++)
{
$postitems[]=$postkeys[$x]."=".$_POST[$postkeys[$x]];
}
$poststring=implode("&",$postitems);
if ($filestring!=false)
{
$data=$http->post($url['path']."?".$url['parameters'],$poststring,false,$filestring,$mimestring);
}
else
{
$data=$http->post($url['path']."?".$url['parameters'],$poststring);
}
if (!empty($_FILES))
{
for ($x=0;$x<count($filekeys);$x++)
{
unlink($dir."/".$_FILES[$filekeys[$x]]['name']);
}
rmdir($dir);
}
}
while (isset($data['head']['location']))
{
if (isset($data['head']['location']['parameters']))
{
$data=$http->get($data['head']['location']['uri'],$data['head']['location']['parameters']);
}
else
{
$data=$http->get($data['head']['location']['uri']);
}
}
if (isset($data['head']['contenttype']))
{
header("Content-Type:".$data['head']['contenttype']);
}
unset($data['head']['contenttype']);
if ((!isset($data['head']['contenttype'])) || (substr($data['head']['contenttype'],0,4)=="text"))
//if ((isset($data['head']['contenttype'])) && (substr($data['head']['contenttype'],0,4)=="text"))
{
$output=preparedata($data['body'],$url);
}
else
{
$output=$data['body'];
}
//echo nl2br(print_r($data['head'],true));
echo $output;
unset($http);
}
?>