Code

Seperated timezone functions
[gosa.git] / gosa-core / plugins / 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;
26   var $timezone_offset  = 0;
28   var $uid_map = array();
29   
31   function gosa_logview (&$config, $dn= NULL)
32   {
33     /* Include config object */
34     $this->config= &$config;
35     $this->ui = get_userinfo();
37     /* Restore last filter settings */
38     $logview = session::get('logview');
39     foreach(array("action","server","time","regex") as $attr){
40       if(isset($logview['gosa_log'][$attr])){
41         $this->$attr = $logview['gosa_log'][$attr];
42       }
43     }
45     $tz = timezone::get_default_timezone();
46     $this->timezone_offset = $tz['value'] * 60 * 60 ;
48     $ldap = $this->config->get_ldap_link();
49     $ldap->search("(&(objectClass=person)(uid=*))",array("uid","dn"));
50     while($attrs = $ldap->fetch()){
51       $this->uid_map[$attrs['dn']] = $attrs['uid'][0];
52     }
53   }
56   function execute()
57   {
58     /* Call parent execute */
59     plugin::execute();
61     /* Log view */
62     if(!$this->view_logged){
63       $this->view_logged = TRUE;
64       new log("view","logview/".get_class($this),$this->dn);
65     }
67     /* Time interval */
68     $date_select = array(
69         "0" => _("one hour"), "1" => _("6 hours"),
70         "2" => _("12 hours"), "3" => _("24 hours"),
71         "4" => _("2 days"),   "5" => _("one week"),
72         "6" => _("2 weeks"),  "7" => _("one month"));
75     $time = time();
76     $date_select_ = array(
77         "0" => $time - 3600, 
78         "1" => $time - 21600,
79         "2" => $time - 43200,
80         "3" => $time - 86400,
81         "4" => $time - 172800,
82         "5" => $time - 604800,
83         "6" => $time - 1209600,
84         "7" => $time - 2419200);
86     $smarty= get_smarty();
87     $smarty->assign("search_result", "");
88     $smarty->assign("plug", "?plug=".validate($_GET['plug']));
89     $smarty->assign("search_image", get_template_path('images/search.png'));
90     $smarty->assign("time_image", get_template_path('images/time.png'));
91     $smarty->assign("server_image", get_template_path('images/server.png'));
92     $smarty->assign("log_image", get_template_path('images/log_warning.png'));
93     $smarty->assign("ruleset_image", get_template_path('images/edit.png'));
94     $smarty->assign("launchimage", get_template_path('images/launch.png'));
95     $smarty->assign("date_select", $date_select);
96     $smarty->assign("actions", array());
97     $smarty->assign("direction", "up");
98     $smarty->assign("mode0", "");
99     $smarty->assign("mode1", "");
100     $smarty->assign("mode2", "");
101     $smarty->assign("mode3", "");
102     $smarty->assign("mode4", "");
103     $smarty->assign("mode5", "");
104     $smarty->assign("mode6", "");
106     /* Assign select option content */
107     foreach( array("server", "action", "time", "regex") as $type){
108       $smarty->assign("$type", $this->$type);
109     }
111     /**** 
112      * Check if defined servers 
113      *  and mysql extension 
114      ****/
115     if (!isset($this->config->data['SERVERS']['LOGGING']) || !count($this->config->data['SERVERS']['LOGGING'])){
116       print_red (_("No GOsa LOG servers defined!"));
117       return ($smarty->fetch (get_template_path('gosa_log_contents.tpl', TRUE)));
118     }
119     
120     if(!is_callable("mysql_connect")){
121       print_red(_("There is no mysql extension available, please check your php setup."));
122       return ($smarty->fetch (get_template_path('gosa_log_contents.tpl', TRUE)));
123     }
125     /****
126      * Get Configurations 
127      ****/
128     $list_of_servers = $this->config->data['SERVERS']['LOGGING']; 
129     $servers         = array();
130     foreach($list_of_servers as $servername => $data){
131       $servers[$servername] = $servername;
132     }
133     $smarty->assign("servers", $servers);
134     
135     /* Set a default server, if there is currently no valid server selected */
136     if(empty($this->server) || !in_array($this->server,$servers)){
137       $this->server = key($servers);
138     }
139    
140     /**** 
141      * Connect to currently selected server 
142      ****/
143     $cfg = $list_of_servers[$this->server];
144     $link = @mysql_pconnect($this->server, $cfg['USER'], $cfg['PWD']);
145     if ($link === FALSE){
146       print_red(_("Can't connect to log database, no logs can be shown!"));
147       new log("debug","logview","",array(),@mysql_error());
148       return ($smarty->fetch (get_template_path('gosa_log_contents.tpl', TRUE)));
149     }
151     /* check of log database is available */
152     if (! @mysql_select_db($cfg['DB'])){
153       print_red(_("Can't select log database for log generation!"));
154       new log("debug","logview","",array(),@mysql_error());
155       return ($smarty->fetch (get_template_path('gosa_log_contents.tpl', TRUE)));
156     }
158     
159     /****
160      * Get all action types 
161      ****/
163     $actions = array("!ALL" => _("All"));
164     $query = "SELECT distinct(action) FROM gosa_log;";
165     $res = @mysql_query($query);
166     while($attrs = @mysql_fetch_assoc($res)){
167       $actions[$attrs['action']] = $attrs['action'];
168     } 
171     /****
172      * Check Acls 
173      ****/
175     $ui = get_userinfo();
176     $dn = $list_of_servers[$this->server]['DN'];
177     $acls = $ui->get_permissions($dn,"server/gosaLogServer","viewEntries");
178     
179     if(!preg_match("/r/",$acls)){
180       $res = "<tr>
181         <td colspan=".count($this->fields).">
182         "._("You have insufficient permissions to view syslog entries.")."
183         </td>
184         </tr>";
186       /* Show main page */
187       $smarty->assign("range_selector", "");
188       $smarty->assign("search_result", $res);
189       return ($smarty->fetch (get_template_path('gosa_log_contents.tpl', TRUE)));
190     }
193     /****
194      * Query stuff 
195      ****/
196     $res= "";
197     $cfg    = $list_of_servers[$this->server];
198     $link   = @mysql_pconnect($this->server, $cfg['USER'], $cfg['PWD']);
200     /* Test connection object && create up query string */
201     if ($link === FALSE){
202       print_red(_("Can't connect to log database, no logs can be shown!"));
203       new log("debug","logview","",array(),@mysql_error());
204     } else {
206       if (! @mysql_select_db($cfg['DB'])){
207         print_red(_("Can't select log database for log generation!"));
208         new log("debug","logview","",array(),@mysql_error());
209       } else {
211         /* Prepare order setting */
212         if ($this->sort_direction == "down"){
213           $desc= "DESC";
214           $sort_sign = "<img src='images/sort_down.png' alt='\/' class='center' title='down' border=0>";
215         } else {
216           $desc= "";
217           $sort_sign = "<img src='images/sort_up.png' alt='/\' class='center' title='up' border=0>";
218         }
220         /* Get start time */
221         $start = $date_select_[$this->time];
222         
223         /* Prepare search filter */
224         $sql_regex =trim(preg_replace("/\*/","%",$this->regex));
225         $sql_regex = "%".trim($sql_regex,"%")."%";
227         /* Create search filter */
228         $query_base= "FROM  gosa_log WHERE timestamp >= $start ";
229   
230         /* Append action filter */
231         if($this->action != "!ALL"){
232           $query_base .=" AND action like '".$this->action."' ";
233         }
235         /* Append search filter */
236         if($sql_regex != "%%"){
237           $query_base .=" AND ( result like '".$sql_regex."' OR user like '".$sql_regex."') ";
238         }
240         /* Get number of entries */
241         $query= "SELECT COUNT(`user`) ".$query_base.";";
242         $result = mysql_query($query);
243         if(!$result){
244           new log("debug","logview","",array(),@mysql_error());
245         }
246         $line= mysql_fetch_array($result, MYSQL_ASSOC);
247         $count= $line['COUNT(`user`)'];
248         if ($count > 25){
249           $smarty->assign("range_selector", range_selector($count, $this->start, $this->range,"EntriesPerPage"));
250         } else {
251           $smarty->assign("range_selector", "");
252         }
254         /* Query results that will be displayed */
255         $query= "SELECT * ".$query_base." ORDER BY ".$this->fields[$this->sort]." ".$desc." LIMIT  ".$this->start.",".$this->range.";";
256         @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__, $query, "Database query");
257         $result = @mysql_query($query);
258         if(!$result){
259           new log("debug","logview","",array(),@mysql_error());
260         }
262         /* Display results */
263         $mod= 0;
265         /* Add entries to result str */
266         while ($line = mysql_fetch_array($result, MYSQL_ASSOC)){
268           /* Toggle background color */
269           if ( ($mod++) & 1){
270             $col= "background-color: #ECECEC;";
271           } else {
272             $col= "background-color: #F5F5F5;";
273           }
275           $base = ", ".substr($this->config->current['BASE'],0,5)."...";
276   
277           if(isset($this->uid_map[$line['user']])){
278             $user = $this->uid_map[$line['user']];
279           }else{
280             $user = preg_replace("/,".normalizePreg($this->config->current['BASE'])."/",$base,$line['user']);
281           }
282           $object = preg_replace("/,".normalizePreg($this->config->current['BASE'])."/",$base,$line['object']);
284           $res.=" <tr style=\"$col\">\n";
285           $res.="   <td title='".$line['objecttype']."'>".$line['action']."</td>";
286           $res.="   <td>".date("H:i:s d.m.Y",($line['timestamp'] + $this->timezone_offset))."</td>";
287           $res.="   <td title='".$line['user']."'>".preg_replace("/,/",", ",$user)."</td>"; 
288           $res.="   <td title='".$line['objecttype']."'>".$line['objecttype']."</td>"; 
289           $res.="   <td title='".$line['object']."'>".preg_replace("/,/",", ",$object)."</td>"; 
290           $res.="   <td>".$line['result']."</td>";
291 #         if($line['repeated'] == 1){
292 #           $res.="   <td>-</td>";
293 #         }else{
294 #           $res.="   <td>".($line['repeated']-1)."</td>";
295 #         }
296           $res.=" </tr>\n";
297         }
298         mysql_close($link);
299         $smarty->assign("mode".$this->sort, $sort_sign);
300         $smarty->assign("search_result", $res);
301       }
302     }
304     $smarty->assign("regex", $this->regex);
305     $smarty->assign("server",$this->server);
306     $smarty->assign("servers",$servers);
307     $smarty->assign("action",$this->action);
308     $smarty->assign("actions",$actions);
309     $smarty->assign("date_select", $date_select);
311     $smarty->assign("direction", $this->sort_direction);
313     /* Show main page */
314     return ($smarty->fetch (get_template_path('gosa_log_contents.tpl', TRUE)));
315   }
318   function save_object()
319   {
320     if(isset($_POST['gosa_log_contents_posted'])){
322       /* Get submitted range */ 
323       if(isset($_POST['EntriesPerPage'])){
324         if(is_numeric($_POST['EntriesPerPage'])){
325           $this->range = $_POST['EntriesPerPage'];
326         }
327       }
329       /* Get actual select boxe values */
330       $logfilter_changed = FALSE;
331       foreach( array("server", "action","time","regex") as $type){
332         if(isset($_POST[$type])){
333           $this->$type = $_POST[$type];   
334         }
335       }
337       /* Filter regex values */
338       if ($this->regex == ""){
339         $this->regex= '*';
340       } else {
341         $new = preg_replace('/\*\**/', '*', $this->regex);
342         $this->regex= $new;
343       }
345       /* Set start value */
346       if (isset($_GET['start'])){
347         $this->start= (int)$_GET['start'];
348       }
350       /* Reset page number if filter has changed */
351       if ($logfilter_changed > 0){
352         $this->start= 0;
353       }
355       /* Adapt sorting */
356       if (isset($_GET['sort'])){
358         if($_GET['direction'] == "up"){
359           $this->sort_direction = "down";
360         }else{
361           $this->sort_direction = "up";
362         }
364         $this->sort= (int)$_GET['sort'];
365         if ($this->sort < 0 || $this->sort > count($this->fields)){
366           $this->sort= 0;
367         }
368       }
370       /* Save attributes in Sessiob, so we are able to recontruct filter on plugin reload */
371       $logview = session::get('logview');
372       $logview['gosa_log'] = array();
373       foreach(array("action","server","time","regex") as $attr){
374         $logview['gosa_log'][$attr] = $this->$attr;
375       }
376       session::set('logview',$logview);
377     }
378   }
381   /* Return plugin informations for acl handling
382      #FIXME You can only read attributes within this report plugin 
383   static function plInfo()
384   {
385     return (array(
386         "plShortName"   => _("Log view"),
387         "plDescription" => _("Log view addon"),
388         "plSelfModify"  => FALSE,
389         "plDepends"     => array(),
390         "plPriority"    => 0,
391         "plSection"     => array("addon"),
392         "plCategory"    => array("logview" => array("objectClass" => "none", "description" => _("System logs"))),
394         "plProvidedAcls" => array()
395         ));
396   }
397   */
399 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
400 ?>