diff --git a/assets/less/main.less b/assets/less/main.less index 80d5fde8..ffa80280 100644 --- a/assets/less/main.less +++ b/assets/less/main.less @@ -67,6 +67,26 @@ body { &--legacy { --background-color: #fbeeff; } + + &--bg-blend { + background-blend-mode: multiply; + } + + /*&--bg-slide { + }*/ + + &--bg-cover { + background-repeat: no-repeat; + } + + &--bg-stretch { + background-size: 100% 100%; + background-repeat: no-repeat; + } + + &--bg-tile { + background-repeat: repeat; + } } // Input elements diff --git a/database/2018_10_01_081226_add_background_settings_field.php b/database/2018_10_01_081226_add_background_settings_field.php new file mode 100644 index 00000000..7436b0c3 --- /dev/null +++ b/database/2018_10_01_081226_add_background_settings_field.php @@ -0,0 +1,20 @@ +exec(" + ALTER TABLE `msz_users` + ADD COLUMN `user_background_settings` TINYINT(4) DEFAULT '0' AFTER `user_about_parser`; + "); +} + +function migrate_down(PDO $conn): void +{ + $conn->exec(' + ALTER TABLE `msz_users` + DROP COLUMN `user_background_settings`; + '); +} diff --git a/misuzu.php b/misuzu.php index 07e9745d..2199780d 100644 --- a/misuzu.php +++ b/misuzu.php @@ -260,6 +260,7 @@ MIG; tpl_add_function('asset_url', true); tpl_add_function('vsprintf', true); tpl_add_function('perms_check', true); + tpl_add_function('bg_settings', true, 'user_background_settings_strings'); tpl_add_function('git_commit_hash'); tpl_add_function('git_branch'); @@ -287,7 +288,7 @@ MIG; $getUserDisplayInfo = Database::prepare(' SELECT - u.`user_id`, u.`username`, + u.`user_id`, u.`username`, u.`user_background_settings`, COALESCE(u.`user_colour`, r.`role_colour`) as `user_colour` FROM `msz_users` as u LEFT JOIN `msz_roles` as r diff --git a/public/settings.php b/public/settings.php index 1128a16d..c150e342 100644 --- a/public/settings.php +++ b/public/settings.php @@ -156,6 +156,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { switch ($_POST['background']['mode'] ?? '') { case 'delete': user_background_delete($settingsUserId); + user_background_set_settings($settingsUserId, MSZ_USER_BACKGROUND_ATTACHMENT_NONE); break; case 'upload': @@ -164,40 +165,55 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { break; } - if (empty($_FILES['background']) - || !is_array($_FILES['background']) - || empty($_FILES['background']['name']['file'])) { + if (empty($_POST['background']) + || !is_array($_POST['background'])) { break; } - if ($_FILES['background']['error']['file'] !== UPLOAD_ERR_OK) { - $settingsErrors[] = sprintf( - MSZ_TMP_USER_ERROR_STRINGS['avatar']['upload'][$_FILES['background']['error']['file']] - ?? MSZ_TMP_USER_ERROR_STRINGS['avatar']['upload']['_'], - $_FILES['background']['error']['file'], - byte_symbol($backgroundProps['max_size'], true), - $backgroundProps['max_width'], - $backgroundProps['max_height'] + if (!empty($_FILES['background']['name']['file'])) { + if ($_FILES['background']['error']['file'] !== UPLOAD_ERR_OK) { + $settingsErrors[] = sprintf( + MSZ_TMP_USER_ERROR_STRINGS['avatar']['upload'][$_FILES['background']['error']['file']] + ?? MSZ_TMP_USER_ERROR_STRINGS['avatar']['upload']['_'], + $_FILES['background']['error']['file'], + byte_symbol($backgroundProps['max_size'], true), + $backgroundProps['max_width'], + $backgroundProps['max_height'] + ); + break; + } + + $setBackground = user_background_set_from_path( + $settingsUserId, + $_FILES['background']['tmp_name']['file'], + $backgroundProps ); - break; + + if ($setBackground !== MSZ_USER_BACKGROUND_NO_ERRORS) { + $settingsErrors[] = sprintf( + MSZ_TMP_USER_ERROR_STRINGS['avatar']['set'][$setBackground] + ?? MSZ_TMP_USER_ERROR_STRINGS['avatar']['set']['_'], + $setBackground, + byte_symbol($backgroundProps['max_size'], true), + $backgroundProps['max_width'], + $backgroundProps['max_height'] + ); + } } - $setBackground = user_background_set_from_path( - $settingsUserId, - $_FILES['background']['tmp_name']['file'], - $backgroundProps - ); + $backgroundSettings = in_array($_POST['background']['attach'] ?? '', MSZ_USER_BACKGROUND_ATTACHMENTS_NAMES) + ? array_flip(MSZ_USER_BACKGROUND_ATTACHMENTS_NAMES)[$_POST['background']['attach']] + : MSZ_USER_BACKGROUND_ATTACHMENTS[0]; - if ($setBackground !== MSZ_USER_BACKGROUND_NO_ERRORS) { - $settingsErrors[] = sprintf( - MSZ_TMP_USER_ERROR_STRINGS['avatar']['set'][$setBackground] - ?? MSZ_TMP_USER_ERROR_STRINGS['avatar']['set']['_'], - $setBackground, - byte_symbol($backgroundProps['max_size'], true), - $backgroundProps['max_width'], - $backgroundProps['max_height'] - ); + if (!empty($_POST['background']['attr']['blend'])) { + $backgroundSettings |= MSZ_USER_BACKGROUND_ATTRIBUTE_BLEND; } + + if (!empty($_POST['background']['attr']['slide'])) { + $backgroundSettings |= MSZ_USER_BACKGROUND_ATTRIBUTE_SLIDE; + } + + user_background_set_settings($settingsUserId, $backgroundSettings); break; } } @@ -334,11 +350,17 @@ switch ($settingsMode) { $getAccountInfo = Database::prepare(sprintf( ' - SELECT %s, `email`, `user_about_content`, `user_about_parser` + SELECT + %1$s, `email`, `user_about_content`, `user_about_parser`, + `user_background_settings` & 0x0F as `user_background_attachment`, + (`user_background_settings` & %2$d) > 0 as `user_background_attr_blend`, + (`user_background_settings` & %3$d) > 0 as `user_background_attr_slide` FROM `msz_users` WHERE `user_id` = :user_id ', - pdo_prepare_array($profileFields, true, '`user_%s`') + pdo_prepare_array($profileFields, true, '`user_%s`'), + MSZ_USER_BACKGROUND_ATTRIBUTE_BLEND, + MSZ_USER_BACKGROUND_ATTRIBUTE_SLIDE )); $getAccountInfo->bindValue('user_id', $settingsUserId); $accountInfo = $getAccountInfo->execute() ? $getAccountInfo->fetch(PDO::FETCH_ASSOC) : []; @@ -354,6 +376,7 @@ switch ($settingsMode) { 'settings_profile_fields' => $profileFields, 'settings_disable_account_options' => $disableAccountOptions, 'account_info' => $accountInfo, + 'background_attachments' => MSZ_USER_BACKGROUND_ATTACHMENTS_NAMES, ]); break; diff --git a/src/Users/user.php b/src/Users/user.php index 8b0811a3..3a8cf15e 100644 --- a/src/Users/user.php +++ b/src/Users/user.php @@ -233,6 +233,77 @@ function user_avatar_set_from_data(int $userId, string $data, array $options = [ define('MSZ_USER_BACKGROUND_FORMAT', '%d.msz'); +// attachment and attributes are to be stored in the same byte +// left half is for attributes, right half is for attachments +// this makes for 16 possible attachments and 4 possible attributes +// since attachments are just an incrementing number and attrs are flags + +define('MSZ_USER_BACKGROUND_ATTACHMENT_NONE', 0); +define('MSZ_USER_BACKGROUND_ATTACHMENT_COVER', 1); +define('MSZ_USER_BACKGROUND_ATTACHMENT_STRETCH', 2); +define('MSZ_USER_BACKGROUND_ATTACHMENT_TILE', 3); + +define('MSZ_USER_BACKGROUND_ATTACHMENTS', [ + MSZ_USER_BACKGROUND_ATTACHMENT_NONE, + MSZ_USER_BACKGROUND_ATTACHMENT_COVER, + MSZ_USER_BACKGROUND_ATTACHMENT_STRETCH, + MSZ_USER_BACKGROUND_ATTACHMENT_TILE, +]); + +define('MSZ_USER_BACKGROUND_ATTACHMENTS_NAMES', [ + MSZ_USER_BACKGROUND_ATTACHMENT_COVER => 'cover', + MSZ_USER_BACKGROUND_ATTACHMENT_STRETCH => 'stretch', + MSZ_USER_BACKGROUND_ATTACHMENT_TILE => 'tile', +]); + +define('MSZ_USER_BACKGROUND_ATTRIBUTE_BLEND', 0x10); +define('MSZ_USER_BACKGROUND_ATTRIBUTE_SLIDE', 0x20); + +define('MSZ_USER_BACKGROUND_ATTRIBUTES', [ + MSZ_USER_BACKGROUND_ATTRIBUTE_BLEND, + MSZ_USER_BACKGROUND_ATTRIBUTE_SLIDE, +]); + +define('MSZ_USER_BACKGROUND_ATTRIBUTES_NAMES', [ + MSZ_USER_BACKGROUND_ATTRIBUTE_BLEND => 'blend', + MSZ_USER_BACKGROUND_ATTRIBUTE_SLIDE => 'slide', +]); + +function user_background_settings_strings(int $settings, string $format = '%s'): array +{ + $arr = []; + + $attachment = $settings & 0x0F; + + if (array_key_exists($attachment, MSZ_USER_BACKGROUND_ATTACHMENTS_NAMES)) { + $arr[] = sprintf($format, MSZ_USER_BACKGROUND_ATTACHMENTS_NAMES[$attachment]); + } + + foreach (MSZ_USER_BACKGROUND_ATTRIBUTES_NAMES as $flag => $name) { + if (($settings & $flag) > 0) { + $arr[] = sprintf($format, $name); + } + } + + return $arr; +} + +function user_background_set_settings(int $userId, int $settings): void +{ + if ($userId < 1) { + return; + } + + $setAttrs = Database::prepare(' + UPDATE `msz_users` + SET `user_background_settings` = :settings + WHERE `user_id` = :user + '); + $setAttrs->bindValue('settings', $settings & 0xFF); + $setAttrs->bindValue('user', $userId); + $setAttrs->execute(); +} + function user_background_delete(int $userId): void { $backgroundFileName = sprintf(MSZ_USER_BACKGROUND_FORMAT, $userId); diff --git a/templates/master.twig b/templates/master.twig index 0cac8f91..8644a238 100644 --- a/templates/master.twig +++ b/templates/master.twig @@ -14,7 +14,7 @@ {% endif %} -
+ {% include '_layout/header.twig' %}