From 0997ee1ca9ccfff8438134e5574dd20cfd54f598 Mon Sep 17 00:00:00 2001 From: n1rwana Date: Tue, 8 Aug 2023 21:14:43 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B1=D0=BE=D1=80=20IP=20=D0=B8=20UserAg?= =?UTF-8?q?ent=20+=20=D1=84=D0=B8=D0=BA=D1=81=20=D0=BB=D0=BE=D0=B3=D0=B8?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F=20=D0=B2=20IPs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Web/Models/Repositories/CurrentUser.php | 24 +++++++++++++++++++++--- Web/Models/Repositories/IPs.php | 2 +- Web/Models/Repositories/Logs.php | 4 +++- Web/Presenters/OpenVKPresenter.php | 2 +- install/{ => sqls}/00038-logs.sql | 4 +++- 5 files changed, 29 insertions(+), 7 deletions(-) rename install/{ => sqls}/00038-logs.sql (79%) diff --git a/Web/Models/Repositories/CurrentUser.php b/Web/Models/Repositories/CurrentUser.php index c71a5ac7..c6cb942b 100644 --- a/Web/Models/Repositories/CurrentUser.php +++ b/Web/Models/Repositories/CurrentUser.php @@ -6,16 +6,24 @@ class CurrentUser { private static $instance = null; private $user; + private $ip; + private $useragent; - public function __construct(?User $user = NULL) + public function __construct(?User $user = NULL, ?string $ip = NULL, ?string $useragent = NULL) { if ($user) $this->user = $user; + + if ($ip) + $this->ip = $ip; + + if ($useragent) + $this->useragent = $useragent; } - public static function get($user) + public static function get($user, $ip, $useragent) { - if (self::$instance === null) self::$instance = new self($user); + if (self::$instance === null) self::$instance = new self($user, $ip, $useragent); return self::$instance; } @@ -24,6 +32,16 @@ class CurrentUser return $this->user; } + public function getIP(): string + { + return $this->ip; + } + + public function getUserAgent(): string + { + return $this->useragent; + } + public static function i() { return self::$instance; diff --git a/Web/Models/Repositories/IPs.php b/Web/Models/Repositories/IPs.php index c4485b73..59fc6570 100644 --- a/Web/Models/Repositories/IPs.php +++ b/Web/Models/Repositories/IPs.php @@ -24,7 +24,7 @@ class IPs if(!$res) { $res = new IP; $res->setIp($ip); - $res->save(); + $res->save(false); return $res; } diff --git a/Web/Models/Repositories/Logs.php b/Web/Models/Repositories/Logs.php index 96ceba0a..6f7d3937 100644 --- a/Web/Models/Repositories/Logs.php +++ b/Web/Models/Repositories/Logs.php @@ -65,7 +65,9 @@ class Logs $log->setXdiff_Old(json_encode($nobject)); $log->setXdiff_New(json_encode($_changes)); $log->setTs(time()); - $log->save(false); + $log->setIp(CurrentUser::i()->getIP()); + $log->setUserAgent(CurrentUser::i()->getUserAgent()); + $log->save(); } } diff --git a/Web/Presenters/OpenVKPresenter.php b/Web/Presenters/OpenVKPresenter.php index 0cf67444..b4444bda 100755 --- a/Web/Presenters/OpenVKPresenter.php +++ b/Web/Presenters/OpenVKPresenter.php @@ -211,7 +211,7 @@ abstract class OpenVKPresenter extends SimplePresenter $this->user->id = $this->user->identity->getId(); $this->template->thisUser = $this->user->identity; $this->template->userTainted = $user->isTainted(); - CurrentUser::get($this->user->identity); + CurrentUser::get($this->user->identity, $_SERVER["REMOTE_ADDR"], $_SERVER["HTTP_USER_AGENT"]); if($this->user->identity->isDeleted() && !$this->deactivationTolerant) { if($this->user->identity->isDeactivated()) { diff --git a/install/00038-logs.sql b/install/sqls/00038-logs.sql similarity index 79% rename from install/00038-logs.sql rename to install/sqls/00038-logs.sql index 3943f629..e136c83f 100644 --- a/install/00038-logs.sql +++ b/install/sqls/00038-logs.sql @@ -8,7 +8,9 @@ CREATE TABLE `logs` `object_id` bigint(20) UNSIGNED NOT NULL, `xdiff_old` longtext COLLATE utf8mb4_unicode_ci NOT NULL, `xdiff_new` longtext COLLATE utf8mb4_unicode_ci NOT NULL, - `ts` bigint(20) NOT NULL + `ts` bigint(20) NOT NULL, + `ip` tinytext NOT NULL, + `useragent` longtext NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; ALTER TABLE `logs`