1 <?php
3 class faxreport extends plugin
4 {
5 /* Definitions */
6 var $plHeadline = "FAX Reports";
7 var $plDescription = "This does something";
9 /* For internal use */
10 var $start = 0;
11 var $search_for = "*";
12 var $search_base = "";
13 var $year = "";
14 var $month = "";
15 var $sort = 1;
16 var $sort_direction = "down";
17 var $report_list = array();
18 var $ui = NULL;
19 var $range = 20;
21 /* Constant stuff */
22 var $status= array( "SENT", "MAILED", "SERROR", "RERROR", "SBLOCK", "RBLOCK",
23 "DELETED", "REQUEUED", "DISABLED", "PRINTED", "DIVERTED",
24 "UNDEFINED", "UNDEFINED", "UNDEFINED", "UNDEFINED",
25 "UNDEFINED");
26 var $fields= array("uid", "queuing_time", "status", "sender_id", "receiver_id", "pages");
28 /* these vars will be stored in session to be able to remember last search config */
29 var $attributes_SO= array("search_for","search_base","month","year","start","year","month","sort","sort_direction","range");
30 var $objectclasses= array();
31 var $fax_users = array();
34 /* Create class */
35 function faxreport ($config, $ui)
36 {
37 /* Include config object */
38 $this->config = $config;
39 $this->ui = $ui;
40 $this->search_base = get_base_from_people($ui->dn);
41 $this->year = date("Y");
42 $this->month = date("m");
44 /* Get global filter config and set class vars ,
45 or create a filter */
46 if (!is_global("faxreportfilter")){
47 $faxreportfilter = array();
48 foreach($this->attributes_SO as $name){
49 $faxreportfilter[$name] = $this->$name;
50 }
51 register_global("faxreportfilter",$faxreportfilter);
52 }else{
53 $faxreportfilter = get_global("faxreportfilter");
54 foreach($this->attributes_SO as $name){
55 $this->$name = $faxreportfilter[$name];
56 }
57 }
58 }
61 /* Create Filter & Search & Display results */
62 function execute()
63 {
64 /* Call parent execute */
65 plugin::execute();
67 /************
68 Variable initialisation
69 ************/
71 /* Create months */
72 $months= array();
73 for($i = 1 ; $i <= 12 ; $i ++ ){
74 $months[$i] = _(date("F",gmmktime(0,0,0,$i)));
75 }
77 /* Create years */
78 $current= date("Y");
79 $years= array();
80 for ($y= $current - 5; $y<=$current; $y++){
81 $years[]= $y;
82 }
85 /************
86 Set smarty defaults
87 ************/
89 $smarty= get_smarty();
90 $smarty->assign("launchimage" , get_template_path('images/launch.png'));
91 $smarty->assign("search_image" , get_template_path('images/search.png'));
92 $smarty->assign("search_for" , $this->search_for);
93 $smarty->assign("bases" , $this->config->idepartments);
94 $smarty->assign("base_select" , $this->search_base);
95 $smarty->assign("months" , $months);
96 $smarty->assign("month_select" , $this->month);
97 $smarty->assign("years" , $years);
98 $smarty->assign("year_select" , $this->year);
99 $smarty->assign("search_result" , "");
102 /************
103 Check database accessibility
104 ************/
106 /* Some checks */
107 if(!isset($this->config->data['SERVERS']['FAX'])){
108 print_red(_("No fax extension defined in your server configuration, no reports can be shown!"));
109 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
110 }elseif(!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('contents.tpl', TRUE)));
113 }else{
114 /* Connecting, selecting database */
115 $cfg = $this->config->data['SERVERS']['FAX'];
116 $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
117 if ($link === FALSE){
118 print_red(_("Can't connect to fax database, no reports can be shown!"));
119 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
120 }
121 if (! @mysql_select_db("gofax")){
122 print_red(_("Can't select fax database for report generation!"));
123 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
124 }
126 if (! mysql_query("SELECT * FROM faxlog;")){
127 print_red(_("Can't query fax table 'faxlog' for report generation!"));
128 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
129 }
130 }
133 /************
134 Perform a deatil view
135 ************/
137 /* Do detail view? */
138 if (isset($_GET['detail'])){
140 /* Create query */
141 $query = "SELECT id,uid,date_format(queuing_time, '%Y%m%d%H%i%s') as queuing_time,status,sender_id,sender_msn,receiver_id,".
142 "receiver_msn,pages,status_message,transfer_time FROM faxlog WHERE id=".$_GET['detail'].";";
144 /* Connecting, selecting database */
145 $cfg= $this->config->data['SERVERS']['FAX'];
147 /* Check if everything went ok*/
148 $result = @mysql_query($query);
149 if ($result === false){
150 print_red(_("Query for fax database failed!"));
151 @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query failed");
152 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
153 }
155 $line = mysql_fetch_array($result, MYSQL_ASSOC);
156 mysql_close($link);
158 if (!preg_match ("/'".$line["uid"]."'/", $this->userfilter)){
159 print_red (_("You have no permission to retrieve informations about this fax id!"));
160 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
161 }
163 /* Check acls */
164 $fax_uids = array_flip($this->fax_users);
165 $uid = $line['uid'];
166 $dn = $fax_uids[$uid];
167 $acls = $this->ui->get_permissions($dn,"faxreport/faxreport");
168 if(!preg_match("/r/",$acls)){
169 print_red (_("You have no permission to retrieve informations about this fax id!"));
170 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
171 }
173 $parts= array( "id", "uid", "queuing_time", "status", "sender_id", "sender_msn",
174 "receiver_id", "receiver_msn", "pages", "status_message", "transfer_time" );
176 foreach ($parts as $vname) {
178 $final="fax_$vname";
179 if($vname != "uid"){
180 $v_acl = $this->ui->get_permissions($dn,"faxreport/faxreport",preg_replace("/_/","",$vname));
181 }else{
182 $v_acl = "r";
183 }
185 if ($line[$vname] != "" && preg_match("/r/",$v_acl)){
186 $smarty->assign("$final", $line[$vname]);
187 } else {
188 $smarty->assign("$final", "-");
189 }
190 }
191 $queuing_time= $line['queuing_time'];
193 $_SESSION['fuserfilter']= $this->userfilter;
194 $smarty->assign("plug", "?plug=".validate($_GET['plug']));
195 $smarty->assign("detail", validate($_GET['detail']));
197 $format= _("Y-M-D");
198 $date= preg_replace("/Y/", substr($queuing_time,0,4), $format);
199 $date= preg_replace("/M/", substr($queuing_time,4,2), $date);
200 $date= preg_replace("/D/", substr($queuing_time,6,2), $date);
201 $smarty->assign("date", $date);
202 $smarty->assign("time", substr($queuing_time,8,2).":".
203 substr($queuing_time,10,2).":".
204 substr($queuing_time,12,2));
205 return($smarty->fetch(get_template_path('detail.tpl', TRUE)));
206 }
209 /************
210 Search for uids matching the filter
211 ************/
213 /* Search button has been pressed */
214 if ($this->search_for != ""){
216 if (is_integer (strpos($this->search_for, "*"))){
217 $s= $this->search_for;
218 } else {
219 $s= "*".$this->search_for."*";
220 }
221 $ldap= $this->config->get_ldap_link();
222 $ldap->cd ($this->search_base);
224 /* Perform ldap search for potential users */
225 $filter= "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))".
226 "(objectClass=goFaxAccount)".
227 "(|(uid=$s)(l=$s)(homePhone=$s)".
228 "(telephoneNumber=$s)(facsimileTelephoneNumber=$s)(mobile=$s)".
229 "(pager=$s)(cn=$s)(givenName=$s)(sn=$s)(personalTitle=$s)".
230 "(title=$s)))";
232 $res = get_list($filter, "users", $this->search_base, array("uid"), GL_SUBSEARCH );
234 /* Check if we are allowed to collect fax data */
235 $fax_users= array();
236 foreach($res as $attrs){
237 $acl = $this->ui->get_permissions($attrs['dn'],"users/user","uid");
238 if(preg_match("/r/",$acl)){
239 $fax_users[ $attrs['dn']]= $attrs["uid"][0];
240 }
241 }
243 $this->fax_users = $fax_users;
245 /* Prepare SQL query */
246 $this->userfilter= "";
247 foreach ($fax_users as $user){
248 $this->userfilter.= "uid = '$user' OR ";
249 }
250 $this->userfilter= preg_replace("/OR $/", "", $this->userfilter);
251 }
253 /************
254 Create filter
255 ************/
257 /* Perform SQL query */
258 if ($this->userfilter){
259 if ($this->sort_direction == "down"){
260 $desc= "DESC";
261 } else {
262 $desc= "";
263 }
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 = "SELECT id,uid,date_format(queuing_time, '%Y%m%d%H%i%s') as queuing_time,status,sender_id,receiver_id,pages FROM faxlog ".
267 "WHERE ( ".$this->userfilter." ) AND queuing_time <= $end AND ".
268 "queuing_time >= $start ORDER BY ".$this->fields[$this->sort]." $desc;";
270 if(!is_callable("mysql_connect")){
271 print_red("There is no mysql extension configured in your php setup.");
272 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
273 }
276 /************
277 Create results
278 ************/
280 /* Connecting, selecting database */
281 $cfg= $this->config->data['SERVERS']['FAX'];
282 $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
284 @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query");
285 $result = @mysql_query($query);
286 if ($result === false){
287 print_red(_("Query for fax database failed!"));
288 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
289 }
291 $this->report_list= array();
292 $user_ids = array_flip($fax_users);
293 while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
295 /* Check permissions for each field */
296 $dn = $user_ids[$line['uid']];
298 /* Hide restricted attributes */
299 foreach(array("pages","receiverid","senderid","status","queuingtime","detailedView") as $attr){
300 $var = $attr."ACL";
301 $$var = $this->ui->get_permissions($dn,"faxreport/faxreport",$attr);
302 }
304 /* Restricted attributes will not be displayed, this will be displayed instead */
305 $no_acl = "<img class='center' src='images/closedlock.png'
306 title='"._("Insufficient permissions to view this attribute")."' alt='"._("Insufficient permissions")."'>";
308 /* Create date */
309 if((!empty($line["queuing_time"])) && preg_match("/r/",$queuingtimeACL)){
310 $hour= substr($line["queuing_time"], 8, 2);
311 $minute=substr($line["queuing_time"], 10, 2);
312 $format= _("Y-M-D");
313 $date= preg_replace("/Y/", substr($line["queuing_time"], 0, 4), $format);
314 $date= preg_replace("/M/", substr($line["queuing_time"], 4, 2), $date);
315 $date= preg_replace("/D/", substr($line["queuing_time"], 6, 2), $date);
316 $str_date = $date." ".$hour.":".$minute;
317 }else{
318 $str_date = $no_acl;
319 }
321 /* Create entry html str */
323 if(preg_match("/r/",$detailedViewACL)){
324 $str = "<td class=\"phonelist\">
325 <a href=\"main.php?plug=".validate($_GET['plug'])."&detail=".$line["id"]."\">
326 <img class='center' alt=\"\" align=\"middle\" border=0 src=\"".get_template_path('images/info_small.png')."\">
327 ".$line["uid"].
328 "</a>
329 </td>
330 <td>$str_date</td>";
331 }else{
332 $str = "<td>
333 <img class='center' alt=\"\" align=\"middle\" border=0 src=\"".get_template_path('images/info_small.png')."\">
334 ".$line["uid"]."</td>
335 <td>$str_date</td>";
336 }
338 /* Add Status td */
339 if(preg_match("/r/",$statusACL)){
340 $str.="<td>".$this->status[$line["status"]]."</td>";
341 }else{
342 $str.="<td>".$no_acl."</td>";
343 }
345 /* Add sender_id td */
346 if(preg_match("/r/",$senderidACL)){
347 $str.="<td>".$line["sender_id"]."</td>";
348 }else{
349 $str.="<td>".$no_acl."</td>";
350 }
352 /* Add receiver_id td */
353 if(preg_match("/r/",$receiveridACL)){
354 $str.="<td>".$line["receiver_id"]."</td>";
355 }else{
356 $str.="<td>".$no_acl."</td>";
357 }
359 /* Add receiver_id td */
360 if(preg_match("/r/",$pagesACL)){
361 $str.="<td>".$line["pages"]."</td>";
362 }else{
363 $str.="<td>".$no_acl."</td>";
364 }
365 $this->report_list[] = $str;
366 }
368 mysql_close($link);
369 }
371 /************
372 Create output out of results
373 ************/
375 /* Generate output */
376 $mod= 0;
377 $output= "";
378 foreach ($this->report_list as $val){
379 if ($mod < $this->start) {
380 $mod++;
381 continue;
382 }
383 if ($mod >= ($this->start + $this->range)){
384 $mod++;
385 break;
386 }
387 if ( ($mod++) & 1){
388 $col= "background-color: #ECECEC;";
389 } else {
390 $col= "background-color: #F5F5F5;";
391 }
392 $output.= "<tr style=\"height:22px; $col\">$val</tr>";
393 }
396 /************
397 Display results
398 ************/
400 if (isset($fax_users) && count($fax_users)){
401 $smarty->assign("search_result", $output);
402 $smarty->assign("range_selector", range_selector(count($this->report_list), $this->start, $this->range,"EntriesPerPage"));
403 }else{
404 $smarty->assign("search_result", "");
405 }
407 /* Show main page */
408 $smarty->assign("plug", "?plug=".validate($_GET['plug']));
409 for($i= 0; $i<7; $i++){
410 $smarty->assign("mode$i", "");
411 }
412 $smarty->assign("mode".$this->sort, "<img alt=\"\" src=\"images/sort_".$this->sort_direction.
413 ".png\" border=0 align=middle>");
414 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
415 }
418 /* Save ui input, and store it in $_SESSION
419 to remember last search next time*/
420 function save_object()
421 {
422 $faxreportfilter = get_global("faxreportfilter");
423 if(isset($_POST['EntriesPerPage'])){
424 $this->range = $_POST['EntriesPerPage'];
425 }
427 if (isset($_GET['start'])){
428 $this->start= (int)$_GET['start'];
429 }
431 /* Adapt sorting */
432 if (isset($_GET['sort'])){
433 if ($this->sort == (int)$_GET['sort']){
434 if ($this->sort_direction == "down"){
435 $this->sort_direction= "up";
436 } else {
437 $this->sort_direction= "down";
438 }
439 }
440 $this->sort= (int)$_GET['sort'];
441 if ($this->sort < 0 || $this->sort > 5){
442 $this->sort= 0;
443 }
444 }
445 foreach( array("year", "month", "search_for", "search_base") as $type){
446 if (isset($_POST[$type])){
447 $faxreportfilter[$type]= $_POST[$type];
449 /* reset start page, if filter has changed */
450 if(!isset($_GET['start'])){
451 $this->start = 0;
452 }
453 }
454 $this->$type= $faxreportfilter[$type];
456 }
457 foreach($this->attributes_SO as $name){
458 $faxreportfilter[$name] = $this->$name;
459 }
460 register_global("faxreportfilter",$faxreportfilter);
461 }
464 /* Return plugin informations for acl handling
465 #FIXME You can only read attributes within this report plugin */
466 function plInfo()
467 {
469 return (array(
470 "plShortName" => _("Fax report"),
471 "plDescription" => _("Fax report")." <i>"._("All entries are readonly")."</i>",
472 "plSelfModify" => TRUE,
473 "plDepends" => array(),
474 "plPriority" => 1, // Position in tabs
475 "plSection" => array("administration"), // This belongs to personal
476 "plCategory" => array("faxreport" => array("description" => _("Fax reports"),
477 "objectClass" => array())),
478 "plOptions" => array(),
480 "plProvidedAcls" => array(
481 "detailedView" => _("Detailed view"),
482 "id" => _("Fax ID"),
483 "queuingtime" => _("Date")." / "._("Time"),
484 "status" => _("Status"),
485 "senderid" => _("Sender ID"),
486 "sendermsn" => _("Sender MSN"),
487 "receiverid" => _("Receiver ID"),
488 "receivermsn" => _("Receiver MSN"),
489 "pages" => _("Number of pages"),
490 "statusmessage" => _("Status Message"),
491 "transfertime" => _("Transfer time"))
492 ));
493 }
495 }
496 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
497 ?>