source: trunk/Modules/Log/Log.php@ 888

Last change on this file since 888 was 888, checked in by chronos, 2 years ago
  • Modified: Updated Common package to latest version.
  • Modified: Fixes related to PHP 8.x.
File size: 9.2 KB
Line 
1<?php
2
3class ModuleLog extends Module
4{
5 var $Excludes;
6
7 function __construct(System $System)
8 {
9 parent::__construct($System);
10 $this->Name = 'Log';
11 $this->Version = '1.0';
12 $this->Creator = 'Chronos';
13 $this->License = 'GNU/GPL';
14 $this->Description = 'Log various application events';
15 $this->Dependencies = array('Error', 'News', 'Translation');
16
17 $this->Excludes = array();
18 }
19
20 function DoStart(): void
21 {
22 $this->System->RegisterPage(['log'], 'PageLog');
23 $this->System->ModuleManager->Modules['Error']->OnError[] = array($this, 'DoAddItem');
24 $this->System->ModuleManager->Modules['News']->RegisterRSS(array('Title' => T('Logs'),
25 'Channel' => 'log', 'Callback' => array('PageLog', 'ShowRSS'), 'Permission' => LICENCE_ADMIN));
26 }
27
28 function DoAddItem($Message)
29 {
30 $this->WriteLog($Message, LOG_TYPE_ERROR);
31 }
32
33 function WriteLog($Text, $Type)
34 {
35 if (isset($this->System->User) and !is_null($this->System->User->Id))
36 $UserId = $this->System->User->Id;
37 else $UserId = 'NULL';
38 $Query = 'INSERT INTO `Log` ( `User` , `Type` , `Text` , `Date` , `IP`, `URL` ) '.
39 'VALUES ('.$UserId.', '.$Type.', "'.addslashes($Text).'", NOW(), "'.
40 GetRemoteAddress().'", "'.GetRequestURI().'")';
41 $this->System->Database->query($Query);
42 }
43}
44
45// Log types
46define('LOG_TYPE_TRANSLATION', 1);
47define('LOG_TYPE_DOWNLOAD', 2);
48define('LOG_TYPE_USER', 3);
49define('LOG_TYPE_MODERATOR', 4);
50define('LOG_TYPE_ERROR', 10);
51define('LOG_TYPE_IMPORT', 11);
52define('LOG_TYPE_EXPORT', 12);
53define('LOG_TYPE_CZWOW', 13);
54define('LOG_TYPE_ADMINISTRATION', 14);
55define('LOG_TYPE_PAGE_NOT_FOUND', 15);
56
57// TODO: Change global function to module class local method
58function WriteLog($Text, $Type)
59{
60 global $System, $User;
61
62 if (isset($User) and !is_null($User->Id)) $UserId = $User->Id;
63 else $UserId = 'NULL';
64 $Query = 'INSERT INTO `Log` ( `User` , `Type` , `Text` , `Date` , `IP`, `URL` ) '.
65 'VALUES ('.$UserId.', '.$Type.', "'.addslashes($Text).'", NOW(), "'.
66 GetRemoteAddress().'", "'.GetRequestURI().'")';
67 $System->Database->query($Query);
68}
69
70class PageLog extends Page
71{
72 function ShowRSS()
73 {
74 $this->RawPage = true;
75 $Output = '';
76 $Items = array();
77 if (array_key_exists('type', $_GET)) $Where = ' WHERE `Type` = "'.($_GET['type'] * 1).'"';
78 else $Where = '';
79 $sql = 'SELECT *, UNIX_TIMESTAMP(`Date`) AS `TimeCreate`, (SELECT `User`.`Name` FROM `User` WHERE `User`.`ID` = `Log`.`User`) AS `UserName`, `Date` FROM `Log`'.
80 $Where.' ORDER BY `Date` DESC LIMIT 100';
81 $DbResult = $this->System->Database->query($sql);
82 while ($Line = $DbResult->fetch_assoc())
83 {
84 $DbResult2 = $this->System->Database->query('SELECT * FROM `LogType` WHERE `Id`='.$Line['Type']);
85 $LogType = $DbResult2->fetch_assoc();
86
87 if ($Line['Type'] == LOG_TYPE_ERROR) $Line['Text'] = htmlspecialchars($Line['Text']);
88 $Line['Text'] = str_replace("\n", '<br>', $Line['Text']);
89
90 $Items[] = array
91 (
92 'Title' => $LogType['Name'].' ('.$Line['UserName'].', '.$Line['IP'].')',
93 'Link' => 'http://'.$this->System->Config['Web']['Host'].$this->System->Link('/log/'),
94 'Description' => $LogType['Name'].': '.$Line['Text'].' ('.$Line['UserName'].
95 ', '.$Line['IP'].', '.HumanDate($Line['Date']).')',
96 'Time' => $Line['TimeCreate'],
97 );
98 }
99
100 $Output .= GenerateRSS(array
101 (
102 'Title' => $this->System->Config['Web']['Title'].' - '.T('Logs'),
103 'Link' => 'http://'.$this->System->Config['Web']['Host'].$this->System->Link('/'),
104 'Description' => $this->System->Config['Web']['Description'],
105 'WebmasterEmail' => $this->System->Config['Web']['AdminEmail'],
106 'Items' => $Items,
107 ));
108 return $Output;
109 }
110
111 function Show(): string
112 {
113 if (array_key_exists('a', $_POST)) $Action = $_POST['a'];
114 else if (array_key_exists('a', $_GET)) $Action = $_GET['a'];
115 else $Action = '';
116 if ($Action == 'delerrlog') $Output = $this->DeleteErrorLog();
117 else $Output = $this->ShowList();
118 return $Output;
119 }
120
121 function ShowList()
122 {
123 $TranslationTree = $this->System->ModuleManager->Modules['Translation']->GetTranslationTree();
124
125 $this->Title = T('System log');
126 $Output = '';
127 if (array_key_exists('type', $_GET))
128 {
129 if (is_numeric($_GET['type'])) $_SESSION['type'] = $_GET['type'] * 1;
130 else $_SESSION['type'] = '';
131 } else if (!array_key_exists('type', $_SESSION)) $_SESSION['type'] = '';
132
133 if (array_key_exists('group', $_GET)) $_SESSION['group'] = $_GET['group'];
134
135 if ($_SESSION['type'] != '') $WhereType = ' (`Type`='.$_SESSION['type'].')';
136 else $WhereType = '1=1';
137
138 $RSSChannels = array(
139 array('Title' => 'Záznamy změn', 'Channel' => 'log&amp;type='.$_SESSION['type'])
140 );
141
142 // Show category filter
143 if ($this->System->User->Licence(LICENCE_MODERATOR))
144 {
145 $Output = '<strong>'.T('Filter').':</strong>';
146 $Item = '<a href="'.$this->System->Link('/log/?type=').'" title="Bez filtrování">'.T('All').'</a>';
147 if ($_SESSION['type'] == '') $Item = '<strong>'.$Item.'</strong>';
148 $Output .= ' '.$Item;
149 $DbResult = $this->System->Database->query('SELECT * FROM `LogType`');
150 while ($LogType = $DbResult->fetch_assoc())
151 {
152 $Item = '<a href="'.$this->System->Link('/log/?type='.$LogType['Id']).'" style="color:'.
153 $LogType['Color'].'" title="'.$LogType['Name'].'">'.T($LogType['Name']).'</a>';
154 if ($_SESSION['type'] == $LogType['Id']) $Item = '<strong>'.$Item.'</strong>';
155 $Output .= ' '.$Item;
156 }
157 // echo ' Formát: datum: text zprávy (uživatel, IP)<br /><br />';
158 $Output .= '<br /><br />';
159
160 if (array_key_exists('type', $_SESSION)) $Where = ' WHERE '.$WhereType;
161 else
162 {
163 if (array_key_exists('group', $_SESSION)) $Where = ' WHERE `Text` LIKE "%'.$TranslationTree[$_SESSION['group']]['Name'].'%"';
164 else $Where = '';
165 }
166 //if (($Where != '') and (array_key_exists('group', $_SESSION))) $Where .= ' AND text LIKE "%'.$TranslationTree[$_SESSION['group']]['Name'].'%"';
167
168 $DbResult = $this->System->Database->query('SELECT COUNT(*) FROM `Log` '.$Where);
169 $DbRow = $DbResult->fetch_row();
170 $PageList = GetPageList($DbRow[0]);
171
172 $Output .= $PageList['Output'];
173
174 $TableColumns = array(
175 array('Name' => 'Date', 'Title' => T('Time')),
176 );
177 if ($_SESSION['type'] == '') $TableColumns[] =
178 array('Name' => 'LogName', 'Title' => T('Type'));
179 $TableColumns = array_merge($TableColumns, array(
180 array('Name' => 'Text', 'Title' => T('Content')),
181 array('Name' => 'UserName', 'Title' => T('User')),
182 array('Name' => 'IP', 'Title' => T('Address')),
183 array('Name' => 'URL', 'Title' => T('URL')),
184 ));
185 $Order = GetOrderTableHeader($TableColumns, 'date', 1);
186 $Output .= '<table width="98%" class="BaseTable">'.
187 $Order['Output'];
188
189 $sql = 'SELECT *, `LogType`.`Color` AS `LogColor`, `LogType`.`Name` AS `LogName`, '.
190 '(SELECT `User`.`Name` FROM `User` WHERE `User`.`ID` = `Log`.`User`) AS `UserName` FROM `Log` LEFT JOIN `LogType` ON `LogType`.`Id`=`Log`.`Type` '.$Where.$Order['SQL'].$PageList['SQLLimit'];
191 $DbResult = $this->System->Database->query($sql);
192 while ($Line = $DbResult->fetch_assoc())
193 {
194 if ($Line['Type'] == LOG_TYPE_ERROR) $Line['Text'] = htmlspecialchars($Line['Text']);
195 $Line['Text'] = str_replace("\n", '<br/>', $Line['Text']);
196 $Output .= '<tr><td>'.$Line['Date'].'</td>';
197 if ($_SESSION['type'] == '') $Output .= '<td>'.T($Line['LogName']).'</td>';
198 $Output .= '<td><span style="color: '.$Line['LogColor'].'">'.$Line['Text'].'</span></td>'.
199 '<td><a href="'.$this->System->Link('/user/?user='.$Line['User']).'">'.$Line['UserName'].'</a></td>'.
200 '<td>'.$Line['IP'].'</td>'.
201 '<td>'.$Line['URL'].'</td></tr>';
202 }
203 $Output .= '</table>'.
204 $PageList['Output'];
205 if ($this->System->User->Licence(LICENCE_ADMIN))
206 {
207 $Output .= '<div>'.T('Remove').': <a href="'.$this->System->Link('/log/?a=delerrlog&amp;type='.LOG_TYPE_ERROR).'">'.T('Error logs').'</a> '.
208 '<a href="'.$this->System->Link('/log/?a=delerrlog&amp;type='.LOG_TYPE_PAGE_NOT_FOUND).'">'.T('Missing').'</a></div>';
209 }
210 } else $Output .= ShowMessage(T('Access denied'), MESSAGE_CRITICAL);
211
212 return $Output;
213 }
214
215 function DeleteErrorLog()
216 {
217 if ($this->System->User->Licence(LICENCE_ADMIN) and
218 (($_GET['type'] == LOG_TYPE_ERROR) or ($_GET['type'] == LOG_TYPE_PAGE_NOT_FOUND)))
219 {
220 $DbResult = $this->System->Database->select('LogType', '*', 'Id='.$_GET['type']);
221 $LogType = $DbResult->fetch_assoc();
222 $DbResult = $this->System->Database->query('SELECT COUNT(*) FROM `Log` WHERE `Type`='.$_GET['type']);
223 $DbRow = $DbResult->fetch_row();
224 $this->System->Database->query('DELETE FROM `Log` WHERE `Type`='.$_GET['type']);
225 $this->System->ModuleManager->Modules['Log']->WriteLog('Vymazáno záznamů z '.$LogType['Description'].'.', LOG_TYPE_ADMINISTRATION);
226 $Output = ShowMessage('Smazáno všech '.$DbRow[0].' záznamů z '.$LogType['Description'].'.');
227 $Output .= $this->ShowList();
228 return $Output;
229 } else $Output = ShowMessage(T('Access denied'), MESSAGE_CRITICAL);
230 }
231}
Note: See TracBrowser for help on using the repository browser.