root/freepbx/trunk/install_amp

Revision 1998, 27.1 kB (checked in by qldrob, 7 years ago)

Bump install_amp to 2.2.0beta1

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