41 lines
1.1 KiB
PHP
41 lines
1.1 KiB
PHP
<?php
|
|
|
|
class BotLogger
|
|
{
|
|
const LOG_FILE = _PS_ROOT_DIR_ . '/var/logs/botlimiter_ban.log';
|
|
const MAX_SIZE = 10485760; // 10 MB
|
|
|
|
public static function logBan($ip, $reason)
|
|
{
|
|
// 1. Check file size before writing (The Safety Valve)
|
|
if (file_exists(self::LOG_FILE) && filesize(self::LOG_FILE) > self::MAX_SIZE) {
|
|
self::rotateLog();
|
|
}
|
|
|
|
$date = date('Y-m-d H:i:s');
|
|
$message = sprintf("[%s] [IP:%s] [REASON:%s]" . PHP_EOL, $date, $ip, $reason);
|
|
|
|
// 2. Append to log file
|
|
file_put_contents(self::LOG_FILE, $message, FILE_APPEND | LOCK_EX);
|
|
}
|
|
|
|
/**
|
|
* Rotates the log:
|
|
* 1. Deletes the .old file
|
|
* 2. Renames current .log to .old
|
|
* 3. Current logging continues in new empty file
|
|
*/
|
|
private static function rotateLog()
|
|
{
|
|
$backup_file = self::LOG_FILE . '.old';
|
|
|
|
// Remove ancient backup
|
|
if (file_exists($backup_file)) {
|
|
@unlink($backup_file);
|
|
}
|
|
|
|
// Rename current to backup
|
|
@rename(self::LOG_FILE, $backup_file);
|
|
}
|
|
}
|