source: trunk/Modules/Log/Log.php

Last change on this file was 958, checked in by chronos, 16 months ago
  • Fixed: Numeric check for input values.
File size: 4.6 KB
Line 
1<?php
2
3class 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 ),
32 ));
33 ModuleRSS::Cast($this->System->GetModule('RSS'))->RegisterRSS(array('Title' => 'Logs',
34 'Channel' => 'log', 'Callback' => array($this, 'ShowRSS'),
35 'Permission' => array('Module' => 'Log', 'Operation' => 'RSS')));
36 }
37
38 function GetRequestURI(): string
39 {
40 if (array_key_exists('REQUEST_URI', $_SERVER)) return $_SERVER['REQUEST_URI'];
41 else return $_SERVER['PHP_SELF'];
42 }
43
44 function NewRecord(string $Module, string $Operation, string $Value = ''): void
45 {
46 if ($this->System->ModuleManager->ModulePresent('User') and
47 array_key_exists('Id', ModuleUser::Cast($this->System->GetModule('User'))->User->User))
48 $UserId = ModuleUser::Cast($this->System->GetModule('User'))->User->User['Id'];
49 else $UserId = NULL;
50 if (array_key_exists('REMOTE_ADDR', $_SERVER)) $IPAddress = $_SERVER['REMOTE_ADDR'];
51 else $IPAddress = '';
52 $this->Database->insert('Log', array('Time' => 'NOW()',
53 'User' => $UserId, 'Module' => $Module,
54 'Operation' => $Operation, 'Value' => $Value, 'IPAddress' => $IPAddress, 'URL' => $this->GetRequestURI()));
55 }
56
57 function ShowRSS(): string
58 {
59 Header('Content-Type: text/xml');
60 $Count = 100;
61
62 $Items = array();
63 if (array_key_exists('type', $_GET) and is_numeric($_GET['type'])) $Where = ' WHERE `Type` = "'.($_GET['type'] * 1).'"';
64 else $Where = '';
65 $sql = 'SELECT *, UNIX_TIMESTAMP(`Time`) AS `TimeCreate`, (SELECT `User`.`Name` FROM `User` WHERE `User`.`Id` = `Log`.`User`) AS `UserName`, `Time` FROM `Log`'.
66 $Where.' ORDER BY `Time` DESC LIMIT '.$Count;
67 $DbResult = $this->System->Database->query($sql);
68 while ($Line = $DbResult->fetch_assoc())
69 {
70 $Line['Value'] = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $Line['Value']);
71 $Line['Value'] = htmlspecialchars($Line['Value']);
72 $Line['Value'] = str_replace("\n", '<br>', $Line['Value']);
73
74 $Items[] = array
75 (
76 'Title' => $Line['Module'].' '.$Line['Operation'].' ('.$Line['UserName'].', '.$Line['IPAddress'].')',
77 'Link' => 'https://'.Core::Cast($this->System)->Config['Web']['Host'].$this->System->Link('/is/?t=Log&amp;a=list'),
78 'Description' => $Line['Module'].' '.$Line['Operation'].': '.$Line['Value'].' ('.$Line['UserName'].
79 ', '.$Line['IPAddress'].', '.HumanDate($Line['Time']).')',
80 'Time' => $Line['TimeCreate'],
81 );
82 }
83
84 $RSS = new RSS();
85 $RSS->Title = Core::Cast($this->System)->Config['Web']['Title'].' - Záznamy';
86 $RSS->Link = 'https://'.Core::Cast($this->System)->Config['Web']['Host'].'/';
87 $RSS->Description = 'Aktuality '.Core::Cast($this->System)->Config['Web']['Description'];
88 $RSS->WebmasterEmail = Core::Cast($this->System)->Config['Web']['AdminEmail'];
89 $RSS->Items = $Items;
90 return $RSS->Generate();
91 }
92
93 static function Cast(Module $Module): ModuleLog
94 {
95 if ($Module instanceof ModuleLog)
96 {
97 return $Module;
98 }
99 throw new Exception('Expected ModuleLog type but got '.gettype($Module));
100 }
101}
102
103class Log extends Model
104{
105 static function GetModelDesc(): ModelDesc
106 {
107 $Desc = new ModelDesc(self::GetClassName());
108 $Desc->AddDateTime('Time');
109 $Desc->AddReference('User', User::GetClassName());
110 $Desc->AddString('Module');
111 $Desc->AddString('Operation');
112 $Desc->AddText('Value');
113 $Desc->AddString('IPAddress');
114 return $Desc;
115 }
116}
Note: See TracBrowser for help on using the repository browser.