| | 414 | } |
|---|
| | 415 | |
|---|
| | 416 | /** |
|---|
| | 417 | * Extract an archive by using GNU and non-GNU tools |
|---|
| | 418 | * @param string $file is the full or relative path to the archive |
|---|
| | 419 | * @param string $dest is the path of the extract destination |
|---|
| | 420 | * @return boolean != 0 on error |
|---|
| | 421 | */ |
|---|
| | 422 | function brouteur_extract($file, $dest="./") |
|---|
| | 423 | { |
|---|
| | 424 | static $i=0, $ret; |
|---|
| | 425 | $file = addslashes($file); |
|---|
| | 426 | $dest = addslashes($dest); |
|---|
| | 427 | if ($i == 0) { |
|---|
| | 428 | #TODO new version of tar supports `tar xf ...` so there is no |
|---|
| | 429 | # need to specify the compression format |
|---|
| | 430 | exec("tar -xzf '$file' -C '$dest'", $void, $ret); |
|---|
| | 431 | } else if ($i == 1) { |
|---|
| | 432 | exec("tar -xjf '$file' -C '$dest'", $void, $ret); |
|---|
| | 433 | } else if ($i == 2) { |
|---|
| | 434 | exec("unzip '$file' -d '$dest'", $void, $ret); |
|---|
| | 435 | } else if ($i == 3) { |
|---|
| | 436 | #FIXME I suck at extracting a rar archive to a specified directory |
|---|
| | 437 | # but I think unrar just sucks <TM> |
|---|
| | 438 | $rarfile = ereg_replace('[a-z0-9]+', '..', $dest)."/$file"; |
|---|
| | 439 | exec("mkdir -p '$dest'"); |
|---|
| | 440 | exec("cd '$dest' && unrar x -o+ '$rarfile'", $void, $ret); |
|---|
| | 441 | } else { |
|---|
| | 442 | return $ret; |
|---|
| | 443 | } |
|---|
| | 444 | |
|---|
| | 445 | if ($ret) { |
|---|
| | 446 | $i++; |
|---|
| | 447 | brouteur_extract($file, $dest); |
|---|
| | 448 | } |
|---|
| | 449 | return $ret; |
|---|
| | 450 | } |
|---|
| | 451 | |
|---|
| | 452 | |
|---|
| | 453 | /** |
|---|
| | 454 | * Copy a source to a destination by either copying recursively a |
|---|
| | 455 | * directory or by downloading a file with a URL (only http:// is |
|---|
| | 456 | * supported) |
|---|
| | 457 | * @param string $name is the application name |
|---|
| | 458 | * @param string $src is the path or URL |
|---|
| | 459 | * @param string $dest is the absolute path inside the users directory |
|---|
| | 460 | * @return boolean false on error |
|---|
| | 461 | */ |
|---|
| | 462 | function brouteur_copy($name, $src, $dest) |
|---|
| | 463 | { |
|---|
| | 464 | global $error, $db; |
|---|
| | 465 | |
|---|
| | 466 | $ok = false; |
|---|
| | 467 | @mkdir($dest, 0777); |
|---|
| | 468 | @chmod($dest, 0777); |
|---|
| | 469 | $f = @fopen("$dest/test.php", 'w'); |
|---|
| | 470 | if ($f) { |
|---|
| | 471 | @fputs($f, '<?php $ok = true; ?>'); |
|---|
| | 472 | @fclose($f); |
|---|
| | 473 | @chmod("$dest/test.php", 0777); |
|---|
| | 474 | include("$dest/test.php"); |
|---|
| | 475 | } |
|---|
| | 476 | @unlink("$dest/test.php"); |
|---|
| | 477 | @rmdir("$dest"); |
|---|
| | 478 | if (!$ok) { |
|---|
| | 479 | $error = _("No write permissions in the destination directory"); |
|---|
| | 480 | return false; |
|---|
| | 481 | } |
|---|
| | 482 | |
|---|
| | 483 | if (substr($src, 0, 7) == "http://") { |
|---|
| | 484 | $filename = basename($src); |
|---|
| | 485 | $extractdir = tempnam("/tmp", "brouteur"); |
|---|
| | 486 | unlink($extractdir); |
|---|
| | 487 | mkdir($extractdir); |
|---|
| | 488 | |
|---|
| | 489 | if (!$http = @fopen($src, "rb")) { |
|---|
| | 490 | // Try to get a handle on $http with fsockopen instead |
|---|
| | 491 | ereg('^http://([^/]+)(/.*)$', $src, $eregs); |
|---|
| | 492 | $hostname = $eregs[1]; |
|---|
| | 493 | $path = $eregs[2]; |
|---|
| | 494 | $http = @fsockopen($hostname, 80); |
|---|
| | 495 | @fputs($http, "GET $path HTTP/1.1\nHost: $hostname\n\n"); |
|---|
| | 496 | } |
|---|
| | 497 | if ($http) { |
|---|
| | 498 | // Save the bits |
|---|
| | 499 | $f = fopen("$extractdir/$filename", "wb"); |
|---|
| | 500 | while (!feof($http)) { |
|---|
| | 501 | $bin = fgets($http, 16384); |
|---|
| | 502 | fwrite($f, $bin); |
|---|
| | 503 | #FIXME if (!trim($bin)) break; |
|---|
| | 504 | } |
|---|
| | 505 | fclose($f); |
|---|
| | 506 | fclose($http); |
|---|
| | 507 | } else { |
|---|
| | 508 | // Dammit, try with wget than |
|---|
| | 509 | exec("wget -q '$src' -O '$extractdir/$filename'", $void, $ret); |
|---|
| | 510 | if ($ret) { |
|---|
| | 511 | $error = _("Unable to download the web application's package."); |
|---|
| | 512 | return false; |
|---|
| | 513 | } |
|---|
| | 514 | } |
|---|
| | 515 | |
|---|
| | 516 | // Now extract that package |
|---|
| | 517 | if (!brouteur_extract("$extractdir/$filename", $extractdir)) { |
|---|
| | 518 | $error = _("Unable to extract the files"); |
|---|
| | 519 | return false; |
|---|
| | 520 | } |
|---|
| | 521 | unlink("$extractdir/$filename"); |
|---|
| | 522 | |
|---|
| | 523 | // Corrupt $src since we want to copy $extractdir/packagename |
|---|
| | 524 | $hd = opendir($extractdir); |
|---|
| | 525 | while ($file = readdir($hd)) { |
|---|
| | 526 | if ($file != "." && $file != "..") { |
|---|
| | 527 | $src = "$extractdir/$file"; |
|---|
| | 528 | break; |
|---|
| | 529 | } |
|---|
| | 530 | } |
|---|
| | 531 | } |
|---|
| | 532 | |
|---|
| | 533 | // Last step // Copy -R |
|---|
| | 534 | $src = addslashes($src); |
|---|
| | 535 | $dest = addslashes($dest); |
|---|
| | 536 | $array = explode('/', $dest); |
|---|
| | 537 | $dir = ""; |
|---|
| | 538 | foreach ($array as $v) { |
|---|
| | 539 | $dir .= "$v/"; |
|---|
| | 540 | @mkdir($dest); |
|---|
| | 541 | } |
|---|
| | 542 | #TODO write a recursive copy function(?) |
|---|
| | 543 | exec("cp -Rf '$src'/* '$dest'", $void, $ret); |
|---|
| | 544 | if ($ret) { |
|---|
| | 545 | $error = _("Errors happened while copying the source to destination."); |
|---|
| | 546 | return false; |
|---|
| | 547 | } |
|---|
| | 548 | |
|---|
| | 549 | $error = _("The web application has been successfully installed."); |
|---|
| | 550 | return true; |
|---|