From fcd2064983726be683ad3291c7c3652128c41936 Mon Sep 17 00:00:00 2001 From: pezcurrel Date: Tue, 27 Aug 2024 19:36:09 +0200 Subject: [PATCH] First commit --- web/lib/ckratelimit.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 web/lib/ckratelimit.php diff --git a/web/lib/ckratelimit.php b/web/lib/ckratelimit.php new file mode 100644 index 0000000..b8ae8b1 --- /dev/null +++ b/web/lib/ckratelimit.php @@ -0,0 +1,26 @@ +false,'error'=>'no «date» header']; + if (!isset($aaheaders['x-ratelimit-reset'])) return ['ok'=>false,'error'=>'no «x-ratelimit-reset» 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']; + $remaining=$aaheaders['x-ratelimit-remaining']+0; + $date=@strtotime($aaheaders['date']); + if (!is_int($date)) return ['ok'=>false,'error'=>'«date» header could not be converted to a unix timestamp']; + $reset=@strtotime($aaheaders['x-ratelimit-reset']); + if (!is_int($reset)) return ['ok'=>false,'error'=>'«x-ratelimit-reset» header could not be converted to a unix timestamp']; + // don't do the one on the line below, since it happens lots of times + //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 ($remaining==0) + return ['ok'=>true,'sleep'=>$reset-$date+1,'remaining'=>$remaining]; + else + return ['ok'=>true,'sleep'=>0,'remaining'=>$remaining]; +} +?>