1 <?php
3 class fonreport extends plugin
4 {
5 /* Definitions */
6 var $plHeadline= "Phone 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 $fields = array("calldate", "src", "dst", "channel", "lastapp", "disposition", "duration");
14 var $year = "";
15 var $month = "";
16 var $sort = 0;
17 var $sort_direction = "down";
18 var $report_list = array();
19 var $userfilter = "";
20 var $ui = NULL;
21 var $range = 20;
22 var $EntryPerPage = 20;
23 var $selected_server = "";
25 /* attribute list for save action */
26 var $attributes_SO = array("start","search_for","search_base","range","month","sort_direction","sort","year");
27 var $objectclasses = array();
30 /* Construct class */
31 function fonreport ($config, $ui)
32 {
33 /* Include config object */
34 $this->config = $config;
35 $this->ui = $ui;
36 $this->search_base= get_base_from_people($ui->dn);
38 $this->month = date("m");
39 $this->year = date("Y");
41 /* Use filter settings if we have already searched */
42 if (!is_global("fonfilter")){
43 $fonfilter = array();
44 foreach($this->attributes_SO as $name){
45 $fonfilter[$name]=$this->$name;
46 }
47 register_global("fonfilter", $fonfilter);
48 }else{
49 $fonfilter = get_global("fonfilter");
50 foreach($this->attributes_SO as $name){
51 $this->$name = $fonfilter[$name];
52 }
53 }
54 }
57 /* Save ui interactions and store results in session,
58 to remember settings */
59 function save_object()
60 {
61 $fonfilter= get_global("fonfilter");
62 if(isset($_POST['EntryPerPage'])){
63 $this->range = $_POST['EntryPerPage'];
64 }
65 if (isset($_GET['start'])){
66 $this->start= (int)$_GET['start'];
67 }
68 foreach( array("year", "month", "search_for", "search_base","selected_server") as $type){
69 if (isset($_POST[$type])){
70 $this->$type= $_POST[$type];
71 }
72 }
74 /* Adapt sorting */
75 if (isset($_GET['sort'])){
76 if ($this->sort == (int)$_GET['sort']){
77 if ($this->sort_direction == "down"){
78 $this->sort_direction= "up";
79 } else {
80 $this->sort_direction= "down";
81 }
82 }
83 $this->sort= (int)$_GET['sort'];
84 if ($this->sort < 0 || $this->sort > 6){
85 $this->sort= 0;
86 }
87 }
89 /* remove unwanted tags */
90 $this->search_for = stripslashes(preg_replace("/[^0-9a-z\*\+ \-]/i","",$this->search_for));
92 foreach($this->attributes_SO as $name){
93 $fonfilter[$name] = $this->$name;
94 }
95 register_global("fonfilter", $fonfilter);
96 }
99 /* Search & display results */
100 function execute()
101 {
102 /* Call parent execute */
103 plugin::execute();
105 /* GVet template engine */
106 $smarty= get_smarty();
109 /*****************
110 Variable Init
111 *****************/
113 $fields_str = "";
115 $months= array();
116 for($i = 1 ; $i <= 12 ; $i ++ ){
117 $months[$i] = _(date("F",gmmktime(0,0,0,$i)));
118 }
120 /* Prepare template */
121 $current= date("Y");
122 $years= array();
123 for ($y= $current - 5; $y<=$current; $y++){
124 $years[$y]= $y;
125 }
127 /*****************
128 Smarty
129 *****************/
131 foreach($this->fields as $field){
132 if(chkacl($this->acl,$field)==""){
133 $smarty->assign($field."ACL","");
134 }else{
135 $smarty->assign($field."ACL"," disabled ");
136 }
137 }
139 $smarty->assign("plug", "?plug=".validate($_GET['plug']));
140 $smarty->assign("launchimage", get_template_path('images/launch.png'));
141 $smarty->assign("search_image", get_template_path('images/search.png'));
142 $smarty->assign("search_for", $this->search_for);
143 $smarty->assign("bases", $this->config->idepartments);
144 $smarty->assign("base_select", $this->search_base);
145 $smarty->assign("months", $months);
146 $smarty->assign("month_select", $this->month);
147 $smarty->assign("years", $years);
148 $smarty->assign("year_select", $this->year);
149 $smarty->assign("search_result", "");
152 /*****************
153 Check Database , Table , Connection
154 *****************/
156 // Collect servers and allow to select the server in the ui.
157 $servers = array();
158 foreach($this->config->data['SERVERS']['FON'] as $key => $server){
159 $servers[$server['SERVER']] = $server['SERVER'];
160 }
161 $smarty->assign("servers", $servers);
162 $smarty->assign("selected_server", $this->selected_server);
165 /* Connecting, selecting database */
166 if (!isset($this->config->data['SERVERS']['FON'][0])){
167 print_red(_("Can't connect to phone database, no reports can be shown!"));
168 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
169 }elseif(!is_callable("mysql_connect")){
170 print_red(_("There is no mysql extension available, please check your php setup."));
171 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
172 }else{
174 // Get CFG for the selected server, if empty use first.
175 if($this->selected_server == ""){
176 $cfg= $this->config->data['SERVERS']['FON'][0];
177 }else{
178 foreach($this->config->data['SERVERS']['FON'] as $server){
179 if($server['SERVER'] == $this->selected_server){
180 $cfg = $server;
181 }
182 }
183 }
185 // Try to connect
186 $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
187 if ($link === FALSE){
188 print_red(_("Can't connect to phone database, no reports can be shown!"));
189 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
190 }
191 if (! @mysql_select_db("gophone")){
192 print_red(_("Can't select phone database for report generation!"));
193 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
194 }
195 }
198 /*****************
199 Get Query String && Search
200 *****************/
202 $query = $this->CreateQuerySyntax();
203 $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
205 @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query");
207 $result = @mysql_query($query);
208 if ($result === false){
209 print_red(_("Query for phone database failed!"));
210 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
211 }
213 /*****************
214 Fetch results
215 *****************/
217 $report_list= array();
218 while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
219 $append_str = "";
220 $hour= substr($line["calldate"], 11, 2);
221 $minute=substr($line["calldate"], 14, 2);
222 $format= _("Y-M-D");
223 $date= preg_replace("/Y/", substr($line["calldate"], 0, 4), $format);
224 $date= preg_replace("/M/", substr($line["calldate"], 5, 2), $date);
225 $date= preg_replace("/D/", substr($line["calldate"], 8, 2), $date);
226 $append_str .= "<td>$date $hour:$minute</td>";
227 foreach(array("src","dst","channel","lastapp","disposition") as $atr){
228 if(isset($line[$atr])){
229 $append_str .= "<td>".$line[$atr]."</td>";
230 }
231 }
232 if(isset($line['duration'])){
233 $append_str .= "<td>".$this->gen_duration($line["duration"])."</td>";
234 }
235 $report_list[] = $append_str;
236 }
238 $this->report_list= $report_list;
239 @mysql_close($link);
242 /*****************
243 Create list of results
244 *****************/
246 /* Generate output */
247 $mod = 0;
248 $output = "";
249 if(count($this->report_list) < $this->start){
250 $this->start = 0;
251 }
252 foreach($this->report_list as $val){
253 if ($mod < $this->start) {
254 $mod++;
255 continue;
256 }
257 if ($mod >= ($this->start + $this->range)){
258 $mod++;
259 break;
260 }
261 if ( ($mod++) & 1){
262 $col= "background-color: #ECECEC;";
263 } else {
264 $col= "background-color: #F5F5F5;";
265 }
266 $output.= "<tr style=\"height:22px; $col\">$val</tr>";
267 }
269 /*****************
270 Tell smarty what we have found
271 *****************/
273 if ($output != ""){
274 $smarty->assign("search_result", $output);
275 $smarty->assign("range_selector", range_selector(count($this->report_list), $this->start,$this->range,"EntryPerPage"));
276 } else {
277 $smarty->assign("search_result", "");
278 }
280 /* Show main page */
281 for($i= 0; $i<7; $i++){
282 $smarty->assign("mode$i", "");
283 }
284 $smarty->assign("mode".$this->sort, "<img alt=\"\" src=\"images/sort_".$this->sort_direction.".png\" border=0 align=middle>");
286 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
287 }
290 /* Create duration string 12'11" */
291 function gen_duration($seconds)
292 {
293 if ($seconds / 60 > 1){
294 $minutes= (int)($seconds / 60);
295 $seconds= $seconds % 60;
296 return ("$minutes’$seconds”");
297 }
298 return ("$seconds”");
299 }
302 /* Create WHERE part for our mysql serach */
303 function GetUidMatchingFilter()
304 {
305 $ldap = $this->config->get_ldap_link();
306 $ldap->cd ($this->search_base);
307 $s = $this->search_for;
309 $s = preg_replace("/\%/","",$s);
310 $s2 = preg_replace("/\*/","%",$s);
312 /* Perform ldap search for potential users */
313 $ldap->search ("(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(objectClass=gofonAccount)".
314 "(|(uid=$s)(l=$s)(homePhone=$s)(telephoneNumber=$s)(facsimileTelephoneNumber=$s)(mobile=$s)".
315 "(pager=$s)(cn=$s)(givenName=$s)(sn=$s)(personalTitle=$s)(title=$s)))" ,array("uid"));
316 $str = " AND (";
317 $fields = array("dstchannel","channel");
318 if($ldap->count()){
319 while ($attrs= $ldap->fetch()){
320 $uid = $attrs["uid"][0];
321 foreach($fields as $name){
322 $str .= $name." like '%".$uid."%' OR ";
323 }
324 }
325 }
326 $str .= " channel like '%".$s."%' OR
327 dstchannel like '%".$s."%' OR
328 dst like '".$s2."' OR
329 src like '".$s2."' OR
330 lastapp like '".$s2."')";
331 return($str);
332 }
335 /* Create query string */
336 function CreateQuerySyntax()
337 {
338 /* Get extended search filter which contain uids and so on */
339 $uidstring = $this->GetUidMatchingFilter();
341 /* Create string with all fields seperated by ,*/
342 $fields_str ="";
343 foreach($this->fields as $field){
344 if($field == "calldate") continue;
345 if(chkacl($this->acl,$field)==""){
346 $fields_str .= $field.", ";
347 }
348 }
349 $fields_str = preg_replace("/, $/","",$fields_str);
351 /* Create Sort tag */
352 if ($this->sort_direction == "down"){
353 $desc= "DESC";
354 } else {
355 $desc= "ASC";
356 }
357 /* Create times */
358 $start= date ("YmdHis", mktime(0,0,0,$this->month,1,$this->year));
359 $end= date ("YmdHis", mktime(23,59,59,($this->month +1),0,$this->year));
360 $query = "SELECT ".$fields_str.",calldate FROM cdr ".
361 "WHERE
362 calldate <= $end
363 AND
364 calldate >= $start
365 ". $uidstring."
366 ORDER BY ".$this->fields[$this->sort]." $desc;";
367 return($query);
368 }
369 }
370 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
371 ?>