root/freepbx/trunk/install_amp

Revision 12549, 43.5 kB (checked in by p_lindheimer, 5 months ago)

Merged revisions 12168,12170,12172,12183,12205,12221,12226-12229,12231,12235,12237,12250,12321,12357,12363,12467,12476,12529 via svnmerge from
http://www.freepbx.org/v2/svn/freepbx/branches/2.9

........

r12168 | GameGamer?43 | 2011-05-19 15:49:56 -0700 (Thu, 19 May 2011) | 1 line


closes #5160 - goes back to how things where done prior to r12066 with the exception that utility.functions.php is included where bootstrap-utility.functions.php used to be

........

r12170 | p_lindheimer | 2011-05-20 08:26:04 -0700 (Fri, 20 May 2011) | 1 line


remove 2.9.0.md5 checksum to regenerate 2.9.0 tag

........

r12172 | p_lindheimer | 2011-05-20 08:28:23 -0700 (Fri, 20 May 2011) | 1 line


Creating release 2.9.0

........

r12183 | p_lindheimer | 2011-05-20 16:32:04 -0700 (Fri, 20 May 2011) | 1 line


fixes #5138 make restrict_mods local and also detect and skip bootstrap if already called

........

r12205 | p_lindheimer | 2011-06-03 09:10:17 -0700 (Fri, 03 Jun 2011) | 1 line


fixed #5194 fix logic flaw so agi scripts can be copied after first module install

........

r12221 | mbrevda | 2011-06-07 08:32:39 -0700 (Tue, 07 Jun 2011) | 1 line


re #5209, write_freepbx should use AMPDBNAME for the db name, not AMPENGINE.

........

r12226 | p_lindheimer | 2011-06-09 15:23:24 -0700 (Thu, 09 Jun 2011) | 1 line


fixes #5217 don't return stripped version of xml to Module Admin

........

r12227 | p_lindheimer | 2011-06-09 15:29:41 -0700 (Thu, 09 Jun 2011) | 1 line


point to 2.9 modules re #5221 - need to rebuild tarball before closing

........

r12228 | p_lindheimer | 2011-06-09 15:53:54 -0700 (Thu, 09 Jun 2011) | 1 line


fixes #5212 if ASTAGIDIR is defined, chown even if it's already been done above through ASTVARLIBDIR...

........

r12229 | p_lindheimer | 2011-06-09 16:08:05 -0700 (Thu, 09 Jun 2011) | 1 line


fixes #5211 make amportal.conf and freepbx.conf 640 permission

........

r12231 | p_lindheimer | 2011-06-11 10:40:16 -0700 (Sat, 11 Jun 2011) | 1 line


closes #5226 include fw_ari and fw_fop in tarball

........

r12235 | p_lindheimer | 2011-06-11 12:17:07 -0700 (Sat, 11 Jun 2011) | 1 line


remove md5 hashes in preperation for re-generating 2.9.0 tarball because of 2.8 modules re #5221

........

r12237 | p_lindheimer | 2011-06-11 12:19:09 -0700 (Sat, 11 Jun 2011) | 1 line


Creating release 2.9.0

........

r12250 | p_lindheimer | 2011-06-11 12:25:01 -0700 (Sat, 11 Jun 2011) | 1 line


special case fw_ari and fw_fop during install_amp as framework re #5227

........

r12321 | mickecarlsson | 2011-07-26 07:43:52 -0700 (Tue, 26 Jul 2011) | 1 line


Closes #5280 revert some of r11047.

........

r12357 | xrobau | 2011-08-04 14:30:20 -0700 (Thu, 04 Aug 2011) | 1 line


Minor fixes for installer, and add the ability to symlink any asterisk path to elsewhere, and it'll still work.

........

r12363 | mickecarlsson | 2011-08-07 12:47:11 -0700 (Sun, 07 Aug 2011) | 1 line


Updated amp.pot for 2.9, should have been submitted a long time ago

........

r12467 | GameGamer?43 | 2011-08-30 15:43:20 -0700 (Tue, 30 Aug 2011) | 1 line


check if the readlink is valid before trying to chown & chmod directories to avoid displaying errors in the event the directories are not there or the user sets this to something else which isnt valid

........

r12476 | mickecarlsson | 2011-09-02 08:52:19 -0700 (Fri, 02 Sep 2011) | 1 line


Close #5348, updated Russian language. Thank you ded

........

r12529 | mickecarlsson | 2011-09-18 02:26:52 -0700 (Sun, 18 Sep 2011) | 1 line


Close #5352, double quotes in string for Advanced Settings should be single quotes and the string itself should be in double quotes.

........

  • 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 //  This file is part of FreePBX.
4 //
5 //    FreePBX is free software: you can redistribute it and/or modify
6 //    it under the terms of the GNU General Public License as published by
7 //    the Free Software Foundation, either version 2 of the License, or
8 //    (at your option) any later version.
9 //
10 //    FreePBX is distributed in the hope that it will be useful,
11 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //    GNU General Public License for more details.
14 //
15 //    You should have received a copy of the GNU General Public License
16 //    along with FreePBX.  If not, see <http://www.gnu.org/licenses/>.
17 //
18 //    Copyright 2006, Rob Thomas <xrobau@gmail.com>
19 //    Copyright 2009, Bandwith.com
20 //
21 require_once ("libfreepbx.install.php");
22 require_once(dirname(__FILE__) . '/amp_conf/htdocs/admin/libraries/php-upgrade.functions.php');
23 require_once(dirname(__FILE__) . '/amp_conf/htdocs/admin/libraries/sql.functions.php');
24 require_once(dirname(__FILE__) . '/amp_conf/htdocs/admin/libraries/utility.functions.php');
25
26 # constants
27 define("AMP_CONF", "/etc/amportal.conf");
28 define("ASTERISK_CONF", "/etc/asterisk/asterisk.conf");
29 define("UPGRADE_DIR", dirname(__FILE__)."/upgrades");
30 define("MODULE_DIR", dirname(__FILE__)."/amp_conf/htdocs/admin/modules/");
31
32 # semi constants
33 $webroot  = "/var/www/html";
34 $fopwebroot = "";       // if blank, will use $webroot/panel
35 $ampsbin_dir = "/usr/local/sbin"; // default if not set
36 $ampbin_dir = "/var/lib/asterisk/bin";
37 $asterisk_user  = "asteriskuser";
38 $asterisk_pass  = "amp109";
39
40 /********************************************************************************************************************/
41
42 function showHelp() {
43   out("Optional parameters:",false);
44   out("  --help, -h, -?           Show this help",false);
45   out("  --dbhost <ip address>    Use a remote database server",false);
46   out("  --dbname databasename    Use database name specified, instead of 'asterisk'",false);
47   out("  --username <user>        Use <user> to connect to db and write config",false);
48   out("  --password <pass>        Use <pass> to connect to db and write config",false);
49   out("  --fopwebroot <path>      Web path where fop will be installed",false);
50   out("  --webroot <path>         Web root where FreePBX will be installed",false);
51   out("  --cgibin <path>          Path where cgi-bin's lives",false);
52   out("  --bin <path>             Path of asterisk binaries",false);
53   out("  --sbin <path>            Path of system admin binaries",false);
54   out("  --asteriskuser <user>    Asterisk Manager username",false);
55   out("  --asteriskpass <pass>    Asterisk Manager password",false);
56   out("  --debug                  Enable debug output",false);
57   out("  --dry-run                Don't actually do anything",false);
58   out("  --force-version <ver>    Force upgrade from version <ver>",false);
59   out("  --skip-module-install    Don't run install scripts for packaged modules, the files are still loaded.",false);
60   out("                           In a development environment you may not want the install scripts run.",false);
61   out("  --no-files               Just run updates without installing files",false);
62   out("  --force-overwrite        Don't ask if it is ok to overwrite modified files, assume yes for all.",false);
63   out("  --install-moh            Install default music-on-hold files (normally doesn't, unless ",false);
64   out("                           it's a new installation)",false);
65   out("  --install-fop false      Don't install FOP and don't display it anywhere on the interface",false);
66   out("  --dev-links              Make links to files in the source directory instead of copying",false);
67   out("                           (intended for developers only)",false);
68   out("  --my-svn-is-correct      Ignore Asterisk version, assume it is correct",false);
69   out("  --engine <name>          Use the specified PBX Engine ('asterisk')",false);
70   out("  --uid <user>             Set ownership of files to user",false);
71   out("  --gid <group>            Set ownership of files to group",false);
72   out("  --scripted               Scripted install. Do not ask any questions.",false);
73   out("                           (Intended for unattended installers only)",false);
74 }
75
76 function install_parse_amportal_conf($filename) {
77   $file = file($filename);
78   foreach ($file as $line) {
79     if (preg_match("/^\s*([a-zA-Z0-9]+)\s*=\s*(.*)\s*([;#].*)?/",$line,$matches)) {
80       $conf[ $matches[1] ] = $matches[2];
81     }
82   }
83
84   // use same defaults as function.inc.php
85   if ( !isset($conf["AMPDBENGINE"]) || ($conf["AMPDBENGINE"] == "")) {
86     $conf["AMPDBENGINE"] = "mysql";
87   }
88  
89   if ( !isset($conf["AMPDBNAME"]) || ($conf["AMPDBNAME"] == "")) {
90     $conf["AMPDBNAME"] = "asterisk";
91   }
92  
93   if ( !isset($conf["AMPENGINE"]) || ($conf["AMPENGINE"] == "")) {
94     $conf["AMPENGINE"] = "asterisk";
95   }
96
97   return $conf;
98 }
99
100 function install_parse_asterisk_conf($filename) {
101   $file = file($filename);
102   foreach ($file as $line) {
103     if (preg_match("/^\s*([a-zA-Z0-9]+)\s* => \s*(.*)\s*([;#].*)?/",$line,$matches)) {
104       $conf[ $matches[1] ] = $matches[2];
105     }
106   }
107   // Now set defaults if not set (although asterisk should fail if not set but at least
108   // it will get the setup somewhat right
109   //
110   if (!isset($asterisk_conf['astetcdir']))    { $asterisk_conf['astetcdir']    = "/etc/asterisk"; }
111   if (!isset($asterisk_conf['astmoddir']))    { $asterisk_conf['astmoddir']    = "/usr/lib/asterisk/modules"; }
112   if (!isset($asterisk_conf['astvarlibdir'])) { $asterisk_conf['astvarlibdir'] = "/var/lib/asterisk"; }
113   if (!isset($asterisk_conf['astagidir']))    { $asterisk_conf['astagidir']    = "/var/lib/asterisk/agi-bin"; }
114   if (!isset($asterisk_conf['astspooldir']))  { $asterisk_conf['astspooldir']  = "/var/spool/asterisk"; }
115   if (!isset($asterisk_conf['astrundir']))    { $asterisk_conf['astrundir']    = "/var/run/asterisk"; }
116   if (!isset($asterisk_conf['astlogdir']))    { $asterisk_conf['astlogdir']    = "/var/log/asterisk"; }
117
118   return $conf;
119 }
120
121 function write_amportal_conf($filename, $conf) {
122   $file = file($filename);
123   // parse through the file
124   foreach (array_keys($file) as $key) {
125     if (preg_match("/^\s*([a-zA-Z0-9]+)\s*=\s*(.*)\s*([;#].*)?/",$file[$key],$matches)) {
126       // this is an option=value line
127       if (isset($conf[ $matches[1] ])) {
128         // rewrite the line, if we have this in $conf
129         $file[$key] = $matches[1]."=".$conf[ $matches[1] ]."\n";
130         // unset it so we know what's new
131         unset($conf[ $matches[1] ]);
132       }
133     }
134   }
135  
136   // add new entries
137   foreach ($conf as $key=>$val) {
138     $file[] = $key."=".$val."\n";
139   }
140  
141   // write the file
142   if (!$fd = fopen($filename, "w")) {
143     fatal("Could not open ".$filename." for writing");
144   }
145   fwrite($fd, implode("",$file));
146   fclose($fd);
147 }
148
149 function ask_overwrite($file1, $file2) {
150   global $check_md5s;
151   do {
152     out($file2." has been changed from the original version.");
153     outn("Overwrite (y=yes/a=all/n=no/d=diff/s=shell/x=exit)? ");
154     $key = fgets(STDIN,1024);
155     switch (strtolower($key[0])) {
156       case "y": return true;
157       case "a": $check_md5s=false; return true;
158       case "n": return false;
159       case "d":
160         out("");
161         // w = ignore whitespace, u = unified
162         passthru("diff -wu ".escapeshellarg($file2)." ".escapeshellarg($file1));
163       break;
164       case "s":
165         if (function_exists("pcntl_fork")) {
166           out("");
167           $shell = (isset($_ENV["SHELL"]) ? $_ENV["SHELL"] : "/bin/bash");
168           out("Dropping to shell. Type 'exit' to return");
169           out("-> Original file:  ".$file2);
170           out("-> New file:       ".$file1);
171          
172           $pid = pcntl_fork();
173           if ($pid == -1) {
174             out("[ERROR] cannot fork");
175           } else if ($pid) {
176             // parent
177             pcntl_waitpid($pid, $status);
178             // we wait till the child exits/dies/whatever
179           } else {
180             pcntl_exec($shell, array(), $_ENV);
181           }
182          
183           out("Returned from shell");
184         } else {
185           out("[ERROR] PHP not built with process control (--enable-pcntl) support: cannot spawn shell");
186         }
187        
188       break;
189       case "x":
190         out("-> Original file:  ".$file2);
191         out("-> New file:       ".$file1);
192         out("Exiting install program.");
193         exit(1);
194       break;
195     }
196     out("");
197   } while(1);
198 }
199
200 /** Write AMP-generated configuration files
201  */
202 function generate_configs() {
203   global $amp_conf;
204   global $dryrun;
205   global $debug;
206   global $runas_uid;
207  
208   out("Generating Configurations.conf, (if Asterisk is not running, you will get an error");
209   out("In case of error, start Asterisk and hit the red bar in the GUI to generate the Configurations.conf files");
210   if (!$dryrun)
211     // added --run-install to make it work like it has been working since retrieve_conf changed to not run module install scripts by default
212
213     //
214     // TODO: Should check if Asterisk is running and/or try to start it.
215     //
216     passthru("sudo -u $runas_uid ".trim($amp_conf["AMPBIN"])."/retrieve_conf --run-install --skip-registry-checks ".($debug ? ' --debug' : ''));
217 }
218
219
220 /** Collect AMP settings
221  */
222 function collect_settings($filename, $dbhost = '', $dbuser = '', $dbpass = '', $dbname = 'asterisk') {
223   global $webroot;
224   global $fopwebroot;
225   global $ampsbin_dir;
226   global $ampbin_dir;
227   global $asterisk_user;
228   global $asterisk_pass;
229   global $scripted;
230   global $freepbxip;
231   global $asteriskip;
232   global $runas_uid;
233   global $runas_gid;
234
235   out("Creating new $filename");
236  
237   if ($scripted == true) {
238     # Scripted install. Everything should have been set for us, don't ask questions.
239     out("Scripted install. Accepting command line params.");
240     $amp_conf["AMPDBUSER"] = $dbuser;
241     $amp_conf["AMPDBPASS"] = $dbpass;
242     $amp_conf["AMPDBHOST"] = $dbhost;
243     $amp_conf["AMPDBNAME"] = $dbname;
244     $amp_conf["AMPMGRUSER"] = "admin";
245     $amp_conf["AMPMGRPASS"] = "unset";
246     $amp_conf["AMPWEBROOT"] = $webroot;
247     $amp_conf["FOPWEBROOT"] = ($fopwebroot ? $fopwebroot : $webroot."/panel");
248     $amp_conf["AMPWEBADDRESS"] = $freepbxip;
249     $amp_conf["ASTMANAGERHOST"] = $asteriskip;
250     $amp_conf["FOPPASSWORD"] = "passw0rd";
251     $amp_conf["AMPEXTENSIONS"] = "extensions";
252     $amp_conf["AMPBIN"] = $ampbin_dir;
253     $amp_conf["AMPSBIN"] = "$ampsbin_dir";
254     $amp_conf["AMPASTERISKWEBUSER"] = $runas_uid;
255     $amp_conf["AMPASTERISKWEBGROUP"] = $runas_gid;
256     $amp_conf["AMPASTERISKUSER"] = $runas_uid;
257     $amp_conf["AMPASTERISKGROUP"] = $runas_gid;
258     $amp_conf["AMPDEVUSER"] = $runas_uid;
259     $amp_conf["AMPDEVGROUP"] = $runas_gid;
260     write_amportal_conf($filename, $amp_conf);
261     outn(AMP_CONF." written");
262     return;
263   }
264    
265   outn("Enter your USERNAME to connect to the '$dbname' database:\n [".($dbuser ? $dbuser : $asterisk_user) . "] ");
266   $key = trim(fgets(STDIN,1024));
267   if (preg_match('/^$/',$key))
268     $amp_conf["AMPDBUSER"] = ($dbuser ? $dbuser : $asterisk_user);
269   else
270     $amp_conf["AMPDBUSER"] = $key;
271  
272   outn("Enter your PASSWORD to connect to the '$dbname' database:\n [".($dbpass ? $dbpass : $asterisk_pass)."] ");
273   $key = trim(fgets(STDIN,1024));
274   if (preg_match('/^$/',$key))
275     $amp_conf["AMPDBPASS"] = ($dbpass ? $dbpass : $asterisk_pass);
276   else
277     $amp_conf["AMPDBPASS"] = $key;
278  
279   outn("Enter the hostname of the '$dbname' database:\n [".($dbhost ? $dbhost : "localhost")."] ");
280   $key = trim(fgets(STDIN,1024));
281   if (preg_match('/^$/',$key))
282     $amp_conf["AMPDBHOST"] = ($dbhost ? $dbhost : "localhost");
283   else
284     $amp_conf["AMPDBHOST"] = $key;
285  
286   outn("Enter a USERNAME to connect to the Asterisk Manager interface:\n [admin] ");
287   $key = trim(fgets(STDIN,1024));
288   if (preg_match('/^$/',$key)) $amp_conf["AMPMGRUSER"] = "admin";
289   else $amp_conf["AMPMGRUSER"] = $key;
290  
291   outn("Enter a PASSWORD to connect to the Asterisk Manager interface:\n [amp111] ");
292   $key = trim(fgets(STDIN,1024));
293   if (preg_match('/^$/',$key)) $amp_conf["AMPMGRPASS"] = "amp111";
294   else $amp_conf["AMPMGRPASS"] = $key;
295  
296   do {
297     out("Enter the path to use for your AMP web root:\n [$webroot] ");
298     $key = trim(fgets(STDIN,1024));
299     if (preg_match('/^$/',$key))
300       $amp_conf["AMPWEBROOT"] = "$webroot";
301     else
302       $amp_conf["AMPWEBROOT"] = rtrim($key,'/');
303
304     if (is_dir($amp_conf["AMPWEBROOT"])) {
305       break;
306     } else if (amp_mkdir($amp_conf["AMPWEBROOT"],"0755",true)){
307       out("Created ".$amp_conf["AMPWEBROOT"]);
308       break;
309     } else {
310       fatal("Cannot create ".$amp_conf["AMPWEBROOT"]."!");
311     }
312   } while(1);
313  
314   // Really no need to ask, is there.
315   if (empty($fopwebroot))
316     $amp_conf["FOPWEBROOT"] = $amp_conf["AMPWEBROOT"]."/panel";
317   else
318     $amp_conf["FOPWEBROOT"] = $fopwebroot;
319  
320   outn("Enter the IP ADDRESS or hostname used to access the AMP web-admin:\n [$freepbxip] ");
321   $key = trim(fgets(STDIN,1024));
322   if (preg_match('/^$/',$key)) $amp_conf["AMPWEBADDRESS"] = $freepbxip;
323   else $amp_conf["AMPWEBADDRESS"] = $key;
324  
325   outn("Enter a PASSWORD to perform call transfers with the Flash Operator Panel:\n [passw0rd] ");
326   $key = trim(fgets(STDIN,1024));
327   if (preg_match('/^$/',$key)) $amp_conf["FOPPASSWORD"] = "passw0rd";
328   else $amp_conf["FOPPASSWORD"] = $key;
329  
330   outn("Use simple Extensions [extensions] admin or separate Devices and Users [deviceanduser]?\n [extensions] ");
331   $key = trim(fgets(STDIN,1024));
332   if (preg_match('/^$/',$key)) $amp_conf["AMPEXTENSIONS"] = "extensions";
333   else $amp_conf["AMPEXTENSIONS"] = $key;
334  
335   do {
336     out("Enter directory in which to store AMP executable scripts:\n [$ampbin_dir] ");
337     $key = trim(fgets(STDIN,1024));
338     if (preg_match('/^$/',$key))
339       $amp_conf["AMPBIN"] = $ampbin_dir;
340     else
341       $amp_conf["AMPBIN"] = rtrim($key,'/');
342
343     if (is_dir($amp_conf["AMPBIN"])) {
344       break;
345     } else if (amp_mkdir($amp_conf["AMPBIN"],"0755",true)){
346       out("Created ".$amp_conf["AMPBIN"]);
347       break;
348     } else {
349       fatal("Cannot create ".$amp_conf["AMPBIN"]."!");
350     }
351   } while(1);
352  
353   do {
354     out("Enter directory in which to store super-user scripts:\n [$ampsbin_dir] ");
355     $key = trim(fgets(STDIN,1024));
356     if (preg_match('/^$/',$key))
357       $amp_conf["AMPSBIN"] = "$ampsbin_dir";
358     else
359       $amp_conf["AMPSBIN"] = rtrim($key,'/');
360
361     if (is_dir($amp_conf["AMPSBIN"])) {
362       break;
363     } else if (amp_mkdir($amp_conf["AMPSBIN"],"0755",true)){
364       out("Created ".$amp_conf["AMPSBIN"]);
365       break;
366     } else {
367       fatal("Cannot create ".$amp_conf["AMPSBIN"]."!");
368     }
369   } while(1);
370  
371   // write amportal.conf
372   write_amportal_conf($filename, $amp_conf);
373   outn(AMP_CONF." written");
374 }
375
376 /** Set base of packaged modules to the versions packaged in the tarball since they are
377  *  getting overwritten with the tarball from anything that may have been updated online.
378  *
379  */
380 function set_base_version() {
381   global $dryrun;
382
383   // read modules list from MODULE_DIR
384   //
385   $included_modules = array();
386   $dir = opendir(MODULE_DIR);
387   while ($file = readdir($dir)) {
388     if ($file[0] != "." && $file[0] != "_" && is_dir(MODULE_DIR."/".$file)) {
389       $included_modules[] = $file;
390     }
391   }
392   closedir($dir);
393
394   foreach ($included_modules as $up_module) {
395     outn("Checking $up_module.. ");
396     if (!$dryrun) {
397       out(set_module_version($up_module));
398     } else {
399       out("Dry Run Not Updated");
400     }
401   }
402 }
403
404 /** Install all modules packaged with the install. We use the force flag because the
405  *  the assumption is that the dependencies are met and the package is able to have
406  *  the modules installed.
407  */
408 function install_modules() {
409   global $dryrun;
410   global $amp_conf;
411
412   // read modules list from MODULE_DIR
413   //
414   $included_modules = array();
415   $dir = opendir(MODULE_DIR);
416   while ($file = readdir($dir)) {
417     if ($file[0] != "." && $file[0] != "_" && is_dir(MODULE_DIR."/".$file)) {
418       $included_modules[] = $file;
419     }
420   }
421   closedir($dir);
422
423   $output = array();
424   $keep_checking = true;
425   $num_modules = false;
426   while ($keep_checking && count($included_modules)) {
427     if ($num_modules === count($included_modules)) {
428       $keep_checking = false;
429     } else {
430       $num_modules = count($included_modules);
431     }
432     foreach ($included_modules as $id => $up_module) {
433       outn("Checking $up_module.. ");
434       if (!$dryrun) {
435         // if $keep_checking then check dependencies first and skip if not met
436         // otherwise we will install anyhow even if some dependencies are not met
437         // since it is included. (TODO: should we not?)
438         //
439         if ($keep_checking) {
440           exec($amp_conf['AMPBIN']."/module_admin --no-warnings checkdepends $up_module",$output,$retval);
441           unset($output);
442           if ($retval) {
443             out("dependencies pending");
444             continue;
445           }
446         }
447         // Framework modules cannot be enabled, only installed.
448         //
449         switch ($up_module) {
450         case 'framework':
451         case 'fw_ari':
452         case 'fw_fop':
453           system($amp_conf['AMPBIN']."/module_admin --no-warnings -f install $up_module");
454           out("installed");
455           break;
456         default:
457           system($amp_conf['AMPBIN']."/module_admin --no-warnings -f install $up_module");
458           system($amp_conf['AMPBIN']."/module_admin --no-warnings -f enable $up_module");
459           out("installed");
460         }
461         unset($included_modules[$id]);
462       } else {
463         out("Dry Run Not Installed");
464       }
465     }
466     }
467 }
468
469 /** Set the module version number to the packaged version and enable
470  *  module must require not install.php or install.sql script
471  *  this is primarily to package core and framework with FreePBX tarballs
472  *
473  */
474 function set_module_version($module) {
475   global $db;
476
477   $module_dir = MODULE_DIR;
478   $file_path = $module_dir.$module."/module.xml";
479   if (file_exists($file_path)) {
480     // TODO: this is bad, there are other version tags (depends on) but this
481     //       is equivalnet to what publish.pl does, so it expects this to be
482     //       at the top.
483     //
484     $module_xml = file_get_contents($file_path);
485     if (preg_match('/<version>(.+)<\/version>/', $module_xml, $matches)) {
486       $version = $matches[1];
487     } else {
488       fatal("ERROR: $file_path found but no version information");
489     }
490   } else {
491     return  "not packaged, no updating needed";
492   }
493
494   // If we didn't return above, then we found the package as part of the install
495   // tarball and want to update the version info since this might be overwriting
496   // an existing install that has a newer version.
497   //
498   $sql = "SELECT version FROM modules WHERE modulename = '$module'";
499   $result = $db->getCol($sql);
500   if(DB::IsError($result)) {     
501     fatal("error accessing version table: ".$result->getMessage());
502   }
503   $sql = "";
504   if (count($result) == 0) {
505     // insert but disable as we have to first run install scripts which come later
506     $sql = "INSERT INTO modules (modulename, version, enabled) VALUES ('$module', '$version', 0)";
507   } else if ($result[0] != $version) {
508     if (version_compare_freepbx($version, $result[0], "gt")) {
509       // if new version is greater than old, then we disable the module and it will get enabled next when installed
510       //
511       $sql = "UPDATE modules SET version = '$version', enabled = 0 WHERE modulename = '$module'";
512     } else {
513       // if new version is equal to or less than old, then we leave it in the enable/disable state it was in but just
514       // reset the version number.
515       //
516       $sql = "UPDATE modules SET version = '$version' WHERE modulename = '$module'";
517     }
518   }
519   if ($sql) {
520     debug($sql);
521     $result = $db->query($sql);
522     if(DB::IsError($result)) {     
523       fatal("error writing to version table: ".$result->getMessage());
524     }
525     return "updated to $version";
526   } else {
527     return "already at $version";
528   }
529 }
530
531 /********************************************************************************************************************/
532
533 //
534 // TODO: will this work, basically using bootstrap once installed if using to upgrade, etc?
535 /*
536 $bootstrap_settings['skip_astman']) = true;
537 $bootstrap_settings['freepbx_auth'] = false;
538 $restrict_mods = true;
539 if (@include_once(getenv('FREEPBX_CONF') ? getenv('FREEPBX_CONF') : '/etc/freepbx.conf')) {
540   out(_("FreePBX already installed, bootstrapping"));
541 } else if (@include_once('/etc/asterisk/freepbx.conf')) {
542   out(_("FreePBX already installed, bootstrapping"));
543 } else {
544   // all the initialization here instead
545 }
546 */
547
548 // **** Make sure we have STDIN etc
549
550 // from  ben-php dot net at efros dot com   at  php.net/install.unix.commandline
551 if (version_compare(phpversion(),'4.3.0','<') || !defined("STDIN")) {
552   define('STDIN',fopen("php://stdin","r"));
553   define('STDOUT',fopen("php://stdout","r"));
554   define('STDERR',fopen("php://stderr","r"));
555   register_shutdown_function( create_function( '' , 'fclose(STDIN); fclose(STDOUT); fclose(STDERR); return true;' ) );
556 }
557    
558 // **** Make sure we have PEAR's DB.php, and include it
559
560 outn("Checking for PEAR DB..");
561 if (! @ include('DB.php')) {
562   out("FAILED");
563   fatal("PEAR must be installed (requires DB.php). Include path: ".ini_get("include_path"));
564 }
565 out("OK");
566
567 // **** Make sure we have PEAR's GetOpts.php, and include it
568
569 outn("Checking for PEAR Console::Getopt..");
570 if (! @ include("Console/Getopt.php")) {
571   out("FAILED");
572   fatal("PEAR must be installed (requires Console/Getopt.php). Include path: ".ini_get("include_path"));
573 }
574 out("OK");
575
576 // **** Parse out command-line options
577 $shortopts = "h?u:p:";
578 $longopts = array("help","debug","dry-run","username=","password=","force-version=","dbhost=","no-files","force-overwrite","dbname=","my-svn-is-correct","engine=","webroot=","install-moh","install-fop=","make-links-devel","dev-links","skip-module-install","uid=","gid=","scripted","freepbxip=","asteriskip=");
579
580 $args = Console_Getopt::getopt(Console_Getopt::readPHPArgv(), $shortopts, $longopts);
581 if (is_object($args)) {
582   // assume it's PEAR_ERROR
583   out($args->message);
584   exit(255);
585 }
586
587 $debug = false;
588 $dryrun = false;
589 $install_files = true;
590 $override_astvers = false;
591 $scripted = false;
592
593 $install_moh = false;
594 $install_fop = true;
595 $make_links = false;
596 $module_install = true;
597
598 //initialize variables to avoid php notices
599 $dbhost = null;
600 $dbname = 'asterisk';
601 $new_username = null;
602 $new_password = null;
603
604 $runas_uid = "asterisk";
605 $runas_gid = "asterisk";
606 $freepbxip = "192.168.1.1";
607 $asteriskip = "localhost";
608
609 foreach ($args[0] as $arg) {
610   switch ($arg[0]) {
611     case "--help": case "h": case "?":
612       showHelp();
613       exit(10);
614     break;
615     case "--dry-run":
616       out("Dry-run only, nothing will be changed");
617       $dryrun = true;
618     break;
619     case "--debug":
620       $debug = true;
621       debug("Debug mode enabled");
622     break;
623     case "--username": case "u":
624       out("Using username: ".$arg[1]);
625       $new_username = $arg[1];
626     break;
627     case "--password": case "p":
628       out("Using password: ".str_repeat("*",strlen($arg[1])));
629       $new_password = $arg[1];
630     break;
631     case "--force-version":
632       $version = $arg[1];
633       out("Forcing upgrade from version ".$version);
634     break;
635     case "--dbhost":
636       $dbhost = $arg[1];
637       out("Using remote database server at ".$dbhost);
638     break;
639     case "--dbname":
640       $dbname = $arg[1];
641       out("Using database ".$dbname);
642     break;
643     case "--no-files":
644       $install_files = false;
645       out("Running upgrade only, without installing files.");
646     break;
647     case "--force-overwrite":
648       $check_md5s = false;
649       out("Overwriting all files including modified ones.");
650     break;
651     case "--my-svn-is-correct":
652       $override_astvers = true;
653     break;
654     case "--engine":
655       if ($arg[1] != 'asterisk') {
656         fatal('Currently only "asterisk" is supported as a PBX engine');
657       }
658       $pbx_engine = $arg[1];
659     break;
660     case "--install-moh":
661       $install_moh = true;
662     break;
663     case "--install-fop":
664       if(strtolower($arg[1]) == "false")  {
665         $install_fop = false;
666         out("FOP will be deactivated in the interface.  Set FOPDISABLE=false in Advanced Settings to change later.");
667       } else  {
668         out("FOP will be installed and activated.");
669       }
670     break;
671     case "--make-links-devel":
672     case "--dev-links":
673       $make_links = true;
674     break;
675     case "--skip-module-install":
676       $module_install = false;
677     break;
678     case "--fopwebroot":
679       $fopwebroot = $arg[1];
680       out("Using fop at ".$fopwebroot);
681     break;
682     case "--webroot":
683       $webroot = $arg[1];
684       out("Using Webroot at ".$webroot);
685     break;
686     case "--cgibin":
687       $cgibin = $arg[1];
688       out("Using CGI-BIN at ".$cgibin);
689     break;
690     case "--bin":
691       $ampbin_dir = $arg[1];
692       out("Using bin at ".$ampbin_dir);
693     break;
694     case "--sbin":
695       $ampsbin_dir = $arg[1];
696       out("Using sbin at ".$ampsbin_dir);
697     break;
698     case "--asteriskuser":
699       $asterisk_user = $arg[1];
700       out("Using Asterisk user ".$asterisk_user);
701     break;
702     case "--asteriskpass":
703       $asterisk_pass = $arg[1];
704       out("Using asteriskpass ".str_repeat("*",strlen($arg[1])));
705     break;
706     case "--uid":
707       $runas_uid = $arg[1];
708       out("Setting ownership (user) to ".$runas_uid);
709     break;
710     case "--gid":
711       $runas_gid = $arg[1];
712       out("Setting ownership (group) to ".$runas_gid);
713     break;
714     case "--scripted":
715       $scripted = true;
716       out("Unattended installation. No questions will be asked");
717     break;
718     case "--freepbxip":
719       $freepbxip = $arg[1];
720       out("Setting IP address of web interface to $freepbxip");
721     break;
722     case "--asteriskip":
723       $asteriskip = $arg[1];
724       out("Setting IP address of asterisk to $asteriskip");
725     break;
726   }
727 }
728
729
730 // **** Look for user = root
731
732 // TODO: if we bootstrap, do this first before even trying. Also, is their a problem boostraping as user root???
733 //
734 outn("Checking user..");
735 //$current_user=(isset($_ENV["USER"]) ? $_ENV["USER"] : exec('whoami',$output));
736 $euid = (posix_getpwuid(posix_geteuid()));
737 $current_user = $euid['name'];
738 if ($current_user != "root"){
739   out("FAILED");
740   fatal($argv[0]." must be run as root");
741 }
742 out("OK");
743
744
745 // TODO: if we bootstrap, do this first before even trying.
746 //
747 outn("Checking if Asterisk is running..");
748 exec("pidof asterisk", $pid_val, $ret);
749 if ($ret) {
750   out("FAILED");
751   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'");
752 }
753 out("running with PID: ".$pid_val[0]."..OK");
754
755 // **** Check for amportal.conf, create if necessary
756
757 outn("Checking for ".AMP_CONF."..");
758 if (!file_exists(AMP_CONF)) {
759   out(AMP_CONF." does not exist, copying default");
760   copy("amportal.conf", AMP_CONF);
761
762   // this file contains password and should not be a+r
763   // this addresses http://freepbx.org/trac/ticket/1878
764   chown(AMP_CONF, $runas_uid);
765   chgrp(AMP_CONF, $runas_gid);
766   chmod(AMP_CONF, 0640);
767
768   collect_settings(AMP_CONF, $dbhost, $new_username, $new_password, $dbname);
769
770   out("Assuming new install, --install-moh added to command line");
771   $install_moh = true;
772 }
773 out("OK");
774
775 // **** read amportal.conf
776
777 // TODO: see comments above, have we already boostraped, if so, skip this?
778 //
779 outn("Reading ".AMP_CONF."..");
780 $amp_conf = install_parse_amportal_conf(AMP_CONF);
781 if (count($amp_conf) == 0) {
782   fatal("FAILED");
783 }
784 out("OK");
785
786 // TODO: if we boostrapped I think we can count on ALL of these being set but maybe it's
787 //       fine to do a sanity check?
788 //
789 // Ensure our "critical" variables are set.  We absolutely need these to copy in files.
790
791 if (!array_key_exists("AMPWEBROOT",$amp_conf)) {
792   out("Adding AMPWEBROOT option to amportal.conf - using AMP default");
793   $amp_conf["AMPWEBROOT"] = "/var/www/html";
794 }
795 if (!array_key_exists("FOPWEBROOT",$amp_conf)) {
796   out("Adding FOPWEBROOT option to amportal.conf - using AMP default");
797   $amp_conf["FOPWEBROOT"] = $amp_conf["AMPWEBROOT"]."/panel";
798 }
799 if (!array_key_exists("AMPBIN",$amp_conf)) {
800   out("Adding AMPBIN option to amportal.conf - using AMP default");
801   $amp_conf["AMPBIN"] = "/var/lib/asterisk/bin";
802 }
803 if (!array_key_exists("AMPSBIN",$amp_conf)) {
804   out("Adding AMPSBIN option to amportal.conf - using AMP default");
805   $amp_conf["AMPSBIN"] = "/usr/sbin";
806 }
807 if (!array_key_exists("AMPDBENGINE",$amp_conf)) {
808   out("Adding AMPDBENGINE option to amportal.conf - using AMP default");
809   $amp_conf["AMPDBENGINE"] = "mysql";
810 }
811 if (!array_key_exists("AMPDBNAME",$amp_conf)) {
812   out("Adding AMPDBNAME option to amportal.conf - using AMP default");
813   $amp_conf["AMPDBNAME"] = "asterisk";
814 }
815
816 // TODO: if we boostrapped do we ignore these? (Can they even be set?)
817 //
818 if (isset($new_username)) {
819   $amp_conf["AMPDBUSER"] = $new_username;
820 }
821 if (isset($new_password)) {
822   $amp_conf["AMPDBPASS"] = $new_password;
823 }
824 if (isset($dbhost)) {
825   $amp_conf["AMPDBHOST"] = $dbhost;
826 }
827 if (isset($dbname)) {
828   $amp_conf["AMPDBNAME"] = $dbname;
829 }
830 if(!$install_fop)  { // Set from --install-fop parameter; Add it to amportal.conf
831   out("Adding FOPDISABLE option to amportal.conf");
832   $amp_conf["FOPDISABLE"] = "true";
833 }
834 // If they pre-set this in their amportal.conf or this is an upgrade, we should honor it as well
835 //
836 // TODO: if we boostrap, then this value could be normalized (1/0 I think?)
837 //
838 if (isset($amp_conf['FOPDISABLE']) && (strtolower(trim($amp_conf['FOPDISABLE'])) == 'true' || strtolower(trim($amp_conf['FOPDISABLE'])) == 'yes' || strtolower(trim($amp_conf['FOPDISABLE'])) == 'y') )  {
839   $install_fop = false;
840 }
841 // write amportal.conf
842 // TODO: if we boostrapped we don't want to write amportal.conf, or do we? e.g. if we are running out of the file vs. the
843 //       database then we probably want to write, though there should be a freepbx_conf class to use that does that and knows
844 //       how to make that decsion as well as what to write?
845 //
846 write_amportal_conf(AMP_CONF, $amp_conf);
847
848 // **** Check for amportal.conf, create if necessary
849
850 outn("Checking for ".ASTERISK_CONF."..");
851 if (!file_exists(ASTERISK_CONF)) {
852   out(ASTERISK_CONF." does not exist, copying default");
853   copy("asterisk.conf", ASTERISK_CONF);
854 }
855 out("OK");
856
857 // **** read asterisk.conf
858
859 // TODO: if we boostrapped don't do this
860 //
861 outn("Reading ".ASTERISK_CONF."..");
862 $asterisk_conf = install_parse_asterisk_conf(ASTERISK_CONF);
863 if (count($asterisk_conf) == 0) {
864   fatal("FAILED. Have you installed Asterisk?");
865 }
866 out("OK");
867
868 /* TODO: if we boostrapped this should have been done but doesn't hurt?
869  */
870 if (isset($asterisk_conf['astetcdir'])) { $amp_conf['ASTETCDIR'] = $asterisk_conf['astetcdir']; }
871 if (isset($asterisk_conf['astmoddir'])) { $amp_conf['ASTMODDIR'] = $asterisk_conf['astmoddir']; }
872 if (isset($asterisk_conf['astvarlibdir'])) { $amp_conf['ASTVARLIBDIR'] = $asterisk_conf['astvarlibdir']; }
873 if (isset($asterisk_conf['astagidir'])) { $amp_conf['ASTAGIDIR'] = $asterisk_conf['astagidir']; }
874 if (isset($asterisk_conf['astspooldir'])) { $amp_conf['ASTSPOOLDIR'] = $asterisk_conf['astspooldir']; }
875 if (isset($asterisk_conf['astrundir'])) { $amp_conf['ASTRUNDIR'] = $asterisk_conf['astrundir']; }
876 if (isset($asterisk_conf['astlogdir'])) { $amp_conf['ASTLOGDIR'] = $asterisk_conf['astlogdir']; }
877
878 if (!isset($pbx_engine)) { $pbx_engine='asterisk'; }
879 out("Using $pbx_engine as PBX Engine");
880 $amp_conf["AMPENGINE"]=$pbx_engine;
881
882 // TODO: I guess we have new variables set, of course see note above if we boostrapped. Probably
883 //       this one stays?
884 //
885 write_amportal_conf(AMP_CONF, $amp_conf);
886
887
888 // **** Write asterisk version to ASTETCDIR/version
889
890 // TODO: does anything use this? What's the point? (other than we need the version for below)
891 //
892 $tmpoutput = '';
893 $tmpout = exec("asterisk -V", $tmpoutput, $exitcode);
894 if ($exitcode != 0) {
895   fatal("Error executing asterisk: be sure Asterisk is installed and in the path");
896 }
897 if (!$fd = fopen($amp_conf['ASTETCDIR'].'/version','w')) {
898   fatal('Cannot open '.$amp_conf['ASTETCDIR'].'/version for writing');
899 }
900 fwrite($fd, $tmpout);
901 fclose($fd);
902 // change to read-only
903 chmod($amp_conf['ASTETCDIR'].'/version',0444);
904
905
906 // normally this would be the contents of ASTETCDIR/version, but this is for simplicity, as we just read it above
907 $verinfo = $tmpout;
908
909 // **** Check asterisk version
910 //  Set the 'engine' to be 'asterisk14' if using asterisk 1.4, otherwise
911 //  'asterisk'
912 outn("Checking for Asterisk version..");
913 if ((preg_match('/^Asterisk (\d+(\.\d+)*)(-?(.*))$/', $verinfo, $matches)) ||
914     (preg_match('/^Asterisk SVN-(\d+(\.\d+)*)(-?(.*))$/', $verinfo, $matches))) {
915   if ((version_compare($matches[1], "1.2") < 0)) {
916     fatal("Asterisk 1.2, 1.4, 1.6, or 1.8 is required for this version of FreePBX. Detected version is: ".$matches[1]);
917   }
918   if (version_compare($matches[1], "1.9", "ge")) {
919     fatal("Asterisk 1.2, 1.4, 1.6, or 1.8 is required for this version of FreePBX. Detected version is: ".$matches[1]);
920   }
921   out("{$matches[1]}");
922   $asterisk_version = $matches[1];
923
924 } elseif (preg_match('/^Asterisk ([ABC]\.\d+(\.\d+)*)(-?(.*))$/', $verinfo, $matches)) {
925   if (substr($matches[1], 0, 1) == "A") {
926     fatal("Asterisk Business Edition B or C is required for this version of FreePBX. Detected version is: ".$matches[1]);
927   }
928   out("{$matches[1]}");
929   $asterisk_version = '1.4'; //Not really, but this makes sure that proper execution occurs later on reload commands, etc.
930
931 } elseif ($asterisk_version = preg_match('/^Asterisk SVN.+/', $verinfo)) {
932   out("FAIL");
933   out("*** WARNING ***");
934   out("You are not using a released version of Asterisk. We are unable to verify");
935   out("that your Asterisk version is compatible with FreePBX. Whilst this probably");
936   out("won't cause any problems, YOU NEED TO BE CERTAIN that it is compatible");
937   out("with at least the released Asterisk version 1.2" );
938   if ($override_astvers==false) {
939     out("If you are SURE that this is compatible, you can re-run ".$argv[0]." with");
940     out("the parameter --my-svn-is-correct");
941     exit;
942   } else {
943     out("--my-svn-is-correct specified, continuing");
944   }
945 } else {
946   fatal("Could not determine asterisk version (got: \"".$verinfo."\" please report this)");
947 }
948
949 // **** Make sure selinux isn't enabled
950
951 outn("Checking for selinux..");
952 $tmpoutput = array();
953 $tmpout = exec("getenforce 2>/dev/null", $tmpoutput, $sereturn);
954 if (strtolower($tmpoutput[0]) === "enabled") {
955         // this method seems better because disabled and permissive are the same
956         // if a user installs and realizes selinux is running the other method
957         // requires a reboot to get selinuxenabled to work after editing the  selinux config
958         // this will allow you to use setenforce 0 which turns selinux into permissive mode which
959         // doesnt enforce, it just warns.
960   fatal("selinux is ENABLED. This is not supported. Please disable selinux before using FreePBX");
961 }
962 out("OK");
963
964 // **** Connect to database
965
966 // TODO: if we bootstrapped, we are already connected and this can all be skipped
967 //
968 outn("Connecting to database..");
969
970 $db_engine = $amp_conf["AMPDBENGINE"];
971 if ($db_engine != "sqlite3") {
972   $db_user = $amp_conf["AMPDBUSER"];
973   $db_pass = $amp_conf["AMPDBPASS"];
974   $db_host = $amp_conf["AMPDBHOST"];
975 }
976 $db_name = $amp_conf["AMPDBNAME"];
977
978 // we still support older configurations,  and fall back
979 // into mysql when no other engine is defined
980 if ($db_engine == "")
981 {
982   $db_engine = "mysql";
983 }
984  
985 switch ($db_engine)
986 {
987   case "pgsql":
988   case "mysql":
989     // datasource in in this style: dbengine://username:password@host/database
990     if (!function_exists($db_engine.'_connect')) {
991       out("FAILED");
992       fatal($db_engine." PHP libraries not installed");
993     }
994  
995     $datasource = $db_engine.'://'.$db_user.':'.$db_pass.'@'.$db_host.'/'.$db_name;
996     $db = DB::connect($datasource); // attempt connection
997     break;
998  
999   case "sqlite":
1000     die_freepbx("SQLite2 support is deprecated. Please use sqlite3 only.");
1001     break;
1002  
1003   case "sqlite3":
1004     if (!isset($amp_conf["AMPDBFILE"]))
1005       die("You must setup properly AMPDBFILE in /etc/amportal.conf");
1006      
1007     if (isset($amp_conf["AMPDBFILE"]) == "")
1008       die("AMPDBFILE in /etc/amportal.conf cannot be blank");
1009
1010     /* on centos this extension is not loaded by default */
1011     if (! extension_loaded('sqlite3.so')  && ! extension_loaded('SQLITE3'))
1012       dl('sqlite3.so');
1013
1014     if (! @require_once('DB/sqlite3.php') )
1015     {
1016       out("FAILED");
1017       fatal( "Your PHP installation has no PEAR/SQLite3 support. Please install php-sqlite3 and php-pear.");
1018     }
1019
1020     $datasource = "sqlite3:///" . $amp_conf["AMPDBFILE"] . "?mode=0666";
1021     $db = DB::connect($datasource);
1022     break;
1023
1024   default:
1025     die( "Unknown SQL engine: [$db_engine]");
1026 }
1027
1028 if(DB::IsError($db)) {
1029   out("FAILED");
1030   debug($db->userinfo);
1031   out("Try running ".$argv[0]." --username=user --password=pass  (using your own user and pass)");
1032   fatal("Cannot connect to database");
1033  
1034 }
1035 out("OK");
1036
1037
1038 // **** Read DB for version info
1039
1040 if (!isset($version)) {
1041   outn("Checking current version of AMP..");
1042   $version = install_getversion();
1043   if (!$version) {
1044     out("no version information");
1045     out("Assuming new installation");
1046   } else {
1047     out($version);
1048   }
1049 }
1050
1051 // Check if freepbx_settings is there and has been initialized
1052 // if it has anything less than about 5 settings in it (and that
1053 // is probably quite low) then something is wrong so assume it
1054 // is not there.
1055 //
1056 $sql = "SELECT count(*) FROM freepbx_settings";
1057 $num_settings = $db->getOne($sql);
1058 $force_amportal_conf = DB::IsError($num_settings) || $num_settings < 5;
1059
1060 // **** Copy files
1061
1062 if ($install_files)
1063 {
1064   outn("Installing new FreePBX files..");
1065   $check_md5s=true;
1066   $md5sums = read_md5_file(UPGRADE_DIR."/".$version.".md5");
1067   list($num_files, $num_copied) = recursive_copy("amp_conf", "", $md5sums);
1068   if (!is_file("/etc/asterisk/voicemail.conf")) copy("/etc/asterisk/voicemail.conf.template","/etc/asterisk/voicemail.conf");
1069   if (!is_dir("/var/spool/asterisk/voicemail/device")) amp_mkdir("/var/spool/asterisk/voicemail/device", "0755", true);
1070   out("OK (".$num_copied." files copied, ".($num_files-$num_copied)." skipped)");
1071
1072   // link the packed js library for ARI, if there the error will indicate that
1073   $libfreepbx = $amp_conf['AMPWEBROOT'].'/admin/common/libfreepbx.javascripts.js';
1074   $dest_libfreepbx = $amp_conf['AMPWEBROOT'].'/recordings/theme/js/libfreepbx.javascripts.js';
1075   if (!file_exists($amp_conf['AMPWEBROOT'].'/recordings/theme/js')) {
1076   mkdir($amp_conf['AMPWEBROOT'].'/recordings/theme/js',0755, true);
1077   }
1078   if (file_exists($libfreepbx) && !file_exists($dest_libfreepbx)) {
1079     outn(_("linking libfreepbx.javascripts.js to theme/js.."));
1080     if (link($libfreepbx, $dest_libfreepbx)) {
1081       out(_("ok"));
1082     } else {
1083       out(_("possible error - check warning message"));
1084     }
1085   }
1086 }
1087
1088 // **** Apply amportal.conf configuration to files
1089 if (file_exists(dirname(__FILE__)."/apply_conf.sh")) {
1090   debug("Running ".dirname(__FILE__)."/apply_conf.sh");
1091 }
1092
1093 // Ensure executables are executable
1094 chmod($amp_conf["AMPBIN"]."/freepbx_setting", 0755);
1095 outn("Configuring install for your environment..");
1096 exec($amp_conf["AMPBIN"]."/freepbx_setting AMPASTERISKWEBGROUP $runas_gid");
1097 exec($amp_conf["AMPBIN"]."/freepbx_setting AMPASTERISKWEBUSER $runas_uid");
1098 exec($amp_conf["AMPBIN"]."/freepbx_setting AMPASTERISKGROUP $runas_gid");
1099 exec($amp_conf["AMPBIN"]."/freepbx_setting AMPASTERISKUSER $runas_uid");
1100 exec($amp_conf["AMPBIN"]."/freepbx_setting AMPDEVGROUP $runas_gid");
1101 exec($amp_conf["AMPBIN"]."/freepbx_setting AMPDEVUSER $runas_uid");
1102 if (!$dryrun) {
1103   $asteriskuser = isset($amp_conf['AMPASTERISKUSER']) && $amp_conf['AMPASTERISKUSER'] ? $amp_conf['AMPASTERISKUSER'] : 'asterisk';
1104
1105   if (file_exists($amp_conf["AMPSBIN"]."/amportal")) {
1106     exec("chmod 755 ".$amp_conf["AMPSBIN"]."/amportal");
1107     outn("amportal..");
1108   } else {
1109     outn("no amportal..");
1110   }
1111   if (file_exists($amp_conf["AMPSBIN"]."/fpbx")) {
1112     exec("chmod 755 ".$amp_conf["AMPSBIN"]."/fpbx");
1113     outn("fpbx..");
1114   } else {
1115     outn("no fpbx..");
1116   }
1117   if (file_exists($amp_conf["AMPBIN"]."/freepbx_engine")) {
1118     exec("chmod 755 ".$amp_conf["AMPBIN"]."/freepbx_engine");
1119     outn("freepbx_engine..");
1120   } else {
1121     outn("no freepbx_engine..");
1122   }
1123   if (file_exists($amp_conf["AMPBIN"]."/freepbx_setting")) {
1124     exec("chmod 755 ".$amp_conf["AMPBIN"]."/freepbx_setting");
1125     outn("freepbx_setting..");
1126   } else {
1127     outn("no freepbx_setting..");
1128   }
1129   if (file_exists($amp_conf["AMPBIN"]."/gen_amp_conf.php")) {
1130     exec("chmod 755 ".$amp_conf["AMPBIN"]."/gen_amp_conf.php");
1131     outn("gen_amp_conf.php..");
1132   } else {
1133     outn("no gen_amp_conf..");
1134   }
1135   out("done");
1136   // Note - freepbx_engine must be run to ensure that freepbx.conf exists.
1137   exec($amp_conf["AMPBIN"]."/freepbx_engine error > /dev/null");
1138   // Ensure UIDs are set correctly before any chowns happen
1139   exec($amp_conf["AMPBIN"]."/freepbx_setting AMPASTERISKWEBGROUP $runas_gid");
1140   exec($amp_conf["AMPBIN"]."/freepbx_setting AMPASTERISKWEBUSER $runas_uid");
1141   exec($amp_conf["AMPBIN"]."/freepbx_setting AMPASTERISKGROUP $runas_gid");
1142   exec($amp_conf["AMPBIN"]."/freepbx_setting AMPASTERISKUSER $runas_uid");
1143   exec($amp_conf["AMPBIN"]."/freepbx_setting AMPDEVGROUP $runas_gid");
1144   exec($amp_conf["AMPBIN"]."/freepbx_setting AMPDEVUSER $runas_uid");
1145   exec($amp_conf["AMPBIN"]."/freepbx_setting ASTMANAGERHOST $asteriskip");
1146   // edit conf file passwords and then
1147   // reload manager in asterisk if it is running:
1148   //
1149   outn("apply username/password changes to conf files..");
1150   if (file_exists(dirname(__FILE__)."/apply_conf.sh")) {
1151     if ($force_amportal_conf) {
1152       putenv('FORCE_AMPORTAL_CONF=1');
1153       exec(dirname(__FILE__)."/apply_conf.sh ".AMP_CONF);
1154       putenv('FORCE_AMPORTAL_CONF=');
1155     } else {
1156       exec(dirname(__FILE__)."/apply_conf.sh");
1157     }
1158   }
1159   out("done");
1160
1161   /* As of Asterisk 1.4.16 or there about, a missing #include file will make the reload fail. So
1162     we need to make sure that we have such for everything that is in our configs. We will simply
1163     look for the #include statements and touch the files vs. trying to inventory everything we may
1164     need and then forgetting something.
1165   */
1166  
1167   outn("creating missing #include files..");
1168   $include_err = false;
1169   exec("grep '#include' ".$amp_conf['ASTETCDIR']."/*.conf | sed 's/;.*//; s/#include//'",$output,$retcode);
1170   if ($retcode != 0) {
1171     out("Error code $retcode: trying to search for missing #include files");
1172     $include_err = true;
1173   }
1174
1175   foreach($output as $file) {
1176     if (trim($file) == '') {
1177       continue;
1178     }
1179     $parse1 = explode(':',$file);
1180     $parse2 = explode(';',$parse1[1]);
1181     $rawfile = trim($parse2[0]);
1182     if ($rawfile == '') {
1183       continue;
1184     }
1185
1186     $target = ($rawfile[0] == '/') ? $rawfile : $amp_conf['ASTETCDIR']."/$rawfile";
1187
1188     if (!file_exists($target)) {
1189       exec("sudo -u $asteriskuser touch $target", $output, $retcode);
1190         if ($retcode != 0) {
1191         out("Error code $retcode: trying to create empty file $target");
1192         $include_err = true;
1193       }
1194     }
1195   }
1196   if (! $include_err) {
1197     out("OK");
1198   }
1199   // reload manager in asterisk if it was running:
1200   //
1201   if (version_compare($asterisk_version,'1.4','ge')) {
1202     system("asterisk -rx 'module reload manager'");
1203   } else {
1204     system("asterisk -rx 'reload manager'"); //1.2 syntax
1205   }
1206 }
1207 out("OK");
1208
1209 // **** Create spool directories for monitor and fax
1210 if (!is_dir($amp_conf["ASTSPOOLDIR"]."/monitor"))
1211   amp_mkdir($amp_conf["ASTSPOOLDIR"]."/monitor","0766",true);
1212 if (!is_dir($amp_conf["ASTSPOOLDIR"]."/fax"))
1213   amp_mkdir($amp_conf["ASTSPOOLDIR"]."/fax","0766",true);
1214
1215
1216 // **** Set permissions all files
1217
1218 // tell amportal/freepbx_engine to use amportal.conf and not bootstrap
1219 // if freepbx_settings has not been setup yet or you will get an error.
1220 //
1221 if ($install_files)
1222 {
1223   outn("Setting permissions on files..");
1224   if (!$dryrun) {
1225     if ($force_amportal_conf) {
1226       putenv('FORCE_AMPORTAL_CONF=1');
1227     }
1228     exec($amp_conf["AMPSBIN"]."/amportal chown");
1229     if ($force_amportal_conf) {
1230       putenv('FORCE_AMPORTAL_CONF=');
1231     }
1232   }
1233   out("OK");
1234 }
1235
1236 // Run through all the upgrade scripts starting at the specified version
1237 //
1238 upgrade_all($version);
1239
1240 outn("Creating or updating freepbx_conf settings..");
1241 freepbx_settings_init(true);
1242 out("..OK");
1243
1244 // **** Generate AMP configs
1245 out("Generating AMP configs..");
1246 generate_configs();
1247 out("Generating AMP configs..OK");
1248
1249 $version = install_getversion();
1250
1251 if($install_fop && $amp_conf["FOPRUN"])  {
1252   // **** Bounce FOP
1253   outn("Restarting Flash Operator Panel..");
1254   exec("sudo -u $asteriskuser ".$amp_conf["AMPBIN"].'/bounce_op.sh');
1255   out("OK");
1256 }
1257
1258 // Now force an install for all modules that are packaged with the tarball
1259 // directory.
1260 //
1261 if ($module_install) {
1262   install_modules();
1263 } else {
1264   out("bypassing packaged module installation because --skip-module-install flag");
1265 }
1266
1267
1268 // **** Set reload flag for AMP admin
1269 needreload();
1270
1271 if ($amp_conf["AMPWEBADDRESS"])
1272 {
1273   out("Please update your modules and reload Asterisk by visiting http://".$amp_conf["AMPWEBADDRESS"]."/admin",false);
1274 }
1275 else
1276 {
1277   out("Please update your modules and reload Asterisk by browsing to your server.",false);
1278 }
1279
1280 out("",false);
1281 out("*************************************************************************",false);
1282 out("* Note: It's possible that if you click the red 'Update Now' bar BEFORE *",false);
1283 out("* updating your modules, your machine will start dropping calls. Ensure *",false);
1284 out("* that all modules are up to date BEFORE YOU CLICK THE RED BAR. As long *",false);
1285 out("* as this is observed, your machine will be fully functional whilst the *",false);
1286 out("* upgrade is in progress.                                               *",false);
1287 out("*************************************************************************",false);
1288 // vim: set ts=4 sw=4 : //
1289 // Ensure you 'set modeline' in your ~/.vimrc to activate this //
1290 ?>
Note: See TracBrowser for help on using the browser.