web-base/core/Configuration/CreateDatabase.class.php

197 lines
7.4 KiB
PHP
Raw Normal View History

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;
class CreateDatabase {
2020-06-25 16:54:58 +02:00
// NOTE:
// explicit serial ids removed due to postgres' serial implementation
2020-04-03 15:56:04 +02:00
public static function createQueries(SQL $sql) {
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"))
2020-06-25 16:54:58 +02:00
->addRow( "en_US", 'American English')
->addRow( "de_DE", 'Deutsch Standard');
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)
2020-04-02 00:02:51 +02:00
->addInt("language_id", true, 1)
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")
->foreignKey("language_id", "Language", "uid", new SetNullStrategy());
$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)
2020-06-14 19:39:52 +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());
2020-06-17 20:20:31 +02:00
$queries[] = $sql->createTable("UserInvitation")
->addString("username",32)
->addString("email",32)
->addString("token",36)
->addDateTime("valid_until");
2020-04-02 00:02:51 +02:00
$queries[] = $sql->createTable("UserToken")
->addInt("user_id")
->addString("token", 36)
2020-06-22 21:50:58 +02:00
->addEnum("token_type", array("password_reset", "email_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)
->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")
->addDateTime("created_at", false, $sql->currentTimestamp())
->addString("title", 32)
->addString("message", 256)
->primaryKey("uid");
$queries[] = $sql->createTable("UserNotification")
->addInt("user_id")
->addInt("notification_id")
->addBool("seen")
->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")
->addBool("seen")
->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")
->addInt("month")
->addInt("count", false, 1)
->addString("cookie", 26)
->unique("month", "cookie");
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)
->primaryKey("uid");
2020-06-20 15:49:53 +02:00
$queries[] = $sql->insert("Route", array("request", "action", "target", "extra"))
->addRow("^/admin(/.*)?$", "dynamic", "\\Documents\\Admin", NULL)
2020-06-20 15:49:53 +02:00
->addRow("^/register(/)?$", "dynamic", "\\Documents\\Account", "\\Views\\Account\\Register")
->addRow("^/confirmEmail(/)?$", "dynamic", "\\Documents\\Account", "\\Views\\Account\\ConfirmEmail")
->addRow("^/acceptInvite(/)?$", "dynamic", "\\Documents\\Account", "\\Views\\Account\\AcceptInvite")
2020-06-26 14:58:17 +02:00
->addRow("^/resetPassword(/)?$", "dynamic", "\\Documents\\Account", "\\Views\\Account\\ResetPassword")
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 14:58:17 +02:00
->addBool("private", false)
2020-06-26 19:31:31 +02:00
->addBool("readonly", false)
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)
->addRow("message_confirm_email", self::MessageConfirmEmail(), false, false)
->addRow("message_accept_invite", self::MessageAcceptInvite(), false, false)
->addRow("message_reset_password", self::MessageResetPassword(), false, false);
2020-06-25 16:54:58 +02:00
(Settings::loadDefaults())->addRows($settingsQuery);
$queries[] = $settingsQuery;
2020-04-02 00:02:51 +02:00
return $queries;
}
2020-06-26 14:58:17 +02:00
private static function MessageConfirmEmail() : string {
2020-06-26 18:24:23 +02:00
return "Hello {{username}},<br>" .
"You recently created an account on {{site_name}}. Please click on the following link to " .
"confirm your email address and complete your registration. If you haven't registered an " .
"account, you can simply ignore this email. The link is valid for the next 48 hours:<br><br> " .
"<a href=\"{{link}}\">{{link}}</a><br><br> " .
"Best Regards<br> " .
"{{site_name}} Administration";
2020-06-26 14:58:17 +02:00
}
private static function MessageAcceptInvite() : string {
2020-06-26 18:24:23 +02:00
return "Hello {{username}},<br>" .
"You were invited to create an account on {{site_name}}. Please click on the following link to " .
"confirm your email address and complete your registration by choosing a new password. " .
"If you want to decline the invitation, you can simply ignore this email. The link is valid for the next 48 hours:<br><br>" .
"<a href=\"{{link}}\">{{link}}</a><br><br>" .
"Best Regards<br>" .
"{{site_name}} Administration";
2020-06-26 14:58:17 +02:00
}
private static function MessageResetPassword() : string {
2020-06-26 18:24:23 +02:00
return "Hello {{username}},<br>" .
"you requested a password reset on {{sitename}}. Please click on the following link to " .
"choose a new password. If this request was not intended, you can simply ignore the email. The Link is valid for one hour:<br><br>" .
"<a href=\"{{link}}\">{{link}}</a><br><br>" .
"Best Regards<br>" .
"{{site_name}} Administration";
2020-06-26 14:58:17 +02:00
}
2020-04-02 00:02:51 +02:00
}