Login   Register  
PHP Classes
elePHPant
Icontem

File: proxy.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Andrey Nikishaev  >  Little PHP Proxy  >  proxy.php  
File: proxy.php
Role: Application script
Content type: text/plain
Description: php proxy
Class: Little PHP Proxy
Show remote pages as if they from the current site
 

Contents

Class file image Download
<?php
/*

  Little PHP Proxy V1.0701

*/
class little_php_proxy {
       var 
$DOMAIN;
       var 
$POSTURL;
       var 
$PURL=array();
       var 
$RESPONSE=array();

       public function 
__construct() {
             
define(HOST,$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']);
       }

       public function 
close() {
               
unlink(session_id());session_destroy();header('Location: /');
       }

       public function 
query($U,$POST,$GET,$FILES,$COOKIE='') {


            if(
count($POST)) {

                foreach(
$POST as $key => $value) {
                if(
$key!='u' && $key!='c'$POSTVARS.=$key."=".$value."&";
                }
                if(
$POSTVARS$posted=1;
            }

            if(
count($FILES)) {
                
$files=1;
                foreach(
$FILES as $key => $value) {
                
$POSTVARS.=$key."=@".$value['tmp_name']."&";
                }
            }

            
$POSTVARS=$POSTVARS?substr($POSTVARS,0,-1):'';

            
$U=urldecode($U);
            if(!
eregi('[.]+',$U)) $U=base64_decode($U);
            if(!
eregi('^(http|ftp|https)',$U)) $U='http://'.$U;
            
$this->POSTURL=$U;
            foreach(
$GET as $k=>$v) {
            if(
$k!='u' && $k!='c'$this->POSTURL.='&'.$k.'='.$v;
            }
            
$this->PURL=parse_url($U);
            
$this->DOMAIN=$this->PURL['host'].$this->PURL['path'];
            if(
ereg('.',$this->PURL['path'])) $this->DOMAIN=ereg_replace('/[^\/]*$','',$this->DOMAIN);
             
define(PHOST,$this->PURL['host']);
             
define(DOMAIN,$this->DOMAIN);
             
$ch curl_init($this->POSTURL);
             if(
$postedcurl_setopt($chCURLOPT_POST,                                                0);
                           
curl_setopt($chCURLOPT_VERBOSE,                                          0);
                         
curl_setopt($chCURLOPT_USERAGENT,                      $_SERVER['user-agent']);
             if(
$postedcurl_setopt($chCURLOPT_POSTFIELDS,                                $POSTVARS);
             if(
$files)  curl_setopt($chCURLOPT_UPLOAD,                                            1);
                         
curl_setopt($chCURLOPT_FOLLOWLOCATION,                                    1);
                         
curl_setopt($chCURLOPT_BINARYTRANSFER,                                     1);
                         
curl_setopt($chCURLOPT_SSL_VERIFYPEER,                                     0);
                         
curl_setopt($chCURLOPT_SSL_VERIFYHOST,                                     0);
                         
curl_setopt($chCURLOPT_REFERER,                               "http://".$this->DOMAIN);
                         
curl_setopt($chCURLOPT_CONNECTTIMEOUT,                                     0);
                         
curl_setopt($chCURLOPT_AUTOREFERER,                                        0);
                         
curl_setopt($chCURLOPT_COOKIEJAR,                       'ses_'.session_id());
                         
curl_setopt($chCURLOPT_COOKIEFILE,                       'ses_'.session_id());
                         if(
strlen($COOKIE)>0curl_setopt($chCURLOPT_COOKIE,          $COOKIE);
                         
curl_setopt($chCURLOPT_RETURNTRANSFER,                                    1);
                         
curl_setopt($chCURLOPT_FAILONERROR,                                      1);


             
$this->CONTENT curl_exec($ch);
             
$this->RESPONSE=curl_getinfo($ch);
             
curl_close($ch);
       }

       public function 
getPage() {
                if(
ereg('html',$this->RESPONSE['content_type'])) {
                     
$this->get_html();
                     
$this->get_css();
             } elseif (
ereg('image',$this->RESPONSE['content_type'])){
                    
$this->get_image();
             } elseif (
ereg('javascript',$this->RESPONSE['content_type'])){
                    
$this->get_javascript();
             } elseif (
ereg('css',$this->RESPONSE['content_type'])){
                    
$this->get_css();
             } else {
                    
$this->get_file();
             }
             
header("Content-Type: ".$this->RESPONSE['content_type']);
             if(
eregi('image/([a-z]+)',$this->RESPONSE['content_type'],$type)) {
                  
$newImg imagecreatefromstring($this->CONTENT);
                 if(
$type[1]="jpeg")imagejpeg($newImg,'',100);
                 if(
$type[1]="png")imagegif($newImg,'',100);
                 if(
$type[1]="gif")imagepng($newImg,'',100);
             } else {
                 echo 
$this->CONTENT;
             }
       }

       public function 
get_file() {

       }

       public function 
get_image() {

       }

       public function 
get_javascript() {
            
$patern=array(
            
'/([\"]+)([^\"\s]*)([\"]+)/i',
            
'/([\']+)([^\'\s]*)([\']+)/i'
             
);
            function 
replace_js($m) {
                  
$d='';
                  if(
eregi('^\/',$m[2])) {
                          
$d='http://'.PHOST;$ok=1;
                  } else {
                            if(!
eregi('^(htt|ftp)',$m[2]) && eregi('/',$m[2])) {$d='http://'.DOMAIN.'/';$ok=1;}
                  }
                  if(
$ok) {
                          return 
$m[1].'http://'.HOST.'?u='.base64_encode($d.$m[2]).$m[1];
                  } else {
                        return 
$m[1].$m[2].$m[1];
                  }

             }
            
$this->CONTENT=preg_replace_callback($patern,"replace_js",$this->CONTENT);
       }

       public function 
get_css() {
              
$patern=array(
              
'/url\([\s]*[\'\"\`]?([^\)\s\'\"\`]+)[\'\"\`]?[\s]*\)/i'
              
);
              function 
replace_css($m) {
                  
$d='';
                  if(
eregi('^\/',$m[1])) {
                          
$d='http://'.PHOST;$ok=1;
                  } else {
                            if(!
eregi('^(htt|ftp)',$m[1])) {
                            
$d='http://'.DOMAIN.'/';$ok=1;
                        } else {
                            
$ok=1;
                        }
                  }

              if(
$ok) {
                      return 
'url(http://'.HOST.'?u='.base64_encode($d.$m[1]).')';
              }


             }
            
$this->CONTENT=preg_replace_callback($patern,"replace_css",$this->CONTENT);
       }

       public function 
get_html() {
               
$patern=array(
            
'/[\s]+(src|href|url|location|background|action)[\s]*=[\s]*([\'\"\`])?[\s]*([^\'\"\`\s>]+)([\'\"\`>])?/i',
            
'/([\"]+)(\/[^\"\s]*)([\"]+)/i',
            
'/([\']+)(\/[^\'\s]*)([\']+)/i'
             
);
             function 
replace_html($m) {
              
$d='';
            if(
$m[1]=='"' || $m[1]=="'") {
            if(
eregi('^\/',$m[2])) {
                  
$d='http://'.PHOST;$ok=1;
              } else {
                    if(!
eregi('^(htt|ftp)',$m[2]) && eregi('/',$m[2])) {$d='http://'.DOMAIN.'/';$ok=1;}
              }
             if(
$ok) {return $m[1].'http://'.HOST.'?u='.base64_encode($d.$m[2]).$m[1];}
                else {
                    return 
$m[1].$m[2].$m[1];
            }

            } else {
              if(
$m[4]=='>'$e=">";
              if(
eregi('^\/',$m[3])) {
                  
$d='http://'.PHOST;
             } else {
                    if(!
eregi('^(htt|ftp)',$m[3])) $d='http://'.DOMAIN.'/';
              }
            if(!
ereg('^java',$m[3])) {
                    return 
' '.$m[1].'="http://'.HOST.'?u='.base64_encode($d.$m[3]).'"'.$e;
            } else {
                    return 
' '.$m[1].'="'.$m[3].'"';
            }
            }
             }
             
$this->CONTENT=preg_replace_callback($patern,'replace_html',$this->CONTENT);
       }


session_start();
    
        
$P=new little_php_proxy();
        
$P->query($_REQUEST['u'],$_POST,$_GET,$_FILES,$_POST['c']);
        
$P->getPage();






?>