-
Notifications
You must be signed in to change notification settings - Fork 16
/
functions.inc.php
537 lines (477 loc) · 17.4 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
<?php
/* $Id:$ */
if (!defined('FREEPBX_IS_AUTH')) { die('No direct script access allowed'); }
// License for all code of this FreePBX module can be found in the license file inside the module directory
// Copyright 2013 Schmooze Com Inc.
//
// Use hookGet_config so that everyone (like core) will have written their
// SIP settings and then we can remove any that we are going to override
//
/* Field Values for type field */
define('SIP_NORMAL','0');
define('SIP_CODEC','1');
define('SIP_VIDEO_CODEC','2');
define('SIP_CUSTOM','9');
function set_prev_owner($owner) {
global $prev_owner;
$prev_owner = $owner;
}
function sipsettings_process_errors($errors) {
$error_display = [];
foreach($errors as $error) {
$error_display[] = ['js' => "$('#".$error['id']."').addClass('validation-error');\n", 'div' => $error['message']];
}
return $error_display;
}
class sipsettings_validate {
public $errors = [];
/* checks if value is an integer */
function is_int($value, $item, $message, $negative=false) {
$value = trim((string) $value);
if($value == "-1"){
return $value;
}
else{
if ($value != '' && $negative) {
$tmp_value = str_starts_with($value, '-') ? substr($value,1) : $value;
if (!ctype_digit($tmp_value)) {
$this->errors[] = ['id' => $item, 'value' => $value, 'message' => $message];
}
} elseif (!$negative) {
if (!ctype_digit($value) || ($value < 0 )) {
$this->errors[] = ['id' => $item, 'value' => $value, 'message' => $message];
}
}
return $value;
}
}
/* checks if value is valid port between 1024 - 65535 */
function is_ip_port($value, $item, $message) {
$value = trim((string) $value);
if ($value != '' && (!ctype_digit($value) || $value < 1024 || $value > 65535)) {
$this->errors[] = ['id' => $item, 'value' => $value, 'message' => $message];
}
return $value;
}
/* checks if value is valid ip format */
function is_ip($value, $item, $message, $ipv6_ok=false) {
$value = trim((string) $value);
if ($value != '' && !preg_match('|^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$|',$value,$matches)) {
$regex = '/^\s*((?=.*::.*)(::)?([0-9A-F]{1,4}(:(?=[0-9A-F])|(?!\2)(?!\5)(::)|\z)){0,7}|((?=.*::.*)(::)?([0-9A-F]{1,4}(:(?=[0-9A-F])|(?!\7)(?!\10)(::))){0,5}|([0-9A-F]{1,4}:){6})((25[0-5]|(2[0-4]|1[0-9]|[1-9]?)[0-9])(\.(?=.)|\z)){4}|([0-9A-F]{1,4}:){7}[0-9A-F]{1,4})\s*$/i';
if ($ipv6_ok && ($value == '::' || preg_match($regex,$value, $matches))) {
return $value;
} else {
$this->errors[] = ['id' => $item, 'value' => $value, 'message' => $message];
}
}
return $value;
}
/* checks if value is valid ip netmask format */
function is_netmask($value, $item, $message) {
$value = trim((string) $value);
if ($value != '' && !(preg_match('|^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$|',$value,$matches) || (ctype_digit($value) && $value >= 0 && $value <= 24))) {
$this->errors[] = ['id' => $item, 'value' => $value, 'message' => $message];
}
return $value;
}
/* checks if value is valid alpha numeric format */
function is_alphanumeric($value, $item, $message) {
$value = trim((string) $value);
if ($value != '' && !preg_match("/^\s*([a-zA-Z0-9.&\-@_!<>!\"\']+)\s*$/",$value,$matches)) {
$this->errors[] = ['id' => $item, 'value' => $value, 'message' => $message];
}
return $value;
}
/* trigger a validation error to be appended to this class */
function log_error($value, $item, $message) {
$this->errors[] = ['id' => $item, 'value' => $value, 'message' => $message];
return $value;
}
}
function sipsettings_hookGet_config($engine) {
global $core_conf;
global $ext; // is this the best way to pass this?
switch($engine) {
case "asterisk":
if (isset($core_conf) && is_a($core_conf, "core_conf")) {
$raw_settings = sipsettings_get(true);
/* TODO: This is example concept code
The only real conflicts are codecs (mainly cause
it will look ugly. So we should strip those but
leave the rest. If we overrite it, oh well
*/
$idx = 0;
foreach ($core_conf->_sip_general as $entry) {
switch (strtolower((string) $entry['key'])) {
case 'allow':
case 'disallow':
unset($core_conf->_sip_general[$idx]);
break;
default:
// do nothing
}
$idx++;
}
$interim_settings = [];
foreach ($raw_settings as $var) {
switch ($var['type']) {
case SIP_NORMAL:
$interim_settings[$var['keyword']] = $var['data'];
break;
case SIP_CUSTOM:
$sip_settings[] = [$var['keyword'], $var['data']];
break;
default:
// Error should be above
}
}
unset($raw_settings);
// Add any defaults that should be in there
$def = FreePBX::Sipsettings()->getChanSipDefaults();
foreach ($def as $k => $v) {
if (!isset($interim_settings[$k]) && $v) {
$interim_settings[$k] = $v;
}
}
/* Codecs First */
$core_conf->addSipGeneral('disallow','all');
foreach (FreePBX::Sipsettings()->getCodecs('audio') as $codec => $enabled) {
if ($enabled != '') {
$core_conf->addSipGeneral('allow',$codec);
}
}
unset($codecs);
if ($interim_settings['videosupport'] == 'yes') {
foreach (FreePBX::Sipsettings()->getCodecs('video') as $codec => $enabled) {
if ($enabled != '') {
$core_conf->addSipGeneral('allow',$codec);
}
}
}
unset($video_codecs);
/* next figure out what we need to write out (deal with things like nat combos, etc. */
$nat_mode = $interim_settings['nat_mode'];
$jbenable = $interim_settings['jbenable'];
$foundexternip = false;
// Ensure default TLS Settings for chansip are available
if(empty($interim_settings['tlsbindport'])) {
// Note - this is TCP, not UDP.
$interim_settings['tlsbindport'] = 5061;
}
if(!empty($interim_settings['tlsbindaddr'])) {
$interim_settings['tlsbindaddr'] = ($interim_settings['tlsbindaddr'] === '::' ? '[::]' : $interim_settings['tlsbindaddr']).":".$interim_settings['tlsbindport'];
} else {
// [::] means 'listen on all interfaces, both ipv4 and ipv6' when in sipsettings.
$interim_settings['tlsbindaddr'] = "[::]:".$interim_settings['tlsbindport'];
}
// There is no sip setting 'tlsbindport', so make sure we remove it before writing the file.
unset($interim_settings['tlsbindport']);
foreach ($interim_settings as $key => $value) {
switch ($key) {
case 'csipcertid':
if(!empty($value) && $interim_settings['tlsenable'] == 'yes' && FreePBX::Modules()->moduleHasMethod("certman","getDefaultCertDetails")) {
$cert = FreePBX::Certman()->getCertificateDetails($value);
if(!empty($cert['files']['crt']) && !empty($cert['files']['key'])) {
$sip_settings[] = ['tlsprivatekey', $cert['files']['key']];
$sip_settings[] = ['tlscertfile', $cert['files']['pem']];
}
}
break;
case 'nat_mode':
break;
case 'externhost_val':
if ($nat_mode == 'externhost' && $value != '') {
$sip_settings[] = ['externhost', $value];
}
break;
case 'externrefresh':
if ($nat_mode == 'externhost' && $value != '') {
$sip_settings[] = [$key, $value];
}
break;
case 'externip_val':
if ($nat_mode == 'externip' && $value != '') {
$foundexternip = true;
$sip_settings[] = ['externip', $value];
}
break;
case 'jbforce':
case 'jbimpl':
case 'jbmaxsize':
case 'jbresyncthreshold':
case 'jblog':
if ($jbenable == 'yes' && $value != '') {
$sip_settings[] = [$key, $value];
}
break;
case 'language':
if ($value != '') {
$sip_settings[] = ['language', $value];
$ext->addGlobal('SIPLANG',$value);
}
break;
//FREEPBX-9737 AND FREEPBX-6518 In Asterisk 11+ nat=yes is deprecated.
case 'nat':
global $amp_conf;
$astge11 = version_compare($amp_conf['ASTVERSION'],'11.5','ge');
if($astge11){
switch ($value) {
case 'yes':
$value = "force_rport,comedia";
break;
case 'never':
$value = "no";
break;
case 'route':
$value = "force_rport";
break;
}
}
$sip_settings[] = [$key, $value];
break;
case 't38pt_udptl':
if ($value != 'no') {
if($value == 'yes') {
$sip_settings[] = ['t38pt_udptl', 'yes,redundancy,maxdatagram=400'];
} elseif($value == 'fec') {
$sip_settings[] = ['t38pt_udptl', 'yes,fec'];
} elseif($value == 'redundancy') {
$sip_settings[] = ['t38pt_udptl', 'yes,redundancy'];
} elseif($value == 'none') {
$sip_settings[] = ['t38pt_udptl', 'yes,none'];
}
}
break;
case "webrtcstunaddr":
case "webrtcturnaddr":
case "webrtcturnpassword":
case "webrtcturnusername":
case "bindport":
case "bindaddr":
break;
default:
// Ignore localnet settings from chansip sipsettings, they're now in general
if (str_starts_with($key, "localnet_") || str_starts_with($key, "netmask_")) {
break;
}
$sip_settings[] = [$key, $value];
break;
}
}
$udpbindaddrr = (!empty($interim_settings['bindaddr']) ? ($interim_settings['bindaddr'] === '::' ? '[::]' : $interim_settings['bindaddr']) : '0.0.0.0').":".(!empty($interim_settings['bindport']) ? $interim_settings['bindport'] : '5060');
$sip_settings[] = ['udpbindaddr', $udpbindaddrr];
if(FreePBX::Modules()->moduleHasMethod("certman","getCABundle")) {
$cafile = FreePBX::Certman()->getCABundle();
if(!empty($cafile)) {
$sip_settings[] = ['tlscafile', $cafile];
}
}
// Is there a global external IP settings? If there wasn't one specified
// as part of the chan_sip settings, check to see if there's one here.
if (!$foundexternip && $nat_mode == "externip") {
$externip = FreePBX::create()->Sipsettings->getConfig('externip');
if ($externip) {
$sip_settings[] = ["externip", $externip];
}
}
// Now do the localnets
$localnets = FreePBX::create()->Sipsettings->getConfig('localnets');
if(!empty($localnets) && is_array($localnets)) {
foreach ($localnets as $arr) {
$net = trim((string) $arr['net']);
$mask = trim((string) $arr['mask']);
$sip_settings[] = ["localnet", $net."/".$mask];
}
}
global $version;
$core_conf->addSipGeneral('context','from-sip-external');
$core_conf->addSipGeneral('callerid','Unknown');
$core_conf->addSipGeneral('notifyringing','yes');
$core_conf->addSipGeneral('notifyhold','yes');
$core_conf->addSipGeneral('tos_sip','cs3'); // Recommended setting from doc/ip-tos.txt
$core_conf->addSipGeneral('tos_audio','ef'); // Recommended setting from doc/ip-tos.txt
$core_conf->addSipGeneral('tos_video','af41'); // Recommended setting from doc/ip-tos.txt
$core_conf->addSipGeneral('alwaysauthreject','yes');
$core_conf->addSipGeneral('limitonpeers','yes');
unset($interim_settings);
if (is_array($sip_settings)) foreach ($sip_settings as $entry) {
if ($entry[1] != '') {
$core_conf->addSipGeneral($entry[0],$entry[1]);
}
}
}
break;
}
return true;
}
function sipsettings_get($raw=false) {
return FreePBX::Sipsettings()->getChanSipSettings($raw);
}
// Add a sipsettings
function sipsettings_edit($sip_settings) {
global $db;
global $amp_conf;
global $prev_owner;
$save_settings = [];
$save_to_admin = []; // Used only by ALLOW_SIP_ANON for now
$chansip_val = FreePBX::create()->Sipsettings()->getChanSipSettings();
$dbbindport = $chansip_val['tlsbindport'] ?? '';
$req_owner = $_REQUEST['tlsportowner'] ?? '';
$vd = new sipsettings_validate();
// TODO: this is where I will build validation before saving
//
$integer_msg = _("%s must be a non-negative integer");
foreach ($sip_settings as $key => $val) {
switch ($key) {
case 'bindaddr':
$msg = _("Bind Address (bindaddr) must be an IP address.");
$ipv6_ok = version_compare($amp_conf['ASTVERSION'],'1.8','ge');
$save_settings[] = [$key, $db->escapeSimple($vd->is_ip($val,$key,$msg,$ipv6_ok)), '2', SIP_NORMAL];
break;
case 'bindport':
$msg = _("Bind Port (bindport) must be between 1024 and 65535");
$save_settings[] = [$key, $db->escapeSimple($vd->is_ip_port($val, $key, $msg)), '1', SIP_NORMAL];
break;
case 'rtpholdtimeout':
// validation: must be > $sip_settings['rtptimeout'] (and of course a proper number)
//$vd->log_error();
if ($val < $sip_settings['rtptimeout']) {
$msg = _("rtpholdtimeout must be higher than rtptimeout");
$vd->log_error($val, $key, $msg);
}
$msg = sprintf($integer_msg,$key);
$save_settings[] = [$key, $db->escapeSimple($vd->is_int($val, $key, $msg)), '10', SIP_NORMAL];
break;
case 'rtptimeout':
case 'rtpkeepalive':
case 'checkmwi':
case 'registertimeout':
case 'minexpiry':
case 'maxexpiry':
case 'defaultexpiry':
$msg = sprintf($integer_msg,$key);
$save_settings[] = [$key, $db->escapeSimple($vd->is_int($val,$key,$msg)), '10', SIP_NORMAL];
break;
case 'maxcallbitrate':
case 'registerattempts':
$msg = sprintf($integer_msg,$key);
$save_settings[] = [$key, $db->escapeSimple($vd->is_int($val,$key,$msg)), '10', SIP_NORMAL];
break;
case 'context':
$msg = sprintf(_("%s must be alphanumeric"),$key);
$save_settings[] = [$key, $db->escapeSimple($vd->is_alphanumeric($val,$key,$msg)), '0', SIP_NORMAL];
break;
case 'externrefresh':
$msg = sprintf($integer_msg,$key);
$save_settings[] = [$key, $db->escapeSimple($vd->is_int($val,$key,$msg)), '41', SIP_NORMAL];
break;
case 'nat':
$save_settings[] = [$key, $val, '39', SIP_NORMAL];
break;
case 'externip_val':
if ($sip_settings['nat_mode'] == 'externip') {
if (trim((string) $val) == "" && !FreePBX::create()->Sipsettings->getConfig('externip')) {
$msg = _("External IP can not be blank when NAT Mode is set to Static and no default IP address provided on the main page");
$vd->log_error($val, $key, $msg);
} else {
$save_settings[] = [$key, $val, '40', SIP_NORMAL];
}
}
break;
case 'externhost_val':
if (trim((string) $val) == '' && $sip_settings['nat_mode'] == 'externhost') {
$msg = _("Dynamic Host can not be blank");
$vd->log_error($val, $key, $msg);
}
$save_settings[] = [$key, $val, '40', SIP_NORMAL];
break;
case 'jbenable':
$save_settings[] = [$key, $val, '4', SIP_NORMAL];
break;
case 'jbforce':
case 'jbimpl':
case 'jblog':
$save_settings[] = [$key, $val, '5', SIP_NORMAL];
break;
case 'jbmaxsize':
case 'jbresyncthreshold':
$msg = sprintf($integer_msg,$key);
$save_settings[] = [$key, $db->escapeSimple($vd->is_int($val,$key,$msg)), '5', SIP_NORMAL];
break;
case 'nat_mode':
case 'g726nonstandard':
case 't38pt_udptl':
case 'videosupport':
case 'canreinvite':
case 'notifyringing':
case 'notifyhold':
case 'allowguest':
case 'srvlookup':
case 'tlsbindaddr':
case 'tlsdontverifyserver':
case 'tlsclientmethod':
case 'tlsenable':
$save_settings[] = [$key, $val, '10', SIP_NORMAL];
break;
case 'ALLOW_SIP_ANON':
$save_to_admin[] = [$key, $val];
break;
default:
if (str_starts_with((string) $key, "localnet_")) {
// ip validate this and store
$seq = substr((string) $key,9);
$msg = _("Localnet setting must be an IP address");
$save_settings[] = [$key, $db->escapeSimple($vd->is_ip($val,$key,$msg)), (42+$seq), SIP_NORMAL];
} else if (str_starts_with((string) $key, "netmask_")) {
// ip validate this and store
$seq = substr((string) $key,8);
$msg = _("Localnet netmask must be formatted properly (e.g. 255.255.255.0 or 24)");
$save_settings[] = [$key, $db->escapeSimple($vd->is_netmask($val,$key,$msg)), $seq, SIP_NORMAL];
} else if (str_starts_with((string) $key, "sip_custom_key_")) {
$seq = substr((string) $key,15);
$save_settings[] = [$db->escapeSimple($val), $db->escapeSimple($sip_settings["sip_custom_val_$seq"]), ($seq), SIP_CUSTOM];
} else if (str_starts_with((string) $key, "sip_custom_val_")) {
// skip it, we will seek it out when we see the sip_custom_key
} else {
if (($key == 'tlsbindport') && ($req_owner == "sip") && (!(isset($prev_owner)) || ($prev_owner == "none"))) {
$save_settings[] = [$key, $dbbindport, '0', SIP_NORMAL];
} else {
$save_settings[] = [$key, $val, '0', SIP_NORMAL];
}
}
}
}
/* if there were any validation errors, we will return them and not proceed with saving */
if (is_countable($vd->errors) ? count($vd->errors) : 0) {
return $vd->errors;
} else {
$fvcodecs = [];
$seq = 1;
if(!empty($_REQUEST['vcodec'])) {
foreach($_REQUEST['vcodec'] as $codec => $v) {
$fvcodecs[$codec] = $seq++;
}
}
if ((isset($_REQUEST['category']) && $_REQUEST['category'] == "general") && $_REQUEST['Submit'] == "Submit"){
FreePBX::Sipsettings()->setCodecs('video',$fvcodecs);
}
// TODO: normally don't like doing delete/insert but otherwise we would have do update for each
// individual setting and then an insert if there was nothing to update. So this is cleaner
// this time around.
//
sql("DELETE FROM `sipsettings` WHERE 1");
$compiled = $db->prepare('INSERT INTO `sipsettings` (`keyword`, `data`, `seq`, `type`) VALUES (?,?,?,?)');
$result = $db->executeMultiple($compiled,$save_settings);
if(DB::IsError($result)) {
die_freepbx($result->getDebugInfo()."<br><br>".'error adding to sipsettings table');
}
if (!empty($save_to_admin)) {
$compiled = $db->prepare("REPLACE INTO `admin` (`variable`, `value`) VALUES (?,?)");
$result = $db->executeMultiple($compiled,$save_to_admin);
if(DB::IsError($result)) {
die_freepbx($result->getDebugInfo()."<br><br>".'error adding to sipsettings table');
}
}
return true;
}
}