...

View Full Version : PHP proxy script



cxn
08-28-2009, 11:50 AM
Hi, I've download a php proxy script but I can't manage to get it to work.

error:


Warning: array_shift() [function.array-shift]: The argument should be an array in /home/cxn/public_html/pr/classWebPage.php on line 84

Warning: array_shift() [function.array-shift]: The argument should be an array in /home/cxn/public_html/pr/URLproxyServer.php on line 83

Warning: Invalid argument supplied for foreach() in /home/cxn/public_html/pr/URLproxyServer.php on line 69

Warning: Cannot modify header information - headers already sent by (output started at /home/cxn/public_html/pr/classWebPage.php:84) in /home/cxn/public_html/pr/URLproxyServer.php on line 140


Files:

URLproxyServer.php


<?


// User should not have to edit this file
include("config.php");
include("classWebPage.php");

// Constants
$STATIC = 1;
$DYNAMIC = 2;

// Mode of the script
$proxyMode = ((isset($server) AND isset($redirectIP)) ? $STATIC : $DYNAMIC);


// Version Number
$phase = "pre-alpha";
$version = "1.3 " . $phase;

/*Flags******************************************\
* $isHTML; true if mimetype is html *
* $isImage; true if mimetype is an image *
* $isDown; true if specified mimetype is to be *
* downloaded *
* $isAuth; true if the page is protected by *
* .htaccess *
* $isForm; true if the page contains a form *
\************************************************/
$isHTML = false;
$isImage = false;
$isDown = false;
$isAuth = false;
$isForm = false;
$isError = false;

/*getContentType*********************************\
* Function for finding the mime type of the file *
* Returns the content type *
\************************************************/
function getContentType($headers){
foreach($headers as $h){
if(eregi("^Content-Type: (.*)$",$h,$A1))
return $A1[1];
}
}

/*processHeaders*********************************\
* Function for handling the headers that apache *
* sends out. *
* Returns an array with the headers that should *
* be sent out *
\************************************************/
function processHeaders($headers, $fileName, $mime_dl, &$type,
&$isDown, &$isHTML, &$isImage){
array_shift($headers);
$type = getContentType($headers);
$isDown = (isset($mime_dl[$type]) ? $mime_dl[$type] : true);
if(eregi("image",$type))
$isImage = true;
elseif(eregi("text/html",$type))
$isHTML = true;
if($isDown)
$headers[] = "Content-Disposition: attachment;" .
" filename=$fileName";
return $headers;
}

/************************************************\
* This block of code gets the directory we are *
* currently in, for rel links. *
\************************************************/
if(eregi("http://",$_GET[$fileVar])){
$relDir = eregi_replace("^http://[^/]*", "", $_GET[$fileVar]);
}else{
$relDir = $_GET[$fileVar];
}
$relDir = eregi_replace("/[^/]*$","/",$relDir);

/************************************************\
* We create a new object of type WebPage and *
* pass it the url we are being a proxy for and *
* other information about the current state. *
\************************************************/
$page = new WebPage($redirectIP."/".$_GET[$fileVar],true,$server,"URLproxyServer.php",$fileVar,$relDir);

/************************************************\
* This tells the WebPage object to open up a *
* connection to the URL. *
* *
* Note: *
* This does not actually get the web page, just *
* opens the connection for the headers. *
\************************************************/
$page->openLink();

/************************************************\
* Process the headers so we know what kind of *
* data we have (html/other) *
\************************************************/
$head = processHeaders($page->getHeaders(),$file,$mime_dl,$type,$isDown,$isHtml,$isImage);

/************************************************\
* This code replicates the headers that were *
* sent when the class connected to the url. *
* *
* FIXME: extra headers need to be sent if we are *
* downloading the file. *
* *
* GOTCHA?: need to check if http 1.1 will work *
* correctly *
\************************************************/
foreach($head as $h) header($h);

/************************************************\
* This block of code displays the page to the *
* user. *
* *
* Note: Both processPageData and getRawPageData *
* close the connection to the URL when they *
* return. You must re-open a connection with *
* openLink to use them again. *
\************************************************/
if($isHtml){
$page->processPageData();
if($encryptPage)
$page->encryptPage();
echo $page->getPageData();

}else{
if($isImage)
$page->getRawPageData();
}
?>





classWebPage.php


<?

/*Class WebPage**********************************\
* Come along with me into the world of ooPHP. *
* *
* WebPage is the main class in this script. It *
* controls getting pages as well as headers and *
* processing those pages. *
\************************************************/
class WebPage {

/*Var Descriptions*******************************\
* $URL The url we are proxying for *
* $pageData The raw data from the file *
* $headers Array containing the headers sent *
* $static Boolean if we are working on only rel *
* links *
* $currentServer String with the script's server *
* $scriptName Name of the main script *
* $varName Name of the var for passed in file *
* $updatedPageData The processed page data *
* $relDir The directory we're in for rel links *
* $fp The file pointer for reading the file *
\************************************************/
var $URL;
var $pageData;
var $headers;
var $static;
var $currentServer;
var $scriptName;
var $varName;
var $updatedPageData;
var $relDir;
var $fp;

/*Class Constructor******************************\
* The main constructor, pass in varible are *
* listed below. *
* *
* $URL The url we are proxying for *
* $static Boolean if we are working on only rel *
* links *
* $currentServer String with the script's server *
* $scriptName Name of the main script *
* $varName Name of the var for passed in file *
* $relDir The directory we're in for rel links *
\************************************************/

function WebPage($URL, $static, $currentServer, $scriptName, $varName, $relDir){
$this->URL = $URL;
$this->currentServer = $currentServer;
$this->static = $static;
$this->relDir = $relDir;
$this->pageData = "";
$this->varName = $varName;
$this->scriptName = $scriptName;
}

/*openLink***************************************\
* Function for connecting to the URL and getting *
* the headers. *
* *
* Note: The connection is closed by *
* getRawPageData or processPage, whichever comes *
* first. *
\************************************************/
function openLink(){
$this->fp = fopen($this->URL, "rb");
array_shift($http_response_header);
$this->headers = $http_response_header;
}

/*getHeaders*************************************\
* Function returns an array containing the *
* headers that resulted from the page. *
\************************************************/
function getHeaders(){
return $this->headers;
}

/*getRawPageData*********************************\
* Prints out the $fp as a stream without *
* processing (for images/such) *
\************************************************/
function getRawPageData(){
fpassthru($this->fp);
fclose($this->fp);
return;
}

/*getPageData************************************\
* Function returns a string containing the *
* page data. processPageData must be used *
* before this function is called. *
\************************************************/
function getPageData(){
return $this->updatedPageData;
}

/*processPageData********************************\
* Function process the page, fixing links and *
* images to use the proxy as specified by the *
* class. *
* *
* Note: Add items here if you want to add extra *
* prefixes or delims. *
\************************************************/
function processPageData(){
$this->pageData = "";
while( !feof( $this->fp)){
$this->pageData .= fgets($this->fp, 4096);
}
fclose($this->fp);
$delim[]='"';
$delim[]="'";
$delim[]="";
$pre[]="src=";
$pre[]="background=";
$pre[]="href=";
$pre[]="url\(";
$pre[]="codebase=";
$pre[]="url=";
$pre[]="archive=";
$this->redirect($pre,$delim);
}

/*fileName***************************************\
* Function returns the name of the file that the *
* URL specifies. *
\************************************************/
function fileName(){
return eregi_replace("[#?].*$", "",
eregi_replace("^.*/", "", $this->URL));
}

/*encodeHTML*************************************\
* Fix Me: *
* This is not done yet but the idea is to change *
* all the html charcters to their special char *
* information (to keep people from stealing your *
* source and using it) *
\************************************************/
function encodeHTML(){
// Not Done Yet
}

/*encryptPage************************************\
* Function changes updatedPageData so that the *
* file is encrypted while sending. *
\************************************************/
function encryptPage(){
$data2 = "";
foreach (split("\n",eregi_replace("\r","",$this->updatedPageData)) as $a){
$data = $this->encrypt(chop($a));
$data = str_replace( "\\", "\\\\", $data);
$data = str_replace( "\"", "\\\"", $data);
$data2 .= "document.write( c(\"".$data."\") );\n";
}
$this->updatedPageData = ""
. "<!-- URL Proxy Server\n"
. "Javascript by Bob Matcuk\n"
. "BMatcuk@Users.SourceForge.Net -->\n"
. "<script language=\"Javascript\">\n"
. " function c(s){\n"
. " var fi = \"\";\n"
. " for( var i = 0; i < s.length-1; i += 2 ){\n"
. " fi = fi + s.charAt(i+1) + s.charAt(i);\n"
. " }\n"
. " if( i < s.length ){\n"
. " fi = fi + s.charAt(i);\n"
. " }\n"
. " fi = fi + \"\\n\";\n"
. " return fi;\n"
. " }\n" . " document.open();\n"
. $data2
. " document.close();\n"
. "</script>\n";
}

/*redirect***************************************\
* Private Function, DO NOT USE FROM PUBLIC SCOPE *
* Basically converts the prefixes in *
* $prefixArray and the delim in $delimArray to a *
* string and uses it to fix links within the *
* page. *
\************************************************/
function redirect($prefixArray,$delimArray){
$a = $this->pageData;
$name = $this->currentServer;
$fileDir = $this->relDir;
foreach($prefixArray as $prefix){
$start2 = stripslashes($prefix);
$start = $prefix . "[ ]*";
foreach($delimArray as $delim){
if(eregi($prefix . "[ ]*" . $delim, $a) && ($delim == "" ? eregi($prefix . "[ ]*" . "[a-z,A-Z,/,0-9]", $a) : 1)){
$a = eregi_replace($start . $delim . "//",
$start2 . '\a' . "//",
$a);
$a = eregi_replace($start . $delim . "/",
$start2 . $delim . $name . "/" . $this->scriptName . "?" . $this->varName . "=/",
$a);
$a = eregi_replace($start . $delim . "http://",
$start2 . '\a' . "http://",
$a);
$a = eregi_replace($start . $delim . "mailto:",
$start2 . '\a' . "mailto:",
$a);
$a = eregi_replace($start . $delim . "#",
$start2 . '\a' . "#",
$a);
$a = eregi_replace($start . $delim . "javascript:",
$start2 . '\a' . "javascript:",
$a);
$a = eregi_replace($start . $delim,
$start2 . $delim . $name . "/" . $this->scriptName . "?" . $this->varName . "=" . $fileDir ,
$a);
$a = eregi_replace($start . '[\]a',
$start2 . $delim,
$a);
}
}
}
$this->updatedPageData = $a;
}

/*encrypt****************************************\
* This function encrypts the page before it is *
* sent out. *
* Returns the file as a string, encrypted *
\************************************************/
function encrypt( $string ){
for( $i = 0; $i < strlen( $string ) - 1; $i += 2 ){
$temp = $string[$i];
$string[$i] = $string[$i+1];
$string[$i+1] = $temp;
}
return $string;
}
}
?>







config.php


<?

global $mime_dl;
global $server;
global $redirectIP;
global $fileVar;
global $encryptPage;
/*Mime Types***********************\
* These are mime types you don't *
* want to have downloaded by *
* default (false is required) *
\**********************************/
$mime_dl["text/html"]=false;
$mime_dl["application/pdf"]=false;
$mime_dl["application/x-javascript"]=false;
$mime_dl["application/x-shockwave-flash"]=false;
$mime_dl["image/bmp"]=false;
$mime_dl["image/gif"]=false;
$mime_dl["image/jpeg"]=false;
$mime_dl["image/tiff"]=false;
$mime_dl["text/css"]=false;
$mime_dl["text/directory"]=false;
$mime_dl["text/plain"]=false;

/*Server Vars***********************\
* Change these to match your server *
* Comment out redirectIP to have *
* the proxy work on all ips (by *
* absolute URLs) *
\***********************************/
$server = "my server ip, right?";
// $redirectIP = " ... ";
$fileVar="www.google.com"; I made it google just to try
?>




Can anyone help me out? Thanks

the-dream
08-28-2009, 12:15 PM
You may get a response, if you actually tell us which file is which.

cxn
08-28-2009, 12:20 PM
Oh, thought it said so in the commentings, my bad. Editing now

the-dream
08-28-2009, 12:47 PM
Replace...

URLProxyServer.php with:

<?
ob_start();
// User should not have to edit this file
include("config.php");
include("classWebPage.php");

// Constants
$STATIC = 1;
$DYNAMIC = 2;

// Mode of the script
$proxyMode = ((isset($server) AND isset($redirectIP)) ? $STATIC : $DYNAMIC);


// Version Number
$phase = "pre-alpha";
$version = "1.3 " . $phase;

/*Flags******************************************\
* $isHTML; true if mimetype is html *
* $isImage; true if mimetype is an image *
* $isDown; true if specified mimetype is to be *
* downloaded *
* $isAuth; true if the page is protected by *
* .htaccess *
* $isForm; true if the page contains a form *
\************************************************/
$isHTML = false;
$isImage = false;
$isDown = false;
$isAuth = false;
$isForm = false;
$isError = false;

/*getContentType*********************************\
* Function for finding the mime type of the file *
* Returns the content type *
\************************************************/
function getContentType($headers){
foreach($headers as $h){
if(eregi("^Content-Type: (.*)$",$h,$A1))
return $A1[1];
}
}

/*processHeaders*********************************\
* Function for handling the headers that apache *
* sends out. *
* Returns an array with the headers that should *
* be sent out *
\************************************************/
function processHeaders($headers, $fileName, $mime_dl, &$type,
&$isDown, &$isHTML, &$isImage){
array_shift($headers);
$type = getContentType($headers);
$isDown = (isset($mime_dl[$type]) ? $mime_dl[$type] : true);
if(eregi("image",$type))
$isImage = true;
elseif(eregi("text/html",$type))
$isHTML = true;
if($isDown)
$headers[] = "Content-Disposition: attachment;" .
" filename=$fileName";
return $headers;
}

/************************************************\
* This block of code gets the directory we are *
* currently in, for rel links. *
\************************************************/
if(eregi("http://",$_GET[$fileVar])){
$relDir = eregi_replace("^http://[^/]*", "", $_GET[$fileVar]);
}else{
$relDir = $_GET[$fileVar];
}
$relDir = eregi_replace("/[^/]*$","/",$relDir);

/************************************************\
* We create a new object of type WebPage and *
* pass it the url we are being a proxy for and *
* other information about the current state. *
\************************************************/
$page = new WebPage($redirectIP."/".$_GET[$fileVar],true,$server,"URLproxyServer.php",$fileVar,$relDir);

/************************************************\
* This tells the WebPage object to open up a *
* connection to the URL. *
* *
* Note: *
* This does not actually get the web page, just *
* opens the connection for the headers. *
\************************************************/
$page->openLink();

/************************************************\
* Process the headers so we know what kind of *
* data we have (html/other) *
\************************************************/
$head = processHeaders($page->getHeaders(),$file,$mime_dl,$type,$isDown,$isHtml,$isImage);

/************************************************\
* This code replicates the headers that were *
* sent when the class connected to the url. *
* *
* FIXME: extra headers need to be sent if we are *
* downloading the file. *
* *
* GOTCHA?: need to check if http 1.1 will work *
* correctly *
\************************************************/
foreach($head as $h) header($h);

/************************************************\
* This block of code displays the page to the *
* user. *
* *
* Note: Both processPageData and getRawPageData *
* close the connection to the URL when they *
* return. You must re-open a connection with *
* openLink to use them again. *
\************************************************/
if($isHtml){
$page->processPageData();
if($encryptPage)
$page->encryptPage();
echo $page->getPageData();

}else{
if($isImage)
$page->getRawPageData();
}
?>

It wont fix all of the errors, so do that change, and then post the remaining errors, so we can work through them one by one.

cxn
08-28-2009, 01:02 PM
Thanks for your help :-)



Warning: array_shift() [function.array-shift]: The argument should be an array in /home/cxn/public_html/pr/classWebPage.php on line 84

Warning: array_shift() [function.array-shift]: The argument should be an array in /home/cxn/public_html/pr/URLproxyServer.php on line 54

Warning: Invalid argument supplied for foreach() in /home/cxn/public_html/pr/URLproxyServer.php on line 40

the-dream
08-28-2009, 01:07 PM
Out of curiosity... which downloaded script are you using...? Link?

cxn
08-28-2009, 01:11 PM
Out of curiosity... which downloaded script are you using...? Link?

Hmm can't remember, I can upload the zzip file if you want.

Called PHP-PROXY_dean

the-dream
08-28-2009, 01:16 PM
Odd I just downloaded it... (Link: http://sourceforge.net/projects/php-proxy/).

And it works fine.

cxn
08-28-2009, 01:25 PM
Odd I just downloaded it... (Link: http://sourceforge.net/projects/php-proxy/).

And it works fine.

Hmm wow :o
What does that mean? I either download an edited file or .. something wrong with the way it's on my host?
I just uploaded them, didn't do anything more though.

the-dream
08-28-2009, 01:26 PM
Re-download them... and READ the documentation, ALL OF IT.

cxn
08-28-2009, 01:34 PM
The comments in the file you mean?

Done it, only edited the config.php file again, and again the same errors.

cxn
08-28-2009, 11:41 PM
Anyone? :[

Zangeel
08-28-2009, 11:48 PM
Looks like the $header variable is not an array. Therefore, using array_shift or foreach would be an invalid argument, since they need arrays.

cxn
08-28-2009, 11:56 PM
How come it works for the-dream?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum