forked from FreePBX/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.inc.php
5409 lines (4798 loc) · 260 KB
/
functions.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
if (!defined('FREEPBX_IS_AUTH')) { die('No direct script access allowed'); }
// vim: set ai ts=4 sw=4 ft=php:
// License for all code of this FreePBX module can be found in the license file inside the module directory
// Copyright 2006-2014 Schmooze Com Inc.
//
use FreePBX\modules\Core\Dialplan as Dialplan;
class core_conf {
var $_sip_general = array();
var $_sip_additional = array();
var $_sip_additionalsection = array();
var $_sip_notify = array();
var $_iax_general = array();
var $_iax_additional = array();
var $_dahdi_additional = array();
var $_featuregeneral = array();
var $_featuregeneralsection = array();
var $_featuremap = array();
var $_applicationmap = array();
var $_res_odbc = array();
var $dev_user_map;
// Static Object used for self-referencing.
private static $obj;
public function __construct() {
// Ensure the local object is available
self::$obj = $this;
}
public static function create() {
if (!isset(self::$obj))
self::$obj = new FreePBX();
return self::$obj;
}
// return an array of filenames to write
function get_filename() {
global $chan_dahdi;
$files = array(
'sip_additional.conf',
'sip_registrations.conf',
'iax_additional.conf',
'iax_registrations.conf',
'sip_general_additional.conf',
'iax_general_additional.conf',
'features_general_additional.conf',
'features_applicationmap_additional.conf',
'features_featuremap_additional.conf',
'localprefixes.conf',
'sip_notify_additional.conf',
'res_odbc_additional.conf',
'chan_dahdi_additional.conf',
'http_additional.conf',
'indications_general_additional.conf',
'indications_additional.conf'
);
return $files;
}
// return the output that goes in each of the files
function generateConf($file) {
global $version;
global $amp_conf;
switch ($file) {
case 'sip_general_additional.conf':
return $this->generate_sip_general_additional($version);
break;
case 'sip_additional.conf':
return $this->generate_sip_additional($version);
break;
case 'sip_registrations.conf':
return $this->generate_sip_registrations($version);
break;
case 'sip_notify_additional.conf':
return $this->generate_sip_notify_additional($version);
break;
case 'iax_general_additional.conf':
return $this->generate_iax_general_additional($version);
break;
case 'iax_additional.conf':
return $this->generate_iax_additional($version);
break;
case 'iax_registrations.conf':
return $this->generate_iax_registrations($version);
break;
case 'chan_dahdi_additional.conf':
return $this->generate_zapata_additional($version, 'dahdi').$this->generate_zapata_additional($version);
break;
case 'zapata_additional.conf':
return $this->generate_zapata_additional($version);
break;
case 'features_general_additional.conf':
return $this->generate_featuregeneral_additional($version);
break;
case 'features_applicationmap_additional.conf':
return $this->generate_applicationmap_additional($version);
break;
case 'features_featuremap_additional.conf':
return $this->generate_featuremap_additional($version);
break;
case 'res_odbc_additional.conf':
return $this->generate_res_odbc_additional($version);
break;
case 'http_additional.conf':
return $this->generate_http_additional($version);
break;
case 'indications_general_additional.conf':
return $this->generate_indications_general_additional($version);
break;
case 'indications_additional.conf':
return $this->generate_indications_additional($version);
break;
}
}
function addSipNotify($section,$entries) {
$this->_sip_notify[] = array('section' => $section, 'entries' => $entries);
}
function generate_sip_notify_additional($ast_version) {
$output = '';
if (isset($this->_sip_notify) && is_array($this->_sip_notify)) {
foreach ($this->_sip_notify as $section) {
$output .= "[".$section['section']."]\n";
foreach ($section['entries'] as $key => $value) {
if (strtolower($key) == 'content-length') {
continue;
}
$output .= "$key=>$value\n";
}
$output .= "\n";
}
}
return $output;
}
function addResOdbc($section,$entries) {
$this->_res_odbc[$section][] = $entries;
}
function generate_res_odbc_additional($ast_version) {
$output = '';
if (!empty($this->_res_odbc)) {
foreach ($this->_res_odbc as $section => $entries) {
$output .= "[".$section."]\n";
foreach ($entries as $key => $entry) {
foreach ($entry as $key => $value) {
$output .= "$key=>$value\n";
}
}
$output .= "\n";
}
}
return $output;
}
function generate_http_additional($ast_version) {
$freepbxConfCreate = freepbx_conf::create();
$freepbx_conf =& $freepbxConfCreate;
$output = "[general]\n";
$output .= "enabled=".($freepbx_conf->get_conf_setting('HTTPENABLED') ? 'yes' : 'no')."\n";
$output .= "enablestatic=".($freepbx_conf->get_conf_setting('HTTPENABLESTATIC') ? 'yes' : 'no')."\n";
$output .= "bindaddr=".$freepbx_conf->get_conf_setting('HTTPBINDADDRESS')."\n";
$output .= "bindport=".$freepbx_conf->get_conf_setting('HTTPBINDPORT')."\n";
$output .= "prefix=".$freepbx_conf->get_conf_setting('HTTPPREFIX')."\n";
$sessionlimit = $freepbx_conf->get_conf_setting('HTTPSESSIONLIMIT');
$output .= "sessionlimit=".((!empty($sessionlimit) && (int)$sessionlimit > 10) ? $sessionlimit : 100)."\n";
$output .= "session_inactivity=".$freepbx_conf->get_conf_setting('HTTPSESSIONINACTIVITY')."\n";
$output .= "session_keep_alive=".$freepbx_conf->get_conf_setting('HTTPSESSIONKEEPALIVE')."\n";
$tls = $freepbx_conf->get_conf_setting('HTTPTLSENABLE');
if ($tls) {
$output .= "tlsenable=yes\n";
// Is this an IPv6 address? If so, it needs brackets around it.
$bindaddr = $freepbx_conf->get_conf_setting('HTTPTLSBINDADDRESS');
if (filter_var($bindaddr, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) {
$bindaddr = "[$bindaddr]";
}
$output .= "tlsdisablev1=".($freepbx_conf->get_conf_setting('TLSDISABLEV1') ? 'yes' : 'no')."\n";
$output .= "tlsdisablev11=".($freepbx_conf->get_conf_setting('TLSDISABLEV11') ? 'yes' : 'no')."\n";
$output .= "tlsdisablev12=".($freepbx_conf->get_conf_setting('TLSDISABLEV12') ? 'yes' : 'no')."\n";
$output .= "tlsbindaddr=$bindaddr:".$freepbx_conf->get_conf_setting('HTTPTLSBINDPORT')."\n";
$output .= "tlscertfile=".$freepbx_conf->get_conf_setting('HTTPTLSCERTFILE')."\n";
$output .= "tlsprivatekey=".$freepbx_conf->get_conf_setting('HTTPTLSPRIVATEKEY')."\n";
}
return $output;
}
function generate_indications_general_additional($ast_version) {
global $amp_conf;
$output = '';
$country = $amp_conf['TONEZONE'];
$output .= "country=".$country."\n";
return $output;
}
function generate_indications_additional($ast_version) {
global $amp_conf;
$output = '';
$country = $amp_conf['TONEZONE'];
$didlist = \FreePBX::Core()->getAllDIDs();
if(is_array($didlist)){
foreach($didlist as $item) {
if($item['indication_zone'] == "default" || $item['indication_zone'] == $country){
continue;
}
$zonelist = core_indications_get($item['indication_zone']);
$output .= "[".$item['indication_zone']."]\n";
$output .= "description = {$zonelist['name']}\n";
$output .= $zonelist['conf']."\n\n";
}
}
$zonelist = core_indications_get($country);
$output .= "[".$country."]\n";
$output .= "description = {$zonelist['name']}\n";
$output .= $zonelist['conf']."\n\n";
return $output;
}
function addSipAdditional($section, $key, $value) {
$this->_sip_additional[$section][] = array('key' => $key, 'value' => $value);
}
function addSipAdditionalSection($section) {
$this->_sip_additionalsection[] = $section;
}
function addSipGeneral($key, $value) {
$this->_sip_general[] = array('key' => $key, 'value' => $value);
}
function generate_sip_general_additional($ast_version) {
$output = '';
if (isset($this->_sip_general) && is_array($this->_sip_general)) {
foreach ($this->_sip_general as $values) {
$output .= $values['key']."=".$values['value']."\n";
}
}
return $output;
}
function addIaxGeneral($key, $value) {
$this->_iax_general[] = array('key' => $key, 'value' => $value);
}
function generate_iax_general_additional($ast_version) {
$output = '';
if (isset($this->_iax_general) && is_array($this->_iax_general)) {
foreach ($this->_iax_general as $values) {
$output .= $values['key']."=".$values['value']."\n";
}
}
return $output;
}
function addFeatureGeneral($key, $value) {
$this->_featuregeneral[] = array('key' => $key, 'value' => $value);
}
function addFeatureGeneralSection($section, $key, $value) {
$this->_featuregeneralsection[$section][] = array('key' => $key, 'value' => $value);
}
function generate_featuregeneral_additional($ast_version) {
$output = '';
if (isset($this->_featuregeneral) && is_array($this->_featuregeneral)) {
foreach ($this->_featuregeneral as $values) {
$output .= $values['key']."=".$values['value']."\n";
}
}
foreach ($this->_featuregeneralsection as $section => $values) {
$output .= "\n[$section]\n";
foreach ($values as $value) {
$output .= $value['key'] . "=" . $value['value'] . "\n";
}
}
return $output;
}
function addFeatureMap($key, $value) {
$this->_featuremap[] = array('key' => $key, 'value' => $value);
}
function generate_featuremap_additional($ast_version) {
$output = '';
if (isset($this->_featuremap) && is_array($this->_featuremap)) {
foreach ($this->_featuremap as $values) {
$output .= $values['key']."=".$values['value']."\n";
}
}
return $output;
}
function addApplicationMap($key, $value, $add_to_dynamic_features=false) {
global $ext;
$this->_applicationmap[] = array('key' => $key, 'value' => $value);
//
// Now add it to the DYNAMIC_FEATURES
// TODO: one caveat, if we ever want to make such an application conditional, we will have to change
// this as for now it makes it for everyone.
//
if ($add_to_dynamic_features) {
$ext->_globals['DYNAMIC_FEATURES'] = empty($ext->_globals['DYNAMIC_FEATURES']) ? $key : $ext->_globals['DYNAMIC_FEATURES'] . '#' . $key;
}
}
function generate_applicationmap_additional($ast_version) {
$output = '';
if (isset($this->_applicationmap) && is_array($this->_applicationmap)) {
foreach ($this->_applicationmap as $values) {
$output .= $values['key']."=>".$values['value']."\n";
}
}
return $output;
}
function generate_sip_additional($ast_version) {
global $db;
$table_name = "sip";
$additional = "";
$finaloutput = "";
if (version_compare($ast_version, "11.5", "ge")) {
$ver115 = true;
} else {
$ver115 = false;
}
// TODO: Temporary Kludge until CCSS is fixed
//
if (function_exists('campon_get_config')) {
$cc_monitor_policy = "cc_monitor_policy=generic\n";
} else {
$cc_monitor_policy = "";
}
$sql = "SELECT tech.keyword,tech.data from $table_name as tech LEFT OUTER JOIN trunks on (tech.id = CONCAT('tr-peer-',trunks.trunkid) OR tech.id = CONCAT('tr-user-',trunks.trunkid)) where tech.id=-1 and tech.keyword <> 'account' and (trunks.disabled = 'off' OR trunks.disabled IS NULL)";
$results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
if(DB::IsError($results)) {
die($results->getMessage());
}
foreach ($results as $result) {
$option = $result['data'];
switch (strtolower($result['keyword'])) {
case 'insecure':
if ($option == 'very') {
$additional .= "insecure=port,invite\n";
} else if ($option == 'yes') {
$additional .= "insecure=port\n";
} else {
$additional .= $result['keyword']."=$option\n";
}
break;
case 'allow':
case 'disallow':
$opt = FreePBX::Core()->filterValidCodecs($option);
if(!empty($opt)) {
$additional .= $result['keyword']."=".FreePBX::Core()->filterValidCodecs($opt)."\n";
}
break;
case 'accountcode':
$additional .= $result['keyword']."=$option\n";
break;
default:
$additional .= $result['keyword']."=$option\n";
}
}
$sql = "SELECT tech.data,tech.id from $table_name as tech LEFT OUTER JOIN trunks on (tech.id = CONCAT('tr-peer-',trunks.trunkid) OR tech.id = CONCAT('tr-user-',trunks.trunkid)) where tech.keyword='account' and (trunks.disabled = 'off' OR trunks.disabled IS NULL) group by data,id";
$results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
if(DB::IsError($results)) {
die($results->getMessage());
}
if(FreePBX::Modules()->moduleHasMethod("sipsettings","getChanSipSettings")) {
$sipsettings = FreePBX::Sipsettings()->getChanSipSettings();
} else {
$sipsettings = array();
}
$usedAccounts = array();
foreach ($results as $result) {
$output = "";
$account = $result['data'];
$usedAccounts[] = $account;
$id = $result['id'];
$sql = "SELECT tech.keyword,tech.data from $table_name tech LEFT OUTER JOIN trunks on (tech.id = CONCAT('tr-peer-',trunks.trunkid) OR tech.id = CONCAT('tr-user-',trunks.trunkid)) where tech.id='$id' and tech.keyword <> 'account' and (trunks.disabled = 'off' OR trunks.disabled IS NULL) order by flags, keyword DESC";
$results2_pre = $db->getAll($sql, DB_FETCHMODE_ASSOC);
if(DB::IsError($results2_pre)) {
die($results2->getMessage());
}
$fcc = new featurecode('core', 'automon');
$code = $fcc->getCodeActive();
unset($fcc);
$enableRecordingFeature = ($code != '');
// Move all 'disallow=all' and 'deny' to the top to avoid errors
//
$results2 = array();
foreach ($results2_pre as $element) {
if($element['keyword'] == 'sipdriver' && $element['data'] == 'chan_pjsip'){
continue(2);
}
if($element['keyword'] == 'sipdriver'){
continue;
}
if(!empty($sipsettings) && $element['keyword'] == 'transport' && !empty($element['data'])) {
$prts = explode(",",$element['data']);
$itms = array("tlsenable" => 'tls',"tcpenable" => 'tcp');
foreach($itms as $x => $z) {
if((!isset($sipsettings[$x]) || $sipsettings[$x] == 'no') && in_array($z,$prts)) {
$ky = array_search($z,$prts);
if($ky !== false) {
unset($prts[$ky]);
}
}
}
$element['data'] = implode(",",$prts);
}
if (strtolower(freepbx_trim ($element['keyword'])) != 'secret') {
$options = explode("&", $element['data']);
foreach ($options as $option) {
if (($element['keyword'] == 'disallow' && $option == 'all') | ($element['keyword'] == 'deny')) {
array_unshift($results2,array('keyword'=>$element['keyword'],'data'=>$option));
} else {
$results2[] = array('keyword'=>$element['keyword'],'data'=>$option);
}
}
} else {
$results2[] = array('keyword'=>$element['keyword'],'data'=>str_replace(';','\;',$element['data']));
}
}
unset($results2_pre);
$output .= "[$account]\n";
$context='';
foreach ($results2 as $result2) {
$option = strtolower($result2['data']);
switch (strtolower($result2['keyword'])) {
case 'sessiontimers':
$output .= "session-timers=".$result2['data']."\n";
break;
case 'videosupport':
if($result2['data'] != 'inherit') {
$output .= "videosupport=".$result2['data']."\n";
}
break;
case 'insecure':
if ($option == 'very')
$output .= "insecure=port,invite\n";
else if ($option == 'yes')
$output .= "insecure=port\n";
else
$output .= $result2['keyword']."=".$result2['data']."\n";
break;
case 'allow':
case 'disallow':
$opt = FreePBX::Core()->filterValidCodecs($result2['data']);
if(!empty($opt)) {
$output .= $result2['keyword']."=".$opt."\n";
}
break;
case 'accountcode':
$output .= $result2['keyword']."=".$result2['data']."\n";
break;
case 'callerid':
case 'mailbox':
$output .= FreePBX\modules\Core\Driver::map_dev_user($account, $result2['keyword'], $result2['data']);
break;
case 'secret_origional':
//stupidness coming through
break;
case 'username':
//http://issues.freepbx.org/browse/FREEPBX-7715
$output .= "username=".$result2['data']."\n";
break;
case 'nat':
//http://issues.freepbx.org/browse/FREEPBX-6518
if($ver115) {
$newval = "";
switch($result2['data']) {
case 'yes':
$newval = "force_rport,comedia";
break;
case 'route':
$newval = "force_rport";
break;
case 'never':
$newval = "no";
break;
default:
$newval = $result2['data'];
break;
}
$output .= $result2['keyword']."=".$newval."\n";
} else {
$output .= $result2['keyword']."=".$result2['data']."\n";
}
break;
case 'context':
$context = $result2['data'];
//fall-through
default:
$output .= $result2['keyword']."=".$result2['data']."\n";
}
}
switch (substr($id,0,8)) {
case 'tr-peer-':
if ($context == '') {
$output .= "context=from-trunk-sip-$account\n";
}
break;
case 'tr-user-':
if ($context == '') {
$tn = substr($id, 8);
// this is a 'user' trunk, we need to get the name of the corresponding 'peer'
// trunk so we can set the context appropriately for the group count
//
$td = core_trunks_getDetails($tn);
if (isset($td['channelid'])) {
$output .= "context=from-trunk-sip-".$td['channelid']."\n";
}
}
break;
default:
if($enableRecordingFeature) {
$output .= "recordonfeature=apprecord\n";
$output .= "recordofffeature=apprecord\n";
}
$output .= "callcounter=yes\n";
$output .= "faxdetect=no\n";
if ($cc_monitor_policy) {
$output .= $cc_monitor_policy;
}
}
if (isset($this->_sip_additional[$account])) {
foreach ($this->_sip_additional[$account] as $asetting) {
$output .= $asetting['key'] . "=" . $asetting['value'] . "\n";
}
}
$output .= $additional."\n";
$finaloutput .= $output;
}
foreach($this->_sip_additionalsection as $section) {
if(!in_array($section,$usedAccounts) && !empty($this->_sip_additional[$section])) {
$output = "[".$section."]\n";
foreach ($this->_sip_additional[$section] as $asetting) {
$output .= $asetting['key'] . "=" . $asetting['value'] . "\n";
}
$finaloutput .= $output;
} elseif(in_array($section,$usedAccounts)) {
throw new \Exception(sprintf(_("%s is already in use sip.conf. Can not continue"),$section));
}
}
return $finaloutput;
}
function generate_sip_registrations($ast_version) {
global $db;
$table_name = "sip";
$output = "";
$sql = "SELECT tech.keyword,tech.data FROM $table_name as tech LEFT OUTER JOIN trunks on tech.id = CONCAT('tr-reg-',trunks.trunkid) WHERE `id` LIKE 'tr-reg-%' AND tech.keyword <> 'account' AND (trunks.disabled = 'off' OR trunks.disabled IS NULL)";
$results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
if(DB::IsError($results)) {
die($results->getMessage());
}
foreach ($results as $result) {
$output .= $result['keyword']."=".$result['data']."\n";
}
return $output;
}
function addIaxAdditional($section, $key, $value) {
$this->_iax_additional[$section][] = array('key' => $key, 'value' => $value);
}
function generate_iax_additional($ast_version) {
global $db;
$table_name = "iax";
$additional = "";
$output = "";
$sql = "SELECT tech.keyword,tech.data from $table_name as tech LEFT OUTER JOIN trunks on (tech.id = CONCAT('tr-peer-',trunks.trunkid) OR tech.id = CONCAT('tr-user-',trunks.trunkid)) where tech.id = -1 and tech.keyword <> 'account' and (trunks.disabled = 'off' OR trunks.disabled IS NULL)";
$results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
if(DB::IsError($results)) {
die($results->getMessage());
}
foreach ($results as $result) {
$option = $result['data'];
switch ($result['keyword']) {
case 'notransfer':
if (strtolower($option) == 'yes') {
$additional .= "transfer=no\n";
} else if (strtolower($option) == 'no') {
$additional .= "transfer=yes\n";
} else if (strtolower($option) == 'mediaonly') {
$additional .= "transfer=mediaonly\n";
} else {
$additional .= $result['keyword']."=$option\n";
}
break;
case 'allow':
case 'disallow':
$opt = FreePBX::Core()->filterValidCodecs($option);
if(!empty($opt)) {
$additional .= $result['keyword']."=".FreePBX::Core()->filterValidCodecs($opt)."\n";
}
break;
case 'accountcode':
$additional .= $result['keyword']."=$option\n";
break;
case 'requirecalltoken':
if ($option != '') {
$additional .= $result['keyword']."=$option\n";
}
break;
default:
$additional .= $result['keyword']."=$option\n";
}
}
$sql = "SELECT tech.data,tech.id from $table_name as tech LEFT OUTER JOIN trunks on (tech.id = CONCAT('tr-peer-',trunks.trunkid) OR tech.id = CONCAT('tr-user-',trunks.trunkid)) where tech.keyword='account' and (trunks.disabled = 'off' OR trunks.disabled IS NULL) group by data,id";
$results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
if(DB::IsError($results)) {
die($results->getMessage());
}
foreach ($results as $result) {
$account = $result['data'];
$id = $result['id'];
$output .= "[$account]\n";
$sql = "SELECT tech.keyword,tech.data from $table_name tech LEFT OUTER JOIN trunks on (tech.id = CONCAT('tr-peer-',trunks.trunkid) OR tech.id = CONCAT('tr-user-',trunks.trunkid)) where tech.id='$id' and tech.keyword <> 'account' and (trunks.disabled = 'off' OR trunks.disabled IS NULL) order by flags, keyword DESC";
$results2_pre = $db->getAll($sql, DB_FETCHMODE_ASSOC);
if(DB::IsError($results2_pre)) {
die($results2_pre->getMessage());
}
// Move all 'disallow=all' and 'deny=' to the top to avoid errors
//
$results2 = array();
foreach ($results2_pre as $element) {
if (strtolower(freepbx_trim ($element['keyword'])) != 'secret') {
$options = explode("&", $element['data']);
foreach ($options as $option) {
if (($element['keyword'] == 'disallow' && $option == 'all') | ($element['keyword'] == 'deny')) {
array_unshift($results2,array('keyword'=>$element['keyword'],'data'=>$option));
} else {
$results2[] = array('keyword'=>$element['keyword'],'data'=>$option);
}
}
} else {
$results2[] = array('keyword'=>$element['keyword'],'data'=>str_replace(';','\;',$element['data']));
}
}
unset($results2_pre);
$context='';
foreach ($results2 as $result2) {
$option = strtolower($result2['data']);
switch ($result2['keyword']) {
case 'notransfer':
if (strtolower($option) == 'yes') {
$output .= "transfer=no\n";
} else if (strtolower($option) == 'no') {
$output .= "transfer=yes\n";
} else if (strtolower($option) == 'mediaonly') {
$output .= "transfer=mediaonly\n";
} else {
$output .= $result2['keyword']."=".$result2['data']."\n";
}
break;
case 'allow':
case 'disallow':
$opt = FreePBX::Core()->filterValidCodecs($result2['data']);
if(!empty($opt)) {
$output .= $result2['keyword']."=".FreePBX::Core()->filterValidCodecs($opt)."\n";
}
break;
case 'accountcode':
$output .= $result2['keyword']."=".$result2['data']."\n";
break;
case 'requirecalltoken':
if ($option != '') {
$output .= $result2['keyword']."=".$result2['data']."\n";
}
break;
case 'callerid':
case 'mailbox':
$output .= FreePBX\modules\Core\Driver::map_dev_user($account, $result2['keyword'], $result2['data']);
break;
case 'context':
$context = $option;
//fall-through
default:
$output .= $result2['keyword']."=".$result2['data']."\n";
}
}
switch (substr($id,0,8)) {
case 'tr-peer-':
if ($context == '') {
$output .= "context=from-trunk-iax2-$account\n";
}
break;
case 'tr-user-':
if ($context == '') {
$tn = substr($id, 8);
// this is a 'user' trunk, we need to get the name of the corresponding 'peer'
// trunk so we can set the context appropriately for the group count
//
$td = core_trunks_getDetails($tn);
if (isset($td['channelid'])) {
$output .= "context=from-trunk-iax2-".$td['channelid']."\n";
}
}
break;
default:
}
if (isset($this->_iax_additional[$account])) {
foreach ($this->_iax_additional[$account] as $asetting) {
$output .= $asetting['key'] . "=" . $asetting['value'] . "\n";
}
}
$output .= $additional."\n";
}
return $output;
}
function generate_iax_registrations($ast_version) {
global $db;
$table_name = "iax";
$output = "";
$sql = "SELECT tech.keyword,tech.data from $table_name as tech LEFT OUTER JOIN trunks on tech.id = CONCAT('tr-reg-',trunks.trunkid) WHERE `id` LIKE 'tr-reg-%' AND tech.keyword <> 'account' AND (trunks.disabled = 'off' OR trunks.disabled IS NULL)";
$results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
if(DB::IsError($results)) {
die($results->getMessage());
}
foreach ($results as $result) {
$output .= $result['keyword']."=".$result['data']."\n";
}
return $output;
}
function addDahdiAdditional($section, $key, $value) {
$this->_dahdi_additional[$section][] = array('key' => $key, 'value' => $value);
}
function generate_zapata_additional($ast_version, $table_name = 'zap') {
global $db;
$additional = "";
$output = '';
$sql = "SELECT tech.keyword,tech.data from $table_name as tech LEFT OUTER JOIN trunks on tech.id = CONCAT('tr-peer-',trunks.trunkid) OR tech.id = CONCAT('tr-user-',trunks.trunkid) where tech.keyword <> 'account' and tech.id = -1 and (trunks.disabled = 'off' OR trunks.disabled IS NULL)";
$results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
if(DB::IsError($results)) {
if($table_name == 'zap') {
return '';
} else {
die($results->getMessage());
}
}
foreach ($results as $result) {
$additional .= $result['keyword']."=".$result['data']."\n";
}
$sql = "SELECT data,id from $table_name where keyword='account' and flags <> 1 group by data,id";
$results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
if(DB::IsError($results)) {
die($results->getMessage());
}
foreach ($results as $result) {
$account = $result['data'];
$id = $result['id'];
$output .= ";;;;;;[$account]\n";
$sql = "SELECT tech.keyword,tech.data from $table_name tech LEFT OUTER JOIN trunks on (tech.id = CONCAT('tr-peer-',trunks.trunkid) OR tech.id = CONCAT('tr-user-',trunks.trunkid)) where tech.keyword <> 'account' and tech.id = $id and (trunks.disabled = 'off' OR trunks.disabled IS NULL)";
$results2 = $db->getAll($sql, DB_FETCHMODE_ASSOC);
if(DB::IsError($results2)) {
die($results2->getMessage());
}
$zapchannel="";
foreach ($results2 as $result2) {
switch ($result2['keyword']) {
case 'channel':
$zapchannel = $result2['data'];
break;
// These are not zapata.conf variables so keep out of file
case 'dial':
break;
case 'callerid':
case 'mailbox':
$output .= FreePBX\modules\Core\Driver::map_dev_user($account, $result2['keyword'], $result2['data']);
break;
default:
$output .= $result2['keyword']."=".$result2['data']."\n";
}
}
if (isset($this->_dahdi_additional[$account])) {
foreach ($this->_dahdi_additional[$account] as $asetting) {
$output .= $asetting['key'] . "=" . $asetting['value'] . "\n";
}
}
$output .= $additional ? $additional."\n" : '';
$output .= "channel=>$zapchannel\n";
}
return $output;
}
}
include __DIR__.'/functions.inc/functions.deprecated.php';
function core_destination_popovers() {
global $amp_conf;
if ($amp_conf['AMPEXTENSIONS'] == "deviceanduser") {
$ret['users'] = 'Users';
} else {
$ret['extensions'] = 'Extensions';
// adding trunk in popover
$ret['trunks'] = 'Trunks';
$ret['did'] = 'Inbound Routes';
}
return $ret;
}
// The destinations this module provides
// returns a associative arrays with keys 'destination' and 'description'
function core_destinations() {
global $amp_conf;
//static destinations
$extens = array();
$category = "Terminate Call";
$ds_id = 'blackhole';
$extens[] = array('destination' => 'app-blackhole,hangup,1', 'description' => _("Hangup"), 'category' => $category, 'id' => $ds_id);
$extens[] = array('destination' => 'app-blackhole,congestion,1', 'description' => _("Congestion"), 'category' => $category, 'id' => $ds_id);
$extens[] = array('destination' => 'app-blackhole,busy,1', 'description' => _("Busy"), 'category' => $category, 'id' => $ds_id);
$extens[] = array('destination' => 'app-blackhole,zapateller,1', 'description' => _("Play SIT Tone (Zapateller)"), 'category' => $category, 'id' => $ds_id);
$extens[] = array('destination' => 'app-blackhole,musiconhold,1', 'description' => _("Put caller on hold forever"), 'category' => $category, 'id' => $ds_id);
$extens[] = array('destination' => 'app-blackhole,ring,1', 'description' => _("Play ringtones to caller until they hangup"), 'category' => $category, 'id' => $ds_id);
$extens[] = array('destination' => 'app-blackhole,no-service,1', 'description' => _("Play no service message"), 'category' => $category, 'id' => $ds_id);
//get the list of meetmes
$results = core_users_list();
$vmboxes = array();
if (isset($results) && function_exists('voicemail_getVoicemail')) {
//get voicemail
$uservm = voicemail_getVoicemail();
$vmcontexts = array_keys($uservm);
foreach ($results as $thisext) {
$extnum = $thisext[0];
// search vm contexts for this extensions mailbox
foreach ($vmcontexts as $vmcontext) {
if(isset($uservm[$vmcontext][$extnum])){
//$vmname = $uservm[$vmcontext][$extnum]['name'];
//$vmboxes[$extnum] = array($extnum, '"' . $vmname . '" <' . $extnum . '>');
$vmboxes[$extnum] = true;
}
}
}
}
// return an associative array with destination and description
// core provides both users and voicemail boxes as destinations
if (isset($results)) {
$cat_id = ($amp_conf['AMPEXTENSIONS'] == "deviceanduser")?'users':'extensions';
$cat = ($amp_conf['AMPEXTENSIONS'] == "deviceanduser")?'Users':'Extensions';
foreach($results as $result) {
$extens[] = array('destination' => 'from-did-direct,'.$result['0'].',1', 'description' => ' '.$result['0'].' '.$result['1'], 'category' => $cat, 'id' => $cat_id);
if(isset($vmboxes[$result['0']])) {
$extens[] = array('destination' => 'ext-local,vmb'.$result['0'].',1', 'description' => $result[0].' '.$result[1].' (Busy Message)', 'category' => 'Voicemail', 'id' => 'voicemail');
$extens[] = array('destination' => 'ext-local,vmu'.$result['0'].',1', 'description' => $result[0].' '.$result[1].' (Unavailable Message)', 'category' => 'Voicemail', 'id' => 'voicemail');
$extens[] = array('destination' => 'ext-local,vms'.$result['0'].',1', 'description' => $result[0].' '.$result[1].' (No Message)', 'category' => 'Voicemail', 'id' => 'voicemail');
$extens[] = array('destination' => 'ext-local,vmi'.$result['0'].',1', 'description' => $result[0].' '.$result[1].' (Instructions Only)', 'category' => 'Voicemail', 'id' => 'voicemail');
}
}
}
$trunklist = core_trunks_listbyid(true);
if (is_array($trunklist)) foreach ($trunklist as $trunk) {
switch($trunk['tech']) {
case 'enum':
break;
default:
$extens[] = array('destination' => 'ext-trunk,'.$trunk['trunkid'].',1', 'description' => $trunk['name'].' ('.$trunk['tech'].')', 'category' => 'Trunks', 'id' => 'trunks');
break;
}
}
$didlist = core_did_list();
if (is_array($didlist)) {
foreach ($didlist as $did) {
$extens[] = array('destination' => 'from-trunk,'.$did['extension'].',1', 'description' => $did['description'].'('.$did['extension'].')', 'category' => 'Inbound Routes', 'id' => 'did');
}
}
return $extens;
}
function core_getdest($exten) {
$dests[] = 'from-did-direct,'.$exten.',1';
$dests[] = 'ext-trunk,'.$exten.',1';
if (!function_exists('voicemail_mailbox_get')) {
return $dests;
}
$box = voicemail_mailbox_get($exten);
if ($box == null) {
return $dests;
}
$dests[] = 'ext-local,vmb'.$exten.',1';
$dests[] = 'ext-local,vmu'.$exten.',1';
$dests[] = 'ext-local,vms'.$exten.',1';
$dests[] = 'ext-local,vmi'.$exten.',1';
return $dests;
}
function core_getdestinfo($dest) {
global $amp_conf;
global $active_modules;
// Check for Extension Number Destinations
//
$users = \FreePBX::Core()->getAllUsers();
if (substr(freepbx_trim ($dest),0,11) == 'from-trunk,') {
$did = explode(',',$dest);
$did = $did[1];
return array('description' => sprintf(_('Inbound Routes : %s'),$did),
'edit_url' => "config.php?display=did&view=form&extdisplay=$did%2F");
}
if (substr(freepbx_trim ($dest),0,16) == 'from-did-direct,') {
$exten = explode(',',$dest);
$exten = $exten[1];
if (!isset($users[$exten])) {
return array();
} else {
$name = isset($users[$exten]['name'])?$users[$exten]['name']:'';
$display = ($amp_conf['AMPEXTENSIONS'] == "deviceanduser")?'users':'extensions';
$data = $users[$exten];
$data['gqltype'] = 'user';
return array(
'description' => sprintf(_("User Extension %s: %s"),$exten,$name),
'edit_url' => "config.php?type=setup&display=$display&extdisplay=".urlencode($exten)."&skip=0",
'data' => $data
);
}
} else if (substr(freepbx_trim ($dest),0,10) == 'ext-trunk,') {
$exten = explode(',',$dest);
$exten = $exten[1];
$thisexten = core_trunks_getDetails($exten);
if (empty($thisexten)) {
return array();
} else {
$display = 'trunks';
$name = isset($thisexten['name']) && $thisexten['name'] ? $thisexten['name'] : '';
$data = $thisexten;
$data['gqltype'] = 'trunk';
return array(
'description' => sprintf(_('Trunk: %s (%s)'),$name,$thisexten['tech']),
'edit_url' => "config.php?type=setup&display=$display&extdisplay=OUT_".urlencode($exten),
'data' => $data
);
}
// Check for voicemail box destinations
//
} else if (substr(freepbx_trim ($dest),0,12) == 'ext-local,vm') {
$exten = explode(',',$dest);
$exten = substr($exten[1],3);
if (!function_exists('voicemail_mailbox_get')) {
return array();
}
if (!isset($users[$exten])) {
return array();
}
$box = voicemail_mailbox_get($exten);
if ($box == null) {