<?php

/*
www-proxy thingie
04.04.2004

Elementary web proxy. Should cover your basic surfing.

REQUIRES
* web server with php support (and fopen()ing http:// streams)

FEATURES
* web based surfing proxy
* translates HREF, SRC and ACTION (absolute, relative, queries)
* basic authentication
* HTTP GET

DOES NOT AND WILL NOT
* HTTP anything except GETs (not supported by php. cant be bothered to code)
* javascript pop-ups, location.href-mumbo-jumbo. cant be bothered.

MIGHT
* obfuscate urls? so the cache wouldn't make sense? need this, contact me.

Use cases:
Some sites are blocked in your shcool/at work. Put it on your web-server.

You sometimes need some privacy agains sniffing on untrusted networks. Put it on
your https server

Thats it.

no copyright or whatnot. In the public domain.
windo@windowlicker.dyn.ee
*/


// Get $user and $pass
include_once('configure.php');

// Tango with the user input
$luser $_COOKIE['user'];
$lpass $_COOKIE['pass'];

if(!
$luser|!$lpass) {
    
$luser $_POST['user'];
    
$lpass $_POST['pass'];
    
setcookie('user'$luser);
    
setcookie('pass'$lpass);
    if(!
$luser|!$pass) {
        echo 
"<form method='post'><input type='text' name='user'><br><input type='password' name='pass'><br><input type='submit' value='auth!'></form>";
        exit;
        }
    }

// Authenticate
if(($user != $luser)|($pass != $lpass)) {
    
setcookie('user');
    
setcookie('pass');
    die(
'wrong pass and shit');
    }

// Do we have an url?
if(!$_GET['url'] && !$_GET['url_plain']) {
    echo 
"<form><input type='text' name='url_plain' value='http://'><input type='submit' value='Gogo!'></form>";
    exit;
    }

// Do some initialization, fetching
$url base64_decode($_GET['url']);
if(
strlen($_GET['url_plain']) > 0)
    
$url $_GET['url_plain'];
$url_bits parse_url($url);
// For rewriting
$host $url_bits['host'];
$path $url_bits['path'];
// Since some web browsers actually support spaces in urls, there are some
// websites that have them as well (edge forums :). Let's be graceful about
// those. no reason to comply with the rfcs, is there?
$url str_replace(' ''%20'$url);

// only http requests
if($url_bits['scheme'] != 'http')
    die(
"HTTP only. For now at least.");

// Get the web resource
if(($connection = @fopen($url'r')) === FALSE) {
    die(
"404 or similar. page can't be opened");
    }
$content '';
while (!
feof($connection))
    
$content .= fread($connection8192);

// Get content-type from headers
foreach($http_response_header as $header)
    
ereg('content-type: ([^;]*)'strtolower($header), $regs);
$ctype $regs[1];

// If it should prove to be neccessary, do some content-type guessing with mime
// magic shit or file name.. doesnt look to important since most servers always
// specify content-type.

fclose($connection);

// Rewrite 'href=', 'src=', 'action='
if($ctype == 'text/html') {
    
// This obviously isn't very elegant.. will be cutting the conent
    // and doing rewriting piece by piece
    
$pose 0;
    
$pos 0;
    
$content_rewritten '';
    
// Find a new tag
    
while(($pos strpos($content'<'$pose)) !== FALSE) {
        
// Add everything between this and the last tag
        
$content_rewritten .= substr($content$pose$pos $pose);
        
// Get content of this tag
        
$pose strpos($content'>'$pos);
        
$cont substr($content$pos$pose $pos);
        
        
// Find possible urls and rewrite them
        
$link '';
        
eregi('(((href)|(src)|(action)) ?= ?[\'"]?)([^\'" >]*)'$cont$link);
        if(
$link[1])
            
$cont ereg_replace($link[1] . slashreg($link[6]), $link[1] . rewrite($link[6]), $cont);
            
        
// Write the tag to the content
        
$content_rewritten .= $cont;
        
$pos $pose;
        }
    
// Write the end of content and make it actual
    
$content_rewritten .= substr($content$pose);
    
$content $content_rewritten;
    }

// Cough it out
echo $content;

// This rewrites the different urls
function rewrite($url) {
    global 
$host$path;
    if(
strtolower(substr($url07)) == 'http://') {
        
$url base64_encode($url);
        }
    else if(
strtolower(substr($url011)) == 'javascript:') {
        return 
$url;
        }
    else if(
substr($url01) == '/') {
        
$url base64_encode('http://' $host $url);
        }
    else {
        
$url base64_encode('http://' $host '/' ereg_replace('/?(.*)/.*''\\1/'$path) . $url);
        }
    
    return 
'http://' $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] . '?url=' $url;
    }

// Slashes some things that fuck with ereg_replace
function slashreg($str) {
    return 
str_replace(array("\\","(",")","?"),array("\\\\","\\(","\\)","\\?"), $str);
    }

?>