root/freepbx/tags/2.3.0beta2/install_amp

Revision 4478, 32.0 kB (checked in by p_lindheimer, 6 years ago)

- #2128: fix AMPDBPASS not getting written in amportal.conf
- remove default values from amportal.conf that are autogenerated in install_amp
- make install_amp check for asterisk running and fail if not
- move module installtion section after everything is setup properly to connect to

asterisk through the manager

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