<?php
// топ статистика
if(!defined('ANTIBOT')) die('access denied');

$title = abTranslate('Top queries');

$table = isset($_GET['table']) ? preg_replace("/[^a-z\_]/","",trim($_GET['table'])) : 'ip';
$status = isset($_GET['status']) ? preg_replace("/[^0-9]/","",trim($_GET['status'])) : '';
$todate = isset($_GET['todate']) ? preg_replace("/[^a-z]/","",trim($_GET['todate'])) : 'lastday';

// искать до указанной даты
$tl['lastday'] = $ab_config['time'] - 86400; // сутки
$tl['lastweek'] = $ab_config['time'] - (86400*7); // неделя
$tl['lastmonth'] = $ab_config['time'] - (86400*30); // месяц
$tl['lastyear'] = $ab_config['time'] - (86400*365); // год

$q = array();

if (isset($tl[$todate])) {
$q[] = 'date >= \''.$tl[$todate].'\'';
}

if ($status != '') {
$q[] = 'passed=\''.$status.'\'';
}

if (count($q) > 0) {
$where = 'WHERE';
} else {
$where = '';
}

$sql = "SELECT count(ROWID) as counter, ".(($table == 'ptr') ? "country, " : '').(($table == 'ip') ? "ptr, country, " : '')." ".$table." FROM hits ".$where." ".implode(' AND ', $q)." GROUP BY ".$table." ORDER BY COUNT(ROWID) DESC LIMIT 200;";

$list = $antibot_db->query($sql); 

$content .= '
<form class="form-inline" action="?'.$abw.$abp.'=top" method="get">';
foreach ($abp_get as $k => $v) {
$content .= '<input name="'.$k.'" type="hidden" value="'.$v.'">';
}
$content .= '<input name="'.$abp.'" type="hidden" value="top">
'.abTranslate('status:').'
<select class="form-control mx-sm-3 form-control-sm" name="status">
<option value="">'.abTranslate('any').'</option>
<option value="0" '.(($status == '0') ? 'selected' : '').'>stop</option>
<option value="1" '.(($status == '1') ? 'selected' : '').'>auto</option>
<option value="2" '.(($status == '2') ? 'selected' : '').'>post</option>
<option value="3" '.(($status == '3') ? 'selected' : '').'>local</option>
<option value="4" '.(($status == '4') ? 'selected' : '').'>allow</option>
<option value="5" '.(($status == '5') ? 'selected' : '').'>goodip</option>
<option value="6" '.(($status == '6') ? 'selected' : '').'>block</option>
<option value="7" '.(($status == '7') ? 'selected' : '').'>fake</option>
</select> 
'.abTranslate('table:').'
<select class="form-control mx-sm-3 form-control-sm" name="table">
<option value="ip" '.(($table == 'ip') ? 'selected' : '').'>IP</option>
<option value="ptr" '.(($table == 'ptr') ? 'selected' : '').'>PTR</option>
<option value="useragent" '.(($table == 'useragent') ? 'selected' : '').'>useragent</option>
<option value="uid" '.(($table == 'uid') ? 'selected' : '').'>uid</option>
<option value="cid" '.(($table == 'cid') ? 'selected' : '').'>cid</option>
<option value="country" '.(($table == 'country') ? 'selected' : '').'>country</option>
<option value="referer" '.(($table == 'referer') ? 'selected' : '').'>referer</option>
<option value="page" '.(($table == 'page') ? 'selected' : '').'>page</option>
<option value="lang" '.(($table == 'lang') ? 'selected' : '').'>lang</option>
<option value="recaptcha" '.(($table == 'recaptcha') ? 'selected' : '').'>recaptcha score</option>
<option value="js_w" '.(($table == 'js_w') ? 'selected' : '').'>monitor width</option>
<option value="js_h" '.(($table == 'js_h') ? 'selected' : '').'>monitor height</option>
<option value="js_cw" '.(($table == 'js_cw') ? 'selected' : '').'>window width</option>
<option value="js_ch" '.(($table == 'js_ch') ? 'selected' : '').'>window height</option>
<option value="js_co" '.(($table == 'js_co') ? 'selected' : '').'>colordepth</option>
<option value="js_pi" '.(($table == 'js_pi') ? 'selected' : '').'>pixeldepth</option>
<option value="refhost" '.(($table == 'refhost') ? 'selected' : '').'>refhost</option>
<option value="asnum" '.(($table == 'asnum') ? 'selected' : '').'>asnum</option>
<option value="asname" '.(($table == 'asname') ? 'selected' : '').'>asname</option>
<option value="result" '.(($table == 'result') ? 'selected' : '').'>result</option>
<option value="http_accept" '.(($table == 'http_accept') ? 'selected' : '').'>http_accept</option>
<option value="method" '.(($table == 'method') ? 'selected' : '').'>http method</option>
<option value="ym_uid" '.(($table == 'ym_uid') ? 'selected' : '').'>yandex clientid</option>
<option value="ga_uid" '.(($table == 'ga_uid') ? 'selected' : '').'>ga clientid</option>
</select>
<select class="form-control mx-sm-3 form-control-sm" name="todate">
<option value="lastday" '.(($todate == 'lastday') ? 'selected' : '').'>'.abTranslate('Last Day').'</option>
<option value="lastweek" '.(($todate == 'lastweek') ? 'selected' : '').'>'.abTranslate('Last Week').'</option>
<option value="lastmonth" '.(($todate == 'lastmonth') ? 'selected' : '').'>'.abTranslate('Last Month').'</option>
<option value="lastyear" '.(($todate == 'lastyear') ? 'selected' : '').'>'.abTranslate('Last Year').'</option>
<option value="" '.(($todate == '') ? 'selected' : '').'>'.abTranslate('All time').'</option>
</select> 

<input style="cursor:pointer;" class="btn btn-sm btn-primary" type="submit" name="submit" value="'.abTranslate('Search').'">
</form>
<br />
<table class="table table-bordered table-hover table-sm">
<thead class="thead-light">
<tr>
<th>'.$table.'</th>';
if ($table == 'ip') {
$content .= '<th>ptr</th><th>whois</th><th>country</th>';
} elseif ($table == 'ptr') {
$content .= '<th>country</th>';
}
$content .= '<th>amount</th>
<th>log</th>
</tr>
</thead>
<tbody>
';

while ($echo = $list->fetchArray(SQLITE3_ASSOC)) {
$content .= '<tr>
<td style="word-break: break-all;">'.$echo[$table].'</td>';
if ($table == 'ip') {
$content .= '<td style="word-break: break-all;">'.$echo['ptr'].'</td>
<td><a href="?'.$abw.$abp.'=ip&ip='.$echo['ip'].'" target="_blank" rel="noopener">whois</a></td>
<td><div class="fflag fflag-'.$echo['country'].' ff-lg" title="'.$echo['country'].'"></div> '.$echo['country'].'</td>';
} elseif ($table == 'ptr') {
$content .= '<td><div class="fflag fflag-'.$echo['country'].' ff-lg" title="'.$echo['country'].'"></div> '.$echo['country'].'</td>';
}
if ($echo[$table] == '') {$echo[$table] = 'null';}
$content .= '<td>'.$echo['counter'].'</td>
<td><a href="?'.$abw.$abp.'=hits&search='.urlencode($echo[$table]).'&table='.$table.'&status='.$status.'&todate='.$todate.'&operator=equally" title="'.abTranslate('selection by:').' '.$table.'" target="_blank">view</a></td>
</tr>';
}

$content .= '</tbody>
</table>';
