| 1 |
#!/usr/bin/php -q |
|---|
| 2 |
<?php |
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
$versions = array( |
|---|
| 6 |
'1.10.005', |
|---|
| 7 |
'1.10.006', |
|---|
| 8 |
'1.10.007beta1', |
|---|
| 9 |
'1.10.007beta2', |
|---|
| 10 |
'1.10.007', |
|---|
| 11 |
'1.10.007a', |
|---|
| 12 |
'1.10.008beta1', |
|---|
| 13 |
'1.10.008beta2', |
|---|
| 14 |
'1.10.008beta3', |
|---|
| 15 |
'1.10.008', |
|---|
| 16 |
'1.10.009beta1', |
|---|
| 17 |
'1.10.009beta2', |
|---|
| 18 |
'1.10.009', |
|---|
| 19 |
'1.10.010beta1', |
|---|
| 20 |
'1.10.010', |
|---|
| 21 |
'2.0beta1', |
|---|
| 22 |
'2.0beta2', |
|---|
| 23 |
'2.0beta3', |
|---|
| 24 |
'2.0beta4', |
|---|
| 25 |
'2.0beta5', |
|---|
| 26 |
'2.0.0', |
|---|
| 27 |
'2.0.1' |
|---|
| 28 |
); |
|---|
| 29 |
|
|---|
| 30 |
define("AMP_CONF", "/etc/amportal.conf"); |
|---|
| 31 |
|
|---|
| 32 |
define("ASTERISK_CONF", "/etc/asterisk/asterisk.conf"); |
|---|
| 33 |
|
|---|
| 34 |
define("UPGRADE_DIR", dirname(__FILE__)."/upgrades"); |
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
function out($text) { |
|---|
| 39 |
echo $text."\n"; |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
function outn($text) { |
|---|
| 43 |
echo $text; |
|---|
| 44 |
} |
|---|
| 45 |
|
|---|
| 46 |
function error($text) { |
|---|
| 47 |
echo "[ERROR] ".$text."\n"; |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
function fatal($text) { |
|---|
| 51 |
echo "[FATAL] ".$text."\n"; |
|---|
| 52 |
exit(1); |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
function debug($text) { |
|---|
| 56 |
global $debug; |
|---|
| 57 |
|
|---|
| 58 |
if ($debug) echo "[DEBUG] ".$text."\n"; |
|---|
| 59 |
} |
|---|
| 60 |
|
|---|
| 61 |
function showHelp() { |
|---|
| 62 |
out("Optional parameters:"); |
|---|
| 63 |
out(" --help, -h, -? Show this help"); |
|---|
| 64 |
out(" --username <user> Use <user> to connect to db and write config"); |
|---|
| 65 |
out(" --password <pass> Use <pass> to connect to db and write config"); |
|---|
| 66 |
out(" --debug Enable debug output"); |
|---|
| 67 |
out(" --dry-run Don't actually do anything"); |
|---|
| 68 |
out(" --force-version <ver> Force upgrade from version <ver>"); |
|---|
| 69 |
out(" --dbhost <ip address> Use a remote database server"); |
|---|
| 70 |
out(" --no-files Just run updates without installing files"); |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
function parse_amportal_conf($filename) { |
|---|
| 74 |
$file = file($filename); |
|---|
| 75 |
foreach ($file as $line) { |
|---|
| 76 |
if (preg_match("/^\s*([a-zA-Z0-9]+)\s*=\s*(.*)\s*([;#].*)?/",$line,$matches)) { |
|---|
| 77 |
$conf[ $matches[1] ] = $matches[2]; |
|---|
| 78 |
} |
|---|
| 79 |
} |
|---|
| 80 |
return $conf; |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
function parse_asterisk_conf($filename) { |
|---|
| 84 |
$file = file($filename); |
|---|
| 85 |
foreach ($file as $line) { |
|---|
| 86 |
if (preg_match("/^\s*([a-zA-Z0-9]+)\s* => \s*(.*)\s*([;#].*)?/",$line,$matches)) { |
|---|
| 87 |
$conf[ $matches[1] ] = $matches[2]; |
|---|
| 88 |
} |
|---|
| 89 |
} |
|---|
| 90 |
return $conf; |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
function getversion() { |
|---|
| 95 |
global $db; |
|---|
| 96 |
$sql = "SELECT value FROM admin WHERE variable = 'version'"; |
|---|
| 97 |
$results = $db->getAll($sql); |
|---|
| 98 |
if(DB::IsError($results)) { |
|---|
| 99 |
return false; |
|---|
| 100 |
} |
|---|
| 101 |
return $results[0][0]; |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
function setversion($version) { |
|---|
| 106 |
global $db; |
|---|
| 107 |
$sql = "UPDATE admin SET value = '".$version."' WHERE variable = 'version'"; |
|---|
| 108 |
debug($sql); |
|---|
| 109 |
$result = $db->query($sql); |
|---|
| 110 |
if(DB::IsError($result)) { |
|---|
| 111 |
die($result->getMessage()); |
|---|
| 112 |
} |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
function write_amportal_conf($filename, $conf) { |
|---|
| 116 |
$file = file($filename); |
|---|
| 117 |
|
|---|
| 118 |
foreach (array_keys($file) as $key) { |
|---|
| 119 |
if (preg_match("/^\s*([a-zA-Z0-9]+)\s*=\s*(.*)\s*([;#].*)?/",$file[$key],$matches)) { |
|---|
| 120 |
|
|---|
| 121 |
if (isset($conf[ $matches[1] ])) { |
|---|
| 122 |
|
|---|
| 123 |
$file[$key] = $matches[1]."=".$conf[ $matches[1] ]."\n"; |
|---|
| 124 |
|
|---|
| 125 |
unset($conf[ $matches[1] ]); |
|---|
| 126 |
} |
|---|
| 127 |
} |
|---|
| 128 |
} |
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
foreach ($conf as $key=>$val) { |
|---|
| 132 |
$file[] = $key."=".$val."\n"; |
|---|
| 133 |
} |
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
if (!$fd = fopen($filename, "w")) { |
|---|
| 137 |
fatal("Could not open ".$filename." for writing"); |
|---|
| 138 |
} |
|---|
| 139 |
fwrite($fd, implode("",$file)); |
|---|
| 140 |
fclose($fd); |
|---|
| 141 |
} |
|---|
| 142 |
|
|---|
| 143 |
function ask_overwrite($file1, $file2) { |
|---|
| 144 |
global $check_md5s; |
|---|
| 145 |
do { |
|---|
| 146 |
out($file2." has been changed from the original version."); |
|---|
| 147 |
outn("Overwrite (y=yes/a=all/n=no/d=diff/s=shell/x=exit)? "); |
|---|
| 148 |
$key = fgets(STDIN,1024); |
|---|
| 149 |
switch (strtolower($key[0])) { |
|---|
| 150 |
case "y": return true; |
|---|
| 151 |
case "a": $check_md5s=false; return true; |
|---|
| 152 |
case "n": return false; |
|---|
| 153 |
case "d": |
|---|
| 154 |
out(""); |
|---|
| 155 |
passthru("diff -u ".$file2." ".$file1); |
|---|
| 156 |
break; |
|---|
| 157 |
case "s": |
|---|
| 158 |
if (function_exists("pcntl_fork")) { |
|---|
| 159 |
out(""); |
|---|
| 160 |
$shell = (isset($_ENV["SHELL"]) ? $_ENV["SHELL"] : "/bin/bash"); |
|---|
| 161 |
out("Dropping to shell. Type 'exit' to return"); |
|---|
| 162 |
out("-> Original file: ".$file2); |
|---|
| 163 |
out("-> New file: ".$file1); |
|---|
| 164 |
|
|---|
| 165 |
$pid = pcntl_fork(); |
|---|
| 166 |
if ($pid == -1) { |
|---|
| 167 |
out("[ERROR] cannot fork"); |
|---|
| 168 |
} else if ($pid) { |
|---|
| 169 |
|
|---|
| 170 |
pcntl_waitpid($pid, $status); |
|---|
| 171 |
|
|---|
| 172 |
} else { |
|---|
| 173 |
pcntl_exec($shell, array(), $_ENV); |
|---|
| 174 |
} |
|---|
| 175 |
|
|---|
| 176 |
out("Returned from shell"); |
|---|
| 177 |
} else { |
|---|
| 178 |
out("[ERROR] PHP not built with process control (--enable-pcntl) support: cannot spawn shell"); |
|---|
| 179 |
} |
|---|
| 180 |
|
|---|
| 181 |
break; |
|---|
| 182 |
case "x": |
|---|
| 183 |
out("-> Original file: ".$file2); |
|---|
| 184 |
out("-> New file: ".$file1); |
|---|
| 185 |
out("Exiting install program."); |
|---|
| 186 |
exit(1); |
|---|
| 187 |
break; |
|---|
| 188 |
} |
|---|
| 189 |
out(""); |
|---|
| 190 |
} while(1); |
|---|
| 191 |
} |
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
function recursive_copy($dirsourceparent, $dirdest, &$md5sums, $dirsource = "") { |
|---|
| 196 |
global $dryrun; |
|---|
| 197 |
global $check_md5s; |
|---|
| 198 |
global $amp_conf; |
|---|
| 199 |
global $asterisk_conf; |
|---|
| 200 |
|
|---|
| 201 |
if ($dirsource && ($dirsource[0] != "/")) $dirsource = "/".$dirsource; |
|---|
| 202 |
|
|---|
| 203 |
if (is_dir($dirsourceparent.$dirsource)) $dir_handle = opendir($dirsourceparent.$dirsource); |
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 |
echo "dirsourceparent: "; var_dump($dirsourceparent); |
|---|
| 207 |
echo "dirsource: "; var_dump($dirsource); |
|---|
| 208 |
echo "dirdest: "; var_dump($dirdest); |
|---|
| 209 |
*/ |
|---|
| 210 |
|
|---|
| 211 |
while (isset($dir_handle) && ($file = readdir($dir_handle))) { |
|---|
| 212 |
if (($file!=".") && ($file!="..") && ($file != "CVS")) { |
|---|
| 213 |
$source = $dirsourceparent.$dirsource."/".$file; |
|---|
| 214 |
$destination = $dirdest.$dirsource."/".$file; |
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
if (strpos($destination,"htdocs_panel")) { |
|---|
| 218 |
$destination=str_replace("/htdocs_panel",$amp_conf["FOPWEBROOT"],$destination); |
|---|
| 219 |
} else { |
|---|
| 220 |
$destination=str_replace("/htdocs",$amp_conf["AMPWEBROOT"],$destination); |
|---|
| 221 |
} |
|---|
| 222 |
$destination=str_replace("/htdocs_panel",$amp_conf["FOPWEBROOT"],$destination); |
|---|
| 223 |
$destination=str_replace("/cgi-bin",$amp_conf["AMPCGIBIN"],$destination); |
|---|
| 224 |
$destination=str_replace("/bin",$amp_conf["AMPBIN"],$destination); |
|---|
| 225 |
$destination=str_replace("/sbin",$amp_conf["AMPSBIN"],$destination); |
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
$destination=str_replace("/astetc",$asterisk_conf["astetcdir"],$destination); |
|---|
| 229 |
$destination=str_replace("/mohmp3",$asterisk_conf["astvarlibdir"]."/mohmp3",$destination); |
|---|
| 230 |
$destination=str_replace("/astvarlib",$asterisk_conf["astvarlibdir"],$destination); |
|---|
| 231 |
$destination=str_replace("/agi-bin",$asterisk_conf["astagidir"],$destination); |
|---|
| 232 |
$destination=str_replace("/sounds",$asterisk_conf["astvarlibdir"]."/sounds",$destination); |
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
if (is_dir($source)) { |
|---|
| 236 |
if (!file_exists($destination)) { |
|---|
| 237 |
debug("mkdir ".$destination); |
|---|
| 238 |
if ((!$dryrun) && ($destination != "")) { |
|---|
| 239 |
mkdir($destination, 0750); |
|---|
| 240 |
} |
|---|
| 241 |
} |
|---|
| 242 |
} |
|---|
| 243 |
|
|---|
| 244 |
if (!is_dir($source)) { |
|---|
| 245 |
if ($check_md5s && file_exists($destination) && isset($md5sums[$destination]) && (md5_file($destination) != $md5sums[$destination])) { |
|---|
| 246 |
$overwrite = ask_overwrite($source, $destination, $md5sums); |
|---|
| 247 |
} else { |
|---|
| 248 |
$overwrite = true; |
|---|
| 249 |
} |
|---|
| 250 |
|
|---|
| 251 |
if ($overwrite) { |
|---|
| 252 |
debug("copy ".$source." -> ".$destination); |
|---|
| 253 |
if (!$dryrun) { |
|---|
| 254 |
copy($source, $destination); |
|---|
| 255 |
} |
|---|
| 256 |
} else { |
|---|
| 257 |
debug("not overwriting ".$destination); |
|---|
| 258 |
} |
|---|
| 259 |
} else { |
|---|
| 260 |
|
|---|
| 261 |
recursive_copy($dirsourceparent, $dirdest, $md5sums, $dirsource."/".$file); |
|---|
| 262 |
} |
|---|
| 263 |
} |
|---|
| 264 |
} |
|---|
| 265 |
|
|---|
| 266 |
if (isset($dir_handle)) closedir($dir_handle); |
|---|
| 267 |
|
|---|
| 268 |
return true; |
|---|
| 269 |
} |
|---|
| 270 |
|
|---|
| 271 |
function read_md5_file($filename) { |
|---|
| 272 |
$md5 = array(); |
|---|
| 273 |
if (file_exists($filename)) { |
|---|
| 274 |
foreach (file($filename) as $line) { |
|---|
| 275 |
if (preg_match("/^([a-f0-9]{32})\s+(.*)$/", $line, $matches)) { |
|---|
| 276 |
$md5[ "/".$matches[2] ] = $matches[1]; |
|---|
| 277 |
} |
|---|
| 278 |
} |
|---|
| 279 |
} |
|---|
| 280 |
return $md5; |
|---|
| 281 |
} |
|---|
| 282 |
|
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
function run_included($file) { |
|---|
| 287 |
global $db; |
|---|
| 288 |
global $amp_conf; |
|---|
| 289 |
|
|---|
| 290 |
include($file); |
|---|
| 291 |
} |
|---|
| 292 |
|
|---|
| 293 |
|
|---|
| 294 |
|
|---|
| 295 |
function install_upgrade($version) { |
|---|
| 296 |
global $db; |
|---|
| 297 |
global $dryrun; |
|---|
| 298 |
|
|---|
| 299 |
if (is_dir(UPGRADE_DIR."/".$version)) { |
|---|
| 300 |
$dir = opendir(UPGRADE_DIR."/".$version); |
|---|
| 301 |
while ($file = readdir($dir)) { |
|---|
| 302 |
if (($file[0] != ".") && is_file(UPGRADE_DIR."/".$version."/".$file)) { |
|---|
| 303 |
if (strtolower(substr($file,-4)) == ".sql") { |
|---|
| 304 |
out("-> Running SQL script ".UPGRADE_DIR."/".$version."/".$file); |
|---|
| 305 |
|
|---|
| 306 |
$fd = fopen(UPGRADE_DIR."/".$version."/".$file, "r"); |
|---|
| 307 |
while (!feof($fd)) { |
|---|
| 308 |
$data .= fread($fd, 1024); |
|---|
| 309 |
} |
|---|
| 310 |
fclose($fd); |
|---|
| 311 |
|
|---|
| 312 |
preg_match_all("/((SELECT|INSERT|UPDATE|DELETE|CREATE|DROP|ALTER).*);\s*\n/Us", $data, $matches); |
|---|
| 313 |
|
|---|
| 314 |
foreach ($matches[1] as $sql) { |
|---|
| 315 |
debug($sql); |
|---|
| 316 |
if (!$dryrun) { |
|---|
| 317 |
$result = $db->query($sql); |
|---|
| 318 |
if(DB::IsError($result)) { |
|---|
| 319 |
fatal($result->getDebugInfo()."\" while running ".$file."\n"); |
|---|
| 320 |
} |
|---|
| 321 |
} |
|---|
| 322 |
} |
|---|
| 323 |
|
|---|
| 324 |
} else if (strtolower(substr($file,-4)) == ".php") { |
|---|
| 325 |
out("-> Running PHP script ".UPGRADE_DIR."/".$version."/".$file); |
|---|
| 326 |
if (!$dryrun) { |
|---|
| 327 |
run_included(UPGRADE_DIR."/".$version."/".$file); |
|---|
| 328 |
} |
|---|
| 329 |
|
|---|
| 330 |
} else if (is_executable(UPGRADE_DIR."/".$version."/".$file)) { |
|---|
| 331 |
out("-> Executing ".UPGRADE_DIR."/".$version."/".$file); |
|---|
| 332 |
if (!$dryrun) { |
|---|
| 333 |
exec(UPGRADE_DIR."/".$version."/".$file); |
|---|
| 334 |
} |
|---|
| 335 |
} else { |
|---|
| 336 |
error("-> Don't know what to do with ".UPGRADE_DIR."/".$version."/".$file); |
|---|
| 337 |
} |
|---|
| 338 |
} |
|---|
| 339 |
} |
|---|
| 340 |
} |
|---|
| 341 |
} |
|---|
| 342 |
|
|---|
| 343 |
|
|---|
| 344 |
|
|---|
| 345 |
|
|---|
| 346 |
function run_upgrade($versions) { |
|---|
| 347 |
global $dryrun; |
|---|
| 348 |
|
|---|
| 349 |
foreach ($versions as $version) { |
|---|
| 350 |
out("Upgrading to ".$version.".."); |
|---|
| 351 |
install_upgrade($version); |
|---|
| 352 |
if (!$dryrun) { |
|---|
| 353 |
setversion($version); |
|---|
| 354 |
} |
|---|
| 355 |
out("Upgrading to ".$version."..OK"); |
|---|
| 356 |
} |
|---|
| 357 |
} |
|---|
| 358 |
|
|---|
| 359 |
|
|---|
| 360 |
|
|---|
| 361 |
function generate_configs() { |
|---|
| 362 |
global $amp_conf; |
|---|
| 363 |
global $dryrun; |
|---|
| 364 |
|
|---|
| 365 |
out("Generating Configurations.conf.."); |
|---|
| 366 |
if (!$dryrun) |
|---|
| 367 |
passthru("su - asterisk -c ".$amp_conf["AMPBIN"]."/retrieve_conf"); |
|---|
| 368 |
} |
|---|
| 369 |
|
|---|
| 370 |
|
|---|
| 371 |
|
|---|
| 372 |
|
|---|
| 373 |
function needreload() { |
|---|
| 374 |
global $db; |
|---|
| 375 |
$sql = "UPDATE admin SET value = 'true' WHERE variable = 'need_reload'"; |
|---|
| 376 |
$result = $db->query($sql); |
|---|
| 377 |
if(DB::IsError($result)) { |
|---|
| 378 |
die($result->getMessage()); |
|---|
| 379 |
} |
|---|
| 380 |
} |
|---|
| 381 |
|
|---|
| 382 |
|
|---|
| 383 |
|
|---|
| 384 |
|
|---|
| 385 |
function collect_settings($filename, $dbhost = '', $dbuser = '', $dbpass = '') { |
|---|
| 386 |
out("Creating new /etc/amportal.conf"); |
|---|
| 387 |
|
|---|
| 388 |
outn("Enter your USERNAME to connect to the 'asterisk' database:\n [".($dbuser ? $dbuser : "asteriskuser")."] "); |
|---|
| 389 |
$key = trim(fgets(STDIN,1024)); |
|---|
| 390 |
if (preg_match('/^$/',$key)) $amp_conf["AMPDBUSER"] = ($dbuser ? $dbuser : "asteriskuser"); |
|---|
| 391 |
else $amp_conf["AMPDBUSER"] = $key; |
|---|
| 392 |
|
|---|
| 393 |
outn("Enter your PASSWORD to connect to the 'asterisk' database:\n [".($dbpass ? $dbpass : "amp109")."] "); |
|---|
| 394 |
$key = trim(fgets(STDIN,1024)); |
|---|
| 395 |
if (preg_match('/^$/',$key)) $amp_conf["AMPDBPASS"] = ($dbpass ? $dbpass : "amp109"); |
|---|
| 396 |
else $amp_conf["AMPDBPASS"] = $key; |
|---|
| 397 |
|
|---|
| 398 |
outn("Enter the hostname of the 'asterisk' database:\n [".($dbhost ? $dbhost : "localhost")."] "); |
|---|
| 399 |
$key = trim(fgets(STDIN,1024)); |
|---|
| 400 |
if (preg_match('/^$/',$key)) $amp_conf["AMPDBHOST"] = ($dbhost ? $dbhost : "localhost"); |
|---|
| 401 |
else $amp_conf["AMPDBHOST"] = $key; |
|---|
| 402 |
|
|---|
| 403 |
outn("Enter a USERNAME to connect to the Asterisk Manager interface:\n [admin] "); |
|---|
| 404 |
$key = trim(fgets(STDIN,1024)); |
|---|
| 405 |
if (preg_match('/^$/',$key)) $amp_conf["AMPMGRUSER"] = "admin"; |
|---|
| 406 |
else $amp_conf["AMPMGRUSER"] = $key; |
|---|
| 407 |
|
|---|
| 408 |
outn("Enter a PASSWORD to connect to the Asterisk Manager interface:\n [amp111] "); |
|---|
| 409 |
$key = trim(fgets(STDIN,1024)); |
|---|
| 410 |
if (preg_match('/^$/',$key)) $amp_conf["AMPMGRPASS"] = "amp111"; |
|---|
| 411 |
else $amp_conf["AMPMGRPASS"] = $key; |
|---|
| 412 |
|
|---|
| 413 |
do { |
|---|
| 414 |
out("Enter the path to use for your AMP web root:\n [/var/www/html] "); |
|---|
| 415 |
$key = trim(fgets(STDIN,1024)); |
|---|
| 416 |
if (preg_match('/^$/',$key)) $amp_conf["AMPWEBROOT"] = "/var/www/html"; |
|---|
| 417 |
else $amp_conf["AMPWEBROOT"] = rtrim($key,'/'); |
|---|
| 418 |
if (is_dir($amp_conf["AMPWEBROOT"])) { |
|---|
| 419 |
break; |
|---|
| 420 |
} else if (mkdir($amp_conf["AMPWEBROOT"],755)){ |
|---|
| 421 |
out("Created ".$amp_conf["AMPWEBROOT"]); |
|---|
| 422 |
break; |
|---|
| 423 |
} else { |
|---|
| 424 |
fatal("Cannot create ".$amp_conf["AMPWEBROOT"]."!"); |
|---|
| 425 |
} |
|---|
| 426 |
} while(1); |
|---|
| 427 |
|
|---|
| 428 |
do { |
|---|
| 429 |
out("Enter the path to use for your FOP web root:\n [/var/www/html/panel] "); |
|---|
| 430 |
$key = trim(fgets(STDIN,1024)); |
|---|
| 431 |
if (preg_match('/^$/',$key)) $amp_conf["FOPWEBROOT"] = "/var/www/html/panel"; |
|---|
| 432 |
else $amp_conf["FOPWEBROOT"] = rtrim($key,'/'); |
|---|
| 433 |
if (is_dir($amp_conf["FOPWEBROOT"])) { |
|---|
| 434 |
break; |
|---|
| 435 |
} else if (mkdir($amp_conf["FOPWEBROOT"],755)){ |
|---|
| 436 |
out("Created ".$amp_conf["FOPWEBROOT"]); |
|---|
| 437 |
break; |
|---|
| 438 |
} else { |
|---|
| 439 |
fatal("Cannot create ".$amp_conf["FOPWEBROOT"]."!"); |
|---|
| 440 |
} |
|---|
| 441 |
} while(1); |
|---|
| 442 |
|
|---|
| 443 |
do { |
|---|
| 444 |
outn("Enter the path to your Apache cgi-bin:\n [/var/www/cgi-bin] "); |
|---|
| 445 |
$key = trim(fgets(STDIN,1024)); |
|---|
| 446 |
if (preg_match('/^$/',$key)) $amp_conf["AMPCGIBIN"] = "/var/www/cgi-bin"; |
|---|
| 447 |
else $amp_conf["AMPCGIBIN"] = rtrim($key,'/'); |
|---|
| 448 |
if (is_dir($amp_conf["AMPCGIBIN"])) break; |
|---|
| 449 |
else fatal($amp_conf["AMPCGIBIN"]." is not a directory!"); |
|---|
| 450 |
} while(1); |
|---|
| 451 |
|
|---|
| 452 |
outn("Enter the IP ADDRESS or hostname used to access the AMP web-admin:\n [xx.xx.xx.xx] "); |
|---|
| 453 |
$key = trim(fgets(STDIN,1024)); |
|---|
| 454 |
if (preg_match('/^$/',$key)) $amp_conf["AMPWEBADDRESS"] = "xx.xx.xx.xx"; |
|---|
| 455 |
else $amp_conf["AMPWEBADDRESS"] = $key; |
|---|
| 456 |
|
|---|
| 457 |
outn("Enter a PASSWORD to perform call transfers with the Flash Operator Panel:\n [passw0rd] "); |
|---|
| 458 |
$key = trim(fgets(STDIN,1024)); |
|---|
| 459 |
if (preg_match('/^$/',$key)) $amp_conf["FOPPASSWORD"] = "passw0rd"; |
|---|
| 460 |
else $amp_conf["FOPPASSWORD"] = $key; |
|---|
| 461 |
|
|---|
| 462 |
outn("Use simple Extensions [extensions] admin or separate Devices and Users [deviceanduser]?\n [extensions] "); |
|---|
| 463 |
$key = trim(fgets(STDIN,1024)); |
|---|
| 464 |
if (preg_match('/^$/',$key)) $amp_conf["AMPEXTENSIONS"] = "extensions"; |
|---|
| 465 |
else $amp_conf["AMPEXTENSIONS"] = $key; |
|---|
| 466 |
|
|---|
| 467 |
do { |
|---|
| 468 |
out("Enter directory in which to store AMP executable scripts:\n [/var/lib/asterisk/bin] "); |
|---|
| 469 |
$key = trim(fgets(STDIN,1024)); |
|---|
| 470 |
if (preg_match('/^$/',$key)) $amp_conf["AMPBIN"] = "/var/lib/asterisk/bin"; |
|---|
| 471 |
else $amp_conf["AMPBIN"] = rtrim($key,'/'); |
|---|
| 472 |
if (is_dir($amp_conf["AMPBIN"])) { |
|---|
| 473 |
break; |
|---|
| 474 |
} else if (mkdir($amp_conf["AMPBIN"],755)){ |
|---|
| 475 |
out("Created ".$amp_conf["AMPBIN"]); |
|---|
| 476 |
break; |
|---|
| 477 |
} else { |
|---|
| 478 |
fatal("Cannot create ".$amp_conf["AMPBIN"]."!"); |
|---|
| 479 |
} |
|---|
| 480 |
} while(1); |
|---|
| 481 |
|
|---|
| 482 |
do { |
|---|
| 483 |
out("Enter directory in which to store super-user scripts:\n [/usr/sbin] "); |
|---|
| 484 |
$key = trim(fgets(STDIN,1024)); |
|---|
| 485 |
if (preg_match('/^$/',$key)) $amp_conf["AMPSBIN"] = "/usr/sbin"; |
|---|
| 486 |
else $amp_conf["AMPSBIN"] = rtrim($key,'/'); |
|---|
| 487 |
if (is_dir($amp_conf["AMPSBIN"])) { |
|---|
| 488 |
break; |
|---|
| 489 |
} else if (mkdir($amp_conf["AMPSBIN"],755)){ |
|---|
| 490 |
out("Created ".$amp_conf["AMPSBIN"]); |
|---|
| 491 |
break; |
|---|
| 492 |
} else { |
|---|
| 493 |
fatal("Cannot create ".$amp_conf["AMPSBIN"]."!"); |
|---|
| 494 |
} |
|---|
| 495 |
} while(1); |
|---|
| 496 |
|
|---|
| 497 |
|
|---|
| 498 |
write_amportal_conf($filename, $amp_conf); |
|---|
| 499 |
outn("/etc/amportal.conf written"); |
|---|
| 500 |
} |
|---|
| 501 |
|
|---|
| 502 |
|
|---|
| 503 |
|
|---|
| 504 |
|
|---|
| 505 |
|
|---|
| 506 |
|
|---|
| 507 |
if (version_compare(phpversion(),'4.3.0','<') || !defined("STDIN")) { |
|---|
| 508 |
define('STDIN',fopen("php://stdin","r")); |
|---|
| 509 |
define('STDOUT',fopen("php://stdout","r")); |
|---|
| 510 |
define('STDERR',fopen("php://stderr","r")); |
|---|
| 511 |
register_shutdown_function( create_function( '' , 'fclose(STDIN); fclose(STDOUT); fclose(STDERR); return true;' ) ); |
|---|
| 512 |
} |
|---|
| 513 |
|
|---|
| 514 |
|
|---|
| 515 |
|
|---|
| 516 |
outn("Checking for PEAR DB.."); |
|---|
| 517 |
if (! @ include('DB.php')) { |
|---|
| 518 |
out("FAILED"); |
|---|
| 519 |
fatal("PEAR must be installed (requires DB.php). Include path: ".ini_get("include_path")); |
|---|
| 520 |
} |
|---|
| 521 |
out("OK"); |
|---|
| 522 |
|
|---|
| 523 |
|
|---|
| 524 |
|
|---|
| 525 |
outn("Checking for PEAR Console::Getopt.."); |
|---|
| 526 |
if (! @ include("Console/Getopt.php")) { |
|---|
| 527 |
out("FAILED"); |
|---|
| 528 |
fatal("PEAR must be installed (requires Console/Getopt.php). Include path: ".ini_get("include_path")); |
|---|
| 529 |
} |
|---|
| 530 |
out("OK"); |
|---|
| 531 |
|
|---|
| 532 |
|
|---|
| 533 |
|
|---|
| 534 |
$perl_test = "perl -e 'use Asterisk::AGI'"; |
|---|
| 535 |
outn( "Checking for libasteriskperl (perl bindings for asterisk)..." ); |
|---|
| 536 |
exec( " $perl_test &> /dev/null", $perl_output, $perl_result ); |
|---|
| 537 |
if ( $perl_result != 0) |
|---|
| 538 |
{ |
|---|
| 539 |
out("FAILED\nReturn code from \"$perl_test\" is: " . $perl_result ); |
|---|
| 540 |
fatal( "Please install libasteriskperl from your vendor.\nThis perl module is needed for executing dialparties.agi." ); |
|---|
| 541 |
} |
|---|
| 542 |
out("OK"); |
|---|
| 543 |
|
|---|
| 544 |
|
|---|
| 545 |
|
|---|
| 546 |
$shortopts = "h?u:p:"; |
|---|
| 547 |
$longopts = array("help","debug","dry-run","username=","password=","force-version=","dbhost=","no-files"); |
|---|
| 548 |
|
|---|
| 549 |
$args = Console_Getopt::getopt(Console_Getopt::readPHPArgv(), $shortopts, $longopts); |
|---|
| 550 |
if (is_object($args)) { |
|---|
| 551 |
|
|---|
| 552 |
out($args->message); |
|---|
| 553 |
exit(255); |
|---|
| 554 |
} |
|---|
| 555 |
|
|---|
| 556 |
$debug = false; |
|---|
| 557 |
$dryrun = false; |
|---|
| 558 |
$install_files = true; |
|---|
| 559 |
|
|---|
| 560 |
|
|---|
| 561 |
$dbhost = null; |
|---|
| 562 |
$new_username = null; |
|---|
| 563 |
$new_password = null; |
|---|
| 564 |
|
|---|
| 565 |
foreach ($args[0] as $arg) { |
|---|
| 566 |
switch ($arg[0]) { |
|---|
| 567 |
case "--help": case "h": case "?": |
|---|
| 568 |
showHelp(); |
|---|
| 569 |
exit(10); |
|---|
| 570 |
break; |
|---|
| 571 |
case "--dry-run": |
|---|
| 572 |
out("Dry-run only, nothing will be changed"); |
|---|
| 573 |
$dryrun = true; |
|---|
| 574 |
break; |
|---|
| 575 |
case "--debug": |
|---|
| 576 |
$debug = true; |
|---|
| 577 |
debug("Debug mode enabled"); |
|---|
| 578 |
break; |
|---|
| 579 |
case "--username": case "u": |
|---|
| 580 |
out("Using username: ".$arg[1]); |
|---|
| 581 |
$new_username = $arg[1]; |
|---|
| 582 |
break; |
|---|
| 583 |
case "--password": case "p": |
|---|
| 584 |
out("Using password: ".str_repeat("*",strlen($arg[1]))); |
|---|
| 585 |
$new_password = $arg[1]; |
|---|
| 586 |
break; |
|---|
| 587 |
case "--force-version": |
|---|
| 588 |
$version = $arg[1]; |
|---|
| 589 |
out("Forcing upgrade from version ".$version); |
|---|
| 590 |
break; |
|---|
| 591 |
case "--dbhost": |
|---|
| 592 |
$dbhost = $arg[1]; |
|---|
| 593 |
out("Using remote database server at ".$dbhost); |
|---|
| 594 |
break; |
|---|
| 595 |
case "--no-files": |
|---|
| 596 |
$install_files = false; |
|---|
| 597 |
out("Running upgrade only, without installing files."); |
|---|
| 598 |
break; |
|---|
| 599 |
} |
|---|
| 600 |
} |
|---|
| 601 |
|
|---|
| 602 |
|
|---|
| 603 |
|
|---|
| 604 |
|
|---|
| 605 |
outn("Checking user.."); |
|---|
| 606 |
|
|---|
| 607 |
$euid = (posix_getpwuid(posix_geteuid())); |
|---|
| 608 |
$current_user = $euid['name']; |
|---|
| 609 |
if ($current_user != "root"){ |
|---|
| 610 |
out("FAILED"); |
|---|
| 611 |
fatal($argv[0]." must be run as root"); |
|---|
| 612 |
} |
|---|
| 613 |
out("OK"); |
|---|
| 614 |
|
|---|
| 615 |
|
|---|
| 616 |
|
|---|
| 617 |
|
|---|
| 618 |
outn("Checking for ".AMP_CONF.".."); |
|---|
| 619 |
if (!file_exists(AMP_CONF)) { |
|---|
| 620 |
out(AMP_CONF." does not exist, copying default"); |
|---|
| 621 |
copy("amportal.conf", "/etc/amportal.conf"); |
|---|
| 622 |
collect_settings(AMP_CONF, $dbhost, $new_username, $new_password); |
|---|
| 623 |
} |
|---|
| 624 |
out("OK"); |
|---|
| 625 |
|
|---|
| 626 |
|
|---|
| 627 |
|
|---|
| 628 |
outn("Reading ".AMP_CONF.".."); |
|---|
| 629 |
$amp_conf = parse_amportal_conf(AMP_CONF); |
|---|
| 630 |
if (count($amp_conf) == 0) { |
|---|
| 631 |
fatal("FAILED"); |
|---|
| 632 |
} |
|---|
| 633 |
out("OK"); |
|---|
| 634 |
|
|---|
| 635 |
|
|---|
| 636 |
|
|---|
| 637 |
if (!array_key_exists("FOPWEBROOT",$amp_conf) || |
|---|
| 638 |
!array_key_exists("AMPBIN",$amp_conf) || |
|---|
| 639 |
!array_key_exists("AMPSBIN",$amp_conf) || |
|---|
| 640 |
!array_key_exists("AMPCGIBIN",$amp_conf) || |
|---|
| 641 |
!array_key_exists("AMPWEBROOT",$amp_conf) |
|---|
| 642 |
) { |
|---|
| 643 |
|
|---|
| 644 |
if (!array_key_exists("AMPWEBROOT",$amp_conf)) { |
|---|
| 645 |
out("Adding AMPWEBROOT option to amportal.conf - using AMP default"); |
|---|
| 646 |
$amp_conf["AMPWEBROOT"] = "/var/www/html"; |
|---|
| 647 |
} |
|---|
| 648 |
|
|---|
| 649 |
if (!array_key_exists("AMPCGIBIN",$amp_conf)) { |
|---|
| 650 |
out("Adding AMPCGIBIN option to amportal.conf - using AMP default"); |
|---|
| 651 |
$amp_conf["AMPCGIBIN"] = "/var/www/cgi-bin"; |
|---|
| 652 |
} |
|---|
| 653 |
|
|---|
| 654 |
if (!array_key_exists("FOPWEBROOT",$amp_conf)) { |
|---|
| 655 |
out("Adding FOPWEBROOT option to amportal.conf - using AMP default"); |
|---|
| 656 |
$amp_conf["FOPWEBROOT"] = $amp_conf["AMPWEBROOT"]."/panel"; |
|---|
| 657 |
} |
|---|
| 658 |
|
|---|
| 659 |
if (!array_key_exists("AMPBIN",$amp_conf)) { |
|---|
| 660 |
out("Adding AMPBIN option to amportal.conf - using AMP default"); |
|---|
| 661 |
$amp_conf["AMPBIN"] = "/var/lib/asterisk/bin"; |
|---|
| 662 |
} |
|---|
| 663 |
|
|---|
| 664 |
if (!array_key_exists("AMPSBIN",$amp_conf)) { |
|---|
| 665 |
out("Adding AMPSBIN option to amportal.conf - using AMP default"); |
|---|
| 666 |
$amp_conf["AMPSBIN"] = "/usr/sbin"; |
|---|
| 667 |
} |
|---|
| 668 |
|
|---|
| 669 |
|
|---|
| 670 |
write_amportal_conf(AMP_CONF, $amp_conf); |
|---|
| 671 |
} |
|---|
| 672 |
|
|---|
| 673 |
if (isset($new_username) || isset($new_password) || isset($dbhost)) { |
|---|
| 674 |
|
|---|
| 675 |
|
|---|
| 676 |
if (isset($new_username)) { |
|---|
| 677 |
$amp_conf["AMPDBUSER"] = $new_username; |
|---|
| 678 |
} |
|---|
| 679 |
if (isset($new_password)) { |
|---|
| 680 |
$amp_conf["AMPDBPASS"] = $new_password; |
|---|
| 681 |
} |
|---|
| 682 |
|
|---|
| 683 |
if (isset($dbhost)) { |
|---|
| 684 |
$amp_conf["AMPDBHOST"] = $dbhost; |
|---|
| 685 |
} |
|---|
| 686 |
|
|---|
| 687 |
|
|---|
| 688 |
write_amportal_conf(AMP_CONF, $amp_conf); |
|---|
| 689 |
} |
|---|
| 690 |
|
|---|
| 691 |
|
|---|
| 692 |
|
|---|
| 693 |
outn("Checking for ".ASTERISK_CONF.".."); |
|---|
| 694 |
if (!file_exists(ASTERISK_CONF)) { |
|---|
| 695 |
out(ASTERISK_CONF." does not exist, copying default"); |
|---|
| 696 |
copy("asterisk.conf", "/etc/asterisk/asterisk.conf"); |
|---|
| 697 |
|
|---|
| 698 |
//collect_ast_settings(ASTERISK_CONF, $dbhost, $new_username, $new_password); |
|---|
| 699 |
} |
|---|
| 700 |
out("OK"); |
|---|
| 701 |
|
|---|
| 702 |
|
|---|
| 703 |
|
|---|
| 704 |
outn("Reading ".ASTERISK_CONF.".."); |
|---|
| 705 |
$asterisk_conf = parse_asterisk_conf(ASTERISK_CONF); |
|---|
| 706 |
if (count($asterisk_conf) == 0) { |
|---|
| 707 |
fatal("FAILED. Have you installed Asterisk?"); |
|---|
| 708 |
} |
|---|
| 709 |
out("OK"); |
|---|
| 710 |
|
|---|
| 711 |
|
|---|
| 712 |
|
|---|
| 713 |
outn("Connecting to database.."); |
|---|
| 714 |
|
|---|
| 715 |
$db_user = $amp_conf["AMPDBUSER"]; |
|---|
| 716 |
$db_pass = $amp_conf["AMPDBPASS"]; |
|---|
| 717 |
$db_host = $amp_conf["AMPDBHOST"]; |
|---|
| 718 |
$db_name = 'asterisk'; |
|---|
| 719 |
$db_engine = 'mysql'; |
|---|
| 720 |
|
|---|
| 721 |
$datasource = $db_engine.'://'.$db_user.':'.$db_pass.'@'.$db_host.'/'.$db_name; |
|---|
| 722 |
|
|---|
| 723 |
if (!function_exists($db_engine.'_connect')) { |
|---|
| 724 |
out("FAILED"); |
|---|
| 725 |
fatal($db_engine." PHP libraries not installed"); |
|---|
| 726 |
} |
|---|
| 727 |
|
|---|
| 728 |
$db = DB::connect($datasource); |
|---|
| 729 |
|
|---|
| 730 |
if(DB::isError($db)) { |
|---|
| 731 |
out("FAILED"); |
|---|
| 732 |
debug($db->userinfo); |
|---|
| 733 |
out("Try running ".$argv[0]." --username=user --password=pass (using your own user and pass)"); |
|---|
| 734 |
fatal("Cannot connect to database"); |
|---|
| 735 |
|
|---|
| 736 |
} |
|---|
| 737 |
out("OK"); |
|---|
| 738 |
|
|---|
| 739 |
|
|---|
| 740 |
|
|---|
| 741 |
|
|---|
| 742 |
if (!isset($version)) { |
|---|
| 743 |
outn("Checking current version of AMP.."); |
|---|
| 744 |
$version = getversion(); |
|---|
| 745 |
if (!$version) { |
|---|
| 746 |
out("no version information"); |
|---|
| 747 |
out("Assuming new installation"); |
|---|
| 748 |
} else { |
|---|
| 749 |
out($version); |
|---|
| 750 |
} |
|---|
| 751 |
} |
|---|
| 752 |
|
|---|
| 753 |
|
|---|
| 754 |
|
|---|
| 755 |
|
|---|
| 756 |
if ($install_files) |
|---|
| 757 |
{ |
|---|
| 758 |
outn("Installing new AMP files.."); |
|---|
| 759 |
$check_md5s=true; |
|---|
| 760 |
$md5sums = read_md5_file(UPGRADE_DIR."/".$version.".md5"); |
|---|
| 761 |
recursive_copy("amp_conf", "", $md5sums); |
|---|
| 762 |
if (!is_file("/etc/asterisk/voicemail.conf")) exec("cp /etc/asterisk/voicemail.conf.template /etc/asterisk/voicemail.conf"); |
|---|
| 763 |
if (!is_dir("/var/spool/asterisk/voicemail/device")) exec("mkdir /var/spool/asterisk/voicemail/device"); |
|---|
| 764 |
out("OK"); |
|---|
| 765 |
} |
|---|
| 766 |
|
|---|
| 767 |
|
|---|
| 768 |
debug("Running ".dirname(__FILE__)."/apply_conf.sh"); |
|---|
| 769 |
outn("Configuring install for your environment.."); |
|---|
| 770 |
if (!$dryrun) { |
|---|
| 771 |
if (file_exists($amp_conf["AMPSBIN"]."/amportal")) |
|---|
| 772 |
exec("chmod u+x ".$amp_conf["AMPSBIN"]."/amportal"); |
|---|
| 773 |
exec(dirname(__FILE__)."/apply_conf.sh"); |
|---|
| 774 |
} |
|---|
| 775 |
out("OK"); |
|---|
| 776 |
|
|---|
| 777 |
|
|---|
| 778 |
if (!is_dir($asterisk_conf["astspooldir"]."/monitor")) |
|---|
| 779 |
mkdir($asterisk_conf["astspooldir"]."/monitor",766); |
|---|
| 780 |
if (!is_dir($asterisk_conf["astspooldir"]."/fax")) |
|---|
| 781 |
mkdir($asterisk_conf["astspooldir"]."/fax",766); |
|---|
| 782 |
|
|---|
| 783 |
|
|---|
| 784 |
|
|---|
| 785 |
|
|---|
| 786 |
if ($install_files) |
|---|
| 787 |
{ |
|---|
| 788 |
outn("Setting permissions on files.."); |
|---|
| 789 |
if (!$dryrun) { |
|---|
| 790 |
exec($amp_conf["AMPSBIN"]."/amportal chown"); |
|---|
| 791 |
} |
|---|
| 792 |
out("OK"); |
|---|
| 793 |
} |
|---|
| 794 |
|
|---|
| 795 |
|
|---|
| 796 |
|
|---|
| 797 |
|
|---|
| 798 |
outn("Checking for upgrades.."); |
|---|
| 799 |
|
|---|
| 800 |
|
|---|
| 801 |
if (!isset($versions)) { |
|---|
| 802 |
$versions = array(); |
|---|
| 803 |
$dir = opendir(UPGRADE_DIR); |
|---|
| 804 |
while ($file = readdir($dir)) { |
|---|
| 805 |
if (($file[0] != ".") && is_dir(UPGRADE_DIR."/".$file)) { |
|---|
| 806 |
$versions[] = $file; |
|---|
| 807 |
} |
|---|
| 808 |
} |
|---|
| 809 |
closedir($dir); |
|---|
| 810 |
|
|---|
| 811 |
|
|---|
| 812 |
usort($versions, "version_compare"); |
|---|
| 813 |
} |
|---|
| 814 |
|
|---|
| 815 |
if (false !== ($pos = array_search($version, $versions))) { |
|---|
| 816 |
$upgrades = array_slice($versions, $pos+1); |
|---|
| 817 |
out(count($upgrades)." found"); |
|---|
| 818 |
|
|---|
| 819 |
run_upgrade($upgrades); |
|---|
| 820 |
} else { |
|---|
| 821 |
out("Current version not found"); |
|---|
| 822 |
} |
|---|
| 823 |
|
|---|
| 824 |
|
|---|
| 825 |
|
|---|
| 826 |
out("Generating AMP configs.."); |
|---|
| 827 |
generate_configs(); |
|---|
| 828 |
out("Generating AMP configs..OK"); |
|---|
| 829 |
|
|---|
| 830 |
|
|---|
| 831 |
outn("Restarting Flash Operator Panel.."); |
|---|
| 832 |
exec('su - asterisk -c "'.$amp_conf["AMPWEBROOT"].'/admin/bounce_op.sh"'); |
|---|
| 833 |
out("OK"); |
|---|
| 834 |
|
|---|
| 835 |
|
|---|
| 836 |
|
|---|
| 837 |
needreload(); |
|---|
| 838 |
|
|---|
| 839 |
if ($amp_conf["AMPWEBADDRESS"]) |
|---|
| 840 |
{ |
|---|
| 841 |
out("Please Reload Asterisk by visiting http://".$amp_conf["AMPWEBADDRESS"]."/admin"); |
|---|
| 842 |
} |
|---|
| 843 |
else |
|---|
| 844 |
{ |
|---|
| 845 |
out("Please Reload Asterisk by browsing your server."); |
|---|
| 846 |
} |
|---|
| 847 |
|
|---|
| 848 |
?> |
|---|
| 849 |
|
|---|