root/freepbx/trunk/install_amp

Revision 7088, 35.6 kB (checked in by p_lindheimer, 1 month ago)

Merged revisions 6976-7087 via svnmerge from
http://svn.freepbx.org/freepbx/branches/2.5

........

r6982 | mickecarlsson | 2008-10-14 03:48:18 -0700 (Tue, 14 Oct 2008) | 1 line


New and modified versions of russian translation for modules from #3279

........

r6987 | mickecarlsson | 2008-10-15 08:41:38 -0700 (Wed, 15 Oct 2008) | 1 line


Add Hungarian as language choice in freepbx_admin.php

........

r7002 | p_lindheimer | 2008-10-18 10:18:15 -0700 (Sat, 18 Oct 2008) | 1 line


ref #3271 detect ABE Business Edition, tested against 1.4 non-business and nothing is broken, need confirmation from testing on BE that it does as adverstised, install_amp change still needs testing before checkin ing

........

r7003 | mickecarlsson | 2008-10-18 10:33:29 -0700 (Sat, 18 Oct 2008) | 1 line


Updated amp.pot and swedish language for amp

........

r7006 | p_lindheimer | 2008-10-18 11:10:33 -0700 (Sat, 18 Oct 2008) | 1 line


closes #3264 add languageprefix=yes and while we are at it, execincludes=yes

........

r7007 | mickecarlsson | 2008-10-18 14:05:26 -0700 (Sat, 18 Oct 2008) | 1 line


Swedish language for amp. All text is translated

........

r7009 | mickecarlsson | 2008-10-19 00:09:14 -0700 (Sun, 19 Oct 2008) | 1 line


Closes #3279, new russian translations for amp

........

r7052 | p_lindheimer | 2008-10-21 07:51:22 -0700 (Tue, 21 Oct 2008) | 1 line


closes #3309 have op_server.pl reload instead of killing and restarting

........

r7058 | p_lindheimer | 2008-10-21 11:11:41 -0700 (Tue, 21 Oct 2008) | 1 line


send raw asterisk version info if the parsed version is blank, and escape the & when sending to wget

........

r7059 | p_lindheimer | 2008-10-21 11:14:30 -0700 (Tue, 21 Oct 2008) | 1 line


revert r7058, should not have been checked in until we fix the css for the tabs

........

r7060 | p_lindheimer | 2008-10-21 11:32:25 -0700 (Tue, 21 Oct 2008) | 1 line


change default of MODULEADMINWGET to false, was set to true in amportal.conf

........

r7061 | sasargen | 2008-10-21 12:49:36 -0700 (Tue, 21 Oct 2008) | 1 line


fix text overflow in left nav tab menu

........

r7062 | p_lindheimer | 2008-10-21 13:16:55 -0700 (Tue, 21 Oct 2008) | 1 line


localize the Tool/Setup lnav tabs, they will be truncated if too wide so translators should review and consider abbreviating Tools and Setup which only appear in the tabs and not elsewhere

........

r7063 | p_lindheimer | 2008-10-21 22:10:22 -0700 (Tue, 21 Oct 2008) | 1 line


create 2.5.1 upgrade directory to bump version

........

r7064 | p_lindheimer | 2008-10-21 22:13:01 -0700 (Tue, 21 Oct 2008) | 1 line


closes #3271 allow install_amp for business edition

........

r7065 | p_lindheimer | 2008-10-21 22:16:00 -0700 (Tue, 21 Oct 2008) | 1 line


updated CHANGES for 2.5.1 release

........

r7066 | p_lindheimer | 2008-10-21 22:20:14 -0700 (Tue, 21 Oct 2008) | 1 line


fix spelling errors to keep mickecarlsson happy :-)

........

r7070 | p_lindheimer | 2008-10-21 22:36:53 -0700 (Tue, 21 Oct 2008) | 1 line


Creating release 2.5.1

........

  • Property svn:mime-type set to text/plain
  • 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 require_once ("libfreepbx.install.php");
5
6 # constants
7 define("AMP_CONF", "/etc/amportal.conf");
8 define("ASTERISK_CONF", "/etc/asterisk/asterisk.conf");
9 define("UPGRADE_DIR", dirname(__FILE__)."/upgrades");
10 define("MODULE_DIR", dirname(__FILE__)."/amp_conf/htdocs/admin/modules/");
11
12 # semi constants
13 $webroot        = "/var/www/html";
14 $fopwebroot     = "";                           // if blank, will use $webroot/panel
15 $ampsbin_dir = "/usr/local/sbin";       // default if not set
16 $ampbin_dir = "/var/lib/asterisk/bin";
17 $asterisk_user  = "asteriskuser";
18 $asterisk_pass  = "amp109";
19
20
21 /*
22 freepbx versions
23                 '1.10.005',
24                 '1.10.006',
25                 '1.10.007beta1',
26                 '1.10.007beta2',
27                 '1.10.007',
28                 '1.10.007a',
29                 '1.10.008beta1',
30                 '1.10.008beta2',
31                 '1.10.008beta3',
32                 '1.10.008',
33                 '1.10.009beta1',
34                 '1.10.009beta2',
35                 '1.10.009',
36                 '1.10.010beta1',
37                 '1.10.010',
38                 '2.0beta1',
39                 '2.0beta2',
40                 '2.0beta3',
41                 '2.0beta4',
42                 '2.0beta5',
43                 '2.0.0',
44                 '2.0.1',
45                 '2.1beta1',
46                 '2.1beta2',
47                 '2.1beta3',
48                 '2.1.0',
49                 '2.1.1',
50                 '2.1.2',
51                 '2.1.3',
52                 '2.2.0beta1',
53                 '2.2.0beta2',
54                 '2.2.0beta3',
55                 '2.2.0rc1',
56                 '2.2.0rc2',
57                 '2.2.0rc3',
58                 '2.2.0',
59                 '2.2.1',
60                 '2.2.2',
61                 '2.2.3',
62                 '2.3.0beta1',
63 */
64
65 /********************************************************************************************************************/
66
67 function out($text) {
68         echo $text."\n";
69 }
70
71 function outn($text) {
72         echo $text;
73 }
74
75 function error($text) {
76         echo "[ERROR] ".$text."\n";
77 }
78
79 function fatal($text) {
80         echo "[FATAL] ".$text."\n";
81         exit(1);
82 }
83
84 function debug($text) {
85         global $debug;
86        
87         if ($debug) echo "[DEBUG-preDB] ".$text."\n";
88 }
89
90 if (!function_exists('version_compare_freepbx')) {
91         /* verison_compare that works with freePBX version numbers
92         */
93         function version_compare_freepbx($version1, $version2, $op = null) {
94                 $version1 = str_replace("rc","RC", strtolower($version1));
95                 $version2 = str_replace("rc","RC", strtolower($version2));
96                         if (!is_null($op)) {
97                                 return version_compare($version1, $version2, $op);
98                         } else {
99                                 return version_compare($version1, $version2);
100                         }
101         }
102 }
103
104 function showHelp() {
105         out("Optional parameters:");
106         out("  --help, -h, -?           Show this help");
107         out("  --dbhost <ip address>    Use a remote database server");
108         out("  --dbname databasename    Use database name specified, instead of 'asterisk'");
109         out("  --username <user>        Use <user> to connect to db and write config");
110         out("  --password <pass>        Use <pass> to connect to db and write config");
111         out("  --fopwebroot <path>      Web path where fop will be installed");
112         out("  --webroot <path>         Web root where freepbx will be installed");
113         out("  --cgibin <path>          Path where cgi-bin's lives");
114         out("  --bin <path>             Path of asterisk binaries");
115         out("  --sbin <path>            Path of system admin binaries");
116         out("  --asteriskuser <user>    Asterisk Manager username");
117         out("  --asteriskpass <pass>    Asterisk Manager password");
118         out("  --systemconfig <path>    System config files");
119         out("  --debug                  Enable debug output");
120         out("  --dry-run                Don't actually do anything");
121         out("  --force-version <ver>    Force upgrade from version <ver>");
122         out("  --skip-module-install    Don't run install scripts for packaged modules, the files are still loaded.");
123         out("                           In a development environment you may not want the install scripts run.");
124         out("  --no-files               Just run updates without installing files");
125         out("  --install-moh            Install default music-on-hold files (normally doesn't, unless ");
126         out("                           it's a new installation)");
127         out("  --install-fop=false      Don't install FOP and don't display it anywhere on the interface");
128         //out("  --make-links-devel       Make links to files in the source directory instead of copying");
129         //out("                           (intended for developers only)");
130         out("  --my-svn-is-correct      Ignore Asterisk version, assume it is correct");
131         out("  --engine <name>          Use the specified PBX Engine ('asterisk', 'asterisk14' or 'openpbx')");
132 }
133
134 function install_parse_amportal_conf($filename) {
135         $file = file($filename);
136         foreach ($file as $line) {
137                 if (preg_match("/^\s*([a-zA-Z0-9]+)\s*=\s*(.*)\s*([;#].*)?/",$line,$matches)) {
138                         $conf[ $matches[1] ] = $matches[2];
139                 }
140         }
141
142         // use same defaults as function.inc.php
143         if ( !isset($conf["AMPDBENGINE"]) || ($conf["AMPDBENGINE"] == "")) {
144                 $conf["AMPDBENGINE"] = "mysql";
145         }
146        
147         if ( !isset($conf["AMPDBNAME"]) || ($conf["AMPDBNAME"] == "")) {
148                 $conf["AMPDBNAME"] = "asterisk";
149         }
150        
151         if ( !isset($conf["AMPENGINE"]) || ($conf["AMPENGINE"] == "")) {
152                 $conf["AMPENGINE"] = "asterisk";
153         }
154
155         return $conf;
156 }
157
158 function install_parse_asterisk_conf($filename) {
159         $file = file($filename);
160         foreach ($file as $line) {
161                 if (preg_match("/^\s*([a-zA-Z0-9]+)\s* => \s*(.*)\s*([;#].*)?/",$line,$matches)) {
162                         $conf[ $matches[1] ] = $matches[2];
163                 }
164         }
165         // Now set defaults if not set (although asterisk should fail if not set but at least
166         // it will get the setup somewhat right
167         //
168         if (!isset($asterisk_conf['astetcdir']))    { $asterisk_conf['astetcdir']    = "/etc/asterisk"; }
169         if (!isset($asterisk_conf['astmoddir']))    { $asterisk_conf['astmoddir']    = "/usr/lib/asterisk/modules"; }
170         if (!isset($asterisk_conf['astvarlibdir'])) { $asterisk_conf['astvarlibdir'] = "/var/lib/asterisk"; }
171         if (!isset($asterisk_conf['astagidir']))    { $asterisk_conf['astagidir']    = "/var/lib/asterisk/agi-bin"; }
172         if (!isset($asterisk_conf['astspooldir']))  { $asterisk_conf['astspooldir']  = "/var/spool/asterisk"; }
173         if (!isset($asterisk_conf['astrundir']))    { $asterisk_conf['astrundir']    = "/var/run/asterisk"; }
174         if (!isset($asterisk_conf['astlogdir']))    { $asterisk_conf['astlogdir']    = "/var/log/asterisk"; }
175
176         return $conf;
177 }
178
179 function write_amportal_conf($filename, $conf) {
180         $file = file($filename);
181         // parse through the file
182         foreach (array_keys($file) as $key) {
183                 if (preg_match("/^\s*([a-zA-Z0-9]+)\s*=\s*(.*)\s*([;#].*)?/",$file[$key],$matches)) {
184                         // this is an option=value line
185                         if (isset($conf[ $matches[1] ])) {
186                                 // rewrite the line, if we have this in $conf
187                                 $file[$key] = $matches[1]."=".$conf[ $matches[1] ]."\n";
188                                 // unset it so we know what's new
189                                 unset($conf[ $matches[1] ]);
190                         }
191                 }
192         }
193        
194         // add new entries
195         foreach ($conf as $key=>$val) {
196                 $file[] = $key."=".$val."\n";
197         }
198        
199         // write the file
200         if (!$fd = fopen($filename, "w")) {
201                 fatal("Could not open ".$filename." for writing");
202         }
203         fwrite($fd, implode("",$file));
204         fclose($fd);
205 }
206
207 function ask_overwrite($file1, $file2) {
208         global $check_md5s;
209         do {
210                 out($file2." has been changed from the original version.");
211                 outn("Overwrite (y=yes/a=all/n=no/d=diff/s=shell/x=exit)? ");
212                 $key = fgets(STDIN,1024);
213                 switch (strtolower($key[0])) {
214                         case "y": return true;
215                         case "a": $check_md5s=false; return true;
216                         case "n": return false;
217                         case "d":
218                                 out("");
219                                 // w = ignore whitespace, u = unified
220                                 passthru("diff -wu ".escapeshellarg($file2)." ".escapeshellarg($file1));
221                         break;
222                         case "s":
223                                 if (function_exists("pcntl_fork")) {
224                                         out("");
225                                         $shell = (isset($_ENV["SHELL"]) ? $_ENV["SHELL"] : "/bin/bash");
226                                         out("Dropping to shell. Type 'exit' to return");
227                                         out("-> Original file:  ".$file2);
228                                         out("-> New file:       ".$file1);
229                                        
230                                         $pid = pcntl_fork();
231                                         if ($pid == -1) {
232                                                 out("[ERROR] cannot fork");
233                                         } else if ($pid) {
234                                                 // parent
235                                                 pcntl_waitpid($pid, $status);
236                                                 // we wait till the child exits/dies/whatever
237                                         } else {
238                                                 pcntl_exec($shell, array(), $_ENV);
239                                         }
240                                        
241                                         out("Returned from shell");
242                                 } else {
243                                         out("[ERROR] PHP not built with process control (--enable-pcntl) support: cannot spawn shell");
244                                 }
245                                
246                         break;
247                         case "x":
248                                 out("-> Original file:  ".$file2);
249                                 out("-> New file:       ".$file1);
250                                 out("Exiting install program.");
251                                 exit(1);
252                         break;
253                 }
254                 out("");
255         } while(1);
256 }
257
258 /** Write AMP-generated configuration files
259  */
260 function generate_configs() {
261         global $amp_conf;
262         global $dryrun;
263         global $debug;
264        
265         out("Generating Configurations.conf, (if Asterisk is not running, you will get an error");
266         out("In case of error, start Asterisk and hit the red bar in the GUI to generate the Configuraions.conf files");
267         if (!$dryrun)
268                 // added --run-install to make it work like it has been working since retrieve_conf changed to not run module install scripts by default
269
270                 //
271                 // TODO: Should check if Asterisk is running and/or try to start it.
272                 //
273                 passthru("su - asterisk -c \"".trim($amp_conf["AMPBIN"])."/retrieve_conf --run-install ".($debug ? ' --debug' : '').'"');
274 }
275
276
277 /** Set reload flag for AMP admin
278  */
279 function install_needreload() {
280         global $db;
281         $sql = "UPDATE admin SET value = 'true' WHERE variable = 'need_reload'";
282         $result = $db->query($sql);
283         if(DB::IsError($result)) {     
284                 die($result->getMessage());
285         }
286 }
287
288
289 /** Collect AMP settings
290  */
291 function collect_settings($filename, $dbhost = '', $dbuser = '', $dbpass = '', $dbname = 'asterisk') {
292         global $webroot;
293         global $fopwebroot;
294         global $ampsbin_dir;
295         global $ampbin_dir;
296         global $asterisk_user;
297         global $asterisk_pass;
298
299         out("Creating new $filename");
300        
301         outn("Enter your USERNAME to connect to the '$dbname' database:\n [".($dbuser ? $dbuser : $asterisk_user) . "] ");
302         $key = trim(fgets(STDIN,1024));
303         if (preg_match('/^$/',$key))
304                 $amp_conf["AMPDBUSER"] = ($dbuser ? $dbuser : $asterisk_user);
305         else
306                 $amp_conf["AMPDBUSER"] = $key;
307        
308         outn("Enter your PASSWORD to connect to the '$dbname' database:\n [".($dbpass ? $dbpass : $asterisk_pass)."] ");
309         $key = trim(fgets(STDIN,1024));
310         if (preg_match('/^$/',$key))
311                 $amp_conf["AMPDBPASS"] = ($dbpass ? $dbpass : $asterisk_pass);
312         else
313                 $amp_conf["AMPDBPASS"] = $key;
314        
315         outn("Enter the hostname of the '$dbname' database:\n [".($dbhost ? $dbhost : "localhost")."] ");
316         $key = trim(fgets(STDIN,1024));
317         if (preg_match('/^$/',$key))
318                 $amp_conf["AMPDBHOST"] = ($dbhost ? $dbhost : "localhost");
319         else
320                 $amp_conf["AMPDBHOST"] = $key;
321        
322         outn("Enter a USERNAME to connect to the Asterisk Manager interface:\n [admin] ");
323         $key = trim(fgets(STDIN,1024));
324         if (preg_match('/^$/',$key)) $amp_conf["AMPMGRUSER"] = "admin";
325         else $amp_conf["AMPMGRUSER"] = $key;
326        
327         outn("Enter a PASSWORD to connect to the Asterisk Manager interface:\n [amp111] ");
328         $key = trim(fgets(STDIN,1024));
329         if (preg_match('/^$/',$key)) $amp_conf["AMPMGRPASS"] = "amp111";
330         else $amp_conf["AMPMGRPASS"] = $key;
331        
332         do {
333                 out("Enter the path to use for your AMP web root:\n [$webroot] ");
334                 $key = trim(fgets(STDIN,1024));
335                 if (preg_match('/^$/',$key))
336                         $amp_conf["AMPWEBROOT"] = "$webroot";
337                 else
338                         $amp_conf["AMPWEBROOT"] = rtrim($key,'/');
339
340                 if (is_dir($amp_conf["AMPWEBROOT"])) {
341                         break;
342                 } else if (amp_mkdir($amp_conf["AMPWEBROOT"],"0755",true)){
343                         out("Created ".$amp_conf["AMPWEBROOT"]);
344                         break;
345                 } else {
346                         fatal("Cannot create ".$amp_conf["AMPWEBROOT"]."!");
347                 }
348         } while(1);
349        
350         // Really no need to ask, is there.
351         if (empty($fopwebroot))
352                 $amp_conf["FOPWEBROOT"] = $amp_conf["AMPWEBROOT"]."/panel";
353         else
354                 $amp_conf["FOPWEBROOT"] = $fopwebroot;
355        
356         outn("Enter the IP ADDRESS or hostname used to access the AMP web-admin:\n [xx.xx.xx.xx] ");
357         $key = trim(fgets(STDIN,1024));
358         if (preg_match('/^$/',$key)) $amp_conf["AMPWEBADDRESS"] = "xx.xx.xx.xx";
359         else $amp_conf["AMPWEBADDRESS"] = $key;
360        
361         outn("Enter a PASSWORD to perform call transfers with the Flash Operator Panel:\n [passw0rd] ");
362         $key = trim(fgets(STDIN,1024));
363         if (preg_match('/^$/',$key)) $amp_conf["FOPPASSWORD"] = "passw0rd";
364         else $amp_conf["FOPPASSWORD"] = $key;
365        
366         outn("Use simple Extensions [extensions] admin or separate Devices and Users [deviceanduser]?\n [extensions] ");
367         $key = trim(fgets(STDIN,1024));
368         if (preg_match('/^$/',$key)) $amp_conf["AMPEXTENSIONS"] = "extensions";
369         else $amp_conf["AMPEXTENSIONS"] = $key;
370        
371         do {
372                 out("Enter directory in which to store AMP executable scripts:\n [$ampbin_dir] ");
373                 $key = trim(fgets(STDIN,1024));
374                 if (preg_match('/^$/',$key))
375                         $amp_conf["AMPBIN"] = $ampbin_dir;
376                 else
377                         $amp_conf["AMPBIN"] = rtrim($key,'/');
378
379                 if (is_dir($amp_conf["AMPBIN"])) {
380                         break;
381                 } else if (amp_mkdir($amp_conf["AMPBIN"],"0755",true)){
382                         out("Created ".$amp_conf["AMPBIN"]);
383                         break;
384                 } else {
385                         fatal("Cannot create ".$amp_conf["AMPBIN"]."!");
386                 }
387         } while(1);
388        
389         do {
390                 out("Enter directory in which to store super-user scripts:\n [$ampsbin_dir] ");
391                 $key = trim(fgets(STDIN,1024));
392                 if (preg_match('/^$/',$key))
393                         $amp_conf["AMPSBIN"] = "$ampsbin_dir";
394                 else
395                         $amp_conf["AMPSBIN"] = rtrim($key,'/');
396
397                 if (is_dir($amp_conf["AMPSBIN"])) {
398                         break;
399                 } else if (amp_mkdir($amp_conf["AMPSBIN"],"0755",true)){
400                         out("Created ".$amp_conf["AMPSBIN"]);
401                         break;
402                 } else {
403                         fatal("Cannot create ".$amp_conf["AMPSBIN"]."!");
404                 }
405         } while(1);
406        
407         // write amportal.conf
408         write_amportal_conf($filename, $amp_conf);
409         outn(AMP_CONF." written");
410 }
411
412 /** Set base of packaged modules to the versions packaged in the tarball since they are
413  *  getting overwritten with the tarball from anything that may have been updated online.
414  *
415  */
416 function set_base_version() {
417         global $dryrun;
418
419         // read modules list from MODULE_DIR
420         //
421         $included_modules = array();
422         $dir = opendir(MODULE_DIR);
423         while ($file = readdir($dir)) {
424                 if ($file[0] != "." && $file[0] != "_" && is_dir(MODULE_DIR."/".$file)) {
425                         $included_modules[] = $file;
426                 }
427         }
428         closedir($dir);
429
430         foreach ($included_modules as $up_module) {
431                 outn("Checking $up_module.. ");
432                 if (!$dryrun) {
433                         out(set_module_version($up_module));
434                 } else {
435                         out("Dry Run Not Updated");
436                 }
437         }
438 }
439
440 /** Install all modules packaged with the install. We use the force flag because the
441  *  the assumption is that the dependencies are met and the package is able to have
442  *  the modules installed.
443  */
444 function install_modules() {
445         global $dryrun;
446         global $amp_conf;
447
448         // read modules list from MODULE_DIR
449         //
450         $included_modules = array();
451         $dir = opendir(MODULE_DIR);
452         while ($file = readdir($dir)) {
453                 if ($file[0] != "." && $file[0] != "_" && is_dir(MODULE_DIR."/".$file)) {
454                         $included_modules[] = $file;
455                 }
456         }
457         closedir($dir);
458
459         foreach ($included_modules as $up_module) {
460                 outn("Checking $up_module.. ");
461                 if (!$dryrun) {
462                         // special case framework, it should not be installed just enabled.
463                         //
464                         if ($up_module != 'framework') {
465                                 system($amp_conf['AMPBIN']."/module_admin --no-warnings -f install $up_module");
466                                 system($amp_conf['AMPBIN']."/module_admin --no-warnings -f enable $up_module");
467                                 out("installed");
468                         } else {
469                                 system($amp_conf['AMPBIN']."/module_admin --no-warnings -f enable $up_module");
470                                 out("enabled");
471                         }
472                 } else {
473                         out("Dry Run Not Installed");
474                 }
475         }
476 }
477
478 /** Set the module version number to the packaged version and enable
479  *  module must require not install.php or install.sql script
480  *  this is primarily to package core and framework with freepbx tarballs
481  *
482  */
483 function set_module_version($module) {
484         global $db;
485
486         $module_dir = MODULE_DIR;
487         $file_path = $module_dir.$module."/module.xml";
488         if (file_exists($file_path)) {
489                 // TODO: this is bad, there are other version tags (depends on) but this
490                 //       is equivalnet to what publish.pl does, so it expects this to be
491                 //       at the top.
492                 //
493                 $module_xml = file_get_contents($file_path);
494                 if (preg_match('/<version>(.+)<\/version>/', $module_xml, $matches)) {
495                         $version = $matches[1];
496                 } else {
497                         fatal("ERROR: $file_path found but no verison information");
498                 }
499         } else {
500                 return  "not packaged, no updating needed";
501         }
502
503         // If we didn't return above, then we found the package as part of the install
504         // tarball and want to update the version info since this might be overwriting
505         // an existing install that has a newever version.
506         //
507         $sql = "SELECT version FROM modules WHERE modulename = '$module'";
508         $result = $db->getCol($sql);
509         if(DB::IsError($result)) {     
510                 fatal("error accessing version table: ".$result->getMessage());
511         }
512         $sql = "";
513         if (count($result) == 0) {
514                 // insert but disable as we have to first run install scripts which come later
515                 $sql = "INSERT INTO modules (modulename, version, enabled) VALUES ('$module', '$version', 0)";
516         } else if ($result[0] != $version) {
517                 if (version_compare_freepbx($version, $result[0], "gt")) {
518                         // if new verison is greater than old, then we disable the module and it will get enabled next when installed
519                         //
520                         $sql = "UPDATE modules SET version = '$version', enabled = 0 WHERE modulename = '$module'";
521                 } else {
522                         // if new version is equal to or less than old, then we leave it in the enable/disable state it was in but just
523                         // reset the version number.
524                         //
525                         $sql = "UPDATE modules SET version = '$version' WHERE modulename = '$module'";
526                 }
527         }
528         if ($sql) {
529                 debug($sql);
530                 $result = $db->query($sql);
531                 if(DB::IsError($result)) {     
532                         fatal("error writing to version table: ".$result->getMessage());
533                 }
534                 return "updated to $version";
535         } else {
536                 return "already at $version";
537         }
538 }
539
540 /********************************************************************************************************************/
541
542 // **** Make sure we have STDIN etc
543
544 // from  ben-php dot net at efros dot com   at  php.net/install.unix.commandline
545 if (version_compare(phpversion(),'4.3.0','<') || !defined("STDIN")) {
546         define('STDIN',fopen("php://stdin","r"));
547         define('STDOUT',fopen("php://stdout","r"));
548         define('STDERR',fopen("php://stderr","r"));
549         register_shutdown_function( create_function( '' , 'fclose(STDIN); fclose(STDOUT); fclose(STDERR); return true;' ) );
550 }
551    
552 // **** Make sure we have PEAR's DB.php, and include it
553
554 outn("Checking for PEAR DB..");
555 if (! @ include('DB.php')) {
556         out("FAILED");
557         fatal("PEAR must be installed (requires DB.php). Include path: ".ini_get("include_path"));
558 }
559 out("OK");
560
561 // **** Make sure we have PEAR's GetOpts.php, and include it
562
563 outn("Checking for PEAR Console::Getopt..");
564 if (! @ include("Console/Getopt.php")) {
565         out("FAILED");
566         fatal("PEAR must be installed (requires Console/Getopt.php). Include path: ".ini_get("include_path"));
567 }
568 out("OK");
569
570 // **** Parse out command-line options
571 $shortopts = "h?u:p:";
572 $longopts = array("help","debug","dry-run","username=","password=","force-version=","dbhost=","no-files","dbname=","my-svn-is-correct","engine=","webroot=","install-moh","install-fop","make-links-devel","skip-module-install");
573
574 $args = Console_Getopt::getopt(Console_Getopt::readPHPArgv(), $shortopts, $longopts);
575 if (is_object($args)) {
576         // assume it's PEAR_ERROR
577         out($args->message);
578         exit(255);
579 }
580
581 $debug = false;
582 $dryrun = false;
583 $install_files = true;
584 $override_astvers = false;
585
586 $install_moh = false;
587 $install_fop = true;
588 $make_links = false;
589 $module_install = true;
590
591 //initialize variables to avoid php notices
592 $dbhost = null;
593 $dbname = null;
594 $new_username = null;
595 $new_password = null;
596
597 foreach ($args[0] as $arg) {
598         switch ($arg[0]) {
599                 case "--help": case "h": case "?":
600                         showHelp();
601                         exit(10);
602                 break;
603                 case "--dry-run":
604                         out("Dry-run only, nothing will be changed");
605                         $dryrun = true;
606                 break;
607                 case "--debug":
608                         $debug = true;
609                         debug("Debug mode enabled");
610                 break;
611                 case "--username": case "u":
612                         out("Using username: ".$arg[1]);
613                         $new_username = $arg[1];
614                 break;
615                 case "--password": case "p":
616                         out("Using password: ".str_repeat("*",strlen($arg[1])));
617                         $new_password = $arg[1];
618                 break;
619                 case "--force-version":
620                         $version = $arg[1];
621                         out("Forcing upgrade from version ".$version);
622                 break;
623                 case "--dbhost":
624                         $dbhost = $arg[1];
625                         out("Using remote database server at ".$dbhost);
626                 break;
627                 case "--dbname":
628                         $dbname = $arg[1];
629                         out("Using database ".$dbname);
630                 break;
631                 case "--no-files":
632                         $install_files = false;
633                         out("Running upgrade only, without installing files.");
634                 break;
635                 case "--my-svn-is-correct":
636                         $override_astvers = true;
637                 break;
638                 case "--engine":
639                         if ($arg[1] != 'asterisk' && $arg[1] != 'asterisk14' && $arg[1] != 'openpbx') {
640                                 fatal('Currently only "asterisk", "asterisk14" or "openpbx" are supported as a PBX engine');
641                         }
642                         $pbx_engine = $arg[1];
643                 break;
644                 case "--install-moh":
645                         $install_moh = true;
646                 break;
647                 case "--install-fop":
648                         if($arg[1] != "false")  {
649                                 $install_fop = true;
650                                 out("FOP will be installed and activated.");
651                         }
652                         else  {
653                                 out("FOP will be deactivated in the interface.  Set FOPDISABLE=false in amportal.conf to change later.");
654
655                         }
656
657                 break;
658                 case "--make-links-devel":
659                         $make_links = true;
660                 break;
661                 case "--skip-module-install":
662                         $module_install = false;
663                 break;
664                 case "--fopwebroot":
665                         $fopwebroot = $arg[1];
666                         out("Using fop at ".$fopwebroot);
667                 break;
668                 case "--webroot":
669                         $webroot = $arg[1];
670                         out("Using Webroot at ".$webroot);
671                 break;
672                 case "--cgibin":
673                         $cgibin = $arg[1];
674                         out("Using CGI-BIN at ".$cgibin);
675                 break;
676                 case "--bin":
677                         $ampbin_dir = $arg[1];
678                         out("Using bin at ".$ampbin_dir);
679                 break;
680                 case "--sbin":
681                         $ampsbin_dir = $arg[1];
682                         out("Using sbin ar ".$ampsbin_dir);
683                 break;
684                 case "--asteriskuser":
685                         $asterisk_user = $arg[1];
686                         out("Using Asterisk user ".$asterisk_user);
687                 break;
688                 case "--asteriskpass":
689                         $asterisk_pass = $arg[1];
690                         out("Using asteriskpass ".str_repeat("*",strlen($arg[1])));
691                 break;
692
693 /*              do we need this ?
694                 case "--systemconfig":
695                         $systemconfig = $arg[1];
696                         out("Using system config at ". $systemconfig);
697                 break;
698 */
699
700         }
701 }
702
703
704 // **** Look for user = root
705
706 outn("Checking user..");
707 //$current_user=(isset($_ENV["USER"]) ? $_ENV["USER"] : exec('whoami',$output));
708 $euid = (posix_getpwuid(posix_geteuid()));
709 $current_user = $euid['name'];
710 if ($current_user != "root"){
711         out("FAILED");
712         fatal($argv[0]." must be run as root");
713 }
714 out("OK");
715
716
717 outn("Checking if Asterisk is running..");
718 exec("pidof asterisk", $pid_val, $ret);
719 if ($ret) {
720         out("FAILED");
721         fatal($argv[0]."\n\tAsterisk must be running. If this is a first time install, you should start\n\tAsterisk by typing './start_asterisk start'\n\tFor upgrading, you should run 'amportal start'");
722 }
723 out("running with PID: ".$pid_val[0]."..OK");
724
725 // **** Check for amportal.conf, create if necessary
726
727 outn("Checking for ".AMP_CONF."..");
728 if (!file_exists(AMP_CONF)) {
729         out(AMP_CONF." does not exist, copying default");
730         copy("amportal.conf", AMP_CONF);
731
732         // this file contains password and should not be a+r
733         // this addresses http://freepbx.org/trac/ticket/1878
734         chown(AMP_CONF, "asterisk");
735         chgrp(AMP_CONF, "asterisk");
736         chmod(AMP_CONF, 0640);
737
738         collect_settings(AMP_CONF, $dbhost, $new_username, $new_password, 'asterisk');
739
740         out("Assuming new install, --install-moh added to command line");
741         $install_moh = true;
742 }
743 out("OK");
744
745 // **** read amportal.conf
746
747 outn("Reading ".AMP_CONF."..");
748 $amp_conf = install_parse_amportal_conf(AMP_CONF);
749 if (count($amp_conf) == 0) {
750         fatal("FAILED");
751 }
752 out("OK");
753
754 // Ensure our "critical" variables are set.  We absolutely need these to copy in files.
755
756 if (!array_key_exists("AMPWEBROOT",$amp_conf)) {
757         out("Adding AMPWEBROOT option to amportal.conf - using AMP default");
758         $amp_conf["AMPWEBROOT"] = "/var/www/html";
759 }
760
761 if (!array_key_exists("FOPWEBROOT",$amp_conf)) {
762         out("Adding FOPWEBROOT option to amportal.conf - using AMP default");
763         $amp_conf["FOPWEBROOT"] = $amp_conf["AMPWEBROOT"]."/panel";
764 }
765
766 if (!array_key_exists("AMPBIN",$amp_conf)) {
767         out("Adding AMPBIN option to amportal.conf - using AMP default");
768         $amp_conf["AMPBIN"] = "/var/lib/asterisk/bin";
769 }
770
771 if (!array_key_exists("AMPSBIN",$amp_conf)) {
772         out("Adding AMPSBIN option to amportal.conf - using AMP default");
773         $amp_conf["AMPSBIN"] = "/usr/sbin";
774 }
775
776 if (!array_key_exists("AMPDBENGINE",$amp_conf)) {
777         out("Adding AMPDBENGINE option to amportal.conf - using AMP default");
778         $amp_conf["AMPDBENGINE"] = "mysql";
779 }
780 if (!array_key_exists("AMPDBNAME",$amp_conf)) {
781         out("Adding AMPDBNAME option to amportal.conf - using AMP default");
782         $amp_conf["AMPDBNAME"] = "asterisk";
783 }
784
785 if (isset($new_username)) {
786         $amp_conf["AMPDBUSER"] = $new_username;
787 }
788
789 if (isset($new_password)) {
790         $amp_conf["AMPDBPASS"] = $new_password;
791 }
792
793 if (isset($dbhost)) {
794         $amp_conf["AMPDBHOST"] = $dbhost;
795 }
796
797 if (isset($dbname)) {
798         $amp_conf["AMPDBNAME"] = $dbname;
799 }
800 if(!$install_fop)  { // Set from --install-fop parameter; Add it to amportal.conf
801         out("Adding FOPDISABLE option to amportal.conf");
802         $amp_conf["FOPDISABLE"] = "true";
803 }
804 // If they pre-set this in their amportal.conf or this is an upgrade, we should honor it as well
805 //
806 if (isset($amp_conf['FOPDISABLE']) && (strtolower(trim($amp_conf['FOPDISABLE'])) == 'true' || strtolower(trim($amp_conf['FOPDISABLE'])) == 'yes' || strtolower(trim($amp_conf['FOPDISABLE'])) == 'y') )  {
807         $install_fop = false;
808 }       
809 // write amportal.conf
810 write_amportal_conf(AMP_CONF, $amp_conf);
811
812 // **** Check for amportal.conf, create if necessary
813
814 outn("Checking for ".ASTERISK_CONF."..");
815 if (!file_exists(ASTERISK_CONF)) {
816         out(ASTERISK_CONF." does not exist, copying default");
817         copy("asterisk.conf", ASTERISK_CONF);
818 }
819 out("OK");
820
821 // **** read asterisk.conf
822
823 outn("Reading ".ASTERISK_CONF."..");
824 $asterisk_conf = install_parse_asterisk_conf(ASTERISK_CONF);
825 if (count($asterisk_conf) == 0) {
826         fatal("FAILED. Have you installed Asterisk?");
827 }
828 out("OK");
829
830 /* deprecated on freepbx 2.2, from now pages need to read this information
831    from $asterik_conf and not $amp_conf.
832    
833    this code will stay in 2.2, but in 2.3 it will be gone. developers - please
834    update your code
835  */
836 if (isset($asterisk_conf['astetcdir'])) { $amp_conf['ASTETCDIR'] = $asterisk_conf['astetcdir']; }
837 if (isset($asterisk_conf['astmoddir'])) { $amp_conf['ASTMODDIR'] = $asterisk_conf['astmoddir']; }
838 if (isset($asterisk_conf['astvarlibdir'])) { $amp_conf['ASTVARLIBDIR'] = $asterisk_conf['astvarlibdir']; }
839 if (isset($asterisk_conf['astagidir'])) { $amp_conf['ASTAGIDIR'] = $asterisk_conf['astagidir']; }
840 if (isset($asterisk_conf['astspooldir'])) { $amp_conf['ASTSPOOLDIR'] = $asterisk_conf['astspooldir']; }
841 if (isset($asterisk_conf['astrundir'])) { $amp_conf['ASTRUNDIR'] = $asterisk_conf['astrundir']; }
842 if (isset($asterisk_conf['astlogdir'])) { $amp_conf['ASTLOGDIR'] = $asterisk_conf['astlogdir']; }
843
844 if (!isset($pbx_engine)) { $pbx_engine='asterisk'; }
845 out("Using $pbx_engine as PBX Engine");
846 $amp_conf["AMPENGINE"]=$pbx_engine;
847
848 write_amportal_conf(AMP_CONF, $amp_conf);
849
850
851 // **** Write asterisk version to ASTETCDIR/version
852
853 $tmpoutput = '';
854 $tmpout = exec("asterisk -V", $tmpoutput, $exitcode);
855 if ($exitcode != 0) {
856         fatal("Error executing asterisk: be sure Asterisk is installed and in the path");
857 }
858 if (!$fd = fopen($amp_conf['ASTETCDIR'].'/version','w')) {
859         fatal('Cannot open '.$amp_conf['ASTETCDIR'].'/version for writing');
860 }
861 fwrite($fd, $tmpout);
862 fclose($fd);
863 // change to read-only
864 chmod($amp_conf['ASTETCDIR'].'/version',0444);
865
866
867 // normally this would be the contents of ASTETCDIR/version, but this is for simplicity, as we just read it above
868 $verinfo = $tmpout;
869
870 // **** Check asterisk verison
871 //  Set the 'engine' to be 'asterisk14' if using asterisk 1.4, otherwise