# searchpro/4.0.21/BerqWP/src/RateLimiter.php

BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS &amp; JavaScript, version 4.0.21. 62 lines.

- Page: https://pluginprobe.com/plugins/searchpro/4.0.21/code/BerqWP/src/RateLimiter.php
- Raw: https://pluginprobe.com/plugins/searchpro/4.0.21/raw/BerqWP/src/RateLimiter.php
- Modified: 2025-04-19T13:22:34+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/searchpro/4.0.21/code/BerqWP/src/RateLimiter.php#L10-L20`.

```php
<?php

namespace BerqWP;

class RateLimiter
{
    private $limit;
    private $timeWindow; // In seconds
    private $storagePath;

    public function __construct($limit, $timeWindow, $storagePath)
    {
        $this->limit = $limit;
        $this->timeWindow = $timeWindow; // e.g. 60 seconds for 1 minute
        $this->storagePath = rtrim($storagePath, '/') . '/';
    }

    public function isRateLimited($clientIdentifier)
    {
        if (!is_dir($this->storagePath)) {
            mkdir($this->storagePath, 0755, true);
        }

        $filePath = $this->storagePath . md5($clientIdentifier) . '.json';

        if (!file_exists($filePath)) {
            // Create a new entry if it doesn't exist
            $this->createLog($filePath);
            return false; // Not rate limited
        }

        // Read the log file
        $logData = json_decode(file_get_contents($filePath), true);
        $currentTime = time();

        if (!is_array($logData)) {
            $logData = [];
        }

        // Remove old entries from the time window
        $logData = array_filter($logData, function($timestamp) use ($currentTime) {
            return ($currentTime - $timestamp) <= $this->timeWindow;
        });

        // Check if the limit is reached
        if (count($logData) >= $this->limit) {
            return true; // Rate limited
        }

        // Otherwise, add the new timestamp and update the log
        $logData[] = $currentTime;
        file_put_contents($filePath, json_encode($logData));
        return false;
    }

    private function createLog($filePath)
    {
        $logData = [time()];
        file_put_contents($filePath, json_encode($logData));
    }
}

```
