_lookup($_REQUEST['lookup']);
}else{
$this->_stats();
}
echo $this->_lookupform();
}
function _stats(){
print $this->locale_xhtml('stats');
$days = 7;
$list = $this->_readlines($days);
$all = 0;
$stats = array();
foreach($list as $line){
if(!$line) continue;
$data = explode("\t",$line);
$stats[$data[6]] = (int) $stats[$data[6]] + 1;
$all++;
}
arsort($stats);
printf('
'.$this->getLang('blocked').'
',$all,$days);
echo '';
echo '';
echo '| '.$this->getLang('percent').' | ';
echo ''.$this->getLang('count').' | ';
echo ''.$this->getLang('reason').' | ';
echo '
';
foreach($stats as $code => $count){
$resp = bb2_get_response($code);
echo '';
echo '| ';
printf("%.2f%%",100*$count/$all);
echo ' | ';
echo '';
echo $count;
echo ' | ';
echo '';
echo $resp['log'];
echo ' | ';
echo '
';
}
echo '
';
}
function _lookup($key){
global $ID;
global $conf;
global $lang;
print $this->locale_xhtml('lookup');
$code = str_replace('-','',$key);
$ip = hexdec(substr($code,0,2)).'.'.
hexdec(substr($code,2,2)).'.'.
hexdec(substr($code,4,2)).'.'.
hexdec(substr($code,6,2));
$code = substr($code,8);
$resp = bb2_get_response($code);
printf(''.$this->getLang('lkpresult').'
',
$ip,$resp['log'],$resp['explanation'],hsc($key));
printf(''.$this->getLang('lkplist').'
',7);
$lines = preg_grep('/'.preg_quote($ip).'/',$this->_readlines());
if(count($lines)){
echo '';
foreach($lines as $line){
$fields = explode("\t",$line);
$resp = bb2_get_response($fields[6]);
echo '';
echo '| '.strftime($conf['dformat'],$fields[0]).' | ';
echo ''.hsc($fields[1]).' | ';
echo ''.hsc($fields[2]).' | ';
echo ''.hsc($fields[3]).' | ';
echo ''.hsc($fields[4]).' | ';
echo ''.hsc($fields[5]).' | ';
echo ''.$resp['log'].' | ';
echo '
';
}
echo '
';
}else{
echo ''.$lang['nothingfound'].'
';
}
}
function _lookupform(){
global $lang;
echo '';
echo '';
echo '
';
}
/**
* Read loglines backward
*/
function _readlines($days=7){
global $conf;
$file = $conf['cachedir'].'/badbehaviour.log';
$date = time() - ($days*24*60*60);
$data = array();
$lines = array();
$chunk_size = 8192;
if (!@file_exists($file)) return $data;
$fp = fopen($file, 'rb');
if ($fp===false) return $data;
//seek to end
fseek($fp, 0, SEEK_END);
$pos = ftell($fp);
$chunk = '';
while($pos){
// how much to read? Set pointer
if($pos > $chunk_size){
$pos -= $chunk_size;
$read = $chunk_size;
}else{
$read = $pos;
$pos = 0;
}
fseek($fp,$pos);
$tmp = fread($fp,$read);
if($tmp === false) break;
$chunk = $tmp.$chunk;
// now split the chunk
$cparts = explode("\n",$chunk);
// keep the first part in chunk (may be incomplete)
if($pos) $chunk = array_shift($cparts);
// no more parts available, read on
if(!count($cparts)) continue;
// put the new lines on the stack
$lines = array_merge($cparts,$lines);
// check date of first line:
list($cdate) = explode("\t",$cparts[0]);
if($cdate < $date) break; // we have enough
}
fclose($fp);
return $lines;
}
}
//Setup VIM: ex: et ts=4 :