| Current Path : /home/h/a/p/happyrenas/fun/public/api/ |
Linux webd005.cluster105.gra.hosting.ovh.net 5.15.206-ovh-vps-grsec-zfs-classid #1 SMP Fri May 15 02:41:25 UTC 2026 x86_64 |
| Current File : /home/h/a/p/happyrenas/fun/public/api/_rate_limit.php |
<?php
function rl_file(string $bucket, array $config): string {
$dir = $config['ratelimit']['dir'] ?? sys_get_temp_dir();
$ip = $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
return rtrim($dir, '/\\') . '/' . sha1($bucket . ':' . $ip) . '.json';
}
function rate_limit_or_429(string $kind, array $config): void {
$rule = $config['ratelimit'][$kind] ?? null;
if (!$rule) return;
$window = (int)($rule['window_seconds'] ?? 600);
$max = (int)($rule['max'] ?? 60);
$file = rl_file($kind, $config);
$now = time();
$j = null;
if (file_exists($file)) {
$raw = @file_get_contents($file);
$j = json_decode($raw ?: '', true);
}
if (!is_array($j)) $j = ['resetAt' => $now + $window, 'count' => 0];
if ($now > (int)$j['resetAt']) {
$j = ['resetAt' => $now + $window, 'count' => 0];
}
$j['count'] = (int)$j['count'] + 1;
@file_put_contents($file, json_encode($j));
if ($j['count'] > $max) {
json_error(429, 'Trop de requêtes. Réessaie plus tard.');
}
}