root/freepbx/tags/2.1.3.bad/install_amp

Revision 2579, 27.1 kB (checked in by diego_iastrubni, 7 years ago)

avoid warnings on some installs

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