Code

Unified unlocking of plugins (main.inc)
[gosa.git] / gosa-plugins / gofax / gofax / faxreports / class_faxreport.inc
1 <?php
3 class faxreport extends plugin
4 {
5   /* Definitions */
6   var $plHeadline     = "FAX Reports";
7   var $plDescription  = "This does something";
8   var $plIcon         = "plugins/gofax/images/reports.png";
10   /* For internal use */
11   var $start          = 0;
12   var $search_for     = "*";
13   var $search_base    = "";
14   var $year           = "";
15   var $month          = "";
16   var $sort           = 1;
17   var $sort_direction = "down";
18   var $report_list    = array();
19   var $ui             = NULL;
20   var $range          = 20;
21   var $view_logged = FALSE;
23   /* Constant stuff */
24   var $status= array( "SENT", "MAILED", "SERROR", "RERROR", "SBLOCK", "RBLOCK",
25       "DELETED", "REQUEUED", "DISABLED", "PRINTED", "DIVERTED",
26       "UNDEFINED", "UNDEFINED", "UNDEFINED", "UNDEFINED",
27       "UNDEFINED");
28   var $fields= array("uid", "queuing_time", "status", "sender_id", "receiver_id", "pages");
30   /* these vars will be stored in session to be able to remember last search config */
31   var $attributes_SO= array("search_for","search_base","month","year","start","year","month","sort","sort_direction","range");
32   var $objectclasses= array();
33   var $fax_users    = array();
36   /* Create class */
37   function faxreport (&$config, &$ui)
38   {
39     /* Include config object */
40     $this->config       = $config;
41     $this->ui           = &$ui;
42     $this->search_base  = get_base_from_people($ui->dn);
43     $this->year         = date("Y");
44     $this->month        = date("m");
46     /* Get global filter config and set class vars , 
47        or create a filter */
48     if (!session::is_set("faxreportfilter")){
49       $faxreportfilter = array();
50       foreach($this->attributes_SO as $name){
51         $faxreportfilter[$name] = $this->$name;
52       } 
53       session::set("faxreportfilter",$faxreportfilter);
54     }else{
55       $faxreportfilter = session::get("faxreportfilter");
56       foreach($this->attributes_SO as $name){
57         $this->$name = $faxreportfilter[$name];
58       }
59     }
60   }
62   
63   /* Create Filter & Search & Display results */
64   function execute()
65   {
66     /* Call parent execute */
67     plugin::execute();
69     /* Log view */
70     if(!$this->view_logged){
71       $this->view_logged = TRUE;
72       new log("view","users/".get_class($this),$this->dn);
73     }
75     /************ 
76       Variable initialisation 
77      ************/
78  
79     /* Create months */ 
80     $months= array();
81     for($i = 1 ; $i <= 12 ; $i ++ ){
82       $months[$i] = _(date("F",gmmktime(0,0,0,$i,1)));
83     }
85     /* Create years */
86     $current= date("Y");
87     $years= array();
88     for ($y= $current - 5; $y<=$current; $y++){
89       $years[]= $y;
90     }
92     
93     /************ 
94       Set smarty defaults  
95      ************/
97     $smarty= get_smarty();
98     $smarty->assign("launchimage"               , get_template_path('images/launch.png'));
99     $smarty->assign("search_image"  , get_template_path('images/lists/search.png'));
100     $smarty->assign("search_for"                , $this->search_for);
101     $smarty->assign("bases"                               , $this->config->idepartments);
102     $smarty->assign("base_select"               , $this->search_base);
103     $smarty->assign("months"                      , $months);
104     $smarty->assign("month_select"      , $this->month);
105     $smarty->assign("years"                               , $years);
106     $smarty->assign("year_select"               , $this->year);
107     $smarty->assign("search_result"     , "");
109     
110     /************ 
111       Check database accessibility 
112      ************/
114     /* Some checks */
115     if(!isset($this->config->data['SERVERS']['FAX'])){
116       msg_dialog::display(_("Error"), _("No fax server found!"), ERROR_DIALOG);
117       return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
118     }elseif(!is_callable("mysql_connect")){
119       msg_dialog::display(_("Configuration error"), sprintf(_("Missing %s PHP extension!"), "mysql"), WARNING_DIALOG);
120       return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
121     }else{
122       /* Connecting, selecting database */
123       $cfg      = $this->config->data['SERVERS']['FAX'];
124       $link     = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
125       if ($link === FALSE){
126         msg_dialog::display(_("Error"), sprintf(_("Cannot connect to %s database!"), "GOfax"), ERROR_DIALOG);
127         return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
128       }
129       if (! @mysql_select_db("gofax")){
130         msg_dialog::display(_("Error"), sprintf(_("Cannot select %s database!"), "GOfax"), ERROR_DIALOG);
131         return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
132       }
134       if (! mysql_query("SELECT * FROM faxlog;")){
135         msg_dialog::display(_("Error"), sprintf(_("Cannot query %s database!"), "GOfax"), ERROR_DIALOG);
136         return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
137       }
138     }           
140     
141     /************ 
142       Perform a deatil view 
143      ************/
145     /* Do detail view? */
146     if (isset($_GET['detail'])){
148       /* Create query */
149       $query = "SELECT id,uid,date_format(queuing_time, '%Y%m%d%H%i%s') as queuing_time,status,sender_id,sender_msn,receiver_id,".
150         "receiver_msn,pages,status_message,transfer_time FROM faxlog WHERE id=".$_GET['detail'].";";
152       /* Connecting, selecting database */
153       $cfg= $this->config->data['SERVERS']['FAX'];
155       /* Check if everything went ok*/
156       $result = @mysql_query($query);
157       if ($result === false){
158         msg_dialog::display(_("Error"), sprintf(_("Cannot query %s database!"), "GOfax"), ERROR_DIALOG);
159         @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query failed");
160         return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
161       }
163       $line = mysql_fetch_array($result, MYSQL_ASSOC);
164       mysql_close($link);
166       if (!preg_match ("/'".$line["uid"]."'/", $this->userfilter)){
167         msg_dialog::display(_("Permission error"), _("You have no permission to view this fax id!"), ERROR_DIALOG);
168         return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
169       }
171       /* Check acls */
172       $fax_uids = array_flip($this->fax_users);
173       $uid = $line['uid'];
174       $dn = $fax_uids[$uid];
175       $acls = $this->ui->get_permissions($dn,"faxreport/faxreport");
176       if(!preg_match("/r/",$acls)){
177         msg_dialog::display(_("Permission error"), _("You have no permission to view this fax id!"), ERROR_DIALOG);
178         return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
179       }
181       $parts= array( "id", "uid", "queuing_time", "status", "sender_id", "sender_msn",
182           "receiver_id", "receiver_msn", "pages", "status_message", "transfer_time" );
184       foreach ($parts as $vname) {
186         $final="fax_$vname";
187         if($vname != "uid"){
188           $v_acl = $this->ui->get_permissions($dn,"faxreport/faxreport",preg_replace("/_/","",$vname));
189         }else{
190           $v_acl = "r";
191         }
193         if ($line[$vname] != "" && preg_match("/r/",$v_acl)){
194           $smarty->assign("$final", $line[$vname]);
195         } else {
196           $smarty->assign("$final", "-");
197         }
198       }
199       $queuing_time= $line['queuing_time'];
201       /* The user is allowed to download all fax images from those users 
202           that are listed in session::get('fuserfilter') 
203          Don't forget to check getfax.php if you change somthing here */
204       session::set('fuserfilter',$this->userfilter);
205       $smarty->assign("plug", "?plug=".validate($_GET['plug']));
206       $smarty->assign("detail", validate($_GET['detail']));
208       $format= _("Y-M-D");
209       $date= preg_replace("/Y/", substr($queuing_time,0,4), $format);
210       $date= preg_replace("/M/", substr($queuing_time,4,2), $date);
211       $date= preg_replace("/D/", substr($queuing_time,6,2), $date);
212       $smarty->assign("date", $date);
213       $smarty->assign("time", substr($queuing_time,8,2).":".
214           substr($queuing_time,10,2).":".
215           substr($queuing_time,12,2));
216       return($smarty->fetch(get_template_path('detail.tpl', TRUE)));
217     }
219     
220     /************ 
221       Search for uids matching the filter  
222      ************/
224     /* Search button has been pressed */
225     if ($this->search_for != ""){
227       if (is_integer (strpos($this->search_for, "*"))){
228         $s= $this->search_for;
229       } else {
230         $s= "*".$this->search_for."*";
231       }
232       $ldap= $this->config->get_ldap_link();
233       $ldap->cd ($this->search_base);
235       /* Perform ldap search for potential users */
236       $filter= "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))".
237           "(objectClass=goFaxAccount)".
238           "(|(uid=$s)(l=$s)(homePhone=$s)".
239           "(telephoneNumber=$s)(facsimileTelephoneNumber=$s)(mobile=$s)".
240           "(pager=$s)(cn=$s)(givenName=$s)(sn=$s)(personalTitle=$s)".
241           "(title=$s)))";
242  
243       $res = get_list($filter, "users", $this->search_base, array("uid"), GL_SUBSEARCH );
245       /* Check if we are allowed to collect fax data */
246       $fax_users= array();
247       foreach($res as $attrs){
248         $acl = $this->ui->get_permissions($attrs['dn'],"users/user","uid");
249         if(preg_match("/r/",$acl)){
250           $fax_users[ $attrs['dn']]= $attrs["uid"][0];
251         }
252       }
254       /* Add self uid, if we have self acl
255        */   
256       if(preg_match("/(r|w)/",$this->ui->get_permissions($this->ui->dn,"faxreport/faxreport",""))){
257         $fax_users[$this->ui->dn] = $this->ui->uid;
258       }
260       $this->fax_users = $fax_users;
262       /* Prepare SQL query */
263       $this->userfilter= "";
264       foreach ($fax_users as $user){
265         $this->userfilter.= "uid = '$user' OR ";
266       }
267       $this->userfilter= preg_replace("/OR $/", "", $this->userfilter);
268     }
270     /************ 
271       Create filter  
272      ************/
274     /* Perform SQL query */
275     if ($this->userfilter){
276       if ($this->sort_direction == "down"){
277         $desc= "DESC";
278       } else {
279         $desc= "";
280       }
281       $start= date ("YmdHis", mktime(0,0,0,$this->month,1,$this->year));
282       $end=   date ("YmdHis", mktime(23,59,59,$this->month+1,0,$this->year));
283       $query = "SELECT id,uid,date_format(queuing_time, '%Y%m%d%H%i%s') as queuing_time,status,sender_id,receiver_id,pages FROM faxlog ".
284         "WHERE ( ".$this->userfilter." ) AND queuing_time <= $end AND ".
285         "queuing_time >= $start ORDER BY ".$this->fields[$this->sort]." $desc;";
287       if(!is_callable("mysql_connect")){
288         msg_dialog::display(_("Configuration error"), sprintf(_("Missing %s PHP extension!"), "mysql"), WARNING_DIALOG);
289         return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
290       }
292     
293     /************ 
294       Create results  
295      ************/
297       /* Connecting, selecting database */
298       $cfg= $this->config->data['SERVERS']['FAX'];
299       $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
300       
301       @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query");
302       $result = @mysql_query($query);
303       if ($result === false){
304         msg_dialog::display(_("Error"), sprintf(_("Cannot query %s database!"), "GOfax"), ERROR_DIALOG);
305         return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
306       }
308       $this->report_list= array();
309       $user_ids = array_flip($fax_users);
310       while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
312         /* Check permissions for each field */
313         $dn = $user_ids[$line['uid']];
315         /* Hide restricted attributes */ 
316         foreach(array("pages","receiverid","senderid","status","queuingtime","detailedView") as $attr){
317           $var = $attr."ACL";
318           $$var = $this->ui->get_permissions($dn,"faxreport/faxreport",$attr);
319         }
321         /* Restricted attributes will not be displayed, this will be displayed instead */
322         $no_acl = "<img class='center' src='images/lists/locked.png' 
323                     title='"._("Insufficient permissions to view this attribute")."' alt='"._("Insufficient permissions")."'>";
325         /* Create date */ 
326         if((!empty($line["queuing_time"])) && preg_match("/r/",$queuingtimeACL)){
327           $hour=  substr($line["queuing_time"], 8, 2);
328           $minute=substr($line["queuing_time"], 10, 2);
329           $format= _("Y-M-D");
330           $date= preg_replace("/Y/", substr($line["queuing_time"], 0, 4), $format);
331           $date= preg_replace("/M/", substr($line["queuing_time"], 4, 2), $date);
332           $date= preg_replace("/D/", substr($line["queuing_time"], 6, 2), $date);
333           $str_date = $date." ".$hour.":".$minute;
334         }else{
335           $str_date = $no_acl;
336         }
337   
338         /* Create entry html str */
340         if(preg_match("/r/",$detailedViewACL)){
341           $str = "<td class=\"phonelist\">
342             <a href=\"main.php?plug=".validate($_GET['plug'])."&amp;detail=".$line["id"]."\">
343             <img class='center' alt=\"\" align=\"middle\" border=0 src=\"".get_template_path('images/info_small.png')."\">
344             &nbsp;".$line["uid"].
345             "</a>
346             </td>
347             <td>$str_date</td>";
348         }else{
349           $str = "<td>
350             <img class='center' alt=\"\" align=\"middle\" border=0 src=\"".get_template_path('images/info_small.png')."\">
351             &nbsp;".$line["uid"]."</td>
352             <td>$str_date</td>";
353         }
354         
355         /* Add Status td */
356         if(preg_match("/r/",$statusACL)){
357           $str.="<td>".$this->status[$line["status"]]."</td>";
358         }else{
359           $str.="<td>".$no_acl."</td>";
360         }
362         /* Add sender_id td */
363         if(preg_match("/r/",$senderidACL)){
364           $str.="<td>".$line["sender_id"]."</td>";
365         }else{
366           $str.="<td>".$no_acl."</td>";
367         }
369         /* Add receiver_id td */
370         if(preg_match("/r/",$receiveridACL)){
371           $str.="<td>".$line["receiver_id"]."</td>";
372         }else{
373           $str.="<td>".$no_acl."</td>";
374         }
376         /* Add receiver_id td */
377         if(preg_match("/r/",$pagesACL)){
378           $str.="<td>".$line["pages"]."</td>";
379         }else{
380           $str.="<td>".$no_acl."</td>";
381         }
382       $this->report_list[] =  $str;
383       }
385       mysql_close($link);
386     }
388     /************ 
389       Create output out of results  
390      ************/
392     /* Generate output */
393     $mod= 0;
394     $output= "";
395     foreach ($this->report_list as $val){
396       if ($mod < $this->start) {
397         $mod++;
398         continue;
399       }
400       if ($mod >= ($this->start + $this->range)){
401         $mod++;
402         break;
403       }
404       if ( ($mod++) & 1){
405         $col= "background-color: #ECECEC;";
406       } else {
407         $col= "background-color: #F5F5F5;";
408       }
409       $output.= "<tr style=\"height:22px; $col\">$val</tr>";
410     }
412     
413     /************ 
414       Display results  
415      ************/
417     if (isset($fax_users) && count($fax_users)){
418       $smarty->assign("search_result", $output);
419       $smarty->assign("range_selector", range_selector(count($this->report_list), $this->start, $this->range,"EntriesPerPage"));
420     }else{
421       $smarty->assign("search_result", "");
422     }
424     /* Show main page */
425     $smarty->assign("plug", "?plug=".validate($_GET['plug']));
426     for($i= 0; $i<7; $i++){
427       $smarty->assign("mode$i", "");
428     }
429     $smarty->assign("mode".$this->sort, "<img alt=\"\" src=\"images/sort_".$this->sort_direction.
430         ".png\" border=0 align=middle>");
431     return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
432   }
435   /* Save ui input, and store it in session::get() 
436      to remember last search next time*/
437   function save_object()
438   {
439     $faxreportfilter = session::get("faxreportfilter");
440     if(isset($_POST['EntriesPerPage'])){
441       $this->range = $_POST['EntriesPerPage'];
442     }
444     if (isset($_GET['start'])){
445       $this->start= (int)$_GET['start'];
446     }
448     /* Adapt sorting */
449     if (isset($_GET['sort'])){
450       if ($this->sort == (int)$_GET['sort']){
451         if ($this->sort_direction == "down"){
452           $this->sort_direction= "up";
453         } else {
454           $this->sort_direction= "down";
455         }
456       }
457       $this->sort= (int)$_GET['sort'];
458       if ($this->sort < 0 || $this->sort > 5){
459         $this->sort= 0;
460       }
461     }
462     foreach( array("year", "month", "search_for", "search_base") as $type){
463       if (isset($_POST[$type])){
464         $faxreportfilter[$type]= $_POST[$type];
466         /* reset start page, if filter has changed */ 
467         if(!isset($_GET['start'])){
468           $this->start = 0;
469         }
470       }
471       $this->$type= $faxreportfilter[$type];
472    
473     }
474     foreach($this->attributes_SO as $name){
475       $faxreportfilter[$name] = $this->$name;
476     }  
477     session::set("faxreportfilter",$faxreportfilter);
478   }
481   /* Return plugin informations for acl handling
482     #FIXME You can only read attributes within this report plugin */
483   static function plInfo()
484   {
485   
486     return (array(
487           "plShortName"     => _("Fax report"),
488           "plDescription"   => _("Fax report")."&nbsp;<i>"._("All entries are readonly")."</i>",
489           "plSelfModify"    => TRUE,
490           "plDepends"       => array(),
491           "plPriority"      => 1,                                 // Position in tabs
492           "plSection"       => array("administration"),                     // This belongs to personal
493           "plCategory"      => array("faxreport" => array("description" => _("Fax reports"),
494                                                          "objectClass" => array())),
495           "plOptions"       => array(),
497           "plProvidedAcls" => array(
498             "detailedView"   => _("Detailed view"),
499             "id"             => _("Fax ID"),
500             "queuingtime"    => _("Date")."&nbsp;/&nbsp;"._("Time"),
501             "status"         => _("Status"),
502             "senderid"       => _("Sender ID"),
503             "sendermsn"      => _("Sender MSN"),
504             "receiverid"     => _("Receiver ID"),
505             "receivermsn"    => _("Receiver MSN"),
506             "pages"          => _("Number of pages"),
507             "statusmessage"  => _("Status Message"),
508             "transfertime"   => _("Transfer time"))
509         ));
510   }
513 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
514 ?>