forked from FreePBX/ucp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUcp.class.php
1517 lines (1394 loc) · 50.3 KB
/
Ucp.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:
/**
* This is the User Control Panel Object.
*
* 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.
*/
//TODO: In 15 this needs to be namespaced!
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
//progress bar
use Symfony\Component\Console\Helper\ProgressBar;
class Ucp implements \BMO {
private $message;
private $registeredHooks = array();
private $brand = 'FreePBX';
private $tokenCache = false;
//node server
private $nodever = "6.5.0";
private $npmver = "3.10.3";
private $icuver = "50.1.2";
private $gcc = "4.8.5";
private $nodeloc = "/tmp";
public function __construct($freepbx = null) {
if ($freepbx == null) {
throw new Exception("Not given a FreePBX Object");
}
$this->FreePBX = $freepbx;
$this->Userman = $this->FreePBX->Userman;
$this->db = $freepbx->Database;
$this->brand = \FreePBX::Config()->get("DASHBOARD_FREEPBX_BRAND");
$this->nodeloc = __DIR__."/node";
}
/**
* get_OS : Retruns the name of the operating system.
*
* @return string
*/
public function get_OS(){
if(file_exists("/etc/os-release")){
$file_content = file_get_contents("/etc/os-release");
$f_content = explode("\n",$file_content);
foreach($f_content as $line){
list($key, $value) = explode("=", $line);
if(!empty($key)){
$var[trim($key)]= str_replace('"','',trim($value));
}
}
extract($var);
if(!empty($ID)){
return $ID;
}
}
$os = php_uname();
preg_match('/\bubuntu\b|\bdebian\b|\bcentos\b|\bfreepbx+[0-9]{2}\b/', strtolower($os), $matches, PREG_OFFSET_CAPTURE, 0);
if(!empty($matches[0][0])){
return trim($matches[0][0]);
}
return "unknown";
}
public function install() {
$settings = array(
'NODEJSENABLED' => false,
'NODEJSTLSENABLED' => false,
'NODEJSBINDADDRESS' => '::',
'NODEJSBINDPORT' => '8001',
'NODEJSHTTPSBINDADDRESS' => '::',
'NODEJSHTTPSBINDPORT' => '8003',
'NODEJSTLSCERTFILE' => '',
'NODEJSTLSPRIVATEKEY' => ''
);
exec("g++ --version",$output,$ret); //g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-4)
if(!empty($ret) || empty($output)) {
out(_("gcc-c++ is not installed"));
return false;
}
$output = exec("node --version"); //v0.10.29
$output = str_replace("v","",trim($output));
if(empty($output)) {
out(_("Node is not installed"));
return false;
}
if(version_compare($output,$this->nodever,"<")) {
out(sprintf(_("Node version is: %s requirement is %s. Run 'yum upgrade nodejs' from the CLI as root"),$output,$this->nodever));
return false;
}
$output = exec("npm --version"); //v0.10.29
$output = trim($output);
if(empty($output)) {
out(_("Node Package Manager is not installed"));
return false;
}
if(version_compare($output,$this->npmver,"<")) {
out(sprintf(_("NPM version is: %s requirement is %s. Run 'yum upgrade nodejs' from the CLI as root"),$output,$this->npmver));
return false;
}
$os = trim($this->get_OS());
out(_("System")." : ".$os);
switch ($os) {
case "ubuntu":
case "debian":
case "raspbian":
$output = exec("pkg-config --modversion icu-i18n", $out, $retval);
$output = trim($output);
if(empty($output)) {
out(_("icu, pkg-config or pkgconf is not installed. You need to run: apt-get install icu libicu-devel pkg-config pkgconf"));
return false;
}
break;
default:
$output = exec("icu-config --version"); //v4.2.1
$output = trim($output);
if(empty($output)) {
out(_("icu is not installed. You need to run: yum install icu libicu-devel"));
return false;
}
}
if(version_compare($output,$this->icuver,"<")) {
out(sprintf(_("ICU version is: %s requirement is %s"),$output,$this->icuver));
return false;
}
$webgroup = $this->FreePBX->Config->get('AMPASTERISKWEBGROUP');
$data = posix_getgrgid(filegroup($this->getHomeDir()));
if($data['name'] != $webgroup) {
out(sprintf(_("Home directory [%s] is not writable"),$this->getHomeDir()));
return false;
}
if(file_exists($this->getHomeDir()."/.npm")) {
$data = posix_getgrgid(filegroup($this->getHomeDir()."/.npm"));
if($data['name'] != $webgroup) {
out(sprintf(_("Home directory [%s] is not writable"),$this->getHomeDir()."/.npm"));
return false;
}
}
outn(_("Installing/Updating Required Libraries. This may take a while..."));
if (php_sapi_name() == "cli") {
out("The following messages are ONLY FOR DEBUGGING. Ignore anything that says 'WARN' or is just a warning");
}
$npmstatus = $this->FreePBX->Pm2->installNodeDependencies($this->nodeloc,function($data) {
outn($data);
});
if(!$npmstatus) {
out("");
out(_("Failed updating libraries!"));
} else {
out("");
out(_("Finished updating libraries!"));
}
$set = array();
$set['module'] = 'ucp';
$set['category'] = 'UCP NodeJS Server';
// NODEJSENABLED
$set['value'] = $settings['NODEJSENABLED'];
$set['defaultval'] = false;
$set['options'] = '';
$set['name'] = 'Enable the NodeJS Server';
$set['description'] = 'Whether to enable the backend server for UCP which allows instantaneous updates to the interface';
$set['emptyok'] = 0;
$set['level'] = 1;
$set['readonly'] = 0;
$set['type'] = CONF_TYPE_BOOL;
$this->FreePBX->Config->define_conf_setting('NODEJSENABLED',$set);
// NODEJSTLSENABLED
$set['value'] = $settings['NODEJSTLSENABLED'];
$set['defaultval'] = false;
$set['options'] = '';
$set['name'] = 'Enable TLS for the NodeJS Server';
$set['description'] = 'Whether to enable TLS for the backend server for UCP which allows instantaneous updates to the interface';
$set['emptyok'] = 0;
$set['level'] = 1;
$set['readonly'] = 0;
$set['type'] = CONF_TYPE_BOOL;
$this->FreePBX->Config->define_conf_setting('NODEJSTLSENABLED',$set);
// NODEJSBINDADDRESS
$set['value'] = $settings['NODEJSBINDADDRESS'];
$set['defaultval'] =& $set['value'];
$set['options'] = '';
$set['name'] = 'NodeJS Bind Address';
$set['description'] = 'Address to bind to. Default is "::" (Listen for all IPv4 and IPv6 Connections)';
$set['emptyok'] = 0;
$set['type'] = CONF_TYPE_TEXT;
$set['level'] = 2;
$set['readonly'] = 0;
$this->FreePBX->Config->define_conf_setting('NODEJSBINDADDRESS',$set);
// NODEJSBINDPORT
$set['value'] = $settings['NODEJSBINDPORT'];
$set['defaultval'] =& $set['value'];
$set['options'] = '';
$set['name'] = 'NodeJS Bind Port';
$set['description'] = 'Port to bind to. Default is 8001';
$set['emptyok'] = 0;
$set['options'] = array(10,65536);
$set['type'] = CONF_TYPE_INT;
$set['level'] = 2;
$set['readonly'] = 0;
$this->FreePBX->Config->define_conf_setting('NODEJSBINDPORT',$set);
// NODEJSHTTPSBINDADDRESS
$set['value'] = $settings['NODEJSHTTPSBINDADDRESS'];
$set['defaultval'] =& $set['value'];
$set['options'] = '';
$set['name'] = 'NodeJS HTTPS Bind Address';
$set['description'] = 'Address to bind to. Default is "::" (Listen for all IPv4 and IPv6 Connections)';
$set['emptyok'] = 0;
$set['type'] = CONF_TYPE_TEXT;
$set['level'] = 2;
$set['readonly'] = 0;
$this->FreePBX->Config->define_conf_setting('NODEJSHTTPSBINDADDRESS',$set);
// NODEJSHTTPSBINDPORT
$set['value'] = $settings['NODEJSHTTPSBINDPORT'];
$set['defaultval'] =& $set['value'];
$set['options'] = '';
$set['name'] = 'NodeJS HTTPS Bind Port';
$set['description'] = 'Port to bind to. Default is 8003';
$set['emptyok'] = 0;
$set['options'] = array(10,65536);
$set['type'] = CONF_TYPE_INT;
$set['level'] = 2;
$set['readonly'] = 0;
$this->FreePBX->Config->define_conf_setting('NODEJSHTTPSBINDPORT',$set);
// NODEJSTLSCERTFILE
$set['value'] = $settings['NODEJSTLSCERTFILE'];
$set['defaultval'] =& $set['value'];
$set['options'] = '';
$set['name'] = 'NodeJS HTTPS TLS Certificate Location';
$set['description'] = 'Sets the path to the HTTPS server certificate. This is required if "Enable TLS for the NodeJS Server" is set to yes.';
$set['emptyok'] = 1;
$set['type'] = CONF_TYPE_TEXT;
$set['level'] = 2;
$set['readonly'] = 0;
$this->FreePBX->Config->define_conf_setting('NODEJSTLSCERTFILE',$set);
// NODEJSTLSPRIVATEKEY
$set['value'] = $settings['NODEJSTLSPRIVATEKEY'];
$set['defaultval'] =& $set['value'];
$set['options'] = '';
$set['name'] = 'NodeJS HTTPS TLS Private Key Location';
$set['description'] = 'Sets the path to the HTTPS private key. This is required if "Enable TLS for the NodeJS Server" is set to yes.';
$set['emptyok'] = 1;
$set['type'] = CONF_TYPE_TEXT;
$set['level'] = 2;
$set['readonly'] = 0;
$this->FreePBX->Config->define_conf_setting('NODEJSTLSPRIVATEKEY',$set);
$this->FreePBX->Config->commit_conf_settings();
$cert = $this->FreePBX->Certman->getDefaultCertDetails();
if(!empty($cert)) {
$this->setDefaultCert($cert, false, false);
}
if($this->FreePBX->Modules->checkStatus("sysadmin")) {
touch("/var/spool/asterisk/incron/ucp.logrotate");
}
//If we are root then start it as asterisk, otherwise we arent root so start it as the web user (all we can do really)
outn(_("Stopping old running processes..."));
$this->stopFreepbx();
out(_("Done"));
$this->expireAllUserSessions();
if($npmstatus) {
outn(_("Starting new UCP Node Process..."));
$started = $this->startFreepbx();
if(!$started) {
out(_("Failed or Disabled!"));
} else {
out(sprintf(_("Started with PID %s!"),$started));
}
}
out(_("Refreshing all UCP Assets, this could take a while..."));
$this->generateUCP(true);
out("Done!");
}
public function uninstall() {
$path = $this->FreePBX->Config->get_conf_setting('AMPWEBROOT');
$location = $path.'/ucp';
unlink($location);
outn(_("Stopping old running processes..."));
$this->stopFreepbx();
out(_("Done"));
exec("rm -Rf ".$this->nodeloc."/node_modules");
try {
$this->FreePBX->Pm2->delete("ucp");
} catch(\Exception $e) {}
}
public function backup(){
}
public function restore($backup){
}
/**
* Force UCP to refresh on next page load
* @param int $uid User Manager ID
*/
public function refreshInterface($uid) {
if(!empty($uid)) {
$ref = $this->Userman->getModuleSettingByID($uid,'ucp|Global','flushPage');
if($ref) {
$this->Userman->setModuleSettingByID($uid,'ucp|Global','flushPage',false);
}
return $ref;
}
return false;
}
public function usermanShowPage() {
if(isset($_REQUEST['action'])) {
$mode = ($_REQUEST['action'] == "showgroup" || $_REQUEST['action'] == "addgroup" ) ? "group" : "user";
switch($_REQUEST['action']) {
case 'showgroup':
$group = $this->getGroupByGID($_REQUEST['group']);
$ausers = array(
'self' => _("User Primary Extension")
);
$users = core_users_list();
if(!empty($users) && is_array($users)) {
foreach($users as $list) {
$ausers[$list[0]] = $list[1] . " <".$list[0].">";
}
}
$sassigned = $this->Userman->getModuleSettingByGID($_REQUEST['group'],'ucp|Settings','assigned');
$sassigned = !empty($sassigned) ? $sassigned : array();
$tempList = $this->Userman->getAllUcpTemplates();
return array(
array(
"title" => "UCP",
"rawname" => "ucp",
"content" => load_view(dirname(__FILE__).'/views/users_hook.php',array(
"mode" => $mode,
"ausers" => $ausers,
"sassigned" => $sassigned,
"mHtml" => $this->constructModuleConfigPages('group',$group,$_REQUEST['action']),
"user" => array(),
"allowLogin" => $this->Userman->getModuleSettingByGID($_REQUEST['group'],'ucp|Global','allowLogin'),
"originate" => $this->Userman->getModuleSettingByGID($_REQUEST['group'],'ucp|Global','originate'),
"isUserRestricted" => $this->Userman->getModuleSettingByGID($_REQUEST['group'],'ucp|Global','isUserRestricted'),
"tourMode" => $this->Userman->getModuleSettingByGID($_REQUEST['group'],'ucp|Global','tour'),
"tempList" => $tempList,
"assignedTemplate" => $this->Userman->getModuleSettingByGID($_REQUEST['group'],'ucp|template','templateid'),
"selectTemplate" => $this->Userman->getModuleSettingByGID($_REQUEST['group'],'ucp|template','assigntemplate'))
)
)
);
break;
case 'addgroup':
$ausers = array(
'self' => _("User Primary Extension")
);
$users = core_users_list();
if(!empty($users) && is_array($users)) {
foreach($users as $list) {
$ausers[$list[0]] = $list[1] . " <".$list[0].">";
}
}
$tempList = $this->Userman->getAllUcpTemplates();
return array(
array(
"title" => "UCP",
"rawname" => "ucp",
"content" => load_view(dirname(__FILE__).'/views/users_hook.php',array(
"mode" => $mode,
"ausers" => $ausers,
"sassigned" => array('self'),
"mHtml" => $this->constructModuleConfigPages('group', array(),$_REQUEST['action']),
"user" => array(),
"allowLogin" => true,
"originate" => false,
"tourMode" => true,
"selectTemplate" => false,
"tempList" => $tempList )
)
)
);
break;
case 'showuser':
$user = $this->getUserByID($_REQUEST['user']);
if(!empty($_REQUEST['deletesession'])) {
$this->expireUserSession($_REQUEST['deletesession']);
$this->setUsermanMessage(_('Deleted User Session'),'success');
}
$ausers = array();
$sassigned = $this->getSetting($user['username'],'Settings','assigned');
$users = core_users_list();
if(!empty($users) && is_array($users)) {
foreach($users as $list) {
$ausers[$list[0]] = $list[1] . " <".$list[0].">";
}
}
$sassigned = !empty($sassigned) ? $sassigned : array();
$tempList = $this->Userman->getAllUcpTemplates();
return array(
array(
"title" => "UCP",
"rawname" => "ucp",
"content" => load_view(dirname(__FILE__).'/views/users_hook.php',array(
"mode" => $mode,
"ausers" => $ausers,
"sassigned" => $sassigned,
"mHtml" => $this->constructModuleConfigPages('user',$user,$_REQUEST['action']),
"user" => $user,
"allowLogin" => FreePBX::create()->Userman->getModuleSettingByID($_REQUEST['user'],'ucp|Global','allowLogin',true),
"originate" => FreePBX::create()->Userman->getModuleSettingByID($_REQUEST['user'],'ucp|Global','originate',true),
"isUserRestricted" => FreePBX::create()->Userman->getModuleSettingByID($_REQUEST['user'],'ucp|Global','isUserRestricted',true),
"tourMode" => FreePBX::create()->Userman->getModuleSettingByID($_REQUEST['user'],'ucp|Global','tour',true),
"sessions" => $this->getUserSessions($user['id']),
"tempList" => $tempList,
"assignedTemplate" => $this->Userman->getModuleSettingByID($_REQUEST['user'],'ucp|template','templateid',true),
"selectTemplate" => $this->Userman->getModuleSettingByID($_REQUEST['user'],'ucp|template','assigntemplate',true)
)
)
)
);
break;
case 'adduser':
$ausers = array();
$users = core_users_list();
if(!empty($users) && is_array($users)) {
foreach($users as $list) {
$ausers[$list[0]] = $list[1] . " <".$list[0].">";
}
}
$tempList = $this->Userman->getAllUcpTemplates();
return array(
array(
"title" => "UCP",
"rawname" => "ucp",
"content" => load_view(dirname(__FILE__).'/views/users_hook.php',array(
"mode" => $mode,
"ausers" => $ausers,
"sassigned" => array('self'),
"mHtml" => $this->constructModuleConfigPages('user',array(),$_REQUEST['action']),
"user" => array(),
"allowLogin" => null,
"originate" => null,
"tourMode" => null,
"sessions" => array(),
"selectTemplate" => null,
"tempList" => $tempList
)
)
)
);
break;
default:
break;
}
}
}
/**
* Hook functionality for sending an email from userman
* @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 usermanSendEmail($id, $display, $data) {
$hostname = '';
if(isset($data['host'])){
$hostname = $data['host'];
}
$link = $this->getUcpLink($hostname);
$usettings = $this->FreePBX->Userman->getAuthAllPermissions();
$final = array(
"\t".sprintf(_('User Control Panel: %s'),$link),
);
if(!$data['password'] && $usettings['changePassword']) {
$token = $this->FreePBX->Userman->generatePasswordResetToken($id,"1 day",true);
$final[] = "\n".sprintf(_('Password Reset Link (Valid Until: %s): %s'),date("F j, Y, g:i A", $token['valid']),$link."/?forgot=".$token['token']);
}
return $final;
}
public function validatePasswordResetToken($token) {
return $this->FreePBX->Userman->validatePasswordResetToken($token);
}
public function resetPasswordWithToken($token,$newpassword) {
return $this->FreePBX->Userman->resetPasswordWithToken($token,$newpassword);
}
/**
* Get the correct URL for UCP
*
* This tries to use the UCP port specified in sysadmin,
* if available. Otherwise it defaults to however this
* was requested, with the /ucp/ path.
*
* return string $url
*/
public function getUcpLink($hostname = null) {
if(empty($hostname)){
$hostname = $_SERVER["SERVER_NAME"];
}else{
$tmp_data = parse_url($hostname);
if(isset($tmp_data['host'])){
$hostname = $tmp_data['host'];
}else{
$hostname = $tmp_data['path'];
}
}
// Start by checking if Sysadmin exists. If it does, try using that.
if($this->FreePBX->Modules->moduleHasMethod("sysadmin","getPorts")) {
$ports = \FreePBX::Sysadmin()->getPorts();
} else {
if(!function_exists('sysadmin_get_portmgmt')) {
// Is sysadmin installed on this machine, but just not loaded?
if (file_exists($this->FreePBX->Config()->get('AMPWEBROOT').'/admin/modules/sysadmin/functions.inc.php')) {
include $this->FreePBX->Config()->get('AMPWEBROOT').'/admin/modules/sysadmin/functions.inc.php';
}
}
if(function_exists('sysadmin_get_portmgmt')) {
$ports = sysadmin_get_portmgmt();
} else {
// No sysadmin on this machine. Let's try and figure out what we've got.
if (isset($_SERVER["HTTPS"])) {
// We're using a SSL connection to request this
$ports = array("sslacp" => $_SERVER["SERVER_PORT"]);
} else {
$ports = array("acp" => $_SERVER["SERVER_PORT"]);
}
}
}
// 1. Prefer SSL Ucp port over anything.
if (isset($ports['sslucp']) && is_numeric($ports['sslucp'])) {
if ($ports['sslucp'] == 443) {
$url = 'https://'. $hostname;
} else {
$url = 'https://'.$hostname.":".$ports['sslucp'];
}
// 2. Try http UCP Port next
} else if(isset($ports['ucp']) && is_numeric($ports['ucp'])) {
if ($ports['ucp'] == 80) {
$url = 'http://'.$hostname;
} else {
$url = 'http://'.$hostname.":".$ports['ucp'];
}
// 3. Try sslacp as our third option
} else if(isset($ports['sslacp']) && is_numeric($ports['sslacp'])) {
if ($ports['sslacp'] == 443) {
$url = 'https://'.$hostname.'/ucp';
} else {
$url = 'https://'.$hostname.":".$ports['sslacp'].'/ucp';
}
// 4. Try normal acp as our third option
} else if(isset($ports['acp']) && is_numeric($ports['acp'])) {
if ($ports['acp'] == 80) {
$url = 'http://'.$hostname.'/ucp';
} else {
$url = 'http://'.$hostname.":".$ports['acp'].'/ucp';
}
} else {
// Somehow I didn't get a SERVER_NAME, so I don't know what url
// to hand back.
$url = 'invalid://unknown.server.name/ucp';
}
return $url;
}
/**
* Sends a password reset email
* @param {int} $id The userid
*/
public function sendPassResetEmail($id) {
global $amp_conf;
$user = $this->getUserByID($id);
if(empty($user) || empty($user['email'])) {
return false;
}
// Forcefully create reset token
$token = $this->Userman->generatePasswordResetToken($id, null, true);
if(!$token) {
freepbx_log(FPBX_LOG_NOTICE, "Unable to generate password token for ".$user['username']);
return false;
}
$user['token'] = $token['token'];
$user['brand'] = $this->brand;
$user['link'] = $this->getUcpLink()."/?forgot=".$user['token'];
$user['valid'] = date("Y-m-d h:i:s A", $token['valid']);
$template = file_get_contents(__DIR__.'/views/emails/reset_text.tpl');
preg_match_all('/%([\w|\d]*)%/',$template,$matches);
foreach($matches[1] as $match) {
$replacement = !empty($user[$match]) ? $user[$match] : '';
$template = str_replace('%'.$match.'%',$replacement,$template);
}
return $this->Userman->sendEmail($user['id'],$this->brand . " password reset",$template);
}
public function delGroup($id,$display,$data) {
$this->FreePBX->Hooks->processHooks($id,$display,false,$data);
$group = $this->Userman->getGroupByGID($id);
if(isset($group['users']) && is_array($group['users'])) {
foreach($group['users'] as $user) {
$this->expireUserSessions($user);
}
}
}
public function addGroup($id, $display, $data) {
if($display == 'userman' && isset($_POST['type']) && $_POST['type'] == 'group') {
if($_POST['ucp_tour'] == 'true') {
$this->Userman->setModuleSettingByGID($id,'ucp|Global','tour', true);
} else {
$this->Userman->setModuleSettingByGID($id,'ucp|Global','tour', false);
}
if($_POST['ucp_login'] == 'true') {
$this->Userman->setModuleSettingByGID($id,'ucp|Global','allowLogin', true);
} else {
$this->Userman->setModuleSettingByGID($id,'ucp|Global','allowLogin', false);
}
if($_POST['ucp_originate'] == 'yes') {
$this->Userman->setModuleSettingByGID($id,'ucp|Global','originate', true);
} else {
$this->Userman->setModuleSettingByGID($id,'ucp|Global','originate', false);
}
if($_POST['ucp_isUserRestricted'] == 'true') {
$this->Userman->setModuleSettingByGID($id,'ucp|Global','isUserRestricted', true);
} else {
$this->Userman->setModuleSettingByGID($id,'ucp|Global','isUserRestricted', false);
}
$this->Userman->setModuleSettingByGID($id,'ucp|Settings','assigned', $_POST['ucp_settings']);
if($_POST['assign_template'] == 'true') {
$this->Userman->setModuleSettingByGID($id,'ucp|template','assigntemplate',true);
$this->Userman->setModuleSettingByGID($id,'ucp|template','templateid',$_POST['templateid']);
} else {
$this->Userman->setModuleSettingByGID($id,'ucp|template','assigntemplate',false);
$this->Userman->setModuleSettingByGID($id,'ucp|template','templateid',null);
}
}
$login = $this->Userman->getModuleSettingByGID($id,'ucp|Global','originate');
$this->FreePBX->Hooks->processHooks($id,$display,$login,$data);
$group = $this->Userman->getGroupByGID($id);
foreach($group['users'] as $user) {
$this->FreePBX->Userman->setModuleSettingByID($user,'ucp|Global','flushPage',true);
}
}
public function updateGroup($id,$display,$data) {
if($display == 'userman' && isset($_POST['type']) && $_POST['type'] == 'group') {
if($_POST['ucp_tour'] == 'true') {
$this->Userman->setModuleSettingByGID($id,'ucp|Global','tour', true);
} else {
$this->Userman->setModuleSettingByGID($id,'ucp|Global','tour', false);
}
if($_POST['ucp_login'] == 'true') {
$this->Userman->setModuleSettingByGID($id,'ucp|Global','allowLogin', true);
} else {
$this->Userman->setModuleSettingByGID($id,'ucp|Global','allowLogin', false);
}
if($_POST['ucp_originate'] == 'yes') {
$this->Userman->setModuleSettingByGID($id,'ucp|Global','originate', true);
} else {
$this->Userman->setModuleSettingByGID($id,'ucp|Global','originate', false);
}
if($_POST['ucp_isUserRestricted'] == 'true') {
$this->Userman->setModuleSettingByGID($id,'ucp|Global','isUserRestricted', true);
} else {
$this->Userman->setModuleSettingByGID($id,'ucp|Global','isUserRestricted', false);
}
$this->Userman->setModuleSettingByGID($id,'ucp|Settings','assigned', $_POST['ucp_settings']);
if($_POST['assign_template'] == 'true') {
$this->Userman->setModuleSettingByGID($id,'ucp|template','assigntemplate',true);
$this->Userman->setModuleSettingByGID($id,'ucp|template','templateid',$_POST['templateid']);
} else {
$this->Userman->setModuleSettingByGID($id,'ucp|template','assigntemplate',false);
$this->Userman->setModuleSettingByGID($id,'ucp|template','templateid',null);
}
}
$login = $this->Userman->getModuleSettingByGID($id,'ucp|Global','originate');
$this->FreePBX->Hooks->processHooks($id,$display,$login,$data);
$group = $this->Userman->getGroupByGID($id);
foreach($group['users'] as $user) {
$this->FreePBX->Userman->setModuleSettingByID($user,'ucp|Global','flushPage',true);
}
}
/**
* 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 delUser($id, $display, $data) {
$this->expireUserSessions($id);
$this->deleteUser($id);
$this->FreePBX->Hooks->processHooks($id,$display,false,$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 addUser($id, $display, $data) {
if($display == 'userman' && isset($_POST['type']) && $_POST['type'] == 'user') {
if(isset($_POST['ucp_login'])) {
if($_POST['ucp_tour'] == 'true') {
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|Global','tour',true);
} elseif($_POST['ucp_tour'] == 'false') {
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|Global','tour',false);
} else {
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|Global','tour',null);
}
if($_POST['ucp_login'] == 'true') {
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|Global','allowLogin',true);
} elseif($_POST['ucp_login'] == 'false') {
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|Global','allowLogin',false);
} else {
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|Global','allowLogin',null);
}
if(isset($_POST['ucp_settings'])) {
$this->setSettingByID($id,'Settings','assigned',$_POST['ucp_settings']);
} else {
$this->setSettingByID($id,'Settings','assigned',null);
}
if($_POST['ucp_originate'] == 'yes') {
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|Global','originate',true);
} elseif($_POST['ucp_originate'] == 'no') {
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|Global','originate',false);
} else {
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|Global','originate',null);
}
if($_POST['ucp_isUserRestricted'] == 'true') {
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|Global','isUserRestricted', true);
} else if($_POST['ucp_isUserRestricted'] == 'false') {
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|Global','isUserRestricted', false);
} else {
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|Global','isUserRestricted', null);
}
if($_POST['assign_template'] == 'true') {
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|template','assigntemplate',true);
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|template','templateid',$_POST['templateid']);
} elseif($_POST['assign_template'] == 'false') {
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|template','assigntemplate',false);
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|template','templateid',null);
} else {
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|template','assigntemplate',null);
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|template','templateid',null);
}
}
}
$login = $this->FreePBX->Userman->getModuleSettingByID($id,'ucp|Global','allowLogin');
$this->FreePBX->Hooks->processHooks($id,$display,$login,$data);
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|Global','flushPage',true);
}
/**
* 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 updateUser($id, $display, $data) {
if($display == 'userman' && isset($_POST['type']) && $_POST['type'] == 'user') {
if(isset($_POST['ucp_login'])) {
if($_POST['ucp_tour'] == 'true') {
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|Global','tour',true);
} elseif($_POST['ucp_tour'] == 'false') {
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|Global','tour',false);
} else {
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|Global','tour',null);
}
if($_POST['ucp_login'] == 'true') {
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|Global','allowLogin',true);
} elseif($_POST['ucp_login'] == 'false') {
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|Global','allowLogin',false);
} else {
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|Global','allowLogin',null);
}
if(isset($_POST['ucp_settings'])) {
$this->setSettingByID($id,'Settings','assigned',$_POST['ucp_settings']);
} else {
$this->setSettingByID($id,'Settings','assigned',null);
}
if($_POST['ucp_originate'] == 'yes') {
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|Global','originate',true);
} elseif($_POST['ucp_originate'] == 'no') {
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|Global','originate',false);
} else {
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|Global','originate',null);
}
if($_POST['ucp_isUserRestricted'] == 'true') {
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|Global','isUserRestricted', true);
} else if($_POST['ucp_isUserRestricted'] == 'false') {
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|Global','isUserRestricted', false);
} else {
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|Global','isUserRestricted', null);
}
if($_POST['assign_template'] == 'true') {
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|template','assigntemplate',true);
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|template','templateid',$_POST['templateid']);
} elseif($_POST['assign_template'] == 'false') {
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|template','assigntemplate',false);
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|template','templateid',null);
} else {
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|template','assigntemplate',null);
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|template','templateid',null);
}
}
}
$login = $this->FreePBX->Userman->getModuleSettingByID($id,'ucp|Global','allowLogin');
$this->FreePBX->Hooks->processHooks($id,$display,$login,$data);
$this->FreePBX->Userman->setModuleSettingByID($id,'ucp|Global','flushPage',true);
return true;
}
/**
* Get language from a module and make it json for UCP translations
* @param {string} $language The Language name
* @param {array} $modules Array of module rawnames
*/
public function getModulesLanguage($language, $modules) {
if(!class_exists("po2json")) {
require_once(__DIR__."/includes/po2json.php");
}
$final = array();
$root = $this->FreePBX->Config->get("AMPWEBROOT");
//first get ucp
$po = $root."/admin/modules/ucp/i18n/" . $language . "/LC_MESSAGES/ucp.po";
if(file_exists($po)) {
$c = new po2json($po,"ucp");
$array = $c->po2array();
if(!empty($array)) {
$final['ucp'] = $array;
}
}
//now get the modules
foreach ($modules as $module) {
$module = strtolower($module);
$po = $root."/admin/modules/".$module."/i18n/" . $language . "/LC_MESSAGES/".$module.".po";
if(file_exists($po)) {
$c = new po2json($po,$module);
$array = $c->po2array();
if(!empty($array)) {
$final[$module] = $array;
}
}
}
return json_encode($final);
}
/**
* Register a hook from another module
* This is semi depreciated as FreePBX 12 has hooking functions now
* @param {string} $action The action
* @param {string} $class The class name
* @param {string} $method The method name
*/
public function registerHook($action,$class,$method) {
$this->registeredHooks[$action] = array('class' => $class, 'method' => $method);
}
/**
* Construct Module Configuration Pages
* This is used to setup and display module configuration pages
* in User Manager
* @param {array} $user The user array
*/
public function constructModuleConfigPages($mode, $user, $action) {
$mods = $this->FreePBX->Hooks->processHooks($mode, $user, $action);
$html = [];
if(!empty($mods) && is_array($mods)) {
foreach($mods as $module) {
if(!empty($module) && is_array($module)) {
foreach($module as $item) {
if(empty($item)) {
continue;
}
if(is_array($item)) {
if(!isset($html[$item['rawname']])) {
$html[$item['rawname']] = array(
"title" => $item['title'],
"rawname" => $item['rawname'],
"content" => $item['content']
);
} else {
$item['rawname']['content'] .= $item['content'];
}
} else {
if(!isset($html[$mod])) {
$html[$mod] = array(
"title" => ucfirst(strtolower($mod)),
"rawname" => $mod,
"content" => $item
);
} else {
$item[$mod]['content'] .= $item;
}
}
}
}
}
}
return $html;
}
/**
* Retrieve Conf Hook to search all modules and add their respective UCP folders
*/
public function genConfig() {
$this->generateUCP();
}
/**
* Generate UCP assets if needed
* @param {bool} $regenassets = false If set to true regenerate assets even if not needed
*/
public function generateUCP($regenassets = false) {
$moduleFunctionsCreate = module_functions::create();
$modulef =& $moduleFunctionsCreate;
$modules = $modulef->getinfo(false);
$path = $this->FreePBX->Config->get_conf_setting('AMPWEBROOT');
$location = $path.'/ucp';
if(!file_exists($location)) {
symlink(dirname(__FILE__).'/htdocs',$location);
}
foreach($modules as $module) {
if(isset($module['rawname'])) {
$rawname = trim($module['rawname']);
if(file_exists($path.'/admin/modules/'.$rawname.'/ucp') && file_exists($path.'/admin/modules/'.$rawname.'/ucp/'.ucfirst($rawname).".class.php")) {
if($module['status'] == MODULE_STATUS_ENABLED) {
if(!file_exists($location."/modules/".ucfirst($rawname))) {
symlink($path.'/admin/modules/'.$rawname.'/ucp',$location.'/modules/'.ucfirst($rawname));
}
} elseif($module['status'] != MODULE_STATUS_DISABLED && $module['status'] != MODULE_STATUS_ENABLED) {
if(file_exists($location."/modules/".ucfirst($rawname)) && is_link($location."/modules/".ucfirst($rawname))) {
unlink($location."/modules/".ucfirst($rawname));
}
}
}
}
}
if($regenassets) {
$this->refreshAssets();
}
}
public function deleteUser($uid) {
//run module functions here if needed otherwise usermanager delete's most of what we are using
}
public function writeConfig($conf){
$this->FreePBX->WriteConfig($conf);
}
public function doConfigPageInit($display) {
switch($_REQUEST['category']) {
case 'users':
if(isset($_POST['submit'])) {
$user = $this->getUserByID($_REQUEST['user']);