root/freepbx/trunk/install_amp

Revision 2090, 26.9 kB (checked in by qldrob, 7 years ago)

Fix hardcoded executable paths, use /usr/bin/env, as per #759

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