| 1 | <?php
|
|---|
| 2 |
|
|---|
| 3 | class ModuleLog extends Module
|
|---|
| 4 | {
|
|---|
| 5 | function __construct(System $System)
|
|---|
| 6 | {
|
|---|
| 7 | parent::__construct($System);
|
|---|
| 8 | $this->Name = 'Log';
|
|---|
| 9 | $this->Version = '1.0';
|
|---|
| 10 | $this->Creator = 'Chronos';
|
|---|
| 11 | $this->License = 'GNU/GPLv3';
|
|---|
| 12 | $this->Description = 'Logging user actions';
|
|---|
| 13 | $this->Dependencies = array(ModuleUser::GetName(), ModuleRSS::GetName());
|
|---|
| 14 | $this->Models = array(Log::GetClassName());
|
|---|
| 15 | }
|
|---|
| 16 |
|
|---|
| 17 | function DoStart(): void
|
|---|
| 18 | {
|
|---|
| 19 | Core::Cast($this->System)->FormManager->RegisterClass('Log', array(
|
|---|
| 20 | 'Title' => 'Záznamy',
|
|---|
| 21 | 'Table' => 'Log',
|
|---|
| 22 | 'DefaultSortColumn' => 'Time',
|
|---|
| 23 | 'DefaultSortOrder' => 1,
|
|---|
| 24 | 'Items' => array(
|
|---|
| 25 | 'Time' => array('Type' => 'DateTime', 'Caption' => 'Čas', 'Default' => '', 'ReadOnly' => true),
|
|---|
| 26 | 'User' => array('Type' => 'TUser', 'Caption' => 'Uživatel', 'Default' => '', 'Null' => true, 'ReadOnly' => true),
|
|---|
| 27 | 'Module' => array('Type' => 'String', 'Caption' => 'Modul', 'Default' => '', 'ReadOnly' => true),
|
|---|
| 28 | 'Operation' => array('Type' => 'String', 'Caption' => 'Operace', 'Default' => '', 'ReadOnly' => true),
|
|---|
| 29 | 'Value' => array('Type' => 'Text', 'Caption' => 'Hodnota', 'Default' => '', 'ReadOnly' => true),
|
|---|
| 30 | 'IPAddress' => array('Type' => 'IPv4Address', 'Caption' => 'Adresa IP', 'Default' => '', 'ReadOnly' => true),
|
|---|
| 31 | 'URL' => array('Type' => 'String', 'Caption' => 'URL', 'Default' => '', 'ReadOnly' => true),
|
|---|
| 32 | ),
|
|---|
| 33 | ));
|
|---|
| 34 | ModuleRSS::Cast($this->System->GetModule('RSS'))->RegisterRSS(array('Title' => 'Logs',
|
|---|
| 35 | 'Channel' => 'log', 'Callback' => array($this, 'ShowRSS'),
|
|---|
| 36 | 'Permission' => array('Module' => 'Log', 'Operation' => 'RSS')));
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | function GetRequestURI(): string
|
|---|
| 40 | {
|
|---|
| 41 | if (array_key_exists('REQUEST_URI', $_SERVER)) return $_SERVER['REQUEST_URI'];
|
|---|
| 42 | else return $_SERVER['PHP_SELF'];
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | function NewRecord(string $Module, string $Operation, string $Value = ''): void
|
|---|
| 46 | {
|
|---|
| 47 | if ($this->System->ModuleManager->ModulePresent('User') and
|
|---|
| 48 | array_key_exists('Id', ModuleUser::Cast($this->System->GetModule('User'))->User->User))
|
|---|
| 49 | $UserId = ModuleUser::Cast($this->System->GetModule('User'))->User->User['Id'];
|
|---|
| 50 | else $UserId = NULL;
|
|---|
| 51 | if (array_key_exists('REMOTE_ADDR', $_SERVER)) $IPAddress = $_SERVER['REMOTE_ADDR'];
|
|---|
| 52 | else $IPAddress = '';
|
|---|
| 53 | $this->Database->insert('Log', array('Time' => 'NOW()',
|
|---|
| 54 | 'User' => $UserId, 'Module' => $Module,
|
|---|
| 55 | 'Operation' => $Operation, 'Value' => $Value, 'IPAddress' => $IPAddress, 'URL' => $this->GetRequestURI()));
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | function ShowRSS(): string
|
|---|
| 59 | {
|
|---|
| 60 | Header('Content-Type: text/xml');
|
|---|
| 61 | $Count = 100;
|
|---|
| 62 |
|
|---|
| 63 | $Items = array();
|
|---|
| 64 | if (array_key_exists('type', $_GET) and is_numeric($_GET['type'])) $Where = ' WHERE `Type` = "'.($_GET['type'] * 1).'"';
|
|---|
| 65 | else $Where = '';
|
|---|
| 66 | $sql = 'SELECT *, UNIX_TIMESTAMP(`Time`) AS `TimeCreate`, (SELECT `User`.`Name` FROM `User` WHERE `User`.`Id` = `Log`.`User`) AS `UserName`, `Time` FROM `Log`'.
|
|---|
| 67 | $Where.' ORDER BY `Time` DESC LIMIT '.$Count;
|
|---|
| 68 | $DbResult = $this->System->Database->query($sql);
|
|---|
| 69 | while ($Line = $DbResult->fetch_assoc())
|
|---|
| 70 | {
|
|---|
| 71 | $Line['Value'] = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $Line['Value']);
|
|---|
| 72 | $Line['Value'] = htmlspecialchars($Line['Value']);
|
|---|
| 73 | $Line['Value'] = str_replace("\n", '<br>', $Line['Value']);
|
|---|
| 74 |
|
|---|
| 75 | $Items[] = array
|
|---|
| 76 | (
|
|---|
| 77 | 'Title' => $Line['Module'].' '.$Line['Operation'].' ('.$Line['UserName'].', '.$Line['IPAddress'].')',
|
|---|
| 78 | 'Link' => 'https://'.Core::Cast($this->System)->Config['Web']['Host'].$this->System->Link('/is/?t=Log&a=list'),
|
|---|
| 79 | 'Description' => $Line['Module'].' '.$Line['Operation'].': '.$Line['Value'].' ('.$Line['UserName'].
|
|---|
| 80 | ', '.$Line['IPAddress'].', '.HumanDate($Line['Time']).')',
|
|---|
| 81 | 'Time' => $Line['TimeCreate'],
|
|---|
| 82 | );
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | $RSS = new RSS();
|
|---|
| 86 | $RSS->Title = Core::Cast($this->System)->Config['Web']['Title'].' - Záznamy';
|
|---|
| 87 | $RSS->Link = 'https://'.Core::Cast($this->System)->Config['Web']['Host'].'/';
|
|---|
| 88 | $RSS->Description = 'Aktuality '.Core::Cast($this->System)->Config['Web']['Description'];
|
|---|
| 89 | $RSS->WebmasterEmail = Core::Cast($this->System)->Config['Web']['AdminEmail'];
|
|---|
| 90 | $RSS->Items = $Items;
|
|---|
| 91 | return $RSS->Generate();
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | static function Cast(Module $Module): ModuleLog
|
|---|
| 95 | {
|
|---|
| 96 | if ($Module instanceof ModuleLog)
|
|---|
| 97 | {
|
|---|
| 98 | return $Module;
|
|---|
| 99 | }
|
|---|
| 100 | throw new Exception('Expected ModuleLog type but got '.gettype($Module));
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | class Log extends Model
|
|---|
| 105 | {
|
|---|
| 106 | static function GetModelDesc(): ModelDesc
|
|---|
| 107 | {
|
|---|
| 108 | $Desc = new ModelDesc(self::GetClassName());
|
|---|
| 109 | $Desc->AddDateTime('Time');
|
|---|
| 110 | $Desc->AddReference('User', User::GetClassName());
|
|---|
| 111 | $Desc->AddString('Module');
|
|---|
| 112 | $Desc->AddString('Operation');
|
|---|
| 113 | $Desc->AddText('Value');
|
|---|
| 114 | $Desc->AddString('IPAddress');
|
|---|
| 115 | return $Desc;
|
|---|
| 116 | }
|
|---|
| 117 | }
|
|---|