root/contributed_modules/modules/extcfg/functions.inc.php

Revision 11299, 12.6 kB (checked in by pnlarsson, 1 year ago)

Added instead of hardcoded settings. Added some images, re #4688

Line 
1 <?php
2 //Copyright (C) 2006 Niklas Larsson
3 //
4 //This program is free software; you can redistribute it and/or
5 //modify it under the terms of the GNU General Public License
6 //as published by the Free Software Foundation; either version 2
7 //of the License, or (at your option) any later version.
8 //
9 //This program is distributed in the hope that it will be useful,
10 //but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //GNU General Public License for more details.
13
14 // returns a associative arrays with keys 'destination' and 'description'
15 function extcfg_destinations() {
16   return null;
17 }
18
19 function extcfg_get_config($engine) {
20
21 }
22
23 define("CW", "CW");
24 define("CFBS", "CFB");
25 define("CFNA", "CFU");
26 define("CFIM", "CF");
27 define("YAC", "YAC");
28 define("DND", "DND");
29
30 require("astman.inc");
31
32 function extcfg_init(){
33   global $server, $amp_conf;
34
35 /*  mysql_connect('localhost', 'asteriskuser', 'amp109') or die ("Could not connect to MySQL");
36   mysql_select_db('asterisk') or die ("Could not select asterisk database" . mysql_error()); 
37  
38   $sql = "select * from servers;";
39   $result = mysql_query($sql) or die ("Server Query failed" . mysql_error());
40  
41   $server_nr = mysql_num_rows($result);
42   $i=0;
43   while ($row = mysql_fetch_array($result)) {
44     $server[$i]["db_host"] = $row["db_host"];
45     $server[$i]["db_user"] = $row["db_user"];
46     $server[$i]["db_passwd"] = $row["db_pass"];
47     $server[$i]["db_db"] = $row["db_db"];
48     $server[$i]["astman_host"] = $row["astman_host"];
49     $server[$i]["astman_user"] = $row["astman_user"];
50     $server[$i]["astman_passwd"] = $row["astman_pass"];
51     $server[$i]["name"] = $row["host_name"];
52     $server[$i]["astman"] = new AstMan;
53     $i++;
54   }
55
56   */
57  
58   $server[] = array(
59    "db_host" => $amp_conf['AMPDBHOST'],
60    "db_user" => $amp_conf['AMPDBUSER'],
61    "db_passwd" => $amp_conf['AMPDBPASS'],
62    "db_db" => $amp_conf['AMPDBNAME'],
63    "astman_host" => 'localhost',
64    "astman_user" => $amp_conf['AMPMGRUSER'],
65    "astman_passwd" => $amp_conf['AMPMGRPASS'],
66    "name" => "localhost",
67    "astman" => new AstMan
68   );
69   /*
70   $server[] = array(
71    "db_host" => "192.168.102.38",
72    "db_user" => "root",
73    "db_passwd" => "xxxxxx",
74    "db_db" => "asterisk",
75    "astman_host" => "192.168.102.38",
76    "astman_user" => "root",
77    "astman_passwd" => "xxxxxxx",
78    "name" => "Alfa",
79    "astman" => new AstMan
80   );
81   */
82  
83   $server_nr = count($server);
84  
85   for($i = 0; $i < $server_nr; $i++){
86     $server[$i]['astman']->Login($server[$i]['astman_host'],$server[$i]['astman_user'],$server[$i]['astman_passwd']);
87   }
88 }
89
90 function extcfg_exit(){
91   global $server;
92   foreach($server as $serv){
93     $serv['astman']->Logout();
94   }
95 }
96
97 function extcfg_show_list($status = false)
98 {
99   global $server;
100   $exts = array();
101   $i = 1;
102  
103   foreach($server as $key => $serv){
104     $exts = array_merge($exts, extcfg_get_extensions_amp($serv['db_host'], $serv['db_user'], $serv['db_passwd'], $serv['db_db'], $key));
105     $status_arr = get_ext_status($serv['db_host'], $serv['db_user'], $serv['db_passwd'], $serv['db_db'], $key, $serv['astman']);
106
107     $dnd[$key] = $serv['astman']->GetFamilyDB(DND);
108     $cw[$key] = $serv['astman']->GetFamilyDB(CW);
109     $cfim[$key] = $serv['astman']->GetFamilyDB(CFIM);
110     $cfbs[$key] = $serv['astman']->GetFamilyDB(CFBS);
111     $cfna[$key] = $serv['astman']->GetFamilyDB(CFNA);
112   }
113   sort($exts);
114  
115   echo "<table border='0' cellspacing='0' cellpadding='3' style=''><tr><th></th><th>Extension</th><th>DND</th><th>Call<br>Waiting</th><th>Call Forward<br>All</th><th>Call Forward<br>Busy</th><th>Call Forward<br>No Answer</th><th>IP</th><th>port</th><th>Status</th><th>Device</th><th>Tech</th></tr>";
116
117   foreach($exts as $ext){
118     $status_bg = $status_arr[$ext[0]]['ok'] ? '#88ff88' : '#ff8888';
119   $dnd_f = empty($dnd[$ext[1]][$ext[0]]) ? '' : '<img src="/admin/images/accept.png" border="0">';
120   $cw_f = '';
121   if(!empty($cw[$ext[1]][$ext[0]])){
122     if($cw[$ext[1]][$ext[0]] == 'ENABLED'){
123       $cw_f = '<img src="/admin/images/accept.png" border="0">';
124     }
125   }
126   $cfim_f = empty($cfim[$ext[1]][$ext[0]]) ? '' : $cfim[$ext[1]][$ext[0]];
127   $cfbs_f = empty($cfbs[$ext[1]][$ext[0]]) ? '' : $cfbs[$ext[1]][$ext[0]];
128   $cfna_f = empty($cfna[$ext[1]][$ext[0]]) ? '' : $cfna[$ext[1]][$ext[0]];
129   $ip = empty($status_arr[$ext[0]]['ip']) ? '' : $status_arr[$ext[0]]['ip'];
130   $port = empty($status_arr[$ext[0]]['port']) ? '' : $status_arr[$ext[0]]['port'];
131   $status = empty($status_arr[$ext[0]]['status']) ? '' : $status_arr[$ext[0]]['status'];
132   $device = empty($status_arr[$ext[0]]['device']) ? '' : $status_arr[$ext[0]]['device'];
133   $type = empty($status_arr[$ext[0]]['type']) ? '' : $status_arr[$ext[0]]['type'];
134     echo "<tr bgcolor='" . varBg($i++) . "'>
135       <td><a href='/admin/config.php?type=setup&display=extensions&extdisplay={$ext[0]}'><img src='/admin/images/telephone_edit.png' border=0 title='Edit extension'></a></td>
136       <td><a href='" . $_SERVER['PHP_SELF'] . "?display=extcfg&type=tool&action=phone&phone={$ext[0]}&srv={$ext[1]}'>{$ext[0]} - {$ext[2]}</a></td>
137       <td style='text-align: center;'>$dnd_f&nbsp;</td>
138       <td style='text-align: center;'>$cw_f&nbsp;</td>
139       <td>$cfim_f&nbsp;</td>
140       <td>$cfbs_f&nbsp;</td>
141       <td>$cfna_f&nbsp;</td>
142       <td>$ip</td>
143       <td>$port</td>
144       <td style='background-color: $status_bg;'>$status</td>
145       <td>$device</td>
146       <td>$type</td>
147     </tr>";
148   }
149  
150   echo "</table>";
151 }
152
153 function extcfg_show_phone($ext, $srv)
154 {
155   global $server;
156   $serv = $server[$srv];
157 ?>
158 <form name="phone" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
159   <input name="ext" type="hidden" value="<?php echo $ext; ?>">
160   <input name="srv" type="hidden" value="<?php echo $srv; ?>">
161   <input type="hidden" name="display" value="extcfg">
162   <input type="hidden" name="type" value="tool">
163   <input type="hidden" name="action" value="phone_save">
164   <table width="80%"  border="0">
165     <tr>
166       <td width="20%" scope="col">Extension</td>
167       <td width="63%" scope="col"><b><?php echo $ext; ?></b></td>
168
169     </tr>
170 <!--    <tr>
171     
172       <td><input type="text" name="YAC" value="<?php echo $serv['astman']->GetDB(YAC, $ext);?>"></td>
173       <td>IP nummer / DNS-namn till datorn <a href="http://www.dynx.net/ASTERISK/misc-progs/YAC/yac-0.16-win32.zip">Programmet finns h&auml;r</a></td>
174     </tr> -->
175     <tr>
176       
177       <td><input name="DND" type="checkbox" value="DND" <?php if ($serv['astman']->GetDB(DND, $ext) == "YES") echo "checked";?>></td>
178       <td>Do Not Disturb</td>
179     </tr>
180     <tr>
181     
182       <td><input type="checkbox" name="CW" value="CW" <?php if ($serv['astman']->GetDB(CW, $ext) == "ENABLED") echo "checked";?>></td>
183       <td>Call Waiting</td>
184     </tr>
185     <tr>
186       
187       <td><input type="text" name="CFIM" value="<?php echo $serv['astman']->GetDB(CFIM, $ext);?>"></td>
188       <td>Call Forward All</td>
189     </tr>
190     <tr>
191     
192       <td><input type="text" name="CFBS" value="<?php echo $serv['astman']->GetDB(CFBS, $ext);?>"></td>
193       <td>Call Forward Busy</td>
194     </tr>
195     <tr>
196     
197       <td><input type="text" name="CFNA" value="<?php echo $serv['astman']->GetDB(CFNA, $ext);?>"></td>
198       <td>Call Forward No Answer</td>
199     </tr>
200     <tr>
201   
202       <td colspan="2"><input type="submit" name="Submit" value="Submit">
203       <input type="reset" name="Reset" value="Reset"></td>
204     </tr>
205   </table>
206 </form> 
207 <?php
208 }
209
210 function extcfg_save_phone($ext, $srv)
211 {
212   global $server;
213   $serv = $server[$srv];
214
215   if (isset($_REQUEST["DND"]))
216     $serv['astman']->PutDB(DND, $ext, "YES");
217   else
218     $serv['astman']->DelDB(DND, $ext);   
219   if (isset($_REQUEST["CW"]))
220     $serv['astman']->PutDB(CW, $ext, "ENABLED");
221   else
222     $serv['astman']->DelDB(CW, $ext);   
223   if (!empty($_REQUEST["CFBS"]))
224     $serv['astman']->PutDB(CFBS, $ext, $_REQUEST["CFBS"]);
225   else
226     $serv['astman']->DelDB(CFBS, $ext);   
227   if (!empty($_REQUEST["CFIM"]))
228     $serv['astman']->PutDB(CFIM, $ext, $_REQUEST["CFIM"]);
229   else
230     $serv['astman']->DelDB(CFIM, $ext);   
231   if (!empty($_REQUEST["CFNA"]))
232     $serv['astman']->PutDB(CFNA, $ext, $_REQUEST["CFNA"]);
233   else
234     $serv['astman']->DelDB(CFNA, $ext);   
235 }
236
237 function extcfg_get_extensions_amp($server, $user, $passwd, $db, $astman_nr)
238 {
239   $exts = array();
240
241   $sql = "SELECT id, description FROM devices d ORDER BY CAST(id AS UNSIGNED);";
242  
243   mysql_connect($server, $user, $passwd) or die ("Could not connect to MySQL");
244   mysql_select_db($db) or die ("Could not select $db database"); 
245  
246   $result = mysql_query($sql) or die ("Query failed");
247   while ($kolumn = mysql_fetch_array($result)) {
248     $exts[] = array($kolumn["id"], $astman_nr, $kolumn['description']);
249   }
250  
251   return ($exts);
252 }
253
254 function get_ext_status($server, $user, $passwd, $db, $astman_nr, $astman){
255   $arr = array();
256   $sccp_sep_arr = array();
257  
258   // SIP 
259   $sip_res = $astman->Query2("Action: sippeers\r\n\r\n", 'Event: PeerlistComplete');
260
261   $sip_peers = explode("\r\n\r\n", $sip_res);
262  
263   foreach ($sip_peers as $sip_peer) {
264     if (strpos($sip_peer, 'ObjectName')){
265       $extension = $astman->get_my_stuff($sip_peer, 'ObjectName: ', "\r\n");
266      
267       $arr[$extension] = array(
268         'ip' => $astman->get_my_stuff($sip_peer, 'IPaddress: ', "\r\n"),
269         'port' => $astman->get_my_stuff($sip_peer, 'IPport: ', "\r\n"),
270         'status' => $astman->get_my_stuff($sip_peer, 'Status: ', "\r\n"),
271         'type' => 'SIP',
272         'device' => '',
273         'ok' => false
274       );   
275       if ($arr[$extension]['ip'] != '-none-' && $arr[$extension]['status'] != 'UNREACHABLE')
276         $arr[$extension]['ok'] = true;
277     }
278   }
279  
280   // IAX
281  
282   $iax_res = $astman->Query2("Action: IAXpeerlist\r\n\r\n", 'PeerlistComplete');
283
284   $iax_peers = explode("\r\n\r\n", $iax_res);
285    
286   foreach ($iax_peers as $iax_peer) {
287     if (strpos($iax_peer, 'ObjectName')){
288       $extension = $astman->get_my_stuff($iax_peer, 'ObjectName: ', "\r\n");
289      
290       $arr[$extension] = array(
291         'ip' => $astman->get_my_stuff($iax_peer, 'IPaddress: ', "\r\n"),
292         'port' => $astman->get_my_stuff($iax_peer, 'Port: ', "\r\n"),
293         'status' => $astman->get_my_stuff($iax_peer, 'Status: '),
294         'type' => 'IAX2',
295         'device' => '',
296         'ok' => false
297       );   
298       if ($arr[$extension]['ip'] != '-none-' && $arr[$extension]['status'] != 'UNREACHABLE')
299         $arr[$extension]['ok'] = true;       
300     }
301   }
302
303   // SCCP
304  
305   $sccp_devices_res = $astman->Query("Action: Command\r\nCommand: sccp show devices\r\n\r\n");
306   $sccp_lines_res = $astman->Query("Action: Command\r\nCommand: sccp show lines\r\n\r\n");
307
308   $sccp_lines = get_astman_lines($sccp_lines_res);
309  
310   unset($sccp_lines[0]);
311   unset($sccp_lines[1]);
312   unset($sccp_lines[2]);
313   unset($sccp_lines[3]);
314   foreach ($sccp_lines as $sccp_line){
315     $extension = trim(substr($sccp_line, 0, 16));
316     $sep = trim(substr($sccp_line, 16, 16));
317     if ($extension){
318       $sccp_sep_arr[$sep] = $extension;
319       $arr[$extension] = array('ip' => '', 'port' => '', 'status' => '', 'type' => 'SCCP', 'device' => $sep, 'ok' => false);
320     }
321   }
322  
323   $sccp_devices = get_astman_lines($sccp_devices_res);
324   unset($sccp_devices[0]);
325   unset($sccp_devices[1]);
326   unset($sccp_devices[2]);
327   unset($sccp_devices[3]);
328
329   foreach($sccp_devices as $sccp_device){
330     $status = trim(substr($sccp_device, -10));
331     $sep = trim(substr($sccp_device, -27, 16));
332     $ip = trim(substr($sccp_device, -43, 15));
333     $extension = empty($sccp_sep_arr[$sep]) ? null : $sccp_sep_arr[$sep];
334     $ok = $sep == '--' ? false : true;
335     if ($extension){
336       $arr[$extension] = array('ip' => $ip, 'port' => '', 'status' => $status, 'type' => 'SCCP', 'device' => $sep, 'ok' => $ok);
337     }
338   }
339  
340   // MGCP
341  
342   $mgcp_endpoints_res = $astman->Query("Action: Command\r\nCommand: mgcp show endpoints\r\n\r\n");
343
344   if (strpos($mgcp_endpoints_res, 'Gateway')){
345  
346   $mgcp_endpoints = get_astman_lines($mgcp_endpoints_res);
347   unset($mgcp_endpoints[0]);
348
349   foreach($mgcp_endpoints as $mgcp_endpoint){
350     $extension = '';
351     $items = explode(' ', trim($mgcp_endpoint));
352     if ($items[0] == 'Gateway'){ // First line
353       $ip = $items[3];
354       $device = str_replace("'", '', $items[1]);
355     }else{
356       $line = str_replace("'", '', $items[1]);
357       $status = $items[5];
358      
359       $sql = "SELECT d.`id`
360               FROM devices d
361               WHERE d.`dial` = 'MGCP/$line'";
362              
363       if ($query = mysql_query($sql)){
364         if ($row = mysql_fetch_assoc($query)){
365           $extension = $row['id'];
366         }
367       }
368    
369       if ($extension){
370         $ok = $ip == '0.0.0.0' ? false : true;
371            
372         $arr[$extension] = array('ip' => $ip, 'port' => '', 'status' => $status, 'type' => 'MGCP', 'device' => $device, 'ok' => $ok);
373       }
374     }
375   }
376   }
377  
378   return $arr; 
379 }
380
381 function get_astman_lines($wrets){
382   $value_start = strpos($wrets, "Response: Follows\r\n") + 19;
383   $value_stop = strpos($wrets, "--END COMMAND--\r\n", $value_start);
384   if ($value_start > 18){
385     $wrets = substr($wrets, $value_start, $value_stop - $value_start);
386   }
387   $lines = explode("\n", $wrets);
388  
389   return $lines;
390 }
391
392 function varBg($i = 0){
393   if($i%2 == 0)
394     return '#ffffff';
395   else
396     return '#eeeeee';
397 }
398 ?>
Note: See TracBrowser for help on using the browser.