-
- // Add filecount, timeout not reached.
- if ($result['files'] < $fileLimit and !$this->compareTime($result['time'], $rateTimeout)) {
- $this->updateRateLimit($result['files'] + $fingerPrintInfo['files_amount'], false, $fingerPrintInfo);
+
+ public function checkRateLimit(array $fingerPrintInfo, int $rateTimeout, int $fileLimit):bool
+ {
+ $query = match ($this->dbType) {
+ 'pgsql' => 'SELECT EXISTS(SELECT id FROM ratelimit WHERE iphash = (:iphash)), id, iphash, files, time FROM ratelimit WHERE iphash = (:iphash) LIMIT 1',
+ default => 'SELECT * FROM ratelimit WHERE iphash = (:iphash) AND EXISTS (SELECT id FROM ratelimit WHERE iphash = (:iphash)) LIMIT 1'
+ };
+ $q = $this->DB->prepare($query);
+ $q->bindValue(':iphash', $fingerPrintInfo['ip_hash']);
+ $q->execute();
+ $result = $q->fetch();
+ $q->closeCursor();
+ //If there is no other match a record does not exist, create one.
+ if (!$result) {
+ $this->createRateLimit($fingerPrintInfo);
+ return false;
+ }
+ // Apply rate-limit when file count reached and timeout not reached.
+ if ($result['files'] === $fileLimit and !$this->compareTime($result['time'], $rateTimeout)) {
+ return true;
+ }
+ // Update timestamp if timeout reached, reset file count and add the incoming file count.
+ if ($this->compareTime($result['time'], $rateTimeout)) {
+ $this->updateRateLimit($fingerPrintInfo['files_amount'], true, $fingerPrintInfo);
+ return false;
+ }
+ // Add filecount, timeout not reached.
+ if ($result['files'] < $fileLimit and !$this->compareTime($result['time'], $rateTimeout)) {
+ $this->updateRateLimit($result['files'] + $fingerPrintInfo['files_amount'], false, $fingerPrintInfo);
+ return false;
+ }