diff --git a/database.php b/database.php index 91d4f63..d33d6cf 100644 --- a/database.php +++ b/database.php @@ -35,10 +35,27 @@ class mxDatabase /** * Creates mxDatabase object - * @param db_input path to the sqlite file where the credentials should be stored + * @param config object which has following members: + * databaseURI: path to the sqlite file where the credentials should be stored * or a param which can be used to connect to a database with PDO + * databaseUser and databasePass when authentication is required + * register_email which email does the register bot have (here used for providing lookup) */ - function __construct($db_input, $user='', $password='') { + function __construct($config) { + if (empty($config)) { + throw new Exception("config is empty"); + } + if (!isset($config["databaseURI"])) { + throw new Exception("'databaseURI' not defined"); + } + $db_input = $config["databaseURI"]; + $user = ''; + $password = ''; + if (isset($config["databaseUser"]) && isset($config["databasePass"])) { + // only use it when both are defined + $user = $config["databaseUser"]; + $password = $config["databasePass"]; + } // create database file when not existent yet $this->db = new PDO($db_input, $user, $password); $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); @@ -53,7 +70,8 @@ class mxDatabase email TEXT, verify_token TEXT, admin_token TEXT, - request_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP)"); + request_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP + )"); $this->db->exec("CREATE TABLE IF NOT EXISTS logins ( id SERIAL PRIMARY KEY, active INT DEFAULT 1, @@ -66,7 +84,6 @@ class mxDatabase last_modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP )"); // make sure the bot is allowed to login - require_once("config.php"); if (!$this->userRegistered("register_bot")) { $password = $this->addUser("Register", "Bot", "register_bot", $config["register_email"]); $config["register_password"] = $password; @@ -317,10 +334,6 @@ class mxDatabase } if (!isset($mx_db)) { - if (isset($config["databaseUser"]) && isset($config["databasePass"])) { - $mx_db = new mxDatabase($config["databaseURI"], $config["databaseUser"], $config["databasePass"]); - } else { - $mx_db = new mxDatabase($config["databaseURI"]); - } + $mx_db = new mxDatabase($config); } ?>