github-counter/index.php
2020-07-22 12:54:04 +02:00

35 lines
1.3 KiB
PHP

<?php
include 'vendor/autoload.php';
use donatj\UserAgent\UserAgentParser;
try {
$db_handle = new PDO('sqlite:database.sqlite');
$db_handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$tables = $db_handle->query("SELECT name FROM sqlite_master WHERE type='table'")->fetch(PDO::FETCH_ASSOC);
if (!in_array('counter', $tables, true)) {
$db_handle->exec("CREATE TABLE `counter` ( ID INTEGER PRIMARY KEY, ua TEXT, ip TEXT, created_at DATE );");
}
$anonymized_ip = sprintf('%s.0.0', implode('.', array_slice(explode('.', $_SERVER['REMOTE_ADDR']), 0, 2)));
$parser = new UserAgentParser();
$ua = $parser->parse();
$insert_request = $db_handle->prepare('INSERT INTO `counter` ( ua, ip, created_at ) VALUES (:ua, :ip, :created_at)');
$query = $insert_request->execute([
'ua' => sprintf('%s / %s / %s', $ua->platform(), $ua->browser(), $ua->browserVersion()),
'ip' => $anonymized_ip,
'created_at' => date('Y-m-d H:i:s'),
]);
$count = $db_handle->query('SELECT count(*) as `count` from `counter`')->fetch(PDO::FETCH_ASSOC)['count'];
include 'badge.php';
header('Content-Type: image/svg+xml');
header('Cache-Control: max-age=0, must-revalidate, no-cache, no-store, private');
exit;
} catch (Exception $e) {
die('Erreur : '.$e->getMessage());
}