Code

0c6b0e515ad5d0bd767468c3938d4b8de4431594
[gosa.git] / trunk / gosa-plugins / log / addons / logview / class_gosa_logview.inc
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         = "modify";
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;
25   var $location         = "";
26   var $location_id      = -1;
27   var $timezone_offset  = 0;
29   var $uid_map = array();
30   
32   function gosa_logview (&$config, $dn= NULL)
33   {
34     /* Include config object */
35     $this->config= &$config;
36     $this->ui = get_userinfo();
37     $this->location = $this->config->current['BASE'];
39     /* Restore last filter settings */
40     $logview = session::get('logview');
41     foreach(array("action","server","time","regex") as $attr){
42       if(isset($logview['gosa_log'][$attr])){
43         $this->$attr = $logview['gosa_log'][$attr];
44       }
45     }
47     $tz = timezone::get_default_timezone();
48     $this->timezone_offset = $tz['value'] * 60 * 60 ;
50     $ldap = $this->config->get_ldap_link();
51     $ldap->search("(&(objectClass=person)(uid=*))",array("uid","dn"));
52     while($attrs = $ldap->fetch()){
53       $this->uid_map[$attrs['dn']] = $attrs['uid'][0];
54     }
55   }
58   function execute()
59   {
60     /* Call parent execute */
61     plugin::execute();
63     /* Log view */
64     if(!$this->view_logged){
65       $this->view_logged = TRUE;
66       new log("view","gosa_logging/".get_class($this),$this->dn);
67     }
69     /* Time interval */
70     $date_select = array(
71         "0" => _("one hour"), "1" => _("6 hours"),
72         "2" => _("12 hours"), "3" => _("24 hours"),
73         "4" => _("2 days"),   "5" => _("one week"),
74         "6" => _("2 weeks"),  "7" => _("one month"));
77     $time = time();
78     $date_select_ = array(
79         "0" => $time - 3600, 
80         "1" => $time - 21600,
81         "2" => $time - 43200,
82         "3" => $time - 86400,
83         "4" => $time - 172800,
84         "5" => $time - 604800,
85         "6" => $time - 1209600,
86         "7" => $time - 2419200);
88     $smarty= get_smarty();
89     $smarty->assign("search_result", "");
90     $smarty->assign("plug", "?plug=".validate($_GET['plug']));
91     $smarty->assign("search_image", get_template_path('images/lists/search.png'));
92     $smarty->assign("time_image", get_template_path('plugins/log/images/time.png'));
93     $smarty->assign("server_image", get_template_path('plugins/systems/images/server.png'));
94     $smarty->assign("log_image", get_template_path('plugins/logview/images/log_warning.png'));
95     $smarty->assign("ruleset_image", get_template_path('images/lists/edit.png'));
96     $smarty->assign("launchimage", get_template_path('images/launch.png'));
97     $smarty->assign("date_select", $date_select);
98     $smarty->assign("actions", array());
99     $smarty->assign("direction", "up");
100     $smarty->assign("mode0", "");
101     $smarty->assign("mode1", "");
102     $smarty->assign("mode2", "");
103     $smarty->assign("mode3", "");
104     $smarty->assign("mode4", "");
105     $smarty->assign("mode5", "");
106     $smarty->assign("mode6", "");
108     /* Assign select option content */
109     foreach( array("server", "action", "time", "regex") as $type){
110       $smarty->assign("$type", $this->$type);
111     }
113   /**** 
114    * Check if defined servers 
115    *  and mysql extension 
116    ****/
117   if (!isset($this->config->data['SERVERS']['LOGGING']) || !count($this->config->data['SERVERS']['LOGGING'])){
118     msg_dialog::display(_("Warning"), msgPool::noserver("GOsa log"), WARNING_DIALOG);
119     $smarty->assign("servers", array());
120     return ($smarty->fetch (get_template_path('gosa_log_contents.tpl', TRUE)));
121   }
122   
123   if(!is_callable("mysql_connect")){
124     msg_dialog::display(_("Configuration error"), sprintf(_("Missing %s PHP extension!"), "mysql"), WARNING_DIALOG);
125     $smarty->assign("servers", array());
126     return ($smarty->fetch (get_template_path('gosa_log_contents.tpl', TRUE)));
127   }
129     /****
130      * Get Configurations 
131      ****/
132     $list_of_servers = $this->config->data['SERVERS']['LOGGING']; 
133     $servers         = array();
134     foreach($list_of_servers as $servername => $data){
135       $servers[$servername] = $servername;
136     }
137     $smarty->assign("servers", $servers);
138     
139     /* Set a default server, if there is currently no valid server selected */
140     if(empty($this->server) || !in_array($this->server,$servers)){
141       $this->server = key($servers);
142     }
143    
144     /**** 
145      * Connect to currently selected server 
146      ****/
147     $cfg = $list_of_servers[$this->server];
148   
149     $link = @mysql_pconnect($this->server, $cfg['USER'], $cfg['PWD']);
150     if ($link === FALSE){
151       msg_dialog::display(_("Error"), msgPool::dbconnect($this->server,@mysql_error(),"GOsa LOG"), ERROR_DIALOG);
152       new log("debug","gosa_logging","dbconnect",array(),@mysql_error());
153       return ($smarty->fetch (get_template_path('gosa_log_contents.tpl', TRUE)));
154     }
156     /* check if log database is available */
157     if (! @mysql_select_db($cfg['DB'])){
158       msg_dialog::display(_("Error"), msgPool::dbselect($cfg['DB'],@mysql_error(), "GOsa LOG"), ERROR_DIALOG);
159       new log("debug","gosa_logging","selectdb",array(),@mysql_error());
160       return ($smarty->fetch (get_template_path('gosa_log_contents.tpl', TRUE)));
161     }
164     /****
165      * Get location_id.
166      ****/
168     if($this->location_id == -1){
169       $actions = array("!ALL" => _("All"));
170       $query = "SELECT id FROM gosa_locations WHERE location='".mysql_escape_string($this->location)."';";
171       $res = @mysql_query($query);
172       if($res){
173         $attrs = @mysql_fetch_assoc($res);
174         if(isset($attrs['id'])){
175           $this->location_id = $attrs['id'];
176         }
177       } 
178     }
179    
180     /****
181      * Get all action types 
182      ****/
184     $actions = array("!ALL" => _("All"));
185     $query = "SELECT distinct(action) FROM gosa_log WHERE location_id='".$this->location_id."'; ";
186     $res = @mysql_query($query);
187     while($attrs = @mysql_fetch_assoc($res)){
188       $actions[$attrs['action']] = $attrs['action'];
189     } 
192     /****
193      * Check Acls 
194      ****/
196     $ui = get_userinfo();
197     $dn = $list_of_servers[$this->server]['DN'];
198     $acls = $ui->get_permissions($dn,"server/gosaLogServer","viewEntries");
199     
200     if(!preg_match("/r/",$acls)){
201       $res = "<tr>
202         <td colspan=".count($this->fields).">
203         "._("You have insufficient permissions to view syslog entries.")."
204         </td>
205         </tr>";
207       /* Show main page */
208       $smarty->assign("range_selector", "");
209       $smarty->assign("search_result", $res);
210       return ($smarty->fetch (get_template_path('gosa_log_contents.tpl', TRUE)));
211     }
213     /* Prepare order setting */
214     if ($this->sort_direction == "down"){
215       $desc= "DESC";
216       $sort_sign = "<img src='images/lists/sort-down.png' alt='\/' class='center' title='down' border=0>";
217     } else {
218       $desc= "";
219       $sort_sign = "<img src='images/lists/sort-up.png' alt='/\' class='center' title='up' border=0>";
220     }
222     /****
223      * Query stuff 
224      ****/
226     /* Check for required tables
227      */
228     $query = "SHOW TABLES;";
229     $res    = @mysql_query($query,$link);
230     $tables = array();
231     while($attrs  = @mysql_fetch_row($res)){
232       $tables[] = $attrs[0];
233     }
234     $error = FALSE;
235     foreach(array("gosa_log","gosa_locations") as $required){
236       if(!in_array($required,$tables)){
237         msg_dialog::display(_("Error"),
238             sprintf(_("Missing logging table (%s.%s) update your GOsa logging database schema."),
239               $cfg['DB'],$required), ERROR_DIALOG);
240         $error = TRUE;
241       }
242     }
243     if(!$error){
245       /* Get start time */
246       $start = $date_select_[$this->time];
248       /* Prepare search filter */
249       $sql_regex =trim(preg_replace("/\*/","%",$this->regex));
250       $sql_regex = "%".trim($sql_regex,"%")."%";
252       /* Create search filter */
253       $query_base= "FROM  gosa_log WHERE timestamp >= $start ";
255       /* Append action filter */
256       if($this->action != "!ALL"){
257         $query_base .=" AND action like '".$this->action."' ";
258       }
260       /* Append search filter */
261       if($sql_regex != "%%"){
262         $query_base .=" AND ( result like '".$sql_regex."' OR user like '".$sql_regex."') ";
263       }
265       /* Appen location */
266       $query_base .= " AND location_id='".$this->location_id."' ";
268       /* Get number of entries */
269       $query= "SELECT COUNT(`user`) ".$query_base.";";
270       $result = mysql_query($query);
271       if(!$result){
272         new log("debug","gosa_logging","",array(),@mysql_error());
273       }
274       $line= mysql_fetch_array($result, MYSQL_ASSOC);
275       $count= $line['COUNT(`user`)'];
276       if ($count > 25){
277         $smarty->assign("range_selector", range_selector($count, $this->start, $this->range,"EntriesPerPage"));
278       } else {
279         $smarty->assign("range_selector", "");
280       }
282       /* Query results that will be displayed */
283       $query= "SELECT * ".$query_base." ORDER BY ".$this->fields[$this->sort]." ".$desc." LIMIT  ".$this->start.",".$this->range.";";
284       @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__, $query, "Database query");
285       $result = @mysql_query($query);
287       if(!$result){
288         msg_dialog::display(_("Error"), msgPool::dbquery("gosa_log",@mysql_error(), "GOsa LOG"), ERROR_DIALOG);
289         new log("debug","gosa_logging","querydb",array(),@mysql_error());
290         return ($smarty->fetch (get_template_path('gosa_log_contents.tpl', TRUE)));
291       }
293       /* Display results */
294       $mod= 0;
296       /* Add entries to result str */
297       $res = "";
298       while ($line = mysql_fetch_array($result, MYSQL_ASSOC)){
300         /* Toggle background color */
301         if ( ($mod++) & 1){
302           $col= "background-color: #ECECEC;";
303         } else {
304           $col= "background-color: #F5F5F5;";
305         }
307         $base = ", ".substr($this->config->current['BASE'],0,5)."...";
309         if(isset($this->uid_map[$line['user']])){
310           $user = $this->uid_map[$line['user']];
311         }else{
312           $user = preg_replace("/,".preg_quote($this->config->current['BASE'], '/')."/",$base,$line['user']);
313         }
314         $object = preg_replace("/,".preg_quote($this->config->current['BASE'], '/')."/",$base,$line['object']);
316         $res.=" <tr style=\"$col\">\n";
317         $res.="   <td title='".$line['objecttype']."'>".$line['action']."</td>";
318         $res.="   <td>".date("H:i:s d.m.Y",($line['timestamp'] + $this->timezone_offset))."</td>";
319         $res.="   <td title='".$line['user']."'>".preg_replace("/,/",", ",$user)."</td>"; 
320         $res.="   <td title='".$line['objecttype']."'>".$line['objecttype']."</td>"; 
321         $res.="   <td title='".$line['object']."'>".preg_replace("/,/",", ",LDAP::fix($object))."</td>"; 
322         $res.="   <td>".$line['result']."</td>";
323         $res.=" </tr>\n";
324       }
325     }
326     @mysql_close($link);
327     $smarty->assign("mode".$this->sort, $sort_sign);
328     $smarty->assign("search_result", $res);
330     $smarty->assign("regex", $this->regex);
331     $smarty->assign("server",$this->server);
332     $smarty->assign("servers",$servers);
333     $smarty->assign("action",$this->action);
334     $smarty->assign("actions",$actions);
335     $smarty->assign("date_select", $date_select);
337     $smarty->assign("direction", $this->sort_direction);
339     /* Show main page */
340     return ($smarty->fetch (get_template_path('gosa_log_contents.tpl', TRUE)));
341   }
344   function save_object()
345   {
346       /* Get submitted range */ 
347       if(isset($_POST['EntriesPerPage'])){
348         if(is_numeric($_POST['EntriesPerPage'])){
349           $this->range = $_POST['EntriesPerPage'];
350         }
351       }
353       /* Get actual select boxe values */
354       $logfilter_changed = FALSE;
355       foreach( array("server", "action","time","regex") as $type){
356         if(isset($_POST[$type])){
357           if($type == "server" && $this->server != $_POST['server']){
358             $this->location_id = -1;
359           }
360           $this->$type = $_POST[$type];  
361         }
362       }
364       /* Filter regex values */
365       if ($this->regex == ""){
366         $this->regex= '*';
367       } else {
368         $new = preg_replace('/\*\**/', '*', $this->regex);
369         $this->regex= $new;
370       }
372       /* Set start value */
373       if (isset($_GET['start'])){
374         $this->start= (int)$_GET['start'];
375       }
377       /* Reset page number if filter has changed */
378       if ($logfilter_changed > 0){
379         $this->start= 0;
380       }
382       /* Adapt sorting */
383       if (isset($_GET['sort'])){
385         if($_GET['direction'] == "up"){
386           $this->sort_direction = "down";
387         }else{
388           $this->sort_direction = "up";
389         }
391         $this->sort= (int)$_GET['sort'];
392         if ($this->sort < 0 || $this->sort > count($this->fields)){
393           $this->sort= 0;
394         }
395       }
397       /* Save attributes in Sessiob, so we are able to recontruct filter on plugin reload */
398       $logview = session::get('logview');
399       $logview['gosa_log'] = array();
401       foreach(array("action","server","time","regex") as $attr){
402         $logview['gosa_log'][$attr] = $this->$attr;
403       }
404       session::set('logview',$logview);
405   }
408   /* Return plugin informations for acl handling
409      #FIXME You can only read attributes within this report plugin 
410   static function plInfo()
411   {
412     return (array(
413         "plShortName"   => _("Log view"),
414         "plDescription" => _("Log view addon"),
415         "plSelfModify"  => FALSE,
416         "plDepends"     => array(),
417         "plPriority"    => 0,
418         "plSection"     => array("addon"),
419         "plCategory"    => array("logview" => array("objectClass" => "none", "description" => _("System logs"))),
421         "plProvidedAcls" => array()
422         ));
423   }
424   */
426 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
427 ?>