Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(away.php): fix link checking #949

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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