3  * PEAR_PackageFile, package.xml parsing utility class
 
   9  * @author     Greg Beaver <cellog@php.net>
 
  10  * @copyright  1997-2009 The Authors
 
  11  * @license    http://opensource.org/licenses/bsd-license.php New BSD License
 
  12  * @version    CVS: $Id: PackageFile.php 313024 2011-07-06 19:51:24Z dufuz $
 
  13  * @link       http://pear.php.net/package/PEAR
 
  14  * @since      File available since Release 1.4.0a1
 
  18  * needed for PEAR_VALIDATE_* constants
 
  20 require_once 'PEAR/Validate.php';
 
  22  * Error code if the package.xml <package> tag does not contain a valid version
 
  24 define('PEAR_PACKAGEFILE_ERROR_NO_PACKAGEVERSION', 1);
 
  26  * Error code if the package.xml <package> tag version is not supported (version 1.0 and 1.1 are the only supported versions,
 
  29 define('PEAR_PACKAGEFILE_ERROR_INVALID_PACKAGEVERSION', 2);
 
  31  * Abstraction for the package.xml package description file
 
  35  * @author     Greg Beaver <cellog@php.net>
 
  36  * @copyright  1997-2009 The Authors
 
  37  * @license    http://opensource.org/licenses/bsd-license.php New BSD License
 
  38  * @version    Release: 1.9.4
 
  39  * @link       http://pear.php.net/package/PEAR
 
  40  * @since      Class available since Release 1.4.0a1
 
  42 class PEAR_PackageFile
 
  54     var $_rawReturn = false;
 
  57      * helper for extracting Archive_Tar errors
 
  61     var $_extractErrors = array();
 
  65      * @param   PEAR_Config $config
 
  67      * @param   string @tmpdir Optional temporary directory for uncompressing
 
  70     function PEAR_PackageFile(&$config, $debug = false)
 
  72         $this->_config = $config;
 
  73         $this->_debug = $debug;
 
  77      * Turn off validation - return a parsed package.xml without checking it
 
  79      * This is used by the package-validate command
 
  83         $this->_rawReturn = true;
 
  86     function setLogger(&$l)
 
  92      * Create a PEAR_PackageFile_Parser_v* of a given version.
 
  94      * @return  PEAR_PackageFile_Parser_v1|PEAR_PackageFile_Parser_v1
 
  96     function &parserFactory($version)
 
  98         if (!in_array($version{0}, array('1', '2'))) {
 
 103         include_once 'PEAR/PackageFile/Parser/v' . $version{0} . '.php';
 
 104         $version = $version{0};
 
 105         $class = "PEAR_PackageFile_Parser_v$version";
 
 111      * For simpler unit-testing
 
 114     function getClassPrefix()
 
 116         return 'PEAR_PackageFile_v';
 
 120      * Create a PEAR_PackageFile_v* of a given version.
 
 121      * @param   int $version
 
 122      * @return  PEAR_PackageFile_v1|PEAR_PackageFile_v1
 
 124     function &factory($version)
 
 126         if (!in_array($version{0}, array('1', '2'))) {
 
 131         include_once 'PEAR/PackageFile/v' . $version{0} . '.php';
 
 132         $version = $version{0};
 
 133         $class = $this->getClassPrefix() . $version;
 
 139      * Create a PEAR_PackageFile_v* from its toArray() method
 
 141      * WARNING: no validation is performed, the array is assumed to be valid,
 
 142      * always parse from xml if you want validation.
 
 144      * @return PEAR_PackageFileManager_v1|PEAR_PackageFileManager_v2
 
 145      * @uses    factory() to construct the returned object.
 
 147     function &fromArray($arr)
 
 149         if (isset($arr['xsdversion'])) {
 
 150             $obj = &$this->factory($arr['xsdversion']);
 
 151             if ($this->_logger) {
 
 152                 $obj->setLogger($this->_logger);
 
 155             $obj->setConfig($this->_config);
 
 156             $obj->fromArray($arr);
 
 160         if (isset($arr['package']['attribs']['version'])) {
 
 161             $obj = &$this->factory($arr['package']['attribs']['version']);
 
 163             $obj = &$this->factory('1.0');
 
 166         if ($this->_logger) {
 
 167             $obj->setLogger($this->_logger);
 
 170         $obj->setConfig($this->_config);
 
 171         $obj->fromArray($arr);
 
 176      * Create a PEAR_PackageFile_v* from an XML string.
 
 178      * @param   string $data contents of package.xml file
 
 179      * @param   int $state package state (one of PEAR_VALIDATE_* constants)
 
 180      * @param   string $file full path to the package.xml file (and the files
 
 182      * @param   string $archive optional name of the archive that the XML was
 
 183      *          extracted from, if any
 
 184      * @return  PEAR_PackageFile_v1|PEAR_PackageFile_v2
 
 185      * @uses    parserFactory() to construct a parser to load the package.
 
 187     function &fromXmlString($data, $state, $file, $archive = false)
 
 189         if (preg_match('/<package[^>]+version=[\'"]([0-9]+\.[0-9]+)[\'"]/', $data, $packageversion)) {
 
 190             if (!in_array($packageversion[1], array('1.0', '2.0', '2.1'))) {
 
 191                 return PEAR::raiseError('package.xml version "' . $packageversion[1] .
 
 192                     '" is not supported, only 1.0, 2.0, and 2.1 are supported.');
 
 195             $object = &$this->parserFactory($packageversion[1]);
 
 196             if ($this->_logger) {
 
 197                 $object->setLogger($this->_logger);
 
 200             $object->setConfig($this->_config);
 
 201             $pf = $object->parse($data, $file, $archive);
 
 202             if (PEAR::isError($pf)) {
 
 206             if ($this->_rawReturn) {
 
 210             if (!$pf->validate($state)) {;
 
 211                 if ($this->_config->get('verbose') > 0
 
 212                     && $this->_logger && $pf->getValidationWarnings(false)
 
 214                     foreach ($pf->getValidationWarnings(false) as $warning) {
 
 215                         $this->_logger->log(0, 'ERROR: ' . $warning['message']);
 
 219                 $a = PEAR::raiseError('Parsing of package.xml from file "' . $file . '" failed',
 
 220                     2, null, null, $pf->getValidationWarnings());
 
 224             if ($this->_logger && $pf->getValidationWarnings(false)) {
 
 225                 foreach ($pf->getValidationWarnings() as $warning) {
 
 226                     $this->_logger->log(0, 'WARNING: ' . $warning['message']);
 
 230             if (method_exists($pf, 'flattenFilelist')) {
 
 231                 $pf->flattenFilelist(); // for v2
 
 235         } elseif (preg_match('/<package[^>]+version=[\'"]([^"\']+)[\'"]/', $data, $packageversion)) {
 
 236             $a = PEAR::raiseError('package.xml file "' . $file .
 
 237                 '" has unsupported package.xml <package> version "' . $packageversion[1] . '"');
 
 240             if (!class_exists('PEAR_ErrorStack')) {
 
 241                 require_once 'PEAR/ErrorStack.php';
 
 244             PEAR_ErrorStack::staticPush('PEAR_PackageFile',
 
 245                 PEAR_PACKAGEFILE_ERROR_NO_PACKAGEVERSION,
 
 246                 'warning', array('xml' => $data), 'package.xml "' . $file .
 
 247                     '" has no package.xml <package> version');
 
 248             $object = &$this->parserFactory('1.0');
 
 249             $object->setConfig($this->_config);
 
 250             $pf = $object->parse($data, $file, $archive);
 
 251             if (PEAR::isError($pf)) {
 
 255             if ($this->_rawReturn) {
 
 259             if (!$pf->validate($state)) {
 
 260                 $a = PEAR::raiseError('Parsing of package.xml from file "' . $file . '" failed',
 
 261                     2, null, null, $pf->getValidationWarnings());
 
 265             if ($this->_logger && $pf->getValidationWarnings(false)) {
 
 266                 foreach ($pf->getValidationWarnings() as $warning) {
 
 267                     $this->_logger->log(0, 'WARNING: ' . $warning['message']);
 
 271             if (method_exists($pf, 'flattenFilelist')) {
 
 272                 $pf->flattenFilelist(); // for v2
 
 280      * Register a temporary file or directory.  When the destructor is
 
 281      * executed, all registered temporary files and directories are
 
 284      * @param string  $file  name of file or directory
 
 287     function addTempFile($file)
 
 289         $GLOBALS['_PEAR_Common_tempfiles'][] = $file;
 
 293      * Create a PEAR_PackageFile_v* from a compresed Tar or Tgz file.
 
 295      * @param string contents of package.xml file
 
 296      * @param int package state (one of PEAR_VALIDATE_* constants)
 
 297      * @return  PEAR_PackageFile_v1|PEAR_PackageFile_v2
 
 298      * @using   Archive_Tar to extract the files
 
 299      * @using   fromPackageFile() to load the package after the package.xml
 
 302     function &fromTgzFile($file, $state)
 
 304         if (!class_exists('Archive_Tar')) {
 
 305             require_once 'Archive/Tar.php';
 
 308         $tar = new Archive_Tar($file);
 
 309         if ($this->_debug <= 1) {
 
 310             $tar->pushErrorHandling(PEAR_ERROR_RETURN);
 
 313         $content = $tar->listContent();
 
 314         if ($this->_debug <= 1) {
 
 315             $tar->popErrorHandling();
 
 318         if (!is_array($content)) {
 
 319             if (is_string($file) && strlen($file < 255) &&
 
 320                   (!file_exists($file) || !@is_file($file))) {
 
 321                 $ret = PEAR::raiseError("could not open file \"$file\"");
 
 325             $file = realpath($file);
 
 326             $ret = PEAR::raiseError("Could not get contents of package \"$file\"".
 
 327                                      '. Invalid tgz file.');
 
 331         if (!count($content) && !@is_file($file)) {
 
 332             $ret = PEAR::raiseError("could not open file \"$file\"");
 
 338         foreach ($content as $file) {
 
 339             $name = $file['filename'];
 
 340             if ($name == 'package2.xml') { // allow a .tgz to distribute both versions
 
 345             if ($name == 'package.xml') {
 
 348             } elseif (preg_match('/package.xml$/', $name, $match)) {
 
 354         $tmpdir = System::mktemp('-t "' . $this->_config->get('temp_dir') . '" -d pear');
 
 355         if ($tmpdir === false) {
 
 356             $ret = PEAR::raiseError("there was a problem with getting the configured temp directory");
 
 360         PEAR_PackageFile::addTempFile($tmpdir);
 
 362         $this->_extractErrors();
 
 363         PEAR::staticPushErrorHandling(PEAR_ERROR_CALLBACK, array($this, '_extractErrors'));
 
 365         if (!$xml || !$tar->extractList(array($xml), $tmpdir)) {
 
 366             $extra = implode("\n", $this->_extractErrors());
 
 368                 $extra = ' ' . $extra;
 
 371             PEAR::staticPopErrorHandling();
 
 372             $ret = PEAR::raiseError('could not extract the package.xml file from "' .
 
 373                 $origfile . '"' . $extra);
 
 377         PEAR::staticPopErrorHandling();
 
 378         $ret = &PEAR_PackageFile::fromPackageFile("$tmpdir/$xml", $state, $origfile);
 
 383      * helper callback for extracting Archive_Tar errors
 
 385      * @param PEAR_Error|null $err
 
 389     function _extractErrors($err = null)
 
 391         static $errors = array();
 
 397         $errors[] = $err->getMessage();
 
 401      * Create a PEAR_PackageFile_v* from a package.xml file.
 
 404      * @param   string  $descfile  name of package xml file
 
 405      * @param   int     $state package state (one of PEAR_VALIDATE_* constants)
 
 406      * @param   string|false $archive name of the archive this package.xml came
 
 408      * @return  PEAR_PackageFile_v1|PEAR_PackageFile_v2
 
 409      * @uses    PEAR_PackageFile::fromXmlString to create the oject after the
 
 410      *          XML is loaded from the package.xml file.
 
 412     function &fromPackageFile($descfile, $state, $archive = false)
 
 415         if (is_string($descfile) && strlen($descfile) < 255 &&
 
 417               !file_exists($descfile) || !is_file($descfile) || !is_readable($descfile)
 
 418               || (!$fp = @fopen($descfile, 'r'))
 
 421             $a = PEAR::raiseError("Unable to open $descfile");
 
 425         // read the whole thing so we only get one cdata callback
 
 426         // for each block of cdata
 
 428         $data = file_get_contents($descfile);
 
 429         $ret = &PEAR_PackageFile::fromXmlString($data, $state, $descfile, $archive);
 
 434      * Create a PEAR_PackageFile_v* from a .tgz archive or package.xml file.
 
 436      * This method is able to extract information about a package from a .tgz
 
 437      * archive or from a XML package definition file.
 
 440      * @param   string  $info file name
 
 441      * @param   int     $state package state (one of PEAR_VALIDATE_* constants)
 
 442      * @return  PEAR_PackageFile_v1|PEAR_PackageFile_v2
 
 443      * @uses    fromPackageFile() if the file appears to be XML
 
 444      * @uses    fromTgzFile() to load all non-XML files
 
 446     function &fromAnyFile($info, $state)
 
 449             $dir_name = realpath($info);
 
 450             if (file_exists($dir_name . '/package.xml')) {
 
 451                 $info = PEAR_PackageFile::fromPackageFile($dir_name .  '/package.xml', $state);
 
 452             } elseif (file_exists($dir_name .  '/package2.xml')) {
 
 453                 $info = PEAR_PackageFile::fromPackageFile($dir_name .  '/package2.xml', $state);
 
 455                 $info = PEAR::raiseError("No package definition found in '$info' directory");
 
 462         if (is_string($info) && strlen($info) < 255 &&
 
 463              (file_exists($info) || ($fp = @fopen($info, 'r')))
 
 470             $tmp = substr($info, -4);
 
 471             if ($tmp == '.xml') {
 
 472                 $info = &PEAR_PackageFile::fromPackageFile($info, $state);
 
 473             } elseif ($tmp == '.tar' || $tmp == '.tgz') {
 
 474                 $info = &PEAR_PackageFile::fromTgzFile($info, $state);
 
 476                 $fp   = fopen($info, 'r');
 
 477                 $test = fread($fp, 5);
 
 479                 if ($test == '<?xml') {
 
 480                     $info = &PEAR_PackageFile::fromPackageFile($info, $state);
 
 482                     $info = &PEAR_PackageFile::fromTgzFile($info, $state);
 
 489         $info = PEAR::raiseError("Cannot open '$info' for parsing");