2020-04-02 00:02:51 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Configuration;
|
|
|
|
|
2020-04-03 15:56:04 +02:00
|
|
|
use Driver\SQL\SQL;
|
2020-04-02 00:02:51 +02:00
|
|
|
use \Driver\SQL\Strategy\SetNullStrategy;
|
|
|
|
use \Driver\SQL\Strategy\CascadeStrategy;
|
|
|
|
|
2021-01-07 15:54:19 +01:00
|
|
|
class CreateDatabase extends DatabaseScript {
|
2020-04-02 00:02:51 +02:00
|
|
|
|
2020-06-25 16:54:58 +02:00
|
|
|
// NOTE:
|
|
|
|
// explicit serial ids removed due to postgres' serial implementation
|
|
|
|
|
2021-04-02 21:58:06 +02:00
|
|
|
public static function createQueries(SQL $sql): array {
|
2020-04-02 00:02:51 +02:00
|
|
|
$queries = array();
|
|
|
|
|
|
|
|
// Language
|
|
|
|
$queries[] = $sql->createTable("Language")
|
|
|
|
->addSerial("uid")
|
|
|
|
->addString("code", 5)
|
|
|
|
->addString("name", 32)
|
|
|
|
->primaryKey("uid")
|
|
|
|
->unique("code")
|
|
|
|
->unique("name");
|
|
|
|
|
2020-06-25 16:58:26 +02:00
|
|
|
$queries[] = $sql->insert("Language", array("code", "name"))
|
2021-04-02 21:58:06 +02:00
|
|
|
->addRow("en_US", 'American English')
|
|
|
|
->addRow("de_DE", 'Deutsch Standard');
|
2020-04-02 00:02:51 +02:00
|
|
|
|
2022-02-20 18:31:54 +01:00
|
|
|
|
|
|
|
$queries[] = $sql->createTable("GpgKey")
|
|
|
|
->addSerial("uid")
|
|
|
|
->addString("fingerprint", 64)
|
|
|
|
->addDateTime("added", false, $sql->now())
|
|
|
|
->addDateTime("expires")
|
|
|
|
->addBool("confirmed")
|
|
|
|
->addString("algorithm", 32)
|
|
|
|
->primaryKey("uid");
|
|
|
|
|
|
|
|
$queries[] = $sql->createTable("2FA")
|
|
|
|
->addSerial("uid")
|
|
|
|
->addEnum("type", ["totp","fido"])
|
|
|
|
->addString("data", 512) // either totp secret, fido challenge or fido public key information
|
|
|
|
->addBool("confirmed", false)
|
|
|
|
->addDateTime("added", false, $sql->now())
|
|
|
|
->primaryKey("uid");
|
|
|
|
|
2020-04-02 00:02:51 +02:00
|
|
|
$queries[] = $sql->createTable("User")
|
|
|
|
->addSerial("uid")
|
|
|
|
->addString("email", 64, true)
|
|
|
|
->addString("name", 32)
|
2020-06-23 18:40:43 +02:00
|
|
|
->addString("password", 128)
|
2022-02-20 16:53:26 +01:00
|
|
|
->addString("fullName", 64, false, "")
|
|
|
|
->addString("profilePicture", 64, true)
|
|
|
|
->addDateTime("last_online", true, NULL)
|
2020-06-29 16:47:02 +02:00
|
|
|
->addBool("confirmed", false)
|
2020-04-02 00:02:51 +02:00
|
|
|
->addInt("language_id", true, 1)
|
2022-02-20 16:53:26 +01:00
|
|
|
->addInt("gpg_id", true)
|
|
|
|
->addInt("2fa_id", true)
|
2020-04-04 01:15:59 +02:00
|
|
|
->addDateTime("registered_at", false, $sql->currentTimestamp())
|
2020-04-02 00:02:51 +02:00
|
|
|
->primaryKey("uid")
|
|
|
|
->unique("email")
|
|
|
|
->unique("name")
|
2022-02-20 16:53:26 +01:00
|
|
|
->foreignKey("language_id", "Language", "uid", new SetNullStrategy())
|
|
|
|
->foreignKey("gpg_id", "GpgKey", "uid", new SetNullStrategy())
|
|
|
|
->foreignKey("2fa_id", "2FA", "uid", new SetNullStrategy());
|
2020-04-02 00:02:51 +02:00
|
|
|
|
|
|
|
$queries[] = $sql->createTable("Session")
|
|
|
|
->addSerial("uid")
|
|
|
|
->addBool("active", true)
|
|
|
|
->addDateTime("expires")
|
|
|
|
->addInt("user_id")
|
|
|
|
->addString("ipAddress", 45)
|
|
|
|
->addString("os", 64)
|
|
|
|
->addString("browser", 64)
|
|
|
|
->addJson("data", false, '{}')
|
|
|
|
->addBool("stay_logged_in", true)
|
2021-04-02 21:58:06 +02:00
|
|
|
->addString("csrf_token", 16)
|
2020-04-02 00:02:51 +02:00
|
|
|
->primaryKey("uid", "user_id")
|
|
|
|
->foreignKey("user_id", "User", "uid", new CascadeStrategy());
|
|
|
|
|
|
|
|
$queries[] = $sql->createTable("UserToken")
|
|
|
|
->addInt("user_id")
|
|
|
|
->addString("token", 36)
|
2022-02-20 16:53:26 +01:00
|
|
|
->addEnum("token_type", array("password_reset", "email_confirm", "invite", "gpg_confirm"))
|
2020-04-02 00:02:51 +02:00
|
|
|
->addDateTime("valid_until")
|
2020-06-22 21:50:58 +02:00
|
|
|
->addBool("used", false)
|
2020-04-02 00:02:51 +02:00
|
|
|
->foreignKey("user_id", "User", "uid", new CascadeStrategy());
|
|
|
|
$queries[] = $sql->createTable("Group")
|
|
|
|
->addSerial("uid")
|
|
|
|
->addString("name", 32)
|
2020-06-23 15:31:09 +02:00
|
|
|
->addString("color", 10)
|
2020-04-02 00:02:51 +02:00
|
|
|
->primaryKey("uid")
|
|
|
|
->unique("name");
|
|
|
|
|
2020-06-25 16:58:26 +02:00
|
|
|
$queries[] = $sql->insert("Group", array("name", "color"))
|
2020-06-25 16:54:58 +02:00
|
|
|
->addRow(USER_GROUP_MODERATOR_NAME, "#007bff")
|
|
|
|
->addRow(USER_GROUP_SUPPORT_NAME, "#28a745")
|
|
|
|
->addRow(USER_GROUP_ADMIN_NAME, "#dc3545");
|
2020-04-02 00:02:51 +02:00
|
|
|
|
|
|
|
$queries[] = $sql->createTable("UserGroup")
|
|
|
|
->addInt("user_id")
|
|
|
|
->addInt("group_id")
|
|
|
|
->unique("user_id", "group_id")
|
2020-06-23 16:41:08 +02:00
|
|
|
->foreignKey("user_id", "User", "uid", new CascadeStrategy())
|
|
|
|
->foreignKey("group_id", "Group", "uid", new CascadeStrategy());
|
2020-04-02 00:02:51 +02:00
|
|
|
|
2020-04-02 21:19:06 +02:00
|
|
|
$queries[] = $sql->createTable("Notification")
|
|
|
|
->addSerial("uid")
|
2021-04-02 21:58:06 +02:00
|
|
|
->addEnum("type", array("default", "message", "warning"), false, "default")
|
2020-04-02 21:19:06 +02:00
|
|
|
->addDateTime("created_at", false, $sql->currentTimestamp())
|
|
|
|
->addString("title", 32)
|
|
|
|
->addString("message", 256)
|
|
|
|
->primaryKey("uid");
|
|
|
|
|
|
|
|
$queries[] = $sql->createTable("UserNotification")
|
|
|
|
->addInt("user_id")
|
|
|
|
->addInt("notification_id")
|
2020-06-26 23:32:45 +02:00
|
|
|
->addBool("seen", false)
|
2020-04-02 21:19:06 +02:00
|
|
|
->foreignKey("user_id", "User", "uid")
|
|
|
|
->foreignKey("notification_id", "Notification", "uid")
|
|
|
|
->unique("user_id", "notification_id");
|
|
|
|
|
|
|
|
$queries[] = $sql->createTable("GroupNotification")
|
|
|
|
->addInt("group_id")
|
|
|
|
->addInt("notification_id")
|
2020-06-26 23:32:45 +02:00
|
|
|
->addBool("seen", false)
|
2020-04-02 21:19:06 +02:00
|
|
|
->foreignKey("group_id", "Group", "uid")
|
|
|
|
->foreignKey("notification_id", "Notification", "uid")
|
|
|
|
->unique("group_id", "notification_id");
|
|
|
|
|
2020-04-02 00:02:51 +02:00
|
|
|
$queries[] = $sql->createTable("ApiKey")
|
|
|
|
->addSerial("uid")
|
|
|
|
->addInt("user_id")
|
|
|
|
->addBool("active", true)
|
|
|
|
->addString("api_key", 64)
|
|
|
|
->addDateTime("valid_until")
|
|
|
|
->primaryKey("uid")
|
|
|
|
->foreignKey("user_id", "User", "uid");
|
|
|
|
|
2020-06-17 23:50:08 +02:00
|
|
|
$queries[] = $sql->createTable("Visitor")
|
2020-07-01 21:10:25 +02:00
|
|
|
->addInt("day")
|
2020-06-17 23:50:08 +02:00
|
|
|
->addInt("count", false, 1)
|
|
|
|
->addString("cookie", 26)
|
2020-07-01 21:10:25 +02:00
|
|
|
->unique("day", "cookie");
|
2020-06-17 23:50:08 +02:00
|
|
|
|
2020-06-19 13:13:13 +02:00
|
|
|
$queries[] = $sql->createTable("Route")
|
|
|
|
->addSerial("uid")
|
|
|
|
->addString("request", 128)
|
|
|
|
->addEnum("action", array("redirect_temporary", "redirect_permanently", "static", "dynamic"))
|
|
|
|
->addString("target", 128)
|
|
|
|
->addString("extra", 64, true)
|
|
|
|
->addBool("active", true)
|
2021-04-06 20:31:52 +02:00
|
|
|
->primaryKey("uid")
|
|
|
|
->unique("request");
|
2020-06-19 13:13:13 +02:00
|
|
|
|
2020-06-20 15:49:53 +02:00
|
|
|
$queries[] = $sql->insert("Route", array("request", "action", "target", "extra"))
|
2020-06-23 15:31:09 +02:00
|
|
|
->addRow("^/admin(/.*)?$", "dynamic", "\\Documents\\Admin", NULL)
|
2022-02-20 16:53:26 +01:00
|
|
|
->addRow("^/register/?$", "dynamic", "\\Documents\\Account", "account/register.twig")
|
|
|
|
->addRow("^/confirmEmail/?$", "dynamic", "\\Documents\\Account", "account/confirm_email.twig")
|
|
|
|
->addRow("^/acceptInvite/?$", "dynamic", "\\Documents\\Account", "account/accept_invite.twig")
|
|
|
|
->addRow("^/resetPassword/?$", "dynamic", "\\Documents\\Account", "account/reset_password.twig")
|
|
|
|
->addRow("^/resendConfirmEmail/?$", "dynamic", "\\Documents\\Account", "account/resend_confirm_email.twig")
|
2020-06-22 19:09:02 +02:00
|
|
|
->addRow("^/$", "static", "/static/welcome.html", NULL);
|
2020-06-19 13:13:13 +02:00
|
|
|
|
2020-06-25 16:54:58 +02:00
|
|
|
$queries[] = $sql->createTable("Settings")
|
|
|
|
->addString("name", 32)
|
|
|
|
->addString("value", 1024, true)
|
2020-06-26 23:32:45 +02:00
|
|
|
->addBool("private", false) // these values are not returned from '/api/settings/get', but can be changed
|
|
|
|
->addBool("readonly", false) // these values are neither returned, nor can be changed from outside
|
2020-06-25 16:54:58 +02:00
|
|
|
->primaryKey("name");
|
|
|
|
|
2020-06-26 19:31:31 +02:00
|
|
|
$settingsQuery = $sql->insert("Settings", array("name", "value", "private", "readonly"))
|
2020-06-25 16:54:58 +02:00
|
|
|
// ->addRow("mail_enabled", "0") # this key will be set during installation
|
2020-06-26 19:31:31 +02:00
|
|
|
->addRow("mail_host", "", false, false)
|
|
|
|
->addRow("mail_port", "", false, false)
|
|
|
|
->addRow("mail_username", "", false, false)
|
|
|
|
->addRow("mail_password", "", true, false)
|
|
|
|
->addRow("mail_from", "", false, false)
|
2022-02-20 16:53:26 +01:00
|
|
|
->addRow("mail_last_sync", "", false, false)
|
|
|
|
->addRow("mail_footer", "", false, false);
|
2020-06-25 16:54:58 +02:00
|
|
|
|
|
|
|
(Settings::loadDefaults())->addRows($settingsQuery);
|
|
|
|
$queries[] = $settingsQuery;
|
|
|
|
|
2020-06-26 23:32:45 +02:00
|
|
|
$queries[] = $sql->createTable("ContactRequest")
|
|
|
|
->addSerial("uid")
|
|
|
|
->addString("from_name", 32)
|
|
|
|
->addString("from_email", 64)
|
|
|
|
->addString("message", 512)
|
2021-04-09 16:05:36 +02:00
|
|
|
->addString("messageId", 78, true) # null = don't sync with mails (usually if mail could not be sent)
|
2020-06-26 23:32:45 +02:00
|
|
|
->addDateTime("created_at", false, $sql->currentTimestamp())
|
2021-04-09 16:05:36 +02:00
|
|
|
->unique("messageId")
|
2020-06-26 23:32:45 +02:00
|
|
|
->primaryKey("uid");
|
|
|
|
|
2021-04-09 16:05:36 +02:00
|
|
|
$queries[] = $sql->createTable("ContactMessage")
|
|
|
|
->addSerial("uid")
|
|
|
|
->addInt("request_id")
|
|
|
|
->addInt("user_id", true) # null = customer has sent this message
|
|
|
|
->addString("message", 512)
|
|
|
|
->addString("messageId", 78)
|
|
|
|
->addDateTime("created_at", false, $sql->currentTimestamp())
|
2021-04-10 00:44:34 +02:00
|
|
|
->addBool("read", false)
|
2021-04-09 16:05:36 +02:00
|
|
|
->unique("messageId")
|
|
|
|
->primaryKey("uid")
|
2021-04-10 00:44:34 +02:00
|
|
|
->foreignKey("request_id", "ContactRequest", "uid", new CascadeStrategy())
|
|
|
|
->foreignKey("user_id", "User", "uid", new SetNullStrategy());
|
|
|
|
|
2020-06-27 01:18:10 +02:00
|
|
|
$queries[] = $sql->createTable("ApiPermission")
|
|
|
|
->addString("method", 32)
|
|
|
|
->addJson("groups", true, '[]')
|
2020-06-27 22:47:12 +02:00
|
|
|
->addString("description", 128, false, "")
|
2020-06-27 01:18:10 +02:00
|
|
|
->primaryKey("method");
|
|
|
|
|
2022-02-20 16:53:26 +01:00
|
|
|
$queries[] = $sql->createTable("MailQueue")
|
|
|
|
->addSerial("uid")
|
|
|
|
->addString("from", 64)
|
|
|
|
->addString("to", 64)
|
|
|
|
->addString("subject")
|
|
|
|
->addString("body")
|
|
|
|
->addString("replyTo", 64, true)
|
|
|
|
->addString("replyName", 32, true)
|
|
|
|
->addString("gpgFingerprint", 64, true)
|
|
|
|
->addEnum("status", ["waiting","success","error"], false, 'waiting')
|
|
|
|
->addInt("retryCount", false, 5)
|
|
|
|
->addDateTime("nextTry", false, $sql->now())
|
|
|
|
->addString("errorMessage", NULL, true)
|
|
|
|
->primaryKey("uid");
|
|
|
|
$queries = array_merge($queries, \Configuration\Patch\log::createTableLog($sql, "MailQueue", 30));
|
|
|
|
|
|
|
|
$queries[] = $sql->createTable("News")
|
|
|
|
->addSerial("uid")
|
|
|
|
->addInt("publishedBy")
|
|
|
|
->addDateTime("publishedAt", false, $sql->now())
|
|
|
|
->addString("title", 128)
|
|
|
|
->addString("text", 1024)
|
|
|
|
->foreignKey("publishedBy", "User", "uid", new CascadeStrategy())
|
|
|
|
->primaryKey("uid");
|
|
|
|
|
2020-06-27 22:47:12 +02:00
|
|
|
$queries[] = $sql->insert("ApiPermission", array("method", "groups", "description"))
|
|
|
|
->addRow("ApiKey/create", array(), "Allows users to create API-Keys for themselves")
|
|
|
|
->addRow("ApiKey/fetch", array(), "Allows users to list their API-Keys")
|
|
|
|
->addRow("ApiKey/refresh", array(), "Allows users to refresh their API-Keys")
|
|
|
|
->addRow("ApiKey/revoke", array(), "Allows users to revoke their API-Keys")
|
|
|
|
->addRow("Groups/fetch", array(USER_GROUP_SUPPORT, USER_GROUP_ADMIN), "Allows users to list all available groups")
|
|
|
|
->addRow("Groups/create", array(USER_GROUP_ADMIN), "Allows users to create a new groups")
|
|
|
|
->addRow("Groups/delete", array(USER_GROUP_ADMIN), "Allows users to delete a group")
|
|
|
|
->addRow("Routes/fetch", array(USER_GROUP_ADMIN), "Allows users to list all configured routes")
|
|
|
|
->addRow("Routes/save", array(USER_GROUP_ADMIN), "Allows users to create, delete and modify routes")
|
|
|
|
->addRow("Mail/test", array(USER_GROUP_SUPPORT, USER_GROUP_ADMIN), "Allows users to send a test email to a given address")
|
2021-04-09 16:05:36 +02:00
|
|
|
->addRow("Mail/Sync", array(USER_GROUP_SUPPORT, USER_GROUP_ADMIN), "Allows users to synchronize mails with the database")
|
2020-06-27 22:47:12 +02:00
|
|
|
->addRow("Settings/get", array(USER_GROUP_ADMIN), "Allows users to fetch server settings")
|
|
|
|
->addRow("Settings/set", array(USER_GROUP_ADMIN), "Allows users create, delete or modify server settings")
|
|
|
|
->addRow("Stats", array(USER_GROUP_ADMIN, USER_GROUP_SUPPORT), "Allows users to fetch server stats")
|
|
|
|
->addRow("User/create", array(USER_GROUP_ADMIN), "Allows users to create a new user, email address does not need to be confirmed")
|
|
|
|
->addRow("User/fetch", array(USER_GROUP_ADMIN, USER_GROUP_SUPPORT), "Allows users to list all registered users")
|
|
|
|
->addRow("User/get", array(USER_GROUP_ADMIN, USER_GROUP_SUPPORT), "Allows users to get information about a single user")
|
|
|
|
->addRow("User/invite", array(USER_GROUP_ADMIN), "Allows users to create a new user and send them an invitation link")
|
|
|
|
->addRow("User/edit", array(USER_GROUP_ADMIN), "Allows users to edit details and group memberships of any user")
|
|
|
|
->addRow("User/delete", array(USER_GROUP_ADMIN), "Allows users to delete any other user")
|
2020-07-01 21:10:25 +02:00
|
|
|
->addRow("Permission/fetch", array(USER_GROUP_ADMIN), "Allows users to list all API permissions")
|
2021-04-10 00:44:34 +02:00
|
|
|
->addRow("Visitors/stats", array(USER_GROUP_ADMIN, USER_GROUP_SUPPORT), "Allows users to see visitor statistics")
|
|
|
|
->addRow("Contact/respond", array(USER_GROUP_ADMIN, USER_GROUP_SUPPORT), "Allows users to respond to contact requests")
|
|
|
|
->addRow("Contact/fetch", array(USER_GROUP_ADMIN, USER_GROUP_SUPPORT), "Allows users to fetch all contact requests")
|
|
|
|
->addRow("Contact/get", array(USER_GROUP_ADMIN, USER_GROUP_SUPPORT), "Allows users to see messages within a contact request");
|
2021-01-07 15:54:19 +01:00
|
|
|
|
|
|
|
self::loadPatches($queries, $sql);
|
2020-06-27 01:18:10 +02:00
|
|
|
|
2020-04-02 00:02:51 +02:00
|
|
|
return $queries;
|
|
|
|
}
|
2020-06-26 14:58:17 +02:00
|
|
|
|
2021-01-07 15:54:19 +01:00
|
|
|
private static function loadPatches(&$queries, $sql) {
|
|
|
|
$patchDirectory = './core/Configuration/Patch/';
|
|
|
|
if (file_exists($patchDirectory) && is_dir($patchDirectory)) {
|
|
|
|
$scan_arr = scandir($patchDirectory);
|
2021-04-02 21:58:06 +02:00
|
|
|
$files_arr = array_diff($scan_arr, array('.', '..'));
|
2021-01-07 15:54:19 +01:00
|
|
|
foreach ($files_arr as $file) {
|
|
|
|
$suffix = ".class.php";
|
|
|
|
if (endsWith($file, $suffix)) {
|
|
|
|
$className = substr($file, 0, strlen($file) - strlen($suffix));
|
|
|
|
$className = "\\Configuration\\Patch\\$className";
|
|
|
|
$method = "$className::createQueries";
|
|
|
|
$patchQueries = call_user_func($method, $sql);
|
2021-04-02 21:58:06 +02:00
|
|
|
foreach ($patchQueries as $query) $queries[] = $query;
|
2021-01-07 15:54:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-02 00:02:51 +02:00
|
|
|
}
|