root/modules/branches/2.10/sipstation/sipstation.utility.php

Revision 13091, 22.4 kB (checked in by p_lindheimer, 1 year ago)

adds FREEPBX_IS_AUTH checking to most module files re #5478

Line 
1 <?php
2 /* $Id:$ */
3 if (!defined('FREEPBX_IS_AUTH')) { die('No direct script access allowed'); }
4
5 /* Copyright (c) 2009 Bandwidth.com
6    Licensed for use by active FreePBX.com SIP Trunking Customers (SIPSTATION(tm)). Not licensed to be modified or redistributed in any fashion.
7    No guarantees or warranties provided, use at your own risk. See license included with module for more details.
8 */
9
10 /* hard coded hash of Asterisk codec names compared to what XML official name may send down
11    will be filtered so only supported codecs remain, From Asterisk:
12
13        1 (1 <<  0)      (0x1)  audio       g723   (G.723.1)
14        2 (1 <<  1)      (0x2)  audio        gsm   (GSM)
15        4 (1 <<  2)      (0x4)  audio       ulaw   (G.711 u-law)
16        8 (1 <<  3)      (0x8)  audio       alaw   (G.711 A-law)
17       16 (1 <<  4)     (0x10)  audio   g726aal2   (G.726 AAL2)
18       32 (1 <<  5)     (0x20)  audio      adpcm   (ADPCM)
19       64 (1 <<  6)     (0x40)  audio       slin   (16 bit Signed Linear PCM)
20      128 (1 <<  7)     (0x80)  audio      lpc10   (LPC10)
21      256 (1 <<  8)    (0x100)  audio       g729   (G.729A)
22      512 (1 <<  9)    (0x200)  audio      speex   (SpeeX)
23     1024 (1 << 10)    (0x400)  audio       ilbc   (iLBC)
24     2048 (1 << 11)    (0x800)  audio       g726   (G.726 RFC3551)
25     4096 (1 << 12)   (0x1000)  audio       g722   (G722)
26 */
27 $failover = false;
28 $sipstation_xml_version = '1.0.0';
29 $ast_codec_hash = array(
30   'g723'     => 'G.723.1',
31   'gsm'      => 'GSM',
32   'ulaw'     => 'G.711.U',
33   'alaw'     => 'G.711.A',
34   'g722aal2' => 'G.726.AAl2',
35   'adpcm'    => 'ADPCM',
36   'slin'     => 'SLIN',
37   'lpc10'    => 'LPC10',
38   'g729'     => 'G.729.A',
39   'speex'    => 'SpeeX',
40   'ilbc'     => 'iLBC',
41   'g726'     => 'G.726',
42   'g722'     => 'G.722',
43 );
44
45 /* callback to filer out codecs not supported
46  */
47 function sipstation_codec_filter($codec) {
48   global $ast_codec_hash;
49
50   $codec_split = explode(':',$codec,2);
51   if (array_key_exists($codec_split[0],$ast_codec_hash)) {
52     return $codec_split[0];
53   } else {
54     return false;
55   }
56 }
57 function sipstation_supported_codecs($codec) {
58   global $codec_array;
59   return in_array($codec,$codec_array) ? $codec : false;
60 }
61
62 /* Returns a hash of settings from 'sip show peers'
63 */
64 function sipstation_get_peer_status($peer) {
65   global $astman;
66   $sip_peer['sipstation_status'] = 'ok';
67   $response = $astman->send_request('Command',array('Command'=>"sip show peer $peer"));
68   $buf = explode("\n",$response['data']);
69   foreach ($buf as $res) {
70     if (preg_match("/$peer\s*not\s+found\.{0,1}\s*$/",$res)) {
71       $sip_peer['sipstation_status'] = 'no_peer';
72     } elseif (preg_match("/^\s*(.*?)\s*:\s*(.*)$/",$res,$match)) {
73       $sip_peer[$match[1]] = $match[2];
74     }
75   }
76   return $sip_peer;
77 }
78
79 /* Returns a hash of settings from 'sip show settings'
80 */
81 function sipstation_get_sip_settings() {
82   global $astman;
83   $sip_peer['sipstation_status'] = 'ok';
84   $response = $astman->send_request('Command',array('Command'=>"sip show settings"));
85   $buf = explode("\n",$response['data']);
86   foreach ($buf as $res) {
87     if (preg_match("/$peer\s*not\s+found\.{0,1}\s*$/",$res)) {
88       $sip_peer['sipstation_status'] = 'no_peer';
89     } elseif (preg_match("/^\s*(.*?)\s*:\s*(.*)$/",$res,$match)) {
90       $sip_peer[$match[1]] = $match[2];
91     }
92   }
93   return $sip_peer;
94 }
95
96 /* Returns a filtered array of currently configured codecs, filtered
97    against the list of supported codecs
98 */
99 function sipstation_get_configured_codecs($peer, $peer_status=false) {
100   if (!is_array($peer_status) || empty($peer_status)) {
101     $peer_status = sipstation_get_peer_status($peer);
102   }
103   if ($peer_status['sipstation_status'] = 'ok') {
104     if (preg_match("/^\s*\((.*)\)\s*$/",$peer_status['Codec Order'],$match)) {
105       $codecs = explode(',',$match[1]);
106       return array_filter(array_map('sipstation_codec_filter',$codecs));
107     }
108   }
109 }
110
111 function sipstation_get_key() {
112   global $db;
113     $sql = "SELECT * FROM module_xml WHERE id = 'sipstation_key'";
114     $result = sql($sql,'getRow',DB_FETCHMODE_ASSOC);
115     if (!isset($result['data']) || trim($result['data']) == "") {
116     return false;
117   } else {
118     return $result['data'];
119   }
120 }
121
122 /* Check if there is a valid key
123  * Returns: nokey, valid, invalid, noserver (if server can't be contacted)
124  */
125 function sipstation_check_key() {
126     $sql = "SELECT * FROM `module_xml` WHERE `id` = 'sipstation_key'";
127     $result = sql($sql,'getRow',DB_FETCHMODE_ASSOC);
128
129     // if not set so this is a first time install
130     // get a new hash to account for first time install
131     //
132     if (!isset($result['data']) || trim($result['data']) == "") {
133     return 'nokey';
134   } else {
135     // TODO: should really encrypt/decrypt key
136     //
137     return sipstation_confirm_key($result['data']);
138   }
139 }
140
141 /* deleted saved configuration if confirmation determines it is stale
142  */
143 function sipstation_confirm_key($key) {
144   $xml_array = sipstation_get_config(trim($key));
145   if ($xml_array['status'] == 'success') {
146     switch ($xml_array['query_status']) {
147       case 'SUCCESS':
148         return 'valid';
149       case 'TEMPNOTAVAIL':
150         return 'tempnotavail';
151       case 'BADKEY':
152         sipstation_remove_key();
153       default:
154         return 'invalid';
155     }
156   } else {
157     return $xml_array['status'];
158   }
159 }
160
161 function sipstation_set_key($key) {
162   global $db;
163   $status = sipstation_confirm_key($key);
164   if ($status == 'valid' || $status == 'tempnotavail') {
165     $data4sql = $db->escapeSimple($key);
166     sql("DELETE FROM `module_xml` WHERE `id` = 'sipstation_key'");
167     sql("INSERT INTO `module_xml` (`id`,`time`,`data`) VALUES ('sipstation_key',".time().",'".$data4sql."')");
168   }
169   return $status;
170 }
171
172 function sipstation_remove_key() {
173   sql("DELETE FROM `module_xml` WHERE `id` = 'sipstation_key'");
174   return $status;
175 }
176
177 /* save the retrieved configuration information into the db to be used to configure trunks and what not
178  */
179 function sipstation_save_config($xml) {
180   global $db;
181   $data4sql = $db->escapeSimple($xml);
182   sql("DELETE FROM `module_xml` WHERE `id` = 'sipstation_config'");
183   sql("INSERT INTO `module_xml` (`id`,`time`,`data`) VALUES ('sipstation_config',".time().",'".$data4sql."')");
184 }
185
186 function sipstation_del_saved_config() {
187   global $db;
188   sql("DELETE FROM `module_xml` WHERE `id` = 'sipstation_config'");
189 }
190
191 function sipstation_retrieve_saved_config($format='') {
192   global $db;
193     $sql = "SELECT `data` FROM `module_xml` WHERE `id` = 'sipstation_config'";
194     $xml_data = sql($sql, "getOne");
195   if ($format == 'xml') {
196     return $xml_data;
197   } else {
198     $parser = new xml2Array($xml_data);
199     return $parser;
200   }
201 }
202
203 function sipstation_get_settings($key, $online=true) {
204   global $amp_conf;
205
206   if ($online) {
207     $xml_data = sipstation_curl_xml($key);
208
209     sipstation_save_config($xml_data); // cache the latest
210         $parser = new xml2Array($xml_data);
211
212     return $parser;
213   } else {
214     return sipstation_retrieve_saved_config();
215   }
216 }
217
218 // TODO: handle timeouts, other issues.
219 // TODO: make more general for any post, make generic RESTFUL function
220 function sipstation_curl_xml($keycode) {
221
222   // TODO: TEST
223   /* would be better to serialize the array and send it
224   curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
225   $params = "keycode=".urlencode($keycode);
226   curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
227   */
228
229   $url = "https://store.freepbx.com/store/myaccount.xml";
230   //$params = "keycode=".urlencode($keycode);
231   $params = array("keycode" => urlencode($keycode));
232   $user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
233
234   $ch = curl_init();
235   //curl_setopt ($ch, CURLOPT_POST, 1);
236   curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
237   curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
238   curl_setopt($ch, CURLOPT_URL, $url);
239   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
240   curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
241   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return results to string
242   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
243   curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookie.txt');    // imitate classic browser behavior
244   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
245
246   curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
247   // TODO: deal with timeout!
248
249   $xml = curl_exec ($ch);
250
251   // TODO: curl info back from request, check return code
252   // freepbx_debug(curl_getinfo($ch));
253
254   // TODO: if ($xml === false)  handle it, retry, etc.
255   // TODO: or at least toss in some error xml response
256   curl_close ($ch);
257
258   return $xml;
259 }
260
261 //TODO: refactor with above to combine common parts
262
263 function sipstation_put_dids($dids) {
264   if (empty($dids)) {
265     return true;
266   }
267   $keycode = sipstation_get_key();
268   $xml = '
269 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
270 <xml type="sipstation/xml" version="1.0.0">
271 <xml_version>1.0.0</xml_version>';
272   $xml .= "
273 <keycode>$keycode</keycode>
274 <dids>";
275   foreach ($dids as $did => $failover) {
276     $xml .= "\n<did failover=\"$failover\">$did</did>";
277   }
278   $xml .= "
279 </dids>
280 </xml>
281 ";
282
283   // TODO: different URL for putting just dids
284   $url = "https://store.freepbx.com/store/myaccount.xml";
285   $user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
286
287   $ch = curl_init();
288   curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
289   curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
290
291   curl_setopt($ch, CURLOPT_URL, $url);
292   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
293   curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
294   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);            // print results to string and not STDOUT
295   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
296   curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookie.txt');    // IMITATE CLASSIC BROWSER'S BEHAVIOUR
297   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
298
299   curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
300   // TODO: deal with timeout!
301
302   $xml = curl_exec ($ch);
303
304   // curl info back from request
305   $return_status = curl_getinfo($ch);
306   // freepbx_debug($return_status);
307
308   // TODO: if ($xml === false) { handle it, retry, etc.
309   // TODO: or at least toss in some error xml response
310   curl_close ($ch);
311
312   return $xml;
313 }
314
315 function sipstation_get_or_create_trunks(&$json_array,&$globalvar1,&$trunknum1,&$globalvar2,&$trunknum2) {
316   // now check on trunk config
317   // fpbx-1-$sip_user / fpbx-2-$sip_user
318   // TODO: 2.6 has trunkname, provider options that should be used
319   //
320   global $ast_codec_hash;
321   $sip_user = $json_array['sip_username'];
322   $sip_pass = $json_array['sip_password'];
323   $default_did = $json_array['e911_address']['default_did'];
324   $need_reload = false;
325
326   $tlist       = core_trunks_list(true);
327   $tech        = 'sip';
328   $keepcid     = 'off';
329   $disabletrunk= 'off';
330
331     $peerdetails = "disallow=all\nallow=".implode('&',array_keys($ast_codec_hash))."\n";
332     $peer_array = array();
333   foreach ($json_array['asterisk_settings']['peer'] as $param) {
334     $peerdetails .= trim($param)."\n";
335     $parts = explode('=',$param,2);
336     $peer_array[$parts[0]] = $parts[1];
337   }
338
339     $peerdetails .= "username=$sip_user\nsecret=$sip_pass\nhost=";
340     $register    = "$sip_user:$sip_pass@";
341
342   for ($i=1;$i<3;$i++) {
343     $gidx = "gw$i";
344     $channelid   = "fpbx-$i-$sip_user";
345     $gw          = $json_array['gateways'][$gidx];
346     if (isset($tlist["SIP/$channelid"])) {
347       $globalvar = $tlist["SIP/$channelid"]['globalvar'];
348       $trunknum  = ltrim($globalvar,'OUT_');
349       // Now get some trunk status info
350       $trunk_status = sipstation_get_peer_status($channelid);
351       if ($trunk_status['sipstation_status'] == 'ok') {
352           $json_array['trunk_qualify'][$gidx] = $trunk_status['Status'];
353           $json_array['trunk_codecs'][$gidx] = implode(' | ',sipstation_get_configured_codecs($channelid,$trunk_status));
354       } else {
355         //TODO: probably nothing
356       }
357       $json_array['trunk_name'][$gidx] = core_trunks_getTrunkTrunkName($trunknum);
358     } else {
359           $trunknum = core_trunks_add($tech, $channelid, '', '', $default_did, $peerdetails.$gw, '', '', $register.$gw, $keepcid, '', $disabletrunk);
360       $globalvar = "OUT_".$trunknum;
361       $need_reload = true;
362         $json_array['created_trunks'][$gidx] = $channelid;
363       // TODO: 2.6 should be name
364       $json_array['trunk_name'][$gidx] = "SIP/$channelid";
365     }
366     // We need these next and need them past back up
367     $gv = "globalvar$i";
368     $tn = "trunknum$i";
369     $$gv = $globalvar;
370     $$tn = $trunknum;
371   }
372
373   $peer_array['username'] = $sip_user;
374   $peer_array['secret'] = $sip_pass;
375
376   $trunk_check = array($trunknum1, $trunknum2);
377   $cnt = 1;
378   foreach ($trunk_check as $tr) {
379       $json_array['trunk_id']["gw$cnt"] = $tr; // need to get this set for both anyhow
380     $gw = $json_array['gateways']["gw$cnt"];
381     $peer_array['host'] = $gw;
382     $peer_stuff = array();
383     $tr_reg = core_trunks_getTrunkRegister($tr);
384     foreach (explode("\n",core_trunks_getTrunkPeerDetails($tr)) as $elem) {
385       $temp = explode("=",$elem,2);
386       if ($temp[0] == 'allow') {
387         $peer_stuff[$temp[0]] = explode('&',$temp[1]);
388       } elseif ($temp[0] != '') {
389         $peer_stuff[$temp[0]] = $temp[1];
390       }
391     }
392     // Unset some settings that do not hurt to change and might help
393     //
394     if (isset($peer_stuff['allow'])) {
395       unset($peer_stuff['allow']);
396     }
397
398     if (isset($peer_stuff['disallow'])) {
399       unset($peer_stuff['disallow']);
400     }
401
402     unset($peer_array['qualify']);
403     if (isset($peer_stuff['qualify'])) {
404       unset($peer_stuff['qualify']);
405     }
406     unset($peer_array['qualify']);
407
408     if (isset($peer_stuff['context'])) {
409       unset($peer_stuff['context']);
410     }
411     unset($peer_array['context']);
412
413     if (isset($peer_stuff['qualifyfreq'])) {
414       unset($peer_stuff['qualifyfreq']);
415     }
416
417     if (isset($peer_stuff['dtmfmode'])) {
418       switch($peer_stuff['dtmfmode']) {
419         case 'inband':
420         case 'rfc2833':
421         case 'auto':
422           unset($peer_stuff['dtmfmode']);
423           unset($peer_array['dtmfmode']);
424         break;
425       }
426     }
427
428     if ($peer_array != $peer_stuff || $tr_reg != $register.$gw) {
429         $json_array['changed_trunks']["gw$cnt"] = $tr;
430     }
431     $cnt++;
432   }
433   return $need_reload;
434 }
435
436 /*
437 Current format of 'sip show registry' with various possible states
438 Host                            Username       Refresh State                Reg.Time                 
439 trunk1.freepbx.com:5060         b04c1dsr           585 Registered           Sat, 27 Jun 2009 00:33:47
440 trunk2.freepbx.com:5060         b04c1dsr           585 Registered           Sat, 27 Jun 2009 00:33:48
441 phonebooth.bandwidth.com:5060   9192221234         585 Timeout              Sat, 27 Jun 2009 00:33:47
442 67.131.62.22:5060               myusername         585 Auth.Sent.           Sat, 27 Jun 2009 00:33:47
443 */
444 function sipstation_get_registration_status($sip_user) {
445   global $astman;
446   $status_arr = array();
447   if (!isset($astman)) {
448     return $status_arr;
449   }
450   $response = $astman->send_request('Command',array('Command'=>"sip show registry"));
451   $buf = explode("\n",$response['data']);
452   $state_pos = false;
453   foreach ($buf as $line) {
454     if (trim($line) != '') {
455       if ($state_pos===false) {
456         // find the positions of the header columns so we can parse
457         if ($state_pos = strpos($line,"State")) {
458           $user_pos = strpos($line,"Username");
459           $reg_pos = strpos($line,"Reg.Time");
460           $host_pos = strpos($line,"Host");
461
462           // Asterisk 1.2 does not have Reg. Time
463           if ($reg_pos === false) {
464             $reg_pos = strlen($line);
465           }
466         }
467       } else {
468         // get the username and if ours, trunk (host) and State of reg
469         preg_match("/^([^\s]+)\s*/",substr($line,$user_pos),$matches);
470         if ($sip_user == $matches[1]) {
471           $trunk = trim(substr($line,$host_pos,($user_pos-$host_pos)));
472           $trunk = preg_match("/^([^\s:]+)[:]{0,1}[\d]{0,5}\s*/",$trunk,$matches) ?  $matches[1] : $trunk;;
473           $state = trim(substr($line,$state_pos,($reg_pos-$state_pos)));
474           $status_arr[$trunk] = $state;
475         }
476       }
477     }
478   }
479   return $status_arr;
480 }
481
482 /* Very simple function to determine if an IP address is in the private range. This would
483    include:
484    192.168.*.*, 172.16-31.*.* and 10.*.*.*
485  */
486 function is_private_ip($address) {
487   if (preg_match('/^(192|172|10)\.(\d{1,3})\.\d{1,3}\.\d{1,3}$/',$address,$match)) {
488     switch($match[1]) {
489     case '10':
490       return true;
491       break;
492     case '192':
493       if ($match[2] == '168') {
494         return true;
495       } else {
496         return false;
497       }
498       break;
499     case '172':
500       if ($match[2] >= 16 && $match[2] <= 31) {
501         return true;
502       } else {
503         return false;
504       }
505       break;
506     }
507   } else {
508     return false;
509   }
510 }
511
512 function sipstation_get_config($account_key, $online=true, $filter_sections=array()) {
513   global $db;
514   global $ast_codec_hash;
515   global $codec_array;
516
517   if (!empty($account_key)) {
518     $json_array = array();
519     $xml_parser = sipstation_get_settings($account_key, $online);
520
521     if (!empty($xml_parser->data)) foreach ($xml_parser->data['xml'] as $key => $value) {
522       switch ($key) {
523         case 'xml_version':
524         case 'query_status':
525         case 'query_status_message':
526         case 'sip_username':
527         case 'sip_password':
528         case 'num_trunks':
529         case 'monthly_cost':
530         case 'cid_format':
531         case 'nat_troubleshooting':
532           if (!empty($filter_sections) && (!isset($filter_sections[$key]) || !$filter_sections[$key])) { continue; }
533           $json_array[$key] = trim("$value");
534         break;
535         case 'gateways':
536         case 'e911_address':
537         case 'registered_status':
538           if (!empty($filter_sections) && (!isset($filter_sections[$key]) || !$filter_sections[$key])) { continue; }
539           foreach ($value as $key2 => $value2) {
540             if (is_array($value2)) {
541               foreach ($value2 as $gw => $value3){
542                 $json_array[$key][$key2][$gw] = $value3 ? trim($value3) : '';
543               }
544             } else {
545               $json_array[$key][$key2] = $value2 ? trim($value2) : '';
546             }
547           }
548           if ($key == 'registered_status') {
549             foreach ($value as $key2 => $value2) {
550               if (is_array($value2)) {
551                 if ($value2['contact_ip'] == $value2['network_ip']) {
552                   $json_array[$key][$key2]['ips_match'] = 'yes';
553                 } else {
554                   $json_array[$key][$key2]['ips_match'] = is_private_ip($value2['contact_ip']) ? 'private' : 'no';
555                 }
556               } else {
557                 if ($key2 != 'registered') continue;
558                 if ($json_array[$key]['contact_ip'] == $json_array[$key]['network_ip'] && $value2 == 'yes') {
559                   $json_array[$key]['ips_match'] = 'yes';
560                 } else {
561                   $json_array[$key]['ips_match'] = 'no';
562                   $json_array[$key]['ips_match'] = is_private_ip($json_array[$key]['contact_ip']) ? 'private' : 'no';
563                 }
564               }
565             }
566           }
567         break;
568         case 'dids':
569           if (!empty($filter_sections) && (!isset($filter_sections[$key]) || !$filter_sections[$key])) { continue; }
570           if (!empty($value['did']) && !is_array($value['did'])) {
571             $tmp = $value['did'];
572             unset($value['did']);
573             $value['did'][] = $tmp;
574             $single = true;
575           } else {
576             $single = false;
577           }
578
579           $idx = 0;
580           foreach ($value['did'] as $did) {
581
582             $path = $single ? "/xml/dids/did" : "/xml/dids/did/$idx";
583             $idx++;
584             $failover = $xml_parser->attributes[$path]['failover'];
585
586             $did = trim($did);
587             $exten = core_did_get($did);
588             if (empty($exten)) {
589               $json_array[$key][$did] = array('destination' => 'blank', 'desc' => _("Not Set"), 'description' => '', 'failover' => "$failover");
590             } else {
591               $dest_results = framework_identify_destinations($exten['destination']);
592               if (is_array($dest_results[$exten['destination']])) {
593                 /* This is really bad but the calls to core_users get are so heavy and core_users_list don't give details
594                   that we will do this for now and deal with it later.
595                 */
596                 $user_cid_hash = array();
597                 $sql = "SELECT `extension`, `outboundcid` FROM `users`";
598                     $user_cids = $db->getAll($sql,DB_FETCHMODE_ASSOC);
599                     if(DB::IsError($user_cids)) {
600                   freepbx_debug("Failed trying to get user cids");
601                   freepbx_debug($user_cids->getMessage());
602                   $user_cids = array();
603                 }
604                 foreach ($user_cids as $item) {
605                   $user_cid_hash[$item['extension']] = $item['outboundcid'];
606                 }
607                 foreach ($dest_results[$exten['destination']] as $mod => $info) {
608                   //$destination = (substr($exten['destination'],0,15) == 'from-did-direct' ? $exten['destination'] : 'assigned');
609                   $is_checked = 0;
610                   if (substr($exten['destination'],0,15) == 'from-did-direct') {
611                     $destination = $exten['destination'];
612                     $exten_arr = explode(',',$destination);
613                     if (isset($exten_arr[1]) && isset($user_cid_hash[$exten_arr[1]])) {
614                       $is_checked = preg_match('/^\s*[<]?('.$did.')[>]?\s*$|^\s*"[^"]*"\s*<('.$did.')>\s*$/',$user_cid_hash[$exten_arr[1]]);
615                     } else {
616                       $is_checked = 0;
617                     }
618                   } else {
619                     $destination = 'assigned';
620                   }
621                   $json_array[$key][$did] = array('destination' => $destination, 'desc' => $info['description'], 'description' => $exten['description'], 'outboundcid' => $is_checked, 'failover' => "$failover");
622                   break;
623                 }
624               } else {
625                 $json_array[$key][$did] = array('destination' => 'blank', 'desc' => _("Not Set"), 'description' => '', 'failover' => "$failover");
626               }
627             }
628           }
629         break;
630         case 'asterisk_settings':
631           if (!empty($filter_sections) && (!isset($filter_sections[$key]) || !$filter_sections[$key])) { continue; }
632           $json_array['asterisk_settings']['peer'] = $value['peer']['setting'];
633         break;
634       case 'codecs':
635           if (!empty($filter_sections) && (!isset($filter_sections[$key]) || !$filter_sections[$key])) { continue; }
636           $codec_array = $value['codec'];
637           /* filter the Asterisk codec hash to only those that we are told are supported
638           */
639           $ast_codec_hash = array_filter(array_map('sipstation_supported_codecs',$ast_codec_hash));
640         break;
641       default:
642       }
643       $json_array['status'] = 'success';
644     } else {
645       $json_array['status'] = 'noserver';
646     }
647   } else {
648     $json_array['status'] = 'nokey';
649   }
650 return $json_array;
651 }
652
Note: See TracBrowser for help on using the browser.