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,1)));
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 /* The user is allowed to download all fax images from those users
194 that are listed in $_SESSION['fuserfilter']
195 Don't forget to check getfax.php if you change somthing here */
196 $_SESSION['fuserfilter']= $this->userfilter;
197 $smarty->assign("plug", "?plug=".validate($_GET['plug']));
198 $smarty->assign("detail", validate($_GET['detail']));
200 $format= _("Y-M-D");
201 $date= preg_replace("/Y/", substr($queuing_time,0,4), $format);
202 $date= preg_replace("/M/", substr($queuing_time,4,2), $date);
203 $date= preg_replace("/D/", substr($queuing_time,6,2), $date);
204 $smarty->assign("date", $date);
205 $smarty->assign("time", substr($queuing_time,8,2).":".
206 substr($queuing_time,10,2).":".
207 substr($queuing_time,12,2));
208 return($smarty->fetch(get_template_path('detail.tpl', TRUE)));
209 }
212 /************
213 Search for uids matching the filter
214 ************/
216 /* Search button has been pressed */
217 if ($this->search_for != ""){
219 if (is_integer (strpos($this->search_for, "*"))){
220 $s= $this->search_for;
221 } else {
222 $s= "*".$this->search_for."*";
223 }
224 $ldap= $this->config->get_ldap_link();
225 $ldap->cd ($this->search_base);
227 /* Perform ldap search for potential users */
228 $filter= "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))".
229 "(objectClass=goFaxAccount)".
230 "(|(uid=$s)(l=$s)(homePhone=$s)".
231 "(telephoneNumber=$s)(facsimileTelephoneNumber=$s)(mobile=$s)".
232 "(pager=$s)(cn=$s)(givenName=$s)(sn=$s)(personalTitle=$s)".
233 "(title=$s)))";
235 $res = get_list($filter, "users", $this->search_base, array("uid"), GL_SUBSEARCH );
237 /* Check if we are allowed to collect fax data */
238 $fax_users= array();
239 foreach($res as $attrs){
240 $acl = $this->ui->get_permissions($attrs['dn'],"users/user","uid");
241 if(preg_match("/r/",$acl)){
242 $fax_users[ $attrs['dn']]= $attrs["uid"][0];
243 }
244 }
246 $this->fax_users = $fax_users;
248 /* Prepare SQL query */
249 $this->userfilter= "";
250 foreach ($fax_users as $user){
251 $this->userfilter.= "uid = '$user' OR ";
252 }
253 $this->userfilter= preg_replace("/OR $/", "", $this->userfilter);
254 }
256 /************
257 Create filter
258 ************/
260 /* Perform SQL query */
261 if ($this->userfilter){
262 if ($this->sort_direction == "down"){
263 $desc= "DESC";
264 } else {
265 $desc= "";
266 }
267 $start= date ("YmdHis", mktime(0,0,0,$this->month,1,$this->year));
268 $end= date ("YmdHis", mktime(23,59,59,$this->month+1,0,$this->year));
269 $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 ".
270 "WHERE ( ".$this->userfilter." ) AND queuing_time <= $end AND ".
271 "queuing_time >= $start ORDER BY ".$this->fields[$this->sort]." $desc;";
273 if(!is_callable("mysql_connect")){
274 print_red("There is no mysql extension configured in your php setup.");
275 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
276 }
279 /************
280 Create results
281 ************/
283 /* Connecting, selecting database */
284 $cfg= $this->config->data['SERVERS']['FAX'];
285 $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
287 @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query");
288 $result = @mysql_query($query);
289 if ($result === false){
290 print_red(_("Query for fax database failed!"));
291 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
292 }
294 $this->report_list= array();
295 $user_ids = array_flip($fax_users);
296 while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
298 /* Check permissions for each field */
299 $dn = $user_ids[$line['uid']];
301 /* Hide restricted attributes */
302 foreach(array("pages","receiverid","senderid","status","queuingtime","detailedView") as $attr){
303 $var = $attr."ACL";
304 $$var = $this->ui->get_permissions($dn,"faxreport/faxreport",$attr);
305 }
307 /* Restricted attributes will not be displayed, this will be displayed instead */
308 $no_acl = "<img class='center' src='images/closedlock.png'
309 title='"._("Insufficient permissions to view this attribute")."' alt='"._("Insufficient permissions")."'>";
311 /* Create date */
312 if((!empty($line["queuing_time"])) && preg_match("/r/",$queuingtimeACL)){
313 $hour= substr($line["queuing_time"], 8, 2);
314 $minute=substr($line["queuing_time"], 10, 2);
315 $format= _("Y-M-D");
316 $date= preg_replace("/Y/", substr($line["queuing_time"], 0, 4), $format);
317 $date= preg_replace("/M/", substr($line["queuing_time"], 4, 2), $date);
318 $date= preg_replace("/D/", substr($line["queuing_time"], 6, 2), $date);
319 $str_date = $date." ".$hour.":".$minute;
320 }else{
321 $str_date = $no_acl;
322 }
324 /* Create entry html str */
326 if(preg_match("/r/",$detailedViewACL)){
327 $str = "<td class=\"phonelist\">
328 <a href=\"main.php?plug=".validate($_GET['plug'])."&detail=".$line["id"]."\">
329 <img class='center' alt=\"\" align=\"middle\" border=0 src=\"".get_template_path('images/info_small.png')."\">
330 ".$line["uid"].
331 "</a>
332 </td>
333 <td>$str_date</td>";
334 }else{
335 $str = "<td>
336 <img class='center' alt=\"\" align=\"middle\" border=0 src=\"".get_template_path('images/info_small.png')."\">
337 ".$line["uid"]."</td>
338 <td>$str_date</td>";
339 }
341 /* Add Status td */
342 if(preg_match("/r/",$statusACL)){
343 $str.="<td>".$this->status[$line["status"]]."</td>";
344 }else{
345 $str.="<td>".$no_acl."</td>";
346 }
348 /* Add sender_id td */
349 if(preg_match("/r/",$senderidACL)){
350 $str.="<td>".$line["sender_id"]."</td>";
351 }else{
352 $str.="<td>".$no_acl."</td>";
353 }
355 /* Add receiver_id td */
356 if(preg_match("/r/",$receiveridACL)){
357 $str.="<td>".$line["receiver_id"]."</td>";
358 }else{
359 $str.="<td>".$no_acl."</td>";
360 }
362 /* Add receiver_id td */
363 if(preg_match("/r/",$pagesACL)){
364 $str.="<td>".$line["pages"]."</td>";
365 }else{
366 $str.="<td>".$no_acl."</td>";
367 }
368 $this->report_list[] = $str;
369 }
371 mysql_close($link);
372 }
374 /************
375 Create output out of results
376 ************/
378 /* Generate output */
379 $mod= 0;
380 $output= "";
381 foreach ($this->report_list as $val){
382 if ($mod < $this->start) {
383 $mod++;
384 continue;
385 }
386 if ($mod >= ($this->start + $this->range)){
387 $mod++;
388 break;
389 }
390 if ( ($mod++) & 1){
391 $col= "background-color: #ECECEC;";
392 } else {
393 $col= "background-color: #F5F5F5;";
394 }
395 $output.= "<tr style=\"height:22px; $col\">$val</tr>";
396 }
399 /************
400 Display results
401 ************/
403 if (isset($fax_users) && count($fax_users)){
404 $smarty->assign("search_result", $output);
405 $smarty->assign("range_selector", range_selector(count($this->report_list), $this->start, $this->range,"EntriesPerPage"));
406 }else{
407 $smarty->assign("search_result", "");
408 }
410 /* Show main page */
411 $smarty->assign("plug", "?plug=".validate($_GET['plug']));
412 for($i= 0; $i<7; $i++){
413 $smarty->assign("mode$i", "");
414 }
415 $smarty->assign("mode".$this->sort, "<img alt=\"\" src=\"images/sort_".$this->sort_direction.
416 ".png\" border=0 align=middle>");
417 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
418 }
421 /* Save ui input, and store it in $_SESSION
422 to remember last search next time*/
423 function save_object()
424 {
425 $faxreportfilter = get_global("faxreportfilter");
426 if(isset($_POST['EntriesPerPage'])){
427 $this->range = $_POST['EntriesPerPage'];
428 }
430 if (isset($_GET['start'])){
431 $this->start= (int)$_GET['start'];
432 }
434 /* Adapt sorting */
435 if (isset($_GET['sort'])){
436 if ($this->sort == (int)$_GET['sort']){
437 if ($this->sort_direction == "down"){
438 $this->sort_direction= "up";
439 } else {
440 $this->sort_direction= "down";
441 }
442 }
443 $this->sort= (int)$_GET['sort'];
444 if ($this->sort < 0 || $this->sort > 5){
445 $this->sort= 0;
446 }
447 }
448 foreach( array("year", "month", "search_for", "search_base") as $type){
449 if (isset($_POST[$type])){
450 $faxreportfilter[$type]= $_POST[$type];
452 /* reset start page, if filter has changed */
453 if(!isset($_GET['start'])){
454 $this->start = 0;
455 }
456 }
457 $this->$type= $faxreportfilter[$type];
459 }
460 foreach($this->attributes_SO as $name){
461 $faxreportfilter[$name] = $this->$name;
462 }
463 register_global("faxreportfilter",$faxreportfilter);
464 }
467 /* Return plugin informations for acl handling
468 #FIXME You can only read attributes within this report plugin */
469 function plInfo()
470 {
472 return (array(
473 "plShortName" => _("Fax report"),
474 "plDescription" => _("Fax report")." <i>"._("All entries are readonly")."</i>",
475 "plSelfModify" => TRUE,
476 "plDepends" => array(),
477 "plPriority" => 1, // Position in tabs
478 "plSection" => array("administration"), // This belongs to personal
479 "plCategory" => array("faxreport" => array("description" => _("Fax reports"),
480 "objectClass" => array())),
481 "plOptions" => array(),
483 "plProvidedAcls" => array(
484 "detailedView" => _("Detailed view"),
485 "id" => _("Fax ID"),
486 "queuingtime" => _("Date")." / "._("Time"),
487 "status" => _("Status"),
488 "senderid" => _("Sender ID"),
489 "sendermsn" => _("Sender MSN"),
490 "receiverid" => _("Receiver ID"),
491 "receivermsn" => _("Receiver MSN"),
492 "pages" => _("Number of pages"),
493 "statusmessage" => _("Status Message"),
494 "transfertime" => _("Transfer time"))
495 ));
496 }
498 }
499 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
500 ?>