irm/lib/MDB.php
/**
     * Parse a data source name
     *
     * A array with the following keys will be returned:
     *  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: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
     *
     * @param   string  $dsn Data Source Name to be parsed
     * @return  array   an associative array
     * @access public
     * @author Tomas V.V.Cox <cox@idecnet.com>
     */
Parse a data source name

A array with the following keys will be returned:
phptype: Database backend used in PHP (mysql, odbc etc.)
dbsyntax: Database used with regards to SQL syntax etc.

The format of the supplied DSN is in its fullest form.

password@protocol+hostspec/database

Most variations are allowed.



@param   string  $dsn Data Source Name to be parsed
@return  array   an associative array
@access public
@author Tomas V.V.Cox 
Parse a data source name A array with the following keys will be returned : phptype : Database backend used in PHP ( mysql odbc etc . ) dbsyntax : Database used with regards to SQL syntax etc . The format of the supplied DSN is in its fullest form . password@protocol + hostspec / database Most variations are allowed . @param string $dsn Data Source Name to be parsed @return array an associative array @access public @author Tomas V . V . Cox
function parseDSN($dsn)
    {
        if (is_array($dsn)) {
            return($dsn);
        }
        $parsed = array(
            'phptype'  => FALSE,
            'dbsyntax' => FALSE,
            'username' => FALSE,
            'password' => FALSE,
            'protocol' => FALSE,
            'hostspec' => FALSE,
            'port'     => FALSE,
            'socket'   => FALSE,
            'database' => FALSE
        );
        if (($pos = strpos($dsn, '://')) !== FALSE) {
            $str = substr($dsn, 0, $pos);
            $dsn = substr($dsn, $pos + 3);
        } else {
            $str = $dsn;
            $dsn = NULL;
        }
        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 (($at = strrpos($dsn,'@')) !== FALSE) {
            $str = substr($dsn, 0, $at);
            $dsn = substr($dsn, $at + 1);
            if (($pos = strpos($str, ':')) !== FALSE) {
                $parsed['username'] = rawurldecode(substr($str, 0, $pos));
                $parsed['password'] = rawurldecode(substr($str, $pos + 1));
            } else {
                $parsed['username'] = rawurldecode($str);
            }
        }
        if (preg_match('|^([^(]+)\((.*?)\)/?(.*?)$|', $dsn, $match)) {
            $proto       = $match[1];
            $proto_opts  = (!empty($match[2])) ? $match[2] : FALSE;
            $dsn         = $match[3];
        } else {
            if (strpos($dsn, '+') !== FALSE) {
                list($proto, $dsn) = explode('+', $dsn, 2);
            }
            if (strpos($dsn, '/') !== FALSE) {
                list($proto_opts, $dsn) = explode('/', $dsn, 2);
            } else {
                $proto_opts = $dsn;
                $dsn = NULL;
            }
        }
        $parsed['protocol'] = (!empty($proto)) ? $proto : 'tcp';
        $proto_opts = rawurldecode($proto_opts);
        if ($parsed['protocol'] == 'tcp') {
            if (strpos($proto_opts, ':') !== FALSE) {
                list($parsed['hostspec'], $parsed['port']) =
                                                     explode(':', $proto_opts);
            } else {
                $parsed['hostspec'] = $proto_opts;
            }
        } elseif ($parsed['protocol'] == 'unix') {
            $parsed['socket'] = $proto_opts;
        }
        if (!empty($dsn)) {
            if (($pos = strpos($dsn, '?')) === FALSE) {
                $parsed['database'] = $dsn;
            ?param1=value1&param2=value2
            } else {
                $parsed['database'] = substr($dsn, 0, $pos);
                $dsn = substr($dsn, $pos + 1);
                if (strpos($dsn, '&') !== FALSE) {
                    $opts = explode('&', $dsn);
                } else { 
                    $opts = array($dsn);
                }
                foreach ($opts as $opt) {
                    list($key, $value) = explode('=', $opt);
                    if (!isset($parsed[$key])) { 
                        $parsed[$key] = rawurldecode($value);
                    }
                }
            }
        }
        return($parsed);
    }
function parseDSN ( $ dsn ) { if ( is_array ( $ dsn ) ) { return ( $ dsn ) ; } $ parsed = array ( 'phptype' => FALSE , 'dbsyntax' => FALSE , 'username' => FALSE , 'password' => FALSE , 'protocol' => FALSE , 'hostspec' => FALSE , 'port' => FALSE , 'socket' => FALSE , 'database' => FALSE ) ; if ( ( $ pos = strpos ( $ dsn , '://' ) ) !== FALSE ) { $ str = substr ( $ dsn , 0 , $ pos ) ; $ dsn = substr ( $ dsn , $ pos + 3 ) ; } else { $ str = $ dsn ; $ dsn = NULL ; } 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 ( ( $ at = strrpos ( $ dsn , '@' ) ) !== FALSE ) { $ str = substr ( $ dsn , 0 , $ at ) ; $ dsn = substr ( $ dsn , $ at + 1 ) ; if ( ( $ pos = strpos ( $ str , ':' ) ) !== FALSE ) { $ parsed [ 'username' ] = rawurldecode ( substr ( $ str , 0 , $ pos ) ) ; $ parsed [ 'password' ] = rawurldecode ( substr ( $ str , $ pos + 1 ) ) ; } else { $ parsed [ 'username' ] = rawurldecode ( $ str ) ; } } if ( preg_match ( '|^([^(]+)\((.*?)\)/?(.*?)$|' , $ dsn , $ match ) ) { $ proto = $ match [ 1 ] ; $ proto_opts = ( ! empty ( $ match [ 2 ] ) ) ? $ match [ 2 ] : FALSE ; $ dsn = $ match [ 3 ] ; } else { if ( strpos ( $ dsn , '+' ) !== FALSE ) { list ( $ proto , $ dsn ) = explode ( '+' , $ dsn , 2 ) ; } if ( strpos ( $ dsn , '/' ) !== FALSE ) { list ( $ proto_opts , $ dsn ) = explode ( '/' , $ dsn , 2 ) ; } else { $ proto_opts = $ dsn ; $ dsn = NULL ; } } $ parsed [ 'protocol' ] = ( ! empty ( $ proto ) ) ? $ proto : 'tcp' ; $ proto_opts = rawurldecode ( $ proto_opts ) ; if ( $ parsed [ 'protocol' ] == 'tcp' ) { if ( strpos ( $ proto_opts , ':' ) !== FALSE ) { list ( $ parsed [ 'hostspec' ] , $ parsed [ 'port' ] ) = explode ( ':' , $ proto_opts ) ; } else { $ parsed [ 'hostspec' ] = $ proto_opts ; } } elseif ( $ parsed [ 'protocol' ] == 'unix' ) { $ parsed [ 'socket' ] = $ proto_opts ; } if ( ! empty ( $ dsn ) ) { if ( ( $ pos = strpos ( $ dsn , '?' ) ) === FALSE ) { $ parsed [ 'database' ] = $ dsn ; } else { $ parsed [ 'database' ] = substr ( $ dsn , 0 , $ pos ) ; $ dsn = substr ( $ dsn , $ pos + 1 ) ; if ( strpos ( $ dsn , '&' ) !== FALSE ) { $ opts = explode ( '&' , $ dsn ) ; } else { $ opts = array ( $ dsn ) ; } foreach ( $ opts as $ opt ) { list ( $ key , $ value ) = explode ( '=' , $ opt ) ; if ( ! isset ( $ parsed [ $ key ] ) ) { $ parsed [ $ key ] = rawurldecode ( $ value ) ; } } } } return ( $ parsed ) ; }
Parse a data source name
Parse a data source name
// Find phptype and dbsyntax // Get phptype and dbsyntax // $str => phptype(dbsyntax) // Get (if found): username and password // $dsn => username:password@protocol+hostspec/database // Find protocol and hostspec // $dsn => proto(proto_opts)/database // $dsn => protocol+hostspec/database (old format) // process the different protocol options // Get dabase if any // $dsn => database // /database // /database?param1=value1&param2=value2 // database?param1=value1 // don't allow params overwrite