Re: [PHP-PEAR] parseDSN ... again

[email protected] (Jon Parise) Sat, 10 Mar 2001 17:41:15 -0500
Newsgroups php.pear,php.pear.dev
Message-ID <[email protected]>
--J2SCkAp4GZ/dPZZf
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

On Sat, Mar 10, 2001 at 05:26:52PM -0500, Jon Parise wrote:

> It looks good to me.  Unless anyone has any other comments, I'll
> commit it to cvs some time tomorrow.

Attached is the patch that I will commit tomorrow (Sunday), if no one
objects.  I reformatted Tomas' original code slightly so that it
conforms to the PEAR coding standards.

If you have a free moment, please apply the attached patch to ensure
that it doesn't break existing code.

-- 
Jon Parise ([email protected])  .  Rochester Inst. of Technology
http://www.csh.rit.edu/~jon/  :  Computer Science House Member

--J2SCkAp4GZ/dPZZf
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="parseDSN.patch"

Index: DB.php
===================================================================
RCS file: /repository/php4/pear/DB.php,v
retrieving revision 1.49
diff -u -r1.49 DB.php
--- DB.php	2001/02/20 23:00:08	1.49
+++ DB.php	2001/03/10 22:34:48
@@ -1,5 +1,5 @@
 <?php
-//
+/* vim: set expandtab tabstop=4 shiftwidth=4: */
 // +----------------------------------------------------------------------+
 // | PHP version 4.0                                                      |
 // +----------------------------------------------------------------------+
@@ -14,7 +14,7 @@
 // | [email protected] so we can mail you a copy immediately.               |
 // +----------------------------------------------------------------------+
 // | Authors: Stig Bakken <[email protected]>                                   |
-// |                                                                      |
+// |          Tomas V.V.Cox <[email protected]>                             |
 // +----------------------------------------------------------------------+
 //
 // $Id: DB.php,v 1.49 2001/02/20 23:00:08 ssb Exp $
@@ -346,95 +346,104 @@
      *
      * @return array an associative array with the following keys:
      *
-     * phptype: Database backend used in PHP (mysql, odbc etc.)
-     * dbsyntax: Database used with regards to SQL syntax etc.
-     * protocol: Communication protocol to use (tcp, unix etc.)
-     * hostspec: Host specification (hostname[:port])
-     * database: Database to use on the DBMS server
-     * username: User name for login
-     * password: Password for login
+     *  phptype: Database backend used in PHP (mysql, odbc etc.)
+     *  dbsyntax: Database used with regards to SQL syntax etc.
+     *  protocol: Communication protocol to use (tcp, unix etc.)
+     *  hostspec: Host specification (hostname[:port])
+     *  database: Database to use on the DBMS server
+     *  username: User name for login
+     *  password: Password for login
      *
      * The format of the supplied DSN is in its fullest form:
      *
      *  phptype(dbsyntax)://username:password@protocol+hostspec/database
      *
      * Most variations are allowed:
-     *  phptype://username:password@protocol+hostspec/database</li>
-     *  phptype://username:password@hostspec/database</li>
-     *  phptype://username:password@hostspec</li>
-     *  phptype://hostspec/database</li>
-     *  phptype://hostspec</li>
-     *  phptype(dbsyntax)</li>
-     *  phptype</li>
+     *
+     *  phptype://username:password@protocol+hostspec:110//usr/db/user.db
+     *  phptype://username:password@hostspec/database
+     *  phptype://username:password@hostspec
+     *  phptype://username@hostspec
+     *  phptype://hostspec/database
+     *  phptype://hostspec
+     *  phptype(dbsyntax)
+     *  phptype
      *
-     * @return bool FALSE is returned on error
+     * @author Tomas V.V.Cox <[email protected]>
      */
     function parseDSN($dsn)
     {
-	if (is_array($dsn)) {
-	    return $dsn;
-	}
-    
-	$parsed = array(
-	    "phptype"  => false,
-	    "dbsyntax" => false,
-	    "protocol" => false,
-	    "hostspec" => false,
-	    "database" => false,
-	    "username" => false,
-	    "password" => false
-	);
-
-	if (preg_match("|^([^:]+)://|", $dsn, $arr)) {
-	    $dbtype = $arr[ 1 ];
-	    $dsn = preg_replace( "|^[^:]+://|", '', $dsn);
-	    
-	    // match "phptype(dbsyntax)"
-	    if (preg_match("|^([^\(]+)\((.+)\)$|", $dbtype, $arr)) {
-		$parsed["phptype"] = $arr[1];
-		$parsed["dbsyntax"] = $arr[2];
-	    } else {
-		$parsed["phptype"] = $dbtype;
-	    }
-        } else {
-	    // match "phptype(dbsyntax)"
-	    if (preg_match("|^([^\(]+)\((.+)\)$|", $dsn, $arr)) {
-		$parsed["phptype"] = $arr[1];
-		$parsed["dbsyntax"] = $arr[2];
-	    } else {
-		$parsed["phptype"] = $dsn;
-	    }
-
-	    return $parsed;
+        if (is_array($dsn)) {
+            return $dsn;
         }
 
-        if (preg_match("|^(.*)/([^/]+)/?$|", $dsn, $arr)) {
-	    $parsed["database"] = $arr[2];
-	    $dsn = $arr[1];
+        $parsed = array(
+            "phptype"  => false,
+            "dbsyntax" => false,
+            "protocol" => false,
+            "hostspec" => false,
+            "database" => false,
+            "username" => false,
+            "password" => false
+        );
+
+        // Find phptype & dbsyntax
+        if (($pos = strpos($dsn,"://")) !== false){
+            $str = substr($dsn, 0, $pos);
+            $dsn = substr($dsn, $pos + 3);
+        } else {
+            $str = $dsn;
+            $dsn = '';
         }
 
-        if (preg_match("|^([^:]+):([^@]*)@?(.*)$|", $dsn, $arr)) {
-	    $parsed["username"] = urldecode($arr[1]);
-	    $parsed["password"] = urldecode($arr[2]);
-	    $dsn = $arr[3];
-        } elseif (preg_match("|^([^:]+)@(.*)$|", $dsn, $arr)) {
-	    $parsed["username"] = urldecode($arr[1]);
-	    $dsn = $arr[2];
+        // Get phptype & dbsyntax
+        // $str => phptype(dbsyntax)
+        if (preg_match("|^([^(]+)\(([^(]*)\)$|", $str, $arr)) {
+            $parsed['phptype'] = $arr[1];
+            $parsed['dbsyntax'] = $arr[2];
+        } else {
+            $parsed['phptype'] = $str;
+        }
+        if (!strlen($dsn)) {
+            return $parsed;
         }
 
-        if (preg_match("|^([^\+]+)\+(.*)$|", $dsn, $arr)) {
-	    $parsed["protocol"] = $arr[1];
-	    $dsn = $arr[2];
+        // Get (if found): username & password
+        // $dsn => username:password@protocol+hostspec/database
+        if (($at = strpos($dsn,'@')) !== false) {
+            $str = substr($dsn, 0, $at);
+            $dsn = substr($dsn, $at + 1);
+            if (($pos = strpos($str,':')) !== false) {
+                $parsed['username'] = substr($str, 0, $pos);
+                $parsed['password'] = substr($str, $pos + 1);
+            } else {
+                $parsed['username'] = $str;
+            }
         }
 
-        if (!$parsed["database"]) {
-	    $dsn = preg_replace("|/+$|", "", $dsn);
+        // Find protocol + hostspec
+        // $dsn => protocol+hostspec/database
+        if (($pos=strpos($dsn,"/")) !== false) {
+            $str = substr($dsn, 0, $pos);
+            $dsn = substr($dsn, $pos + 1);
+        } else {
+            $str = $dsn;
+            $dsn = '';
         }
 
-        $parsed["hostspec"] = urldecode($dsn);
+        // Get protocol + hostspec
+        // $str => protocol+hostspec
+        if (($pos=strpos($str, '+')) !== false) {
+            $parsed['protocol'] = substr($str, 0, $pos);
+            $parsed['hostspec'] = substr($str,$pos+1);
+        } else {
+            $parsed['hostspec'] = $str;
+        }
 
-        if(!$parsed["dbsyntax"]) {
-	    $parsed["dbsyntax"] = $parsed["phptype"];
+        // Get dabase if any
+        // $dsn => database
+        if (strlen($dsn)) {
+            $parsed['database'] = $dsn;
         }
 
         return $parsed;

--J2SCkAp4GZ/dPZZf--