Location: PHPKode > scripts > Gonx Proxy > gonx-proxy/gonxproxy.class.php
<?php

/**
 * gonxproxy class : Just a HTTP Proxy
 * 
 * @package 
 * @author Ben Yacoub Hatem <hide@address.com>
 * @copyright Copyright (c) 2004
 * @version $Id$ - 07/04/2004 13:48:08 - gonxproxy.class.php
 * @access public
 **/
class gonxproxy{
	/**
     * Constructor
     * @access protected
     */
	function gonxproxy(){
		
	}
	
	/**
	 *
	 * @access public
	 * @return void 
	 **/
	function version(){
		return "1.0.1";
	}
	
	
	/**
	 * Return html form of GonxProxy
	 * 
	 * @access public
	 * @return void 
	 **/
	function form(){
		global $url;
		if (!ereg("http",$url) and $url!="") {
		    $url = base64_decode($url);
		}
		return "\n\n<!-- GonxProxy Form -->
<style><!--
.gonxproxy {
	background-color: #F6F6F6;
	margin: 4px;
	padding: 4px;
	border: 1px solid #CCCCCC;
}
input.gonxproxy {
	background-color: white;
	border: 1px solid #CCCCCC;
}
//-->
</style>
	<form method=get action=\"?\">
	<table class=gonxproxy width=600 cellpadding=4 cellspacing=10>
	<tr><td>
	 <input name=url value=\"$url\" size=70 class=gonxproxy>
	 <input type=submit class=gonxproxy value=Submit><br/><br/>
	 <input type=hidden name=proxy value=1>
	 <font size=1>GonxProxy version ".gonxproxy::version()." - Author : Ben Yacoub Hatem - Copyright © <a href=\"http://www.phptunisie.net/\" target=_blank>PHPTunisie</a>  2004-2005 </font>
	</td></tr></table>
	</form>
<!-- /GonxProxy Form -->\n\n";
	}
	
	/**
	 * Retreive a url
	 * 
	 * @access public
	 * @return void 
	 **/
	function connect($_url){
		$file = @fopen( $_url, "rb" );
		if (!$file) {
		    return "<font size=2 color=red>Error while opening URL</font>";
		}
		$line = fread( $file, 1024 );
		 if( eregi( "html", $line ) ){
		 	fclose( $file );
		 	$html = implode('',@file($_url) );
		    return gonxproxy::parse_html($html,$_url);
		} else {
			echo $line;
			fpassthru( $file );
			@fclose( $file );
			exit;
		}
	}
	
	/**
	 * Parse a HTML Page, remove JScript, convert Urls and return it.
	 * 
	 * @access public
	 * @return void 
	 **/
	function parse_html($_data,$_url){
		 $matches = preg_match_all( "/(href|src|background|@import) *=? *\"(.*)\"/Ui", $_data, $regs );
		 foreach($regs[2] as $k=>$v){
		 	$_data = str_replace($v,gonxproxy::chg_url($v,$_url), $_data);
		 }
		 return $_data;
	}
	
	
	/**
	 * Convert an URL to a GONX formatted url
	 * 
	 * Original function code is taken from GURL v2.1 BETA
	 *  by Bob Matcuk (hide@address.com)
	 * http://gurl.sourceforge.net
	 * 
	 * @access public
	 * @return void 
	 **/
	function chg_url($piece,$url){
	
		$url_elements = explode("/",$url );
		$url_file = ( preg_match( "/\/$/i", $url ) ) ? 0 : 1;
		if( preg_match( "/(.*)\#(.*)/", $piece, $regs ) ){ // Links to an anchor (#)
			return ( ( $regs[1] == "" ) ? "" : gonxproxy::chg_url( $regs[1],$url ) ) . "#" . $regs[2];
		} else if( preg_match( "/http:\/\/(.*)/i", $piece, $regs ) ){ // http:// form
			$piece = $regs[1];
		} else if( preg_match( "/^\//i", $piece ) ){ // starts with / form
			$piece = $url_elements[0] . $piece;
		} else if( preg_match( "/^\.\/(.*)/i", $piece, $regs ) ){ // single dots
			$piece = $regs[1];
			if( $url_file ){
				for( $i = sizeof( $url_elements ) - 2; $i >= 0; $i-- ){
					$piece = $url_elements[$i] . "/" . $piece;
				}
			} else {
				$piece = $url . $piece;
			}
		} else if( preg_match( "/^(\.\.\/)+(.*)/i", $piece, $regs ) ){ // double dots
			$tok = explode( "/", $piece );
			$i = 1 + sizeof( $tok ) - 2;
			if( $url_file ){
				$i++;
			}
			$piece = $regs[2];
			if( sizeof( $url_elements ) <= $i ){
				$piece = $url_elements[0] . "/" . $piece;
			} else {
				for( $j = sizeof( $url_elements ) - $i; $j >= 0; $j-- ){
					$piece = $url_elements[$j] . "/" . $piece;
				}
			}
		} else {
			if( $url_file ){
				for( $i = sizeof( $url_elements ) - 2; $i >= 0; $i-- ){
					$piece = $url_elements[$i] . "/" . $piece;
				}
			} else {
				$piece = $url . $piece;
			}
		}
		if( !( preg_match( "/(\/$|\..+$)/i", $piece ) && preg_match( "/\//i", $piece ) ) ){ // must end in / or file extension
			$piece .= "/";
		}
		if (substr($piece,strlen($piece)-1,1 )=="/") {
		    $piece = substr($piece,0,strlen($piece)-1 );
		}
		return "?url=".base64_encode($piece);
	}
	
}

?>
Return current item: Gonx Proxy