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