HEX
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.30
System: Linux iZj6c1151k3ad370bosnmsZ 3.10.0-1160.76.1.el7.x86_64 #1 SMP Wed Aug 10 16:21:17 UTC 2022 x86_64
User: root (0)
PHP: 7.4.30
Disabled: NONE
Upload Files
File: /var/www/html/inventory.breadsecret.com/class/Database/DatabaseFactory.php
<?php
namespace Database {

class DatabaseFactory {
	public static $dbList;
	
	public static function createDatabaseByConfig($cfg) {
		foreach ($cfg as $config) {
			$options = $config['options'] ?? [];
			if (array_key_exists('dsn', $config))
				self::createDatabaseByDSN($config['dsn'], $config['username'], $config['password'], $options);
			else {
				self::createDatabaseByParam($config['host'], $config['username'], $config['password'], $config['schema'], 
						$options, $config['driver'] ?? "mysql", $config['port'] ?? 3306, $config['charset'] ?? "utf8");
			}
		}
	}
	
	public static function createDatabaseByParam($host, $username, $password, $schema, $options=[], $driver="mysql", $port=3306, $charset="utf8") {
		$dsn = $driver.':host='.$host.';port='.$port.';dbname='.$schema.';charset='.$charset;
			
		return self::createDatabaseByDSN($dsn, $username, $password, $options);
	}
	
	
	public static function createDatabaseByDSN($dsn, $username, $password, $options = []) {
		self::$dbList = self::$dbList ?? [];
		self::$dbList[] = new \PDO($dsn, $username, $password, $options);
		
		return count(self::$dbList)-1;
	}
	
	public static function getDatabase($index = 0) {
		return self::$dbList[$index];
	}
}

} // end of namespace Database
namespace {
function db($idx = 0) { return Database\DatabaseFactory::getDatabase($idx); }
function dbes($str, $datatype = PDO::PARAM_STR, $idx = 0) { return db($idx)->quote($str, $datatype); }
}