openvk/Web/Models/Repositories/IPs.php
Alma Armas aed731a59f [SECURITY] Fix IP-based rate-limiting
Co-authored-by: Celestine <34442450+rem-pai@users.noreply.github.com>
2021-01-07 16:23:24 +00:00

34 lines
793 B
PHP

<?php declare(strict_types=1);
namespace openvk\Web\Models\Repositories;
use Chandler\Database\DatabaseConnection;
use openvk\Web\Models\Entities\IP;
class IPs
{
private $context;
private $ips;
function __construct()
{
$this->context = DatabaseConnection::i()->getContext();
$this->ips = $this->context->table("ip");
}
function get(string $ip): ?IP
{
$bip = inet_pton($ip);
if(!$bip)
throw new \UnexpectedValueException("Malformed IP address");
$res = $this->ips->where("ip", $bip)->fetch();
if(!$res) {
$res = new IP;
$res->setIp($ip);
$res->save();
return $res;
}
return new IP($res);
}
}