<?php
    if(!function_exists("gethost")){
        function gethost($ip) {
            $list=explode(",", $ip);
            $ret="";
            foreach($list as $item) {
                $item=trim($item);
                $tmp=@gethostbyaddr($item);
                if($tmp==$item) $tmp="";
                $ret.=$tmp." ";
            }
            $ret=trim($ret);
            return addslashes($ret);
        }
    }

    function isValidIP($ip){
        if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ||
        filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) return true;
        else return false;
    }

    function getFirstIP($ip,$delim){
        if (strstr($ip, $delim)) {
            $ips = explode($delim, $ip);
            foreach($ips as $ip) {
                if (isValidIP($ip)) return $ip;
            }
            return $ips;
        } else return $ip;
    }
    $ips = array();
    $proxies = array();
    $logmsg="";

    if (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
        if (isValidIP($_SERVER["HTTP_X_FORWARDED_FOR"])) {
            if (!in_array($_SERVER["HTTP_X_FORWARDED_FOR"],$proxies)) $proxies[] = $_SERVER["HTTP_X_FORWARDED_FOR"];
        } else {
            $logmsg.= "Proxy (HTTP_X_FORWARDED_FOR) not valid: ".$_SERVER["HTTP_X_FORWARDED_FOR"]."\n";
        }
    }

    if (!empty($_SERVER["HTTP_VIA"])) {
        $via_ignorelist = array(
            "1.1 squid.test (squid/3.5.20)"
        );
        if (isValidIP($_SERVER["HTTP_VIA"])) {
            if (!in_array($_SERVER["HTTP_VIA"],$proxies)) $proxies[] = $_SERVER['HTTP_VIA'];
        } else {
            if (!in_array($_SERVER["HTTP_VIA"],$via_ignorelist)) $logmsg.= "Proxy (HTTP_VIA) not valid: ".$_SERVER["HTTP_VIA"]."\n";
        }
    }
    if (!empty($_SERVER["REMOTE_ADDR"])) {
        if (isValidIP($_SERVER["REMOTE_ADDR"])) {
            if (!in_array($_SERVER["REMOTE_ADDR"],$ips)) $ips[] = $_SERVER["REMOTE_ADDR"];
        } else {
            $logmsg.= "IP (REMOTE_ADDR) not valid: ".$_SERVER["REMOTE_ADDR"]."\n";
        }
    }
    if (!empty($_SERVER["HTTP_CLIENT_IP"]) && isValidIP($_SERVER["HTTP_CLIENT_IP"])) {
        if (isValidIP($_SERVER["HTTP_CLIENT_IP"])) {
            if (!in_array($_SERVER["HTTP_CLIENT_IP"],$ips)) $ips[] = $_SERVER["HTTP_CLIENT_IP"];
        } else {
            $logmsg.= "IP (HTTP_CLIENT_IP) not valid: ".$_SERVER["HTTP_CLIENT_IP"]."\n";
        }
    }

    if (!empty($ips)) $ip = $ips[0];
    else $ip = "unknown";
    if (!empty($proxies)) $proxy = $proxies[0];
    else $proxy = "";
    if (count($ips) > 1) $logmsg .= "Multiple ip's: ".implode(" ", $ips)."\n";
    if (count($proxies) > 1) $logmsg .= "Multiple proxies: ".implode(" ", $proxies)."\n";

    if($ip != "unknown") {
        if (isValidIP($ip)) {
            if (file_exists("$prefix/include/db.php")) {
                include "db.php";
                if($db_link) {
                    mysqli_select_db($db_link,$db);
                    if (!mysqli_query($db_link,"desc hosts")) {
                        $query="create table hosts (id int not null auto_increment primary key, ip text not null, host text not null, lastcheck int not null default 0)";
                        mysqli_query($db_link,$query) or die(mysqli_error($db_link));
                    }
                    $timestamp = time();
                    $result = mysqli_query($db_link,"select host, lastcheck from hosts where ip='".mysqli_real_escape_string($db_link,$ip)."'") or die(mysqli_error($db_link));
                    if (!mysqli_num_rows($result)) {
                        $host = gethost($ip);
                        mysqli_query($db_link,"insert into hosts (ip, host, lastcheck) values ('".mysqli_real_escape_string($db_link,$ip)."','".mysqli_real_escape_string($db_link,$host)."',$timestamp)") or die (mysqli_error($db_link));
                    } elseif ($arr=mysqli_fetch_assoc($result)){;
                        $host=$arr['host'];
                        if ($arr['lastcheck'] < $timestamp - 60*60*24) {
                            $host = gethost($ip);
                            mysqli_query($db_link,"update hosts set host='".mysqli_real_escape_string($db_link,$host)."', lastcheck=$timestamp where ip='".mysqli_real_escape_string($db_link,$ip)."'") or die (mysqli_error($db_link));
                        }
                        
                    }
                    mysqli_close($db_link);
                    unset($db_link, $timestamp);
                }
            }
            if (!isset($host))
                $host=gethost($ip);
        }

        else $host="[Invalid IP]";
    } else $host = "";

    if ($logmsg) {
        $file=fopen("$prefix/logs/getip.txt", "a");
        $msg = getinfo()."\n".$logmsg;
        fwrite($file, $msg);
        fclose($file);
    }

    /*if (!isValidIP($ip)) {
        $ret=getFirstIP($ip,',');
        if (gettype($ret) == "array") {
            foreach ($ret as $ip){
                $ret2=getFirstIP($ip,':');
                if (gettype($ret2) == "array"){
                    $ip=false;
                } else $ip = $ret2;
            }
        } else $ip = $ret;
        if (!isValidIP($ip)) {
            $ret=getFirstIP($ip,':');
            if (gettype($ret) == "array"){
            $ip=false;
            } else $ip = $ret;
        }
    }*/

?>