root/freepbx/branches/2.10/install_amp

Revision 12809, 42.6 kB (checked in by p_lindheimer, 2 years ago)

remove some additional FOP code since making a module

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