openvk/Web/Models/Entities/IP.php

116 lines
3.3 KiB
PHP
Raw Permalink Normal View History

<?php declare(strict_types=1);
namespace openvk\Web\Models\Entities;
use openvk\Web\Models\RowModel;
use openvk\Web\Util\DateTime;
class IP extends RowModel
{
protected $tableName = "ip";
const RL_RESET = 0;
const RL_CANEXEC = 1;
const RL_VIOLATION = 2;
const RL_BANNED = 3;
function getIp(): string
{
return inet_ntop($this->getRecord()->ip);
}
function getDiscoveryDate(): DateTime
{
return new DateTime($this->getRecord()->first_seen);
}
function isBanned(): bool
{
return (bool) $this->getRecord()->banned;
}
function ban(): void
{
$this->stateChanges("banned", true);
$this->save();
}
function pardon(): void
{
$this->stateChanges("banned", false);
$this->save();
}
function clear(): void
{
$this->stateChanges("rate_limit_counter_start", 0);
$this->stateChanges("rate_limit_counter", 0);
$this->stateChanges("rate_limit_violation_counter_start", 0);
$this->stateChanges("rate_limit_violation_counter", 0);
$this->save();
}
function rateLimit(int $actionComplexity = 1): int
{
$counterSessionStart = $this->getRecord()->rate_limit_counter_start;
$vCounterSessionStart = $this->getRecord()->rate_limit_violation_counter_start;
$aCounter = $this->getRecord()->rate_limit_counter;
$vCounter = $this->getRecord()->rate_limit_violation_counter;
$config = (object) OPENVK_ROOT_CONF["openvk"]["preferences"]["security"]["rateLimits"];
try {
if((time() - $config->time) > $counterSessionStart) {
$counterSessionStart = time();
$aCounter = $actionComplexity;
return static::RL_RESET;
}
if(($aCounter + $actionComplexity) <= $config->actions) {
$aCounter += $actionComplexity;
return static::RL_CANEXEC;
}
if((time() - $config->maxViolationsAge) > $vCounterSessionStart) {
$vCounterSessionStart = time();
$vCounter = 1;
return static::RL_VIOLATION;
}
$vCounter += 1;
if($vCounter >= $config->maxViolations) {
$this->stateChanges("banned", true);
return static::RL_BANNED;
}
return static::RL_VIOLATION;
} finally {
$this->stateChanges("rate_limit_counter_start", $counterSessionStart);
$this->stateChanges("rate_limit_counter", $aCounter);
$this->stateChanges("rate_limit_violation_counter_start", $vCounterSessionStart);
$this->stateChanges("rate_limit_violation_counter", $vCounter);
$this->save(false);
}
}
function setIp(string $ip): void
{
$ip = inet_pton($ip);
if(!$ip)
throw new \UnexpectedValueException("Malformed IP address");
$this->stateChanges("ip", $ip);
}
[WIP] Textarea: Upload multiple pictures (#800) * VKAPI: Fix bug when DELETED user appear if there is no user_ids * Textarea: Make multiple attachments * постмодернистское искусство * Use only attachPic for grabbing pic attachments TODO throw flashFail on bruh moment with pic attachments * draft masonry picture layout in posts xddd где мои опиаты??? * fix funny typos in computeMasonryLayout * Fix video bruh moment in textarea * Posts: add multiple kakahi for microblog * Photo: Add minimal implementation of миниатюра открывашка Co-authored-by: Daniel <60743585+myslivets@users.noreply.github.com> * Photo: Add ability to slide trough photos in one post This also gives ability to easily implement comments and actions * Photo: The Fxck Is This implementation of comments under photo in viewer * FloatingPhotoViewer: Better CSS - Fix that details background issue - Make slide buttons slightly shorter by height * FloatingPhotoViewer: Refactor, and make it better - Now you can actually check the comments under EVERY photo - Fix for textarea. Now you can publish comments * Fix funny typos xddd * Kinda fix poll display in non-microblog posts * Posts: Fix poll display in microblog posts * Add photos picker (#986) * early implementation of photos pickir Добавлен пикер фоточек и быстрая загрузка фото. Так же пофикшен просмотрщик фото в группах. Но, правда, я сломал копипейст, но это ладн. * Fiks fotos viver four coments. * Add picking photos from clubs albums Копипейст и граффити так и не пофикшены * Fix graffiti and copypaste Какого-то хуя копипаста у постов срабатывает два раза. * some fixesx * dragon drop * Fix PHP 8 compatibility * 5 (#988) --------- Co-authored-by: celestora <kitsuruko@gmail.com> Co-authored-by: Daniel <60743585+myslivets@users.noreply.github.com> Co-authored-by: lalka2016 <99399973+lalka2016@users.noreply.github.com> Co-authored-by: Alexander Minkin <weryskok@gmail.com>
2023-10-03 19:40:13 +03:00
function save(?bool $log = false): void
{
if(is_null($this->getRecord()))
$this->stateChanges("first_seen", time());
parent::save($log);
}
}