Using default “lib” version

This commit is contained in:
pezcurrel 2024-08-27 21:18:12 +02:00
parent 0a8d7377ad
commit f94d03ffdf

View file

@ -1,36 +1,26 @@
<?php <?php
function ckratelimit($headers,$echofun,$onlyret=false,$verbose=false) {//$echofun has to be the name of a defined function to pass messages to function ckratelimit($headers) {
$ret=null; $aaheaders=[];
if (is_array($headers)) { array_shift($headers);
//echo "ckratelimit: {$headers}: ".print_r($headers,true)); foreach ($headers as $header)
$buff=[]; if (preg_match('#^([^:]+): (.*)$#',$header,$matches)===1)
array_shift($headers); $aaheaders[strtolower($matches[1])]=$matches[2];
foreach ($headers as $header) //$aaheaders['x-ratelimit-remaining']=0;
if (preg_match('/^([^:]+):(.*)$/Uu',$header,$matches)===1) //print_r($aaheaders);
$buff[$matches[1]]=trim($matches[2]); if (!isset($aaheaders['date'])) return ['ok'=>false,'error'=>'no «date» header'];
$headers=$buff; if (!isset($aaheaders['x-ratelimit-reset'])) return ['ok'=>false,'error'=>'no «x-ratelimit-reset» header'];
//print_r($headers); if (!isset($aaheaders['x-ratelimit-remaining'])) return ['ok'=>false,'error'=>'no «x-ratelimit-remaining» header'];
if (isset($headers['Date']) && isset($headers['X-RateLimit-Reset']) && isset($headers['X-RateLimit-Remaining'])) { if (preg_match('#^\d+$#',$aaheaders['x-ratelimit-remaining'])!==1) return ['ok'=>false,'error'=>'«x-ratelimit-remaining» header is not an integer'];
//Wed, 30 Mar 2022 21:27:22 GMT $remaining=$aaheaders['x-ratelimit-remaining']+0;
$srvnow=strtotime($headers['Date']); $date=@strtotime($aaheaders['date']);
//2022-03-31T04:05:00.058705Z if (!is_int($date)) return ['ok'=>false,'error'=>'«date» header could not be converted to a unix timestamp'];
$srvrlreset=strtotime($headers['X-RateLimit-Reset']); $reset=@strtotime($aaheaders['x-ratelimit-reset']);
$srvrlremain=$headers['X-RateLimit-Remaining']; if (!is_int($reset)) return ['ok'=>false,'error'=>'«x-ratelimit-reset» header could not be converted to a unix timestamp'];
$secstoreset=$srvrlreset-$srvnow; // don't do the one on the line below, since it happens lots of times
$ret=['remaining'=>$srvrlremain,'secstoreset'=>$secstoreset]; //if ($reset<$date) return ['ok'=>false,'error'=>'the unix timestamp parsed from «x-ratelimit-reset» header is less than the unix timestamp parsed from «date» header'];
if ($onlyret) if ($remaining==0)
return $ret; return ['ok'=>true,'sleep'=>$reset-$date+1,'remaining'=>$remaining];
if ($verbose) $echofun("ckratelimit: X-RateLimit-Remaining: {$srvrlremain}; server time: {$srvnow}: ".gmdate('c',$srvnow).'; X-RateLimit-Reset: '.gmdate('c',$srvrlreset).'; current seconds before reset: '.$secstoreset.".\n"); else
if ($srvrlremain==0) { return ['ok'=>true,'sleep'=>0,'remaining'=>$remaining];
$echofun("Reached rate limit, waiting {$secstoreset} seconds for rate limit reset ...\n");
sleep($secstoreset);
}
} else {
if ($verbose) $echofun("ckratelimit: no «Date» / «X-RateLimit-Reset» / «X-RateLimit-Remaining» header(s)!\n");
}
} else {
if ($verbose) $echofun("ckratelimit: headers is not an array!\n");
}
return $ret;
} }
?> ?>