Replace OpenVKPresenter

Code formatted for nice view.
And removed tracy warning in function onBeforeRender() - added <if> statement to check user session.
This commit is contained in:
FireFox1121 2021-11-11 11:14:07 +03:00 committed by GitHub
parent 610b2bda6d
commit e2141d17af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,5 +1,9 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);
namespace openvk\Web\Presenters;
use Chandler\Signaling\SignalManager;
use Chandler\MVC\SimplePresenter;
use Chandler\Session\Session;
@ -7,16 +11,19 @@ use Chandler\Security\Authenticator;
use Latte\Engine as TemplatingEngine;
use openvk\Web\Models\Entities\IP;
use openvk\Web\Themes\Themepacks;
use openvk\Web\Models\Repositories\{IPs, Users, APITokens};
use openvk\Web\Models\Repositories\{
IPs,
Users,
APITokens
};
abstract class OpenVKPresenter extends SimplePresenter {
abstract class OpenVKPresenter extends SimplePresenter
{
protected $banTolerant = false;
protected $errorTemplate = "@error";
protected $user = NULL;
private function calculateQueryString(array $data): string
{
private function calculateQueryString(array $data): string {
$rawUrl = "tcp+stratum://fakeurl.net$_SERVER[REQUEST_URI]"; #HTTP_HOST can be tainted
$url = (object) parse_url($rawUrl);
$path = $url->path;
@ -24,8 +31,7 @@ abstract class OpenVKPresenter extends SimplePresenter
return "$path?" . http_build_query(array_merge($_GET, $data));
}
protected function flash(string $type, string $title, ?string $message = NULL, ?int $code = NULL): void
{
protected function flash(string $type, string $title, ?string $message = NULL, ?int $code = NULL): void {
Session::i()->set("_error", json_encode([
"type" => $type,
"title" => $title,
@ -34,13 +40,11 @@ abstract class OpenVKPresenter extends SimplePresenter
]));
}
protected function setTempTheme(string $theme): void
{
protected function setTempTheme(string $theme): void {
Session::i()->set("_tempTheme", $theme);
}
protected function flashFail(string $type, string $title, ?string $message = NULL, ?int $code = NULL): void
{
protected function flashFail(string $type, string $title, ?string $message = NULL, ?int $code = NULL): void {
$this->flash($type, $title, $message, $code);
$referer = $_SERVER["HTTP_REFERER"] ?? "/";
@ -49,8 +53,7 @@ abstract class OpenVKPresenter extends SimplePresenter
exit;
}
protected function logInUserWithToken(): void
{
protected function logInUserWithToken(): void {
$header = $_SERVER["HTTP_AUTHORIZATION"] ?? "";
$token;
@ -71,8 +74,7 @@ abstract class OpenVKPresenter extends SimplePresenter
$this->template->userTainted = false;
}
protected function assertUserLoggedIn(bool $returnUrl = true): void
{
protected function assertUserLoggedIn(bool $returnUrl = true): void {
if (is_null($this->user)) {
$loginUrl = "/login";
if ($returnUrl && $_SERVER["REQUEST_METHOD"] === "GET") {
@ -87,8 +89,7 @@ abstract class OpenVKPresenter extends SimplePresenter
}
}
protected function hasPermission(string $model, string $action, int $context): bool
{
protected function hasPermission(string $model, string $action, int $context): bool {
if (is_null($this->user)) {
if ($model !== "user") {
$this->flash("info", "Недостаточно прав", "Чтобы просматривать эту страницу, нужно зайти на сайт.");
@ -104,9 +105,9 @@ abstract class OpenVKPresenter extends SimplePresenter
return (bool) $this->user->raw->can($action)->model($model)->whichBelongsTo($context === -1 ? null : $context);
}
protected function assertPermission(string $model, string $action, int $context, bool $throw = false): void
{
if($this->hasPermission($model, $action, $context)) return;
protected function assertPermission(string $model, string $action, int $context, bool $throw = false): void {
if ($this->hasPermission($model, $action, $context))
return;
if ($throw)
throw new SecurityPolicyViolationException("Permission error");
@ -114,14 +115,12 @@ abstract class OpenVKPresenter extends SimplePresenter
$this->flashFail("err", "Недостаточно прав", "У вас недостаточно прав чтобы выполнять это действие.");
}
protected function assertCaptchaCheckPassed(): void
{
protected function assertCaptchaCheckPassed(): void {
if (!check_captcha())
$this->flashFail("err", "Неправильно введены символы", "Пожалуйста, убедитесь, что вы правильно заполнили поле с капчей.");
}
protected function willExecuteWriteAction(): void
{
protected function willExecuteWriteAction(): void {
$ip = (new IPs)->get(CONNECTING_IP);
$res = $ip->rateLimit();
@ -132,16 +131,15 @@ abstract class OpenVKPresenter extends SimplePresenter
}
$this->flashFail("err", "Чумба, ты совсем ёбнутый?", "Сходи к мозгоправу, попей колёсики. В OpenVK нельзя вбрасывать щитпосты так часто. Код исключения: $res.");
//$this->flashFail("err", "Чумба, ты совсем ёбнутый?", "Пиздуй к мозгоправу, проглоти колёсики. В OpenVK нельзя вбрасывать щитпосты так часто. Код исключения: $res.");
}
}
protected function signal(object $event): bool
{
protected function signal(object $event): bool {
return (SignalManager::i())->triggerEvent($event, $this->user->id);
}
protected function logEvent(string $type, array $data): bool
{
protected function logEvent(string $type, array $data): bool {
$db = eventdb();
if (!$db)
return false;
@ -165,13 +163,11 @@ abstract class OpenVKPresenter extends SimplePresenter
/**
* @override
*/
protected function sendmail(string $to, string $template, array $params = []): void
{
protected function sendmail(string $to, string $template, array $params = []): void {
parent::sendmail($to, __DIR__ . "/../../Email/$template", $params);
}
function getTemplatingEngine(): TemplatingEngine
{
function getTemplatingEngine(): TemplatingEngine {
$latte = parent::getTemplatingEngine();
$latte->addFilter("translate", function ($s) {
return tr($s);
@ -180,8 +176,7 @@ abstract class OpenVKPresenter extends SimplePresenter
return $latte;
}
function onStartup(): void
{
function onStartup(): void {
$user = Authenticator::i()->getUser();
$this->template->isXmas = intval(date('d')) >= 15 && date('m') == 12 || intval(date('d')) <= 15 && date('m') == 1 ? true : false;
@ -206,7 +201,6 @@ abstract class OpenVKPresenter extends SimplePresenter
$this->user->identity->setOnline(time());
$this->user->identity->save();
}
}
setlocale(LC_TIME, ...(explode(";", tr("__locale"))));
@ -214,8 +208,7 @@ abstract class OpenVKPresenter extends SimplePresenter
parent::onStartup();
}
function onBeforeRender(): void
{
function onBeforeRender(): void {
parent::onBeforeRender();
if (!is_null($this->user)) {
@ -234,9 +227,11 @@ abstract class OpenVKPresenter extends SimplePresenter
$this->template->theme = Themepacks::i()[Session::i()->get("_tempTheme", "ovk")];
else if ($this->requestParam("themePreview"))
$this->template->theme = Themepacks::i()[$this->requestParam("themePreview")];
else if($this->user->identity !== null && $this->user->identity->getTheme())
else if (!is_null($this->user)) {
if ($this->user->identity !== null && $this->user->identity->getTheme()) {
$this->template->theme = $this->user->identity->getTheme();
}
// Знаю, каша ебаная, целестора рефактор всё равно сделает :)))
}
}
}