$params['db-type'] ]; if (empty($params['flyers-user']) || empty($params['flyers-password'])) return "All fields are required"; switch ($params['db-type']) { case "mysql": if (!empty($params) && count($params) != count($fieldNames)) return "All fields are required"; $config += [ 'host' => $params['db-host'], 'user' => $params['db-username'], 'pass' => $params['db-password'], 'db' => $params['db-database'] ]; $db = MysqlDb::Connect($config->host, $config->user, $config->pass, $config->db); break; case "sqlite": $db = SqliteDb::Connect(); break; default: return "Invalid Database Type"; } $success = $db->exec_multi(" CREATE TABLE IF NOT EXISTS `members` ( `skymanager_id` integer NOT NULL PRIMARY KEY, `name` varchar(128) NOT NULL, `username` varchar(64) NOT NULL, `voting_id` int DEFAULT NULL UNIQUE, `email` varchar(128) DEFAULT NULL, `pollworker` BOOLEAN NOT NULL DEFAULT false, `checkedin` BOOLEAN NOT NULL DEFAULT false); CREATE TABLE IF NOT EXISTS `proxy` ( `voting_id` integer NOT NULL, `delegate_id` integer NOT NULL, PRIMARY KEY (`voting_id`, `delegate_id`)); CREATE TABLE IF NOT EXISTS `positions` ( `position` varchar(64) NOT NULL PRIMARY KEY, `description` varchar(128) NOT NULL UNIQUE, `active` BOOLEAN NOT NULL DEFAULT false ); CREATE TABLE IF NOT EXISTS `votes` ( `candidate_id` integer NOT NULL, `position` varchar(64) NOT NULL, `member_id` integer NOT NULL, -- `vote_type` enum('IN PERSON','ONLINE','PROXY IN PERSON','PROXY ONLINE','UNANIMOUS') NOT NULL DEFAULT 'ONLINE', `vote_type` varchar(24) NOT NULL DEFAULT 'ONLINE', `submitted_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `submitter_id` integer NOT NULL, PRIMARY KEY (`position`,`member_id`), FOREIGN KEY (`position`) REFERENCES `positions` (`position`) ON DELETE CASCADE) "); if (!$success) return "Failed to set up database schema: " . $db->getError(); $success = $user->login($params['flyers-user'], $params['flyers-password']); if (!$success) return "Login Failed"; $db->query("UPDATE members SET `pollworker`=TRUE where skymanager_id=" . ((int) $user->getUserId())); if ($err = $db->getError()) return "Failed to update user permissions: $err"; $conf = ""; $conf = json_encode($config, JSON_PRETTY_PRINT); if (file_put_contents(BASE . "/inc/config/config.json", $conf) === false) return "Failed to write configuration."; return false; } $params = []; foreach ($fieldNames as $field) { if (array_key_exists($field, $_POST) && !empty($_POST[$field])) $params[$field] = $_POST[$field]; } $error = null; if (!empty($params)) $error = test_config($params); if ($error === false) { header('Location: /index.php'); die(); } ?>