Added “showrem” parameter, defaulting to false; shows remaining hits when true

This commit is contained in:
pezcurrel 2024-10-23 23:10:11 +02:00
parent 08bd2a0005
commit 1b939b6dc0

View file

@ -1,6 +1,6 @@
<?php <?php
function ckratelimit($headers) { function ckratelimit($headers,$showrem=false) {
$aaheaders=[]; $aaheaders=[];
array_shift($headers); array_shift($headers);
foreach ($headers as $header) foreach ($headers as $header)
@ -13,6 +13,7 @@ function ckratelimit($headers) {
if (!isset($aaheaders['x-ratelimit-remaining'])) return ['ok'=>false,'error'=>'no «x-ratelimit-remaining» header']; if (!isset($aaheaders['x-ratelimit-remaining'])) return ['ok'=>false,'error'=>'no «x-ratelimit-remaining» header'];
if (preg_match('#^\d+$#',$aaheaders['x-ratelimit-remaining'])!==1) return ['ok'=>false,'error'=>'«x-ratelimit-remaining» header is not an integer']; if (preg_match('#^\d+$#',$aaheaders['x-ratelimit-remaining'])!==1) return ['ok'=>false,'error'=>'«x-ratelimit-remaining» header is not an integer'];
$remaining=$aaheaders['x-ratelimit-remaining']+0; $remaining=$aaheaders['x-ratelimit-remaining']+0;
if ($showrem) echo "ckratelimit: remaining: {$remaining}\n";
$date=@strtotime($aaheaders['date']); $date=@strtotime($aaheaders['date']);
if (!is_int($date)) return ['ok'=>false,'error'=>'«date» header could not be converted to a unix timestamp']; if (!is_int($date)) return ['ok'=>false,'error'=>'«date» header could not be converted to a unix timestamp'];
$reset=@strtotime($aaheaders['x-ratelimit-reset']); $reset=@strtotime($aaheaders['x-ratelimit-reset']);