From b7de5acfd8bd0da170f105faae10849beea7bc7b Mon Sep 17 00:00:00 2001 From: flashwave Date: Thu, 3 Aug 2023 12:40:30 +0000 Subject: [PATCH] Fixed search and updated collations of various fields to more appropriate ones. --- ...03_update_collations_in_various_tables.php | 162 ++++++++++++++++++ public-legacy/search.php | 2 +- src/Http/Handlers/HomeHandler.php | 2 +- src/Users/User.php | 16 +- src/Users/Users.php | 6 +- templates/user/macros.twig | 61 ++++--- 6 files changed, 212 insertions(+), 37 deletions(-) create mode 100644 database/2023_08_03_114403_update_collations_in_various_tables.php diff --git a/database/2023_08_03_114403_update_collations_in_various_tables.php b/database/2023_08_03_114403_update_collations_in_various_tables.php new file mode 100644 index 0000000..30a7f2e --- /dev/null +++ b/database/2023_08_03_114403_update_collations_in_various_tables.php @@ -0,0 +1,162 @@ +execute(' + ALTER TABLE msz_audit_log + CHANGE COLUMN log_action log_action VARCHAR(50) NOT NULL COLLATE "ascii_general_ci" AFTER user_id, + CHANGE COLUMN log_country log_country CHAR(2) NOT NULL DEFAULT "XX" COLLATE "ascii_general_ci" AFTER log_ip; + '); + + $conn->execute(' + ALTER TABLE msz_auth_tfa + CHANGE COLUMN tfa_token tfa_token CHAR(32) NOT NULL COLLATE "ascii_bin" AFTER user_id; + '); + + $conn->execute(' + ALTER TABLE msz_changelog_changes + CHANGE COLUMN change_log change_log VARCHAR(255) NOT NULL COLLATE "utf8mb4_unicode_520_ci" AFTER change_created, + CHANGE COLUMN change_text change_text TEXT NULL DEFAULT NULL COLLATE "utf8mb4_unicode_520_ci" AFTER change_log; + '); + + $conn->execute(' + ALTER TABLE msz_changelog_tags + CHANGE COLUMN tag_name tag_name VARCHAR(255) NOT NULL COLLATE "utf8mb4_unicode_520_ci" AFTER tag_id, + CHANGE COLUMN tag_description tag_description TEXT NULL DEFAULT NULL COLLATE "utf8mb4_unicode_520_ci" AFTER tag_name; + '); + + $conn->execute(' + ALTER TABLE msz_comments_categories + CHANGE COLUMN category_name category_name VARCHAR(255) NOT NULL COLLATE "ascii_bin" AFTER category_id; + '); + + $conn->execute(' + ALTER TABLE msz_comments_posts + CHANGE COLUMN comment_text comment_text TEXT NOT NULL COLLATE "utf8mb4_unicode_520_ci" AFTER comment_reply_to; + '); + + $conn->execute(' + ALTER TABLE msz_config + CHANGE COLUMN config_name config_name VARCHAR(100) NOT NULL COLLATE "ascii_general_ci" FIRST; + '); + + $conn->execute(' + ALTER TABLE msz_emoticons + CHANGE COLUMN emote_url emote_url VARCHAR(255) NOT NULL COLLATE "ascii_bin" AFTER emote_hierarchy; + '); + + $conn->execute(' + ALTER TABLE msz_emoticons_strings + CHANGE COLUMN emote_string emote_string VARCHAR(50) NOT NULL COLLATE "ascii_general_ci" AFTER emote_string_order; + '); + + $conn->execute(' + ALTER TABLE msz_forum_categories + CHANGE COLUMN forum_name forum_name VARCHAR(255) NOT NULL COLLATE "utf8mb4_unicode_520_ci" AFTER forum_parent, + CHANGE COLUMN forum_description forum_description TEXT NULL DEFAULT NULL COLLATE "utf8mb4_unicode_520_ci" AFTER forum_type, + CHANGE COLUMN forum_icon forum_icon VARCHAR(50) NULL DEFAULT NULL COLLATE "ascii_bin" AFTER forum_description, + CHANGE COLUMN forum_link forum_link VARCHAR(255) NULL DEFAULT NULL COLLATE "ascii_bin" AFTER forum_colour; + '); + + $conn->execute(' + ALTER TABLE msz_forum_posts + CHANGE COLUMN post_text post_text MEDIUMTEXT NOT NULL COLLATE "utf8mb4_unicode_520_ci" AFTER post_ip; + '); + + $conn->execute(' + ALTER TABLE msz_forum_topics + CHANGE COLUMN topic_title topic_title VARCHAR(255) NOT NULL COLLATE "utf8mb4_unicode_520_ci" AFTER topic_type; + '); + + $conn->execute(' + ALTER TABLE msz_forum_topics_redirects + CHANGE COLUMN topic_redir_url topic_redir_url VARCHAR(255) NOT NULL COLLATE "ascii_bin" AFTER user_id; + '); + + $conn->execute(' + ALTER TABLE msz_login_attempts + CHANGE COLUMN attempt_country attempt_country CHAR(2) NOT NULL DEFAULT "XX" COLLATE "ascii_general_ci" AFTER attempt_ip, + CHANGE COLUMN attempt_user_agent attempt_user_agent TEXT NOT NULL COLLATE "utf8mb4_unicode_520_ci" AFTER attempt_created; + '); + + $conn->execute(' + ALTER TABLE msz_news_categories + CHANGE COLUMN category_name category_name VARCHAR(255) NOT NULL COLLATE "utf8mb4_unicode_520_ci" AFTER category_id, + CHANGE COLUMN category_description category_description TEXT NOT NULL COLLATE "utf8mb4_unicode_520_ci" AFTER category_name; + '); + + $conn->execute(' + ALTER TABLE msz_news_posts + CHANGE COLUMN post_title post_title VARCHAR(255) NOT NULL COLLATE "utf8mb4_unicode_520_ci" AFTER post_is_featured, + CHANGE COLUMN post_text post_text TEXT NOT NULL COLLATE "utf8mb4_unicode_520_ci" AFTER post_title; + '); + + $conn->execute(' + ALTER TABLE msz_profile_fields + CHANGE COLUMN field_key field_key VARCHAR(50) NOT NULL COLLATE "ascii_general_ci" AFTER field_order, + CHANGE COLUMN field_title field_title VARCHAR(50) NOT NULL COLLATE "utf8mb4_unicode_520_ci" AFTER field_key, + CHANGE COLUMN field_regex field_regex VARCHAR(255) NOT NULL COLLATE "ascii_bin" AFTER field_title; + '); + + $conn->execute(' + ALTER TABLE msz_profile_fields_formats + CHANGE COLUMN format_regex format_regex VARCHAR(255) NULL DEFAULT NULL COLLATE "ascii_bin" AFTER field_id, + CHANGE COLUMN format_link format_link VARCHAR(255) NULL DEFAULT NULL COLLATE "ascii_bin" AFTER format_regex, + CHANGE COLUMN format_display format_display VARCHAR(255) NOT NULL DEFAULT "%s" COLLATE "utf8mb4_unicode_520_ci" AFTER format_link; + '); + + $conn->execute(' + ALTER TABLE msz_profile_fields_values + CHANGE COLUMN field_value field_value VARCHAR(255) NOT NULL COLLATE "utf8mb4_unicode_520_ci" AFTER format_id; + '); + + $conn->execute(' + ALTER TABLE msz_roles + CHANGE COLUMN role_name role_name VARCHAR(255) NOT NULL COLLATE "utf8mb4_unicode_520_ci" AFTER role_hierarchy, + CHANGE COLUMN role_title role_title VARCHAR(64) NULL DEFAULT NULL COLLATE "utf8mb4_unicode_520_ci" AFTER role_name, + CHANGE COLUMN role_description role_description TEXT NULL DEFAULT NULL COLLATE "utf8mb4_unicode_520_ci" AFTER role_title; + '); + + $conn->execute(' + ALTER TABLE msz_sessions + CHANGE COLUMN session_user_agent session_user_agent TEXT NOT NULL COLLATE "utf8mb4_unicode_520_ci" AFTER session_ip_last, + CHANGE COLUMN session_country session_country CHAR(2) NOT NULL DEFAULT "XX" COLLATE "ascii_general_ci" AFTER session_client_info; + '); + + $conn->execute(' + ALTER TABLE msz_users + CHANGE COLUMN username username VARCHAR(255) NOT NULL COLLATE "utf8mb4_unicode_520_ci" AFTER user_id, + CHANGE COLUMN password password VARCHAR(255) NULL DEFAULT NULL COLLATE "ascii_bin" AFTER username, + CHANGE COLUMN email email VARCHAR(255) NOT NULL COLLATE "utf8mb4_unicode_520_ci" AFTER password, + CHANGE COLUMN user_country user_country CHAR(2) NOT NULL DEFAULT "XX" COLLATE "ascii_general_ci" AFTER user_super, + CHANGE COLUMN user_totp_key user_totp_key CHAR(26) NULL DEFAULT NULL COLLATE "ascii_bin" AFTER display_role, + CHANGE COLUMN user_about_content user_about_content TEXT NULL DEFAULT NULL COLLATE "utf8mb4_unicode_520_ci" AFTER user_totp_key, + CHANGE COLUMN user_signature_content user_signature_content TEXT NULL DEFAULT NULL COLLATE "utf8mb4_unicode_520_ci" AFTER user_about_parser, + CHANGE COLUMN user_title user_title VARCHAR(64) NULL DEFAULT NULL COLLATE "utf8mb4_unicode_520_ci" AFTER user_background_settings; + '); + + $conn->execute(' + ALTER TABLE msz_users_bans + CHANGE COLUMN ban_reason_public ban_reason_public TEXT NOT NULL COLLATE "utf8mb4_unicode_520_ci" AFTER ban_severity, + CHANGE COLUMN ban_reason_private ban_reason_private TEXT NOT NULL COLLATE "utf8mb4_unicode_520_ci" AFTER ban_reason_public; + '); + + $conn->execute(' + ALTER TABLE msz_users_modnotes + CHANGE COLUMN note_title note_title VARCHAR(255) NOT NULL COLLATE "utf8mb4_unicode_520_ci" AFTER note_created, + CHANGE COLUMN note_body note_body TEXT NOT NULL COLLATE "utf8mb4_unicode_520_ci" AFTER note_title; + '); + + $conn->execute(' + ALTER TABLE msz_users_password_resets + CHANGE COLUMN verification_code verification_code CHAR(12) NULL DEFAULT NULL COLLATE "ascii_general_ci" AFTER reset_requested; + '); + + $conn->execute(' + ALTER TABLE msz_users_warnings + CHANGE COLUMN warn_body warn_body TEXT NOT NULL COLLATE "utf8mb4_unicode_520_ci" AFTER mod_id; + '); + } +} diff --git a/public-legacy/search.php b/public-legacy/search.php index a34d51b..2c44f1c 100644 --- a/public-legacy/search.php +++ b/public-legacy/search.php @@ -81,7 +81,7 @@ if(!empty($searchQuery)) { ON r.`role_id` = u.`display_role` LEFT JOIN `msz_users_roles` AS ur ON ur.`user_id` = u.`user_id` - WHERE LOWER(u.`username`) LIKE CONCAT("%%", LOWER(:query), "%%") + WHERE u.`username` LIKE CONCAT("%%", :query, "%%") GROUP BY u.`user_id` '); $findUsers->bind('query', $searchQuery); diff --git a/src/Http/Handlers/HomeHandler.php b/src/Http/Handlers/HomeHandler.php index 7f3e3e1..0ebe912 100644 --- a/src/Http/Handlers/HomeHandler.php +++ b/src/Http/Handlers/HomeHandler.php @@ -57,7 +57,7 @@ final class HomeHandler extends Handler { orderBy: 'random', ); - // can also chat here, whoa + // can also cheat here, whoa $stats['users:online:recent'] = count($onlineUserInfos); // TODO: don't hardcode forum ids diff --git a/src/Users/User.php b/src/Users/User.php index 8a4bca1..dbc1fd0 100644 --- a/src/Users/User.php +++ b/src/Users/User.php @@ -36,12 +36,8 @@ class User { if(!preg_match('#^' . self::NAME_REGEX . '$#u', $name)) return 'invalid'; - $userId = (int)DB::prepare( - 'SELECT `user_id`' - . ' FROM `msz_users`' - . ' WHERE LOWER(`username`) = LOWER(:username)' - ) ->bind('username', $name) - ->fetchColumn(); + $userId = (int)DB::prepare('SELECT user_id FROM msz_users WHERE username = :username') + ->bind('username', $name)->fetchColumn(); if($userId > 0) return 'in-use'; @@ -75,12 +71,8 @@ class User { if(!checkdnsrr(mb_substr(mb_strstr($address, '@'), 1), 'MX')) return 'dns'; - $userId = (int)DB::prepare( - 'SELECT `user_id`' - . ' FROM `msz_users`' - . ' WHERE LOWER(`email`) = LOWER(:email)' - ) ->bind('email', $address) - ->fetchColumn(); + $userId = (int)DB::prepare('SELECT user_id FROM msz_users WHERE email = :email') + ->bind('email', $address)->fetchColumn(); if($userId > 0) return 'in-use'; diff --git a/src/Users/Users.php b/src/Users/Users.php index 9b4850a..7e9e091 100644 --- a/src/Users/Users.php +++ b/src/Users/Users.php @@ -214,10 +214,10 @@ class Users { ++$args; $query .= ' WHERE user_id = ?'; } - if($selectName) // change the collation for both name and email to a case insensitive one - $query .= sprintf(' %s LOWER(username) = LOWER(?)', ++$args > 1 ? 'OR' : 'WHERE'); + if($selectName) + $query .= sprintf(' %s username = ?', ++$args > 1 ? 'OR' : 'WHERE'); if($selectMail) - $query .= sprintf(' %s LOWER(email) = LOWER(?)', ++$args > 1 ? 'OR' : 'WHERE'); + $query .= sprintf(' %s email = ?', ++$args > 1 ? 'OR' : 'WHERE'); $args = 0; $stmt = $this->cache->get($query); diff --git a/templates/user/macros.twig b/templates/user/macros.twig index 9e91bd9..5f4e729 100644 --- a/templates/user/macros.twig +++ b/templates/user/macros.twig @@ -1,31 +1,52 @@ {% macro user_card(user) %} {% from 'macros.twig' import avatar %} + {% if user.info is defined %} + {% set colour = user.colour %} + {% set info = user.info %} + {% set ftopics = user.ftopics %} + {% set fposts = user.fposts %} + {% else %} + {% set colour = user.user_colour %} + {% set info = { + 'id': user.user_id, + 'name': user.username, + 'hasTitle': user.user_title is defined and user.user_title is not empty, + 'title': user.user_title|default(''), + 'hasCountryCode': user.user_country is defined and user.user_country != 'XX', + 'countryCode': user.user_country|default('XX'), + 'hasLastActive': user.user_active is defined and user.user_active > 0, + 'lastActiveTime': user.user_active, + 'createdTime': user.user_created + } %} + {% set ftopics = user.user_count_topics %} + {% set fposts = user.user_count_posts %} + {% endif %} -
+
- +
- {{ avatar(user.info.id, 50, user.info.name) }} + {{ avatar(info.id, 50, info.name) }}
- {{ user.info.name }} + {{ info.name }}
- {% if user.info.hasTitle %} + {% if info.hasTitle %}
- {{ user.info.title }} + {{ info.title }}
{% endif %} - {% if user.info.hasCountryCode %} + {% if info.hasCountryCode %}
-
+
- {{ user.info.countryCode|country_name }} + {{ info.countryCode|country_name }}
{% endif %} @@ -33,30 +54,30 @@
- {% if user.ftopics > 0 %} - + {% if ftopics > 0 %} +
Topics
-
{{ user.ftopics|number_format }}
+
{{ ftopics|number_format }}
{% endif %} - {% if user.fposts > 0 %} - + {% if fposts > 0 %} +
Posts
-
{{ user.fposts|number_format }}
+
{{ fposts|number_format }}
{% endif %} - {% if user.info.hasLastActive %} -
+ {% if info.hasLastActive %} +
Last seen
-
+
{% endif %} -
+
Joined
-
+