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