function __construct($debugging = false) { if (!$debugging) { set_exception_handler(array($this, 'exceptionHandler')); } \PEAR2\Pyrus\Logger::attach($this); if (!isset(static::$commandParser)) { $schemapath = \PEAR2\Pyrus\Main::getDataPath() . '/customcommand-2.0.xsd'; $defaultcommands = \PEAR2\Pyrus\Main::getDataPath() . '/built-in-commands.xml'; if (!file_exists($schemapath)) { $schemapath = realpath(__DIR__ . '/../../../data/customcommand-2.0.xsd'); $defaultcommands = realpath(__DIR__ . '/../../../data/built-in-commands.xml'); } $parser = new \PEAR2\Pyrus\XMLParser; $commands = $parser->parse($defaultcommands, $schemapath); $commands = $commands['commands']['command']; if ('@PACKAGE_VERSION@' == '@'.'PACKAGE_VERSION@') { $version = '2.0.0a1'; // running from svn } else { $version = '@PACKAGE_VERSION@'; } static::$commandParser = new \PEAR2\Pyrus\ScriptFrontend(array( 'version' => $version, 'description' => 'Pyrus, the installer for PEAR2', 'name' => 'php ' . basename($_SERVER['argv'][0]) ) ); // set up our custom renderer for help options static::$commandParser->accept(new \PEAR2\Pyrus\ScriptFrontend\Renderer(static::$commandParser)); // set up command-less options and argument static::$commandParser->addOption('verbose', array( 'short_name' => '-v', 'long_name' => '--verbose', 'action' => 'Counter', 'description' => 'increase verbosity' )); static::$commandParser->addOption('paranoid', array( 'short_name' => '-p', 'long_name' => '--paranoid', 'action' => 'Counter', 'description' => 'set or increase paranoia level' )); \PEAR2\Pyrus\PluginRegistry::registerFrontend($this); \PEAR2\Pyrus\PluginRegistry::addCommand($commands); } $term = getenv('TERM'); if (function_exists('posix_isatty') && !posix_isatty(1)) { // output is being redirected to a file or through a pipe } elseif ($term) { if (preg_match('/^(xterm|vt220|linux)/', $term)) { $this->term['bold'] = sprintf("%c%c%c%c", 27, 91, 49, 109); $this->term['normal'] = sprintf("%c%c%c", 27, 91, 109); } elseif (preg_match('/^vt100/', $term)) { $this->term['bold'] = sprintf("%c%c%c%c%c%c", 27, 91, 49, 109, 0, 0); $this->term['normal'] = sprintf("%c%c%c%c%c", 27, 91, 109, 0, 0); } } }
/** * Replace all line endings with windows line endings * * See validateXml() source for the complete list of allowed fields * @param \PEAR2\Pyrus\PackageInterface * @param resource open file pointer, set to the beginning of the file * @param string the eventual final file location (informational only) * @return string */ function startSession($fp, $dest) { $contents = stream_get_contents($fp); \PEAR2\Pyrus\Logger::log(3, "replacing all line endings with \\r\\n in $dest"); $contents = preg_replace("/\r\n|\n\r|\r|\n/", "\r\n", $contents); rewind($fp); ftruncate($fp, 0); fwrite($fp, $contents); return true; }
function getRelativeLocation(\PEAR2\Pyrus\PackageFileInterface $pkg, \PEAR2\Pyrus\PackageFile\v2Iterator\FileTag $file, $retDir = false) { if ($this->md5 === null) { return parent::getRelativeLocation($pkg, $file, $retDir); } $info = parent::getRelativeLocation($pkg, $file, $retDir); $path = \PEAR2\Pyrus\Config::current()->cfg_dir . DIRECTORY_SEPARATOR; if ($retDir) { $filepath = $info[1]; } else { $filepath = $info; } if (@file_exists($path .$filepath)) { // configuration has already been installed, check for modifications // made by the user $md5 = md5_file($path .$filepath); $newmd5 = $pkg->files[$file->packagedname]['attribs']; if (!isset($newmd5['md5sum'])) { $newmd5 = md5_file($pkg->getFilePath($file->packagedname)); } else { $newmd5 = $newmd5['md5sum']; } // first check to see if the user modified the file // next check to see if the config file changed from the last installed version // if both tests are satisfied, install the new file under another name and display a warning if ($md5 !== $this->md5 && $md5 !== $newmd5) { // configuration has been modified, so save our version as // configfile.new-version $old = $filepath; $filepath .= '.new-' . $pkg->version['release']; \PEAR2\Pyrus\Logger::log(0, "WARNING: configuration file $old is being installed as $filepath, " . "you should manually merge in changes to the existing configuration file"); } } if ($retDir) { $info[1] = $filepath; } else { $info = $filepath; } return $info; }
public function listPackages($channel) { $dir = $this->_namePath($channel, ''); if (!@file_exists($dir)) { return array(); } $ret = array(); try { $parser = new \PEAR2\Pyrus\XMLParser(); foreach (new \DirectoryIterator($dir) as $file) { if ($file->isDot()) { continue; } try { foreach (new \DirectoryIterator($file->getPathName()) as $registries) { if ($registries->isDir()) { continue; } $a = $parser->parse($registries->getPathName()); $ret[] = $a['package']['name']; } } catch (\Exception $e) { \PEAR2\Pyrus\Logger::log(0, 'Warning: corrupted XML registry entry: ' . $file->getPathName() . ': ' . $e); } } } catch (\Exception $e) { throw new Exception('Could not open channel directory for ' . 'channel ' . $channel, $e); } return $ret; }
/** * validate a downloaded package against installed packages * * @param $pkg downloaded package package.xml object * @param array $params full list of packages to install * @return bool */ function validateDownloadedPackage(\PEAR2\Pyrus\PackageFileInterface $pkg, $params = array()) { $me = $pkg->channel . '/' . $pkg->name; $reg = Config::current()->registry; $deppackages = $reg->getDependentPackages($pkg); $fail = false; if ($deppackages) { $actual = array(); // first, remove packages that will be installed foreach ($deppackages as $package) { foreach ($params as $packd) { if (strtolower($packd->name) == strtolower($package->name) && $packd->channel == $package->channel) { \PEAR2\Pyrus\Logger::log(3, 'skipping installed package check of "' . Config::parsedPackageNameToString(array('channel' => $package->channel, 'package' => $package->name), true) . '", version "' . $packd->version['release'] . '" will be ' . 'downloaded and installed'); continue 2; } } $actual[] = $package; } foreach ($actual as $package) { $checker = new \PEAR2\Pyrus\Dependency\Validator(array('channel' => $package->channel, 'package' => $package->name), $this->_state, $this->errs); foreach ($params as $packd) { $deps = $package->dependencies['required']->package; if (isset($deps[$me])) { $ret = $checker->_validatePackageDownload($deps[$me], array($pkg, $package)); } $deps = $package->dependencies['required']->subpackage; if (isset($deps[$me])) { $ret = $checker->_validatePackageDownload($deps[$me], array($pkg)); } $deps = $package->dependencies['optional']->package; if (isset($deps[$me])) { $ret = $checker->_validatePackageDownload($deps[$me], array($pkg, $package)); } $deps = $package->dependencies['optional']->subpackage; if (isset($deps[$me])) { $ret = $checker->_validatePackageDownload($deps[$me], array($pkg)); } } } } if (count($this->errs->E_ERROR)) { return $this->raiseError('%s cannot be installed, conflicts with installed packages'); } return true; }
function _runSkipIf($section_text, $temp_skipif, $tested, $ini_settings) { $info = ''; $warn = false; if (array_key_exists('SKIPIF', $section_text) && trim($section_text['SKIPIF'])) { $this->save_text($temp_skipif, $section_text['SKIPIF']); $output = $this->system_with_timeout("{$this->_php}{$ini_settings} -f \"{$temp_skipif}\""); $output = $output[1]; $loutput = ltrim($output); unlink($temp_skipif); if (!strncasecmp('skip', $loutput, 4)) { $skipreason = "SKIP {$tested}"; if (preg_match('/^\\s*skip\\s*(.+)\\s*/i', $output, $m)) { $skipreason .= '(reason: ' . $m[1] . ')'; } if (!isset($this->_options['quiet'])) { \PEAR2\Pyrus\Logger::log(0, $skipreason); } if (isset($this->_options['tapoutput'])) { return array('ok', ' # skip ' . $reason); } return 'SKIPPED'; } if (!strncasecmp('info', $loutput, 4) && preg_match('/^\\s*info\\s*(.+)\\s*/i', $output, $m)) { $info = " (info: {$m['1']})"; } if (!strncasecmp('warn', $loutput, 4) && preg_match('/^\\s*warn\\s*(.+)\\s*/i', $output, $m)) { $warn = true; /* only if there is a reason */ $info = " (warn: {$m['1']})"; } } return array('warn' => $warn, 'info' => $info); }
function _analyzeBundledPackages() { if (!$this->_pf->type == 'bundle') { return false; } if (!$this->_pf->packagefile) { return false; } $dir_prefix = $this->_pf->filepath; foreach ($this->_pf->bundledpackage as $package) { if (!file_exists($dir_prefix . DIRECTORY_SEPARATOR . $package)) { $this->errors->E_ERROR[] = new \PEAR2\Pyrus\PackageFile\Exception( 'File "' . $dir_prefix . DIRECTORY_SEPARATOR . $package . '" in package.xml does not exist'); continue; } \PEAR2\Pyrus\Logger::log(1, "Analyzing bundled package $package"); try { $ret = new \PEAR2\Pyrus\Package\Tar($dir_prefix . DIRECTORY_SEPARATOR . $package); } catch (\Exception $e) { $this->errors->E_ERROR[] = new \PEAR2\Pyrus\PackageFile\Exception( 'File "' . $dir_prefix . DIRECTORY_SEPARATOR . $package . '" in package.xml is not valid', $e); continue; } } return true; }
/** * Extract configuration from system + user configuration files * * Configuration is stored in XML format, in two locations. * * The system configuration contains all of the important directory * configuration variables like data_dir, and the location of php.ini and * the php executable php.exe or php. This configuration is tightly bound * to the repository, and cannot be moved. As such, php_dir is auto-defined * as dirname(/path/to/pear/.config), or /path/to/pear. * * Only 1 user configuration file is allowed, and contains user-specific * settings, including the locations where to download package releases * and where to cache files downloaded from the internet. If false is passed * in, \PEAR2\Pyrus\Config will attempt to guess at the config file location as * documented in the class docblock {@link \PEAR2\Pyrus\Config}. * @param string $pearDirectory * @param string|false $userfile */ protected function loadConfigFile($pearDirectory, $snapshot = null) { if (!file_exists($pearDirectory . DIRECTORY_SEPARATOR . '.configsnapshots')) { // no configurations - this may be an extracted-from-disk install. // in this case, we use the defaults, as this is intended return; } $snapshotdir = $pearDirectory . DIRECTORY_SEPARATOR . '.configsnapshots'; $snapshotfile = $snapshotdir . DIRECTORY_SEPARATOR . $snapshot; if (!file_exists($snapshotfile)) { if (preg_match('/^\\d{4}\\-\\d{2}\\-\\d{2} \\d{2}[-:]\\d{2}[-:]\\d{2}$/', $snapshot)) { // passed a date, locate a matching snapshot if (!strpos($snapshot, ':')) { // change YYYY-MM-DD HH-MM-SS to YYYY-MM-DD HH:MM:SS $snapshot = explode(' ', $snapshot); $snapshot[1] = str_replace('-', ':', $snapshot[1]); $snapshot = implode(' ', $snapshot); } $us = new \DateTime($snapshot); $dir = new \RegexIterator(new \RecursiveDirectoryIterator($snapshotdir), '/configsnapshot\\-\\d{4}\\-\\d{2}\\-\\d{2} \\d{2}\\-\\d{2}\\-\\d{2}.xml/', \RegexIterator::MATCH, \RegexIterator::USE_KEY); foreach ($dir as $match) { $matches[] = $match; } usort($matches, array($this, 'datediff')); unset($match); $found = false; foreach ($matches as $match) { $match = substr($match->getFileName(), strlen('configsnapshot-')); $match = str_replace('.xml', '', $match); $match = explode(' ', $match); $match[1] = str_replace('-', ':', $match[1]); $match = implode(' ', $match); $testdate = new \DateTime($match); if ($testdate > $us) { continue; } if ($testdate == $us) { // found a snapshot match $found = true; break; } if ($us > $testdate) { // we fall between these two snapshots, so use this one $found = true; break; } } if (!$found) { // no config snapshots return parent::loadConfigFile($pearDirectory); } $snapshotfile = $snapshotdir . DIRECTORY_SEPARATOR . 'configsnapshot-' . str_replace(':', '-', $match) . '.xml'; } } \PEAR2\Pyrus\Logger::log(5, 'Loading configuration snapshot ' . $snapshotfile . ' for ' . $pearDirectory); try { $this->helperLoadConfigFile($pearDirectory, $snapshotfile, 'snapshot'); } catch (\Exception $e) { // no config snapshots found, so simply load the existing config return parent::loadConfigFile($pearDirectory); } }
/** * Unlike other tasks, the installed file name is passed in instead of the file contents, * because this task is handled post-installation * @param \PEAR2\Pyrus\PackageInterface * @param string path to the post-install script * @return bool false to skip this file */ function setupPostInstall() { $files = \PEAR2\Pyrus\Config::current()->registry->info($this->pkg->name, $this->pkg->channel, 'installedfiles'); foreach ($files as $path => $info) { if ($info['name'] == $this->_filename) { break; } } Logger::log(0, 'Including external post-installation script "' . $path . '" - any errors are in this script'); include $path; if (class_exists($this->scriptClass) === false) { throw new Exception('init of post-install script class "' . $this->scriptClass . '" failed'); } Logger::log(0, 'Inclusion succeeded'); $this->obj = new $this->scriptClass; Logger::log(1, 'running post-install script "' . $this->scriptClass . '->init()"'); try { $this->obj->init2($this->pkg, $this->lastVersion); } catch (\Exception $e) { throw new Exception('init of post-install script "' . $this->scriptClass . '->init()" failed', $e); } Logger::log(0, 'init succeeded'); return true; }
final function packageTimeValidate(\PEAR2\Pyrus\Package $package, array $fileXml) { if (!isset($this->info['validationmethod'])) { return true; } if (!method_exists($this, $this->info['validationmethod'])) { \PEAR2\Pyrus\Logger::log(0, 'WARNING: custom role ' . $this->info['name'] . ' specifies non-existing validation method ' . $this->info['validationmethod']); return true; } return $this->{$this->info['validationmethod']}($package, $fileXml); }
/** * Do a package.xml 1.0 replacement, with additional package-info fields available * * See validateXml() source for the complete list of allowed fields * @param \PEAR2\Pyrus\PackageInterface * @param resource open file pointer, set to the beginning of the file * @param string the eventual final file location (informational only) * @return string|false */ function startSession($fp, $dest) { $contents = stream_get_contents($fp); $subst_from = $subst_to = array(); foreach ($this->_replacements as $a) { $a = $a['attribs']; $to = ''; if ($a['type'] == 'pear-config') { if ($this->installphase == Common::PACKAGE) { return false; } $to = \PEAR2\Pyrus\Config::current()->{$a['to']}; if (is_null($to)) { \PEAR2\Pyrus\Logger::log(0, "{$dest}: invalid pear-config replacement: {$a['to']}"); return false; } } elseif ($a['type'] == 'php-const') { if ($this->installphase == Common::PACKAGE) { return false; } if (defined($a['to'])) { $to = constant($a['to']); } else { \PEAR2\Pyrus\Logger::log(0, "{$dest}: invalid php-const replacement: {$a['to']}"); return false; } } else { if ($t = $this->pkg->{$a['to']}) { if ($a['to'] == 'version') { $t = $t['release']; } $to = $t; } else { \PEAR2\Pyrus\Logger::log(0, "{$dest}: invalid package-info replacement: {$a['to']}"); return false; } } if (!is_null($to)) { $subst_from[] = $a['from']; $subst_to[] = $to; } } \PEAR2\Pyrus\Logger::log(3, "doing " . sizeof($subst_from) . " substitution(s) for {$dest}"); if (sizeof($subst_from)) { $contents = str_replace($subst_from, $subst_to, $contents); } rewind($fp); ftruncate($fp, 0); fwrite($fp, $contents); return true; }
public function listPackages($channel) { $dir = $this->_namePath($channel, ''); if (!@file_exists($dir)) { return array(); } $ret = array(); try { foreach (new \DirectoryIterator($dir) as $file) { if ($file->isDot() || !$file->isFile()) { continue; } $a = @unserialize(file_get_contents($file->getPathName())); // $a['name'] is not set on v1 regs if ($a !== false && isset($a['name'])) { $ret[] = $a['name']; } elseif ($a !== false && isset($a['package'])) { $ret[] = $a['package']; } else { \PEAR2\Pyrus\Logger::log(0, 'Warning: corrupted REG registry entry: ' . $file->getPathName()); } } } catch (\Exception $e) { throw new Exception('Could not open channel directory for channel ' . $channel, $e); } return $ret; }
/** * * @param string|array pass in an array of format * array( * 'package' => 'pname', * ['channel' => 'channame',] * ['version' => 'version',] * ['state' => 'state',]) * or a string of format [channame/]pname[-version|-state] */ protected function fromString($param) { try { $pname = Config::parsePackageName($param, true); } catch (\PEAR2\Pyrus\ChannelRegistry\ParseException $e) { if ($e->why !== 'channel') { throw new Exception('invalid package name/package file "' . $param . '"', $e); } if (Config::current()->auto_discover) { try { try { $chan = new \PEAR2\Pyrus\Channel( new \PEAR2\Pyrus\ChannelFile('https://' . $e->params['channel'] . '/channel.xml', false, true)); } catch (\Exception $e) { $chan = new \PEAR2\Pyrus\Channel( new \PEAR2\Pyrus\ChannelFile('http://' . $e->params['channel'] . '/channel.xml', false, true)); } } catch (\Exception $e) { throw new Exception('Cannot auto-discover channel ' . $e->params['channel'], $e); } Config::current()->channelregistry[] = $chan; try { Config::parsePackageName($param, Config::current()->default_channel); } catch (\Exception $e) { throw new Exception('invalid package name/package file "' . $param . '"', $e); } } else { \PEAR2\Pyrus\Logger::log(0, 'Channel "' . $param['channel'] . '" is not initialized, use ' . '"pyrus channel-discover ' . $param['channel'] . '" to initialize' . 'or pyrus set auto_discover 1'); } } $this->parsedname = $pname; $this->explicitVersion = isset($pname['version']) ? $pname['version'] : false; $this->explicitState = isset($pname['state']) ? $pname['state'] : false; $this->explicitGroup = isset($pname['group']) ? true : false; $reg = Config::current()->registry; $version = $reg->info($pname['package'], $pname['channel'], 'version'); $stability = $reg->info($pname['package'], $pname['channel'], 'state'); if (!isset(\PEAR2\Pyrus\Main::$options['force']) && !isset(\PEAR2\Pyrus\Main::$options['downloadonly']) && $version && $this->explicitVersion && !isset($pname['group'])) { if (version_compare($version, $pname['version'], '>=')) { throw new InstalledException( Config::parsedPackageNameToString($pname, true) . ' is already installed and is newer than detected ' . 'release version ' . $pname['version']); } } if (!$this->explicitVersion && $stability) { // if installed, use stability of the installed package, // but only if it is less restrictive than preferred_state. // This allows automatic upgrade to a newer beta for 1 package // even if preferred_state is stable, for instance. $states = \PEAR2\Pyrus\Installer::betterStates(Config::current()->preferred_state); $newstates = \PEAR2\Pyrus\Installer::betterStates($stability); if (count($newstates) > count($states)) { $this->explicitState = $stability; } } $this->type = 'abstract'; $ret = $this->getRemotePackage($pname); if ($this->explicitVersion) { $ret->setExplicitVersion($this->explicitVersion); $ret->version['release'] = $this->explicitVersion; } if ($this->explicitState) { $ret->setExplicitState($this->explicitState); } return $ret; }
/** * Figure out which version is best, and use this, or error out if none work * @param \PEAR2\Pyrus\PackageFile\v2\Dependencies\Package $compositeDep * the composite of all dependencies on this package, as calculated * by {@link \PEAR2\Pyrus\Package\Dependency::getCompositeDependency()} */ function figureOutBestVersion(\PEAR2\Pyrus\PackageFile\v2\Dependencies\Package $compositeDep, $versions = null, \PEAR2\Pyrus\PackageFile\v2\Dependencies\Package $compositeConflictingDep = null) { // set up release list if not done yet $this->rewind(); $ok = \PEAR2\Pyrus\Installer::betterStates($this->minimumStability, true); $v = $this->explicitVersion; $n = $this->channel . '/' . $this->name; $failIfExplicit = function ($versioninfo) use($v, $n) { if ($v && $versioninfo['v'] == $v) { throw new Exception($n . ' Cannot be installed, it does not satisfy ' . 'all dependencies'); } }; foreach ($this->releaseList as $versioninfo) { if (isset(\PEAR2\Pyrus\Main::$options['force'])) { // found one if ($this->versionSet && $versioninfo['v'] != $this->version['release']) { // inform the installer we need to reset dependencies $this->version['release'] = $versioninfo['v']; return true; } $this->version['release'] = $versioninfo['v']; return; } if ($versions && !in_array($versioninfo['v'], $versions)) { continue; } if (!isset(\PEAR2\Pyrus\Main::$options['force']) && isset($versioninfo['m'])) { // minimum PHP version required if (version_compare($versioninfo['m'], $this->getPHPVersion(), '>')) { $failIfExplicit($versioninfo); continue; } } if (!in_array($versioninfo['s'], $ok) && !isset(\PEAR2\Pyrus\Main::$options['force'])) { // release is not stable enough continue; } if ($this->explicitVersion && $versioninfo['v'] != $this->explicitVersion) { continue; } if (!$compositeDep->satisfied($versioninfo['v'])) { $failIfExplicit($versioninfo); continue; } if ($compositeConflictingDep && !$compositeConflictingDep->satisfied($versioninfo['v'])) { $failIfExplicit($versioninfo); continue; } $paranoia = \PEAR2\Pyrus\Main::getParanoiaLevel(); if (!$this->explicitVersion && $paranoia > 1) { // first, we check to see if we are upgrading if (isset(\PEAR2\Pyrus\Main::$options['upgrade'])) { // now we check to see if we are installed if (isset(Config::current()->registry->package[$n])) { $installed = Config::current()->registry->info($this->name, $this->channel, 'apiversion'); $installed = explode('.', $installed); if (count($installed) == 2) { $installed[] = '0'; } if (count($installed) == 1) { $installed[] = '0'; $installed[] = '0'; } if (isset($this->parent->protocols->rest['REST1.3'])) { $api = $this->rest->retrieveCacheFirst($this->parent->protocols->rest['REST1.3']->baseurl . 'r/' . strtolower($this->name) . '/v2.' . $versioninfo['v'] . '.xml'); } else { throw new Exception('Channel ' . $this->channel . ' does not support ' . 'a paranoia greater than 1'); } $api = explode('.', $api['a']); if (count($api) == 2) { $api[] = '0'; } if (count($api) == 1) { $api[] = '0'; $api[] = '0'; } if ($paranoia > 4) { $paranoia = 4; } switch ($paranoia) { case 4: if ($installed != $api) { Logger::log(0, 'Skipping ' . $this->channel . '/' . $this->name . ' version ' . $versioninfo['v'] . ', API has changed'); continue 2; } break; case 3: if ($installed[0] == $api[0] && $installed[1] != $api[1]) { Logger::log(0, 'Skipping ' . $this->channel . '/' . $this->name . ' version ' . $versioninfo['v'] . ', API has added' . ' new features'); continue 2; } // break intentionally omitted // break intentionally omitted case 2: if ($installed[0] != $api[0]) { Logger::log(0, 'Skipping ' . $this->channel . '/' . $this->name . ' version ' . $versioninfo['v'] . ', API breaks' . ' backwards compatibility'); continue 2; } break; } } } } // found one if ($this->versionSet && $versioninfo['v'] != $this->version['release']) { // inform the installer we need to reset dependencies $this->version['release'] = $versioninfo['v']; return true; } $this->version['release'] = $versioninfo['v']; return; } throw new Exception('Unable to locate a package release for ' . $this->channel . '/' . $this->name . ' that can satisfy all dependencies'); }