<?php
// cron для переноса статистики из мемкешед в базу
if (!isset($ab_version)) die('stop cron');
$ab_time_cron = $ab_config['time'];
for ($x = 0; $x < 2; $x++) {
$date = date('Ymd', $ab_time_cron);
clearstatcache();
$test = (int) @filesize(__DIR__.'/../data/counters/test_'.$date) + 0;
@unlink(__DIR__.'/../data/counters/test_'.$date);
$auto = (int) @filesize(__DIR__.'/../data/counters/auto_'.$date) + 0;
@unlink(__DIR__.'/../data/counters/auto_'.$date);
$click = (int) @filesize(__DIR__.'/../data/counters/click_'.$date) + 0;
@unlink(__DIR__.'/../data/counters/click_'.$date);
$miss = (int) @filesize(__DIR__.'/../data/counters/miss_'.$date) + 0;
@unlink(__DIR__.'/../data/counters/miss_'.$date);
$allow = (int) @filesize(__DIR__.'/../data/counters/allow_'.$date) + 0;
@unlink(__DIR__.'/../data/counters/allow_'.$date);
$goodip = (int) @filesize(__DIR__.'/../data/counters/goodip_'.$date) + 0;
@unlink(__DIR__.'/../data/counters/goodip_'.$date);
$local = (int) @filesize(__DIR__.'/../data/counters/local_'.$date) + 0;
@unlink(__DIR__.'/../data/counters/local_'.$date);
$block = (int) @filesize(__DIR__.'/../data/counters/block_'.$date) + 0;
@unlink(__DIR__.'/../data/counters/block_'.$date);
$fakes = (int) @filesize(__DIR__.'/../data/counters/fakes_'.$date) + 0;
@unlink(__DIR__.'/../data/counters/fakes_'.$date);
$husers = (int) @filesize(__DIR__.'/../data/counters/husers_'.$date) + 0;
@unlink(__DIR__.'/../data/counters/husers_'.$date);
$uusers = (int) @filesize(__DIR__.'/../data/counters/uusers_'.$date) + 0;
@unlink(__DIR__.'/../data/counters/uusers_'.$date);
$google = (int) @filesize(__DIR__.'/../data/counters/google_'.$date) + 0;
@unlink(__DIR__.'/../data/counters/google_'.$date);
$yandex = (int) @filesize(__DIR__.'/../data/counters/yandex_'.$date) + 0;
@unlink(__DIR__.'/../data/counters/yandex_'.$date);
$bing = (int) @filesize(__DIR__.'/../data/counters/bing_'.$date) + 0;
@unlink(__DIR__.'/../data/counters/bing_'.$date);

$sql = 'UPDATE counters SET test = test + '.$test.', auto = auto + '.$auto.', click = click + '.$click.', miss = miss + '.$miss.', allow = allow + '.$allow.', goodip = goodip + '.$goodip.', local = local + '.$local.', block = block + '.$block.', fakes = fakes + '.$fakes.', husers = husers + '.$husers.', uusers = uusers + '.$uusers.', google = google + '.$google.', yandex = yandex + '.$yandex.', bing = bing + '.$bing.' WHERE date = \''.$date.'\';';
$update = @$antibot_db->exec($sql);
if ($antibot_db->lastErrorMsg() == 'database is locked') {
file_put_contents(__DIR__.'/../data/sqlerrorlog/'.date("YmdH", $ab_config['time']).floor(date("i")/10).'.txt', $sql."\n", FILE_APPEND | LOCK_EX);
}

if ($antibot_db->changes() == 0) {
$add = @$antibot_db->exec("INSERT INTO counters (test, auto, click, miss, allow, goodip, local, block, fakes, husers, uusers, google, yandex, bing, date) VALUES ('".$test."', '".$auto."', '".$click."', '".$miss."', '".$allow."', '".$goodip."', '".$local."', '".$block."', '".$fakes."', '".$husers."', '".$uusers."', '".$google."', '".$yandex."', '".$bing."', '".$date."');");

}
$ab_time_cron = $ab_time_cron - 86400;
}

// удаление истекших правил:
$del = @$antibot_db->exec("DELETE FROM ipv4rules WHERE expires < ".$ab_config['time'].";");

$del = @$antibot_db->exec("DELETE FROM ipv6rules WHERE expires < ".$ab_config['time'].";");

$del = @$antibot_db->exec("DELETE FROM rules WHERE expires < ".$ab_config['time'].";");

// перенос в базу потерянной статистики:
if ($antibot_db->lastErrorMsg() != 'database is locked') {
$link = glob(__DIR__."/../data/sqlerrorlog/*.txt");
$realtime = date("YmdH", $ab_config['time']).floor(date("i")/10);
foreach ($link as $line) {
if (basename($line, '.txt') != $realtime) {
$file = file($line);
foreach($file as $sql) {
$sql = trim($sql);
if ($sql != '') {
$add = @$antibot_db->exec($sql);
}
}
@unlink($line);
}
}
}

clearstatcache();
