diff --git a/libraries/joomla/database/driver/postgresql.php b/libraries/joomla/database/driver/postgresql.php index b139d6162d12b..951825905afbb 100644 --- a/libraries/joomla/database/driver/postgresql.php +++ b/libraries/joomla/database/driver/postgresql.php @@ -85,6 +85,7 @@ public function __construct($options) $options['user'] = (isset($options['user'])) ? $options['user'] : ''; $options['password'] = (isset($options['password'])) ? $options['password'] : ''; $options['database'] = (isset($options['database'])) ? $options['database'] : ''; + $options['port'] = (isset($options['port'])) ? $options['port'] : null; // Finalize initialization parent::__construct($options); @@ -111,12 +112,47 @@ public function connect() throw new JDatabaseExceptionUnsupported('The pgsql extension for PHP is not installed or enabled.'); } + /* + * pg_connect() takes the port as separate argument. Therefore, we + * have to extract it from the host string (if provided). + */ + + // Check for empty port + if (!$this->options['port']) + { + // Port is empty or not set via options, check for port annotation (:) in the host string + $tmp = substr(strstr($this->options['host'], ':'), 1); + + if (!empty($tmp)) + { + // Get the port number + if (is_numeric($tmp)) + { + $this->options['port'] = $tmp; + } + + // Extract the host name + $this->options['host'] = substr($this->options['host'], 0, strlen($this->options['host']) - (strlen($tmp) + 1)); + + // This will take care of the following notation: ":5432" + if ($this->options['host'] === '') + { + $this->options['host'] = 'localhost'; + } + } + // No port annotation (:) found, setting port to default PostgreSQL port 5432 + else + { + $this->options['port'] = '5432'; + } + } + // Build the DSN for the connection. $dsn = ''; if (!empty($this->options['host'])) { - $dsn .= "host={$this->options['host']} "; + $dsn .= "host={$this->options['host']} port={$this->options['port']} "; } $dsn .= "dbname={$this->options['database']} user={$this->options['user']} password={$this->options['password']}";