root/freepbx/tags/2.2.0b2/install_amp

Revision 3013, 31.3 kB (checked in by qldrob, 7 years ago)

Merged revisions 2995-2999,3005-3007,3011-3012 via svnmerge from
https://svn.sourceforge.net/svnroot/amportal/freepbx/trunk

........

r3012 | qldrob | 2006-11-06 13:29:38 +1000 (Mon, 06 Nov 2006) | 2 lines


Update version to 2.2b2

........

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