2020-06-07 19:04:43 +03:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
namespace openvk\Web\Presenters;
|
|
|
|
use Chandler\Signaling\SignalManager;
|
|
|
|
use Chandler\MVC\SimplePresenter;
|
|
|
|
use Chandler\Session\Session;
|
|
|
|
use Chandler\Security\Authenticator;
|
|
|
|
use Latte\Engine as TemplatingEngine;
|
2021-01-01 00:18:53 +03:00
|
|
|
use openvk\Web\Models\Entities\IP;
|
2021-11-06 23:43:48 +03:00
|
|
|
use openvk\Web\Themes\Themepacks;
|
2021-11-27 12:24:34 +03:00
|
|
|
use openvk\Web\Models\Repositories\{IPs, Users, APITokens, Tickets};
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
abstract class OpenVKPresenter extends SimplePresenter
|
|
|
|
{
|
|
|
|
protected $banTolerant = false;
|
|
|
|
protected $errorTemplate = "@error";
|
|
|
|
protected $user = NULL;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
return "$path?" . http_build_query(array_merge($_GET, $data));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function flash(string $type, string $title, ?string $message = NULL, ?int $code = NULL): void
|
|
|
|
{
|
|
|
|
Session::i()->set("_error", json_encode([
|
|
|
|
"type" => $type,
|
|
|
|
"title" => $title,
|
|
|
|
"msg" => $message,
|
|
|
|
"code" => $code,
|
|
|
|
]));
|
|
|
|
}
|
2021-11-06 23:43:48 +03:00
|
|
|
|
2021-12-12 16:11:44 +03:00
|
|
|
protected function setTempTheme(string $theme): void
|
|
|
|
{
|
|
|
|
Session::i()->set("_tempTheme", $theme);
|
|
|
|
}
|
2020-06-07 19:04:43 +03:00
|
|
|
|
2021-12-25 19:03:21 +03:00
|
|
|
protected function flashFail(string $type, string $title, ?string $message = NULL, ?int $code = NULL, bool $json = false): void
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
2021-12-25 19:03:21 +03:00
|
|
|
if($json) {
|
|
|
|
$this->returnJson([
|
|
|
|
"success" => $type !== "err",
|
|
|
|
"flash" => [
|
|
|
|
"type" => $type,
|
|
|
|
"title" => $title,
|
|
|
|
"message" => $message,
|
|
|
|
"code" => $code,
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
$this->flash($type, $title, $message, $code);
|
|
|
|
$referer = $_SERVER["HTTP_REFERER"] ?? "/";
|
|
|
|
|
|
|
|
header("HTTP/1.1 302 Found");
|
|
|
|
header("Location: $referer");
|
|
|
|
exit;
|
|
|
|
}
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 21:00:30 +03:00
|
|
|
protected function logInUserWithToken(): void
|
|
|
|
{
|
|
|
|
$header = $_SERVER["HTTP_AUTHORIZATION"] ?? "";
|
|
|
|
$token;
|
|
|
|
|
|
|
|
preg_match("%Bearer (.*)$%", $header, $matches);
|
|
|
|
$token = $matches[1] ?? "";
|
|
|
|
$token = (new APITokens)->getByCode($token);
|
|
|
|
if(!$token) {
|
|
|
|
header("HTTP/1.1 401 Unauthorized");
|
|
|
|
header("Content-Type: application/json");
|
|
|
|
exit(json_encode(["error" => "The access token is invalid"]));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->user = (object) [];
|
|
|
|
$this->user->identity = $token->getUser();
|
|
|
|
$this->user->raw = $this->user->identity->getChandlerUser();
|
|
|
|
$this->user->id = $this->user->identity->getId();
|
|
|
|
$this->template->thisUser = $this->user->identity;
|
|
|
|
$this->template->userTainted = false;
|
|
|
|
}
|
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
protected function assertUserLoggedIn(bool $returnUrl = true): void
|
|
|
|
{
|
|
|
|
if(is_null($this->user)) {
|
|
|
|
$loginUrl = "/login";
|
|
|
|
if($returnUrl && $_SERVER["REQUEST_METHOD"] === "GET") {
|
|
|
|
$currentUrl = function_exists("get_current_url") ? get_current_url() : $_SERVER["REQUEST_URI"];
|
|
|
|
$loginUrl .= "?jReturnTo=" . rawurlencode($currentUrl);
|
|
|
|
}
|
|
|
|
|
2021-12-10 20:21:25 +03:00
|
|
|
$this->flash("err", tr("login_required_error"), tr("login_required_error_comment"));
|
2020-06-07 19:04:43 +03:00
|
|
|
header("HTTP/1.1 302 Found");
|
|
|
|
header("Location: $loginUrl");
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function hasPermission(string $model, string $action, int $context): bool
|
|
|
|
{
|
|
|
|
if(is_null($this->user)) {
|
|
|
|
if($model !== "user") {
|
2021-12-10 20:21:25 +03:00
|
|
|
$this->flash("info", tr("login_required_error"), tr("login_required_error_comment"));
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
header("HTTP/1.1 302 Found");
|
|
|
|
header("Location: /login");
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ($action === "register" || $action === "login");
|
|
|
|
}
|
|
|
|
|
2020-07-17 19:25:08 +03:00
|
|
|
return (bool) $this->user->raw->can($action)->model($model)->whichBelongsTo($context === -1 ? null : $context);
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
else
|
2021-12-10 20:21:25 +03:00
|
|
|
$this->flashFail("err", tr("not_enough_permissions"), tr("not_enough_permissions_comment"));
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function assertCaptchaCheckPassed(): void
|
|
|
|
{
|
2021-01-01 00:18:53 +03:00
|
|
|
if(!check_captcha())
|
2021-12-10 20:21:25 +03:00
|
|
|
$this->flashFail("err", tr("captcha_error"), tr("captcha_error_comment"));
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
|
|
|
|
2021-12-25 19:03:21 +03:00
|
|
|
protected function willExecuteWriteAction(bool $json = false): void
|
2021-01-01 00:18:53 +03:00
|
|
|
{
|
|
|
|
$ip = (new IPs)->get(CONNECTING_IP);
|
|
|
|
$res = $ip->rateLimit();
|
|
|
|
|
|
|
|
if(!($res === IP::RL_RESET || $res === IP::RL_CANEXEC)) {
|
|
|
|
if($res === IP::RL_BANNED && OPENVK_ROOT_CONF["openvk"]["preferences"]["security"]["rateLimits"]["autoban"]) {
|
2021-12-25 23:32:15 +03:00
|
|
|
$this->user->identity->ban("Account has possibly been stolen", false);
|
2021-01-01 00:18:53 +03:00
|
|
|
exit("Хакеры? Интересно...");
|
|
|
|
}
|
|
|
|
|
2021-12-25 19:03:21 +03:00
|
|
|
$this->flashFail("err", tr("rate_limit_error"), tr("rate_limit_error_comment", OPENVK_ROOT_CONF["openvk"]["appearance"]["name"], $res), NULL, $json);
|
2021-01-01 00:18:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
protected function signal(object $event): bool
|
|
|
|
{
|
|
|
|
return (SignalManager::i())->triggerEvent($event, $this->user->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function logEvent(string $type, array $data): bool
|
|
|
|
{
|
|
|
|
$db = eventdb();
|
|
|
|
if(!$db)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
$data = array_merge([
|
|
|
|
"timestamp" => time(),
|
|
|
|
"verified" => (int) true,
|
|
|
|
], $data);
|
|
|
|
$columns = implode(", ", array_map(function($col) {
|
|
|
|
return "`" . addslashes($col) . "`";
|
|
|
|
}, array_keys($data)));
|
|
|
|
$values = implode(", ", array_map(function($val) {
|
|
|
|
return "'" . addslashes((string) (int) $val) . "'";
|
|
|
|
}, array_values($data)));
|
|
|
|
|
|
|
|
$db->getConnection()->query("INSERT INTO " . $type . "s($columns) VALUES ($values);");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
protected function sendmail(string $to, string $template, array $params = []): void
|
|
|
|
{
|
|
|
|
parent::sendmail($to, __DIR__ . "/../../Email/$template", $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTemplatingEngine(): TemplatingEngine
|
|
|
|
{
|
|
|
|
$latte = parent::getTemplatingEngine();
|
|
|
|
$latte->addFilter("translate", function($s) {
|
|
|
|
return tr($s);
|
|
|
|
});
|
|
|
|
|
|
|
|
return $latte;
|
|
|
|
}
|
|
|
|
|
|
|
|
function onStartup(): void
|
|
|
|
{
|
|
|
|
$user = Authenticator::i()->getUser();
|
|
|
|
|
2021-12-01 23:12:05 +03:00
|
|
|
$this->template->isXmas = intval(date('d')) >= 1 && date('m') == 12 || intval(date('d')) <= 15 && date('m') == 1 ? true : false;
|
2020-12-29 20:24:17 +03:00
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
if(!is_null($user)) {
|
|
|
|
$this->user = (object) [];
|
|
|
|
$this->user->raw = $user;
|
|
|
|
$this->user->identity = (new Users)->getByChandlerUser($user);
|
|
|
|
$this->user->id = $this->user->identity->getId();
|
|
|
|
$this->template->thisUser = $this->user->identity;
|
|
|
|
$this->template->userTainted = $user->isTainted();
|
|
|
|
|
|
|
|
if($this->user->identity->isBanned() && !$this->banTolerant) {
|
|
|
|
header("HTTP/1.1 403 Forbidden");
|
|
|
|
$this->getTemplatingEngine()->render(__DIR__ . "/templates/@banned.xml", [
|
|
|
|
"thisUser" => $this->user->identity,
|
2021-12-13 17:37:43 +03:00
|
|
|
"csrfToken" => $GLOBALS["csrfToken"],
|
2020-06-07 19:04:43 +03:00
|
|
|
]);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2021-09-11 01:33:45 +03:00
|
|
|
if ($this->user->identity->onlineStatus() == 0) {
|
|
|
|
$this->user->identity->setOnline(time());
|
|
|
|
$this->user->identity->save();
|
|
|
|
}
|
|
|
|
|
2021-11-27 12:24:34 +03:00
|
|
|
$this->template->ticketAnsweredCount = (new Tickets)->getTicketsCountByuId($this->user->id, 1);
|
2021-12-05 01:56:42 +03:00
|
|
|
if($user->can("write")->model("openvk\Web\Models\Entities\TicketReply")->whichBelongsTo(0))
|
|
|
|
$this->template->helpdeskTicketNotAnsweredCount = (new Tickets)->getTicketCount(0);
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
setlocale(LC_TIME, ...(explode(";", tr("__locale"))));
|
|
|
|
|
|
|
|
parent::onStartup();
|
|
|
|
}
|
|
|
|
|
|
|
|
function onBeforeRender(): void
|
|
|
|
{
|
|
|
|
parent::onBeforeRender();
|
|
|
|
|
2021-12-12 17:08:38 +03:00
|
|
|
$theme = NULL;
|
|
|
|
if(Session::i()->get("_tempTheme")) {
|
2021-12-12 16:11:44 +03:00
|
|
|
$theme = Themepacks::i()[Session::i()->get("_tempTheme", "ovk")];
|
2021-12-12 17:08:38 +03:00
|
|
|
Session::i()->set("_tempTheme", NULL);
|
|
|
|
} else if($this->requestParam("themePreview")) {
|
2021-12-12 16:11:44 +03:00
|
|
|
$theme = Themepacks::i()[$this->requestParam("themePreview")];
|
2021-12-12 17:08:38 +03:00
|
|
|
} else if($this->user->identity !== null && $this->user->identity->getTheme()) {
|
2021-12-12 16:11:44 +03:00
|
|
|
$theme = $this->user->identity->getTheme();
|
2021-12-12 17:08:38 +03:00
|
|
|
}
|
2021-12-12 16:11:44 +03:00
|
|
|
|
2021-12-12 17:08:38 +03:00
|
|
|
$this->template->theme = $theme;
|
2021-12-12 16:11:44 +03:00
|
|
|
if(!is_null($theme) && $theme->overridesTemplates())
|
|
|
|
$this->template->_templatePath = $theme->getBaseDir() . "/tpl";
|
2021-09-16 20:24:03 +03:00
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
if(!is_null(Session::i()->get("_error"))) {
|
|
|
|
$this->template->flashMessage = json_decode(Session::i()->get("_error"));
|
|
|
|
Session::i()->set("_error", NULL);
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 19:03:21 +03:00
|
|
|
|
|
|
|
protected function returnJson(array $json): void
|
|
|
|
{
|
|
|
|
$payload = json_encode($json);
|
|
|
|
$size = strlen($payload);
|
|
|
|
header("Content-Type: application/json");
|
|
|
|
header("Content-Length: $size");
|
|
|
|
exit($payload);
|
|
|
|
}
|
2021-12-28 13:54:20 +03:00
|
|
|
|
|
|
|
function isActivityPubClient(): bool
|
|
|
|
{
|
2021-12-28 14:19:49 +03:00
|
|
|
$accept = explode(";", $_SERVER['HTTP_ACCEPT']);
|
2021-12-28 16:38:02 +03:00
|
|
|
return (bool) preg_match("/(application\/(ld\+json|activity\+json))/", $accept[0]);
|
2021-12-28 13:54:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function getPersonContext()
|
|
|
|
{
|
|
|
|
// Sometimes i being lazy...
|
|
|
|
return json_decode('[
|
|
|
|
"https://www.w3.org/ns/activitystreams",
|
|
|
|
{
|
|
|
|
"sm": "http://smithereen.software/ns#",
|
|
|
|
"wall": {
|
|
|
|
"@id": "sm:wall",
|
|
|
|
"@type": "@id"
|
|
|
|
},
|
|
|
|
"sc": "http://schema.org#",
|
|
|
|
"firstName": "sc:givenName",
|
|
|
|
"lastName": "sc:familyName",
|
|
|
|
"middleName": "sc:additionalName",
|
|
|
|
"gender": {
|
|
|
|
"@id": "sc:gender",
|
|
|
|
"@type": "sc:GenderType"
|
|
|
|
},
|
|
|
|
"supportsFriendRequests": "sm:supportsFriendRequests",
|
|
|
|
"maidenName": "sm:maidenName",
|
|
|
|
"friends": {
|
|
|
|
"@id": "sm:friends",
|
|
|
|
"@type": "@id"
|
|
|
|
},
|
|
|
|
"groups": {
|
|
|
|
"@id": "sm:groups",
|
|
|
|
"@type": "@id"
|
|
|
|
},
|
|
|
|
"vcard": "http://www.w3.org/2006/vcard/ns#"
|
|
|
|
},
|
|
|
|
"https://w3id.org/security/v1"
|
|
|
|
]');
|
|
|
|
// Гришк)
|
|
|
|
}
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|