<?php
// Settings
$block_proxy_attemts        = true;         // true or false
$log_proxy_attemts      = true;         // true or false
$log_proxy_attemts_to   = "blockedproxy.log";  // Filename or path with filename
// To use this on your own page, please add following line:
// if (block_proxys() == true AND $block_proxy_attemts == true) { exit("Proxys are not allowed to connect to this page, please connect directly"); }
// Please do not change any thing under this comment
function block_proxys()
{
    global $_SERVER, $log_proxy_attemts, $log_proxy_attemts_to;
    $proxy_detected = '0';
function block_proxys()
{
    global $_SERVER, $log_proxy_attemts, $log_proxy_attemts_to;
    $proxy_detected = '0';
    $blockedheaders = array(
        'HTTP_VIA',
        'HTTP_X_FORWARDED_FOR',
        'HTTP_FORWARDED_FOR',
        'HTTP_X_FORWARDED',
        'HTTP_FORWARDED',
        'HTTP_CLIENT_IP',
        'HTTP_FORWARDED_FOR_IP',
        'VIA',
        'X_FORWARDED_FOR',
        'FORWARDED_FOR',
        'X_FORWARDED',
        'FORWARDED',
        'CLIENT_IP',
        'FORWARDED_FOR_IP',
        'HTTP_PROXY_CONNECTION'
    );
    foreach($blockedheaders as $i)
    {
        if (isset($_SERVER[$i])) { $proxy_detected++; $log .= $i.", "; }
    }
    if (gethostbyname(ReverseIPOctets($_SERVER['REMOTE_ADDR']).".".$_SERVER['SERVER_PORT'].".".ReverseIPOctets($_SERVER['SERVER_ADDR']).".ip-port.exitlist.torproject.org") == "127.0.0.2")
    { $proxy_detected++; $log .= "TOR exit node, "; }
    if (exists_in_rbl() == true)
    { $proxy_detected++; $log .= "RBL, "; }
    if ($proxy_detected >= '1')
    {
        if ($log_proxy_attemts == true)
        {
            if (is_writable($log_proxy_attemts_to)) {
                $fp = fopen($log_proxy_attemts_to,"a");
                fwrite($fp,date("Y-m-d H:i:s").": Proxy Server detected: ".gethostbyaddr($_SERVER['REMOTE_ADDR'])." (".$_SERVER['REMOTE_ADDR'].") - Info: ".$log."\n");
                fclose($fp);
            } else {
                exit("Connection Logfile is not writeable");
            }
        }
        return true;
    }
    else
    {
        return false;
    }
}
function exists_in_rbl() {
    $rbls = array('http.dnsbl.sorbs.net', 'misc.dnsbl.sorbs.net');
    $remote = getenv('REMOTE_ADDR');
    if (preg_match("/([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/",
      $remote, $matches)) {
        foreach ($rbls as $rbl) {
            $rblhost = $matches[4] . "." . $matches[3] . "." .
                $matches[2] . "." . $matches[1] . "." . $rbl;
            $resolved = gethostbyname($rblhost);
            if ($resolved != $rblhost) {
                return true;
            }
        }
    }
    return false;
}
function ReverseIPOctets($inputip)
{
    $ipoc = explode(".",$inputip);
    return $ipoc[3].".".$ipoc[2].".".$ipoc[1].".".$ipoc[0];
}
?>