Browse Source

Add optional database connection URI parameters (#2552)

#fix https://github.com/FreshRSS/FreshRSS/issues/2549
Alexandre Alapetite 6 years ago
parent
commit
77afd1393e
2 changed files with 19 additions and 13 deletions
  1. 15 10
      config.default.php
  2. 4 3
      lib/Minz/ModelPdo.php

+ 15 - 10
config.default.php

@@ -123,33 +123,38 @@ return array(
 		//CURLOPT_PROXYUSERPWD => 'user:password',
 	),
 
-	'db' => array(
+	'db' => [
 
-		# Type of database: `sqlite` or `mysql`.
+		# Type of database: `sqlite` or `mysql` or 'pgsql'
 		'type' => 'sqlite',
 
-		# MySQL host.
+		# Database server
 		'host' => 'localhost',
 
-		# MySQL user.
+		# Database user
 		'user' => '',
 
-		# MySQL password.
+		# Database password
 		'password' => '',
 
-		# MySQL database.
+		# Database name
 		'base' => '',
 
-		# MySQL table prefix.
+		# Tables prefix (useful if you use the same database for multiple things)
 		'prefix' => 'freshrss_',
 
-		'pdo_options' => array(
+		# Additional connection string parameters, such as PostgreSQL 'sslmode=??;sslrootcert=??'
+		# https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS
+		'connection_uri_params' => '',
+
+		# Additional PDO parameters, such as offered by MySQL https://php.net/ref.pdo-mysql
+		'pdo_options' => [
 			//PDO::MYSQL_ATTR_SSL_KEY	=> '/path/to/client-key.pem',
 			//PDO::MYSQL_ATTR_SSL_CERT	=> '/path/to/client-cert.pem',
 			//PDO::MYSQL_ATTR_SSL_CA	=> '/path/to/ca-cert.pem',
-		),
+		],
 
-	),
+	],
 
 	# Configure the default feeds to which users will automatically be subscribed.
 	'default_feeds' => array(

+ 4 - 3
lib/Minz/ModelPdo.php

@@ -47,6 +47,7 @@ class Minz_ModelPdo {
 		$driver_options = isset($db['pdo_options']) && is_array($db['pdo_options']) ? $db['pdo_options'] : [];
 		$dbServer = parse_url('db://' . $db['host']);
 		$dsn = '';
+		$dsnParams = empty($db['connection_uri_params']) ? '' : (';' . $db['connection_uri_params']);
 
 		try {
 			switch ($db['type']) {
@@ -59,12 +60,12 @@ class Minz_ModelPdo {
 						$dsn .= ';port=' . $dbServer['port'];
 					}
 					$driver_options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES utf8mb4';
-					$this->pdo = new MinzPDOMySql($dsn, $db['user'], $db['password'], $driver_options);
+					$this->pdo = new MinzPDOMySql($dsn . $dsnParams, $db['user'], $db['password'], $driver_options);
 					$this->pdo->setPrefix($db['prefix'] . $currentUser . '_');
 					break;
 				case 'sqlite':
 					$dsn = 'sqlite:' . join_path(DATA_PATH, 'users', $currentUser, 'db.sqlite');
-					$this->pdo = new MinzPDOSQLite($dsn, $db['user'], $db['password'], $driver_options);
+					$this->pdo = new MinzPDOSQLite($dsn . $dsnParams, $db['user'], $db['password'], $driver_options);
 					$this->pdo->setPrefix('');
 					break;
 				case 'pgsql':
@@ -75,7 +76,7 @@ class Minz_ModelPdo {
 					if (!empty($dbServer['port'])) {
 						$dsn .= ';port=' . $dbServer['port'];
 					}
-					$this->pdo = new MinzPDOPGSQL($dsn, $db['user'], $db['password'], $driver_options);
+					$this->pdo = new MinzPDOPGSQL($dsn . $dsnParams, $db['user'], $db['password'], $driver_options);
 					$this->pdo->setPrefix($db['prefix'] . $currentUser . '_');
 					break;
 				default: