<?php 

if(!isset($prefix)) $prefix=(!isset ($_SERVER['HTTP_HOST']) || (strpos($_SERVER['HTTP_HOST'], "juozas24.lt") === FALSE))?"/var/www/html":"/home/juozas24/domains/juozas24.lt/public_html";
$admin=isset($admin)?$admin:false;
$server=isset($_SERVER['HTTP_HOST'])?$_SERVER['HTTP_HOST']:$_SERVER['SERVER_ADDR'];
//if (isset($_GET['testdbg'])) ipdbg();
/*if (!function_exists('ipdbg')){
    function ipdbg(){
        global $_SERVER,$_COOKIE,$_SESSION, $ip, $host, $proxy, $ips, $prefix;
        //todo: ip spam blacklist check, add optional vars (limit check-only, etc.)
        $msg="[".date("Y-m-d H:i:s")."] ".((isset($_SERVER["HTTP_X_FORWARDED_FOR"]) && !empty($_SERVER["HTTP_X_FORWARDED_FOR"]) && strcasecmp($_SERVER["HTTP_X_FORWARDED_FOR"], "unknown"))?("\$_SERVER[\"HTTP_X_FORWARDED_FOR\"]=\"".$_SERVER["HTTP_X_FORWARDED_FOR"]."\"; "):"").
        ((isset($_SERVER['HTTP_VIA']) && !empty($_SERVER["HTTP_VIA"]) && strcasecmp($_SERVER["HTTP_VIA"], "unknown"))?("\$_SERVER[\"HTTP_VIA\"]=\"".$_SERVER["HTTP_VIA"]."\"; "):"").
        ((isset($_SERVER["HTTP_CLIENT_IP"]) && !empty($_SERVER["HTTP_CLIENT_IP"]) && strcasecmp($_SERVER["HTTP_CLIENT_IP"], "unknown"))?("\$_SERVER[\"HTTP_CLIENT_IP\"]=\"".$_SERVER["HTTP_CLIENT_IP"]."\"; "):"").
        ((isset($_SERVER["REMOTE_ADDR"]) && !empty($_SERVER["REMOTE_ADDR"]) && strcasecmp($_SERVER["REMOTE_ADDR"], "unknown"))?("\$_SERVER[\"REMOTE_ADDR\"]=\"".$_SERVER["REMOTE_ADDR"]."\"; "):"").
        ((isset($_SERVER["HTTP_REFERER"]) && !empty($_SERVER["HTTP_REFERER"]))?("\$_SERVER[\"HTTP_REFERER\"]=\"".$_SERVER["HTTP_REFERER"]."\"; "):"").
        "\$ip=\"$ip\"; "."\$host=\"$host\"; ".
        (isset($proxy)?("\$proxy=\"".$proxy."\"; "):"").
        (isset($ips)?("\$ips=".printVal($ips)."; "):"")."\n".
        "\$_SERVER=".printVal($_SERVER).";\n".
        (!empty($_COOKIE)?("\$_COOKIE=".printVal($_COOKIE).";\n"):"").
        (!empty($_SESSION)?("\$_SESSION=".printVal($_SESSION).";\n"):"");
        $file=fopen("$prefix/logs/ipdbg.txt", "a");
        if ($file !== false) {
            fwrite($file, $msg);
            fclose($file);
        }
    } 
}*/
if (file_exists("$prefix/include/db.php")){
    include 
"db.php";
    if(
$db_link){
        
mysqli_select_db($db_link,$db);
        
$table="accesslog";
        if(isset(
$_GET['clean']) && ($_GET['clean']=="log") && $admin) {
            
$days=isset($_GET['days'])?$_GET['days']:"";
            if(!
is_numeric($days)) {
                
mysqli_query($db_link,"drop table if exists $table") or die(mysqli_error($db_link));
            } else {
                
mysqli_query($db_link,"delete from $table where date<=".(time() - (60 60 24 mysqli_real_escape_string($db_link,$days)))) or die(mysqli_error($db_link));
            }
            
        }
        if(!
mysqli_query($db_link,"desc $table")){
            
mysqli_query($db_link,"create table $table (id int not null auto_increment primary key, ip varchar (16) not null default '', host varchar (64) not null default '', proxy varchar (16) not null default '', agent varchar (256) not null default '', method varchar(8) not null default '', protocol varchar(8) not null default '',server varchar (256) not null default '', url varchar (2048) not null default '', referer varchar (2048) not null default '', result int not null default 200, date int not null default 0) Engine=InnoDB") or die(mysqli_error($db_link));
        }
        
//insert/update data
        
$result=mysqli_query($db_link,"delete from $table where date <= ".(time()-45*24*60*60)) or die(mysqli_error($db_link));
        
$agent=isset($_SERVER['HTTP_USER_AGENT'])?$_SERVER['HTTP_USER_AGENT']:"";
        
$protocol=(isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']))?"https":"http";
        if(!isset(
$method)) $method=isset($_SERVER['REDIRECT_REDIRECT_REQUEST_METHOD'])?$_SERVER['REDIRECT_REDIRECT_REQUEST_METHOD']:(isset($_SERVER['REDIRECT_REQUEST_METHOD'])?$_SERVER['REDIRECT_REQUEST_METHOD']:(isset($_SERVER['REQUEST_METHOD'])?$_SERVER['REQUEST_METHOD']:false));
        
/*if (isset($chkref) && $chkref !==false) {
            //if (!isset($_SESSION['ref'])
            if (!empty($_SESSION['ref']) && !empty($_SERVER['HTTP_REFERER']) && preg_match("/^".preg_quote("$protocol://".$_SERVER['HTTP_HOST'],"/")."/i",$_SERVER['HTTP_REFERER'])) {
                if($_SESSION['ref'] == $_SERVER['HTTP_REFERER']) {
                    $_SESSION['ref']="$protocol://".$_SERVER['HTTP_HOST'].trim($_SERVER['REQUEST_URI']);

                } else {
                    die ("Invalid Referer");
                }
            } else $_SESSION['ref']="$protocol://".$_SERVER['HTTP_HOST'].trim($_SERVER['REQUEST_URI']);
        }*/
        /*$result=mysqli_query($db_link,"select max(date) as date, min(date) as date2 from $table where ip='$ip'") or die(mysqli_error($db_link));
        if(mysqli_num_rows($result) > 0) {
            if ($arr = mysqli_fetch_assoc($result)) {
                if ($arr['date'] < time() - 7*24*60*60) {
                    ipdbg(); //todo: adjust ip dbg freq. (for blacklist checking)
                } //elseif ($admin){
                //    echo ((($arr['date'] - $arr['date2'])/(24*60*60))%7==0 && ($arr['date'] < time() - 24*60*60))?"true":"false";
                //    //$arr['date'] < time() - 24*60*60
                //}
            }
            unset ($arr);
        } else {
            ipdbg();
        }*/
        
$result=mysqli_query($db_link,"select * from $table where url='".mysqli_real_escape_string($db_link,$_SERVER['REQUEST_URI'])."' and method='".(($method !== false)?$method:"")."' and protocol='$protocol' and server='$server' and referer='".(isset($_SERVER['HTTP_REFERER'])?mysqli_real_escape_string($db_link,$_SERVER['HTTP_REFERER']):"")."' and agent='$agent' and ip='$ip' and result='".(isset($evt)?mysqli_real_escape_string($db_link,$evt):200)."'") or die(mysqli_error($db_link));
        if(
mysqli_num_rows($result) > 0) {
            
mysqli_query($db_link,"update $table set date='".time()."',host='$host',proxy='".(isset($proxy)?$proxy:"")."' where ip='$ip' and agent='$agent' and protocol='$protocol' and server='$server' and url='".mysqli_real_escape_string($db_link,$_SERVER['REQUEST_URI'])."' and method='".(($method !== false)?$method:"")."' and referer='".(isset($_SERVER['HTTP_REFERER'])?mysqli_real_escape_string($db_link,$_SERVER['HTTP_REFERER']):"")."' and result='".(isset($evt)?mysqli_real_escape_string($db_link,$evt):200)."'") or die(mysqli_error($db_link));
        } else {
            
mysqli_query($db_link,"insert into $table (ip, host, proxy, agent, method, protocol, server, url, referer, date, result) values ('$ip','$host','".(isset($proxy)?$proxy:"")."','$agent','".(($method !== false)?$method:"")."','$protocol','$server','".mysqli_real_escape_string($db_link,$_SERVER['REQUEST_URI'])."','".(isset($_SERVER['HTTP_REFERER'])?mysqli_real_escape_string($db_link,$_SERVER['HTTP_REFERER']):"")."','".time()."', '".(isset($evt)?mysqli_real_escape_string($db_link,$evt):200)."')") or die(mysqli_error($db_link));
        }
        
        if(
$admin && ($action=="showlog") && !isset($evt)) {
            if (!isset(
$_SESSION['iplog']) || isset($_POST['reset'])) $_SESSION['iplog']=Array();
            if (isset(
$_POST['fld'])){ $fld=$_POST['fld']; $_SESSION['iplog']['fld']=$fld; } 
            elseif (!empty(
$_SESSION['iplog']['fld'])) $fld=$_SESSION['iplog']['fld'];
            else 
$fld=Array();
            if(isset(
$_POST['opr'])){ $opr=$_POST['opr']; $_SESSION['iplog']['opr']=$opr; }
            elseif (!empty(
$_SESSION['iplog']['opr'])) $opr=$_SESSION['iplog']['opr'];
            else 
$opr=Array();
            if(isset(
$_POST['val'])){ $val=$_POST['val']; $_SESSION['iplog']['val']=$val; }
            elseif (!empty(
$_SESSION['iplog']['val'])) $val=$_SESSION['iplog']['val'];
            else 
$val=Array();
            if(isset(
$_POST['and'])){ $and=$_POST['and']; $_SESSION['iplog']['and']=$and; }
            elseif (!empty(
$_SESSION['iplog']['and'])) $and=$_SESSION['iplog']['and'];
            else 
$and=Array();
            if(!empty(
$fld) && !empty($opr) && !empty($val)) {
                
$fList=Array();
                
$i=0; foreach($fld as $value$fList[$i++][0]=$value;
                
$i=0; foreach($opr as $value$fList[$i++][1]=$value
                
$i=0; foreach($val as $value$fList[$i++][2]=$value
                if (!empty(
$and)) foreach($and as $value$fList[$value][3]="on";     
            } else 
$fList=array();
            echo 
"<script>var cnt=0;</script><form method=\"post\" action=\"$PHP_SELF?action=showlog".(isset($_GET['ip'])?("&ip=".$_GET['ip']):"")."\"><h3>Filter results</h3><input type=\"hidden\" name=\"reset\"><div id=\"list\">";
            
$first=true$i=0;
            foreach(
$fList as $item) {
                echo 
"<div>";
                if (!
$first) echo "<input name=\"and[]\" value=\"".$i."\" id=\"ch$i\" type=\"checkbox\" ".((isset($item[3]) && ($item[3] == "on"))?" checked":"").">And ";
                echo 
"<select name=\"fld[]\">
<option value=\"ip\""
.((isset($item[0]) && ($item[0] == "ip"))?" selected":"").">IP Address</option>
<option value=\"host\""
.((isset($item[0]) && ($item[0] == "host"))?" selected":"").">Host</option>
<option value=\"url\""
.((isset($item[0]) && ($item[0] == "url"))?" selected":"").">Requrest URL</option>
<option value=\"method\""
.((isset($item[0]) && ($item[0] == "method"))?" selected":"").">Requrest Method</option>
<option value=\"protocol\""
.((isset($item[0]) && ($item[0] == "protocol"))?" selected":"").">Requrest Protocol</option>
<option value=\"result\""
.((isset($item[0]) && ($item[0] == "result"))?" selected":"").">Result</option>
<option value=\"agent\""
.((isset($item[0]) && ($item[0] == "agent"))?" selected":"").">User Agent</option>
<option value=\"proxy\""
.((isset($item[0]) && ($item[0] == "proxy"))?" selected":"").">Proxy IP</option>
<option value=\"referer\""
.((isset($item[0]) && ($item[0] == "referer"))?" selected":"").">Referer</option>
</select><select name=\"opr[]\">
<option value=\"=\""
.((isset($item[1]) && ($item[1] == "="))?" selected":"").">=</option>
<option value=\"!=\""
.((isset($item[1]) && ($item[1] == "!="))?" selected":"").">!=</option>
<option value=\"like\""
.((isset($item[1]) && ($item[1] == "like"))?" selected":"").">Like</option>
<option value=\"not like\""
.((isset($item[1]) && ($item[1] == "not like"))?" selected":"").">Not Like</option>
<option value=\"regexp\""
.((isset($item[1]) && ($item[1] == "regexp"))?" selected":"").">Regexp</option>
<option value=\"not regexp\""
.((isset($item[1]) && ($item[1] == "not regexp"))?" selected":"").">Not Regexp</option>
</select><input name=\"val[]\" type=\"text\" value=\""
.(isset($item[2])?htmlspecialchars($item[2]):"")."\"><input type=\"button\" onclick=\"this.parentNode.parentNode.removeChild(this.parentNode); cnt--;\" value=\"Remove\"></div>";
                
$first=false$i++;
            }
            
$out"<select name=\"fld[]\">
<option value=\"ip\">IP Address</option>
<option value=\"host\">Host</option>
<option value=\"url\">Requrest URL</option>
<option value=\"method\">Requrest Method</option>
<option value=\"protocol\">Requrest Protocol</option>
<option value=\"result\">Result</option>
<option value=\"agent\">User Agent</option>
<option value=\"proxy\">Proxy IP</option>
<option value=\"referer\">Referer</option>
</select><select name=\"opr[]\">
<option value=\"=\">=</option>
<option value=\"!=\">!=</option>
<option value=\"like\">Like</option>
<option value=\"not like\">Not Like</option>
<option value=\"regexp\">Regexp</option>
<option value=\"not regexp\">Not Regexp</option>
</select><input name=\"val[]\" type=\"text\"><input type=\"button\" onclick=\"this.parentNode.parentNode.removeChild(this.parentNode); cnt--;\" value=\"Remove\">"
;
            echo 
"</div>
<script>
cnt = 
$i;
function addInput() {
    var input="
.json_encode($out).";
    var div=document.createElement('div');
    if (document.getElementById('list').innerHTML != \"\") {
        div.innerHTML = "
.json_encode("<input name=\"and[]\" value=\"")." + cnt + ".json_encode("\" id=\"ch")." + cnt++ + ".json_encode("\" type=\"checkbox\" checked>And ")." +input;
    } else {
        div.innerHTML=input; cnt++;
    }
    document.getElementById('list').appendChild(div);
}
//alert(document.getElementById('list').innerHTML);
</script>
<input type=\"button\" onclick=\"addInput()\" value=\"Add Filter\">
<input type=\"submit\" value=\"Submit\"></form>"
;
            
$sta=(isset($_GET['sta']) && is_numeric($_GET['sta']))?$_GET['sta']:0;
            if(isset(
$_GET['ip'])) {
                
$step=500;
                
$next=$end=$sta+$step;
                
$prev=$sta-$step;
                
$target=mysqli_real_escape_string($db_link,$_GET['ip']);
                if(
$target!="all") {
                    
$query="select * from $table where ip='$target'";
                } else {
                    
$query="select * from $table";
                }
                unset(
$arr);
                
$first=true;
                foreach(
$fList as $item) {
                    if(!empty(
$item[0]) && isset ($item[1])) {
                            
$field $item[0];
                            
$oper $item[1];
                            
$val=isset($item[2])?mysqli_real_escape_string($db_link,$item[2]):"";
                            if(
$target!="all") {
                                
$query .= " ".($first?"and (":(isset($item[3])?"and":"or")).$field $oper '$val'";
                            }
                            else 
$query .= " ".($first?"where":(isset($item[3])?"and":"or")).$field $oper '$val'";
                            
$first=false;
                    }
                }
                if(
$target!="all" && $first != true$query.=")";
                
$query .= " order by date desc, id desc limit $sta,$step";
                echo 
"<h3>SQL Queries</h3>";
                echo 
"<pre>".htmlspecialchars($query)."</pre>";
                
$result=mysqli_query($db_link,$query) or die(mysqli_error($db_link));
                
$count mysqli_num_rows($result);
                if (
$count 0) {
                    
$mylinks[]=Array("Clean Access Log","$PHP_SELF?action=$action&clean=log&lang=$lang");
                    if(
$target!="all") {
                        
$query="select count(*) as count, max(date) as maxdate, min(date) as mindate from $table where ip='$target'";
                        
$first=true;
                        foreach(
$fList as $item) {
                            if(!empty(
$item[0]) && isset ($item[1])) {
                                
$field $item[0];
                                
$oper $item[1];
                                
$val=isset($item[2])?mysqli_real_escape_string($db_link,$item[2]):"";
                                
$query .= " ".($first?"and (":(isset($item[3])?"and":"or")).$field $oper '$val'";
                                
$first=false;
                            }
                        }
                        if(
$first != true$query.=")";
                        echo 
"<pre>".htmlspecialchars($query)."</pre>";
                        
$res2=mysqli_query($db_link,$query) or die(mysqli_error($db_link));
                        if(
$arr mysqli_fetch_assoc($res2)) {
                            
$t $arr['maxdate']-$arr['mindate'];
                            
$days floor($t/60/60/24);
                            
$hours floor($t/60/60)-$days*24;
                            
$minutes floor($t/60)-$hours*60-$days*24*60;
                            
$seconds $t-$minutes*60-$hours*60*60-$days*24*60*60;
                            if (
$t 0) {
                                echo 
"<h3>Statistics for ip $target".(!empty($fList)?" matching filters above":"")."</h3>";
                                
$vps=$arr['count']/$t;
                                
$vpm=$vps*60;
                                
$vph=$vpm*60;
                                
$vpd=$vph*24;
                                
$vpw=$vpd*7;
                                
$vpt=$vpd*30;
                                
$vpy=$vpd*365;
                                if (
$vps >= 1) echo floor($vps)." queries per second"
                                else if (
$vpm >= 1) echo floor($vpm)." queries per minute";
                                else if (
$vph >= 1) echo floor($vph)." queries per hour";
                                else if (
$vpd >= 1) echo floor($vpd)." queries per day";
                                else if (
$vpw >= 1) echo floor($vpw)." queries per week";
                                else if (
$vpt >= 1) echo floor($vpt)." queries per month";
                                else if (
$vpy >= 1) echo floor($vpy)." queries per year";
                                echo 
" (".$arr['count']." visits in ";
                                echo (
$days>0)?($days." days "):"";
                                echo (
$hours>0)?($hours." hours "):"";
                                echo (
$minutes>0)?($minutes." minutes "):"";
                                echo (
$seconds>0)?($seconds." seconds"):"";
                                echo 
")";
                            }
                        }
                        echo 
"<h3>Showing Logs for IP: $target</h3>";
                    } else {
                        
$query="select count(*) as count, max(date) as maxdate, min(date) as mindate from $table";
                        
$first=true;
                        foreach(
$fList as $item) {
                            if(!empty(
$item[0]) && isset ($item[1])) {
                                
$field $item[0];
                                
$oper $item[1];
                                
$val=isset($item[2])?mysqli_real_escape_string($db_link,$item[2]):"";
                                
$query .= " ".($first?"where (":(isset($item[3])?"and":"or")).$field $oper '$val'";
                                
$first=false;
                            }
                        }
                        if(
$first != true$query.=")";
                        echo 
"<pre>".htmlspecialchars($query)."</pre>";
                        
$res2=mysqli_query($db_link,$query) or die(mysqli_error($db_link));
                        if(
$arr mysqli_fetch_assoc($res2)) {
                            
$t $arr['maxdate']-$arr['mindate'];
                            
$days floor($t/60/60/24);
                            
$hours floor($t/60/60)-$days*24;
                            
$minutes floor($t/60)-$hours*60-$days*24*60;
                            
$seconds $t-$minutes*60-$hours*60*60-$days*24*60*60;
                            if (
$t 0) {
                                echo 
"<h3>Statistics".(!empty($fList)?" matching filters above":" for all ip's")."</h3>";
                                
$vps=$arr['count']/$t;
                                
$vpm=$vps*60;
                                
$vph=$vpm*60;
                                
$vpd=$vph*24;
                                
$vpw=$vpd*7;
                                
$vpt=$vpd*30;
                                
$vpy=$vpd*365;
                                if (
$vps >= 1) echo floor($vps)." queries per second"
                                else if (
$vpm >= 1) echo floor($vpm)." queries per minute";
                                else if (
$vph >= 1) echo floor($vph)." queries per hour";
                                else if (
$vpd >= 1) echo floor($vpd)." queries per day";
                                else if (
$vpw >= 1) echo floor($vpw)." queries per week";
                                else if (
$vpt >= 1) echo floor($vpt)." queries per month";
                                else if (
$vpy >= 1) echo floor($vpy)." queries per year";
                                echo 
" (".$arr['count']." visits in ";
                                echo (
$days>0)?($days." days "):"";
                                echo (
$hours>0)?($hours." hours "):"";
                                echo (
$minutes>0)?($minutes." minutes "):"";
                                echo (
$seconds>0)?($seconds." seconds"):"";
                                echo 
")";
                            }
                        }
                        echo 
"<h3>Showing All IP Logs</h3>";
                    }
                    
                    if (
$prev >= 0) {
                        if (
$prev >= $step) echo "<a href=\"$PHP_SELF?action=showlog&ip=".$_GET['ip']."&sta=$prev\">[Prev Page]</a> ";
                        else echo 
"<a href=\"$PHP_SELF?action=showlog&ip=".$_GET['ip']."\">[Prev Page]</a> ";
                        echo 
"<a href=\"$PHP_SELF?action=showlog&ip=".$_GET['ip']."\">[Index]</a> ";
                    } else {
                        echo 
"[Prev Page] [Index] ";
                    }
                    if (
$step <= $count) echo "<a href=\"$PHP_SELF?action=showlog&ip=".$_GET['ip']."&sta=$next\">[Next Page]</a> ";
                    else echo 
"[Next Page] ";
                    echo 
"<style>
.nowrap {
    white-space: nowrap;                    
}
</style>"
;
                    echo 
"<div style=\"width: 960px; height:330px; overflow:auto\">";
                    echo 
"<table class=\"form expand\" id=\"log\">";
                    echo 
"<tr><th>IP</th><th>Host</th><th>Proxy</th><th>Method</th><th>Protocol</th><th>Server</th><th>Url</th><th>Referer</th><th>Result</th><th>Last access date</th><th>User Agent</th></tr>";
                    
                    
                    while (
$item=mysqli_fetch_assoc($result)) {
                        
                        
$res2=mysqli_query($db_link,"select * from bans where ip='".$item['ip']."'") or die(mysqli_error($db_link));
                        if (
mysqli_num_rows($res2) > 0$banned_ip=true;
                        else 
$banned_ip=false;
                        
$res2=mysqli_query($db_link,"select * from bans where ip='".$item['proxy']."'") or die(mysqli_error($db_link));
                        if (
mysqli_num_rows($res2) > 0$banned_proxy=true;
                        else 
$banned_proxy=false;
                        echo 
"<tr><td><a href=\"http://www.google.com/search?q=".$item['ip']."\" target=\"_blank\">";
                        if (
$target=="all") echo (($banned_ip || $banned_proxy)?"<b>":"").($banned_proxy?"<i>":"");
                        if (
$item['referer'] == "$protocol://".$server.$item['url']) echo "<font color=#FFc0c0>";
                        echo 
$item['ip'];
                        if (
$item['referer'] == "$protocol://".$server.$item['url']) echo "</font>";
                        if (
$target=="all") echo (($banned_ip || $banned_proxy)?"<b>":"").($banned_proxy?"</i>":"");
                        echo 
"</a> ";
                        echo 
"<td class=\"nowrap\">";
                        if (
$target=="all" && ($banned_ip || $banned_proxy)) echo "<b>".($banned_proxy?"<i>":"");
                        if (
$item['referer'] == ("$protocol://".$server.$item['url'])) echo "<font color=#FFc0c0>";
                        echo (!empty(
$item['host'])?$item['host']:"N/A");
                        if (
$item['referer'] == ("$protocol://".$server.$item['url'])) echo "</font>";
                        if (
$target=="all") echo (($banned_ip || $banned_proxy)?"<b>":"").($banned_proxy?"</i>":"");
                        echo 
"<td>";
                        if (!empty(
$item['proxy'])) echo "<a href=\"http://www.google.com/search?q=".$item['proxy']."\" target=\"_blank\">";
                        if (
$target=="all" && ($banned_ip || $banned_proxy)) echo "<b>".($banned_proxy?"<i>":"");
                        if (
$item['referer'] == ("$protocol://".$server.$item['url'])) echo "<font color=#FFc0c0>";
                        echo (!empty(
$item['proxy'])?$item['proxy']:"");
                        if (
$item['referer'] == ("$protocol://".$server.$item['url'])) echo "</font>";
                        if (
$target=="all") echo (($banned_ip || $banned_proxy)?"<b>":"").($banned_proxy?"</i>":"");
                        if (!empty(
$item['proxy'])) echo "</a>";
                        echo 
"<td>".$item['method'];
                        echo 
"<td>".$item['protocol'];
                        echo 
"<td>".$item['server'];
                        echo 
"<td class=\"nowrap\">";
                        if (
$target=="all" && ($banned_ip || $banned_proxy)) echo "<b>".($banned_proxy?"<i>":"");
                        if (
$item['referer'] == ("$protocol://".$server.$item['url'])) echo "<font color=#FFc0c0>";
                        echo 
htmlspecialchars($item['url']);
                        if (
$item['referer'] == ("$protocol://".$server.$item['url'])) echo "</font>";                                                                        
                        if (
$target=="all") echo (($banned_ip || $banned_proxy)?"<b>":"").($banned_proxy?"</i>":"");
                        echo 
"<td class=\"nowrap\">";
                        if (
$target=="all" && ($banned_ip || $banned_proxy)) echo "<b>".($banned_proxy?"<i>":"");
                        if (
$item['referer'] == ("$protocol://".$server.$item['url'])) echo "<font color=#FFc0c0>";
                        
                        echo 
htmlspecialchars($item['referer']);
                        if (
$item['referer'] == ("$protocol://".$server.$item['url'])) echo "</font>";
                        if (
$target=="all") echo (($banned_ip || $banned_proxy)?"<b>":"").($banned_proxy?"</i>":"");
                        echo 
"<td>";
                        if (
$target=="all" && ($banned_ip || $banned_proxy)) echo "<b>".($banned_proxy?"<i>":"");
                        if (
$item['referer'] == ("$protocol://".$server.$item['url'])) echo "<font color=#FFc0c0>";
                        echo 
$item['result'];
                        if (
$item['referer'] == ("$protocol://".$server.$item['url'])) echo "</font>";
                        if (
$target=="all") echo (($banned_ip || $banned_proxy)?"<b>":"").($banned_proxy?"</i>":"");
                        echo 
"<td class=\"nowrap\">";
                        if (
$target=="all" && ($banned_ip || $banned_proxy)) echo "<b>".($banned_proxy?"<i>":"");
                        if (
$item['referer'] == ("$protocol://".$server.$item['url'])) echo "<font color=#FFc0c0>";
                        echo 
date("Y-m-d H:i:s"$item['date']);
                        if (
$item['referer'] == ("$protocol://".$server.$item['url'])) echo "</font>";
                        if (
$target=="all") echo (($banned_ip || $banned_proxy)?"<b>":"").($banned_proxy?"</i>":"");
                        echo 
"<td class=\"nowrap\">";
                        
                        if (!empty(
$item['agent'])) echo "<a href=\"http://www.google.com/search?q=&quot;".urlencode($item['agent'])."&quot;\" target=\"_blank\">";
                        if (
$target=="all" && ($banned_ip || $banned_proxy)) echo "<b>";
                        if (
$item['referer'] == ("$protocol://".$server.$item['url'])) echo "<font color=#FFc0c0>";
                        echo 
htmlspecialchars($item['agent']);
                        if (
$item['referer'] == ("$protocol://".$server.$item['url'])) echo "</font>";
                        if (
$target=="all") echo (($banned_ip || $banned_proxy)?"<b>":"");
                        if (!empty(
$item['agent'])) echo "</a>";
                        echo 
"</tr>";
                    }
                    echo 
"</table>";
                    
                    echo 
"</div>";
                    echo 
"<hr><a href=\"$PHP_SELF?action=showlog\">Show IP List</a>";
                } else { 
                    if(
$target!="all") echo "No entries for ".stripslashes($target);
                    else echo 
"No entries available.";
                }
            } else {
                
$step=100;
                
$next=$end=$sta+$step;
                
//$next=$sta+$step;
                
$prev=$sta-$step;
                
$query "select distinct ip, host, proxy, count(*) as count, max(date) as maxdate, min(date) as mindate from $table";
                
$first=true;
                foreach(
$fList as $item) {
                    if(!empty(
$item[0]) && isset ($item[1])) {
                    
$field $item[0];
                    
$oper $item[1];
                    
$val=isset($item[2])?mysqli_real_escape_string($db_link,$item[2]):"";
                    
//if (!empty($val)) {
                        
$query .= " ".($first?"where":(isset($item[3])?"and":"or")).$field $oper '$val'";
                        
$first=false;
                    
//}
                    
}
                }
                
$query .= " group by ip order by maxdate desc";
                
$query .= " limit $sta,$step";
                echo 
"<h3>SQL Queries</h3>";
                echo 
"<pre>$query</pre>";
                
$result=mysqli_query($db_link,$query) or die(mysqli_error($db_link));
                
$count mysqli_num_rows($result);
                if(
$count 0) {
                    
$mylinks[]=Array("Clean Access Log","$PHP_SELF?action=$action&clean=log&lang=$lang");
                    
$query="select count(*) as count, max(date) as maxdate, min(date) as mindate from $table";
                    
$first=true;
                    foreach(
$fList as $item) {
                        if(!empty(
$item[0]) && isset ($item[1])) {
                            
$field $item[0];
                            
$oper $item[1];
                            
$val=isset($item[2])?mysqli_real_escape_string($db_link,$item[2]):"";
                            
$query .= " ".($first?"where (":(isset($item[3])?"and":"or")).$field $oper '$val'";
                            
$first=false;
                        }
                    }
                    if(
$first != true$query.=")";
                    echo 
"<pre>".htmlspecialchars($query)."</pre>";
                    
$res2=mysqli_query($db_link,$query) or die(mysqli_error($db_link));
                    if(
$arr mysqli_fetch_assoc($res2)) {
                        
$t $arr['maxdate']-$arr['mindate'];
                        
$days floor($t/60/60/24);
                        
$hours floor($t/60/60)-$days*24;
                        
$minutes floor($t/60)-$hours*60-$days*24*60;
                        
$seconds $t-$minutes*60-$hours*60*60-$days*24*60*60;
                        if (
$t 0) {
                            echo 
"<h3>Statistics".(!empty($fList)?" matching filters above":" for all ip's")."</h3>";
                            
$vps=$arr['count']/$t;
                            
$vpm=$vps*60;
                            
$vph=$vpm*60;
                            
$vpd=$vph*24;
                            
$vpw=$vpd*7;
                            
$vpt=$vpd*30;
                            
$vpy=$vpd*365;
                            if (
$vps >= 1) echo floor($vps)." queries per second"
                            else if (
$vpm >= 1) echo floor($vpm)." queries per minute";
                            else if (
$vph >= 1) echo floor($vph)." queries per hour";
                            else if (
$vpd >= 1) echo floor($vpd)." queries per day";
                            else if (
$vpw >= 1) echo floor($vpw)." queries per week";
                            else if (
$vpt >= 1) echo floor($vpt)." queries per month";
                            else if (
$vpy >= 1) echo floor($vpy)." queries per year";
                            echo 
" (".$arr['count']." visits in ";
                            echo (
$days>0)?($days." days "):"";
                            echo (
$hours>0)?($hours." hours "):"";
                            echo (
$minutes>0)?($minutes." minutes "):"";
                            echo (
$seconds>0)?($seconds." seconds"):"";
                            echo 
")";
                        }
                    }
                    echo 
"<h3>Logged IP Adresses</h3>";
                    
//echo "<tr><th>Host or IP<th>Last Access</tr>";
                    
$out="<table width=\"400\">";
                    
$out.="<tr><td style=\"text-align:center\">";
                    if (
$prev >= 0) {
                        if (
$prev >= $step$out.= "<a href=\"$PHP_SELF?action=showlog&sta=$prev\">[Prev Page]</a> ";
                        else 
$out.= "<a href=\"$PHP_SELF?action=showlog\">[Prev Page]</a> ";
                        
$out.= "<a href=\"$PHP_SELF?action=showlog\">[Index]</a> ";
                    } else {
                        
$out.="[Prev Page] [Index] ";
                    }
                    if (
$step <= $count$out.= "<a href=\"$PHP_SELF?action=showlog&sta=$next\">[Next Page]</a> ";
                    else 
$out.="[Next Page] ";
                    
$out.= "</tr></table><div style=\"width: 960px; height:330px; overflow:auto\"><table class=\"form expand\" style=\"white-space: nowrap;\"><tr>";
                    while(
$item=mysqli_fetch_assoc($result)) {
                        
                        
$res2=mysqli_query($db_link,"select * from bans where ip='".$item['ip']."'") or die(mysqli_error($db_link));
                        if (
mysqli_num_rows($res2) > 0$banned_ip=true;
                        else 
$banned_ip=false;
                        
$res2=mysqli_query($db_link,"select * from bans where ip='".$item['proxy']."'") or die(mysqli_error($db_link));
                        if (
mysqli_num_rows($res2) > 0$banned_proxy=true;
                        else 
$banned_proxy=false;
                        
$out.="<tr><td>";
                        
$out.="[<a href=\"http://www.google.com/search?q=".$item['ip']."\" target=\"_blank\">Google</a>
                        <a href=\"https://www.abuseipdb.com/check/"
.$item['ip']."\" target=\"_blank\">AbuseIPDB</a>
                        <a href=\"http://www.projecthoneypot.org/ip_"
.$item['ip']."\" target=\"_blank\">ProjectHoneyPot</a>
                        <a href=\"http://www.stopforumspam.com/ipcheck/"
.$item['ip']."\" target=\"_blank\">StopForumSpam</a>] ";
                        
                        if (
$banned_ip$out.="<b>";
                        else if (
$banned_proxy$out.="<b><i>";
                        
$out.="<a href=\"$PHP_SELF?action=showlog&ip=".$item['ip']."\">".(!empty($item['host'])?$item['host']:$item['ip'])."</a>";
                        if (
$banned_proxy$out.= "</i></b>";
                        else if (
$banned_ip$out.= "</b>";
                        
$out.="<td>".date("Y-m-d H:i:s",$item['maxdate']);
                        
$out.="<td>";
                        
$t $item['maxdate']-$item['mindate'];
                        
$days floor($t/60/60/24);
                        
$hours floor($t/60/60)-$days*24;
                        
$minutes floor($t/60)-$hours*60-$days*24*60;
                        
$seconds $t-$minutes*60-$hours*60*60-$days*24*60*60;
                        if (
$t 0) {
                            
$vps=$item['count']/$t;
                            
$vpm=$vps*60;
                            
$vph=$vpm*60;
                            
$vpd=$vph*24;
                            
$vpw=$vpd*7;
                            
$vpt=$vpd*30;
                            
$vpy=$vpd*365;
                            
$out.= "<a title=\"";
                            
$out.= ($days>0)?($days." days "):"";
                            
$out.= ($hours>0)?($hours." hours "):"";
                            
$out.= ($minutes>0)?($minutes." minutes "):"";
                            
$out.= ($seconds>0)?($seconds." seconds"):"";
                            
$out.= "\">";
                        }
                        
$out.= $item['count']." visits";
                        if (
$t 0) {
                            if (
$vps >= 1$out.= ", ".floor($vps)." queries per second"
                            else if (
$vpm >= 1$out.= ", ".floor($vpm)." queries per minute";
                            else if (
$vph >= 1$out.= ", ".floor($vph)." queries per hour";
                            else if (
$vpd >= 1$out.= ", ".floor($vpd)." queries per day";
                            else if (
$vpw >= 1$out.= ", ".floor($vpw)." queries per week";
                            else if (
$vpt >= 1$out.= ", ".floor($vpt)." queries per month";
                            else if (
$vpy >= 1$out.= ", ".floor($vpy)." queries per year";
                            
$out.= "</a>";
                        }
                        
$out.="</tr>\n";
                        
                        
                    }
                    
$out.="</table></div>";
                    echo 
$out;
                    echo 
"<hr><a href=\"$PHP_SELF?action=showlog&ip=all\">Show All Entries</a>";
                } else echo 
"No entries found jet.";
            }
        }
            
        
    } 
    

}
?>