Code

Backport from trunk
[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  = "View the FAX report or single documents that have been received";
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 $ui             = NULL;
19     var $range          = 20;
20     var $view_logged    = FALSE;
22     /* Constant stuff */
23     var $status= array( "SENT", "MAILED", "SERROR", "RERROR", "SBLOCK", "RBLOCK",
24         "DELETED", "REQUEUED", "DISABLED", "PRINTED", "DIVERTED",
25         "UNDEFINED", "UNDEFINED", "UNDEFINED", "UNDEFINED",
26         "UNDEFINED");
27     var $fields= array("uid", "queuing_time", "status", "sender_id", "receiver_id", "pages");
29     /* these vars will be stored in session to be able to remember last search config */
30     var $attributes_SO= array("search_for","month","year",
31         "start","year","month","sort","sort_direction","range");
33     var $objectclasses= array();
34     var $fax_users    = array();
36     // An array containing all valid fax accounts (uid => dn)
37     // This enables us to perform correct ACL checks later. 
38     // We require "users/viewFaxEntries" acls for to users dn, if he exists.
39     //  and "users/viewFaxEntries" acls for the LDAP-Base if the uid is 
40     //  unknown (The uid is not represented by a valid FAX account).
41     var $uidToDN = array();
43     /* Create class */
44     function faxreport (&$config, &$ui)
45     {
46         $this->initTime = microtime(TRUE);
48         /* Include config object */
49         $this->config       = $config;
50         $this->ui           = &$ui;
51         $this->search_base  = $this->config->current['BASE'];
52         $this->year         = date("Y");
53         $this->month        = date("m");
55         // Get global filter config and set class vars , or create a filter 
56         if (!session::is_set("faxreportfilter")){
57             $faxreportfilter = array();
58             foreach($this->attributes_SO as $name){
59                 $faxreportfilter[$name] = $this->$name;
60             } 
61             session::set("faxreportfilter",$faxreportfilter);
62         }else{
63             $faxreportfilter = session::get("faxreportfilter");
64             foreach($this->attributes_SO as $name){
65                 $this->$name = $faxreportfilter[$name];
66             }
67         }
69         // Get ALL valid FAX-Accounts and their dns, this allows us to perform correct
70         //  permissions checks later. 
71         $filter= "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(objectClass=goFaxAccount)(uid=*))";
72         $tmp= get_list($filter, "users/viewFaxEntries", $this->search_base, 
73             array("uid"), GL_SUBSEARCH | GL_NO_ACL_CHECK);
74         $this->uidToDN = array();
75         foreach($tmp as $attrs){
76             $this->uidToDN[$attrs['uid'][0]] = $attrs['dn'];
77         }
79         // Create statistic table entry
80         stats::log('plugin', $class = get_class($this), $category = array($this->acl_category),  $action = 'open',
81                 $amount = 1, $duration = (microtime(TRUE) - $this->initTime));
83     }
86     /* Create Filter & Search & Display results */
87     function execute()
88     {
89         /* Call parent execute */
90         plugin::execute();
92         /* Log view */
93         if(!$this->view_logged){
94             $this->view_logged = TRUE;
95             new log("view","users/".get_class($this),$this->dn);
96         }
98         // Variable initialisation 
100         /* Create months */ 
101         $months= array();
102         for($i = 1 ; $i <= 12 ; $i ++ ){
103             $months[$i] = _(date("F",gmmktime(0,0,0,$i,1)));
104         }
106         /* Create years */
107         $current= date("Y");
108         $years= array();
109         for ($y= $current - 5; $y<=$current; $y++){
110             $years[]= $y;
111         }
114         // Set smarty defaults  
115         $smarty= get_smarty();
116         $smarty->assign("search_for"            , set_post($this->search_for));
117         $smarty->assign("months"                          , $months);
118         $smarty->assign("month_select"  , $this->month);
119         $smarty->assign("years"                           , $years);
120         $smarty->assign("year_select"           , $this->year);
121         $smarty->assign("search_result" , "");
124         // Check database accessibility 
125         if(!isset($this->config->data['SERVERS']['FAX'])){
126             msg_dialog::display(_("Error"), _("No FAX server found!"), ERROR_DIALOG);
127             return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
128         }elseif(!is_callable("mysql_connect")){
129             msg_dialog::display(_("Configuration error"), sprintf(
130                 _("Missing %s PHP extension!"), "mysql"), WARNING_DIALOG);
131             return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
132         }else{
133             /* Connecting, selecting database */
134             $cfg        = $this->config->data['SERVERS']['FAX'];
135             $link       = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
136             if ($link === FALSE){
137                 msg_dialog::display(_("Error"), sprintf(
138                     _("Cannot connect to %s database!"), "<b>".$cfg['SERVER'].":"."GOfax"."</b>"), ERROR_DIALOG);
139                 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
140             }
141             if (! @mysql_select_db("gofax")){
142                 msg_dialog::display(_("Error"), sprintf(
143                     _("Cannot select %s database!"), "GOfax"), ERROR_DIALOG);
144                 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
145             }
147             if (! mysql_query("SELECT * FROM faxlog;")){
148                 msg_dialog::display(_("Error"), sprintf(
149                     _("Cannot query %s database!"), "GOfax"), ERROR_DIALOG);
150                 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
151             }
152         }               
155         // Perform a deatil view 
156         $detail = -1;
157         foreach($_POST as $name => $desc){
158             if(preg_match("/^detail_/", $name)){
159                 $detail = postDecode(preg_replace("/^detail_/","",$name));
160                 break;
161             }
162         }
163         if ($detail != -1){
165             // Query for the requested fay entry 
166             $query = "SELECT id,uid,date_format(queuing_time, '%Y%m%d%H%i%s') ".
167                 "as queuing_time,status,sender_id,sender_msn,receiver_id,".
168                 "receiver_msn,pages,status_message,transfer_time FROM faxlog WHERE id='".$detail."';";
170             $cfg= $this->config->data['SERVERS']['FAX'];
171             $result = @mysql_query($query);
172             if ($result === false){
173                 msg_dialog::display(_("Error"), sprintf(
174                     _("Cannot query %s database!"), "GOfax"), ERROR_DIALOG);
175                 @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query failed");
176                 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
177             }
178             $line = mysql_fetch_array($result, MYSQL_ASSOC);
179             mysql_close($link);
181             // Get dn to check ACLs for
182             // Existing ldap-user -> use its dn  
183             // Not existing user  -> use ldap base dn  
184             $dn = $this->search_base;
185             if(isset($this->uidToDN[$line['uid']])){
186                 $dn = $this->uidToDN[$line['uid']];
187             }
189             // We do not have any ACLs for this entry, so continue.
190             $acls = $this->ui->get_permissions($dn,"users/viewFaxEntries","");
191             if(!preg_match("/r/",$acls)){
192                 msg_dialog::display(_("Permission error"), 
193                 _("You have no permission to view this FAX id!"), ERROR_DIALOG);
194                 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
195             }
197             // Check permissions for each attribute
198             $parts= array( "id", "uid", "queuing_time", "status", "sender_id", "sender_msn",
199                 "receiver_id", "receiver_msn", "pages", "status_message", "transfer_time" );
200             foreach ($parts as $vname) {
201                 $final="fax_$vname";
202                 $v_acl = $this->ui->get_permissions($dn,"users/viewFaxEntries",preg_replace("/_/","",$vname));
203                 if($line[$vname] != "" && preg_match("/r/i", $v_acl)){
204                     $smarty->assign("$final", set_post($line[$vname]));
205                 } else {
206                     $smarty->assign("$final", "-");
207                 }
208             }
209             $format= _("Y-M-D");
210             $queuing_time= $line['queuing_time'];
212             $plug = (isset($_GET['plug'])) ? '?plug='.$_GET['plug'] : '';
213             $smarty->assign("plug", $plug);
214             $smarty->assign("detail", set_post($detail));
215             $date= preg_replace("/Y/", substr($queuing_time,0,4), $format);
216             $date= preg_replace("/M/", substr($queuing_time,4,2), $date);
217             $date= preg_replace("/D/", substr($queuing_time,6,2), $date);
218             $smarty->assign("date", $date);
219             $smarty->assign("time", substr($queuing_time,8,2).":".
220                 substr($queuing_time,10,2).":".
221                 substr($queuing_time,12,2));
223             return($smarty->fetch(get_template_path('detail.tpl', TRUE)));
224         }
227         // Convert search filter into useable string
228         $fax_users= array();
229         $s = preg_replace("/\*/","",$this->search_for);
230         if(!empty($s)) $s = "*{$s}*"; else $s="*";
232         // Collect potential 'uid's that match the filter.
233         $filter= "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))".
234             "(objectClass=goFaxAccount)".
235             "(|(uid=$s)(l=$s)(homePhone=$s)".
236             "(telephoneNumber=$s)(facsimileTelephoneNumber=$s)(mobile=$s)".
237             "(pager=$s)(cn=$s)(givenName=$s)(sn=$s)(personalTitle=$s)".
238             "(title=$s)))";
239         $res = get_list($filter, "users/viewFaxEntries", $this->search_base, array("uid"), GL_SUBSEARCH );
240         $fax_users = array();
241         foreach($res as $attrs){
242            $fax_users[ $attrs['dn']]= $attrs["uid"][0];
243         }
245         // Search for entries mathcing the filter
246         $userfilter = "uid like '".preg_replace("/\*/","%%", $s)."' OR ";
247         $userfilter.= "sender_id like '".preg_replace("/\*/","%%", $s)."' OR ";
248         $userfilter.= "receiver_id like '".preg_replace("/\*/","%%", $s)."' OR ";
250         // ... and additionally search for a status value.
251         $id = array_search($this->search_for, $this->status);
252         if($id !== FALSE){
253             $userfilter.= "status = '".$id."' OR ";
254         }
256         // Add collectod users to the query
257         foreach ($fax_users as $user){
258             $userfilter.= "uid = '$user' OR ";
259         }
260         $userfilter= "(".preg_replace("/OR $/", "", $userfilter).")";
262         // Add date settings
263         $desc = ($this->sort_direction == "down")? "": "DESC";
264         $start= date ("YmdHis", mktime(0,0,0,$this->month,1,$this->year));
265         $end=   date ("YmdHis", mktime(23,59,59,$this->month+1,0,$this->year));
266         $query_entries = "SELECT id,uid,date_format(queuing_time, '%Y%m%d%H%i%s') as queuing_time,".
267             "status,sender_id,receiver_id,pages FROM faxlog ".
268             "WHERE {$userfilter} AND queuing_time <= {$end} AND ".
269             "queuing_time >= {$start} ".
270             "ORDER BY ".$this->fields[$this->sort].
271             " {$desc } ";
274         $cfg= $this->config->data['SERVERS']['FAX'];
275         $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
277         // Get the results 
278         @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query");
279         $result =@mysql_query($query_entries);
280         if ($result === false){
281             msg_dialog::display(_("Error"), sprintf(_("Cannot query %s database!"), "GOfax"), ERROR_DIALOG);
282             return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
283         }
285         // Restricted attributes will not be displayed, this will be displayed instead */
286         $no_acl = image('images/lists/locked.png','',
287             _("Insufficient permissions to view this attribute"));
289         // Add found entries to result list.
290         $report_list= array();
291         $user_ids = array_flip($fax_users);
292         while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
294             // Get dn to check ACLs for
295             // Existing ldap-user -> use its dn  
296             // Not existing user  -> use ldap base dn  
297             $dn = $this->search_base;
298             if(isset($this->uidToDN[$line['uid']])){
299                 $dn = $this->uidToDN[$line['uid']];
300             }
302             // We do not have any ACLs for this entry, so continue.
303             $tmp = $this->ui->get_permissions($dn,"users/viewFaxEntries","");
304             if(empty($tmp)) continue;
306             // Hide field for which we have no permissions
307             foreach(array("pages","receiverid","senderid","status","queuingtime","detailedView") as $attr){
308                 $var = $attr."ACL";
309                 $$var = $this->ui->get_permissions($dn,"users/viewFaxEntries",$attr);
310             }
312             // Create date 
313             if((!empty($line["queuing_time"])) && preg_match("/r/",$queuingtimeACL)){
314                 $hour=  substr($line["queuing_time"], 8, 2);
315                 $minute=substr($line["queuing_time"], 10, 2);
316                 $format= _("Y-M-D");
317                 $date= preg_replace("/Y/", substr($line["queuing_time"], 0, 4), $format);
318                 $date= preg_replace("/M/", substr($line["queuing_time"], 4, 2), $date);
319                 $date= preg_replace("/D/", substr($line["queuing_time"], 6, 2), $date);
320                 $str_date = $date." ".$hour.":".$minute;
321             }else{
322                 $str_date = $no_acl;
323             }
325             /* Create entry html str */
326             $str = " \n<td class='list0'>".$line["uid"]."</td>";
327             $str.= " \n<td class='list0'>".$str_date."</td>";
329             /* Add Status td */
330             if(preg_match("/r/",$statusACL)){
331                 $str.=" \n<td class='list0'>".$this->status[$line["status"]]."</td>";
332             }else{
333                 $str.=" \n<td class='list0'>".$no_acl."</td>";
334             }
336             /* Add sender_id td */
337             if(preg_match("/r/",$senderidACL)){
338                 $str.=" \n<td class='list0'>".htmlentities($line["sender_id"],ENT_COMPAT,'UTF-8')."</td>";
339             }else{
340                 $str.=" \n<td class='list0'>".$no_acl."</td>";
341             }
343             /* Add receiver_id td */
344             if(preg_match("/r/",$receiveridACL)){
345                 $str.=" \n<td class='list0'>".htmlentities($line["receiver_id"],ENT_COMPAT,'UTF-8')."</td>";
346             }else{
347                 $str.=" \n<td class='list0'>".$no_acl."</td>";
348             }
350             /* Add receiver_id td */
351             if(preg_match("/r/",$pagesACL)){
352                 $str.=" \n<td class='list0'>".$line["pages"]."</td>";
353             }else{
354                 $str.=" \n<td class='list0'>".$no_acl."</td>";
355             }
356             /* Create entry html str */
357             if(preg_match("/r/",$detailedViewACL)){
358                 $str.= " \n<td class='list0' style='border-right: 0pt none;'>".
359                     image('images/info_small.png', 'detail_'.postEncode($line["id"]))."</td>";
360             }else{
361                 $str.= " \n<td class='list0' style='border-right: 0pt none;'>&nbsp;</td>";
362             }
363             $report_list[] =  $str;
364         }
365         mysql_close($link);
367         $entry_count = count($report_list);
368         if($this->start >=$entry_count) $this->start =0;
370         /* Generate output */
371         $mod= 0;
372         $output= "";
373         $report_list = array_slice($report_list, $this->start, $this->range);
374         foreach ($report_list as $val){
375             $output.= "\n<tr>{$val}</tr>";
376         }
377         $smarty->assign("search_result", $output);
378         $smarty->assign("range_selector", 
379             range_selector($entry_count, $this->start, $this->range,"EntriesPerPage"));
381         // Add sorting links
382         $smarty->assign("plug", "?plug=".validate($_GET['plug']));
383         for($i= 0; $i<7; $i++){
384             $smarty->assign("mode$i", "");
385         }
386         $smarty->assign("mode".$this->sort, image("images/lists/sort-".$this->sort_direction.".png"));
387         return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
388     }
391     // Save posted filter settings
392     function save_object()
393     {
394         $faxreportfilter = session::get("faxreportfilter");
395         if(isset($_POST['EntriesPerPage'])){
396             $this->range = get_post('EntriesPerPage');
397         }
399         if (isset($_GET['start'])){
400             $this->start= (int)$_GET['start'];
401         }
403         /* Adapt sorting */
404         if (isset($_GET['sort'])){
405             if ($this->sort == (int)$_GET['sort']){
406                 if ($this->sort_direction == "down"){
407                     $this->sort_direction= "up";
408                 } else {
409                     $this->sort_direction= "down";
410                 }
411             }
412             $this->sort= (int)$_GET['sort'];
413             if ($this->sort < 0 || $this->sort > 5){
414                 $this->sort= 0;
415             }
416         }
417         foreach( array("year", "month", "search_for") as $type){
418             if (isset($_POST[$type])){
419                 $faxreportfilter[$type]= get_post($type);
421                 /* reset start page, if filter has changed */ 
422                 if(!isset($_GET['start'])){
423                     $this->start = 0;
424                 }
425             }
426             $this->$type= $faxreportfilter[$type];
428         }
429         foreach($this->attributes_SO as $name){
430             $faxreportfilter[$name] = $this->$name;
431         }  
432         session::set("faxreportfilter",$faxreportfilter);
433     }
437 class viewFaxEntries extends plugin {
439     static function plInfo()
440     {
442         return (array(
443             "plShortName"     => _("View FAX reports"),
444             "plDescription"   => _("View FAX reports")."&nbsp;<i>"._("All entries are read-only")."</i>",
445             "plRequirements"=> array(
446                 'activePlugin' => 'faxreport', 
447                 'ldapSchema' => array('goFaxAccount' => '>=1.0.4'),
448                 'onFailureDisablePlugin' => array('faxreport','gofaxAccount','goFaxServer')
449                 ),
450             "plSelfModify"    => TRUE,
451             "plDepends"       => array(),
452             "plPriority"      => 89,                                 // Position in tabs
453             "plSection"       => array("administration"),                     // This belongs to personal
454             "plCategory"      => array("users"),
455             "plOptions"       => array(),
457             "plProvidedAcls" => array(
458                 "detailedView"   => _("Detailed view and download"),
459                 "id"             => _("Fax ID"),
460                 "uid"            => _("The username"),
461                 "queuingtime"    => _("Date")."&nbsp;/&nbsp;"._("Time"),
462                 "status"         => _("Status"),
463                 "senderid"       => _("Sender ID"),
464                 "sendermsn"      => _("Sender MSN"),
465                 "receiverid"     => _("Receiver ID"),
466                 "receivermsn"    => _("Receiver MSN"),
467                 "pages"          => _("Number of pages"),
468                 "statusmessage"  => _("Status Message"),
469                 "transfertime"   => _("Transfer time"))
470             ));
471     }
475 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
476 ?>