Extract configuration variables from the DSN.

@param string $dsn
DSN to extract variables from.

@return array
Returns an array with variable names as keys with corresponding values.
338485
private function parseDsn($dsn) {
    $parsed = array();

    $typeEnd = strpos($dsn, ':');
    $dbType = substr($dsn, 0, $typeEnd);
    $parsed['dbtype'] = $dbType;

    $dsn = substr($dsn, $typeEnd+1);

    $parts = explode(';', $dsn);
    foreach($parts as $part) {
      list($key, $val) = explode('=', $part);
      $parsed[$key] = $val;
    }

    return $parsed;
  }
402891
protected function getDsn(array $config)
    {
        extract($config);

        $dsn = "mysql:host={$hostname};dbname={$database}";

        if (isset($config['port'])) {
            $dsn .= ";port={$port}";
        }

        if (isset($config['unix_socket'])) {
            $dsn .= ";unix_socket={$config['unix_socket']}";
        }

        return $dsn;
    }
818926
public function getConnectionConfig($conn = null)
    {
        $conn = $conn ?: $this->getConnectionName();

        return $this->config->extract('queue.' . $conn);
    }
14679
public function userUsernameFromDn($dn) {

    $ldap_entry = @$this->dnExists($dn, 'ldap_entry', array());
    if (!$ldap_entry || !is_array($ldap_entry)) {
      return FALSE;
    }
    else {
      return $this->userUsernameFromLdapEntry($ldap_entry);
    }

  }
837203
protected function conn_create($dsn)
    {
        // Get database specific connection options
        $dsn_options = $this->dsn_options($dsn);

        $function = $this->db_pconn ? 'oci_pconnect' : 'oci_connect';

        if (!function_exists($function)) {
            $this->db_error     = true;
            $this->db_error_msg = 'OCI8 extension not loaded. See http://php.net/manual/en/book.oci8.php';

            rcube::raise_error(array('code' => 500, 'type' => 'db',
                'line' => __LINE__, 'file' => __FILE__,
                'message' => $this->db_error_msg), true, false);

            return;
        }

        // connect
        $dbh = @$function($dsn['username'], $dsn['password'], $dsn_options['database'], $dsn_options['charset']);

        if (!$dbh) {
            $error              = oci_error();
            $this->db_error     = true;
            $this->db_error_msg = $error['message'];

            rcube::raise_error(array('code' => 500, 'type' => 'db',
                'line' => __LINE__, 'file' => __FILE__,
                'message' => $this->db_error_msg), true, false);

            return;
        }

        // configure session
        $this->conn_configure($dsn, $dbh);

        return $dbh;
    }
70748
protected function getOptions($dsn, $username, $password, $driverOptions)
    {
        $dsn = explode(':', $dsn);
        $driver = ucwords(array_shift($dsn));
        $dsn = explode(';', implode(':', $dsn));
        $configuredOptions = [];

        static::checkDriver($driver);

        foreach ($dsn as $kv) {
            $kv = explode('=', $kv);
            if (count($kv)) {
                $configuredOptions[$kv[0]] = $kv[1] ?? '';
            }
        }

        $authorization = [
            'user' => $username,
            'password' => $password,
        ];

        $configuredOptions = $driverOptions + $authorization + $configuredOptions;

        foreach (static::$keyMap as $pdoKey => $swpdoKey) {
            if (isset($configuredOptions[$pdoKey])) {
                $configuredOptions[$swpdoKey] = $configuredOptions[$pdoKey];
                unset($configuredOptions[$pdoKey]);
            }
        }

        return array_merge($configuredOptions, static::$options);
    }
241905
public static function Connect ($dsn) {
		try {
			$dsn = static::AdjustDsn($dsn);

			static::ValidateDsn($dsn);

			return new static (
				static::MakeDsn($dsn),
				$dsn['username'] ?? null,
				$dsn['password'] ?? null,
				static::MakeOptionList($dsn['options'] ?? [])
			);
		} catch (\PDOException $pdo_e) {
 			throw new \Exception(sprintf(
 				'%s dsn=%s user=%s password=%s options=%s',
 				$pdo_e->getMessage(),
 				static::MakeDsn($dsn),
 				$dsn['username'] ?? '',
 				$dsn['password'] ?? '',
 				implode(', ', static::MakeOptionList($dsn['options'] ?? []))
 			), 0, $pdo_e);
		}
	}
287024
protected function addSslOptions($dsn, array $config)
    {
        foreach (['sslmode', 'sslcert', 'sslkey', 'sslrootcert'] as $option) {
            if (isset($config[$option])) {
                $dsn .= ";{$option}={$config[$option]}";
            }
        }

        return $dsn;
    }
200076
static public function parseDsn($dsnStr)
    {
        if( empty($dsnStr) ){
            return false;
        }
        
        $info = parse_url($dsnStr);
        if(!$info) {
            return false;
        }
        $dsn = array(
            'adapter'    =>  $info['scheme'],
            'host'      =>  isset($info['host']) ? $info['host'] : '',
            'port'  =>  isset($info['port']) ? $info['port'] : '',
            'username'  =>  isset($info['user']) ? $info['user'] : '',
            'password'  =>  isset($info['pass']) ? $info['pass'] : '',
            'database'  =>  isset($info['path']) ? substr($info['path'],1) : '',
            'charset'   =>  isset($info['fragment'])?$info['fragment']:'utf8',
        );
        
        if(isset($info['query'])) {
            parse_str($info['query'], $dsn['params']);
        }else{
            $dsn['params']  =   array();
        }
        foreach ($dsn['params'] as $k=>$v) {
            $dsn[$k] = $v;
        }
        return $dsn;
    }
1256711
private function runMigrationTool(array $dsn)
    {
        $runner=new CConsoleCommandRunner();
        $runner->commands=array(
            'migrate' => array(
                'class' => 'application.commands.CiiMigrateCommand',
                'dsn' => $dsn,
                'interactive' => 0,
            ),
            'db'=>array(
                'class'=>'CDbConnection',
                'connectionString' => "mysql:host={$dsn['host']};dbname={$dsn['dbname']}",
                'emulatePrepare' => true,
                'username' => $dsn['username'],
                'password' => $dsn['password'],
                'charset' => 'utf8',
            ),
        );
        
        ob_start();
        $runner->run(array(
            'yiic',
            'migrate'
        ));
        
        return htmlentities(ob_get_clean(), null, Yii::app()->charset);
    }
187316
public function extractParts($hostname)
    {
        $suffix = $this->extractSuffix($hostname);

        if ($suffix === $hostname) {
            return [null, $hostname, null];
        }

        if (null !== $suffix) {
            $hostname = Str::substr($hostname, 0, -Str::length($suffix) - 1);
        }

        $lastDot = Str::strrpos($hostname, '.');

        if (false === $lastDot) {
            return [null, $hostname, $suffix];
        }

        $subDomain = Str::substr($hostname, 0, $lastDot);
        $host = Str::substr($hostname, $lastDot + 1);

        return [
            $subDomain,
            $host,
            $suffix
        ];
    }
1269627
protected function conn_prepare($dsn)
    {
        // Create database file, required by PDO to exist on connection
        if (!empty($dsn['database']) && !file_exists($dsn['database'])) {
            $created = touch($dsn['database']);

            // File mode setting, for compat. with MDB2
            if (!empty($dsn['mode']) && $created) {
                chmod($dsn['database'], octdec($dsn['mode']));
            }
        }
    }
949623
protected Map<String, String> getTags(Dsn dsn) {
        return Util.parseTags(Lookup.lookup(TAGS_OPTION, dsn));
    }
504550
public function read($dn)
	{
		if (!$this->isBound || !$this->isConnected())
		{
			return false;
		}

		$base   = substr($dn, strpos($dn, ',') + 1);
		$cn     = substr($dn, 0, strpos($dn, ','));
		$result = ldap_read($this->resource, $base, $cn);

		if ($result === false)
		{
			return false;
		}

		return ldap_get_entries($this->resource, $result);
	}
1249682
protected function _dsn()
    {
        $dsn = parent::_dsn();
        if (isset($this->_config['charset'])) {
            $dsn .= ';charset=' . $this->_config['charset'];
        }
        return $dsn;
    }
127114
protected function parseAgnosticDsn(array $options)
    {
        if (isset($options['dsn']) && is_string($options['dsn'])) {
            $regex = '#^(?P<adapter>[^\\:]+)\\://(?:(?P<user>[^\\:@]+)(?:\\:(?P<pass>[^@]*))?@)?'
                   . '(?P<host>[^\\:@/]+)(?:\\:(?P<port>[1-9]\\d*))?/(?P<name>[^\?]+)(?:\?(?P<query>.*))?$#';
            if (preg_match($regex, trim($options['dsn']), $parsedOptions)) {
                $additionalOpts = [];
                if (isset($parsedOptions['query'])) {
                    parse_str($parsedOptions['query'], $additionalOpts);
                }
                $validOptions = ['adapter', 'user', 'pass', 'host', 'port', 'name'];
                $parsedOptions = array_filter(array_intersect_key($parsedOptions, array_flip($validOptions)));
                $options = array_merge($additionalOpts, $parsedOptions, $options);
                unset($options['dsn']);
            }
        }

        return $options;
    }
538507
protected function createClient($dsn, array $config, array $options)
    {
        // By default driver options is an empty array.
        $driverOptions = [];

        if (isset($config['driver_options']) && is_array($config['driver_options'])) {
            $driverOptions = $config['driver_options'];
        }

        // Check if the credentials are not already set in the options
        if (!isset($options['username']) && !empty($config['username'])) {
            $options['username'] = $config['username'];
        }
        if (!isset($options['password']) && !empty($config['password'])) {
            $options['password'] = $config['password'];
        }

        return new Client($dsn, $options, $driverOptions);
    }