root/freepbx/tags/2.3.1/install_amp

Revision 5020, 31.9 kB (checked in by p_lindheimer, 6 years ago)

Merged revisions 5019 via svnmerge from
https://amportal.svn.sourceforge.net/svnroot/amportal/freepbx/trunk

........

r5019 | p_lindheimer | 2007-09-09 21:24:41 -0700 (Sun, 09 Sep 2007) | 1 line


remove some unused variales causing confusion because of a typo

........

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