root/freepbx/tags/2.5.0rc3/install_amp

Revision 6601, 35.3 kB (checked in by ethans, 5 years ago)

Adds FOPDISABLE option to amportal.conf. This will disable FOP on the web interfaces as well as server operations in retrieve_conf and freepbx_engine

References: 3170

  • Property svn:mime-type set to text/plain
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1 #!/usr/bin/php -q
2 <?php
3
4 require_once ("libfreepbx.install.php");
5
6 # constants
7 define("AMP_CONF", "/etc/amportal.conf");
8 define("ASTERISK_CONF", "/etc/asterisk/asterisk.conf");
9 define("UPGRADE_DIR", dirname(__FILE__)."/upgrades");
10 define("MODULE_DIR", dirname(__FILE__)."/amp_conf/htdocs/admin/modules/");
11
12 # semi constants
13 $webroot  = "/var/www/html";
14 $fopwebroot = "";       // if blank, will use $webroot/panel
15 $ampsbin_dir = "/usr/local/sbin"; // default if not set
16 $ampbin_dir = "/var/lib/asterisk/bin";
17 $asterisk_user  = "asteriskuser";
18 $asterisk_pass  = "amp109";
19
20
21 /*
22 freepbx versions
23     '1.10.005',
24     '1.10.006',
25     '1.10.007beta1',
26     '1.10.007beta2',
27     '1.10.007',
28     '1.10.007a',
29     '1.10.008beta1',
30     '1.10.008beta2',
31     '1.10.008beta3',
32     '1.10.008',
33     '1.10.009beta1',
34     '1.10.009beta2',
35     '1.10.009',
36     '1.10.010beta1',
37     '1.10.010',
38     '2.0beta1',
39     '2.0beta2',
40     '2.0beta3',
41     '2.0beta4',
42     '2.0beta5',
43     '2.0.0',
44     '2.0.1',
45     '2.1beta1',
46     '2.1beta2',
47     '2.1beta3',
48     '2.1.0',
49     '2.1.1',
50     '2.1.2',
51     '2.1.3',
52     '2.2.0beta1',
53     '2.2.0beta2',
54     '2.2.0beta3',
55     '2.2.0rc1',
56     '2.2.0rc2',
57     '2.2.0rc3',
58     '2.2.0',
59     '2.2.1',
60     '2.2.2',
61     '2.2.3',
62     '2.3.0beta1',
63 */
64
65 /********************************************************************************************************************/
66
67 function out($text) {
68   echo $text."\n";
69 }
70
71 function outn($text) {
72   echo $text;
73 }
74
75 function error($text) {
76   echo "[ERROR] ".$text."\n";
77 }
78
79 function fatal($text) {
80   echo "[FATAL] ".$text."\n";
81   exit(1);
82 }
83
84 function debug($text) {
85   global $debug;
86  
87   if ($debug) echo "[DEBUG-preDB] ".$text."\n";
88 }
89
90 if (!function_exists('version_compare_freepbx')) {
91   /* verison_compare that works with freePBX version numbers
92   */
93   function version_compare_freepbx($version1, $version2, $op = null) {
94           $version1 = str_replace("rc","RC", strtolower($version1));
95           $version2 = str_replace("rc","RC", strtolower($version2));
96       if (!is_null($op)) {
97         return version_compare($version1, $version2, $op);
98       } else {
99         return version_compare($version1, $version2);
100       }
101   }
102 }
103
104 function showHelp() {
105   out("Optional parameters:");
106   out("  --help, -h, -?           Show this help");
107   out("  --dbhost <ip address>    Use a remote database server");
108   out("  --dbname databasename    Use database name specified, instead of 'asterisk'");
109   out("  --username <user>        Use <user> to connect to db and write config");
110   out("  --password <pass>        Use <pass> to connect to db and write config");
111   out("  --fopwebroot <path>      Web path where fop will be installed");
112   out("  --webroot <path>         Web root where freepbx will be installed");
113   out("  --cgibin <path>          Path where cgi-bin's lives");
114   out("  --bin <path>             Path of asterisk binaries");
115   out("  --sbin <path>            Path of system admin binaries");
116   out("  --asteriskuser <user>    Asterisk Manager username");
117   out("  --asteriskpass <pass>    Asterisk Manager password");
118   out("  --systemconfig <path>    System config files");
119   out("  --debug                  Enable debug output");
120   out("  --dry-run                Don't actually do anything");
121   out("  --force-version <ver>    Force upgrade from version <ver>");
122   out("  --skip-module-install    Don't run install scripts for packaged modules, the files are still loaded.");
123   out("                           In a development environment you may not want the install scripts run.");
124   out("  --no-files               Just run updates without installing files");
125   out("  --install-moh            Install default music-on-hold files (normally doesn't, unless ");
126   out("                           it's a new installation)");
127   out("  --install-fop=false      Don't install FOP and don't display it anywhere on the interface");
128   //out("  --make-links-devel       Make links to files in the source directory instead of copying");
129   //out("                           (intended for developers only)");
130   out("  --my-svn-is-correct      Ignore Asterisk version, assume it is correct");
131   out("  --engine <name>          Use the specified PBX Engine ('asterisk', 'asterisk14' or 'openpbx')");
132 }
133
134 function install_parse_amportal_conf($filename) {
135   $file = file($filename);
136   foreach ($file as $line) {
137     if (preg_match("/^\s*([a-zA-Z0-9]+)\s*=\s*(.*)\s*([;#].*)?/",$line,$matches)) {
138       $conf[ $matches[1] ] = $matches[2];
139     }
140   }
141
142   // use same defaults as function.inc.php
143   if ( !isset($conf["AMPDBENGINE"]) || ($conf["AMPDBENGINE"] == "")) {
144     $conf["AMPDBENGINE"] = "mysql";
145   }
146  
147   if ( !isset($conf["AMPDBNAME"]) || ($conf["AMPDBNAME"] == "")) {
148     $conf["AMPDBNAME"] = "asterisk";
149   }
150  
151   if ( !isset($conf["AMPENGINE"]) || ($conf["AMPENGINE"] == "")) {
152     $conf["AMPENGINE"] = "asterisk";
153   }
154
155   return $conf;
156 }
157
158 function install_parse_asterisk_conf($filename) {
159   $file = file($filename);
160   foreach ($file as $line) {
161     if (preg_match("/^\s*([a-zA-Z0-9]+)\s* => \s*(.*)\s*([;#].*)?/",$line,$matches)) {
162       $conf[ $matches[1] ] = $matches[2];
163     }
164   }
165   // Now set defaults if not set (although asterisk should fail if not set but at least
166   // it will get the setup somewhat right
167   //
168   if (!isset($asterisk_conf['astetcdir']))    { $asterisk_conf['astetcdir']    = "/etc/asterisk"; }
169   if (!isset($asterisk_conf['astmoddir']))    { $asterisk_conf['astmoddir']    = "/usr/lib/asterisk/modules"; }
170   if (!isset($asterisk_conf['astvarlibdir'])) { $asterisk_conf['astvarlibdir'] = "/var/lib/asterisk"; }
171   if (!isset($asterisk_conf['astagidir']))    { $asterisk_conf['astagidir']    = "/var/lib/asterisk/agi-bin"; }
172   if (!isset($asterisk_conf['astspooldir']))  { $asterisk_conf['astspooldir']  = "/var/spool/asterisk"; }
173   if (!isset($asterisk_conf['astrundir']))    { $asterisk_conf['astrundir']    = "/var/run/asterisk"; }
174   if (!isset($asterisk_conf['astlogdir']))    { $asterisk_conf['astlogdir']    = "/var/log/asterisk"; }
175
176   return $conf;
177 }
178
179 function write_amportal_conf($filename, $conf) {
180   $file = file($filename);
181   // parse through the file
182   foreach (array_keys($file) as $key) {
183     if (preg_match("/^\s*([a-zA-Z0-9]+)\s*=\s*(.*)\s*([;#].*)?/",$file[$key],$matches)) {
184       // this is an option=value line
185       if (isset($conf[ $matches[1] ])) {
186         // rewrite the line, if we have this in $conf
187         $file[$key] = $matches[1]."=".$conf[ $matches[1] ]."\n";
188         // unset it so we know what's new
189         unset($conf[ $matches[1] ]);
190       }
191     }
192   }
193  
194   // add new entries
195   foreach ($conf as $key=>$val) {
196     $file[] = $key."=".$val."\n";
197   }
198  
199   // write the file
200   if (!$fd = fopen($filename, "w")) {
201     fatal("Could not open ".$filename." for writing");
202   }
203   fwrite($fd, implode("",$file));
204   fclose($fd);
205 }
206
207 function ask_overwrite($file1, $file2) {
208   global $check_md5s;
209   do {
210     out($file2." has been changed from the original version.");
211     outn("Overwrite (y=yes/a=all/n=no/d=diff/s=shell/x=exit)? ");
212     $key = fgets(STDIN,1024);
213     switch (strtolower($key[0])) {
214       case "y": return true;
215       case "a": $check_md5s=false; return true;
216       case "n": return false;
217       case "d":
218         out("");
219         // w = ignore whitespace, u = unified
220         passthru("diff -wu ".escapeshellarg($file2)." ".escapeshellarg($file1));
221       break;
222       case "s":
223         if (function_exists("pcntl_fork")) {
224           out("");
225           $shell = (isset($_ENV["SHELL"]) ? $_ENV["SHELL"] : "/bin/bash");
226           out("Dropping to shell. Type 'exit' to return");
227           out("-> Original file:  ".$file2);
228           out("-> New file:       ".$file1);
229          
230           $pid = pcntl_fork();
231           if ($pid == -1) {
232             out("[ERROR] cannot fork");
233           } else if ($pid) {
234             // parent
235             pcntl_waitpid($pid, $status);
236             // we wait till the child exits/dies/whatever
237           } else {
238             pcntl_exec($shell, array(), $_ENV);
239           }
240          
241           out("Returned from shell");
242         } else {
243           out("[ERROR] PHP not built with process control (--enable-pcntl) support: cannot spawn shell");
244         }
245        
246       break;
247       case "x":
248         out("-> Original file:  ".$file2);
249         out("-> New file:       ".$file1);
250         out("Exiting install program.");
251         exit(1);
252       break;
253     }
254     out("");
255   } while(1);
256 }
257
258 /** Write AMP-generated configuration files
259  */
260 function generate_configs() {
261   global $amp_conf;
262   global $dryrun;
263   global $debug;
264  
265   out("Generating Configurations.conf, (if Asterisk is not running, you will get an error");
266   out("In case of error, start Asterisk and hit the red bar in the GUI to generate the Configuraions.conf files");
267   if (!$dryrun)
268     // added --run-install to make it work like it has been working since retrieve_conf changed to not run module install scripts by default
269
270     //
271     // TODO: Should check if Asterisk is running and/or try to start it.
272     //
273     passthru("su - asterisk -c \"".trim($amp_conf["AMPBIN"])."/retrieve_conf --run-install ".($debug ? ' --debug' : '').'"');
274 }
275
276
277 /** Set reload flag for AMP admin
278  */
279 function install_needreload() {
280   global $db;
281   $sql = "UPDATE admin SET value = 'true' WHERE variable = 'need_reload'";
282   $result = $db->query($sql);
283   if(DB::IsError($result)) {     
284     die($result->getMessage());
285   }
286 }
287
288
289 /** Collect AMP settings
290  */
291 function collect_settings($filename, $dbhost = '', $dbuser = '', $dbpass = '', $dbname = 'asterisk') {
292   global $webroot;
293   global $fopwebroot;
294   global $ampsbin_dir;
295   global $ampbin_dir;
296   global $asterisk_user;
297   global $asterisk_pass;
298
299   out("Creating new $filename");
300  
301   outn("Enter your USERNAME to connect to the '$dbname' database:\n [".($dbuser ? $dbuser : $asterisk_user) . "] ");
302   $key = trim(fgets(STDIN,1024));
303   if (preg_match('/^$/',$key))
304     $amp_conf["AMPDBUSER"] = ($dbuser ? $dbuser : $asterisk_user);
305   else
306     $amp_conf["AMPDBUSER"] = $key;
307  
308   outn("Enter your PASSWORD to connect to the '$dbname' database:\n [".($dbpass ? $dbpass : $asterisk_pass)."] ");
309   $key = trim(fgets(STDIN,1024));
310   if (preg_match('/^$/',$key))
311     $amp_conf["AMPDBPASS"] = ($dbpass ? $dbpass : $asterisk_pass);
312   else
313     $amp_conf["AMPDBPASS"] = $key;
314  
315   outn("Enter the hostname of the '$dbname' database:\n [".($dbhost ? $dbhost : "localhost")."] ");
316   $key = trim(fgets(STDIN,1024));
317   if (preg_match('/^$/',$key))
318     $amp_conf["AMPDBHOST"] = ($dbhost ? $dbhost : "localhost");
319   else
320     $amp_conf["AMPDBHOST"] = $key;
321  
322   outn("Enter a USERNAME to connect to the Asterisk Manager interface:\n [admin] ");
323   $key = trim(fgets(STDIN,1024));
324   if (preg_match('/^$/',$key)) $amp_conf["AMPMGRUSER"] = "admin";
325   else $amp_conf["AMPMGRUSER"] = $key;
326  
327   outn("Enter a PASSWORD to connect to the Asterisk Manager interface:\n [amp111] ");
328   $key = trim(fgets(STDIN,1024));
329   if (preg_match('/^$/',$key)) $amp_conf["AMPMGRPASS"] = "amp111";
330   else $amp_conf["AMPMGRPASS"] = $key;
331  
332   do {
333     out("Enter the path to use for your AMP web root:\n [$webroot] ");
334     $key = trim(fgets(STDIN,1024));
335     if (preg_match('/^$/',$key))
336       $amp_conf["AMPWEBROOT"] = "$webroot";
337     else
338       $amp_conf["AMPWEBROOT"] = rtrim($key,'/');
339
340     if (is_dir($amp_conf["AMPWEBROOT"])) {
341       break;
342     } else if (amp_mkdir($amp_conf["AMPWEBROOT"],"0755",true)){
343       out("Created ".$amp_conf["AMPWEBROOT"]);
344       break;
345     } else {
346       fatal("Cannot create ".$amp_conf["AMPWEBROOT"]."!");
347     }
348   } while(1);
349  
350   // Really no need to ask, is there.
351   if (empty($fopwebroot))
352     $amp_conf["FOPWEBROOT"] = $amp_conf["AMPWEBROOT"]."/panel";
353   else
354     $amp_conf["FOPWEBROOT"] = $fopwebroot;
355  
356   outn("Enter the IP ADDRESS or hostname used to access the AMP web-admin:\n [xx.xx.xx.xx] ");
357   $key = trim(fgets(STDIN,1024));
358   if (preg_match('/^$/',$key)) $amp_conf["AMPWEBADDRESS"] = "xx.xx.xx.xx";
359   else $amp_conf["AMPWEBADDRESS"] = $key;
360  
361   outn("Enter a PASSWORD to perform call transfers with the Flash Operator Panel:\n [passw0rd] ");
362   $key = trim(fgets(STDIN,1024));
363   if (preg_match('/^$/',$key)) $amp_conf["FOPPASSWORD"] = "passw0rd";
364   else $amp_conf["FOPPASSWORD"] = $key;
365  
366   outn("Use simple Extensions [extensions] admin or separate Devices and Users [deviceanduser]?\n [extensions] ");
367   $key = trim(fgets(STDIN,1024));
368   if (preg_match('/^$/',$key)) $amp_conf["AMPEXTENSIONS"] = "extensions";
369   else $amp_conf["AMPEXTENSIONS"] = $key;
370  
371   do {
372     out("Enter directory in which to store AMP executable scripts:\n [$ampbin_dir] ");
373     $key = trim(fgets(STDIN,1024));
374     if (preg_match('/^$/',$key))
375       $amp_conf["AMPBIN"] = $ampbin_dir;
376     else
377       $amp_conf["AMPBIN"] = rtrim($key,'/');
378
379     if (is_dir($amp_conf["AMPBIN"])) {
380       break;
381     } else if (amp_mkdir($amp_conf["AMPBIN"],"0755",true)){
382       out("Created ".$amp_conf["AMPBIN"]);
383       break;
384     } else {
385       fatal("Cannot create ".$amp_conf["AMPBIN"]."!");
386     }
387   } while(1);
388  
389   do {
390     out("Enter directory in which to store super-user scripts:\n [$ampsbin_dir] ");
391     $key = trim(fgets(STDIN,1024));
392     if (preg_match('/^$/',$key))
393       $amp_conf["AMPSBIN"] = "$ampsbin_dir";
394     else
395       $amp_conf["AMPSBIN"] = rtrim($key,'/');
396
397     if (is_dir($amp_conf["AMPSBIN"])) {
398       break;
399     } else if (amp_mkdir($amp_conf["AMPSBIN"],"0755",true)){
400       out("Created ".$amp_conf["AMPSBIN"]);
401       break;
402     } else {
403       fatal("Cannot create ".$amp_conf["AMPSBIN"]."!");
404     }
405   } while(1);
406  
407   // write amportal.conf
408   write_amportal_conf($filename, $amp_conf);
409   outn(AMP_CONF." written");
410 }
411
412 /** Set base of packaged modules to the versions packaged in the tarball since they are
413  *  getting overwritten with the tarball from anything that may have been updated online.
414  *
415  */
416 function set_base_version() {
417   global $dryrun;
418
419   // read modules list from MODULE_DIR
420   //
421   $included_modules = array();
422   $dir = opendir(MODULE_DIR);
423   while ($file = readdir($dir)) {
424     if ($file[0] != "." && $file[0] != "_" && is_dir(MODULE_DIR."/".$file)) {
425       $included_modules[] = $file;
426     }
427   }
428   closedir($dir);
429
430   foreach ($included_modules as $up_module) {
431     outn("Checking $up_module.. ");
432     if (!$dryrun) {
433       out(set_module_version($up_module));
434     } else {
435       out("Dry Run Not Updated");
436     }
437   }
438 }
439
440 /** Install all modules packaged with the install. We use the force flag because the
441  *  the assumption is that the dependencies are met and the package is able to have
442  *  the modules installed.
443  */
444 function install_modules() {
445   global $dryrun;
446   global $amp_conf;
447
448   // read modules list from MODULE_DIR
449   //
450   $included_modules = array();
451   $dir = opendir(MODULE_DIR);
452   while ($file = readdir($dir)) {
453     if ($file[0] != "." && $file[0] != "_" && is_dir(MODULE_DIR."/".$file)) {
454       $included_modules[] = $file;
455     }
456   }
457   closedir($dir);
458
459   foreach ($included_modules as $up_module) {
460     outn("Checking $up_module.. ");
461     if (!$dryrun) {
462       // special case framework, it should not be installed just enabled.
463       //
464       if ($up_module != 'framework') {
465         system($amp_conf['AMPBIN']."/module_admin --no-warnings -f install $up_module");
466         system($amp_conf['AMPBIN']."/module_admin --no-warnings -f enable $up_module");
467         out("installed");
468       } else {
469         system($amp_conf['AMPBIN']."/module_admin --no-warnings -f enable $up_module");
470         out("enabled");
471       }
472     } else {
473       out("Dry Run Not Installed");
474     }
475   }
476 }
477
478 /** Set the module version number to the packaged version and enable
479  *  module must require not install.php or install.sql script
480  *  this is primarily to package core and framework with freepbx tarballs
481  *
482  */
483 function set_module_version($module) {
484   global $db;
485
486   $module_dir = MODULE_DIR;
487   $file_path = $module_dir.$module."/module.xml";
488   if (file_exists($file_path)) {
489     // TODO: this is bad, there are other version tags (depends on) but this
490     //       is equivalnet to what publish.pl does, so it expects this to be
491     //       at the top.
492     //
493     $module_xml = file_get_contents($file_path);
494     if (preg_match('/<version>(.+)<\/version>/', $module_xml, $matches)) {
495       $version = $matches[1];
496     } else {
497       fatal("ERROR: $file_path found but no verison information");
498     }
499   } else {
500     return  "not packaged, no updating needed";
501   }
502
503   // If we didn't return above, then we found the package as part of the install
504   // tarball and want to update the version info since this might be overwriting
505   // an existing install that has a newever version.
506   //
507   $sql = "SELECT version FROM modules WHERE modulename = '$module'";
508   $result = $db->getCol($sql);
509   if(DB::IsError($result)) {     
510     fatal("error accessing version table: ".$result->getMessage());
511   }
512   $sql = "";
513   if (count($result) == 0) {
514     // insert but disable as we have to first run install scripts which come later
515     $sql = "INSERT INTO modules (modulename, version, enabled) VALUES ('$module', '$version', 0)";
516   } else if ($result[0] != $version) {
517     if (version_compare_freepbx($version, $result[0], "gt")) {
518       // if new verison is greater than old, then we disable the module and it will get enabled next when installed
519       //
520       $sql = "UPDATE modules SET version = '$version', enabled = 0 WHERE modulename = '$module'";
521     } else {
522       // if new version is equal to or less than old, then we leave it in the enable/disable state it was in but just
523       // reset the version number.
524       //
525       $sql = "UPDATE modules SET version = '$version' WHERE modulename = '$module'";
526     }
527   }
528   if ($sql) {
529     debug($sql);
530     $result = $db->query($sql);
531     if(DB::IsError($result)) {     
532       fatal("error writing to version table: ".$result->getMessage());
533     }
534     return "updated to $version";
535   } else {
536     return "already at $version";
537   }
538 }
539
540 /********************************************************************************************************************/
541
542 // **** Make sure we have STDIN etc
543
544 // from  ben-php dot net at efros dot com   at  php.net/install.unix.commandline
545 if (version_compare(phpversion(),'4.3.0','<') || !defined("STDIN")) {
546   define('STDIN',fopen("php://stdin","r"));
547   define('STDOUT',fopen("php://stdout","r"));
548   define('STDERR',fopen("php://stderr","r"));
549   register_shutdown_function( create_function( '' , 'fclose(STDIN); fclose(STDOUT); fclose(STDERR); return true;' ) );
550 }
551    
552 // **** Make sure we have PEAR's DB.php, and include it
553
554 outn("Checking for PEAR DB..");
555 if (! @ include('DB.php')) {
556   out("FAILED");
557   fatal("PEAR must be installed (requires DB.php). Include path: ".ini_get("include_path"));
558 }
559 out("OK");
560
561 // **** Make sure we have PEAR's GetOpts.php, and include it
562
563 outn("Checking for PEAR Console::Getopt..");
564 if (! @ include("Console/Getopt.php")) {
565   out("FAILED");
566   fatal("PEAR must be installed (requires Console/Getopt.php). Include path: ".ini_get("include_path"));
567 }
568 out("OK");
569
570 // **** Parse out command-line options
571 $shortopts = "h?u:p:";
572 $longopts = array("help","debug","dry-run","username=","password=","force-version=","dbhost=","no-files","dbname=","my-svn-is-correct","engine=","webroot=","install-moh","install-fop","make-links-devel","skip-module-install");
573
574 $args = Console_Getopt::getopt(Console_Getopt::readPHPArgv(), $shortopts, $longopts);
575 if (is_object($args)) {
576   // assume it's PEAR_ERROR
577   out($args->message);
578   exit(255);
579 }
580
581 $debug = false;
582 $dryrun = false;
583 $install_files = true;
584 $override_astvers = false;
585
586 $install_moh = false;
587 $install_fop = true;
588 $make_links = false;
589 $module_install = true;
590
591 //initialize variables to avoid php notices
592 $dbhost = null;
593 $dbname = null;
594 $new_username = null;
595 $new_password = null;
596
597 foreach ($args[0] as $arg) {
598   switch ($arg[0]) {
599     case "--help": case "h": case "?":
600       showHelp();
601       exit(10);
602     break;
603     case "--dry-run":
604       out("Dry-run only, nothing will be changed");
605       $dryrun = true;
606     break;
607     case "--debug":
608       $debug = true;
609       debug("Debug mode enabled");
610     break;
611     case "--username": case "u":
612       out("Using username: ".$arg[1]);
613       $new_username = $arg[1];
614     break;
615     case "--password": case "p":
616       out("Using password: ".str_repeat("*",strlen($arg[1])));
617       $new_password = $arg[1];
618     break;
619     case "--force-version":
620       $version = $arg[1];
621       out("Forcing upgrade from version ".$version);
622     break;
623     case "--dbhost":
624       $dbhost = $arg[1];
625       out("Using remote database server at ".$dbhost);
626     break;
627     case "--dbname":
628       $dbname = $arg[1];
629       out("Using database ".$dbname);
630     break;
631     case "--no-files":
632       $install_files = false;
633       out("Running upgrade only, without installing files.");
634     break;
635     case "--my-svn-is-correct":
636       $override_astvers = true;
637     break;
638     case "--engine":
639       if ($arg[1] != 'asterisk' && $arg[1] != 'asterisk14' && $arg[1] != 'openpbx') {
640         fatal('Currently only "asterisk", "asterisk14" or "openpbx" are supported as a PBX engine');
641       }
642       $pbx_engine = $arg[1];
643     break;
644     case "--install-moh":
645       $install_moh = true;
646     break;
647     case "--install-fop":
648       if($arg[1] != "false")  {
649         $install_fop = true;
650         out("FOP will be installed and activated.");
651       }
652       else  {
653         out("FOP will be deactivated in the interface.  Set FOPDISABLE=false in amportal.conf to change later.");
654
655       }
656
657     break;
658     case "--make-links-devel":
659       $make_links = true;
660     break;
661     case "--skip-module-install":
662       $module_install = false;
663     break;
664     case "--fopwebroot":
665       $fopwebroot = $arg[1];
666       out("Using fop at ".$fopwebroot);
667     break;
668     case "--webroot":
669       $webroot = $arg[1];
670       out("Using Webroot at ".$webroot);
671     break;
672     case "--cgibin":
673       $cgibin = $arg[1];
674       out("Using CGI-BIN at ".$cgibin);
675     break;
676     case "--bin":
677       $ampbin_dir = $arg[1];
678       out("Using bin at ".$ampbin_dir);
679     break;
680     case "--sbin":
681       $ampsbin_dir = $arg[1];
682       out("Using sbin ar ".$ampsbin_dir);
683     break;
684     case "--asteriskuser":
685       $asterisk_user = $arg[1];
686       out("Using Asterisk user ".$asterisk_user);
687     break;
688     case "--asteriskpass":
689       $asterisk_pass = $arg[1];
690       out("Using asteriskpass ".str_repeat("*",strlen($arg[1])));
691     break;
692
693 /*    do we need this ?
694     case "--systemconfig":
695       $systemconfig = $arg[1];
696       out("Using system config at ". $systemconfig);
697     break;
698 */
699
700   }
701 }
702
703
704 // **** Look for user = root
705
706 outn("Checking user..");
707 //$current_user=(isset($_ENV["USER"]) ? $_ENV["USER"] : exec('whoami',$output));
708 $euid = (posix_getpwuid(posix_geteuid()));
709 $current_user = $euid['name'];
710 if ($current_user != "root"){
711   out("FAILED");
712   fatal($argv[0]." must be run as root");
713 }
714 out("OK");
715
716
717 outn("Checking if Asterisk is running..");
718 exec("pidof asterisk", $pid_val, $ret);
719 if ($ret) {
720   out("FAILED");
721   fatal($argv[0]."\n\tAsterisk must be running. If this is a first time install, you should start\n\tAsterisk by typing './start_asterisk start'\n\tFor upgrading, you should run 'amportal start'");
722 }
723 out("running with PID: ".$pid_val[0]."..OK");
724
725 // **** Check for amportal.conf, create if necessary
726
727 outn("Checking for ".AMP_CONF."..");
728 if (!file_exists(AMP_CONF)) {
729   out(AMP_CONF." does not exist, copying default");
730   copy("amportal.conf", AMP_CONF);
731
732   // this file contains password and should not be a+r
733   // this addresses http://freepbx.org/trac/ticket/1878
734   chown(AMP_CONF, "asterisk");
735   chgrp(AMP_CONF, "asterisk");
736   chmod(AMP_CONF, 0640);
737
738   collect_settings(AMP_CONF, $dbhost, $new_username, $new_password, 'asterisk');
739
740   out("Assuming new install, --install-moh added to command line");
741   $install_moh = true;
742 }
743 out("OK");
744
745 // **** read amportal.conf
746
747 outn("Reading ".AMP_CONF."..");
748 $amp_conf = install_parse_amportal_conf(AMP_CONF);
749 if (count($amp_conf) == 0) {
750   fatal("FAILED");
751 }
752 out("OK");
753
754 // Ensure our "critical" variables are set.  We absolutely need these to copy in files.
755
756 if (!array_key_exists("AMPWEBROOT",$amp_conf)) {
757   out("Adding AMPWEBROOT option to amportal.conf - using AMP default");
758   $amp_conf["AMPWEBROOT"] = "/var/www/html";
759 }
760
761 if (!array_key_exists("FOPWEBROOT",$amp_conf)) {
762   out("Adding FOPWEBROOT option to amportal.conf - using AMP default");
763   $amp_conf["FOPWEBROOT"] = $amp_conf["AMPWEBROOT"]."/panel";
764 }
765
766 if (!array_key_exists("AMPBIN",$amp_conf)) {
767   out("Adding AMPBIN option to amportal.conf - using AMP default");
768   $amp_conf["AMPBIN"] = "/var/lib/asterisk/bin";
769 }
770
771 if (!array_key_exists("AMPSBIN",$amp_conf)) {
772   out("Adding AMPSBIN option to amportal.conf - using AMP default");
773   $amp_conf["AMPSBIN"] = "/usr/sbin";
774 }
775
776 if (!array_key_exists("AMPDBENGINE",$amp_conf)) {
777   out("Adding AMPDBENGINE option to amportal.conf - using AMP default");
778   $amp_conf["AMPDBENGINE"] = "mysql";
779 }
780 if (!array_key_exists("AMPDBNAME",$amp_conf)) {
781   out("Adding AMPDBNAME option to amportal.conf - using AMP default");
782   $amp_conf["AMPDBNAME"] = "asterisk";
783 }
784
785 if (isset($new_username)) {
786   $amp_conf["AMPDBUSER"] = $new_username;
787 }
788
789 if (isset($new_password)) {
790   $amp_conf["AMPDBPASS"] = $new_password;
791 }
792
793 if (isset($dbhost)) {
794   $amp_conf["AMPDBHOST"] = $dbhost;
795 }
796
797 if (isset($dbname)) {
798   $amp_conf["AMPDBNAME"] = $dbname;
799 }
800 if(!$install_fop)  { // Set from --install-fop parameter; Add it to amportal.conf
801   out("Adding FOPDISABLE option to amportal.conf");
802   $amp_conf["FOPDISABLE"] = "true";
803 }
804 // If they pre-set this in their amportal.conf or this is an upgrade, we should honor it as well
805 if($amp_conf["FOPDISABLE"] == "true" || $amp_conf["FOPDISABLE"] == "YES" || $amp_conf["FOPDISABLE"] == "yes" || $amp_conf["FOPDISABLE"] == "Yes" || $amp_conf["FOPDISABLE"] == "y" )  {
806   $install_fop = false;
807 }
808 // write amportal.conf
809 write_amportal_conf(AMP_CONF, $amp_conf);
810
811 // **** Check for amportal.conf, create if necessary
812
813 outn("Checking for ".ASTERISK_CONF."..");
814 if (!file_exists(ASTERISK_CONF)) {
815   out(ASTERISK_CONF." does not exist, copying default");
816   copy("asterisk.conf", ASTERISK_CONF);
817 }
818 out("OK");
819
820 // **** read asterisk.conf
821
822 outn("Reading ".ASTERISK_CONF."..");
823 $asterisk_conf = install_parse_asterisk_conf(ASTERISK_CONF);
824 if (count($asterisk_conf) == 0) {
825   fatal("FAILED. Have you installed Asterisk?");
826 }
827 out("OK");
828
829 /* deprecated on freepbx 2.2, from now pages need to read this information
830    from $asterik_conf and not $amp_conf.
831    
832    this code will stay in 2.2, but in 2.3 it will be gone. developers - please
833    update your code
834  */
835 if (isset($asterisk_conf['astetcdir'])) { $amp_conf['ASTETCDIR'] = $asterisk_conf['astetcdir']; }
836 if (isset($asterisk_conf['astmoddir'])) { $amp_conf['ASTMODDIR'] = $asterisk_conf['astmoddir']; }
837 if (isset($asterisk_conf['astvarlibdir'])) { $amp_conf['ASTVARLIBDIR'] = $asterisk_conf['astvarlibdir']; }
838 if (isset($asterisk_conf['astagidir'])) { $amp_conf['ASTAGIDIR'] = $asterisk_conf['astagidir']; }
839 if (isset($asterisk_conf['astspooldir'])) { $amp_conf['ASTSPOOLDIR'] = $asterisk_conf['astspooldir']; }
840 if (isset($asterisk_conf['astrundir'])) { $amp_conf['ASTRUNDIR'] = $asterisk_conf['astrundir']; }
841 if (isset($asterisk_conf['astlogdir'])) { $amp_conf['ASTLOGDIR'] = $asterisk_conf['astlogdir']; }
842
843 if (!isset($pbx_engine)) { $pbx_engine='asterisk'; }
844 out("Using $pbx_engine as PBX Engine");
845 $amp_conf["AMPENGINE"]=$pbx_engine;
846
847 write_amportal_conf(AMP_CONF, $amp_conf);
848
849
850 // **** Write asterisk version to ASTETCDIR/version
851
852 $tmpoutput = '';
853 $tmpout = exec("asterisk -V", $tmpoutput, $exitcode);
854 if ($exitcode != 0) {
855   fatal("Error executing asterisk: be sure Asterisk is installed and in the path");
856 }
857 if (!$fd = fopen($amp_conf['ASTETCDIR'].'/version','w')) {
858   fatal('Cannot open '.$amp_conf['ASTETCDIR'].'/version for writing');
859 }
860 fwrite($fd, $tmpout);
861 fclose($fd);
862 // change to read-only
863 chmod($amp_conf['ASTETCDIR'].'/version',0444);
864
865
866 // normally this would be the contents of ASTETCDIR/version, but this is for simplicity, as we just read it above
867 $verinfo = $tmpout;
868
869 // **** Check asterisk verison
870 //  Set the 'engine' to be 'asterisk14' if using asterisk 1.4, otherwise
871 //  'asterisk'
872 outn("Checking for Asterisk version..");
873 if ((preg_match('/^Asterisk (\d+(\.\d+)*)(-?(.*))$/', $verinfo, $matches)) ||
874     (preg_match('/^Asterisk SVN-(\d+(\.\d+)*)(-?(.*))$/', $verinfo, $matches))) {
875
876   if (version_compare($matches[1], "1.2") < 0) {
877     fatal("Asterisk 1.2 or 1.4 is required for this version of FreePBX. Detected version is: ".$matches[1]);
878   }
879   if (version_compare($matches[1], "1.7", "ge")) {
880     fatal("Asterisk 1.2, 1.4, or 1.6 is required for this version of FreePBX. Detected version is: ".$matches[1]);
881   }
882   out("{$matches[1]}");
883 } elseif (preg_match('/^Asterisk SVN.+/', $verinfo)) {
884   out("FAIL");
885   out("*** WARNING ***");
886   out("You are not using a released version of Asterisk. We are unable to verify");
887   out("that your Asterisk version is compatible with FreePBX. Whilst this probably");
888   out("won't cause any problems, YOU NEED TO BE CERTAIN that it is compatible");
889   out("with at least the released Asterisk version 1.2" );
890   if ($override_astvers==false) {
891     out("If you are SURE that this is compatible, you can re-run ".$argv[0]." with");
892     out("the parameter --my-svn-is-correct");
893     exit;
894   } else {
895     out("--my-svn-is-correct specified, continuing");
896   }
897 } else {
898   fatal("Could not determine asterisk version (got: \"".$verinfo."\" please report this)");
899 }
900
901 // **** Make sure selinux isn't enabled
902
903 outn("Checking for selinux..");
904 $tmpoutput;
905 $tmpout = exec("getenforce", $tmpoutput, $sereturn);
906 if (strtolower($tmpoutput[0]) === "enabled") {
907         // this method seems better because disabled and premissive are the same
908         // if a user installs and realizes selinux is running the other method
909         // requires a reboot to get selinuxenabled to work after editing the  selinux config
910         // this will allow you to use setenforce 0 which turns selinux into premissive mode which
911         // doesnt enforce, it just warns.
912   fatal("selinux is ENABLED. This is not supported. Please disable selinux before using FreePBX");
913 }
914 out("OK");
915
916 // **** Connect to database
917
918 outn("Connecting to database..");
919
920 $db_engine = $amp_conf["AMPDBENGINE"];
921 if ($db_engine != "sqlite3") {
922   $db_user = $amp_conf["AMPDBUSER"];
923   $db_pass = $amp_conf["AMPDBPASS"];
924   $db_host = $amp_conf["AMPDBHOST"];
925 }
926 $db_name = $amp_conf["AMPDBNAME"];
927
928 // we still support older configurations,  and fall back
929 // into mysql when no other engine is defined
930 if ($db_engine == "")
931 {
932   $db_engine = "mysql";
933 }
934  
935 switch ($db_engine)
936 {
937   case "pgsql":
938   case "mysql":
939     // datasource in in this style: dbengine://username:password@host/database
940     if (!function_exists($db_engine.'_connect')) {
941       out("FAILED");
942       fatal($db_engine." PHP libraries not installed");
943     }
944  
945     $datasource = $db_engine.'://'.$db_user.':'.$db_pass.'@'.$db_host.'/'.$db_name;
946     $db = DB::connect($datasource); // attempt connection
947     break;
948  
949   case "sqlite":
950     die_freepbx("SQLite2 support is deprecated. Please use sqlite3 only.");
951     break;
952  
953   case "sqlite3":
954     if (!isset($amp_conf["AMPDBFILE"]))
955       die("You must setup properly AMPDBFILE in /etc/amportal.conf");
956      
957     if (isset($amp_conf["AMPDBFILE"]) == "")
958       die("AMPDBFILE in /etc/amportal.conf cannot be blank");
959
960     /* on centos this extension is not loaded by default */
961     if (! extension_loaded('sqlite3.so')  && ! extension_loaded('SQLITE3'))
962       dl('sqlite3.so');
963
964     if (! @require_once('DB/sqlite3.php') )
965     {
966       out("FAILED");
967       fatal( "Your PHP installation has no PEAR/SQLite3 support. Please install php-sqlite3 and php-pear.");
968     }
969
970     $datasource = "sqlite3:///" . $amp_conf["AMPDBFILE"] . "?mode=0666";
971     $db = DB::connect($datasource);
972     break;
973
974   default:
975     die( "Unknown SQL engine: [$db_engine]");
976 }
977
978 if(DB::isError($db)) {
979   out("FAILED");
980   debug($db->userinfo);
981   out("Try running ".$argv[0]." --username=user --password=pass  (using your own user and pass)");
982   fatal("Cannot connect to database");
983  
984 }
985 out("OK");
986
987
988 // **** Read DB for version info
989
990 if (!isset($version)) {
991   outn("Checking current version of AMP..");
992   $version = install_getversion();
993   if (!$version) {
994     out("no version information");
995     out("Assuming new installation");
996   } else {
997     out($version);
998   }
999 }
1000
1001
1002 // **** Copy files
1003
1004 if ($install_files)
1005 {
1006   outn("Installing new FreePBX files..");
1007   $check_md5s=true;
1008   $md5sums = read_md5_file(UPGRADE_DIR."/".$version.".md5");
1009   list($num_files, $num_copied) = recursive_copy("amp_conf", "", $md5sums);
1010   if (!is_file("/etc/asterisk/voicemail.conf")) copy("/etc/asterisk/voicemail.conf.template","/etc/asterisk/voicemail.conf");
1011   if (!is_dir("/var/spool/asterisk/voicemail/device")) amp_mkdir("/var/spool/asterisk/voicemail/device", "0755", true);
1012   out("OK (".$num_copied." files copied, ".($num_files-$num_copied)." skipped)");
1013 }
1014
1015 // **** Apply amportal.conf configuration to files
1016 debug("Running ".dirname(__FILE__)."/apply_conf.sh");
1017 outn("Configuring install for your environment..");
1018 if (!$dryrun) {
1019   if (file_exists($amp_conf["AMPSBIN"]."/amportal")) {
1020     exec("chmod u+x ".$amp_conf["AMPSBIN"]."/amportal");
1021     outn("amportal..");
1022   } else {
1023     outn("no amportal..");
1024   }
1025   if (file_exists($amp_conf["AMPBIN"]."/freepbx_engine")) {
1026     exec("chmod u+x ".$amp_conf["AMPBIN"]."/freepbx_engine");
1027     outn("freepbx_engine..");
1028   } else {
1029     outn("no freepbx_engine..");
1030   }
1031   out("done");
1032   // edit conf file passwords and then
1033   // reload manager in asterisk if it is running:
1034   //
1035   outn("apply username/password changes to conf files..");
1036   exec(dirname(__FILE__)."/apply_conf.sh");
1037   out("done");
1038
1039   /* As of Asterisk 1.4.16 or there abouts, a missing #include file will make the reload fail. So
1040     we need to make sure that we have such for everything that is in our configs. We will simply
1041     look for the #include statements and touch the files vs. trying to inventory everything we may
1042     need and then forgetting something.
1043   */
1044
1045   outn("creating missing #include files..");
1046   $include_err = false;
1047   exec("grep '#include' ".$amp_conf['ASTETCDIR']."/*.conf | sed 's/;.*//; s/#include//'",$output,$retcode);
1048   if ($retcode != 0) {
1049     out("Error code $retcode: trying to search for missing #include files");
1050     $include_err = true;
1051   }
1052
1053   foreach($output as $file) {
1054     if (trim($file) == '') {
1055       continue;
1056     }
1057     $parse1 = explode(':',$file);
1058     $parse2 = explode(';',$parse1[1]);
1059     $rawfile = trim($parse2[0]);
1060     if ($rawfile == '') {
1061       continue;
1062     }
1063
1064     $target = ($rawfile[0] == '/') ? $rawfile : $amp_conf['ASTETCDIR']."/$rawfile";
1065
1066     if (!file_exists($target)) {
1067       exec("touch $target", $output, $retcode);
1068         if ($retcode != 0) {
1069         out("Error code $retcode: trying to create empty file $target");
1070         $include_err = true;
1071       }
1072     }
1073   }
1074   if (! $include_err) {
1075     out("OK");
1076   }
1077   // reload manager in asterisk if it was running:
1078   // Execute the 1.4+ syntax and the 1.2 syntax, it will reload twice on 1.4 and in the other cases
1079   // it will simply ignore the command since it has been removed. (not worth trying to figure out what
1080   // version they have...
1081   //
1082   system("asterisk -rx 'module reload manager'");
1083   system("asterisk -rx 'reload manager'");
1084 }
1085 out("OK");
1086
1087 // **** Create spool directories for monitor and fax
1088 if (!is_dir($asterisk_conf["astspooldir"]."/monitor"))
1089   amp_mkdir($asterisk_conf["astspooldir"]."/monitor","0766",true);
1090 if (!is_dir($asterisk_conf["astspooldir"]."/fax"))
1091   amp_mkdir($asterisk_conf["astspooldir"]."/fax","0766",true);
1092
1093
1094 // **** Set permissions all files
1095
1096 if ($install_files)
1097 {
1098   outn("Setting permissions on files..");
1099   if (!$dryrun) {
1100     exec($amp_conf["AMPSBIN"]."/amportal chown");
1101   }
1102   out("OK");
1103 }
1104
1105 // Run through all the upgrade scripts starting at the specified version
1106 //
1107 upgrade_all($version);
1108
1109 // **** Generate AMP configs
1110 out("Generating AMP configs..");
1111 generate_configs();
1112 out("Generating AMP configs..OK");
1113
1114 $version = install_getversion();
1115
1116 if($install_fop && $amp_conf["FOPRUN"])  {
1117   // **** Bounce FOP
1118   outn("Restarting Flash Operator Panel..");
1119   exec('su - asterisk -c "'.$amp_conf["AMPBIN"].'/bounce_op.sh"');
1120   out("OK");
1121 }
1122
1123 // Now force an install for all modules that are packaged with the tarball
1124 // directory.
1125 //
1126 if ($module_install) {
1127   install_modules();
1128 } else {
1129   out("bypassing packaged module installation because --skip-module-install flag");
1130 }
1131
1132
1133 // **** Set reload flag for AMP admin
1134 install_needreload();
1135
1136 if ($amp_conf["AMPWEBADDRESS"])
1137 {
1138   out("Please update your modules and reload Asterisk by visiting http://".$amp_conf["AMPWEBADDRESS"]."/admin");
1139 }
1140 else
1141 {
1142   out("Please update your modules and reload Asterisk by browsing to your server.");
1143 }
1144
1145 out("");
1146 out("*************************************************************************");
1147 out("* Note: It's possible that if you click the red 'Update Now' bar BEFORE *");
1148 out("* updating your modules, your machine will start dropping calls. Ensure *");
1149 out("* that all modules are up to date BEFORE YOU CLICK THE RED BAR. As long *");
1150 out("* as this is observed, your machine will be fully functional whilst the *");
1151 out("* upgrade is in progress.                                               *");
1152 out("*************************************************************************");
1153 ?>
Note: See TracBrowser for help on using the browser.