From af8cc763bfdfa436bfe284bff54657b011f63726 Mon Sep 17 00:00:00 2001 From: flashwave Date: Wed, 18 Jul 2018 18:01:17 +0200 Subject: [PATCH] Added per user colours, removed old chat key stuff and fixed a permissions editor error. --- ...18_07_18_151413_add_user_colour_column.php | 22 ++++++++++++ misuzu.php | 2 +- public/auth.php | 9 ----- public/changelog.php | 2 +- public/index.php | 12 ++----- public/manage/changelog.php | 2 +- public/manage/users.php | 35 +++++++++++++++++-- public/members.php | 2 +- public/news.php | 6 ++-- public/profile.php | 2 +- src/Forum/forum.php | 2 +- src/Forum/post.php | 2 +- src/Forum/topic.php | 4 +-- src/Users/user.php | 17 --------- src/audit_log.php | 2 +- src/changelog.php | 2 +- views/manage/users/view.twig | 32 +++++++++++++++++ 17 files changed, 102 insertions(+), 53 deletions(-) create mode 100644 database/2018_07_18_151413_add_user_colour_column.php diff --git a/database/2018_07_18_151413_add_user_colour_column.php b/database/2018_07_18_151413_add_user_colour_column.php new file mode 100644 index 00000000..88a50cbc --- /dev/null +++ b/database/2018_07_18_151413_add_user_colour_column.php @@ -0,0 +1,22 @@ +exec(' + ALTER TABLE `msz_users` + ADD COLUMN `user_colour` INT(11) NULL DEFAULT NULL AFTER `user_country`, + DROP COLUMN `user_chat_key`; + '); +} + +function migrate_down(PDO $conn): void +{ + $conn->exec(' + ALTER TABLE `msz_users` + DROP COLUMN `user_colour`, + ADD COLUMN `user_chat_key` VARCHAR(32) NULL DEFAULT NULL AFTER `user_country`; + '); +} diff --git a/misuzu.php b/misuzu.php index cd8eae00..918e0117 100644 --- a/misuzu.php +++ b/misuzu.php @@ -221,7 +221,7 @@ MIG; $getUserDisplayInfo = Database::prepare(' SELECT u.`user_id`, u.`username`, - COALESCE(r.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `colour` + COALESCE(u.`user_colour`, r.`role_colour`) as `colour` FROM `msz_users` as u LEFT JOIN `msz_roles` as r ON u.`display_role` = r.`role_id` diff --git a/public/auth.php b/public/auth.php index 9ad1f001..15765008 100644 --- a/public/auth.php +++ b/public/auth.php @@ -117,15 +117,6 @@ switch ($authMode) { set_cookie_m('uid', $userId, $cookieLife); set_cookie_m('sid', $sessionKey, $cookieLife); - if (strpos($_SERVER['HTTP_HOST'], 'flashii.net') !== false) { - $chatKey = user_generate_chat_key($userId); - - if ($chatKey !== '') { - setcookie('msz_tmp_id', $userId, $cookieLife, '/', '.flashii.net'); - setcookie('msz_tmp_key', $chatKey, $cookieLife, '/', '.flashii.net'); - } - } - header('Location: /'); return; } diff --git a/public/changelog.php b/public/changelog.php index 899d7b5a..39df690b 100644 --- a/public/changelog.php +++ b/public/changelog.php @@ -26,7 +26,7 @@ if ($changelogChange > 0) { u.`user_id`, u.`username`, DATE(`change_created`) as `change_date`, COALESCE(u.`user_title`, r.`role_title`) as `user_title`, - COALESCE(r.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `user_colour` + COALESCE(u.`user_colour`, r.`role_colour`) as `user_colour` FROM `msz_changelog_changes` as c LEFT JOIN `msz_users` as u ON u.`user_id` = c.`user_id` diff --git a/public/index.php b/public/index.php index cc20c81e..1aebba31 100644 --- a/public/index.php +++ b/public/index.php @@ -5,14 +5,6 @@ use Misuzu\Database; require_once __DIR__ . '/../misuzu.php'; -/*if ($app->getUserId() === 1) { - $sMessage = new Swift_Message('Test e-mail!'); - $sMessage->setFrom(['sys@flashii.net' => 'Flashii.net']); - $sMessage->setTo(['julianvdg@gmail.com' => 'flash']); - $sMessage->setBody('Misuzu and SwiftMailer are cool and cute.'); - var_dump(Application::mailer()->send($sMessage)); -}*/ - $config = $app->getConfig(); $tpl = $app->getTemplating(); @@ -30,7 +22,7 @@ $news = Database::query(' SELECT p.`post_id`, p.`post_title`, p.`post_text`, p.`created_at`, u.`user_id`, u.`username`, - COALESCE(r.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `user_colour` + COALESCE(u.`user_colour`, r.`role_colour`) as `user_colour` FROM `msz_news_posts` as p LEFT JOIN `msz_users` as u ON p.`user_id` = u.`user_id` @@ -50,7 +42,7 @@ $statistics = Cache::instance()->get('index:stats:v1', function () { 'lastUser' => Database::query(' SELECT u.`user_id`, u.`username`, u.`created_at`, - COALESCE(r.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `user_colour` + COALESCE(u.`user_colour`, r.`role_colour`) as `user_colour` FROM `msz_users` as u LEFT JOIN `msz_roles` as r ON r.`role_id` = u.`display_role` diff --git a/public/manage/changelog.php b/public/manage/changelog.php index bbf99895..da589956 100644 --- a/public/manage/changelog.php +++ b/public/manage/changelog.php @@ -28,7 +28,7 @@ switch ($_GET['v'] ?? null) { c.`change_id`, c.`change_log`, c.`change_created`, a.`action_name`, a.`action_colour`, a.`action_class`, u.`user_id`, u.`username`, - COALESCE(r.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `user_colour` + COALESCE(u.`user_colour`, r.`role_colour`) as `user_colour` FROM `msz_changelog_changes` as c LEFT JOIN `msz_changelog_actions` as a ON a.`action_id` = c.`action_id` diff --git a/public/manage/users.php b/public/manage/users.php index 9fe2b47f..e532fd6d 100644 --- a/public/manage/users.php +++ b/public/manage/users.php @@ -33,7 +33,7 @@ switch ($_GET['v'] ?? null) { $getManageUsers = Database::prepare(' SELECT u.`user_id`, u.`username`, - COALESCE(r.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `colour` + COALESCE(u.`user_colour`, r.`role_colour`) as `colour` FROM `msz_users` as u LEFT JOIN `msz_roles` as r ON u.`display_role` = r.`role_id` @@ -70,7 +70,7 @@ switch ($_GET['v'] ?? null) { u.*, INET6_NTOA(u.`register_ip`) as `register_ip_decoded`, INET6_NTOA(u.`last_ip`) as `last_ip_decoded`, - COALESCE(r.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `colour` + COALESCE(u.`user_colour`, r.`role_colour`) as `colour` FROM `msz_users` as u LEFT JOIN `msz_roles` as r ON u.`display_role` = r.`role_id` @@ -151,6 +151,35 @@ switch ($_GET['v'] ?? null) { user_avatar_set_from_path($manageUser['user_id'], $_FILES['avatar']['tmp_name']['file']); } + if (!empty($_POST['colour']) && is_array($_POST['colour'])) { + $userColour = null; + + if (!empty($_POST['colour']['enable'])) { + $userColour = colour_create(); + + foreach (['red', 'green', 'blue'] as $key) { + $value = (int)($_POST['colour'][$key] ?? -1); + $func = 'colour_set_' . ucfirst($key); + + if ($value < 0 || $value > 0xFF) { + echo 'invalid colour value'; + break 2; + } + + $func($userColour, $value); + } + } + + $updateUserColour = Database::prepare(' + UPDATE `msz_users` + SET `user_colour` = :colour + WHERE `user_id` = :user_id + '); + $updateUserColour->bindValue('colour', $userColour); + $updateUserColour->bindValue('user_id', $userId); + $updateUserColour->execute(); + } + if (!empty($_POST['password']) && is_array($_POST['password']) && !empty($_POST['password']['new']) @@ -276,7 +305,7 @@ switch ($_GET['v'] ?? null) { $roleId = $_GET['r'] ?? null; if ($canManagePerms) { - $tpl->var('permissions', $permissions = manage_perms_list(perms_get_role_raw($roleId))); + $tpl->var('permissions', $permissions = manage_perms_list(perms_get_role_raw($roleId ?? 0))); } if ($isPostRequest) { diff --git a/public/members.php b/public/members.php index b47114fc..5a2496c7 100644 --- a/public/members.php +++ b/public/members.php @@ -91,7 +91,7 @@ $getUsers = Database::prepare(" u.`user_id`, u.`username`, u.`user_country`, u.`created_at` as `user_joined`, u.`last_seen` as `user_last_seen`, COALESCE(u.`user_title`, r.`role_title`) as `user_title`, - COALESCE(r.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `user_colour`, + COALESCE(u.`user_colour`, r.`role_colour`) as `user_colour`, ( SELECT COUNT(`topic_id`) FROM `msz_forum_topics` diff --git a/public/news.php b/public/news.php index 170480c1..01371f1e 100644 --- a/public/news.php +++ b/public/news.php @@ -21,7 +21,7 @@ if ($postId !== null) { p.`post_id`, p.`post_title`, p.`post_text`, p.`created_at`, c.`category_id`, c.`category_name`, u.`user_id`, u.`username`, - COALESCE(r.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `user_colour` + COALESCE(u.`user_colour`, r.`role_colour`) as `user_colour` FROM `msz_news_posts` as p LEFT JOIN `msz_news_categories` as c ON p.`category_id` = c.`category_id` @@ -67,7 +67,7 @@ if ($categoryId !== null) { p.`post_id`, p.`post_title`, p.`post_text`, p.`created_at`, c.`category_id`, c.`category_name`, u.`user_id`, u.`username`, - COALESCE(r.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `user_colour` + COALESCE(u.`user_colour`, r.`role_colour`) as `user_colour` FROM `msz_news_posts` as p LEFT JOIN `msz_news_categories` as c ON p.`category_id` = c.`category_id` @@ -133,7 +133,7 @@ $getPosts = Database::prepare(' p.`post_id`, p.`post_title`, p.`post_text`, p.`created_at`, c.`category_id`, c.`category_name`, u.`user_id`, u.`username`, - COALESCE(r.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `user_colour` + COALESCE(u.`user_colour`, r.`role_colour`) as `user_colour` FROM `msz_news_posts` as p LEFT JOIN `msz_news_categories` as c ON p.`category_id` = c.`category_id` diff --git a/public/profile.php b/public/profile.php index a548b982..cc3317bb 100644 --- a/public/profile.php +++ b/public/profile.php @@ -46,7 +46,7 @@ switch ($mode) { SELECT u.*, COALESCE(u.`user_title`, r.`role_title`) as `user_title`, - COALESCE(r.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `user_colour`, + COALESCE(u.`user_colour`, r.`role_colour`) as `user_colour`, ( SELECT COUNT(`topic_id`) FROM `msz_forum_topics` diff --git a/src/Forum/forum.php b/src/Forum/forum.php index bb0a860a..a99bb57b 100644 --- a/src/Forum/forum.php +++ b/src/Forum/forum.php @@ -178,7 +178,7 @@ define('MSZ_FORUM_GET_CHILDREN_QUERY_STANDARD', ' p.`post_created` as `recent_post_created`, u.`user_id` as `recent_post_user_id`, u.`username` as `recent_post_username`, - COALESCE(r.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `recent_post_user_colour`, + COALESCE(u.`user_colour`, r.`role_colour`) as `recent_post_user_colour`, ( SELECT COUNT(`topic_id`) FROM `msz_forum_topics` diff --git a/src/Forum/post.php b/src/Forum/post.php index 44d0ae29..9a5d2872 100644 --- a/src/Forum/post.php +++ b/src/Forum/post.php @@ -69,7 +69,7 @@ define('MSZ_FORUM_POST_LISTING_QUERY_STANDARD', ' u.`user_id` as `poster_id`, u.`username` as `poster_name`, u.`created_at` as `poster_joined`, - COALESCE(r.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `poster_colour` + COALESCE(u.`user_colour`, r.`role_colour`) as `poster_colour` FROM `msz_forum_posts` as p LEFT JOIN `msz_users` as u ON u.`user_id` = p.`user_id` diff --git a/src/Forum/topic.php b/src/Forum/topic.php index eb175b77..50f24635 100644 --- a/src/Forum/topic.php +++ b/src/Forum/topic.php @@ -88,12 +88,12 @@ define('MSZ_TOPIC_LISTING_QUERY_STANDARD', ' :user_id as `target_user_id`, t.`topic_id`, t.`topic_title`, t.`topic_locked`, t.`topic_type`, t.`topic_created`, au.`user_id` as `author_id`, au.`username` as `author_name`, - COALESCE(ar.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `author_colour`, + COALESCE(au.`user_colour`, ar.`role_colour`) as `author_colour`, lp.`post_id` as `response_id`, lp.`post_created` as `response_created`, lu.`user_id` as `respondent_id`, lu.`username` as `respondent_name`, - COALESCE(lr.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `respondent_colour`, + COALESCE(lu.`user_colour`, lr.`role_colour`) as `respondent_colour`, ( SELECT COUNT(`post_id`) FROM `msz_forum_posts` diff --git a/src/Users/user.php b/src/Users/user.php index 09600c5e..7b6df0e1 100644 --- a/src/Users/user.php +++ b/src/Users/user.php @@ -48,23 +48,6 @@ function user_password_hash(string $password): string return password_hash($password, MSZ_USERS_PASSWORD_HASH_ALGO); } -// Temporary key generation for chat login. -// Should eventually be replaced with a callback login system. -function user_generate_chat_key(int $userId): string -{ - $chatKey = bin2hex(random_bytes(16)); - - $setChatKey = Database::prepare(' - UPDATE `msz_users` - SET `user_chat_key` = :user_chat_key - WHERE `user_id` = :user_id - '); - $setChatKey->bindValue('user_chat_key', $chatKey); - $setChatKey->bindValue('user_id', $userId); - - return $setChatKey->execute() ? $chatKey : ''; -} - define('MSZ_USER_AVATAR_FORMAT', '%d.msz'); function user_avatar_delete(int $userId): void diff --git a/src/audit_log.php b/src/audit_log.php index 3351b100..0401a63f 100644 --- a/src/audit_log.php +++ b/src/audit_log.php @@ -39,7 +39,7 @@ function audit_log_list(int $offset, int $take, int $userId = 0): array l.`log_id`, l.`log_action`, l.`log_params`, l.`log_created`, u.`user_id`, u.`username`, INET6_NTOA(l.`log_ip`) as `log_ip`, - COALESCE(r.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `user_colour` + COALESCE(u.`user_colour`, r.`role_colour`) as `user_colour` FROM `msz_audit_log` as l LEFT JOIN `msz_users` as u ON u.`user_id` = l.`user_id` diff --git a/src/changelog.php b/src/changelog.php index b3dff49c..cad05e1b 100644 --- a/src/changelog.php +++ b/src/changelog.php @@ -49,7 +49,7 @@ define('MSZ_CHANGELOG_GET_QUERY', ' u.`user_id`, u.`username`, DATE(`change_created`) as `change_date`, !ISNULL(c.`change_text`) as `change_has_text`, - COALESCE(r.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `user_colour` + COALESCE(u.`user_colour`, r.`role_colour`) as `user_colour` FROM `msz_changelog_changes` as c LEFT JOIN `msz_users` as u ON u.`user_id` = c.`user_id` diff --git a/views/manage/users/view.twig b/views/manage/users/view.twig index ace67652..4b11e38c 100644 --- a/views/manage/users/view.twig +++ b/views/manage/users/view.twig @@ -115,6 +115,38 @@ {% endfor %} +

Colour

+ + {% set colour_is_defined = view_user is defined and view_user.user_colour is not null and not view_user.user_colour|colour_get_inherit %} + + + + + + + + + {% if can_manage_perms %}

Permissions