5f1513ae2459bb7def4ebdf7c267cbd2ca72e5c2
1 <?php
3 class gosa_logview extends plugin
4 {
5 /* Definitions */
6 var $plHeadline= "System logs";
7 var $plDescription= "This does something";
9 /* attribute list for save action */
10 var $attributes = array();
11 var $objectclasses = array();
13 var $server = "";
14 var $action = "view";
15 var $time = 2;
16 var $regex = "*";
18 var $start = 0;
19 var $sort = 1;
21 var $sort_direction = "down";
22 var $fields = array("action","timestamp","user","objecttype","object","result","repeated");
23 var $range = 25;
24 var $view_logged = FALSE;
26 var $timezone_offset = 0;
28 function gosa_logview (&$config, $dn= NULL)
29 {
30 /* Include config object */
31 $this->config= &$config;
32 $this->ui = get_userinfo();
34 /* Restore last filter settings */
35 foreach(array("action","server","time","regex") as $attr){
36 if(isset($_SESSION['logview']['gosa_log'][$attr])){
37 $this->$attr = $_SESSION['logview']['gosa_log'][$attr];
38 }
39 }
41 $tz = get_default_timezone();
42 $this->timezone_offset = $tz['value'] * 60 * 60 ;
43 }
46 function execute()
47 {
48 /* Call parent execute */
49 plugin::execute();
51 /* Log view */
52 if(!$this->view_logged){
53 $this->view_logged = TRUE;
54 new log("view","logview/".get_class($this),$this->dn);
55 }
57 /* Time interval */
58 $date_select = array(
59 "0" => _("one hour"), "1" => _("6 hours"),
60 "2" => _("12 hours"), "3" => _("24 hours"),
61 "4" => _("2 days"), "5" => _("one week"),
62 "6" => _("2 weeks"), "7" => _("one month"));
65 $time = time();
66 $date_select_ = array(
67 "0" => $time - 3600,
68 "1" => $time - 21600,
69 "2" => $time - 43200,
70 "3" => $time - 86400,
71 "4" => $time - 172800,
72 "5" => $time - 604800,
73 "6" => $time - 1209600,
74 "7" => $time - 2419200);
76 $smarty= get_smarty();
77 $smarty->assign("search_result", "");
78 $smarty->assign("plug", "?plug=".validate($_GET['plug']));
79 $smarty->assign("search_image", get_template_path('images/search.png'));
80 $smarty->assign("time_image", get_template_path('images/time.png'));
81 $smarty->assign("server_image", get_template_path('images/server.png'));
82 $smarty->assign("log_image", get_template_path('images/log_warning.png'));
83 $smarty->assign("ruleset_image", get_template_path('images/edit.png'));
84 $smarty->assign("launchimage", get_template_path('images/launch.png'));
85 $smarty->assign("date_select", $date_select);
86 $smarty->assign("actions", array());
87 $smarty->assign("direction", "up");
88 $smarty->assign("mode0", "");
89 $smarty->assign("mode1", "");
90 $smarty->assign("mode2", "");
91 $smarty->assign("mode3", "");
92 $smarty->assign("mode4", "");
93 $smarty->assign("mode5", "");
94 $smarty->assign("mode6", "");
96 /* Assign select option content */
97 foreach( array("server", "action", "time", "regex") as $type){
98 $smarty->assign("$type", $this->$type);
99 }
101 /****
102 * Check if defined servers
103 * and mysql extension
104 ****/
105 if (!isset($this->config->data['SERVERS']['LOGGING']) || !count($this->config->data['SERVERS']['LOGGING'])){
106 print_red (_("No GOsa LOG servers defined!"));
107 return ($smarty->fetch (get_template_path('gosa_log_contents.tpl', TRUE)));
108 }
110 if(!is_callable("mysql_connect")){
111 print_red(_("There is no mysql extension available, please check your php setup."));
112 return ($smarty->fetch (get_template_path('gosa_log_contents.tpl', TRUE)));
113 }
115 /****
116 * Get Configurations
117 ****/
118 $list_of_servers = $this->config->data['SERVERS']['LOGGING'];
119 $servers = array();
120 foreach($list_of_servers as $servername => $data){
121 $servers[$servername] = $servername;
122 }
123 $smarty->assign("servers", $servers);
125 /* Set a default server, if there is currently no valid server selected */
126 if(empty($this->server) || !in_array($this->server,$servers)){
127 $this->server = key($servers);
128 }
130 /****
131 * Connect to currently selected server
132 ****/
133 $cfg = $list_of_servers[$this->server];
134 $link = mysql_pconnect($this->server, $cfg['USER'], $cfg['PWD']);
135 if ($link === FALSE){
136 print_red(_("Can't connect to log database, no logs can be shown!"));
137 new log("debug","logview","",array(),@mysql_error());
138 return ($smarty->fetch (get_template_path('gosa_log_contents.tpl', TRUE)));
139 }
141 /* check of log database is available */
142 if (! @mysql_select_db($cfg['DB'])){
143 print_red(_("Can't select log database for log generation!"));
144 new log("debug","logview","",array(),@mysql_error());
145 return ($smarty->fetch (get_template_path('gosa_log_contents.tpl', TRUE)));
146 }
149 /****
150 * Get all action types
151 ****/
153 $actions = array("!ALL" => _("All"));
154 $query = "SELECT distinct(action) FROM gosa_log;";
155 $res = @mysql_query($query);
156 while($attrs = @mysql_fetch_assoc($res)){
157 $actions[$attrs['action']] = $attrs['action'];
158 }
161 /****
162 * Check Acls
163 ****/
165 $ui = get_userinfo();
166 $dn = $list_of_servers[$this->server]['DN'];
167 $acls = $ui->get_permissions($dn,"server/gosaLogServer","viewEntries");
169 if(!preg_match("/r/",$acls)){
170 $res = "<tr>
171 <td colspan=".count($this->fields).">
172 "._("You have insufficient permissions to view syslog entries.")."
173 </td>
174 </tr>";
176 /* Show main page */
177 $smarty->assign("range_selector", "");
178 $smarty->assign("search_result", $res);
179 return ($smarty->fetch (get_template_path('gosa_log_contents.tpl', TRUE)));
180 }
183 /****
184 * Query stuff
185 ****/
186 $res= "";
187 $cfg = $list_of_servers[$this->server];
188 $link = @mysql_pconnect($this->server, $cfg['USER'], $cfg['PWD']);
190 /* Test connection object && create up query string */
191 if ($link === FALSE){
192 print_red(_("Can't connect to log database, no logs can be shown!"));
193 new log("debug","logview","",array(),@mysql_error());
194 } else {
196 if (! @mysql_select_db($cfg['DB'])){
197 print_red(_("Can't select log database for log generation!"));
198 new log("debug","logview","",array(),@mysql_error());
199 } else {
203 $start = $date_select_[$this->time];
205 /* Order setting */
206 if ($this->sort_direction == "down"){
207 $desc= "DESC";
208 $sort_sign = "\\/";
209 } else {
210 $desc= "";
211 $sort_sign="/\\";
212 }
213 $end= date ("YmdHis",time());
214 $query_base= " FROM
215 gosa_log
216 WHERE
217 result like '".preg_replace("/\*/","%",$this->regex)."' ";
220 if($this->action != "!ALL"){
221 $query_base .="
222 AND
223 action like '".$this->action."'
224 ";
225 }
227 $query_base .="
228 AND
229 timestamp <= $end AND timestamp >= $start";
231 /* Get number of entries */
232 $query= "SELECT COUNT(*)".$query_base.";";
233 $result = mysql_query($query);
234 if(!$result){
235 new log("debug","logview","",array(),@mysql_error());
236 }
237 $line= mysql_fetch_array($result, MYSQL_ASSOC);
238 $count= $line['COUNT(*)'];
239 if ($count > 25){
240 $smarty->assign("range_selector", range_selector($count, $this->start, $this->range,"EntriesPerPage"));
241 } else {
242 $smarty->assign("range_selector", "");
243 }
245 /* Query results */
246 $query= "SELECT count(`action`) as 'repeated',`user`,`result`,`timestamp`,`action`,`action`,`objecttype`,`object`".$query_base." GROUP BY `action`,`result`,`timestamp` ORDER BY ".$this->fields[$this->sort]." $desc LIMIT ".$this->start.",".$this->range.";";
247 @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__, $query, "Database query");
248 $result = @mysql_query($query);
250 if(!$result){
251 new log("debug","logview","",array(),@mysql_error());
252 }
253 /* Display results */
254 $mod= 0;
256 /* Add entries to result str */
257 while ($line = mysql_fetch_array($result, MYSQL_ASSOC)){
259 /* Toggle background color */
260 if ( ($mod++) & 1){
261 $col= "background-color: #ECECEC;";
262 } else {
263 $col= "background-color: #F5F5F5;";
264 }
266 $base = ", ".substr($this->config->current['BASE'],0,5)."...";
267 $user = preg_replace("/,".normalizePreg($this->config->current['BASE'])."/",$base,$line['user']);
268 $object = preg_replace("/,".normalizePreg($this->config->current['BASE'])."/",$base,$line['object']);
270 $res.=" <tr style=\"$col\">\n";
271 $res.=" <td title='".$line['objecttype']."'>".$line['action']."</td>";
272 $res.=" <td>".date("H:i:s d.m.Y",($line['timestamp'] + $this->timezone_offset))."</td>";
273 $res.=" <td title='".$line['user']."'>".preg_replace("/,/",", ",$user)."</td>";
274 $res.=" <td title='".$line['objecttype']."'>".$line['objecttype']."</td>";
275 $res.=" <td title='".$line['object']."'>".preg_replace("/,/",", ",$object)."</td>";
276 $res.=" <td>".$line['result']."</td>";
277 if($line['repeated'] == 1){
278 $res.=" <td>-</td>";
279 }else{
280 $res.=" <td>".($line['repeated']-1)."</td>";
281 }
282 $res.=" </tr>\n";
283 }
284 mysql_close($link);
285 $smarty->assign("mode".$this->sort, $sort_sign);
286 $smarty->assign("search_result", $res);
287 }
288 }
290 $smarty->assign("regex", $this->regex);
291 $smarty->assign("server",$this->server);
292 $smarty->assign("servers",$servers);
293 $smarty->assign("action",$this->action);
294 $smarty->assign("actions",$actions);
295 $smarty->assign("date_select", $date_select);
297 $smarty->assign("direction", $this->sort_direction);
299 /* Show main page */
300 return ($smarty->fetch (get_template_path('gosa_log_contents.tpl', TRUE)));
301 }
304 function save_object()
305 {
306 /* Get submitted range */
307 if(isset($_POST['EntriesPerPage'])){
308 if(is_numeric($_POST['EntriesPerPage'])){
309 $this->range = $_POST['EntriesPerPage'];
310 }
311 }
313 /* Get actual select boxe values */
314 $logfilter_changed = FALSE;
315 foreach( array("server", "action","time","regex") as $type){
316 if(isset($_POST[$type])){
317 $this->$type = $_POST[$type];
318 }
319 }
321 /* Filter regex values */
322 if ($this->regex == ""){
323 $this->regex= '*';
324 } else {
325 $new = preg_replace('/\*\**/', '*', $this->regex);
326 $this->regex= $new;
327 }
329 /* Set start value */
330 if (isset($_GET['start'])){
331 $this->start= (int)$_GET['start'];
332 }
334 /* Reset page number if filter has changed */
335 if ($logfilter_changed > 0){
336 $this->start= 0;
337 }
339 /* Adapt sorting */
340 if (isset($_GET['sort'])){
342 if($_GET['direction'] == "up"){
343 $this->sort_direction = "down";
344 }else{
345 $this->sort_direction = "up";
346 }
348 $this->sort= (int)$_GET['sort'];
349 if ($this->sort < 0 || $this->sort > count($this->fields)){
350 $this->sort= 0;
351 }
352 }
354 /* Save attributes in Sessiob, so we are able to recontruct filter on plugin reload */
355 $_SESSION['logview']['gosa_log'] = array();
356 foreach(array("action","server","time","regex") as $attr){
357 $_SESSION['logview']['gosa_log'][$attr] = $this->$attr;
358 }
361 }
364 /* Return plugin informations for acl handling
365 #FIXME You can only read attributes within this report plugin
366 static function plInfo()
367 {
368 return (array(
369 "plShortName" => _("Log view"),
370 "plDescription" => _("Log view addon"),
371 "plSelfModify" => FALSE,
372 "plDepends" => array(),
373 "plPriority" => 0,
374 "plSection" => array("addon"),
375 "plCategory" => array("logview" => array("objectClass" => "none", "description" => _("System logs"))),
377 "plProvidedAcls" => array()
378 ));
379 }
380 */
381 }
382 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
383 ?>