root/freepbx/trunk/install_amp

Revision 1728, 26.7 kB (checked in by qldrob, 7 years ago)

Puff puff pant pant.. Check for asterisk 1.2 before installing.

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