Skip to content

Commit

Permalink
Фикс проверки заблокированных ссылок
Browse files Browse the repository at this point in the history
  • Loading branch information
n1rwana committed Aug 5, 2023
1 parent 5c76b56 commit a16e15e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Web/Models/Entities/BannedLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function getComment(): string

function getRegexpRule(): string
{
return addslashes("/" . $this->getDomain() . $this->getRawRegexp() . "/");
return "/^" . $this->getDomain() . "\/" . $this->getRawRegexp() . "$/i";
}

function getRawRegexp(): string
Expand Down
9 changes: 6 additions & 3 deletions Web/Models/Repositories/BannedLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Chandler\Database\DatabaseConnection as DB;
use Nette\Database\Table\{ActiveRow, Selection};
use openvk\Web\Models\Entities\BannedLink;
use function Symfony\Component\Translation\t;

class BannedLinks
{
Expand Down Expand Up @@ -43,7 +44,7 @@ function getByDomain(string $domain): ?Selection

function isDomainBanned(string $domain): bool
{
return sizeof($this->bannedLinks->where(["link" => $domain, "regexp_rule" => ""])) > 0;
return sizeof($this->bannedLinks->where(["domain" => $domain, "regexp_rule" => ""])) > 0;
}

function genLinks($rules): \Traversable
Expand All @@ -57,12 +58,14 @@ function genEntries($links, $uri): \Traversable
foreach($links as $link)
if (preg_match($link->getRegexpRule(), $uri))
yield $link->getId();
else if ($this->isDomainBanned($link->getDomain()))
yield $link->getId();
}

function check(string $url): ?array
{
$uri = strstr(str_replace(["https://", "http://"], "", $url), "/", true);
$domain = str_replace("www.", "", $uri);
$uri = str_replace(["https://", "http://"], "", $url);
$domain = explode("/", str_replace("www.", "", $uri))[0];
$rules = $this->getByDomain($domain);

if (is_null($rules))
Expand Down
4 changes: 2 additions & 2 deletions Web/Presenters/AdminPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ function renderBannedLink(int $id): void
if ($link) {
$link->setDomain($new_domain ?? $this->postParam("link"));
$link->setReason($new_reason);
$link->setRegexp_rule($this->postParam("regexp"));
$link->setRegexp_rule(mb_strlen(trim($this->postParam("regexp"))) > 0 ? $this->postParam("regexp") : "");
$link->save();
} else {
if (!$new_domain)
Expand All @@ -438,7 +438,7 @@ function renderBannedLink(int $id): void
$link = new BannedLink;
$link->setDomain($new_domain);
$link->setReason($new_reason);
$link->setRegexp_rule($this->postParam("regexp"));
$link->setRegexp_rule(mb_strlen(trim($this->postParam("regexp"))) > 0 ? $this->postParam("regexp") : "");
$link->setInitiator($this->user->identity->getId());
$link->save();

Expand Down
2 changes: 1 addition & 1 deletion Web/Presenters/AwayPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ final class AwayPresenter extends OpenVKPresenter
{
function renderAway(): void
{
$checkBanEntries = (new BannedLinks)->check($this->queryParam("to") . "/");
$checkBanEntries = (new BannedLinks)->check($this->queryParam("to"));
if (OPENVK_ROOT_CONF["openvk"]["preferences"]["susLinks"]["warnings"])
if (sizeof($checkBanEntries) > 0)
$this->pass("openvk!Away->view", $checkBanEntries[0]);
Expand Down

0 comments on commit a16e15e

Please sign in to comment.