root/freepbx/branches/2.1/install_amp

Revision 2609, 27.1 kB (checked in by qldrob, 6 years ago)

Forgot to bump the version number. Sigh.

  • 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.1.2',
34     '2.1.3'
35   );
36
37 define("AMP_CONF", "/etc/amportal.conf");
38
39 define("ASTERISK_CONF", "/etc/asterisk/asterisk.conf");
40
41 define("UPGRADE_DIR", dirname(__FILE__)."/upgrades");
42
43 /********************************************************************************************************************/
44
45 function out($text) {
46   echo $text."\n";
47 }
48
49 function outn($text) {
50   echo $text;
51 }
52
53 function error($text) {
54   echo "[ERROR] ".$text."\n";
55 }
56
57 function fatal($text) {
58   echo "[FATAL] ".$text."\n";
59   exit(1);
60 }
61
62 function debug($text) {
63   global $debug;
64  
65   if ($debug) echo "[DEBUG] ".$text."\n";
66 }
67
68 function showHelp() {
69   out("Optional parameters:");
70   out("  --help, -h, -?           Show this help");
71   out("  --username <user>        Use <user> to connect to db and write config");
72   out("  --password <pass>        Use <pass> to connect to db and write config");
73   out("  --debug                  Enable debug output");
74   out("  --dry-run                Don't actually do anything");
75   out("  --force-version <ver>    Force upgrade from version <ver>");
76   out("  --dbhost <ip address>    Use a remote database server");
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 = '') {
422   out("Creating new /etc/amportal.conf");
423  
424   outn("Enter your USERNAME to connect to the 'asterisk' 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 'asterisk' 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 'asterisk' 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");
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 $new_username = null;
586 $new_password = null;
587
588 foreach ($args[0] as $arg) {
589   switch ($arg[0]) {
590     case "--help": case "h": case "?":
591       showHelp();
592       exit(10);
593     break;
594     case "--dry-run":
595       out("Dry-run only, nothing will be changed");
596       $dryrun = true;
597     break;
598     case "--debug":
599       $debug = true;
600       debug("Debug mode enabled");
601     break;
602     case "--username": case "u":
603       out("Using username: ".$arg[1]);
604       $new_username = $arg[1];
605     break;
606     case "--password": case "p":
607       out("Using password: ".str_repeat("*",strlen($arg[1])));
608       $new_password = $arg[1];
609     break;
610     case "--force-version":
611       $version = $arg[1];
612       out("Forcing upgrade from version ".$version);
613     break;
614     case "--dbhost":
615       $dbhost = $arg[1];
616       out("Using remote database server at ".$dbhost);
617     break;
618     case "--no-files":
619       $install_files = false;
620       out("Running upgrade only, without installing files.");
621     break;
622   }
623 }
624
625
626 // **** Look for user = root
627
628 outn("Checking user..");
629 //$current_user=(isset($_ENV["USER"]) ? $_ENV["USER"] : exec('whoami',$output));
630 $euid = (posix_getpwuid(posix_geteuid()));
631 $current_user = $euid['name'];
632 if ($current_user != "root"){
633   out("FAILED");
634   fatal($argv[0]." must be run as root");
635 }
636 out("OK");
637
638
639 // **** Check for amportal.conf, create if necessary
640
641 outn("Checking for ".AMP_CONF."..");
642 if (!file_exists(AMP_CONF)) {
643   out(AMP_CONF." does not exist, copying default");
644   copy("amportal.conf", "/etc/amportal.conf");
645   collect_settings(AMP_CONF, $dbhost, $new_username, $new_password);
646 }
647 out("OK");
648
649 // **** read amportal.conf
650
651 outn("Reading ".AMP_CONF."..");
652 $amp_conf = install_parse_amportal_conf(AMP_CONF);
653 if (count($amp_conf) == 0) {
654   fatal("FAILED");
655 }
656 out("OK");
657
658 // Ensure our "critical" variables are set.  We absolutely need these to copy in files.
659
660 if (!array_key_exists("FOPWEBROOT",$amp_conf) ||
661   !array_key_exists("AMPBIN",$amp_conf) ||
662   !array_key_exists("AMPSBIN",$amp_conf) ||
663   !array_key_exists("AMPCGIBIN",$amp_conf) ||
664   !array_key_exists("AMPWEBROOT",$amp_conf)
665 ) {
666
667   if (!array_key_exists("AMPWEBROOT",$amp_conf)) {
668     out("Adding AMPWEBROOT option to amportal.conf - using AMP default");
669     $amp_conf["AMPWEBROOT"] = "/var/www/html";
670   }
671  
672   if (!array_key_exists("AMPCGIBIN",$amp_conf)) {
673     out("Adding AMPCGIBIN option to amportal.conf - using AMP default");
674     $amp_conf["AMPCGIBIN"] = "/var/www/cgi-bin";
675   }
676  
677   if (!array_key_exists("FOPWEBROOT",$amp_conf)) {
678     out("Adding FOPWEBROOT option to amportal.conf - using AMP default");
679     $amp_conf["FOPWEBROOT"] = $amp_conf["AMPWEBROOT"]."/panel";
680   }
681  
682   if (!array_key_exists("AMPBIN",$amp_conf)) {
683     out("Adding AMPBIN option to amportal.conf - using AMP default");
684     $amp_conf["AMPBIN"] = "/var/lib/asterisk/bin";
685   }
686  
687   if (!array_key_exists("AMPSBIN",$amp_conf)) {
688     out("Adding AMPSBIN option to amportal.conf - using AMP default");
689     $amp_conf["AMPSBIN"] = "/usr/sbin";
690   }
691  
692   // write amportal.conf
693   write_amportal_conf(AMP_CONF, $amp_conf);
694 }
695
696 if (isset($new_username) || isset($new_password) || isset($dbhost)) {
697   // new username/pwd
698  
699   if (isset($new_username)) {
700     $amp_conf["AMPDBUSER"] = $new_username;
701   }
702   if (isset($new_password)) {
703     $amp_conf["AMPDBPASS"] = $new_password;
704   }
705  
706   if (isset($dbhost)) {
707     $amp_conf["AMPDBHOST"] = $dbhost;
708   }
709  
710   // write amportal.conf
711   write_amportal_conf(AMP_CONF, $amp_conf);
712 }
713
714 // **** Check for amportal.conf, create if necessary
715
716 outn("Checking for ".ASTERISK_CONF."..");
717 if (!file_exists(ASTERISK_CONF)) {
718   out(ASTERISK_CONF." does not exist, copying default");
719   copy("asterisk.conf", "/etc/asterisk/asterisk.conf");
720   //TODO - need to prompt for asterisk specific directories - using * defaults for now
721   //collect_ast_settings(ASTERISK_CONF, $dbhost, $new_username, $new_password);
722 }
723 out("OK");
724
725 // **** read asterisk.conf
726
727 outn("Reading ".ASTERISK_CONF."..");
728 $asterisk_conf = install_parse_asterisk_conf(ASTERISK_CONF);
729 if (count($asterisk_conf) == 0) {
730   fatal("FAILED. Have you installed Asterisk?");
731 }
732 out("OK");
733
734 if (isset($asterisk_conf['astetcdir'])) { $amp_conf['ASTETCDIR'] = $asterisk_conf['astetcdir']; }
735 if (isset($asterisk_conf['astmoddir'])) { $amp_conf['ASTMODDIR'] = $asterisk_conf['astmoddir']; }
736 if (isset($asterisk_conf['astvarlibdir'])) { $amp_conf['ASTVARLIBDIR'] = $asterisk_conf['astvarlibdir']; }
737 if (isset($asterisk_conf['astagidir'])) { $amp_conf['ASTAGIDIR'] = $asterisk_conf['astagidir']; }
738 if (isset($asterisk_conf['astspooldir'])) { $amp_conf['ASTSPOOLDIR'] = $asterisk_conf['astspooldir']; }
739 if (isset($asterisk_conf['astrundir'])) { $amp_conf['ASTRUNDIR'] = $asterisk_conf['astrundir']; }
740 if (isset($asterisk_conf['astlogdir'])) { $amp_conf['ASTLOGDIR'] = $asterisk_conf['astlogdir']; }
741
742 write_amportal_conf(AMP_CONF, $amp_conf);
743
744 // **** Check for func_callerid.so - this is only in asterisk 1.2
745
746 outn("Checking for Asterisk 1.2..");
747 if (!file_exists($amp_conf["ASTMODDIR"]."/func_callerid.so")) {
748   fatal("Asterisk 1.2 is required for this version of freePBX");
749 }
750 out("OK");
751
752 // **** Make sure selinux isn't enabled
753
754 outn("Checking for selinux..");
755 $tmpout = exec("selinuxenabled 2>&1", $tmpoutput, $sereturn);
756 if ($sereturn == 0) {
757   fatal("selinux is ENABLED. This is not supported. Please disable selinux before using freePBX");
758 }
759 out("OK");
760
761 // **** Connect to database
762
763 outn("Connecting to database..");
764
765 $db_user = $amp_conf["AMPDBUSER"];
766 $db_pass = $amp_conf["AMPDBPASS"];
767 $db_host = $amp_conf["AMPDBHOST"];
768 $db_engine = $amp_conf["AMPDBENGINE"];
769 $db_name = 'asterisk';
770
771 // we still support older configurations,  and fall back
772 // into mysql when no other engine is defined
773 if ( !isset($conf["AMPDBENGINE"]) || ($conf["AMPDBENGINE"] == ""))
774 {
775   $db_engine = "mysql";
776 }
777  
778 switch ($db_engine)
779 {
780   case "pgsql":
781   case "mysql":
782     // datasource in in this style: dbengine://username:password@host/database
783     if (!function_exists($db_engine.'_connect')) {
784       out("FAILED");
785       fatal($db_engine." PHP libraries not installed");
786     }
787  
788     $datasource = $db_engine.'://'.$db_user.':'.$db_pass.'@'.$db_host.'/'.$db_name;
789     $db = DB::connect($datasource); // attempt connection
790     break;
791  
792   case "sqlite":
793     if (! @ include('DB/sqlite.php'))
794     {
795       out("FAILED");
796       fatal( "Your PHP installation lacks SQLite support" );
797     }
798  
799     if (!isset($amp_conf["AMPDBFILE"]))
800       die("You must setup properly AMPDBFILE in /etc/amportal.conf");
801  
802     if (isset($amp_conf["AMPDBFILE"]) == "")
803       die("AMPDBFILE in /etc/amportal.conf cannot be blank");
804  
805     $DSN = array (
806       "database" => $amp_conf["AMPDBFILE"],
807       "mode" => 0666
808     );
809  
810     $db = new DB_sqlite();
811     $db->connect( $DSN );
812     break;
813  
814   default:
815     die( "Unknown SQL engine: [$db_engine]");
816 }
817
818 if(DB::isError($db)) {
819   out("FAILED");
820   debug($db->userinfo);
821   out("Try running ".$argv[0]." --username=user --password=pass  (using your own user and pass)");
822   fatal("Cannot connect to database");
823  
824 }
825 out("OK");
826
827
828 // **** Read DB for version info
829
830 if (!isset($version)) {
831   outn("Checking current version of AMP..");
832   $version = install_getversion();
833   if (!$version) {
834     out("no version information");
835     out("Assuming new installation");
836   } else {
837     out($version);
838   }
839 }
840
841
842 // **** Copy files
843
844 if ($install_files)
845 {
846   outn("Installing new AMP files..");
847   $check_md5s=true;
848   $md5sums = read_md5_file(UPGRADE_DIR."/".$version.".md5");
849   recursive_copy("amp_conf", "", $md5sums);
850   if (!is_file("/etc/asterisk/voicemail.conf")) copy("/etc/asterisk/voicemail.conf.template","/etc/asterisk/voicemail.conf");
851   if (!is_dir("/var/spool/asterisk/voicemail/device")) amp_mkdir("/var/spool/asterisk/voicemail/device", "0755", true);
852   out("OK");
853 }
854
855 // **** Apply amportal.conf configuration to files
856 debug("Running ".dirname(__FILE__)."/apply_conf.sh");
857 outn("Configuring install for your environment..");
858 if (!$dryrun) {
859   if (file_exists($amp_conf["AMPSBIN"]."/amportal"))
860     exec("chmod u+x ".$amp_conf["AMPSBIN"]."/amportal");
861   exec(dirname(__FILE__)."/apply_conf.sh");
862 }
863 out("OK");
864
865 // **** Create spool directories for monitor and fax
866 if (!is_dir($asterisk_conf["astspooldir"]."/monitor"))
867   amp_mkdir($asterisk_conf["astspooldir"]."/monitor","0766",true);
868 if (!is_dir($asterisk_conf["astspooldir"]."/fax"))
869   amp_mkdir($asterisk_conf["astspooldir"]."/fax","0766",true);
870
871
872 // **** Set permissions all files
873
874 if ($install_files)
875 {
876   outn("Setting permissions on files..");
877   if (!$dryrun) {
878     exec($amp_conf["AMPSBIN"]."/amportal chown");
879   }
880   out("OK");
881 }
882
883
884 // **** Read upgrades/ directory
885
886 outn("Checking for upgrades..");
887
888 // read it from ugprades/ unless $version has already been defined
889 if (!isset($versions)) {
890   $versions = array();
891   $dir = opendir(UPGRADE_DIR);
892   while ($file = readdir($dir)) {
893     if (($file[0] != ".") && is_dir(UPGRADE_DIR."/".$file)) {
894       $versions[] = $file;
895     }
896   }
897   closedir($dir);
898
899   // callback to use php's version_compare() to sort 
900   usort($versions, "version_compare");
901 }
902
903 if (false !== ($pos = array_search($version, $versions))) {
904   $upgrades = array_slice($versions, $pos+1);
905   out(count($upgrades)." found");
906  
907   run_upgrade($upgrades);
908 } else {
909   out("Current version not found");
910 }
911
912
913 // **** Generate AMP configs
914 out("Generating AMP configs..");
915 generate_configs();
916 out("Generating AMP configs..OK");
917
918 // **** Bounce FOP
919 outn("Restarting Flash Operator Panel..");
920 exec('su - asterisk -c "'.$amp_conf["AMPWEBROOT"].'/admin/bounce_op.sh"');
921 out("OK");
922
923 $version = install_getversion();
924 $filename = $amp_conf["AMPWEBROOT"]."/admin/version.txt";
925 if (!$fd = fopen($filename, "w")) {
926   fatal("Could not open ".$filename." for writing");
927 }
928 fwrite($fd, $version);
929 fclose($fd);
930
931
932
933 // **** Set reload flag for AMP admin
934 install_needreload();
935
936 if ($amp_conf["AMPWEBADDRESS"])
937 {
938   out("Please Reload Asterisk by visiting http://".$amp_conf["AMPWEBADDRESS"]."/admin");
939 }
940 else
941 {
942   out("Please Reload Asterisk by browsing your server.");
943 }
944
945 ?>
Note: See TracBrowser for help on using the browser.