root/freepbx/trunk/install_amp

Revision 2172, 28.7 kB (checked in by diego_iastrubni, 7 years ago)

- added to index.php the collection of asterisk.conf settings, is it needed?
- footer will read $asterisk_conf[] instead of $amp_conf[] for reading asterisk settings
- install_amp contains a comment which says the merging of asterisk.conf into amportal.conf will get

removed in next version (2.3).


developers:

please dont use $amp_conf[] to read configuraiton from asterisk.conf, this is going to be removed in the
future. those settings can be collected from other hashes, which are available for you. if not, make them.


i am leaving it for now, just for you to update the code.


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