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

Last change on this file since 568 was 568, checked in by chronos, 12 years ago
  • Property svn:executable set to *
File size: 7.6 KB
Line 
1<?php
2
3class ModuleLog extends AppModule
4{
5 var $Excludes;
6
7 function __construct($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');
16
17 $this->Excludes = array();
18 }
19
20 function Start()
21 {
22 $this->System->RegisterPage('log.php', 'PageLog');
23 $this->System->ModuleManager->Modules['Error']->OnError[] = array($this, 'DoAddItem');
24 }
25
26 function DoAddItem($Message)
27 {
28 WriteLog($Message, LOG_TYPE_ERROR);
29 }
30}
31
32// Log types
33define('LOG_TYPE_TRANSLATION', 1);
34define('LOG_TYPE_DOWNLOAD', 2);
35define('LOG_TYPE_USER', 3);
36define('LOG_TYPE_MODERATOR', 4);
37define('LOG_TYPE_ERROR', 10);
38define('LOG_TYPE_IMPORT', 11);
39define('LOG_TYPE_EXPORT', 12);
40define('LOG_TYPE_CZWOW', 13);
41define('LOG_TYPE_ADMINISTRATION', 14);
42
43// TODO: Change global function to module class local method
44function WriteLog($Text, $Type)
45{
46 global $System, $User;
47
48 if(!isset($_SERVER['REMOTE_ADDR'])) $IP = 'Konzole';
49 else $IP = addslashes($_SERVER['REMOTE_ADDR']);
50
51 if(!is_null($User->Id)) $UserId = $User->Id;
52 else $UserId = 'NULL';
53 $Query = 'INSERT INTO `Log` ( `User` , `Type` , `Text` , `Date` , `IP`, `URL` ) '.
54 'VALUES ('.$UserId.', '.$Type.', "'.addslashes($Text).'", NOW(), "'.$IP.'", "'.$_SERVER['REQUEST_URI'].'")';
55 $System->Database->query($Query);
56 echo($Query);
57}
58
59class PageLog extends Page
60{
61 function ShowRSS()
62 {
63 $this->RawPage = true;
64 $Output = '';
65 $Items = array();
66 if(array_key_exists('type', $_GET)) $Where = ' WHERE `Type` = "'.($_GET['type'] * 1).'"';
67 else $Where = '';
68 $sql = 'SELECT *, UNIX_TIMESTAMP(`Date`) AS `TimeCreate`, (SELECT `User`.`Name` FROM `User` WHERE `User`.`ID` = `Log`.`User`) AS `UserName` FROM `Log`'.$Where.' ORDER BY `Date` DESC LIMIT 100';
69 $DbResult = $this->System->Database->query($sql);
70 while($Line = $DbResult->fetch_assoc())
71 {
72 $DbResult2 = $this->System->Database->query('SELECT * FROM `LogType` WHERE `Id`='.$Line['Type']);
73 $LogType = $DbResult2->fetch_assoc();
74
75 if($Line['Type'] == LOG_TYPE_ERROR) $Line['Text'] = htmlspecialchars($Line['Text']);
76 $Line['Text'] = str_replace("\n", '<br>', $Line['Text']);
77
78 $Items[] = array
79 (
80 'Title' => $LogType['Name'].' ('.$Line['UserName'].', '.$Line['IP'].')',
81 'Link' => 'http://'.$this->System->Config['Web']['Host'].$this->System->Link('/log.php'),
82 'Description' => $LogType['Name'].': '.$Line['Text'].' ('.$Line['UserName'].', '.$Line['IP'].')',
83 'Time' => $Line['TimeCreate'],
84 );
85 }
86
87 $Output .= GenerateRSS(array
88 (
89 'Title' => $this->System->Config['Web']['Title'],
90 'Link' => 'http://'.$this->System->Config['Web']['Host'].$this->System->Link('/'),
91 'Description' => $this->System->Config['Web']['Title'],
92 'WebmasterEmail' => $this->System->Config['Web']['AdminEmail'],
93 'Items' => $Items,
94 ));
95 return($Output);
96 }
97
98 function Show()
99 {
100 if(array_key_exists('a', $_POST)) $Action = $_POST['a'];
101 else if(array_key_exists('a', $_GET)) $Action = $_GET['a'];
102 else $Action = '';
103 if($Action == 'rss') $Output = $this->ShowRSS();
104 if($Action == 'delerrlog') $Output = $this->DeleteErrorLog();
105 else $Output = $this->ShowList();
106 return($Output);
107 }
108
109 function ShowList()
110 {
111 global $TranslationTree;
112
113 $Output = '';
114 if(array_key_exists('type', $_GET)) $_SESSION['type'] = $_GET['type'] * 1;
115 else if(!array_key_exists('type', $_SESSION)) $_SESSION['type'] = '';
116
117 if(array_key_exists('group', $_GET)) $_SESSION['group'] = $_GET['group'];
118
119 if($_SESSION['type'] != '') $WhereType = ' `Type`='.$_SESSION['type'];
120 else $WhereType = '1=1';
121
122 $RSSChannels = array(
123 array('Title' => 'Záznamy změn', 'Channel' => 'log&amp;type='.$_SESSION['type'])
124 );
125
126 if($this->System->User->Licence(LICENCE_MODERATOR))
127 {
128 $Output = '<strong>Filtr: </strong>'.
129 '<span style="color:black"><a href="log.php?type=" title="Bez filtrování">Všechny</a></span> ';
130 $DbResult = $this->System->Database->query('SELECT * FROM `LogType`');
131 while($LogType = $DbResult->fetch_assoc())
132 {
133 $Output .= '<a href="log.php?type='.$LogType['Id'].'" style="color:'.$LogType['Color'].'" title="'.$LogType['Name'].'">'.$LogType['Name'].'</a> ';
134 }
135 // echo ' Formát: datum: text zprávy (uživatel, IP)<br /><br />';
136 $Output .= '<br /><br />';
137
138 if(array_key_exists('type', $_SESSION)) $Where = ' WHERE '.$WhereType;
139 else
140 {
141 if(array_key_exists('group', $_SESSION)) $Where = ' WHERE `Text` LIKE "%'.$TranslationTree[$_SESSION['group']]['Name'].'%"';
142 else $Where = '';
143 }
144 //if(($Where != '') and (array_key_exists('group', $_SESSION))) $Where .= ' AND text LIKE "%'.$TranslationTree[$_SESSION['group']]['Name'].'%"';
145
146 $DbResult = $this->System->Database->query('SELECT COUNT(*) FROM `Log` '.$Where);
147 $DbRow = $DbResult->fetch_row();
148 $PageList = GetPageList($DbRow[0]);
149
150 $Output .= $PageList['Output'];
151
152 $TableColumns = array(
153 array('Name' => 'Date', 'Title' => 'Čas'),
154 array('Name' => 'LogName', 'Title' => 'Typ'),
155 array('Name' => 'Text', 'Title' => 'Text'),
156 array('Name' => 'UserName', 'Title' => 'Uživatel'),
157 array('Name' => 'IP', 'Title' => 'Adresa'),
158 array('Name' => 'URL', 'Title' => 'URL'),
159 );
160 $Order = GetOrderTableHeader($TableColumns, 'date', 1);
161 $Output .= '<table width="98%" class="BaseTable">'.
162 $Order['Output'];
163
164 $sql = 'SELECT *, `LogType`.`Color` AS `LogColor`, `LogType`.`Name` AS `LogName`, '.
165 '(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'];
166 $DbResult = $this->System->Database->query($sql);
167 while($Line = $DbResult->fetch_assoc())
168 {
169 if($Line['Type'] == LOG_TYPE_ERROR) $Line['Text'] = htmlspecialchars($Line['Text']);
170 $Line['Text'] = str_replace("\n", '<br>', $Line['Text']);
171 $Output .= '<tr><td>'.$Line['Date'].'</td>'.
172 '<td>'.$Line['LogName'].'</td>'.
173 '<td><span style="color: '.$Line['LogColor'].'">'.$Line['Text'].'</span></td>'.
174 '<td><a href="user.php?user='.$Line['User'].'">'.$Line['UserName'].'</a></td>'.
175 '<td>'.$Line['IP'].'</td>'.
176 '<td>'.$Line['URL'].'</td></tr>';
177 }
178 $Output .= '</table>'.
179 $PageList['Output'];
180 if($this->System->User->Licence(LICENCE_ADMIN))
181 $Output .= '<div><a href="'.$this->System->Link('/log.php?a=delerrlog').'">Vymázání chybových záznamů</a></div>';
182 } else $Output .= ShowMessage('Nemáte oprávnění.', MESSAGE_CRITICAL);
183
184 return($Output);
185 }
186
187 function DeleteErrorLog()
188 {
189 if($this->System->User->Licence(LICENCE_ADMIN))
190 {
191 $DbResult = $this->System->Database->query('SELECT COUNT(*) FROM `Log` WHERE `Type`='.LOG_TYPE_ERROR);
192 $DbRow = $DbResult->fetch_row();
193 $this->System->Database->query('DELETE FROM `Log` WHERE `Type`='.LOG_TYPE_ERROR);
194 WriteLog('Vymazány chybové záznamy', LOG_TYPE_ADMINISTRATION);
195 $Output = ShowMessage('Smazáno všech '.$DbRow[0].' chybových záznamů.');
196 $Output .= $this->ShowList();
197 return($Output);
198 } else $Output .= ShowMessage('Nemáte oprávnění.', MESSAGE_CRITICAL);
199 }
200}
Note: See TracBrowser for help on using the repository browser.