jon Sun Mar 11 15:26:19 2001 EDT
Modified files:
/php4/pear DB.php
Log:
Revised version of the parseDSN function.
Submitted by: "Tomas V.V.Cox" <[EMAIL PROTECTED]>
Index: php4/pear/DB.php
diff -u php4/pear/DB.php:1.49 php4/pear/DB.php:1.50
--- php4/pear/DB.php:1.49 Tue Feb 20 15:00:08 2001
+++ php4/pear/DB.php Sun Mar 11 15:26:18 2001
@@ -1,5 +1,5 @@
<?php
-//
+/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP version 4.0 |
// +----------------------------------------------------------------------+
@@ -14,10 +14,10 @@
// | [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 $
+// $Id: DB.php,v 1.50 2001/03/11 23:26:18 jon Exp $
//
// Database independent query interface.
//
@@ -346,95 +346,106 @@
*
* @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_file.db
+ * phptype://username:password@hostspec/database_name
+ * 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 and dbsyntax
+ if (($pos = strpos($dsn, '://')) !== false) {
+ $str = substr($dsn, 0, $pos);
+ $dsn = substr($dsn, $pos + 3);
+ } else {
+ $str = $dsn;
+ $dsn = NULL;
}
- 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 and dbsyntax
+ // $str => phptype(dbsyntax)
+ if (preg_match('|^([^(]+)\(([^(]*)\)$|', $str, $arr)) {
+ $parsed['phptype'] = $arr[1];
+ $parsed['dbsyntax'] = (empty($arr[2])) ? $arr[1] : $arr[2];
+ } else {
+ $parsed['phptype'] = $str;
+ $parsed['dbsyntax'] = $str;
+ }
+
+ if (empty($dsn)) {
+ return $parsed;
}
- if (preg_match("|^([^\+]+)\+(.*)$|", $dsn, $arr)) {
- $parsed["protocol"] = $arr[1];
- $dsn = $arr[2];
+ // Get (if found): username and 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'] = urldecode(substr($str, 0, $pos));
+ $parsed['password'] = urldecode(substr($str, $pos + 1));
+ } else {
+ $parsed['username'] = urldecode($str);
+ }
}
- if (!$parsed["database"]) {
- $dsn = preg_replace("|/+$|", "", $dsn);
+ // Find protocol and hostspec
+ // $dsn => protocol+hostspec/database
+ if (($pos=strpos($dsn, '/')) !== false) {
+ $str = substr($dsn, 0, $pos);
+ $dsn = substr($dsn, $pos + 1);
+ } else {
+ $str = $dsn;
+ $dsn = NULL;
}
- $parsed["hostspec"] = urldecode($dsn);
+ // Get protocol + hostspec
+ // $str => protocol+hostspec
+ if (($pos=strpos($str, '+')) !== false) {
+ $parsed['protocol'] = substr($str, 0, $pos);
+ $parsed['hostspec'] = urldecode(substr($str, $pos + 1));
+ } else {
+ $parsed['hostspec'] = urldecode($str);
+ }
- if(!$parsed["dbsyntax"]) {
- $parsed["dbsyntax"] = $parsed["phptype"];
+ // Get dabase if any
+ // $dsn => database
+ if (!empty($dsn)) {
+ $parsed['database'] = $dsn;
}
return $parsed;
--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]