-
Notifications
You must be signed in to change notification settings - Fork 18
/
Voicemail.class.php
2972 lines (2807 loc) · 106 KB
/
Voicemail.class.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
// vim: set ai ts=4 sw=4 ft=php:
namespace FreePBX\modules;
use Symfony\Component\Finder\Finder;
use BMO;
use FreePBX_Helpers;
use Exception;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
class Voicemail extends FreePBX_Helpers implements BMO {
//message to display to client
public $displayMessage = array(
"type" => "warning",
"message" => ""
);
//supported greeting names
public $greetings = array(
'unavail' => 'Unavailable Greeting',
'greet' => 'Name Greeting',
'busy' => 'Busy Greeting',
'temp' => 'Temporary Greeting',
);
//Voicemail folders to search
private $folders = array(
"INBOX",
"Family",
"Friends",
"Old",
"Work",
"Urgent"
);
//limits the messages to process
private $messageLimit = 3000;
private $vmBoxData = array();
private $vmFolders = array();
private $vmPath = null;
private $messageCache = array();
public $Vmx = null;
private $boxes = array();
private $validFiles = array();
private $vmCache = array();
public function __construct($freepbx = null) {
if ($freepbx == null) {
throw new Exception("Not given a FreePBX Object");
}
if(!class_exists('FreePBX\modules\Voicemail\Vmx') && file_exists(__DIR__.'/Vmx.class.php')) {
include(__DIR__.'/Vmx.class.php');
}
if(!is_object($this->Vmx) && class_exists('FreePBX\modules\Voicemail\Vmx')) {
$this->Vmx = new Voicemail\Vmx($freepbx);
} elseif(!class_exists('FreePBX\modules\Voicemail\Vmx')) {
throw new Exception("Unable to load VmX Locator class");
}
$this->FreePBX = $freepbx;
$this->astman = $this->FreePBX->astman;
$this->db = $freepbx->Database;
$this->vmPath = $this->FreePBX->Config->get_conf_setting('ASTSPOOLDIR') . "/voicemail";
$this->messageLimit = $this->FreePBX->Config->get_conf_setting('UCP_MESSAGE_LIMIT');
\modgettext::push_textdomain("voicemail");
foreach($this->folders as $folder) {
$this->vmFolders[$folder] = array(
"folder" => $folder,
"name" => _($folder)
);
}
\modgettext::pop_textdomain();
//Force translation for later pickup
if(false) {
_("INBOX");
_("Family");
_("Friends");
_("Old");
_("Work");
_("Urgent");
_('Unavailable Greeting');
_('Name Greeting');
_('Busy Greeting');
_('Temporary Greeting');
}
}
public function __get($var) {
switch($var) {
case 'dontUseSymlinks':
$engine_info = engine_getinfo();
$version = $engine_info['version'];
$this->dontUseSymlinks = (version_compare($version, "13.25", ">=") && version_compare($version, "14", "<")) || version_compare($version, "16.2", ">=");
return $this->dontUseSymlinks;
break;
}
}
public function doConfigPageInit($page) {
}
public function install() {
if($this->FreePBX->Modules->checkStatus("userman") && is_object($this->Vmx)) {
$users = $this->FreePBX->Userman()->getAllUsers();
foreach($users as $user) {
if($user['default_extension'] != 'none') {
if($this->FreePBX->Modules->checkStatus("ucp") && $this->Vmx->isInitialized($user['default_extension']) && $this->Vmx->isEnabled($user['default_extension'])) {
$this->FreePBX->Ucp->setSettingByID($user['id'],'Voicemail','vmxlocater',true);
}
}
}
}
}
public function uninstall() {
}
public function genConfig() {
}
public function getQuickCreateDisplay() {
return array(
1 => array(
array(
'html' => load_view(__DIR__.'/views/quickCreate.php',array()),
'validate' => 'if($("#vm_on").is(":checked") && !isInteger($("#vmpwd").val())) {warnInvalid($("#vmpwd"),"'._("Voicemail Password must contain only digits").'");return false}'
)
)
);
}
/**
* Quick Create hook
* @param string $tech The device tech
* @param int $extension The extension number
* @param array $data The associated data
*/
public function processQuickCreate($tech, $extension, $data) {
if($data['vm'] == "yes" && trim($data['vmpwd'] !== "")) {
$this->addMailbox($extension, array(
"vm" => "enabled",
"name" => $data['name'],
"vmpwd" => $data['vmpwd'],
"email" => $data['email'],
"passlogin" => "passlogin=no",
"attach" => "attach=no",
"envelope" => "envelope=no",
"vmdelete" => "vmdelete=no",
"saycid" => "saycid=no"
));
$sql = "UPDATE users SET voicemail = 'default' WHERE extension = ?";
$sth = $this->db->prepare($sql);
$sth->execute(array($extension));
$this->astman->database_put("AMPUSER",$extension."/voicemail",'default');
$this->mapMailBox($extension);
}
}
/**
* Change the mailbox context
* @param int $mailbox The mailbox number
* @param string $vmcontext
*/
public function updateMailBoxContext($mailbox, $vmcontext = 'default') {
if (!is_numeric($mailbox)) {
throw new Exception(sprintf(_("Mailbox is not in the proper format [%s]"), $mailbox));
}
// Update FreePBX database
$sql = "UPDATE users SET voicemail = ? WHERE extension = ?";
$sth = $this->db->prepare($sql);
$sth->execute([$vmcontext, $mailbox]);
// Update Asterisk database
$this->astman->database_put("AMPUSER", $mailbox."/voicemail", $vmcontext);
$this->mapMailBox($mailbox);
}
/**
* Setup mailbox alias mapping
* @param int $mailbox The mailbox number
*/
public function mapMailBox($mailbox) {
if(empty($mailbox) || $mailbox == 'none'){
return;
}
if(!is_numeric($mailbox)) {
throw new Exception(sprintf(_("Mailbox is not in the proper format [%s]"),$mailbox));
}
if(isset($_REQUEST['vmcontext'])) {
$vmcontext = !empty($_REQUEST['vmcontext']) ? $_REQUEST['vmcontext'] : 'default';
$user = array(
'voicemail' => $vmcontext
);
} else {
$user = $this->FreePBX->Core->getUser($mailbox);
if(empty($user)) {
return;
}
$vmcontext = isset($user['voicemail']) ? $user['voicemail'] : 'default';
}
if($user['voicemail'] != "novm") {
if($this->dontUseSymlinks) {
$this->updateAliasDeviceMapping($mailbox, "$mailbox@$vmcontext", false);
} else {
// Create voicemail symlink
$spooldir = $this->FreePBX->Config->get('ASTSPOOLDIR');
$src = "$spooldir/voicemail/$vmcontext/$mailbox";
$dest = "$spooldir/voicemail/device/$mailbox";
// Remove anything that was previously there
exec("rm -rf $dest");
// Make sure our source parent directory exists - This may be missing if a restore
// was partially done. Asterisk may or may not create this on demand.
if (!is_dir(dirname($src))) {
mkdir(dirname($src), 0775, true);
}
// Make sure the destination parent exists, too.
if (!is_dir(dirname($dest))) {
mkdir(dirname($dest), 0775, true);
}
// Now do the symlink
@symlink($src, $dest);
}
}
return;
}
/**
* Parse the voicemail.conf file the way we need it to be
* @param bool $cached If true then attempt to get cached values
* @return array The array of the voicemail.conf file
*/
public function getVoicemail($cached = true) {
if($cached && !empty($this->vmCache)) {
return $this->vmCache;
}
$vm = $this->FreePBX->LoadConfig->getConfig("voicemail.conf");
//Parse mailbox data into something useful
$vm = is_array($vm) ? $vm : array();
foreach($vm as $name => &$context) {
if($name == "general" || $name == "zonemessages" || $name == "pbxaliases" || $name == 'device') {
if($name == "pbxaliases") {
//FREEI-752 voicemail.conf contains [=" which are one character length which never gets removed on reload
//Or need to remove it manuelly
foreach($context as $key => $row) {
if(strlen($row) < 2) {
unset($context[$key]);
}
}
}
continue;
}
foreach($context as $mailbox => &$data) {
$options = explode(",",$data);
$fopts = array();
if(!empty($options[4])) {
foreach(explode("|",$options[4]) as $odata) {
$t = explode("=",$odata);
$fopts[$t[0]] = $t[1];
}
}
$data = array(
'mailbox' => $mailbox,
'pwd' => isset($options[0]) ? $options[0] : '',
'name' => isset($options[1]) ? $options[1] : '',
'email' => isset($options[2]) ? $options[2] : '',
'pager' => isset($options[3]) ? $options[3] : '',
'options' => isset($fopts) ? $fopts : ''
);
}
}
$this->vmCache = $vm;
return $this->vmCache;
}
/**
* Get the mailbox options from voicemail.conf parsing
* @param int $mailbox The mailbox number
* @param bool $cached Attempt to get cached voicemail file
*/
public function getMailbox($mailbox, $cached = true) {
$uservm = $this->getVoicemail($cached);
$vmcontexts = array_keys($uservm);
foreach ($vmcontexts as $vmcontext) {
if($vmcontext == "general" || $vmcontext == "zonemessages" || $vmcontext == "pbxaliases" || $vmcontext == 'device') {
continue;
}
if(isset($uservm[$vmcontext][$mailbox])){
$vmbox['vmcontext'] = $vmcontext;
$vmbox['pwd'] = $uservm[$vmcontext][$mailbox]['pwd'];
$vmbox['name'] = $uservm[$vmcontext][$mailbox]['name'];
$vmbox['email'] = str_replace('|',',',$uservm[$vmcontext][$mailbox]['email']);
$vmbox['pager'] = $uservm[$vmcontext][$mailbox]['pager'];
$vmbox['options'] = $uservm[$vmcontext][$mailbox]['options'];
return $vmbox;
}
}
return null;
}
/**
* Alias for updateMailbox
* @method saveMailbox
*/
public function saveMailbox($mailbox, $settings, $cached = true) {
return $this->updateMailbox($mailbox, $settings, $cached);
}
/**
* Update Mailbox using data from getMailbox
* @method updateMailbox
* @param string $mailbox The mailbox number
* @param array $settings Array of mailbox settings
* @param boolean $cached Attempt to get cached voicemail file
* @return boolean Return true if success
*/
public function updateMailbox($mailbox, $settings, $cached = true) {
if(trim($mailbox) == "") {
throw new Exception("Mailbox is not defined!");
}
if(empty($settings)) {
throw new Exception("Nothing to save! Did you mean to delMailbox?");
}
$voicemail = $this->getVoicemail($cached);
if(empty($settings['vmcontext'])) {
throw new Exception("There is no context!");
}
$vmcontext = $settings['vmcontext'];
if($vmcontext == "general" || $vmcontext == "zonemessages" || $vmcontext == "pbxaliases" || $vmcontext == 'device') {
throw new Exception("Invalid context!");
}
unset($settings['vmcontext']);
if(empty($voicemail[$vmcontext])) {
throw new Exception("Context does not exist");
}
if(empty($voicemail[$vmcontext][$mailbox])) {
throw new Exception("Mailbox did not previously exist. Did you mean to addMailbox?");
}
$voicemail[$vmcontext][$mailbox] = $settings;
$this->saveVoicemail($voicemail);
return true;
}
/**
* Remove the mailbox from the system (hard drive)
* @param bool $cached If true then attempt to get cached values
* @param int $mailbox The mailbox number
*/
public function removeMailbox($mailbox, $cached = true) {
$uservm = $this->getVoicemail($cached);
$vmcontexts = array_keys($uservm);
$return = true;
foreach ($vmcontexts as $vmcontext) {
if(isset($uservm[$vmcontext][$mailbox])){
$vm_dir = $this->FreePBX->Config->get('ASTSPOOLDIR')."/voicemail/$vmcontext/$mailbox";
exec("rm -rf $vm_dir",$output,$ret);
if ($ret) {
$return = false;
$text = sprintf(_("Failed to delete vmbox: %s@%s"),$mailbox, $vmcontext);
$etext = sprintf(_("failed with retcode %s while removing %s:"),$ret, $vm_dir)."<br>";
$etext .= implode("<br>",$output);
$nt =& \notifications::create($db);
$nt->add_error('voicemail', 'MBOXREMOVE', $text, $etext, '', true, true);
}
}
}
return $return;
}
/* UCP template to get the user assigned vm extension details
* @defaultexten is the default_extensionof the userman userid
* @userid is userman user id
* @widget is an array we need to replace few item based on the userid
*/
public function getWidgetListByModule($defaultexten, $userid,$widget) {
// if the widget_type_id is not defaultextension and widget_type_id is not in extensions
// then return only the defaultexten details
$widgets = array();
$widget_type_id = $widget['widget_type_id'];// this will be an extension number
$extensions = $this->FreePBX->UCP->getCombinedSettingByID($userid,'Voicemail','assigned');
if(in_array($widget_type_id,$extensions)){
// nothing to do return the same widget
return $widget;
}else {// lets check VM enabled for this extension
$o = $this->getVoicemailBoxByExtension($defaultexten);
if (!empty($o)){
$data = $this->FreePBX->Core->getDevice($defaultexten);
if(empty($data) || empty($data['description'])) {
$data = $this->FreePBX->Core->getUser($defaultexten);
$name = $data['name'];
} else {
$name = $data['description'];
}
$widget['widget_type_id'] = $defaultexten;
$widget['name'] = $name;
return $widget;
}else{
return false;
}
}
}
/**
* Delete mailbox from voicemail.conf
* @param bool $cached If true then attempt to get cached values
* @param int $mailbox The mailbox number
*/
public function delMailbox($mailbox, $cached = true) {
$uservm = $this->getVoicemail($cached);
$vmcontexts = array_keys($uservm);
foreach ($vmcontexts as $vmcontext) {
if(isset($uservm[$vmcontext][$mailbox])){
$this->delConfig($mailbox, 'vmmapping');
unset($uservm[$vmcontext][$mailbox]);
unset($uservm["pbxaliases"]);
$this->saveVoicemail($uservm);
return true;
}
}
return false;
}
/**
* Update Alias Mapping
*
* @param int $mailbox The mailbox number
* @return void
*/
public function updateAliasDeviceMapping($device, $mailbox, $save=true) {
if(!empty($mailbox)) {
$this->setConfig($device, array("$device@device", $mailbox), 'vmmapping');
} else {
$this->delConfig($device, 'vmmapping');
}
if($save) {
$uservm = $this->getVoicemail(true);
$this->saveVoicemail($uservm);
}
}
/**
* Save Voicemail.conf file
* @param array $vmconf Array of settings which are returned from LoadConfig
*/
public function saveVoicemail($vmconf, $fromReload = false) {
// just in case someone tries to be sneaky and not call getVoicemail() first..
if ($vmconf == null) {
throw new Exception(_("Null value was sent to saveVoicemail() can not continue"));
}
if($this->dontUseSymlinks) {
$vmconf['general']['aliasescontext'] = 'pbxaliases';
$vmm = $this->getAll('vmmapping');
foreach($vmm as $mailbox => $data) {
if (!is_array($data)) {
$data = @json_decode($data,true);
}
$vmconf['pbxaliases'][$data[0]] = $data[1];
}
} else {
if(isset($vmconf['general']['aliasescontext'])) {
unset($vmconf['general']['aliasescontext']);
}
if(isset($vmconf['pbxaliases'])) {
unset($vmconf['pbxaliases']);
}
}
foreach($vmconf as $cxtname => &$context) {
if($cxtname == "general" || $cxtname == "zonemessages" || $cxtname == 'pbxaliases' || $cxtname == 'device') {
$cdata = array();
foreach($context as $key => $value) {
$cdata[$key] = str_replace(array("\n","\t","\r"),array("\\n","\\t","\\r"),$value);
}
$context = $cdata;
continue;
}
$cdata = array();
foreach($context as $mailbox => $data) {
$opts = array();
//lets remove the ',' from name
//FREEPBX-11103 Voicemail issue for extension if display name contains a comma
$data['name']=str_replace(",","",$data['name']);
if(!empty($data['options'])) {
foreach($data['options'] as $key => $value) {
$opts[] = $key."=".$value;
}
}
//FREEPBX-14851 Voicemail issue for extension if display name contains a comma
$data['name']=str_replace(",","",$data['name']);
$data['email'] = str_replace(",","|",$data['email']);
$data['pager'] = str_replace(",","|",$data['pager']);
$data['options'] = implode("|",$opts);
$cdata[] = $mailbox ."=" .
$data['pwd'] . "," .
$data['name'] . "," .
$data['email'] . "," .
$data['pager'] . "," .
$data['options'];
}
$context = $cdata;
}
if(!$fromReload) {
$extEmailBody = $this->getConfig('email_body');
if($extEmailBody) {
$this->setConfig('email_body', $vmconf['general']['emailbody'] ?? "");
}
}
$this->FreePBX->WriteConfig->writeConfig("voicemail.conf", $vmconf, false);
$this->vmCache = array();
}
/**
* Add a Mailbox and all of it's settings
* @param int $mailbox The mailbox number
* @param array $settings The settings for said mailbox
* @param bool $cached If true then attempt to get cached values
*/
public function addMailbox($mailbox, $settings, $cached = true) {
global $astman;
if(trim($mailbox) == "") {
throw new Exception(_("Mailbox can not be empty"));
}
$vmconf = $this->getVoicemail($cached);
$settings['vmcontext'] = !empty($settings['vmcontext']) ? $settings['vmcontext'] : 'default';
$settings['pwd'] = isset($settings['pwd']) ? $settings['pwd'] : '';
$settings['name'] = isset($settings['name']) ? $settings['name'] : '';
$settings['email'] = isset($settings['email']) ? $settings['email'] : '';
$settings['pager'] = isset($settings['pager']) ? $settings['pager'] : '';
if (isset($settings['vm']) && $settings['vm'] != 'disabled') {
$vmoptions = array();
// need to check if there are any options entered in the text field
if (!empty($settings['options'])) {
$options = explode("|",$settings['options']);
foreach($options as $option) {
$vmoption = explode("=", $option);
$vmoptions[$vmoption[0]] = $vmoption[1];
}
}
if (isset($settings['imapuser']) && trim($settings['imapuser']) != '' && isset($settings['imapuser']) && trim($settings['imapuser']) != '') {
$vmoptions['imapuser'] = $settings['imapuser'];
$vmoptions['imappassword'] = $settings['imappassword'];
}
if(isset($settings['passlogin']) && $settings['passlogin']!='no') {
$vmoption = explode("=",$settings['passlogin']);
$settings['passlogin'] = $vmoption[1];
}
if(isset($settings['novmstar'])&& $settings['novmstar']!='no') {
$vmoption = explode("=",$settings['novmstar']);
$settings['novmstar'] = $vmoption[1];
}
if(isset($settings['attach']) && $settings['attach']!='no') {
$vmoption = explode("=",$settings['attach']);
$vmoptions['attach'] = $vmoption[1];
}
if(isset($settings['saycid']) && $settings['saycid']!='no') {
$vmoption = explode("=",$settings['saycid']);
$vmoptions['saycid'] = $vmoption[1];
}
if(isset($settings['envelope']) && $settings['envelope']!='no') {
$vmoption = explode("=",$settings['envelope']);
$vmoptions['envelope'] = $vmoption[1];
}
if(isset($settings['vmdelete']) && $settings['vmdelete']!='no') {
$vmoption = explode("=",$settings['vmdelete']);
$vmoptions['delete'] = $vmoption[1];
}
$vmconf[$settings['vmcontext']][$mailbox] = array(
'mailbox' => $mailbox,
'pwd' => $settings['vmpwd'],
'name' => $settings['name'],
'email' => str_replace(',','|',$settings['email']),
'pager' => $settings['pager'],
'options' => $vmoptions
);
$this->setConfig($mailbox, array("$mailbox@device", $mailbox."@".$settings['vmcontext']), 'vmmapping');
}
$this->saveVoicemail($vmconf);
if(isset($settings['passlogin']) && $settings['passlogin'] == 'no') {
//The value doesnt matter, could be yes no f bark
$this->astman->database_put("AMPUSER", $mailbox."/novmpw", 'yes');
} else {
$this->astman->database_del("AMPUSER", $mailbox."/novmpw");
}
if(isset($settings['novmstar']) && $settings['novmstar'] == 'yes') {
//The value doesnt matter, could be yes no f bark
$this->astman->database_put("AMPUSER", $mailbox."/novmstar", 'yes');
} else {
$this->astman->database_del("AMPUSER", $mailbox."/novmstar");
}
// Operator extension can be set even without VmX enabled so that it can be
// used as an alternate way to provide an operator extension for a user
// without VmX enabled.
//
if (isset($settings['vmx_option_0_system_default']) && $settings['vmx_option_0_system_default'] != '') {
$this->Vmx->setMenuOpt($mailbox,"",0,'unavail');
$this->Vmx->setMenuOpt($mailbox,"",0,'busy');
$this->Vmx->setMenuOpt($mailbox,"",0,'temp');
} else {
if (!isset($settings['vmx_option_0_number'])) {
$settings['vmx_option_0_number'] = '';
}
$settings['vmx_option_0_number'] = preg_replace("/[^0-9\*#]/" ,"", $settings['vmx_option_0_number']);
$this->Vmx->setMenuOpt($mailbox,$settings['vmx_option_0_number'],0,'unavail');
$this->Vmx->setMenuOpt($mailbox,$settings['vmx_option_0_number'],0,'busy');
$this->Vmx->setMenuOpt($mailbox,$settings['vmx_option_0_number'],0,'temp');
}
if (isset($settings['vmx_state']) && $settings['vmx_state'] != 'disabled') {
if (isset($settings['vmx_unavail_enabled']) && $settings['vmx_unavail_enabled'] != '') {
$this->Vmx->setState($mailbox,'unavail','enabled');
} else {
$this->Vmx->setState($mailbox,'unavail','disabled');
}
if (isset($settings['vmx_busy_enabled']) && $settings['vmx_busy_enabled'] != '') {
$this->Vmx->setState($mailbox,'busy','enabled');
} else {
$this->Vmx->setState($mailbox,'busy','disabled');
}
if (isset($settings['vmx_temp_enabled']) && $settings['vmx_temp_enabled'] != '') {
$this->Vmx->setState($mailbox,'temp','enabled');
} else {
$this->Vmx->setState($mailbox,'temp','disabled');
}
if (isset($settings['vmx_play_instructions']) && $settings['vmx_play_instructions'] == 'yes') {
$this->Vmx->setVmPlay($mailbox,'unavail',true);
$this->Vmx->setVmPlay($mailbox,'busy',true);
$this->Vmx->setVmPlay($mailbox,'temp',true);
} else {
$this->Vmx->setVmPlay($mailbox,'unavail',false);
$this->Vmx->setVmPlay($mailbox,'busy',false);
$this->Vmx->setVmPlay($mailbox,'temp',false);
}
if (isset($settings['vmx_option_1_system_default']) && $settings['vmx_option_1_system_default'] != '') {
$this->Vmx->setFollowMe($mailbox,1,'unavail');
$this->Vmx->setFollowMe($mailbox,1,'busy');
$this->Vmx->setFollowMe($mailbox,1,'temp');
} else {
if (!isset($settings['vmx_option_1_number'])) {
$settings['vmx_option_1_number'] = '';
}
$settings['vmx_option_1_number'] = preg_replace("/[^0-9\*#]/" ,"", $settings['vmx_option_1_number']);
$this->Vmx->setMenuOpt($mailbox,$settings['vmx_option_1_number'],1,'unavail');
$this->Vmx->setMenuOpt($mailbox,$settings['vmx_option_1_number'],1,'busy');
$this->Vmx->setMenuOpt($mailbox,$settings['vmx_option_1_number'],1,'temp');
}
if (isset($settings['vmx_option_2_number'])) {
$settings['vmx_option_2_number'] = preg_replace("/[^0-9\*#]/" ,"", $settings['vmx_option_2_number']);
$this->Vmx->setMenuOpt($mailbox,$settings['vmx_option_2_number'],2,'unavail');
$this->Vmx->setMenuOpt($mailbox,$settings['vmx_option_2_number'],2,'busy');
$this->Vmx->setMenuOpt($mailbox,$settings['vmx_option_2_number'],2,'temp');
}
} else {
if ($this->Vmx->isInitialized($mailbox)) {
$this->Vmx->disable($mailbox);
}
}
return true;
}
/**
* Get a list of users
*/
public function getUsersList() {
return $this->FreePBX->Core->listUsers(true);
}
public function ucpDelGroup($id,$display,$data) {
}
public function ucpAddGroup($id, $display, $data) {
$this->ucpUpdateGroup($id,$display,$data);
}
public function ucpUpdateGroup($id,$display,$data) {
if($display == 'userman' && isset($_POST['type']) && $_POST['type'] == 'group') {
if(!empty($_POST['ucp_voicemail'])) {
$this->FreePBX->Ucp->setSettingByGID($id,'Voicemail','assigned',$_POST['ucp_voicemail']);
} else {
$this->FreePBX->Ucp->setSettingByGID($id,'Voicemail','assigned',array('self'));
}
if(!empty($_POST['voicemail_enable']) && $_POST['voicemail_enable'] == "yes") {
$this->FreePBX->Ucp->setSettingByGID($id,'Voicemail','enable',true);
} else {
$this->FreePBX->Ucp->setSettingByGID($id,'Voicemail','enable',false);
}
if(!empty($_POST['voicemail_playback']) && $_POST['voicemail_playback'] == "yes") {
$this->FreePBX->Ucp->setSettingByGID($id,'Voicemail','playback',true);
} else {
$this->FreePBX->Ucp->setSettingByGID($id,'Voicemail','playback',false);
}
if(!empty($_POST['voicemail_download']) && $_POST['voicemail_download'] == "yes") {
$this->FreePBX->Ucp->setSettingByGID($id,'Voicemail','download',true);
} else {
$this->FreePBX->Ucp->setSettingByGID($id,'Voicemail','download',false);
}
if(!empty($_POST['voicemail_settings']) && $_POST['voicemail_settings'] == "yes") {
$this->FreePBX->Ucp->setSettingByGID($id,'Voicemail','settings',true);
} else {
$this->FreePBX->Ucp->setSettingByGID($id,'Voicemail','settings',false);
}
if(!empty($_POST['voicemail_greetings']) && $_POST['voicemail_greetings'] == "yes") {
$this->FreePBX->Ucp->setSettingByGID($id,'Voicemail','greetings',true);
} else {
$this->FreePBX->Ucp->setSettingByGID($id,'Voicemail','greetings',false);
}
if(!empty($_POST['vmxlocater']) && $_POST['vmxlocater'] == "yes") {
$this->FreePBX->Ucp->setSettingByGID($id,'Voicemail','vmxlocater',true);
} else {
$this->FreePBX->Ucp->setSettingByGID($id,'Voicemail','vmxlocater',false);
}
}
}
/**
* Delete user function, it's run twice because of scemantics with
* old freepbx but it's harmless
* @param string $extension The extension number
* @param bool $editmode If we are in edit mode or not
*/
public function delUser($extension, $editmode=false) {
if(!$editmode) {
if(!function_exists('voicemail_mailbox_remove')) {
$this->FreePBX->Modules->loadFunctionsInc('voicemail');
}
voicemail_mailbox_remove($extension);
voicemail_mailbox_del($extension);
}
}
/**
* Hook functionality from userman when a user is deleted
* @param {int} $id The userman user id
* @param {string} $display The display page name where this was executed
* @param {array} $data Array of data to be able to use
*/
public function ucpDelUser($id, $display, $ucpStatus, $data) {
}
/**
* Hook functionality from userman when a user is added
* @param {int} $id The userman user id
* @param {string} $display The display page name where this was executed
* @param {array} $data Array of data to be able to use
*/
public function ucpAddUser($id, $display, $ucpStatus, $data) {
$this->ucpUpdateUser($id, $display, $ucpStatus, $data);
}
/**
* Hook functionality from userman when a user is updated
* @param {int} $id The userman user id
* @param {string} $display The display page name where this was executed
* @param {array} $data Array of data to be able to use
*/
public function ucpUpdateUser($id, $display, $ucpStatus, $data) {
if($display == 'userman' && isset($_POST['type']) && $_POST['type'] == 'user') {
if(!empty($_POST['ucp_voicemail'])) {
$this->FreePBX->Ucp->setSettingByID($id,'Voicemail','assigned',$_POST['ucp_voicemail']);
} else {
$this->FreePBX->Ucp->setSettingByID($id,'Voicemail','assigned',null);
}
if(!empty($_POST['voicemail_enable']) && $_POST['voicemail_enable'] == "yes") {
$this->FreePBX->Ucp->setSettingByID($id,'Voicemail','enable',true);
} elseif(!empty($_POST['voicemail_enable']) && $_POST['voicemail_enable'] == "no") {
$this->FreePBX->Ucp->setSettingByID($id,'Voicemail','enable',false);
} elseif(!empty($_POST['voicemail_enable']) && $_POST['voicemail_enable'] == "inherit") {
$this->FreePBX->Ucp->setSettingByID($id,'Voicemail','enable',null);
}
if(!empty($_POST['voicemail_playback']) && $_POST['voicemail_playback'] == "yes") {
$this->FreePBX->Ucp->setSettingByID($id,'Voicemail','playback',true);
} elseif(!empty($_POST['voicemail_playback']) && $_POST['voicemail_playback'] == "no") {
$this->FreePBX->Ucp->setSettingByID($id,'Voicemail','playback',false);
} elseif(!empty($_POST['voicemail_playback']) && $_POST['voicemail_playback'] == "inherit") {
$this->FreePBX->Ucp->setSettingByID($id,'Voicemail','playback',null);
}
if(!empty($_POST['voicemail_download']) && $_POST['voicemail_download'] == "yes") {
$this->FreePBX->Ucp->setSettingByID($id,'Voicemail','download',true);
} elseif(!empty($_POST['voicemail_download']) && $_POST['voicemail_download'] == "no") {
$this->FreePBX->Ucp->setSettingByID($id,'Voicemail','download',false);
} elseif(!empty($_POST['voicemail_download']) && $_POST['voicemail_download'] == "inherit") {
$this->FreePBX->Ucp->setSettingByID($id,'Voicemail','download',null);
}
if(!empty($_POST['voicemail_settings']) && $_POST['voicemail_settings'] == "yes") {
$this->FreePBX->Ucp->setSettingByID($id,'Voicemail','settings',true);
} elseif(!empty($_POST['voicemail_settings']) && $_POST['voicemail_settings'] == "no") {
$this->FreePBX->Ucp->setSettingByID($id,'Voicemail','settings',false);
} elseif(!empty($_POST['voicemail_settings']) && $_POST['voicemail_settings'] == "inherit") {
$this->FreePBX->Ucp->setSettingByID($id,'Voicemail','settings',null);
}
if(!empty($_POST['voicemail_greetings']) && $_POST['voicemail_greetings'] == "yes") {
$this->FreePBX->Ucp->setSettingByID($id,'Voicemail','greetings',true);
} elseif(!empty($_POST['voicemail_greetings']) && $_POST['voicemail_greetings'] == "no") {
$this->FreePBX->Ucp->setSettingByID($id,'Voicemail','greetings',false);
} elseif(!empty($_POST['voicemail_greetings']) && $_POST['voicemail_greetings'] == "inherit") {
$this->FreePBX->Ucp->setSettingByID($id,'Voicemail','greetings',null);
}
if(!empty($_POST['vmxlocater']) && $_POST['vmxlocater'] == "yes") {
$this->FreePBX->Ucp->setSettingByID($id,'Voicemail','vmxlocater',true);
} elseif(!empty($_POST['vmxlocater']) && $_POST['vmxlocater'] == "no") {
$this->FreePBX->Ucp->setSettingByID($id,'Voicemail','vmxlocater',false);
} elseif(!empty($_POST['vmxlocater']) && $_POST['vmxlocater'] == "inherit") {
$this->FreePBX->Ucp->setSettingByID($id,'Voicemail','vmxlocater',null);
}
}
}
public function ucpConfigPage($mode, $user, $action) {
if(empty($user)) {
$enable = ($mode == 'group') ? true : null;
$playback = ($mode == 'group') ? true : null;
$download = ($mode == 'group') ? true : null;
$settings = ($mode == 'group') ? true : null;
$greetings = ($mode == 'group') ? true : null;
$vmxlocater = ($mode == 'group') ? true : null;
} else {
if($mode == "group") {
$vmassigned = $this->FreePBX->Ucp->getSettingByGID($user['id'],'Voicemail','assigned');
$enable = $this->FreePBX->Ucp->getSettingByGID($user['id'],'Voicemail','enable');
$enable = !($enable) ? false : true;
$vmassigned = !empty($vmassigned) ? $vmassigned : array('self');
$playback = $this->FreePBX->Ucp->getSettingByGID($user['id'],'Voicemail','playback');
$playback = !($playback) ? false : true;
$download = $this->FreePBX->Ucp->getSettingByGID($user['id'],'Voicemail','download');
$download = !($download) ? false : true;
$settings = $this->FreePBX->Ucp->getSettingByGID($user['id'],'Voicemail','settings');
$settings = !($settings) ? false : true;
$greetings = $this->FreePBX->Ucp->getSettingByGID($user['id'],'Voicemail','greetings');
$greetings = !($greetings) ? false : true;
$vmxlocater = $this->FreePBX->Ucp->getSettingByGID($user['id'],'Voicemail','vmxlocater');
$vmxlocater = !($vmxlocater) ? false : true;
} else {
$vmassigned = $this->FreePBX->Ucp->getSettingByID($user['id'],'Voicemail','assigned');
$enable = $this->FreePBX->Ucp->getSettingByID($user['id'],'Voicemail','enable');
$playback = $this->FreePBX->Ucp->getSettingByID($user['id'],'Voicemail','playback');
$download = $this->FreePBX->Ucp->getSettingByID($user['id'],'Voicemail','download');
$settings = $this->FreePBX->Ucp->getSettingByID($user['id'],'Voicemail','settings');
$greetings = $this->FreePBX->Ucp->getSettingByID($user['id'],'Voicemail','greetings');
$vmxlocater = $this->FreePBX->Ucp->getSettingByID($user['id'],'Voicemail','vmxlocater');
}
}
$vmassigned = !empty($vmassigned) ? $vmassigned : array();
$ausers = array();
if($action == "showgroup" || $action == "addgroup") {
$ausers['self'] = _("User Primary Extension");
}
if($action == "addgroup") {
$vmassigned = array('self');
}
foreach(core_users_list() as $list) {
$cul[$list[0]] = array(
"name" => $list[1],
"vmcontext" => $list[2]
);
$ausers[$list[0]] = $list[1] . " <".$list[0].">";
}
$html[0] = array(
"title" => _("Voicemail"),
"rawname" => "voicemail",
"content" => load_view(dirname(__FILE__)."/views/ucp_config.php",array("vmxlocater" => $vmxlocater, "playback" => $playback, "download" => $download, "settings" => $settings, "greetings" => $greetings, "mode" => $mode, "enable" => $enable, "ausers" => $ausers, "vmassigned" => $vmassigned))
);
return $html;
}
/**
* Get all known folders
*/
public function getFolders() {
return $this->vmFolders;
}
/**
* Delete vm greeting from system
* @param int $ext The voicemail extension
* @param string $type the type to remove
*/
public function deleteVMGreeting($ext,$type) {
$o = $this->getVoicemailBoxByExtension($ext);
$context = $o['vmcontext'];
$vmfolder = $this->vmPath . '/'.$context.'/'.$ext;
$type = basename($type);
$file = $this->checkFileType($vmfolder, $type);
if(isset($this->greetings[$type]) && !empty($file)) {
foreach(glob($vmfolder."/".$type."*.*") as $filename) {
if(!file_exists($filename)) {
continue;
}
if(!unlink($filename)) {
return false;
}
}
return true;
}
return false;
}
/**
* Copy a VM Greeting
* @param int $ext The voicemail extension
* @param string $source Voicemail source type
* @param string $target voicemail destination type
*/
public function copyVMGreeting($ext,$source,$target) {
$o = $this->getVoicemailBoxByExtension($ext);
$context = $o['vmcontext'];
$vmfolder = $this->vmPath . '/'.$context.'/'.basename($ext);
if(!file_exists($vmfolder)) {
mkdir($vmfolder,0777,true);
}
if(isset($this->greetings[$source]) && isset($this->greetings[$target])) {
$tfile = $this->checkFileType($vmfolder, $target);
if(!empty($tfile)) {
$this->deleteVMGreeting($ext, $target);
}
$file = $this->checkFileType($vmfolder, $source);
$extension = $this->getFileExtension($vmfolder, $source);
copy($file, $vmfolder."/".basename($target).".".$extension);
}
return true;
}
/**
* Save Voicemail Greeting
* @param int $ext The voicemail extension
* @param string $type The voicemail type
* @param string $format The file format
* @param string $file The full path to the file
*/
public function saveVMGreeting($ext,$type,$format,$file) {
$media = $this->FreePBX->Media;
$o = $this->getVoicemailBoxByExtension($ext);
$context = $o['vmcontext'];
$vmfolder = $this->vmPath . '/'.$context.'/'.$ext;
if(!file_exists($vmfolder)) {
mkdir($vmfolder,0777,true);
}
if(isset($this->greetings[$type])) {
$media->load($file);
$media->convert($vmfolder . "/" . $type . ".wav");
unlink($file);
return true;
} else {
return false;
}
}
/**
* Get a voicemail box by extension
* @param int $ext The extension
*/
public function getVoicemailBoxByExtension($ext) {
if(empty($this->vmBoxData[$ext])) {
$this->vmBoxData[$ext] = $this->getMailbox($ext);
}
return !empty($this->vmBoxData[$ext]) ? $this->vmBoxData[$ext] : false;
}
/**
* Get all greetings by extension
* @param int $ext The extension number
*/
public function getGreetingsByExtension($ext) {
$o = $this->getVoicemailBoxByExtension($ext);
//temp greeting <--overrides (temp.wav)