tutorials.de Buch-Aktion 05/2012
ERLEDIGT
NEIN
ANTWORTEN
2
ZUGRIFFE
17999
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    Registriert seit
    Mar 2004
    Beiträge
    542
    Hallo!
    Ich möchte folgendes gerne per PHP realisieren:

    Ich suche ein PHP-Script, das wie ein Proxy funktioniert! Folgendermassen soll das Script laufen:

    1) Script aufrufen (gewünschte Seite wird z.B: per URL übergeben)
    2) Script lädt sich vom angegeben Server alle Daten des Zielservers auf den Server, auf dem das PHP Script läuft (natürlich nur die Sichtbaren Elemente (Grafiken, HTML Code usw).
    3) Die Seite wird dann an den Browser weitergeben (natürlich muss sie aus den Seiten auf dem Script-Server erstellt werden)

    Wäre natürlich klasse, wenn die Links, die man anwählt, dann genauso übertragen werden (Von Original-Server auf Script-Server laden und dann an Browser weitergeben)!

    Gibt es sowas?
    Habe ein Projekt auf Source-Forge gefunden, jedoch kann man das nicht wirklich!

    Dankeschön für die Hilfe!
     

  2. #2
    Registriert seit
    Dec 2002
    Ort
    Trier
    Beiträge
    17.502
    Blog-Einträge
    10
    Falls dir Apache mit dem Modul „mod_proxy“ zur Verfügung steht, könntest du das Ganze ohne PHP abwickeln.
     
    Markus Wulftange

  3. #3
    Avatar von Dennis Wronka
    Dennis Wronka Dennis Wronka ist offline Soulcollector
    Registriert seit
    Apr 2002
    Ort
    Hong Kong
    Beiträge
    12.296
    Blog-Einträge
    231
    Ist noch nicht ganz fertig, aber es funktioniert zum Teil schon.
    Hab schon eine Weile nicht mehr dran gearbeitet, weiss also nicht genau was jetzt alles laeuft und was noch fehlt.
    Die verwendete Klasse gibt's hier.
    phpproxy.php
    PHP-Code:
    <?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);
        }
    ?>
     
    PHP Class Collection - PHP-Klassen fuer PHP 5 (und Teilweise auch fuer PHP 4)
    Updates: Catcher 1.1, FTPConnection 1.2, MultiSQL 1.1, RSS2 1.1, SMTPConnection 1.4
    __________________
    EasyLFS - Hintergrundinformationen, Installationsanleitung, Softwareliste und Download
    EasyLFS Projektthread - Informationen, Status und Diskussion zu meiner Linux-Distribution
    __________________
    Ich bin die Schildkroete, mein Sohn. Ich habe das Universum erschaffen, aber bitte mach mir daraus keinen Vorwurf; ich hatte Bauchschmerzen.
    __________________
    Zitat Zitat von Friedrich Nietzsche
    Man muss noch Chaos in sich haben, um einen tanzenden Stern gebaeren zu koennen.

Ähnliche Themen

  1. TCP Proxy
    Von Peter86 im Forum .NET Web und Kommunikation
    Antworten: 0
    Letzter Beitrag: 06.10.09, 00:58
  2. Url mit Proxy
    Von Zero_Base im Forum .NET Café
    Antworten: 0
    Letzter Beitrag: 08.06.06, 17:02
  3. CVS & Proxy
    Von WiESi im Forum Internet, DSL & Flatrate
    Antworten: 1
    Letzter Beitrag: 22.12.04, 21:06
  4. Proxy
    Von Schnubbel im Forum Java
    Antworten: 1
    Letzter Beitrag: 17.09.03, 19:31
  5. Proxy
    Von Pollux im Forum Microsoft Windows
    Antworten: 9
    Letzter Beitrag: 18.04.01, 17:07