From fe437f882228b58bf16c2994470f80a2dbc134de Mon Sep 17 00:00:00 2001 From: sophie Date: Mon, 31 Aug 2026 10:42:13 +0200 Subject: git commit --- counter.php | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 counter.php (limited to 'counter.php') diff --git a/counter.php b/counter.php new file mode 100644 index 0000000..0ec74d2 --- /dev/null +++ b/counter.php @@ -0,0 +1,69 @@ +. +$dbPath = '/var/lib/sophii-counter/counter.db'; +$robotsPath = '/var/www/html/robots.txt'; + +function isKnownBot(string $userAgent, string $robotsPath): bool { + if ($userAgent === '') { + return true; + } + + if (!is_readable($robotsPath)) { + return false; + } + + static $botNames = null; + if ($botNames === null) { + $botNames = []; + foreach (file($robotsPath, FILE_IGNORE_NEW_LINES) as $line) { + if (preg_match('/^User-agent:\s*(.+)$/i', trim($line), $m)) { + $name = trim($m[1]); + if (preg_match('/^[A-Za-z0-9 _\-\.]+$/', $name) && strlen($name) > 2) { + $botNames[] = $name; + } + } + } + } + + foreach ($botNames as $bot) { + if (stripos($userAgent, $bot) !== false) { + return true; + } + } + + $genericPatterns = '/curl|wget|python-requests|go-http-client|scrapy|headless/i'; + if (preg_match($genericPatterns, $userAgent)) { + return true; + } + return false; +} + +$userAgent = $_SERVER['HTTP_USER_AGENT'] ?? ''; +$isBot = isKnownBot($userAgent, $robotsPath); + +$db = new SQLite3($dbPath); +$db->busyTimeout(2000); +$db->exec('CREATE TABLE IF NOT EXISTS hits (id INTEGER PRIMARY KEY, count INTEGER)'); +$db->exec('INSERT OR IGNORE INTO hits (id, count) VALUES (1, 0)'); + +if (!$isBot) { + $stmt = $db->prepare('UPDATE hits SET count = count + 1 WHERE id = 1'); + $stmt->execute(); +} + +$result = $db->querySingle('SELECT count FROM hits WHERE id = 1'); + +header('Content-Type: text/plain'); +echo $result; -- cgit v1.2.3