root/freepbx/trunk/install_amp

Revision 6816, 35.3 kB (checked in by p_lindheimer, 5 years ago)

Merged revisions 6600-6815 via svnmerge from
http://svn.freepbx.org/freepbx/branches/2.5

........

r6601 | ethans | 2008-09-07 15:39:51 -0700 (Sun, 07 Sep 2008) | 4 lines


Adds FOPDISABLE option to amportal.conf. This will disable FOP on the web interfaces as well as server operations in retrieve_conf and freepbx_engine


References: 3170

........

r6605 | p_lindheimer | 2008-09-07 19:23:41 -0700 (Sun, 07 Sep 2008) | 1 line


create 2.5.0rc3 upgrade dir and remove zh_TW from error in #2609 placement

........

r6635 | p_lindheimer | 2008-09-07 20:01:27 -0700 (Sun, 07 Sep 2008) | 1 line


Creating release 2.5.0rc3

........

r6672 | p_lindheimer | 2008-09-09 08:46:14 -0700 (Tue, 09 Sep 2008) | 1 line


closes #3176 fix message to reflect that module will be downloaded but kept disable

........

r6675 | p_lindheimer | 2008-09-09 14:32:14 -0700 (Tue, 09 Sep 2008) | 1 line


tweak on freepbx_debug function to easily write out arrays

........

r6680 | sasargen | 2008-09-10 05:58:58 -0700 (Wed, 10 Sep 2008) | 1 line


fixes #3181 by adding value='yes' to checkbox tags

........

r6699 | sasargen | 2008-09-12 08:06:35 -0700 (Fri, 12 Sep 2008) | 1 line


basic fix of #3184 for ARI in 2.5

........

r6708 | p_lindheimer | 2008-09-13 15:04:03 -0700 (Sat, 13 Sep 2008) | 1 line


re #3191 fix uninitialized variables

........

r6725 | mickecarlsson | 2008-09-15 09:06:13 -0700 (Mon, 15 Sep 2008) | 1 line


Added menu choice for Swedish in framework

........

r6731 | p_lindheimer | 2008-09-15 20:38:35 -0700 (Mon, 15 Sep 2008) | 1 line


closes #3184 Secruity Issue allowing access of arbitrary files from authenticated ARI users with brute force cracking of a weak encryption key

........

r6746 | sasargen | 2008-09-16 08:20:31 -0700 (Tue, 16 Sep 2008) | 1 line


ARI: fixes #3202 by checking if any files selected

........

r6747 | sasargen | 2008-09-16 08:29:34 -0700 (Tue, 16 Sep 2008) | 1 line


ARI: fixes #3203 by removing checkbox when recording file does not exist

........

r6755 | sasargen | 2008-09-16 12:53:50 -0700 (Tue, 16 Sep 2008) | 1 line


ARI: disable delete button on callmonitor page when no recordings shown

........

r6756 | p_lindheimer | 2008-09-17 00:02:57 -0700 (Wed, 17 Sep 2008) | 1 line


fixes #3204 set localization domain when applying hooks, for gui hooks, implementation depends on standardization of the gui functions to start with modulename_ which is currently the standard although not required. Implemented so that the domain will only be set if the current target language localization directory exists for the module, otherwise it will fallback to amp since many past languages have put their localizations in the amp domain. They should move this to the proper module going forward

........

r6764 | p_lindheimer | 2008-09-17 11:29:39 -0700 (Wed, 17 Sep 2008) | 1 line


fixes #3209 Reports SECURITY VULNERABILITY by forcing all access to the CDR Reports section to flow through config.php

........

r6766 | p_lindheimer | 2008-09-17 18:27:30 -0700 (Wed, 17 Sep 2008) | 1 line


remove old unused code in drawListMenu and allow the Add to be optional

........

r6767 | p_lindheimer | 2008-09-17 19:21:08 -0700 (Wed, 17 Sep 2008) | 1 line


don't print out submit button in guielements if no fields form fields are generated

........

r6769 | p_lindheimer | 2008-09-17 22:15:04 -0700 (Wed, 17 Sep 2008) | 1 line


add 2.5.0 upgrade directory and remove the moved files re #3209

........

r6803 | p_lindheimer | 2008-09-19 08:47:01 -0700 (Fri, 19 Sep 2008) | 1 line


update CHANGES document before final release, and updated ChangeLog? for once

........

r6804 | p_lindheimer | 2008-09-19 08:48:03 -0700 (Fri, 19 Sep 2008) | 1 line


Creating release 2.5.0

........

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