b55022
b55022
while (@ob_end_flush());
b55022
/* $Id$ */
b55022
b55022
error_reporting(1803);
b55022
b55022
if (ini_get('date.timezone') === '' && function_exists('date_default_timezone_set')) {
b55022
    date_default_timezone_set('UTC');
b55022
}
b55022
b55022
$pear_dir = dirname(__FILE__);
b55022
ini_set('include_path', '');
b55022
if (function_exists('mb_internal_encoding')) {
b55022
    mb_internal_encoding('ASCII');
b55022
}
b55022
set_time_limit(0);
b55022
include_once 'PEAR.php';
b55022
include_once 'PEAR/Installer.php';
b55022
include_once 'PEAR/Registry.php';
b55022
include_once 'PEAR/PackageFile.php';
b55022
include_once 'PEAR/Downloader/Package.php';
b55022
include_once 'PEAR/Frontend.php';
b55022
$a = true;
b55022
if (!PEAR::loadExtension('xml')) {
b55022
    $a = false;
b55022
    echo "[PEAR] xml extension is required\n";
b55022
}
b55022
if (!PEAR::loadExtension('pcre')) {
b55022
    $a = false;
b55022
    echo "[PEAR] pcre extension is required\n";
b55022
}
b55022
if (!$a) {
b55022
    return -1;
b55022
}
b55022
b55022
$force = false;
b55022
$install_files = array();
b55022
array_shift($argv);
b55022
$debug = false;
b55022
for ($i = 0; $i < sizeof($argv); $i++) {
b55022
    $arg = $argv[$i];
b55022
    $bn = basename($arg);
b55022
    if (preg_match('/package-(.*)\.xml$/', $bn, $matches) ||
b55022
        preg_match('/([A-Za-z0-9_:]+)-.*\.(tar|tgz)$/', $bn, $matches)) {
b55022
        $install_files[$matches[1]] = $arg;
b55022
    } elseif ($arg == '-a' || $arg == '--cache') {
b55022
        $cache_dir = $argv[$i+1];
b55022
        $i++;
b55022
    } elseif ($arg == '--force') {
b55022
        $force = true;
b55022
    } elseif ($arg == '-dp') {
b55022
        $prefix = $argv[$i+1];
b55022
        $i++;
b55022
    } elseif ($arg == '-ds') {
b55022
        $suffix = $argv[$i+1];
b55022
        $i++;
b55022
    } elseif ($arg == '-d' || $arg == '--dir') {
b55022
        $with_dir = $argv[$i+1];
b55022
        $i++;
b55022
    } elseif ($arg == '-b' || $arg == '--bin') {
b55022
        $bin_dir = $argv[$i+1];
b55022
        $i++;
b55022
    } elseif ($arg == '-c' || $arg == '--config') {
b55022
        $cfg_dir = $argv[$i+1];
b55022
        $i++;
b55022
    } elseif ($arg == '-w' || $arg == '--www') {
b55022
        $www_dir = $argv[$i+1];
b55022
        $i++;
b55022
    } elseif ($arg == '-p' || $arg == '--php') {
b55022
        $php_bin = $argv[$i+1];
b55022
        $i++;
b55022
    } elseif ($arg == '-o' || $arg == '--download') {
b55022
        $download_dir = $argv[$i+1];
b55022
        $i++;
b55022
    } elseif ($arg == '-m' || $arg == '--metadata') {
b55022
        $metadata_dir = $argv[$i+1];
b55022
        $i++;
b55022
    } elseif ($arg == '-t' || $arg == '--temp') {
b55022
        $temp_dir = $argv[$i+1];
b55022
        $i++;
b55022
    } elseif ($arg == '-A' || $arg == '--data') {
b55022
        $data_dir = $argv[$i+1];
b55022
        $i++;
b55022
    } elseif ($arg == '-D' || $arg == '--doc') {
b55022
        $doc_dir = $argv[$i+1];
b55022
        $i++;
b55022
    } elseif ($arg == '-T' || $arg == '--test') {
b55022
        $test_dir = $argv[$i+1];
b55022
        $i++;
b55022
    } elseif ($arg == '--debug') {
b55022
        $debug = 1;
b55022
    } elseif ($arg == '--extremedebug') {
b55022
        $debug = 2;
b55022
    }
b55022
}
b55022
b55022
$config = PEAR_Config::singleton();
b55022
b55022
if (PEAR::isError($config)) {
b55022
    $locs = PEAR_Config::getDefaultConfigFiles();
b55022
    die("ERROR: One of $locs[user] or $locs[system] is corrupt, please remove them and try again");
b55022
}
b55022
b55022
// make sure we use only default values
b55022
$config_layers = $config->getLayers();
b55022
foreach ($config_layers as $layer) {
b55022
    if ($layer == 'default') continue;
b55022
    $config->removeLayer($layer);
b55022
}
b55022
$keys = $config->getKeys();
b55022
if ($debug) {
b55022
    $config->set('verbose', 5, 'default');
b55022
} else {
b55022
    $config->set('verbose', 0, 'default');
b55022
}
b55022
// PEAR executables
b55022
if (!empty($bin_dir)) {
b55022
    $config->set('bin_dir', $bin_dir, 'default');
b55022
}
b55022
b55022
// Cache files
b55022
if (!empty($cache_dir)) {
b55022
    $config->set('cache_dir', $cache_dir, 'default');
b55022
}
b55022
b55022
// Config files
b55022
if (!empty($cfg_dir)) {
b55022
    $config->set('cfg_dir', $cfg_dir, 'default');
b55022
}
b55022
b55022
// Web files
b55022
if (!empty($www_dir)) {
b55022
    $config->set('www_dir', $www_dir, 'default');
b55022
}
b55022
b55022
// Downloaded files
b55022
if (!empty($download_dir)) {
b55022
    $config->set('download_dir', $download_dir, 'default');
b55022
}
b55022
b55022
// Temporary files
b55022
if (!empty($temp_dir)) {
b55022
    $config->set('temp_dir', $temp_dir, 'default');
b55022
}
b55022
b55022
// Documentation files
b55022
if (!empty($doc_dir)) {
b55022
    $config->set('doc_dir', $doc_dir, 'default');
b55022
}
b55022
b55022
// Data files
b55022
if (!empty($data_dir)) {
b55022
    $config->set('data_dir', $data_dir, 'default');
b55022
}
b55022
b55022
// Unit tests
b55022
if (!empty($test_dir)) {
b55022
    $config->set('test_dir', $test_dir, 'default');
b55022
}
b55022
b55022
// User supplied a dir prefix
b55022
if (!empty($with_dir)) {
b55022
    $ds = DIRECTORY_SEPARATOR;
b55022
    $config->set('php_dir', $with_dir, 'default');
b55022
    // Metadata
b55022
    if (!empty($metadata_dir)) {
b55022
        $config->set('metadata_dir', $metadata_dir, 'default');
b55022
    }
b55022
    if (empty($doc_dir)) {
b55022
        $config->set('doc_dir', $with_dir . $ds . 'doc', 'default');
b55022
    }
b55022
    if (empty($data_dir)) {
b55022
        $config->set('data_dir', $with_dir . $ds . 'data', 'default');
b55022
    }
b55022
    if (empty($test_dir)) {
b55022
        $config->set('test_dir', $with_dir . $ds . 'test', 'default');
b55022
    }
b55022
    if (empty($www_dir)) {
b55022
        $config->set('www_dir', $with_dir . $ds . 'htdocs', 'default');
b55022
    }
b55022
    if (empty($cfg_dir)) {
b55022
        $config->set('cfg_dir', $with_dir . $ds . 'cfg', 'default');
b55022
    }
b55022
    if (!is_writable($config->get('cache_dir'))) {
b55022
        include_once 'System.php';
b55022
        $cdir = System::mktemp(array('-d', 'pear'));
b55022
        if (PEAR::isError($cdir)) {
b55022
            $ui->outputData("[PEAR] cannot make new temporary directory: " . $cdir);
b55022
            die(1);
b55022
        }
b55022
        $oldcachedir = $config->get('cache_dir');
b55022
        $config->set('cache_dir', $cdir);
b55022
    }
b55022
}
b55022
b55022
// PHP executable
b55022
if (!empty($php_bin)) {
b55022
    $config->set('php_bin', $php_bin);
b55022
}
b55022
b55022
// PHP prefix
b55022
if (isset($prefix)) {
b55022
    if ($prefix != 'a') {
b55022
        if ($prefix[0] == 'a') {
b55022
            $prefix = substr($prefix, 1);
b55022
        }
b55022
        $config->set('php_prefix', $prefix, 'system');
b55022
    }
b55022
}
b55022
b55022
// PHP suffix
b55022
if (isset($suffix)) {
b55022
    if ($suffix != 'a') {
b55022
        if ($suffix[0] == 'a') {
b55022
            $suffix = substr($suffix, 1);
b55022
        }
b55022
        $config->set('php_suffix', $suffix, 'system');
b55022
    }
b55022
}
b55022
b55022
/* Print PEAR Conf (useful for debuging do NOT REMOVE) */
b55022
if ($debug) {
b55022
    sort($keys);
b55022
    foreach ($keys as $key) {
b55022
        echo $key . '    ' .
b55022
            $config->getPrompt($key) . ": " . $config->get($key, null, 'default') . "\n";
b55022
    }
b55022
    if ($debug == 2) { // extreme debugging
b55022
        exit;
b55022
    }
b55022
}
b55022
// end print
b55022
b55022
$php_dir = $config->get('php_dir');
b55022
$options = array();
b55022
$options['upgrade'] = true;
b55022
$install_root = getenv('INSTALL_ROOT');
b55022
if (!empty($install_root)) {
b55022
    $options['packagingroot'] = $install_root;
b55022
    $reg = &new PEAR_Registry($options['packagingroot'], false, false, $metadata_dir);
b55022
} else {
b55022
    $reg = $config->getRegistry('default');
b55022
}
b55022
b55022
$ui = PEAR_Frontend::singleton('PEAR_Frontend_CLI');
b55022
if (PEAR::isError($ui)) {
b55022
    die($ui->getMessage());
b55022
}
b55022
$installer = new PEAR_Installer($ui);
b55022
$pkg = new PEAR_PackageFile($config, $debug);
b55022
b55022
foreach ($install_files as $package => $instfile) {
b55022
    $info = $pkg->fromAnyFile($instfile, PEAR_VALIDATE_INSTALLING);
b55022
    if (PEAR::isError($info)) {
b55022
        if (is_array($info->getUserInfo())) {
b55022
            foreach ($info->getUserInfo() as $err) {
b55022
                $ui->outputData(sprintf("[PEAR] %s: %s", $package, $err['message']));
b55022
            }
b55022
        }
b55022
        $ui->outputData(sprintf("[PEAR] %s: %s", $package, $info->getMessage()));
b55022
        continue;
b55022
    }
b55022
    $new_ver = $info->getVersion();
b55022
    $downloaderpackage = new PEAR_Downloader_Package($installer);
b55022
    $err = $downloaderpackage->initialize($instfile);
b55022
    if (PEAR::isError($err)) {
b55022
        $ui->outputData(sprintf("[PEAR] %s: %s", $package, $err->getMessage()));
b55022
        continue;
b55022
    }
b55022
    if ($reg->packageExists($package)) {
b55022
        $old_ver = $reg->packageInfo($package, 'version');
b55022
        if (version_compare($new_ver, $old_ver, 'gt')) {
b55022
            $installer->setOptions($options);
b55022
            $dp = array($downloaderpackage);
b55022
            $installer->setDownloadedPackages($dp);
b55022
            $err = $installer->install($downloaderpackage, $options);
b55022
            if (PEAR::isError($err)) {
b55022
                $ui->outputData(sprintf("[PEAR] %s: %s", $package, $err->getMessage()));
b55022
                continue;
b55022
            }
b55022
            $ui->outputData(sprintf("[PEAR] %-15s- upgraded:  %s", $package, $new_ver));
b55022
        } else {
b55022
            if ($force) {
b55022
                $options['force'] = true;
b55022
                $installer->setOptions($options);
b55022
                $dp = array($downloaderpackage);
b55022
                $installer->setDownloadedPackages($dp);
b55022
                $err = $installer->install($downloaderpackage, $options);
b55022
                if (PEAR::isError($err)) {
b55022
                    $ui->outputData(sprintf("[PEAR] %s: %s", $package, $err->getMessage()));
b55022
                    continue;
b55022
                }
b55022
                $ui->outputData(sprintf("[PEAR] %-15s- installed: %s", $package, $new_ver));
b55022
            } else {
b55022
                $ui->outputData(sprintf("[PEAR] %-15s- already installed: %s", $package, $old_ver));
b55022
            }
b55022
        }
b55022
    } else {
b55022
        $options['nodeps'] = true;
b55022
        $installer->setOptions($options);
b55022
        $dp = array($downloaderpackage);
b55022
        $installer->setDownloadedPackages($dp);
b55022
        $err = $installer->install($downloaderpackage, $options);
b55022
        if (PEAR::isError($err)) {
b55022
            $ui->outputData(sprintf("[PEAR] %s: %s", $package, $err->getMessage()));
b55022
            continue;
b55022
        }
b55022
        $ui->outputData(sprintf("[PEAR] %-15s- installed: %s", $package, $new_ver));
b55022
    }
b55022
    if ($package == 'PEAR') {
b55022
        if (is_file($ufile = $config->getConfFile('user'))) {
b55022
            $ui->outputData('Warning! a PEAR user config file already exists from ' .
b55022
                            'a previous PEAR installation at ' .
b55022
                            "'$ufile'. You may probably want to remove it.");
b55022
        }
b55022
        $config->set('verbose', 1, 'default');
b55022
        if (isset($oldcachedir)) {
b55022
            $config->set('cache_dir', $oldcachedir);
b55022
        }
b55022
        $data = array();
b55022
        foreach ($config->getKeys() as $key) {
b55022
            $data[$key] = $config->get($key);
b55022
        }
b55022
        $cnf_file = $config->getConfFile('system');
b55022
        if (!empty($install_root)) {
b55022
            $cnf_file = $install_root . DIRECTORY_SEPARATOR . $cnf_file;
b55022
        }
b55022
        $config->writeConfigFile($cnf_file, 'system', $data);
b55022
        $ui->outputData('Wrote PEAR system config file at: ' . $cnf_file);
b55022
        $ui->outputData('You may want to add: ' . $config->get('php_dir') . ' to your php.ini include_path');
b55022
    }
b55022
}
b55022
?>