forked from FreePBX/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_amp
executable file
·1777 lines (1612 loc) · 58.1 KB
/
install_amp
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
#!/usr/bin/php -q
<?php
// License for all code of this FreePBX module can be found in the license file inside the module directory
// Copyright 2006-2014 Schmooze Com Inc.
//
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
echo "FreePBX Requires PHP Version 5.3.0 or Higher, you have: ".PHP_VERSION."\n";
die();
}
date_default_timezone_set('America/Los_Angeles');
require_once ("libfreepbx.install.php");
require_once(dirname(__FILE__) . '/amp_conf/htdocs/admin/libraries/php-upgrade.functions.php');
require_once(dirname(__FILE__) . '/amp_conf/htdocs/admin/libraries/sql.functions.php');
require_once(dirname(__FILE__) . '/amp_conf/htdocs/admin/libraries/utility.functions.php');
# constants
define("AMP_CONF", "/etc/amportal.conf");
define("ASTERISK_CONF", "/etc/asterisk/asterisk.conf");
define("UPGRADE_DIR", dirname(__FILE__)."/upgrades");
define("MODULE_DIR", dirname(__FILE__)."/amp_conf/htdocs/admin/modules/");
# semi constants
$webroot = "/var/www/html";
$ampsbin_dir = "/usr/local/sbin"; // default if not set
$ampbin_dir = "/var/lib/asterisk/bin";
$asterisk_user = "asteriskuser";
$asterisk_pass = "amp109";
/********************************************************************************************************************/
function showHelp() {
out("Optional parameters:",false);
out(" --help, -h, -? Show this help",false);
out(" --dbhost <ip address> Use a remote database server",false);
out(" --dbname databasename Use database name specified, instead of 'asterisk'",false);
out(" --username <user> Use <user> to connect to db and write config",false);
out(" --password <pass> Use <pass> to connect to db and write config",false);
out(" --freepbxip <ip> FreePBX web interface is accessed from <ip>",false);
out(" --webroot <path> Web root where FreePBX will be installed",false);
out(" --cgibin <path> Path where cgi-bin's lives",false);
out(" --bin <path> Path of asterisk binaries",false);
out(" --sbin <path> Path of system admin binaries",false);
out(" --asteriskuser <user> Asterisk Manager username",false);
out(" --asteriskpass <pass> Asterisk Manager password",false);
out(" --asteriskip <ip> Asterisk Manager IP address",false);
out(" --debug Enable debug output",false);
out(" --dry-run Don't actually do anything",false);
out(" --force-version <ver> Force upgrade from version <ver>",false);
out(" --skip-module-install Don't run install scripts for packaged modules, the files are still loaded.",false);
out(" In a development environment you may not want the install scripts run.",false);
out(" --no-files Just run updates without installing files",false);
out(" --force-overwrite Don't ask if it is ok to overwrite modified files, assume yes for all.",false);
out(" --install-moh Install default music-on-hold files (normally doesn't, unless ",false);
out(" it's a new installation)",false);
out(" --dev-links Make links to files in the source directory instead of copying",false);
out(" (intended for developers only)",false);
out(" --update-links Update dev links without running through the whole install process (Bail before modules)", false);
out(" --engine <name> Use the specified PBX Engine ('asterisk')",false);
out(" --uid <user> Set ownership of files to user",false);
out(" --gid <group> Set ownership of files to group",false);
out(" --scripted Scripted install. Do not ask any questions.",false);
out(" (Intended for unattended installers only)",false);
out(" --set-freepbx-settings Force set the internal FreePBX DB Settings with install time parameters",false);
out(" --installdb Set up the FreePBX database",false);
out(" --cleaninstall Like 'make clean' will clean up any previous settings or previous setup while installing a-new",false);
out(" --uninstall Completely Uninstall FreePBX",false);
}
function install_parse_amportal_conf($filename) {
$file = file($filename);
foreach ($file as $line) {
if (preg_match("/^\s*([a-zA-Z0-9]+)\s*=\s*(.*)\s*([;#].*)?/",$line,$matches)) {
// TODO: force booleans into 0/1 this should be ok given the limited number of configuration values
//
switch (trim(strtolower($matches[2]))) {
case 'false':
case 'no':
case 'off':
case '0':
$conf[$matches[1]] = '0';
case 'true':
case 'yes':
case 'on':
case '1':
$conf[$matches[1]] = '1';
default:
$conf[$matches[1]] = $matches[2];
break;
}
}
}
// use same defaults as function.inc.php
if ( !isset($conf["AMPDBENGINE"]) || ($conf["AMPDBENGINE"] == "")) {
$conf["AMPDBENGINE"] = "mysql";
}
if ( !isset($conf["AMPDBNAME"]) || ($conf["AMPDBNAME"] == "")) {
$conf["AMPDBNAME"] = "asterisk";
}
if ( !isset($conf["AMPENGINE"]) || ($conf["AMPENGINE"] == "")) {
$conf["AMPENGINE"] = "asterisk";
}
out("parsed amp_conf variables from $filename:");
foreach ($conf as $keyword => $value) {
out("amp_conf [$keyword] => [$value]");
}
return $conf;
}
function install_parse_asterisk_conf($filename) {
$file = file($filename);
foreach ($file as $line) {
if (preg_match("/^\s*([a-zA-Z0-9]+)\s* (?:=>|=) \s*(.*)\s*([;#].*)?/",$line,$matches)) {
$conf[ $matches[1] ] = $matches[2];
}
}
// Now set defaults if not set (although asterisk should fail if not set but at least
// it will get the setup somewhat right
//
if (!isset($asterisk_conf['astetcdir'])) { $asterisk_conf['astetcdir'] = "/etc/asterisk"; }
if (!isset($asterisk_conf['astmoddir'])) { $asterisk_conf['astmoddir'] = "/usr/lib/asterisk/modules"; }
if (!isset($asterisk_conf['astvarlibdir'])) { $asterisk_conf['astvarlibdir'] = "/var/lib/asterisk"; }
if (!isset($asterisk_conf['astagidir'])) { $asterisk_conf['astagidir'] = "/var/lib/asterisk/agi-bin"; }
if (!isset($asterisk_conf['astspooldir'])) { $asterisk_conf['astspooldir'] = "/var/spool/asterisk"; }
if (!isset($asterisk_conf['astrundir'])) { $asterisk_conf['astrundir'] = "/var/run/asterisk"; }
if (!isset($asterisk_conf['astlogdir'])) { $asterisk_conf['astlogdir'] = "/var/log/asterisk"; }
return $conf;
}
function write_amportal_conf($filename, $conf) {
$file = file($filename);
// parse through the file
foreach (array_keys($file) as $key) {
if (preg_match("/^\s*([a-zA-Z0-9]+)\s*=\s*(.*)\s*([;#].*)?/",$file[$key],$matches)) {
// this is an option=value line
if (isset($conf[ $matches[1] ])) {
// rewrite the line, if we have this in $conf
$file[$key] = $matches[1]."=".$conf[ $matches[1] ]."\n";
// unset it so we know what's new
unset($conf[ $matches[1] ]);
}
}
}
// add new entries
foreach ($conf as $key=>$val) {
$file[] = $key."=".$val."\n";
}
// write the file
if (!$fd = fopen($filename, "w")) {
fatal("Could not open ".$filename." for writing");
}
fwrite($fd, implode("",$file));
fclose($fd);
}
function ask_overwrite($file1, $file2) {
global $check_md5s;
do {
out($file2." has been changed from the original version.");
outn("Overwrite (y=yes/a=all/n=no/d=diff/s=shell/x=exit)? ");
$key = fgets(STDIN,1024);
switch (strtolower($key[0])) {
case "y": return true;
case "a": $check_md5s=false; return true;
case "n": return false;
case "d":
out("");
// w = ignore whitespace, u = unified
passthru("diff -wu ".escapeshellarg($file2)." ".escapeshellarg($file1));
break;
case "s":
if (function_exists("pcntl_fork")) {
out("");
$shell = (isset($_ENV["SHELL"]) ? $_ENV["SHELL"] : "/bin/bash");
out("Dropping to shell. Type 'exit' to return");
out("-> Original file: ".$file2);
out("-> New file: ".$file1);
$pid = pcntl_fork();
if ($pid == -1) {
out("[ERROR] cannot fork");
} else if ($pid) {
// parent
pcntl_waitpid($pid, $status);
// we wait till the child exits/dies/whatever
} else {
pcntl_exec($shell, array(), $_ENV);
}
out("Returned from shell");
} else {
out("[ERROR] PHP not built with process control (--enable-pcntl) support: cannot spawn shell");
}
break;
case "x":
out("-> Original file: ".$file2);
out("-> New file: ".$file1);
out("Exiting install program.");
exit(1);
break;
}
out("");
} while(1);
}
/** Write AMP-generated configuration files
*/
function generate_configs() {
global $amp_conf;
global $dryrun;
global $debug;
global $runas_uid;
out("Generating Configurations.conf, (if Asterisk is not running, you will get an error)");
out("In case of error, start Asterisk and hit the red bar in the GUI to generate the Configurations.conf files");
if (!$dryrun) {
// added --run-install to make it work like it has been working since retrieve_conf changed to not run module install scripts by default
//
// TODO: Should check if Asterisk is running and/or try to start it.
//
passthru("sudo -u $runas_uid ".trim($amp_conf["AMPBIN"])."/retrieve_conf --run-install --skip-registry-checks ".($debug ? ' --debug' : ''));
}
}
/** Collect AMP settings */
function collect_settings($filename, $dbhost = '', $dbuser = '', $dbpass = '', $dbname = 'asterisk') {
global $webroot;
global $ampsbin_dir;
global $ampbin_dir;
global $asterisk_user;
global $asterisk_pass;
global $scripted;
global $freepbxip;
global $asteriskip;
global $runas_uid;
global $runas_gid;
global $asterisk_user;
global $asterisk_pass;
out("Creating new $filename");
if ($scripted == true) {
# Scripted install. Everything should have been set for us, don't ask questions.
out("Scripted install. Accepting command line params.");
$amp_conf["AMPDBUSER"] = $dbuser;
$amp_conf["AMPDBPASS"] = $dbpass;
$amp_conf["AMPDBHOST"] = $dbhost;
$amp_conf["AMPDBNAME"] = $dbname;
$amp_conf["AMPMGRUSER"] = $asterisk_user;
$amp_conf["AMPMGRPASS"] = $asterisk_pass;
$amp_conf["AMPWEBROOT"] = $webroot;
$amp_conf["AMPWEBADDRESS"] = $freepbxip;
$amp_conf["ASTMANAGERHOST"] = $asteriskip;
$amp_conf["AMPEXTENSIONS"] = "extensions";
$amp_conf["AMPBIN"] = $ampbin_dir;
$amp_conf["AMPSBIN"] = "$ampsbin_dir";
$amp_conf["AMPASTERISKWEBUSER"] = $runas_uid;
$amp_conf["AMPASTERISKWEBGROUP"] = $runas_gid;
$amp_conf["AMPASTERISKUSER"] = $runas_uid;
$amp_conf["AMPASTERISKGROUP"] = $runas_gid;
$amp_conf["AMPDEVUSER"] = $runas_uid;
$amp_conf["AMPDEVGROUP"] = $runas_gid;
write_amportal_conf($filename, $amp_conf);
outn(AMP_CONF." written");
return;
}
outn("Enter your USERNAME to connect to the '$dbname' database:\n [".($dbuser ? $dbuser : $asterisk_user) . "] ");
$key = trim(fgets(STDIN,1024));
if (preg_match('/^$/',$key)) {
$amp_conf["AMPDBUSER"] = ($dbuser ? $dbuser : $asterisk_user);
} else {
$amp_conf["AMPDBUSER"] = $key;
}
outn("Enter your PASSWORD to connect to the '$dbname' database:\n [".($dbpass ? $dbpass : $asterisk_pass)."] ");
$key = trim(fgets(STDIN,1024));
if (preg_match('/^$/',$key)) {
$amp_conf["AMPDBPASS"] = ($dbpass ? $dbpass : $asterisk_pass);
} else {
$amp_conf["AMPDBPASS"] = $key;
}
outn("Enter the hostname of the '$dbname' database:\n [".($dbhost ? $dbhost : "localhost")."] ");
$key = trim(fgets(STDIN,1024));
if (preg_match('/^$/',$key)) {
$amp_conf["AMPDBHOST"] = ($dbhost ? $dbhost : "localhost");
} else {
$amp_conf["AMPDBHOST"] = $key;
}
outn("Enter a USERNAME to connect to the Asterisk Manager interface:\n [admin] ");
$key = trim(fgets(STDIN,1024));
if (preg_match('/^$/',$key)) {
$amp_conf["AMPMGRUSER"] = "admin";
} else {
$amp_conf["AMPMGRUSER"] = $key;
}
$rs = substr(md5(rand()), 0, 5);
outn("Enter a PASSWORD to connect to the Asterisk Manager interface:\n [amp111] ");
$key = trim(fgets(STDIN,1024));
if (preg_match('/^$/',$key)) {
$amp_conf["AMPMGRPASS"] = "amp111";
} else {
$amp_conf["AMPMGRPASS"] = $key;
}
do {
out("Enter the path to use for your AMP web root:\n [$webroot] ");
$key = trim(fgets(STDIN,1024));
if (preg_match('/^$/',$key)) {
$amp_conf["AMPWEBROOT"] = "$webroot";
if (amp_mkdir($amp_conf["AMPWEBROOT"],"0755",true)){
out("Created ".$amp_conf["AMPWEBROOT"]);
break;
} else {
fatal("Cannot create ".$amp_conf["AMPWEBROOT"]."!");
}
} else {
$amp_conf["AMPWEBROOT"] = rtrim($key,'/');
//regression follows us all around freepbx #6159
$webroot = $amp_conf["AMPWEBROOT"];
if (is_dir($amp_conf["AMPWEBROOT"])) {
break;
} else if (amp_mkdir($amp_conf["AMPWEBROOT"],"0755",true)){
out("Created ".$amp_conf["AMPWEBROOT"]);
break;
} else {
fatal("Cannot create ".$amp_conf["AMPWEBROOT"]."!");
}
}
} while(1);
outn("Enter the IP ADDRESS or hostname used to access the AMP web-admin:\n [$freepbxip] ");
$key = trim(fgets(STDIN,1024));
if (preg_match('/^$/',$key)) {
$amp_conf["AMPWEBADDRESS"] = $freepbxip;
} else {
$amp_conf["AMPWEBADDRESS"] = $key;
}
outn("Use simple Extensions [extensions] admin or separate Devices and Users [deviceanduser]?\n [extensions] ");
$key = trim(fgets(STDIN,1024));
if (preg_match('/^$/',$key)) {
$amp_conf["AMPEXTENSIONS"] = "extensions";
} else {
$amp_conf["AMPEXTENSIONS"] = $key;
}
do {
out("Enter directory in which to store AMP executable scripts:\n [$ampbin_dir] ");
$key = trim(fgets(STDIN,1024));
if (preg_match('/^$/',$key)) {
$amp_conf["AMPBIN"] = $ampbin_dir;
} else {
$amp_conf["AMPBIN"] = rtrim($key,'/');
}
if (is_dir($amp_conf["AMPBIN"])) {
break;
} else if (amp_mkdir($amp_conf["AMPBIN"],"0755",true)){
out("Created ".$amp_conf["AMPBIN"]);
break;
} else {
fatal("Cannot create ".$amp_conf["AMPBIN"]."!");
}
} while(1);
do {
out("Enter directory in which to store super-user scripts:\n [$ampsbin_dir] ");
$key = trim(fgets(STDIN,1024));
if (preg_match('/^$/',$key)) {
$amp_conf["AMPSBIN"] = "$ampsbin_dir";
} else {
$amp_conf["AMPSBIN"] = rtrim($key,'/');
}
if (is_dir($amp_conf["AMPSBIN"])) {
break;
} else if (amp_mkdir($amp_conf["AMPSBIN"],"0755",true)){
out("Created ".$amp_conf["AMPSBIN"]);
break;
} else {
fatal("Cannot create ".$amp_conf["AMPSBIN"]."!");
}
} while(1);
// write amportal.conf
write_amportal_conf($filename, $amp_conf);
outn(AMP_CONF." written");
}
/** Set base of packaged modules to the versions packaged in the tarball since they are
* getting overwritten with the tarball from anything that may have been updated online.
*
*/
function set_base_version() {
global $dryrun;
// read modules list from MODULE_DIR
//
$included_modules = array();
$dir = opendir(MODULE_DIR);
while ($file = readdir($dir)) {
if ($file[0] != "." && $file[0] != "_" && is_dir(MODULE_DIR."/".$file)) {
$included_modules[] = $file;
}
}
closedir($dir);
foreach ($included_modules as $up_module) {
outn("Checking $up_module.. ");
if (!$dryrun) {
out(set_module_version($up_module));
} else {
out("Dry Run Not Updated");
}
}
}
/** Install all modules packaged with the install. We use the force flag because the
* the assumption is that the dependencies are met and the package is able to have
* the modules installed.
*/
function install_modules() {
global $dryrun;
global $amp_conf;
// read modules list from MODULE_DIR
//
$included_modules = array();
$dir = opendir(MODULE_DIR);
while ($file = readdir($dir)) {
if ($file[0] != "." && $file[0] != "_" && is_dir(MODULE_DIR."/".$file)) {
$included_modules[] = $file;
}
}
closedir($dir);
$output = array();
$keep_checking = true;
$num_modules = false;
while ($keep_checking && count($included_modules)) {
if ($num_modules === count($included_modules)) {
$keep_checking = false;
} else {
$num_modules = count($included_modules);
}
foreach ($included_modules as $id => $up_module) {
outn("Checking $up_module.. ");
if (!$dryrun) {
// if $keep_checking then check dependencies first and skip if not met
// otherwise we will install anyhow even if some dependencies are not met
// since it is included. (TODO: should we not?)
//
if ($keep_checking) {
exec($amp_conf['AMPBIN']."/module_admin --no-warnings checkdepends $up_module",$output,$retval);
unset($output);
if ($retval) {
out("dependencies pending");
continue;
}
}
// Framework modules cannot be enabled, only installed.
//
switch ($up_module) {
case 'framework':
case 'fw_ari':
system($amp_conf['AMPBIN']."/module_admin --no-warnings -f install $up_module");
out("installed");
break;
default:
system($amp_conf['AMPBIN']."/module_admin --no-warnings -f install $up_module");
system($amp_conf['AMPBIN']."/module_admin --no-warnings -f enable $up_module");
out("installed");
}
unset($included_modules[$id]);
} else {
out("Dry Run Not Installed");
}
}
}
}
/** Set the module version number to the packaged version and enable
* module must require not install.php or install.sql script
* this is primarily to package core and framework with FreePBX tarballs
*
*/
function set_module_version($module) {
global $db;
$module_dir = MODULE_DIR;
$file_path = $module_dir.$module."/module.xml";
if (file_exists($file_path)) {
// TODO: this is bad, there are other version tags (depends on) but this
// is equivalnet to what publish.pl does, so it expects this to be
// at the top.
//
$module_xml = file_get_contents($file_path);
if (preg_match('/<version>(.+)<\/version>/', $module_xml, $matches)) {
$version = $matches[1];
} else {
fatal("ERROR: $file_path found but no version information");
}
} else {
return "not packaged, no updating needed";
}
// If we didn't return above, then we found the package as part of the install
// tarball and want to update the version info since this might be overwriting
// an existing install that has a newer version.
//
$sql = "SELECT version FROM modules WHERE modulename = '$module'";
$result = $db->getCol($sql);
if(DB::IsError($result)) {
fatal("error accessing version table: ".$result->getMessage());
}
$sql = "";
if (count($result) == 0) {
// insert but disable as we have to first run install scripts which come later
$sql = "INSERT INTO modules (modulename, version, enabled) VALUES ('$module', '$version', 0)";
} else if ($result[0] != $version) {
if (version_compare_freepbx($version, $result[0], "gt")) {
// if new version is greater than old, then we disable the module and it will get enabled next when installed
//
$sql = "UPDATE modules SET version = '$version', enabled = 0 WHERE modulename = '$module'";
} else {
// if new version is equal to or less than old, then we leave it in the enable/disable state it was in but just
// reset the version number.
//
$sql = "UPDATE modules SET version = '$version' WHERE modulename = '$module'";
}
}
if ($sql) {
debug($sql);
$result = $db->query($sql);
if(DB::IsError($result)) {
fatal("error writing to version table: ".$result->getMessage());
}
return "updated to $version";
} else {
return "already at $version";
}
}
function installampimport_mysql_dump($file,$db=NULL) {
if(!isset($db)) {
global $db;
}
$dir = dirname(__FILE__);
if(file_exists($dir.'/SQL/'.$file)) {
// Temporary variable, used to store current query
$templine = '';
// Read in entire file
$lines = file($dir.'/SQL/'.$file);
// Loop through each line
foreach ($lines as $line) {
// Skip it if it's a comment
if (substr($line, 0, 2) == '--' || $line == '') {
continue;
}
// Add this line to the current segment
$templine .= $line;
// If it has a semicolon at the end, it's the end of the query
if (substr(trim($line), -1, 1) == ';') {
// Perform the query
$result = $db->query($templine);
if(DB::IsError($result)) {
fatal("Error performing query: ". $templine . " Message:".$result->getMessage());
}
// Reset temp variable to empty
$templine = '';
}
}
}
}
/********************************************************************************************************************/
//
// TODO: will this work, basically using bootstrap once installed if using to upgrade, etc?
/*
$bootstrap_settings['skip_astman']) = true;
$bootstrap_settings['freepbx_auth'] = false;
$restrict_mods = true;
if (@include_once(getenv('FREEPBX_CONF') ? getenv('FREEPBX_CONF') : '/etc/freepbx.conf')) {
out(_("FreePBX already installed, bootstrapping"));
} else if (@include_once('/etc/asterisk/freepbx.conf')) {
out(_("FreePBX already installed, bootstrapping"));
} else {
// all the initialization here instead
}
*/
// **** Make sure we have STDIN etc
// from ben-php dot net at efros dot com at php.net/install.unix.commandline
if (version_compare(phpversion(),'4.3.0','<') || !defined("STDIN")) {
define('STDIN',fopen("php://stdin","r"));
define('STDOUT',fopen("php://stdout","r"));
define('STDERR',fopen("php://stderr","r"));
register_shutdown_function( create_function( '' , 'fclose(STDIN); fclose(STDOUT); fclose(STDERR); return true;' ) );
}
// **** Make sure we have PEAR's DB.php, and include it
outn("Checking for PEAR DB..");
if (! @ include('DB.php')) {
out("FAILED");
fatal("PEAR must be installed (requires DB.php). Include path: ".ini_get("include_path"));
}
out("OK");
// **** Make sure we have PEAR's GetOpts.php, and include it
outn("Checking for PEAR Console::Getopt..");
if (! @ include("Console/Getopt.php")) {
out("FAILED");
fatal("PEAR must be installed (requires Console/Getopt.php). Include path: ".ini_get("include_path"));
}
out("OK");
// **** Parse out command-line options
$shortopts = "h?u:p:";
$longopts = array(
"asteriskip=",
"asteriskpass=",
"asteriskuser=",
"bin=",
"cgibin=",
"cleaninstall",
"dbhost=",
"dbname=",
"debug",
"dev-links",
"dry-run",
"engine=",
"fopwebroot=",
"force-overwrite",
"force-version=",
"freepbxip=",
"gid=",
"help",
"install-moh",
"installdb",
"make-links-devel",
"no-files",
"password=",
"sbin=",
"scripted",
"set-freepbx-settings",
"skip-module-install",
"uid=",
"uninstall",
"update-links",
"username=",
"webroot=",
);
$args = Console_Getopt::getopt(Console_Getopt::readPHPArgv(), $shortopts, $longopts);
if (is_object($args)) {
// assume it's PEAR_ERROR
out($args->message);
exit(255);
}
$debug = false;
$dryrun = false;
$install_files = true;
$override_astvers = false;
$scripted = false;
$install_moh = false;
$make_links = false;
$update_links = false;
$module_install = true;
$set_freepbx_settings = false;
$installamp_clean = false;
$uninstallamp = false;
$installdb = false;
//initialize variables to avoid php notices
$dbhost = null;
$dbname = 'asterisk';
$new_username = null;
$new_password = null;
$runas_uid = "asterisk";
$runas_gid = "asterisk";
$freepbxip = "192.168.1.1";
$asteriskip = "localhost";
foreach ($args[0] as $arg) {
switch ($arg[0]) {
case "--help": case "h": case "?":
showHelp();
exit(10);
break;
case "--dry-run":
out("Dry-run only, nothing will be changed");
$dryrun = true;
break;
case "--debug":
$debug = true;
debug("Debug mode enabled");
break;
case "--username": case "u":
out("Using username: ".$arg[1]);
$new_username = $arg[1];
break;
case "--password": case "p":
out("Using password: ".str_repeat("*",strlen($arg[1])));
$new_password = $arg[1];
break;
case "--force-version":
$version = $arg[1];
out("Forcing upgrade from version ".$version);
break;
case "--dbhost":
$dbhost = $arg[1];
out("Using remote database server at ".$dbhost);
break;
case "--dbname":
$dbname = $arg[1];
out("Using database ".$dbname);
break;
case "--no-files":
$install_files = false;
out("Running upgrade only, without installing files.");
break;
case "--force-overwrite":
$check_md5s = false;
out("Overwriting all files including modified ones.");
break;
case "--engine":
if ($arg[1] != 'asterisk') {
fatal('Currently only "asterisk" is supported as a PBX engine');
}
$pbx_engine = $arg[1];
break;
case "--install-moh":
$install_moh = true;
break;
case "--set-freepbx-settings":
$set_freepbx_settings = true;
break;
case "--fopwebroot":
case "--install-fop":
out("install-fop: depricated setting, does nothing.");
break;
case "--update-links":
$update_links = true;
//Note: there is NO break here on purpose!
case "--make-links-devel":
case "--dev-links":
$make_links = true;
break;
case "--skip-module-install":
$module_install = false;
break;
case "--webroot":
$webroot = $arg[1];
out("Using Webroot at ".$webroot);
break;
case "--cgibin":
$cgibin = $arg[1];
out("Using CGI-BIN at ".$cgibin);
break;
case "--bin":
$ampbin_dir = $arg[1];
out("Using bin at ".$ampbin_dir);
break;
case "--sbin":
$ampsbin_dir = $arg[1];
out("Using sbin at ".$ampsbin_dir);
break;
case "--asteriskuser":
$asterisk_user = $arg[1];
out("Using Asterisk user ".$asterisk_user);
break;
case "--asteriskpass":
$asterisk_pass = $arg[1];
out("Using asteriskpass ".str_repeat("*",strlen($arg[1])));
break;
case "--uid":
$runas_uid = $arg[1];
out("Setting ownership (user) to ".$runas_uid);
break;
case "--gid":
$runas_gid = $arg[1];
out("Setting ownership (group) to ".$runas_gid);
break;
case "--scripted":
$scripted = true;
out("Unattended installation. No questions will be asked");
break;
case "--freepbxip":
$freepbxip = $arg[1];
out("Setting IP address of web interface to $freepbxip");
break;
case "--asteriskip":
$asteriskip = $arg[1];
out("Setting IP address of asterisk to $asteriskip");
break;
case "--cleaninstall":
$installamp_clean = true;
break;
case "--uninstall":
$uninstallamp = true;
break;
case "--installdb":
$installdb = true;
break;
}
}
if($installamp_clean || $uninstallamp) {
global $webroot;
global $asterisk_user;
global $asterisk_pass;
$oldampconf = array();
if(file_exists('/etc/amportal.conf')) {
$amportalconf = file_get_contents('/etc/amportal.conf');
if(preg_match('/AMPWEBROOT=(.*)/',$amportalconf,$matches)) {
$oldampconf['AMPWEBROOT'] = $matches[1];
}
if(preg_match('/AMPDBENGINE=(.*)/',$amportalconf,$matches)) {
$oldampconf['AMPDBENGINE'] = $matches[1];
}
if(preg_match('/AMPDBUSER=(.*)/',$amportalconf,$matches)) {
$oldampconf['AMPDBUSER'] = $matches[1];
}
if(preg_match('/AMPDBPASS=(.*)/',$amportalconf,$matches)) {
$oldampconf['AMPDBPASS'] = $matches[1];
}
if(preg_match('/AMPDBNAME=(.*)/',$amportalconf,$matches)) {
$oldampconf['AMPDBNAME'] = $matches[1];
}
if(preg_match('/AMPDBHOST=(.*)/',$amportalconf,$matches)) {
$oldampconf['AMPDBHOST'] = $matches[1];
}
if(preg_match('/AMPBIN=(.*)/',$amportalconf,$matches)) {
$oldampconf['AMPBIN'] = $matches[1];
}
outn("Remove /etc/amportal.conf?:\n [y] ");
$key = trim(fgets(STDIN,1024));
switch($key) {
case "n":
case "no":
break;
case "y":
case "yes":
default:
outn("Removing /etc/amportal.conf.....");
unlink('/etc/amportal.conf');
out("Done!");
break;
}
}
if(file_exists('/etc/freepbx.conf')) {
outn("Remove /etc/freepbx.conf?:\n [y] ");
$key = trim(fgets(STDIN,1024));
switch($key) {
case "n":
case "no":
break;
case "y":
case "yes":
default:
outn("Removing /etc/freepbx.conf.....");
unlink('/etc/freepbx.conf');
out("Done!");
break;
}
}
if(file_exists('/usr/local/sbin/amportal')) {
outn("Remove /usr/local/sbin/amportal?:\n [y] ");
$key = trim(fgets(STDIN,1024));
switch($key) {
case "n":
case "no":
break;
case "y":
case "yes":
default:
outn("Removing /usr/local/sbin/amportal.....");
unlink('/usr/local/sbin/amportal');
out("Done!");
break;
}
}
if(!empty($oldampconf['AMPBIN']) && file_exists($oldampconf['AMPBIN'])) {
outn("Remove All FreePBX Tools in ".$oldampconf['AMPBIN']."?:\n [y] ");
$key = trim(fgets(STDIN,1024));
$files = array("archive_recordings","freepbx_engine", "gen_amp_conf.php", "libfreepbx.confgen.php", "retrieve_conf", "freepbx-cron-scheduler.php", "freepbx_setting", "generate_hints.php", "module_admin", "retrieve_parse_amportal_conf.pl");
foreach (glob($oldampconf['AMPBIN']."/*") as $filename) {
if(is_link($filename)) {
if(preg_match('/'.preg_quote($oldampconf['AMPWEBROOT'],'/').'/i',readlink($filename))) {
$files[] = basename($filename);
}
}
}
switch($key) {
case "n":
case "no":
break;
case "y":
case "yes":
default:
foreach($files as $file) {
if(file_exists($oldampconf['AMPBIN'].'/'.$file)) {
outn("Removing ".$oldampconf['AMPBIN']."/".$file.".....");
unlink($oldampconf['AMPBIN'].'/'.$file);
out("Done!");
}
}
break;
}
}
if(!empty($oldampconf) && isset($oldampconf['AMPWEBROOT']) && file_exists($oldampconf['AMPWEBROOT'])) {
outn("Remove ".$oldampconf['AMPWEBROOT']."?:\n [y] ");
$key = trim(fgets(STDIN,1024));
switch($key) {
case "n":
case "no":
break;
case "y":
case "yes":
default:
outn("Removing ".$oldampconf['AMPWEBROOT']."......");
exec('rm -Rf '.$oldampconf['AMPWEBROOT']);
out("Done!");
break;
}
}
if(!$uninstallamp && !empty($oldampconf) && $oldampconf['AMPDBENGINE'] == 'mysql') {
outn("Reinitalize & Reinstall Freepbx's Asterisk Database?:\n [y] ");
$key = trim(fgets(STDIN,1024));
switch($key) {
case "n":
case "no":
break;
case "y":
case "yes":
default:
outn("Erasing FreePBX Asterisk Database...");
$datasource = 'mysql://'.$oldampconf["AMPDBUSER"].':'.$oldampconf["AMPDBPASS"].'@'.$oldampconf["AMPDBHOST"].'/'.$oldampconf["AMPDBNAME"];
$cleandb = DB::connect($datasource); // attempt connection
if(DB::IsError($cleandb)) {
out("FAILED");
fatal("Try not selecting 'y' on Reinitalize & Reinstall Freepbx's Asterisk Database");
} else {
$sql = "SHOW TABLES FROM ".$oldampconf['AMPDBNAME'];
$tables = $cleandb->getAll($sql);
foreach($tables as $list) {
$sql = "DROP TABLE ".$oldampconf['AMPDBNAME'].".".$list[0];
$cleandb->query($sql);
}
out("Done");
outn("Reinstalling FreePBX Asterisk DB Structure...");
installampimport_mysql_dump('newinstall.sql',$cleandb);
out("Done");
}
break;
}
outn("Reinitalize & Reinstall Freepbx's CDR Database?:\n [y] ");
$key = trim(fgets(STDIN,1024));
switch($key) {
case "n":
case "no":
break;
case "y":