From 164b593d236c27bb22226508602b27f14961f302 Mon Sep 17 00:00:00 2001 From: "Tomas V.V.Cox" Date: Mon, 6 Oct 2003 15:15:56 +0000 Subject: [PATCH] better version checks (contributed by Roman) --- pear/PEAR/Common.php | 35 +++++++++++++++++++++++++++++++---- pear/PEAR/Installer.php | 9 +-------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/pear/PEAR/Common.php b/pear/PEAR/Common.php index e4cfa7515f..478ca11c16 100644 --- a/pear/PEAR/Common.php +++ b/pear/PEAR/Common.php @@ -26,10 +26,15 @@ require_once 'PEAR/Config.php'; // {{{ constants and globals -define('PEAR_COMMON_PACKAGE_NAME_PREG', '/^[A-Za-z][a-zA-Z0-9_]+$/'); +define('_PEAR_COMMON_PACKAGE_NAME_PREG', '[A-Za-z][a-zA-Z0-9_]+'); +define('PEAR_COMMON_PACKAGE_NAME_PREG', '/^' . _PEAR_COMMON_PACKAGE_NAME_PREG . '$/'); + +// this should allow: 1, 1.0, 1.0RC1, 1.0dev, 1.0dev123234234234, 1.0a1, 1.0b1, 1.0pl1 +define('_PEAR_COMMON_PACKAGE_VERSION_PREG', '\d+(?:\.\d+)*(?:[a-z]+\d*)?'); +define('PEAR_COMMON_PACKAGE_VERSION_PREG', '/^' . _PEAR_COMMON_PACKAGE_VERSION_PREG . '$/i'); // XXX far from perfect :-) -define('PEAR_COMMON_PACKAGE_DOWNLOAD_PREG', '/^([A-Za-z][a-zA-Z0-9_]+)(-([.0-9a-zA-Z]+))?$/'); +define('PEAR_COMMON_PACKAGE_DOWNLOAD_PREG', '/^(' . _PEAR_COMMON_PACKAGE_NAME_PREG . ')(-([.0-9a-zA-Z]+))?$/'); /** * List of temporary files and directories registered by @@ -1036,8 +1041,10 @@ class PEAR_Common extends PEAR } $errors = array(); $warnings = array(); - if (empty($info['package'])) { + if (!isset($info['package'])) { $errors[] = 'missing package name'; + } elseif (!$this->validPackageName($info['package'])) { + $errors[] = 'invalid package name'; } if (empty($info['summary'])) { $errors[] = 'missing summary'; @@ -1050,8 +1057,10 @@ class PEAR_Common extends PEAR if (empty($info['release_license'])) { $errors[] = 'missing license'; } - if (empty($info['version'])) { + if (!isset($info['version'])) { $errors[] = 'missing version'; + } elseif (!$this->validPackageVersion($info['version'])) { + $errors[] = 'invalid package version'; } if (empty($info['release_state'])) { $errors[] = 'missing release state'; @@ -1555,6 +1564,24 @@ class PEAR_Common extends PEAR } + // }}} + // {{{ validPackageVersion() + + /** + * Test whether a string contains a valid package version. + * + * @param string $ver the package version to test + * + * @return bool + * + * @access public + */ + function validPackageVersion($ver) + { + return (bool)preg_match(PEAR_COMMON_PACKAGE_VERSION_PREG, $ver); + } + + // }}} // {{{ downloadHttp() diff --git a/pear/PEAR/Installer.php b/pear/PEAR/Installer.php index 19c8b67a32..556368ec9a 100644 --- a/pear/PEAR/Installer.php +++ b/pear/PEAR/Installer.php @@ -1062,10 +1062,6 @@ class PEAR_Installer extends PEAR_Common return $this->raiseError("$pkgname already installed"); } } else { - // checks to do only when upgrading packages -/* if (!$this->registry->packageExists($pkgname)) { - return $this->raiseError("$pkgname not installed"); - }*/ if ($this->registry->packageExists($pkgname)) { $v1 = $this->registry->packageInfo($pkgname, 'version'); $v2 = $pkginfo['version']; @@ -1095,12 +1091,9 @@ class PEAR_Installer extends PEAR_Common null, PEAR_ERROR_DIE); } - // don't want strange characters - $pkgname = ereg_replace ('[^a-zA-Z0-9._]', '_', $pkginfo['package']); - $pkgversion = ereg_replace ('[^a-zA-Z0-9._\-]', '_', $pkginfo['version']); $tmp_path = dirname($descfile); if (substr($pkgfile, -4) != '.xml') { - $tmp_path .= DIRECTORY_SEPARATOR . $pkgname . '-' . $pkgversion; + $tmp_path .= DIRECTORY_SEPARATOR . $pkgname . '-' . $pkginfo['version']; } // ==> XXX This part should be removed later on -- 2.50.1