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();
32 /* Create class */
33 function faxreport ($config, $ui)
34 {
35 /* Include config object */
36 $this->config = $config;
37 $this->ui = $ui;
38 $this->search_base = get_base_from_people($ui->dn);
39 $this->year = date("Y");
40 $this->month = date("m");
42 /* Get global filter config and set class vars ,
43 or create a filter */
44 if (!is_global("faxreportfilter")){
45 $faxreportfilter = array();
46 foreach($this->attributes_SO as $name){
47 $faxreportfilter[$name] = $this->$name;
48 }
49 register_global("faxreportfilter",$faxreportfilter);
50 }else{
51 $faxreportfilter = get_global("faxreportfilter");
52 foreach($this->attributes_SO as $name){
53 $this->$name = $faxreportfilter[$name];
54 }
55 }
56 }
59 /* Create Filter & Search & Display results */
60 function execute()
61 {
62 /* Call parent execute */
63 plugin::execute();
65 /************
66 Variable initialisation
67 ************/
69 /* Create months */
70 $months= array();
71 for($i = 1 ; $i <= 12 ; $i ++ ){
72 $months[$i] = _(date("F",gmmktime(0,0,0,$i)));
73 }
75 /* Create years */
76 $current= date("Y");
77 $years= array();
78 for ($y= $current - 5; $y<=$current; $y++){
79 $years[]= $y;
80 }
83 /************
84 Set smarty defaults
85 ************/
87 $smarty= get_smarty();
88 $smarty->assign("launchimage" , get_template_path('images/launch.png'));
89 $smarty->assign("search_image" , get_template_path('images/search.png'));
90 $smarty->assign("search_for" , $this->search_for);
91 $smarty->assign("bases" , $this->config->idepartments);
92 $smarty->assign("base_select" , $this->search_base);
93 $smarty->assign("months" , $months);
94 $smarty->assign("month_select" , $this->month);
95 $smarty->assign("years" , $years);
96 $smarty->assign("year_select" , $this->year);
97 $smarty->assign("search_result" , "");
100 /************
101 Check database accessibility
102 ************/
104 /* Some checks */
105 if(!isset($this->config->data['SERVERS']['FAX'])){
106 print_red(_("Can't connect to fax database, no reports can be shown!"));
107 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
108 }elseif(!is_callable("mysql_connect")){
109 print_red(_("There is no mysql extension available, please check your php setup."));
110 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
111 }else{
112 /* Connecting, selecting database */
113 $cfg = $this->config->data['SERVERS']['FAX'];
114 $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
115 if ($link === FALSE){
116 print_red(_("Can't connect to fax database, no reports can be shown!"));
117 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
118 }
119 if (! @mysql_select_db("gofax")){
120 print_red(_("Can't select fax database for report generation!"));
121 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
122 }
124 if (! mysql_query("SELECT * FROM faxlog;")){
125 print_red(_("Can't select fax database for report generation!"));
126 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
127 }
128 }
131 /************
132 Perform a deatil view
133 ************/
135 /* Do detail view? */
136 if (isset($_GET['detail'])){
138 /* Create query */
139 $query = "SELECT id,uid,date_format(queuing_time, '%Y%m%d%H%i%s') as queuing_time,status,sender_id,sender_msn,receiver_id,".
140 "receiver_msn,pages,status_message,transfer_time FROM faxlog WHERE id=".$_GET['detail'].";";
142 /* Connecting, selecting database */
143 $cfg= $this->config->data['SERVERS']['FAX'];
145 /* Check if everything went ok*/
146 $result = @mysql_query($query);
147 if ($result === false){
148 print_red(_("Query for fax database failed!"));
149 @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query failed");
150 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
151 }
153 $line = mysql_fetch_array($result, MYSQL_ASSOC);
154 mysql_close($link);
156 if (!preg_match ("/'".$line["uid"]."'/", $this->userfilter)){
157 print_red (_("You have no permission to retrieve informations about this fax id!"));
158 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
159 }
161 $parts= array( "id", "uid", "queuing_time", "status", "sender_id", "sender_msn",
162 "receiver_id", "receiver_msn", "pages", "status_message", "transfer_time" );
163 foreach ($parts as $vname) {
164 $final="fax_$vname";
165 if ($line[$vname] != ""){
166 $smarty->assign("$final", $line[$vname]);
167 } else {
168 $smarty->assign("$final", "-");
169 }
170 }
171 $queuing_time= $line['queuing_time'];
173 $_SESSION['fuserfilter']= $this->userfilter;
174 $smarty->assign("plug", "?plug=".validate($_GET['plug']));
175 $smarty->assign("detail", validate($_GET['detail']));
177 $format= _("Y-M-D");
178 $date= preg_replace("/Y/", substr($queuing_time,0,4), $format);
179 $date= preg_replace("/M/", substr($queuing_time,4,2), $date);
180 $date= preg_replace("/D/", substr($queuing_time,6,2), $date);
181 $smarty->assign("date", $date);
182 $smarty->assign("time", substr($queuing_time,8,2).":".
183 substr($queuing_time,10,2).":".
184 substr($queuing_time,12,2));
185 return($smarty->fetch(get_template_path('detail.tpl', TRUE)));
186 }
189 /************
190 Search for uids matching the filter
191 ************/
193 /* Search button has been pressed */
194 if ($this->search_for != ""){
196 if (is_integer (strpos($this->search_for, "*"))){
197 $s= $this->search_for;
198 } else {
199 $s= "*".$this->search_for."*";
200 }
201 $ldap= $this->config->get_ldap_link();
202 $ldap->cd ($this->search_base);
204 /* Perform ldap search for potential users */
205 $ldap->search ("(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))".
206 "(objectClass=goFaxAccount)".
207 "(|(uid=$s)(l=$s)(homePhone=$s)".
208 "(telephoneNumber=$s)(facsimileTelephoneNumber=$s)(mobile=$s)".
209 "(pager=$s)(cn=$s)(givenName=$s)(sn=$s)(personalTitle=$s)".
210 "(title=$s)))");
212 $fax_users= array();
213 while ($attrs= $ldap->fetch()){
214 $ui = get_userinfo();
215 $acl= get_permissions ($ui->dn, $ui->subtreeACL);
216 $acl2= get_module_permission($acl, "faxreport", $attrs['dn']);
218 if (chkacl ($acl2, "faxreport") == ""){
219 $fax_users[]= $attrs["uid"][0];
220 }
221 }
223 /* Prepare SQL query */
224 $this->userfilter= "";
225 foreach ($fax_users as $user){
226 $this->userfilter.= "uid = '$user' OR ";
227 }
228 $this->userfilter= preg_replace("/OR $/", "", $this->userfilter);
229 }
232 /************
233 Create filter
234 ************/
236 /* Perform SQL query */
237 if ($this->userfilter){
238 if ($this->sort_direction == "down"){
239 $desc= "DESC";
240 } else {
241 $desc= "";
242 }
243 $start= date ("YmdHis", mktime(0,0,0,$this->month,1,$this->year));
244 $end= date ("YmdHis", mktime(23,59,59,$this->month+1,0,$this->year));
245 $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 ".
246 "WHERE ( ".$this->userfilter." ) AND queuing_time <= $end AND ".
247 "queuing_time >= $start ORDER BY ".$this->fields[$this->sort]." $desc;";
249 if(!is_callable("mysql_connect")){
250 print_red("There is no mysql extension configured in your php setup.");
251 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
252 }
255 /************
256 Create results
257 ************/
259 /* Connecting, selecting database */
260 $cfg= $this->config->data['SERVERS']['FAX'];
261 $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
263 @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query");
264 $result = @mysql_query($query);
265 if ($result === false){
266 print_red(_("Query for fax database failed!"));
267 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
268 }
270 $report_list= array();
271 while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
272 $hour= substr($line["queuing_time"], 8, 2);
273 $minute=substr($line["queuing_time"], 10, 2);
274 $format= _("Y-M-D");
275 $date= preg_replace("/Y/", substr($line["queuing_time"], 0, 4), $format);
276 $date= preg_replace("/M/", substr($line["queuing_time"], 4, 2), $date);
277 $date= preg_replace("/D/", substr($line["queuing_time"], 6, 2), $date);
280 $report_list[]= "<td class=\"phonelist\"><a href=\"main.php?plug=".validate($_GET['plug'])."&detail=".
281 $line["id"]."\"><img alt=\"\" align=\"middle\" border=0 src=\"".get_template_path('images/info_small.png')."\"> ".$line["uid"]."</a></td>".
282 "<td>$date $hour:$minute</td>".
283 "<td>".$this->status[$line["status"]]."</td>".
284 "<td>".$line["sender_id"]."</td>".
285 "<td>".$line["receiver_id"]."</td>".
286 "<td>".$line["pages"]."</td>";
287 }
289 $this->report_list= $report_list;
290 mysql_close($link);
291 }
294 /************
295 Create output out of results
296 ************/
298 /* Generate output */
299 $mod= 0;
300 $output= "";
301 foreach ($this->report_list as $val){
302 if ($mod < $this->start) {
303 $mod++;
304 continue;
305 }
306 if ($mod >= ($this->start + $this->range)){
307 $mod++;
308 break;
309 }
310 if ( ($mod++) & 1){
311 $col= "background-color: #ECECEC;";
312 } else {
313 $col= "background-color: #F5F5F5;";
314 }
315 $output.= "<tr style=\"height:22px; $col\">$val</tr>";
316 }
319 /************
320 Display results
321 ************/
323 if (isset($fax_users) && count($fax_users)){
324 $smarty->assign("search_result", $output);
325 $smarty->assign("range_selector", range_selector(count($this->report_list), $this->start, $this->range,"EntriesPerPage"));
326 }else{
327 $smarty->assign("search_result", "");
328 }
330 /* Show main page */
331 $smarty->assign("plug", "?plug=".validate($_GET['plug']));
332 for($i= 0; $i<7; $i++){
333 $smarty->assign("mode$i", "");
334 }
335 $smarty->assign("mode".$this->sort, "<img alt=\"\" src=\"images/sort_".$this->sort_direction.
336 ".png\" border=0 align=middle>");
337 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
338 }
341 /* Save ui input, and store it in $_SESSION
342 to remember last search next time*/
343 function save_object()
344 {
345 $faxreportfilter = get_global("faxreportfilter");
346 if(isset($_POST['EntriesPerPage'])){
347 $this->range = $_POST['EntriesPerPage'];
348 }
350 if (isset($_GET['start'])){
351 $this->start= (int)$_GET['start'];
352 }
354 /* Adapt sorting */
355 if (isset($_GET['sort'])){
356 if ($this->sort == (int)$_GET['sort']){
357 if ($this->sort_direction == "down"){
358 $this->sort_direction= "up";
359 } else {
360 $this->sort_direction= "down";
361 }
362 }
363 $this->sort= (int)$_GET['sort'];
364 if ($this->sort < 0 || $this->sort > 5){
365 $this->sort= 0;
366 }
367 }
368 foreach( array("year", "month", "search_for", "search_base") as $type){
369 if (isset($_POST[$type])){
370 $faxreportfilter[$type]= $_POST[$type];
371 }
372 $this->$type= $faxreportfilter[$type];
373 }
374 foreach($this->attributes_SO as $name){
375 $faxreportfilter[$name] = $this->$name;
376 }
377 register_global("faxreportfilter",$faxreportfilter);
378 }
381 /* Return plugin informations for acl handling
382 #FIXME You can only read attributes within this report plugin */
383 function plInfo()
384 {
385 return (array(
386 "plShortName" => _("Fax report"),
387 "plDescription" => _("Fax report"),
388 "plSelfModify" => TRUE,
389 "plDepends" => array(),
390 "plPriority" => 1, // Position in tabs
391 "plSection" => "FAX Reports", // This belongs to personal
392 "plCategory" => array("fax"),
393 "plOptions" => array(),
395 "plProvidedAcls" => array(
396 "detailedView" => _("Detailed view"))
397 ));
398 }
400 }
401 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
402 ?>