diff --git a/jilo-web.conf.php b/jilo-web.conf.php index ebe503d..b581160 100644 --- a/jilo-web.conf.php +++ b/jilo-web.conf.php @@ -3,8 +3,12 @@ $config = [ 'domain' => 'localhost', 'folder' => '/jilo-web/', - 'database' => '/home/yasen/work/code/git/lindeas-code/jilo-web/jilo-web.db', - 'jilo_database' => '/home/yasen/work/code/git/lindeas-code/jilo/jilo.db', + 'jilo_database' => '../../jilo/jilo.db', + + 'db_type' => 'sqlite', + + 'sqlite_file' => '../jilo-web.db', + 'registration_enabled' => true, 'login_message' => '', 'version' => '0.1.1', diff --git a/public_html/classes/database.php b/public_html/classes/database.php index 57a0c57..b5b3519 100644 --- a/public_html/classes/database.php +++ b/public_html/classes/database.php @@ -3,25 +3,68 @@ class Database { private $pdo; - public function __construct($dbFile) { + public function __construct($options) { + // pdo needed + if ( !extension_loaded('pdo') ) { + throw new Exception('PDO extension not loaded.'); + } - // pdo and pdo_sqlite needed - if ( !extension_loaded('pdo_sqlite') ) { + // options check + if (empty($options['type'])) { + throw new Exception('Database type is not set.'); + } + + // database type + switch ($options['type']) { + case 'sqlite': + $this->connectSqlite($options); + break; + case 'mysql' || 'mariadb': + $this->connectMysql($options); + break; + default: + throw newException("Database type \"{$options['type']}\" is not supported."); + } + } + + private function connectSqlite($options) { + // pdo_sqlite extension is needed + if (!extension_loaded('pdo_sqlite')) { throw new Exception('PDO extension for SQLite not loaded.'); } - // database file check - if (empty($dbFile) || !file_exists($dbFile)) { - throw new Exception('Database file is not found.'); + // SQLite options + if (empty($options['dbFile']) || !file_exists($options['dbFile'])) { + throw new Exception('SQLite database file not found.'); } - // connect to database - // FIXME: add mysql/mariadb option + // connect to SQLite try { - $this->pdo = new PDO("sqlite:" . $dbFile); + $this->pdo = new PDO("sqlite:" . $options['dbFile']); $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { - throw new Exception('DB connection failed: ' . $e->getMessage()); + throw new Exception('SQLite connection failed: ' . $e->getMessage()); + } + } + + private function connectMysql($options) { + // pdo_mysql extension is needed + if (!extension_loaded('pdo_mysql')) { + throw new Exception('PDO extension for MySQL not loaded.'); + } + + // MySQL options + if (empty($options['host']) || empty($options['dbname']) || empty($options['user'])) { + throw new Exception('MySQL connection data is missing.'); + } + + // Connect to MySQL + try { + $dsn = "mysql:host={$options['host']};port={$options['port']};dbname={$options['dbname']};charset=utf8"; + $this->pdo = new PDO($dsn, $options['user'], $options['password'] ?? ''); + $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + } catch (PDOException $e) { + throw new Exception('MySQL connection failed: ' . $e->getMessage()); } } diff --git a/public_html/helpers/database.php b/public_html/helpers/database.php new file mode 100644 index 0000000..8122fe2 --- /dev/null +++ b/public_html/helpers/database.php @@ -0,0 +1,43 @@ + $config['db_type'], + 'dbFile' => $config['sqlite_file'], + ]); + $pdo = $db->getConnection(); + } catch (Exception $e) { + $error = 'Error: ' . $e->getMessage(); + include 'templates/block-message.php'; + exit(); + } + // mysql/mariadb database + } elseif ($config['db_type'] === 'mysql' || $config['db_type'] === 'mariadb') { + try { + $db = new Database([ + 'type' => $config['db_type'], + 'host' => $config['sql_host'] ?? 'localhost', + 'port' => $config['sql_port'] ?? '3306', + 'dbname' => $config['sql_database'], + 'user' => $config['sql_username'], + 'password' => $config['sql_password'], + ]); + $pdo = $db->getConnection(); + } catch (Exception $e) { + $error = 'Error: ' . $e->getMessage(); + include 'templates/block-message.php'; + exit(); + } + // unknown database + } else { + $error = "Error: unknow database type \"{$config['db_type']}\""; + include 'templates/block-message.php'; + exit(); + } + return $db; +} +?> diff --git a/public_html/index.php b/public_html/index.php index ba7ee55..070210f 100644 --- a/public_html/index.php +++ b/public_html/index.php @@ -54,6 +54,7 @@ if ($config_file) { $app_root = $config['folder']; +session_name('jilo'); session_start(); if (isset($_GET['page'])) { diff --git a/public_html/pages/components.php b/public_html/pages/components.php index cf4a780..f6a1a13 100644 --- a/public_html/pages/components.php +++ b/public_html/pages/components.php @@ -3,6 +3,10 @@ require_once 'classes/database.php'; require 'classes/component.php'; +// connect to database +require 'helpers/database.php'; +$db = connectDB($config); + // FIXME move thi sto a special function $time_range_specified = false; if (!isset($_REQUEST['from_time']) || (isset($_REQUEST['from_time']) && $_REQUEST['from_time'] == '')) { @@ -32,15 +36,6 @@ if (isset($_REQUEST['name']) && $_REQUEST['name'] != '') { $component_id = 'component_id'; } -// connect to database -try { - $db = new Database($config['jilo_database']); -} catch (Exception $e) { - $error = 'Error: ' . $e->getMessage(); - include 'templates/block-message.php'; - exit(); -} - // // Component events listings diff --git a/public_html/pages/conferences.php b/public_html/pages/conferences.php index c1f0fd1..83e7c56 100644 --- a/public_html/pages/conferences.php +++ b/public_html/pages/conferences.php @@ -3,6 +3,10 @@ require_once 'classes/database.php'; require 'classes/conference.php'; +// connect to database +require 'helpers/database.php'; +$db = connectDB($config); + // FIXME move thi sto a special function $time_range_specified = false; if (!isset($_REQUEST['from_time']) || (isset($_REQUEST['from_time']) && $_REQUEST['from_time'] == '')) { @@ -33,15 +37,6 @@ if (isset($_REQUEST['id']) && $_REQUEST['id'] != '') { unset($conferenceName); } -// connect to database -try { - $db = new Database($config['jilo_database']); -} catch (Exception $e) { - $error = 'Error: ' . $e->getMessage(); - include 'templates/block-message.php'; - exit(); -} - // // Conference listings diff --git a/public_html/pages/front.php b/public_html/pages/front.php index e50636b..d76b3bc 100644 --- a/public_html/pages/front.php +++ b/public_html/pages/front.php @@ -5,13 +5,9 @@ require 'classes/conference.php'; require 'classes/participant.php'; // connect to database -try { - $db = new Database($config['jilo_database']); -} catch (Exception $e) { - $error = 'Error: ' . $e->getMessage(); - include 'templates/block-message.php'; - exit(); -} +require 'helpers/database.php'; +$db = connectDB($config); + // // dashboard widget listings diff --git a/public_html/pages/login.php b/public_html/pages/login.php index 3ab3ee5..0188f64 100644 --- a/public_html/pages/login.php +++ b/public_html/pages/login.php @@ -7,7 +7,11 @@ require 'classes/user.php'; unset($error); try { - $db = new Database($config['database']); + + // connect to database + require 'helpers/database.php'; + $db = connectDB($config); + $user = new User($db); if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) { diff --git a/public_html/pages/participants.php b/public_html/pages/participants.php index f7b3b70..255b46d 100644 --- a/public_html/pages/participants.php +++ b/public_html/pages/participants.php @@ -3,6 +3,10 @@ require_once 'classes/database.php'; require 'classes/participant.php'; +// connect to database +require 'helpers/database.php'; +$db = connectDB($config); + // FIXME move thi sto a special function $time_range_specified = false; if (!isset($_REQUEST['from_time']) || (isset($_REQUEST['from_time']) && $_REQUEST['from_time'] == '')) { @@ -37,15 +41,6 @@ if (isset($_REQUEST['id']) && $_REQUEST['id'] != '') { unset($participantName); } -// connect to database -try { - $db = new Database($config['jilo_database']); -} catch (Exception $e) { - $error = 'Error: ' . $e->getMessage(); - include 'templates/block-message.php'; - exit(); -} - // // Participant listings diff --git a/public_html/pages/register.php b/public_html/pages/register.php index b356805..1b7a560 100644 --- a/public_html/pages/register.php +++ b/public_html/pages/register.php @@ -8,7 +8,11 @@ if ($config['registration_enabled'] === true) { unset($error); try { - $db = new Database($config['database']); + + // connect to database + require 'helpers/database.php'; + $db = connectDB($config); + $user = new User($db); if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {