Name = 'Log'; $this->Version = '1.0'; $this->Creator = 'Chronos'; $this->License = 'GNU/GPLv3'; $this->Description = 'Logging user actions'; $this->Dependencies = array(ModuleUser::GetName(), ModuleRSS::GetName()); $this->Models = array(Log::GetClassName()); } function DoStart(): void { Core::Cast($this->System)->FormManager->RegisterClass('Log', array( 'Title' => 'Záznamy', 'Table' => 'Log', 'DefaultSortColumn' => 'Time', 'DefaultSortOrder' => 1, 'Items' => array( 'Time' => array('Type' => 'DateTime', 'Caption' => 'Čas', 'Default' => '', 'ReadOnly' => true), 'User' => array('Type' => 'TUser', 'Caption' => 'Uživatel', 'Default' => '', 'Null' => true, 'ReadOnly' => true), 'Module' => array('Type' => 'String', 'Caption' => 'Modul', 'Default' => '', 'ReadOnly' => true), 'Operation' => array('Type' => 'String', 'Caption' => 'Operace', 'Default' => '', 'ReadOnly' => true), 'Value' => array('Type' => 'Text', 'Caption' => 'Hodnota', 'Default' => '', 'ReadOnly' => true), 'IPAddress' => array('Type' => 'IPv4Address', 'Caption' => 'Adresa IP', 'Default' => '', 'ReadOnly' => true), ), )); ModuleRSS::Cast($this->System->GetModule('RSS'))->RegisterRSS(array('Title' => 'Logs', 'Channel' => 'log', 'Callback' => array($this, 'ShowRSS'), 'Permission' => array('Module' => 'Log', 'Operation' => 'RSS'))); } function GetRequestURI(): string { if (array_key_exists('REQUEST_URI', $_SERVER)) return $_SERVER['REQUEST_URI']; else return $_SERVER['PHP_SELF']; } function NewRecord(string $Module, string $Operation, string $Value = ''): void { if ($this->System->ModuleManager->ModulePresent('User') and array_key_exists('Id', ModuleUser::Cast($this->System->GetModule('User'))->User->User)) $UserId = ModuleUser::Cast($this->System->GetModule('User'))->User->User['Id']; else $UserId = NULL; if (array_key_exists('REMOTE_ADDR', $_SERVER)) $IPAddress = $_SERVER['REMOTE_ADDR']; else $IPAddress = ''; $this->Database->insert('Log', array('Time' => 'NOW()', 'User' => $UserId, 'Module' => $Module, 'Operation' => $Operation, 'Value' => $Value, 'IPAddress' => $IPAddress, 'URL' => $this->GetRequestURI())); } function ShowRSS(): string { Header('Content-Type: text/xml'); $Count = 100; $Items = array(); if (array_key_exists('type', $_GET) and is_numeric($_GET['type'])) $Where = ' WHERE `Type` = "'.($_GET['type'] * 1).'"'; else $Where = ''; $sql = 'SELECT *, UNIX_TIMESTAMP(`Time`) AS `TimeCreate`, (SELECT `User`.`Name` FROM `User` WHERE `User`.`Id` = `Log`.`User`) AS `UserName`, `Time` FROM `Log`'. $Where.' ORDER BY `Time` DESC LIMIT '.$Count; $DbResult = $this->System->Database->query($sql); while ($Line = $DbResult->fetch_assoc()) { $Line['Value'] = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $Line['Value']); $Line['Value'] = htmlspecialchars($Line['Value']); $Line['Value'] = str_replace("\n", '
', $Line['Value']); $Items[] = array ( 'Title' => $Line['Module'].' '.$Line['Operation'].' ('.$Line['UserName'].', '.$Line['IPAddress'].')', 'Link' => 'https://'.Core::Cast($this->System)->Config['Web']['Host'].$this->System->Link('/is/?t=Log&a=list'), 'Description' => $Line['Module'].' '.$Line['Operation'].': '.$Line['Value'].' ('.$Line['UserName']. ', '.$Line['IPAddress'].', '.HumanDate($Line['Time']).')', 'Time' => $Line['TimeCreate'], ); } $RSS = new RSS(); $RSS->Title = Core::Cast($this->System)->Config['Web']['Title'].' - Záznamy'; $RSS->Link = 'https://'.Core::Cast($this->System)->Config['Web']['Host'].'/'; $RSS->Description = 'Aktuality '.Core::Cast($this->System)->Config['Web']['Description']; $RSS->WebmasterEmail = Core::Cast($this->System)->Config['Web']['AdminEmail']; $RSS->Items = $Items; return $RSS->Generate(); } static function Cast(Module $Module): ModuleLog { if ($Module instanceof ModuleLog) { return $Module; } throw new Exception('Expected ModuleLog type but got '.gettype($Module)); } } class Log extends Model { static function GetModelDesc(): ModelDesc { $Desc = new ModelDesc(self::GetClassName()); $Desc->AddDateTime('Time'); $Desc->AddReference('User', User::GetClassName()); $Desc->AddString('Module'); $Desc->AddString('Operation'); $Desc->AddText('Value'); $Desc->AddString('IPAddress'); return $Desc; } }