]> granicus.if.org Git - php/commitdiff
better version checks (contributed by Roman)
authorTomas V.V.Cox <cox@php.net>
Mon, 6 Oct 2003 15:15:56 +0000 (15:15 +0000)
committerTomas V.V.Cox <cox@php.net>
Mon, 6 Oct 2003 15:15:56 +0000 (15:15 +0000)
pear/PEAR/Common.php
pear/PEAR/Installer.php

index e4cfa7515f9d08c2bb3d45e8717ab283e9f93494..478ca11c164c6061354a30420e9e1c20a5bd8eee 100644 (file)
@@ -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()
index 19c8b67a327b14a158d14a2be3429584792bd5a4..556368ec9a38384f8aaa5f15d62cbfc7c41faaaa 100644 (file)
@@ -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