/** * @param \PEAR2\Pyrus\PackageFile\v2 * @param int */ function validate(\PEAR2\Pyrus\PackageInterface $pf, $state = \PEAR2\Pyrus\Validate::NORMAL) { $this->errors = new \PEAR2\MultiErrors; if (!$pf->schemaOK) { // this package.xml was created from scratch, not loaded from an existing // package.xml $dom = new \DOMDocument; libxml_use_internal_errors(true); libxml_clear_errors(); $dom->loadXML($pf); $a = $pf->toArray(); if ($a['package']['attribs']['version'] == '2.1') { $schema = \PEAR2\Pyrus\Main::getDataPath() . '/package-2.1.xsd'; // for running out of cvs if (!file_exists($schema)) { $schema = dirname(dirname(dirname(dirname(__DIR__)))) . '/data/package-2.1.xsd'; } } else { $schema = \PEAR2\Pyrus\Main::getDataPath() . '/package-2.0.xsd'; // for running out of cvs if (!file_exists($schema)) { $schema = dirname(dirname(dirname(dirname(__DIR__)))) . '/data/package-2.0.xsd'; } } $dom->schemaValidate($schema); $causes = array(); foreach (libxml_get_errors() as $error) { $this->errors->E_ERROR[] = new \PEAR2\Pyrus\PackageFile\Exception("Line " . $error->line . ': ' . $error->message); } if (count($this->errors)) { throw new \PEAR2\Pyrus\PackageFile\Exception('Invalid package.xml, does' . ' not validate against schema', $this->errors); } } $this->_pf = $pf; $this->_curState = $state; $this->_packageInfo = $this->_pf->toArray(); $this->_packageInfo = $this->_packageInfo['package']; if (!isset($this->_packageInfo) || !is_array($this->_packageInfo)) { return false; } $myversion = self::VERSION; if ($myversion === '@PACKAGE_VERSION@') { // we're running from CVS, assume we're 2.0.0 $myversion = '2.0.0'; } $test = $this->_packageInfo; if (isset($test['dependencies']) && isset($test['dependencies']['required']) && isset($test['dependencies']['required']['pearinstaller']) && isset($test['dependencies']['required']['pearinstaller']['min']) && version_compare($myversion, $test['dependencies']['required']['pearinstaller']['min'], '<')) { $this->errors->E_ERROR[] = new \PEAR2\Pyrus\PackageFile\Exception( 'This package.xml requires PEAR version ' . $test['dependencies']['required']['pearinstaller']['min'] . ' to parse properly, we are version ' . $myversion); } $fail = false; foreach ($pf->contents as $file) { // leverage the hidden iterators to do our validation $name = $file->dir . $file->name; if ($name[0] == '.' && $name[1] == '/') { // name is something like "./doc/whatever.txt" $this->errors->E_ERROR[] = new \PEAR2\Pyrus\Package\Exception( 'File "' . $name . '" cannot begin with "."'); continue; } if (!$this->_validateRole($file->role)) { if (isset($this->_packageInfo['usesrole'])) { $roles = $this->_packageInfo['usesrole']; if (!isset($roles[0])) { $roles = array($roles); } foreach ($roles as $role) { if ($role['role'] = $file->role) { if (isset($role['uri'])) { $package = $role['uri']; } else { $package = \PEAR2\Pyrus\Config:: parsedPackageNameToString(array('package' => $role['package'], 'channel' => $role['channel']), true); } $msg = 'This package contains role "' . $file->role . '" and requires package "' . $package . '" to be used'; $this->errors->E_WARNING[] = new \PEAR2\Pyrus\PackageFile\Exception($msg); } } } $this->errors->E_ERROR[] = new \PEAR2\Pyrus\PackageFile\Exception( 'File "' . $name . '" has invalid role "' . $file->role . '", should be one of ' . implode(', ', \PEAR2\Pyrus\Installer\Role::getValidRoles($this->_pf->getPackageType()))); } if (count($file->tasks) && $this->_curState != \PEAR2\Pyrus\Validate::DOWNLOADING) { // has tasks $save = $file->getArrayCopy(); foreach ($file->tasks as $task => $value) { if ($tagClass = \PEAR2\Pyrus\Task\Common::getTask($task)) { if (!is_array($value) || !isset($value[0])) { $value = array($value); } foreach ($value as $v) { try { $ret = $tagClass::validateXml($this->_pf, $v, $save['attribs'], $file->name); } catch (\PEAR2\Pyrus\Task\Exception $e) { $this->errors->E_ERROR[] = new \PEAR2\Pyrus\PackageFile\Exception('Invalid task $task', $e); } } } else { if (isset($this->_packageInfo['usestask'])) { $roles = $this->_packageInfo['usestask']; if (!isset($roles[0])) { $roles = array($roles); } foreach ($roles as $role) { if ($role['task'] = $task) { if (isset($role['uri'])) { $package = $role['uri']; } else { $package = \PEAR2\Pyrus\Config:: parsedPackageNameToString(array('package' => $role['package'], 'channel' => $role['channel']), true); } $msg = 'This package contains task "' . str_replace($this->_pf->getTasksNs() . ':', '', $task) . '" and requires package "' . $package . '" to be used'; $this->errors->E_WARNING[] = new \PEAR2\Pyrus\PackageFile\Exception($msg); } } } $this->errors->E_ERROR[] = new \PEAR2\Pyrus\PackageFile\Exception( 'Unknown task "' . $task . '" passed in file <file name="' . $name . '">'); } } } } $this->_validateRelease(); if (count($this->errors->E_ERROR)) { throw new \PEAR2\Pyrus\PackageFile\Exception('Invalid package.xml', $this->errors); } try { $validator = \PEAR2\Pyrus\Config::current() ->channelregistry[$this->_pf->channel] ->getValidationObject($this->_pf->name); } catch (\PEAR2\Pyrus\Config\Exception $e) { throw new \PEAR2\Pyrus\PackageFile\Exception( 'Unable to process channel-specific configuration for channel ' . $this->_pf->getChannel(), $e); } catch (\Exception $e) { $valpack = \PEAR2\Pyrus\Config::current() ->channelregistry[$this->_pf->channel]->getValidationPackage(); $this->errors->E_ERROR[] = new \PEAR2\Pyrus\PackageFile\Exception( 'Unknown channel ' . $this->_pf->channel); $this->errors->E_ERROR[] = new \PEAR2\Pyrus\PackageFile\Exception( 'package "' . $this->_pf->channel . '/' . $this->_pf->name . '" cannot be properly validated without validation package "' . $this->_pf->channel . '/' . $valpack['name'] . '-' . $valpack['version'] . '"'); } try { $validator->setPackageFile($this->_pf); $validator->setChannel(\PEAR2\Pyrus\Config::current() ->channelregistry[$this->_pf->channel]); $validator->validate($state); // merge in errors from channel-specific validation $this->errors[] = $validator->getFailures(); } catch (\Exception $e) { $this->errors->E_ERROR[] = $e; } if (count($this->errors->E_ERROR)) { throw new \PEAR2\Pyrus\PackageFile\Exception('Invalid package.xml', $this->errors); } if ($state == \PEAR2\Pyrus\Validate::PACKAGING) { if ($this->_pf->type == 'bundle') { if (!$this->_analyzeBundledPackages()) { throw new \PEAR2\Pyrus\PackageFile\Exception('Invalid package.xml', $this->errors); } } else { if (!$this->_analyzePhpFiles()) { throw new \PEAR2\Pyrus\PackageFile\Exception('Invalid package.xml', $this->errors); } } } return $state; }
function warning($msg) { $package = Config::parsedPackageNameToString($this->_currentPackage, true); $this->errs->E_WARNING[] = new Exception(sprintf($msg, $package)); return true; }
/** * * @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; }