forked from Cynjut/SCCP_Manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.inc.php
1160 lines (963 loc) · 32.3 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
/**
* @copyright Dave Burgess, 2015 based on original work by
* @copyright Javier de la Fuente, EEZ CSIC
* @license GPL2
*/
function sccp_reset_phone($name) {
global $astman;
$astman->Command("sccp reset ".$name);
}
function sccp_get_asterisk_version() {
global $astman;
$ast_sig = $astman->Command("core show version");
list ($software, $version) = explode(' ',$ast_sig['data']);
return $version;
}
function sccp_get_moh_classes() {
global $astman;
$ast_out = $astman->Command("moh show classes");
$ast_out = preg_split("/[\n]/",$ast_out['data']);
foreach ($ast_out as $text) {
if (substr($text,0,5) == 'Class') {
$text = str_replace('Class: ','',$text);
$ast_moh[] = $text;
}
}
return $ast_moh;
}
function sccp_list_devices() {
global $db;
$sql = "SELECT b.device, RIGHT(b.device,12) AS mac, b.name AS ext, d.type
FROM buttonconfig b LEFT JOIN sccpdevice d ON b.device = d.name
WHERE b.type='line' ORDER BY b.name";
$results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
if(DB::IsError($results)) {
die_freepbx($results->getMessage()."<br><br>Error selecting from buttonconfig, sccpdevice");
}
return $results;
}
function sccp_list_tftp_devices() {
global $db;
$sql = "SELECT DISTINCT name, type
FROM sccpdevice ORDER BY name";
$results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
if(DB::IsError($results)) {
die_freepbx($results->getMessage()."<br><br>Error selecting from sccpdevice");
}
return $results;
}
function sccp_list_extensions() {
global $db;
$sql = "SELECT sccpline.name, sccpline.label, buttonconfig.device
FROM sccpline, buttonconfig
WHERE sccpline.name=buttonconfig.name
ORDER BY sccpline.name";
$results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
if(DB::IsError($results)) {
die_freepbx($results->getMessage()."<br><br>Error selecting from sccpline");
}
return $results;
}
function sccp_list_devices_wo_extension() {
global $db;
$sql = "SELECT name, type
FROM sccpdevice
WHERE name NOT IN (SELECT device FROM buttonconfig WHERE type='line')
ORDER BY name";
$results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
if(DB::IsError($results)) {
die_freepbx($results->getMessage()."<br><br>Error selecting from sccpdevice");
}
return $results;
}
function sccp_list_extensions_wo_device() {
global $db;
$sql = "SELECT name, label
FROM sccpline
WHERE name NOT IN (select name from buttonconfig where type='line')
ORDER BY name";
$results = $db->getAll($sql, DB_FETCHMODE_ASSOC);
if(DB::IsError($results)) {
die_freepbx($results->getMessage()."<br><br>Error selecting from sccpline");
}
return $results;
}
function sccp_list_keysets() {
global $astman;
$ast_out = $astman->Command("sccp show softkeyssets");
$ast_out = preg_split("/[\n]/",$ast_out['data']);
for ($i = 0; $i < 5; $i++) {
$ast_out[$i] = "";
}
$i = count($ast_out) - 1;
$ast_out[--$i] = "";
foreach ($ast_out as $line) {
if (strlen($line) > 3) {
$line = substr($line,2);
list ($line,$junk) = split(' ',$line);
if (strlen($ast_key[$line]) < 1) {
$ast_key[$line] = $line;;
}
}
}
return $ast_key;
}
function sccp_get_device($device) {
global $db;
$sql = "SELECT RIGHT(name,12) AS mac, type, description
FROM sccpdevice
WHERE name='$device'";
$row = $db->getRow($sql, DB_FETCHMODE_ASSOC);
if(DB::IsError($row)) {
die_freepbx($row->getMessage()."<br><br>Error selecting row from buttonconfig, sccpdevice for device $device");
}
return($row);
}
function sccp_get_device_full($device) {
global $db;
$sql = "SELECT *
FROM sccpdevice
WHERE name='$device'";
$row = $db->getRow($sql, DB_FETCHMODE_ASSOC);
if(DB::IsError($row)) {
die_freepbx($row->getMessage()."<br><br>Error selecting row from sccpdevice");
}
return $row;
}
function sccp_get_extension($extension) {
global $db;
$extension = (int) $extension;
$sql = "SELECT label, description
FROM sccpline
WHERE name=$extension ";
$row = $db->getRow($sql, DB_FETCHMODE_ASSOC);
if(DB::IsError($row)) {
die_freepbx($row->getMessage()."<br><br>Error selecting row from sccpline");
}
return $row;
}
function sccp_get_extension_full($extension) {
global $db;
$sql = "SELECT *
FROM sccpline
WHERE name='$extension'";
$row = $db->getRow($sql, DB_FETCHMODE_ASSOC);
if(DB::IsError($row)) {
die_freepbx($row->getMessage()."<br><br>Error selecting row from sccpline");
}
return $row;
}
function sccp_get_buttons_assoc($device) {
global $db;
$sql = "SELECT instance, type, name as extension
FROM buttonconfig
WHERE device='$device'
ORDER BY instance";
$res = mysql_query($sql);
$num_row = 0;
while ($row[$num_row] = mysql_fetch_assoc($res)) {
$num_row++;
}
return $row;
}
function sccp_get_ext_assoc($device) {
global $db;
$sql = "SELECT name as extension
FROM buttonconfig
WHERE device='$device' ";
$row = $db->getRow($sql, DB_FETCHMODE_ASSOC);
if(DB::IsError($row)) {
die_freepbx($row->getMessage()."<br><br>Error selecting row from buttonconfig, sccpdevice");
}
return $row;
}
function sccp_get_dev_assoc($extension) {
global $db;
$extension = (int) $extension;
$sql = "SELECT device
FROM buttonconfig
WHERE name='$extension' ";
$row = $db->getRow($sql, DB_FETCHMODE_ASSOC);
if(DB::IsError($row)) {
die_freepbx($row->getMessage()."<br><br>Error selecting row from buttonconfig, sccpdevice");
}
return $row;
}
function get_buttons_devtype($type) {
$res = mysql_query("SELECT dns, buttons
FROM sccpdevmodel
WHERE model='$type' ");
while ($row = mysql_fetch_row($res)) {
$modelData['dns'] = $row[0];
$modelData['buttons'] = $row[1];
}
return $modelData;
}
function sccp_add_device($devData, $buttonData) {
global $db;
if ( strpos($devData['name'],"SEP") === false )
$devData['name'] = "SEP".$devData['name'];
foreach ($devData as $Field => $Value) {
$Query .= ($Query ? "," : "")."$Field=".(trim($Value)!="" ? "\"$Value\"" : "null");
}
$sql = "INSERT INTO sccpdevice set $Query";
$result = $db->query($sql);
if(DB::IsError($result)) {
die_freepbx($result->getMessage().$sql);
}
$numButton = 0;
while ( strlen($buttonData['type'.$numButton]) > 0 ) {
if ($buttonData['type'.$numButton] == 'line') {
$sql = "INSERT IGNORE INTO sccpline
(id, name, label, description, mailbox, cid_num, cid_name)
VALUES ('{$buttonData['name'.$numButton]}', '{$buttonData['name'.$numButton]}', 'Extension {$buttonData['name'.$numButton]}', 'Line {$buttonData['name'.$numButton]}', '{$buttonData['name'.$numButton]}', '{$buttonData['name'.$numButton]}', '{$buttonData['name'.$numButton]}' )";
$result = $db->query($sql);
if(DB::IsError($result)) {
die_freepbx($result->getMessage().$sql);
}
if ($buttonData['default_line'] == $buttonData['name'.$numButton]) {
$buttonData['options'.$numButton] = "Default";
} else {
$buttonData['options'.$numButton] = "";
}
}
$sql = "INSERT INTO buttonconfig
(device, instance, type, name, options)
VALUES ('{$devData['name']}', $numButton+1, '{$buttonData['type'.$numButton]}', '{$buttonData['name'.$numButton]}', '{$buttonData['options'.$numButton]}')";
$result = $db->query($sql);
if(DB::IsError($result)) {
die_freepbx($result->getMessage().$sql);
}
$numButton++;
}
sccp_edit_tftp($devData['name']);
}
function sccp_add_extension($extData) {
global $db;
$extData['id'] = $extData['name'];
$extData['cid_num'] = $extData['name'];
$extData['mailbox'] = ( !$extData['mailbox'] ? $extData['name'] : $extData['mailbox']);
foreach ($extData as $Field => $Value) {
if (!empty($Value)) {
$Query .= ($Query ? "," : "")."$Field=".(trim($Value)!="" ? "\"$Value\"" : "null");
}
}
$sql = "REPLACE INTO sccpline set $Query";
$result = $db->query($sql);
if(DB::IsError($result)) {
die_freepbx($result->getMessage().' '.$sql);
}
$Query = '';
$astData['id'] = $extData['name'];
$astData['tech'] = 'custom';
$astData['dial'] = 'SCCP/'.$extData['name'];
$astData['devicetype'] = 'fixed';
$astData['user'] = $extData['name'];
$astData['description'] = $extData['description'];
$astData['emergency_cid'] = '';
foreach ($astData as $Field => $Value) {
if (!empty($Value))
$Query .= ($Query ? "," : "")."$Field=".(trim($Value)!="" ? "\"$Value\"" : "null");
}
$Query = "REPLACE INTO devices SET $Query";
$result = $db->query($Query);
if(DB::IsError($result)) {
die_freepbx($result->getMessage().' '.$Query);
}
}
/*
* We don't delete the sccpline entries since they could be
* shared. They are actually managed in page.sccp_lines.php, so
* there's no reason to delete them unless we mean it.
*/
function sccp_edit_device($devData, $buttonData) {
global $db;
if ( strpos($devData['name'],"SEP") === false )
$devData['name'] = "SEP".$devData['name'];
$sql = "DELETE FROM buttonconfig
WHERE device = '{$devData['name']}' ";
$result = $db->query($sql);
if(DB::IsError($result)) {
die_freepbx($result->getMessage().$sql);
}
$sql = "DELETE FROM sccpdevice
WHERE name = '{$devData['name']}' ";
$result = $db->query($sql);
if(DB::IsError($result)) {
die_freepbx($result->getMessage().$sql);
}
sccp_delete_tftp($devData['name']);
sccp_add_device($devData, $buttonData);
}
function sccp_edit_extension($extData) {
global $db;
$extData['id'] = $extData['name'];
$extData['mailbox'] = ( $extData['mailbox'] ? $extData['mailbox'] : $extData['name'] );
$extData['trnsfvm'] = ( $extData['trnsfvm'] ? $extData['trnsfvm'] : '*'.$extData['name'] );
foreach ($extData as $Field => $Value) {
$Query .= ($Query ? "," : "")."$Field=".(trim($Value)!="" ? "\"$Value\"" : "null");
}
$sql = "UPDATE sccpline set $Query
WHERE name = '{$extData['name']}' ";
$result = $db->query($sql);
if(DB::IsError($result)) {
die_freepbx($result->getMessage().$sql);
}
}
function sccp_delete_device($device) {
global $db;
$sql = "DELETE FROM buttonconfig
WHERE device = '$device'";
$result = $db->query($sql);
if(DB::IsError($result)) {
die_freepbx($result->getMessage().$sql);
}
$sql = "DELETE FROM sccpdevice
WHERE name = '$device'";
$result = $db->query($sql);
if(DB::IsError($result)) {
die_freepbx($result->getMessage().$sql);
}
sccp_delete_tftp($device);
}
function sccp_delete_extension($extension) {
global $db;
$sql = "DELETE FROM buttonconfig
WHERE name = '{$extension}'";
$result = $db->query($sql);
if(DB::IsError($result)) {
die_freepbx($result->getMessage().$sql);
}
$sql = "DELETE FROM sccpline
WHERE name = '{$extension}'";
$result = $db->query($sql);
if(DB::IsError($result)) {
die_freepbx($result->getMessage().$sql);
}
}
function sccp_get_model_data(){
global $db;
$res = mysql_query("SELECT model, dns, buttons, loadimage
FROM sccpdevmodel
WHERE dns > 0
ORDER BY model ");
while ($row = mysql_fetch_row($res)) {
$modelData['model'][] = $row[0];
$modelData['dns'][] = $row[1];
$modelData['buttons'][] = $row[2];
$modelData['loadimage'][] = $row[3];
}
return $modelData;
}
function sccp_get_addon_data(){
global $db;
$res = mysql_query("SELECT model, buttons, loadimage
FROM sccpdevmodel
WHERE dns = 0
ORDER BY model ");
while ($row = mysql_fetch_row($res)) {
$addonData['model'][] = $row[0];
$addonData['buttons'][] = $row[1];
$addonData['loadimage'][] = $row[2];
}
return $addonData;
}
//
// The old way of doing this bugs me, but I can see why they did it.
// The way I'm going to do it now is to use the XMLDefault.cnf.xml file,
// but I'm going to pull some of the elements out and not include some
// pieces in the "per-device" files.
//
function sccp_create_tftp($device){
if (! file_exists("/tftpboot/XMLDefault.cnf.xml")) {
$template = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/admin/modules/sccp_manager/XMLDefault.cnf.xml');
file_put_contents('/tftpboot/XMLDefault.cnf.xml', $template);
}
$confData = sccp_get_confData('client');
$asterisk_ip = $confDate['bindaddr'];
$filename = '/tftpboot/' . $device . '.cnf.xml';
file_put_contents($filename, $template);
chmod ($filename,0666);
return true;
}
function sccp_delete_tftp($device){
$filename = '/tftpboot/' . $device . '.cnf.xml';
$command = 'rm -f '.$filename;
exec($command);
}
function add_free_space($lines){
$cad = "";
$lines = (int) $lines;
for ($i=0; $i <= $lines; $i++) {
$cad .= "<br/>";
}
return $cad;
}
function tras_button_data($b_Data){
$num_bt = count($b_Data);
for ($i=0; $i < $num_bt; $i++) {
$buttonData['instance'.$i] = $b_Data[$i]['instance'];
$buttonData['type'.$i] = $b_Data[$i]['type'];
$buttonData['button'.$i] = $b_Data[$i]['extension'];
}
return $buttonData;
}
function get_properties_in_button($device, $instance) {
global $db;
$sql = "SELECT type, name, options
FROM buttonconfig
WHERE device='$device' AND instance='$instance' ";
$row = $db->getRow($sql, DB_FETCHMODE_ASSOC);
if(DB::IsError($row)) {
die_freepbx($row->getMessage()."<br><br>Error selecting row from buttonconfig, sccpdevice");
}
switch ($row['type']) {
case ('line'):
break;
case ('speeddial'):
case ('function'):
case ('service'):
list ($description, $hint) = explode(",",$row['options']);
$row['description'] = $description;
$row['hint'] = $hint;
break;
case ('empty'):
$row['name'] = "";
$row['options'] = "";
break;
}
return $row;
}
function sccp_edit_config($confData) {
$confDir = $amp_conf["ASTETCDIR"];
if (strlen($confDir) < 1) {
$confDir = "/etc/asterisk";
}
$inputfile = "$confDir/sccp.conf";
if (! file_exists("$confDir/sccp.conf")) {
$sccpfile = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/admin/modules/sccp_manager/sccp.conf');
file_put_contents("$confDir/sccp.conf", $sccpfile);
}
$handle = fopen($inputfile, "r");
if (strpos($confData['allow'],',')) {
$confData['allow'] = str_replace(',',"\nallow=",$confData['allow']);
}
$written = 'No';
if ($handle) {
$file_context = '';
while (($input = fgets($handle)) != false) {
if (strpos($input,"]")) {
$file_context = $input;
}
if (trim($file_context) != '[general]') {
$outfile .= $input;
} else {
if ($written == 'No') {
$outfile .= "[general]\n";
foreach ($confData as $field => $value) {
$outfile .= "$field=$value\n";
}
$outfile .= "\n";
$written = 'Yes';
}
}
//
// 'allow' is a multientry field, so we add commas.
//
}
fclose($handle);
} else {
die_freepbx($results->getMessage()."<br><br>Error retrieving data from $inputfile");
}
file_put_contents("$confDir/sccp.conf", $outfile);
}
function sccp_get_confData($type) {
global $db;
$confDir = $amp_conf["ASTETCDIR"];
if (strlen($confDir) < 1) {
$confDir = "/etc/asterisk";
}
$inputfile = "$confDir/sccp.conf";
if (! file_exists("$confDir/sccp.conf")) {
$sccpfile = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/admin/modules/sccp_manager/sccp.conf');
file_put_contents("$confDir/sccp.conf", $sccpfile);
} else {
$sccpfile = file_get_contents("$inputfile");
}
$handle = fopen($inputfile, "r");
if ($handle) {
while (($input = fgets($handle)) != false) {
if ($loc = strpos($input,';')) {
$input = substr($input,0,($loc-1));
}
$input = trim($input);
$input = preg_replace('/[\s]+=[\s]+/','=',$input);
if (strpos($input,"]")) {
$file_context = $input;
$confData['file_context'] = preg_replace('/[\[\]]/','',$input);
}
//
// If the $type is either 'client' or 'server', we want the information
// from the general context. If it's anything else, we want to pull
// the context from the file and get the info from it.
//
// 'allow' is a multientry field, so we add commas.
//
if ($type == 'server' || $type == 'client') {
$search = '[general]';
} else {
$search = "[".$type."]";
$confData['type'] = $type;
}
if ($file_context == "$search" && strpos($input,"=")) {
list($field,$value) = split("=",$input);
if ($field == 'allow') {
$confData['allow'] .= $value . ',';
} else {
$confData[$field] = $value;
}
}
}
fclose($handle);
} else {
die_freepbx($results->getMessage()."<br><br>Error retrieving data from $inputfile");
}
//
// Special Fields - these are returned if we are searching for [general]
//
if ($search == '[general]') {
$bindaddr = $confData['bindaddr'];
if ($binaddr == '') {
$bindaddr = '0.0.0.0';
}
if ($type == 'client' && $bindaddr == '0.0.0.0') {
$bindaddr = $_SERVER['SERVER_ADDR'];
}
$confData['bindaddr'] = $bindaddr;
if ($confData['permit'] == '') {
$confData['permit'] = 'internal';
}
if (($lastComma = strlen($confData['allow'])) > 1) {
$confData['allow'] = substr($confData['allow'],0,$lastComma-1);
}
if ($confData['disallow'] == '') {
$confData['disallow'] = 'all';
}
if ($confData['allow'] == '') {
$confData['allow'] = 'ulaw';
}
}
return($confData);
}
function sccp_get_keysetdata($name) {
if ($name == 'default') {
$keysetData = sccp_get_confData('softkeyset');
$keysetData['name'] = 'default';
} else {
$keysetData = sccp_get_confData($name);
}
$keysetData['name'] = ($keysetData['name'] ? $keysetData['name'] : $name);
return $keysetData;
}
function sccp_get_tftp($tftpdisplay) {
$inputfile = "/tftpboot/$tftpdisplay.cnf.xml";
$tftpfile = file_get_contents($inputfile);
if (strlen($tftpfile) < 24) {
$inputfile = "/tftpboot/XMLDefault.cnf.xml";
$tftpfile = file_get_contents($inputfile);
$tftpfile = preg_replace('/^\s\s+/imU','',$tftpfile);
$tftpfile = preg_replace('/<default>$/imU','',$tftpfile);
$tftpfile = preg_replace('/<[\/]+default>$/imU','',$tftpfile);
$tftpfile = preg_replace('/.*<loadInformation[0-9].*$/imU','',$tftpfile);
}
$xml = simplexml_load_string($tftpfile);
$json = json_encode($xml);
$confData = json_decode($json,TRUE);
if (is_array($confData['device'])) {
$confData = $confData['device'];
}
$tftpData['deviceProtocol'] = $confData['deviceProtocol'];
$tftpData['port'] = $confData['devicePool']['callManagerGroup']['members']['member']['callManager']["ports"]["ethernetPhonePort"];
$tftpData['bindaddr'] = $confData['devicePool']['callManagerGroup']['members']['member']['callManager']["processNodeName"];
$tftpData['versionStamp'] = $confData['versionStamp'];
$tftpData['loadInformation'] = $confData['loadInformation'];
$tftpData['addonidx'] = $confData["addOnModules"]["addOnModule"]['@attributes']["idx"];
$tftpData['module_loadinfo'] = $confData["addOnModules"]["addOnModule"]["loadInformation"];
$tftpData['locale_name'] = $confData['userLocale']['name'];
$tftpData['locale_code'] = $confData['userLocale']['langCode'];
$tftpData['directoryURL'] = $confData['directoryURL'];
$tftpData['idleTimeout'] = $confData['idleTimeout'];
if (! is_array($confData['idleURL'])) {
$tftpData['idleURL'] = $confData['idleURL'];
}
if (! is_array($confData['proxyServerURL'])) {
$tftpData['proxyServerURL'] = $confData['proxyServerURL'];
}
if (! is_array($confData['servicesURL'])) {
$tftpData['servicesURL'] = $confData['servicesURL'];
}
$tftpData['autoSelectLineEnable'] = $confData['autoSelectLineEnable'];
$tftpData['autoCallSelect'] = $confData['autoCallSelect'];
return($tftpData);
}
function sccp_edit_tftp($tftpdisplay,$tftpData) {
global $db;
try {
unlink("/tftpboot/".$tftpdisplay.".cnf.xml");
} catch (Exception $ex) {
}
try {
copy("/tftpboot/SEPXML.txt", "/tftpboot/".$tftpdisplay.".cnf.xml");
} catch (Exception $ex) {
die_freepbx("Error creating a CONF file!\n\n".var_dump($ex, true));
}
return;
}
function arrayToXML(Array $array, SimpleXMLElement $xml) {
// Attribute string
foreach($array as $key => $value) {
// Nonarray
if (!is_array($value)) {
(is_numeric($key)) ? $xml->addChild("item$key", $value) : $xml->addChild($key, $value);
continue;
}
// Array
$xmlChild = (is_numeric($key)) ? $xml->addChild("item$key") : $xml->addChild($key);
if (is_array($key['@attributes'])) {
foreach ($key['@attributes'] AS $attrid => $attr) {
$xmlChild->addAttribute($attrid, $attr);
}
# unset($key['@attributes'];
}
arrayToXML($value, $xmlChild);
}
return($xml->asXML());
}
function sccp_get_tftp_loadlist($model) {
global $db;
if (strtoupper($model) == 'DEFAULT') {
$where = "WHERE loadimage IS NOT NULL";
} else {
$where = "WHERE model = '$model'";
}
$res = mysql_query("SELECT loadinformationid,loadimage
FROM sccpdevmodel
$where
ORDER BY loadinformationid");
while ($row = mysql_fetch_row($res)) {
if (strtoupper($model) == 'DEFAULT') {
$loadlist[$row[0]] = $row[1];
} else {
$loadlist['loadimage'] = $row[1];
}
}
return $loadlist;
}
function sccp_edit_keyset($keysetData) {
$keysetImplode['name'] = $keysetData['name'];
$keysetImplode['type'] = $keysetData['type'];
$keysetImplode['file_context'] = $keysetData['file_context'];
$keysetImplode['onhook'] = implode(',',$keysetData['onhook']);
$keysetImplode['connected'] = implode(',',$keysetData['connected']);
$keysetImplode['onhold'] = implode(',',$keysetData['onhold']);
$keysetImplode['ringin'] = implode(',',$keysetData['ringin']);
$keysetImplode['offhook'] = implode(',',$keysetData['offhook']);
$keysetImplode['conntrans'] = implode(',',$keysetData['conntrans']);
$keysetImplode['digitsfoll'] = implode(',',$keysetData['digitsfoll']);
$keysetImplode['connconf'] = implode(',',$keysetData['connconf']);
$keysetImplode['ringout'] = implode(',',$keysetData['ringout']);
$keysetImplode['offhookfeat'] = implode(',',$keysetData['offhookfeat']);
$keysetImplode['onhint'] = implode(',',$keysetData['onhint']);
$keysetImplode['onstealable'] = implode(',',$keysetData['onstealable']);
//
// Write config file context section.
//
$file_context = $keysetData['name'];
if ($file_context != 'default') {
$confDir = $amp_conf["ASTETCDIR"];
if (strlen($confDir) < 1) {
$confDir = "/etc/asterisk";
}
$inputfile = "$confDir/sccp.conf";
if (! file_exists("$confDir/sccp.conf")) {
$sccpfile = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/admin/modules/sccp_manager/sccp.conf');
file_put_contents("$confDir/sccp.conf", $sccpfile);
}
$handle = fopen($inputfile, "r");
$sccpfile = '';
$file_context = "[$file_context]";
if ($handle) {
while (($input = fgets($handle)) != false) {
if (trim($input) != $file_context) {
$sccpfile .= $input;
} else {
$sccpfile .= "$file_context ; Managed by sccp_manager\n";
$sccpfile .= "type=softkeyset\n";
//
// We don't include the 'name=' directive in sccp.conf contexts.
// $sccpfile .= "name=".$keysetImplode['name']."\n";
//
foreach ($keysetImplode as $field => $value) {
if ($field != 'type' && $field != 'name' && $field != 'file_context') {
if (strlen($value) > 1) {
$sccpfile .= $field . "=" . $value . "\n";
}
}
}
$sccpfile .= "\n";
$trimmer = true;
while ($trimmer) {
$trimmer = ($input = fgets($handle));
if (substr($input,0,1) == '[') {
$trimmer = false;
$sccpfile .= $input;
}
}
}
}
}
}
file_put_contents("$confDir/sccp.conf", $sccpfile);
return $sccpfile;
}
function sccp_display_keyset($keysetData,$softkey,$option) {
if ($keysetData['name'] == 'default') {
$output = "<font size='+1'>";
if (strpos(' '.$keysetData[$softkey],$option)) {
$output .= '☑';
} else {
$output .= '☐';
}
$output .= "</font> $option<br>";
} else {
$output = "<input type='checkbox' name='keysetData[$softkey][]' value='$option'";
if (strpos(' '.$keysetData[$softkey],$option)) {
$output .= ' checked';
}
$output .= "> $option<br>";
}
return $output;
}
function sccp_list_devmodel() {
global $db;
$sql = "SELECT * FROM sccpdevmodel ORDER BY model;";
$devmodel = $db->getAll($sql, DB_FETCHMODE_ASSOC);
if(DB::IsError($devmodel)) {
die_freepbx($devmodel->getMessage()."<br><br>Error selecting from sccpdevmodel");
}
return $devmodel;
}
function sccp_edit_devmodel($devmodel) {
global $db;
foreach ($devmodel as $field => $value) {
list ($field, $model) = split('_',$field);
if ($field == "del" && $value == 'on') {
$dquery = "DELETE FROM sccpdevmodel WHERE model = '$model';";
$result = $db->query($dquery);
if(DB::IsError($result)) {
die_freepbx($result->getMessage()."Failed devmodel delete $dquery");
}
$deleted[$model] = 'on';
} else {
$modellist[$model] = $model;
$fieldlist[$model][$field] = $value;
}
}
$query = '';
foreach ($modellist AS $model) {
if ((isset($deleted[$model]) && $deleted[$model] == 'on') || $fieldlist[$model]['model'] == "NEW" || $fieldlist[$model]['model'] == "") {
unset($fieldlist[$model]);
} else {
$query = "REPLACE sccpdevmodel SET ";
$sql = '';
foreach ($fieldlist[$model] AS $field => $value) {
$sql .= ($sql ? "," : "")."$field=".(trim($value)!="" ? "\"$value\"" : "''");
}
$query .= $sql . ";\n";
$result = $db->query($query);
if(DB::IsError($result)) {
die_freepbx($result->getMessage().' '.$query);
}
}
}
return;
}
//
// This function creates new OS79xx.txt files in the /tftpboot
// directory. There's only a few that we actually need to do,
// and the access to the database is the slow part, so we
// whack the DB once and search through for the files we know
// we're going build.
//
function sccp_create_osf() {
global $db;
$query = "SELECT * FROM sccpdevmodel WHERE model LIKE '79%' AND loadimage != '' AND dns > 0";
$res = mysql_query($query);
if(DB::IsError($res)) {
die_freepbx($res->getMessage()."Failed OS File create $query");
}
$name = 'not written';
while ($row = mysql_fetch_assoc($res)) {
$model = $row['model'];
$loadimage = $row['loadimage'];
if ($model == '7940' && $name != '79XX') {
$name = '79XX';
$filename = "/tftpboot/OS79XX.TXT";
}
if (strcmp($model,"7940") < 0) {
$filename = "/tftpboot/OS$model.TXT";
}
file_put_contents($filename, $loadimage);
chmod ($filename,0666);
}
return;
}
/**
* Array2XML: A class to convert array in PHP to XML
* It also takes into account attributes names unlike SimpleXML in PHP
* It returns the XML in form of DOMDocument class for further manipulation.