From f345b7fe51fa8be3f79f2f52ea5d851c1b49a4de Mon Sep 17 00:00:00 2001 From: flashwave Date: Mon, 24 Dec 2018 21:35:25 +0100 Subject: [PATCH] Added some base stuff for warnings. --- assets/less/classes/profile/warning.less | 46 ++++++++ assets/less/main.less | 1 + .../2018_12_24_203231_add_warnings_table.php | 42 ++++++++ misuzu.php | 1 + public/profile.php | 3 + src/Users/warning.php | 100 ++++++++++++++++++ templates/user/profile.twig | 41 +++++++ 7 files changed, 234 insertions(+) create mode 100644 assets/less/classes/profile/warning.less create mode 100644 database/2018_12_24_203231_add_warnings_table.php create mode 100644 src/Users/warning.php diff --git a/assets/less/classes/profile/warning.less b/assets/less/classes/profile/warning.less new file mode 100644 index 00000000..e7991fab --- /dev/null +++ b/assets/less/classes/profile/warning.less @@ -0,0 +1,46 @@ +.profile__warning { + margin: 2px; + padding: 1px; + border-radius: 2px; + border: 1px solid var(--accent-colour); + + &--warning { + --accent-colour: #666; + } + + &--silence { + --accent-colour: #f70; + } + + &--ban { + --accent-colour: #c33; + } + + &__public { + display: flex; + flex-wrap: wrap; + } + + &__type { + min-width: 60px; + text-align: center; + background-color: var(--accent-colour); + border-radius: 1px; + padding: 0 4px; + } + + &__datetime { + min-width: 100px; + padding: 0 4px; + } + + &__note { + padding: 1px 4px; + } + + &__private { + border-top: 1px solid var(--accent-colour); + margin-top: 1px; + padding: 1px 4px; + } +} diff --git a/assets/less/main.less b/assets/less/main.less index 89bd61fa..ed664507 100644 --- a/assets/less/main.less +++ b/assets/less/main.less @@ -122,6 +122,7 @@ html { @import "classes/profile/about"; @import "classes/profile/guidelines"; @import "classes/profile/background-settings"; +@import "classes/profile/warning"; // Changelog @import "classes/changelog"; diff --git a/database/2018_12_24_203231_add_warnings_table.php b/database/2018_12_24_203231_add_warnings_table.php new file mode 100644 index 00000000..66fe7a66 --- /dev/null +++ b/database/2018_12_24_203231_add_warnings_table.php @@ -0,0 +1,42 @@ +exec(" + CREATE TABLE `msz_user_warnings` ( + `warning_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, + `user_id` INT(10) UNSIGNED NOT NULL, + `user_ip` VARBINARY(16) NOT NULL, + `issuer_id` INT(10) UNSIGNED NULL DEFAULT NULL, + `issuer_ip` VARBINARY(16) NOT NULL, + `warning_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + `warning_type` TINYINT(3) UNSIGNED NOT NULL, + `warning_note` VARCHAR(255) NOT NULL, + `warning_note_private` TEXT NOT NULL, + PRIMARY KEY (`warning_id`), + INDEX `user_warnings_user_foreign` (`user_id`), + INDEX `user_warnings_issuer_foreign` (`issuer_id`), + INDEX `user_warnings_indices` (`warning_created`, `warning_type`), + CONSTRAINT `user_warnings_issuer_foreign` + FOREIGN KEY (`issuer_id`) + REFERENCES `msz_users` (`user_id`) + ON UPDATE CASCADE + ON DELETE SET NULL, + CONSTRAINT `user_warnings_user_foreign` + FOREIGN KEY (`user_id`) + REFERENCES `msz_users` (`user_id`) + ON UPDATE CASCADE + ON DELETE CASCADE + ) + "); +} + +function migrate_down(PDO $conn): void +{ + $conn->exec(" + DROP TABLE `msz_user_warnings`; + "); +} diff --git a/misuzu.php b/misuzu.php index ebfa5e58..15286b99 100644 --- a/misuzu.php +++ b/misuzu.php @@ -64,6 +64,7 @@ require_once 'src/Users/role.php'; require_once 'src/Users/session.php'; require_once 'src/Users/user.php'; require_once 'src/Users/validation.php'; +require_once 'src/Users/warning.php'; config_load(MSZ_ROOT . '/config/config.ini'); mail_prepare(config_get_default([], 'Mail')); diff --git a/public/profile.php b/public/profile.php index 03ad82b4..cdc021b8 100644 --- a/public/profile.php +++ b/public/profile.php @@ -89,6 +89,7 @@ switch ($mode) { break; } + $warnings = user_warning_fetch($userId, 90); $notices = []; if ($isEditing) { @@ -323,6 +324,8 @@ switch ($mode) { 'profile_notices' => $notices, 'can_edit' => $canEdit, 'is_editing' => $isEditing, + 'warnings' => $warnings, + 'can_view_private_note' => $viewingOwnProfile, 'profile_fields' => user_session_active() ? user_profile_fields_display($profile, !$isEditing) : [], 'friend_info' => user_session_active() ? user_relation_info(user_session_current('user_id', 0), $profile['user_id']) : [], ]); diff --git a/src/Users/warning.php b/src/Users/warning.php new file mode 100644 index 00000000..5f0268b5 --- /dev/null +++ b/src/Users/warning.php @@ -0,0 +1,100 @@ +bindValue('user_id', $userId); + $addWarning->bindValue('user_ip', $userIp); + $addWarning->bindValue('issuer_id', $issuerId); + $addWarning->bindValue('issuer_ip', $issuerIp); + $addWarning->bindValue('type', $type); + $addWarning->bindValue('note', $publicNote); + $addWarning->bindValue('note_private', $privateNote); + + if (!$addWarning->execute()) { + return 0; + } + + return (int)db_last_insert_id(); +} + +function user_warning_count(int $userId): int +{ + if ($userId < 1) { + return 0; + } + + $countWarnings = db_prepare(' + SELECT COUNT(`warning_id`) + FROM `msz_user_warnings` + WHERE `user_id` = :user_id + '); + $countWarnings->bindValue('user_id', $userId); + return (int)($countWarnings->execute() ? $countWarnings->fetchColumn() : 0); +} + +function user_warning_remove(int $warningId): bool +{ + if ($warningId < 1) { + return false; + } + + $removeWarning = db_prepare(' + DELETE FROM `msz_user_warnings` + WHERE `warning_id` = :warning_id + '); + $removeWarning->bindValue('warning_id', $warningId); + return $removeWarning->execute(); +} + +function user_warning_fetch( + int $userId, + ?int $days = null +): array { + $fetchWarnings = db_prepare(sprintf( + ' + SELECT + uw.`warning_id`, uw.`warning_created`, uw.`warning_type`, uw.`warning_note`, + uw.`warning_note_private`, uw.`user_id`, uw.`issuer_id`, + INET6_NTOA(uw.`user_ip`) AS `user_ip`, INET6_NTOA(uw.`issuer_ip`) AS `issuer_ip`, + iu.`username` AS `issuer_username` + FROM `msz_user_warnings` AS uw + LEFT JOIN `msz_users` AS iu + ON iu.`user_id` = uw.`issuer_id` + WHERE uw.`user_id` = :user_id + %s + ', + $days !== null ? 'AND uw.`warning_created` >= NOW() - INTERVAL :days DAY' : '' + )); + $fetchWarnings->bindValue('user_id', $userId); + + if ($days !== null) { + $fetchWarnings->bindValue('days', $days); + } + + $warnings = $fetchWarnings->execute() ? $fetchWarnings->fetchAll(PDO::FETCH_ASSOC) : false; + return $warnings ? $warnings : []; +} diff --git a/templates/user/profile.twig b/templates/user/profile.twig index 5fa76a8b..db84ec47 100644 --- a/templates/user/profile.twig +++ b/templates/user/profile.twig @@ -156,6 +156,47 @@ {% endfor %} + + {% if warnings|length > 0 %} +
+ {{ container_title('Account Standing') }} + + {% for warning in warnings %} + {% if warning.warning_type == constant('MSZ_WARN_SILENCE') %} + {% set warning_text = 'Silence' %} + {% set warning_class = 'silence' %} + {% elseif warning.warning_type == constant('MSZ_WARN_BAN') %} + {% set warning_text = 'Ban' %} + {% set warning_class = 'ban' %} + {% else %} + {% set warning_text = 'Warning' %} + {% set warning_class = 'warning' %} + {% endif %} + +
+
+
+ {{ warning_text }} +
+ + + +
+ {{ warning.warning_note }} +
+
+ + {% if can_view_private_note and warning.warning_note_private|length > 0 %} +
+ {{ warning.warning_note_private|nl2br }} +
+ {% endif %} +
+ {% endfor %} +
+ {% endif %} {% endif %}