misuzu/public-legacy/profile.php

399 lines
19 KiB
PHP
Raw Normal View History

<?php
namespace Misuzu;
use stdClass;
2020-06-07 20:37:03 +00:00
use InvalidArgumentException;
use RuntimeException;
use Index\ByteFormat;
use Misuzu\Parsers\TextFormat;
use Misuzu\Profile\ProfileBackgroundAttach;
2025-02-08 02:43:54 +00:00
use Misuzu\Users\{User,UsersContext};
use Misuzu\Users\Assets\UserAvatarAsset;
2020-06-07 20:37:03 +00:00
use Misuzu\Users\Assets\UserBackgroundAsset;
2019-12-03 02:06:36 +01:00
2024-12-02 02:28:08 +00:00
if(!isset($msz) || !($msz instanceof \Misuzu\MisuzuContext))
die('Script must be called through the Misuzu route dispatcher.');
$userId = !empty($_GET['u']) && is_string($_GET['u']) ? trim($_GET['u']) : 0;
2019-03-18 23:02:30 +01:00
$profileMode = !empty($_GET['m']) && is_string($_GET['m']) ? (string)$_GET['m'] : '';
2024-12-02 02:28:08 +00:00
$isEditing = !empty($_GET['edit']) && is_string($_GET['edit']) ? (bool)$_GET['edit'] : !empty($_POST);
2019-03-18 23:02:30 +01:00
$viewerInfo = $msz->authInfo->userInfo;
$viewingAsGuest = $viewerInfo === null;
2024-11-30 04:20:20 +00:00
$viewerId = $viewingAsGuest ? '0' : $viewerInfo->id;
2020-05-18 21:27:34 +00:00
try {
$userInfo = $msz->usersCtx->getUserInfo($userId, 'profile');
} catch(RuntimeException $ex) {
http_response_code(404);
Template::render('profile.index', [
'profile_is_guest' => $viewingAsGuest,
'profile_is_deleted' => true,
'profile_is_banned' => false,
]);
return;
}
2019-02-27 15:05:27 +01:00
if($userInfo->deleted) {
2022-04-27 23:24:38 +00:00
http_response_code(404);
Template::render('profile.index', [
'profile_is_guest' => $viewingAsGuest,
'profile_is_deleted' => true,
'profile_is_banned' => false,
]);
2022-04-27 23:24:38 +00:00
return;
}
2023-08-28 01:17:34 +00:00
switch($profileMode) {
default:
Template::throwError(404);
2023-08-28 01:17:34 +00:00
case 'forum-topics':
Tools::redirect($msz->urls->format('search-query', ['query' => sprintf('type:forum:topic author:%s', $userInfo->name), 'section' => 'topics']));
2023-08-28 01:17:34 +00:00
return;
case 'forum-posts':
Tools::redirect($msz->urls->format('search-query', ['query' => sprintf('type:forum:post author:%s', $userInfo->name), 'section' => 'posts']));
2023-08-28 01:17:34 +00:00
return;
case '':
break;
}
$notices = [];
$userRank = $msz->usersCtx->getUserRank($userInfo);
$viewerRank = $msz->usersCtx->getUserRank($viewerInfo);
$viewerPermsGlobal = $msz->authInfo->getPerms('global');
$viewerPermsUser = $msz->authInfo->getPerms('user');
2023-08-30 22:37:21 +00:00
$activeBanInfo = $msz->usersCtx->tryGetActiveBan($userInfo);
$isBanned = $activeBanInfo !== null;
2024-11-30 04:20:20 +00:00
$viewingOwnProfile = (string)$viewerId === $userInfo->id;
2024-01-30 23:47:02 +00:00
$canManageWarnings = $viewerPermsUser->check(Perm::U_WARNINGS_MANAGE);
$canEdit = !$viewingAsGuest && ((!$isBanned && $viewingOwnProfile) || $viewerInfo->super || (
2024-01-30 23:47:02 +00:00
$viewerPermsUser->check(Perm::U_USERS_MANAGE) && ($viewingOwnProfile || $viewerRank > $userRank)
));
$avatarAsset = new UserAvatarAsset($userInfo);
$backgroundInfo = $msz->profileCtx->backgrounds->getProfileBackground($userInfo);
$backgroundAsset = new UserBackgroundAsset($userInfo, $backgroundInfo);
2019-06-10 19:04:53 +02:00
if($isEditing) {
if(!$canEdit)
Template::throwError(403);
2024-01-30 23:47:02 +00:00
$perms = $viewerPermsUser->checkMany([
2023-08-30 22:37:21 +00:00
'edit_profile' => Perm::U_PROFILE_EDIT,
'edit_avatar' => Perm::U_AVATAR_CHANGE,
2024-12-02 02:28:08 +00:00
'edit_background' => Perm::U_PROFILE_BACKGROUND_CHANGE,
2023-08-30 22:37:21 +00:00
'edit_about' => Perm::U_PROFILE_ABOUT_EDIT,
'edit_birthdate' => Perm::U_PROFILE_BIRTHDATE_EDIT,
'edit_signature' => Perm::U_FORUM_SIGNATURE_EDIT,
2019-04-30 02:55:10 +02:00
]);
Template::set([
'perms' => $perms,
2025-02-08 02:43:54 +00:00
'birthdate_info' => $msz->usersCtx->birthdates->getUserBirthdate($userInfo),
]);
2024-12-02 02:28:08 +00:00
if(!empty($_POST)) {
2019-12-11 19:10:54 +01:00
if(!CSRF::validateRequest()) {
2020-06-07 20:37:03 +00:00
$notices[] = 'Couldn\'t verify you, please refresh the page and retry.';
2019-03-18 23:20:16 +01:00
} else {
2023-07-20 19:36:43 +00:00
$profileFieldsSubmit = filter_input(INPUT_POST, 'profile', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
if(!empty($profileFieldsSubmit)) {
2023-08-30 22:37:21 +00:00
if(!$perms->edit_profile) {
2020-06-07 20:37:03 +00:00
$notices[] = 'You\'re not allowed to edit your profile';
2019-03-18 23:20:16 +01:00
} else {
$profileFieldInfos = iterator_to_array($msz->profileCtx->fields->getFields());
2023-07-20 19:36:43 +00:00
$profileFieldsSetInfos = [];
$profileFieldsSetValues = [];
$profileFieldsRemove = [];
foreach($profileFieldInfos as $fieldInfo) {
$fieldName = $fieldInfo->name;
2023-07-20 19:36:43 +00:00
$fieldValue = empty($profileFieldsSubmit[$fieldName]) ? '' : (string)filter_var($profileFieldsSubmit[$fieldName]);
if(empty($profileFieldsSubmit[$fieldName])) {
$profileFieldsRemove[] = $fieldInfo;
continue;
2019-03-18 23:20:16 +01:00
}
2023-07-20 19:36:43 +00:00
if($fieldInfo->checkValue($fieldValue)) {
$profileFieldsSetInfos[] = $fieldInfo;
$profileFieldsSetValues[] = $fieldValue;
} else
$notices[] = sprintf('%s isn\'t properly formatted.', $fieldInfo->title);
unset($fieldName, $fieldValue, $fieldInfo);
}
2023-07-20 19:36:43 +00:00
if(!empty($profileFieldsRemove))
$msz->profileCtx->fields->removeFieldValues($userInfo, $profileFieldsRemove);
2023-07-20 19:36:43 +00:00
if(!empty($profileFieldsSetInfos))
$msz->profileCtx->fields->setFieldValues($userInfo, $profileFieldsSetInfos, $profileFieldsSetValues);
}
}
2019-02-27 15:05:27 +01:00
2019-06-10 19:04:53 +02:00
if(!empty($_POST['about']) && is_array($_POST['about'])) {
2023-08-30 22:37:21 +00:00
if(!$perms->edit_about) {
2020-06-07 20:37:03 +00:00
$notices[] = 'You\'re not allowed to edit your about page.';
2019-03-18 23:20:16 +01:00
} else {
2020-06-07 20:37:03 +00:00
$aboutText = (string)($_POST['about']['text'] ?? '');
$aboutParse = TextFormat::tryFrom((string)($_POST['about']['parser'] ?? '')) ?? TextFormat::Plain;
$aboutValid = $msz->usersCtx->users->validateProfileAbout($aboutParse, $aboutText);
2020-06-07 20:37:03 +00:00
if($aboutValid === '')
$msz->usersCtx->users->updateUser($userInfo, aboutBody: $aboutText, aboutBodyFormat: $aboutParse);
else
$notices[] = $msz->usersCtx->users->validateProfileAboutText($aboutValid);
}
}
2019-02-27 15:05:27 +01:00
2019-06-10 19:04:53 +02:00
if(!empty($_POST['signature']) && is_array($_POST['signature'])) {
2023-08-30 22:37:21 +00:00
if(!$perms->edit_signature) {
2020-06-07 20:37:03 +00:00
$notices[] = 'You\'re not allowed to edit your forum signature.';
2019-03-18 23:20:16 +01:00
} else {
2020-06-07 20:37:03 +00:00
$sigText = (string)($_POST['signature']['text'] ?? '');
2025-02-08 18:00:07 +00:00
$sigParse = TextFormat::tryFrom((string)($_POST['signature']['parser'] ?? '')) ?? TextFormat::Plain;
$sigValid = $msz->usersCtx->users->validateForumSignature($sigParse, $sigText);
2020-06-07 20:37:03 +00:00
if($sigValid === '')
$msz->usersCtx->users->updateUser($userInfo, signatureBody: $sigText, signatureBodyFormat: $sigParse);
else
$notices[] = $msz->usersCtx->users->validateForumSignatureText($sigValid);
}
}
2018-03-24 05:31:42 +01:00
2019-06-10 19:04:53 +02:00
if(!empty($_POST['birthdate']) && is_array($_POST['birthdate'])) {
2023-08-30 22:37:21 +00:00
if(!$perms->edit_birthdate) {
2019-03-18 23:20:16 +01:00
$notices[] = "You aren't allow to change your birthdate.";
} else {
2020-06-07 20:37:03 +00:00
$birthYear = (int)($_POST['birthdate']['year'] ?? 0);
$birthMonth = (int)($_POST['birthdate']['month'] ?? 0);
$birthDay = (int)($_POST['birthdate']['day'] ?? 0);
2025-02-08 02:43:54 +00:00
$birthValid = UsersContext::validateBirthdate($birthYear, $birthMonth, $birthDay);
if($birthValid === '') {
if($birthMonth === 0 && $birthDay === 0)
$msz->usersCtx->birthdates->deleteUserBirthdate($userInfo);
else
$msz->usersCtx->birthdates->updateUserBirthdate($userInfo, $birthYear === 0 ? null : $birthYear, $birthMonth, $birthDay);
} else
$notices[] = $msz->usersCtx->users->validateBirthdateText($birthValid);
}
}
2019-06-10 19:04:53 +02:00
if(!empty($_FILES['avatar'])) {
if(!empty($_POST['avatar']['delete'])) {
$avatarAsset->delete();
2019-03-18 23:20:16 +01:00
} else {
2023-08-30 22:37:21 +00:00
if(!$perms->edit_avatar) {
2020-06-07 20:37:03 +00:00
$notices[] = 'You aren\'t allow to change your avatar.';
2019-06-10 19:04:53 +02:00
} elseif(!empty($_FILES['avatar'])
2019-03-18 23:20:16 +01:00
&& is_array($_FILES['avatar'])
&& !empty($_FILES['avatar']['name']['file'])) {
2019-06-10 19:04:53 +02:00
if($_FILES['avatar']['error']['file'] !== UPLOAD_ERR_OK) {
2020-06-07 20:37:03 +00:00
switch($_FILES['avatar']['error']['file']) {
case UPLOAD_ERR_NO_FILE:
$notices[] = 'Select a file before hitting upload!';
break;
case UPLOAD_ERR_PARTIAL:
$notices[] = 'The upload was interrupted, please try again!';
break;
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
$notices[] = sprintf('Your avatar is not allowed to be larger in file size than %s!', ByteFormat::format($avatarAsset->getMaxBytes()));
2020-06-07 20:37:03 +00:00
break;
default:
$notices[] = 'Unable to save your avatar, contact an administator!';
break;
}
2019-03-18 23:20:16 +01:00
} else {
2020-06-07 20:37:03 +00:00
try {
$avatarAsset->setFromPath($_FILES['avatar']['tmp_name']['file']);
} catch(InvalidArgumentException $ex) {
$exMessage = $ex->getMessage();
$notices[] = match($exMessage) {
'$path is not a valid image.' => 'The file you uploaded was not an image!',
'$path is not an allowed image file.' => 'This type of image is not supported, keep to PNG, JPG or GIF!',
'Dimensions of $path are too large.' => sprintf('Your avatar can\'t be larger than %dx%d!', $avatarAsset->getMaxWidth(), $avatarAsset->getMaxHeight()),
'File size of $path is too large.' => sprintf('Your avatar is not allowed to be larger in file size than %s!', ByteFormat::format($avatarAsset->getMaxBytes())),
default => $exMessage,
};
} catch(RuntimeException $ex) {
2020-06-07 20:37:03 +00:00
$notices[] = 'Unable to save your avatar, contact an administator!';
2019-03-18 23:20:16 +01:00
}
}
}
}
}
2019-01-18 23:24:37 +01:00
if(filter_has_var(INPUT_POST, 'bg_attach')) {
$bgFormat = ProfileBackgroundAttach::tryFrom((string)filter_input(INPUT_POST, 'bg_attach'));
if($bgFormat === null) {
$backgroundAsset->delete();
$msz->profileCtx->backgrounds->deleteProfileBackground($userInfo);
$backgroundAsset = null;
2019-03-18 23:20:16 +01:00
} else {
2023-08-30 22:37:21 +00:00
if(!$perms->edit_background) {
2020-06-07 20:37:03 +00:00
$notices[] = 'You aren\'t allow to change your background.';
} elseif(!empty($_FILES['bg_file']) && is_array($_FILES['bg_file'])) {
if(!empty($_FILES['bg_file']['name'])) {
if($_FILES['bg_file']['error'] !== UPLOAD_ERR_OK) {
switch($_FILES['bg_file']['error']) {
2020-06-07 20:37:03 +00:00
case UPLOAD_ERR_NO_FILE:
$notices[] = 'Select a file before hitting upload!';
break;
case UPLOAD_ERR_PARTIAL:
$notices[] = 'The upload was interrupted, please try again!';
break;
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
2024-12-02 02:28:08 +00:00
$notices[] = sprintf('Your background is not allowed to be larger in file size than %s!', ByteFormat::format(isset($backgroundProps) && is_array($backgroundProps) ? $backgroundProps['max_size'] : 0));
2020-06-07 20:37:03 +00:00
break;
default:
$notices[] = 'Unable to save your background, contact an administator!';
break;
}
2019-03-18 23:20:16 +01:00
} else {
2020-06-07 20:37:03 +00:00
try {
$backgroundAsset->setFromPath($_FILES['bg_file']['tmp_name']);
} catch(InvalidArgumentException $ex) {
$exMessage = $ex->getMessage();
$notices[] = match($exMessage) {
'$path is not a valid image.' => 'The file you uploaded was not an image!',
'$path is not an allowed image file.' => 'This type of image is not supported, keep to PNG, JPG or GIF!',
'Dimensions of $path are too large.' => sprintf('Your background can\'t be larger than %dx%d!', $backgroundAsset->getMaxWidth(), $backgroundAsset->getMaxHeight()),
'File size of $path is too large.' => sprintf('Your background is not allowed to be larger in file size than %s!', ByteFormat::format($backgroundAsset->getMaxBytes())),
default => $exMessage,
};
} catch(RuntimeException $ex) {
2020-06-07 20:37:03 +00:00
$notices[] = 'Unable to save your background, contact an administator!';
2019-03-18 23:20:16 +01:00
}
2020-06-07 20:37:03 +00:00
}
2019-03-18 23:20:16 +01:00
}
2020-06-07 20:55:43 +00:00
$backgroundInfo = $msz->profileCtx->backgrounds->updateProfileBackground(
$userInfo,
$bgFormat,
filter_has_var(INPUT_POST, 'bg_blend'),
filter_has_var(INPUT_POST, 'bg_slide')
);
2019-03-18 23:20:16 +01:00
}
}
2020-06-07 20:37:03 +00:00
$backgroundAsset = new UserBackgroundAsset($userInfo, $backgroundInfo);
}
2018-09-19 00:16:29 +02:00
}
2019-03-18 23:20:16 +01:00
// Unset $isEditing and hope the user doesn't refresh their profile!
2020-06-07 20:37:03 +00:00
if(empty($notices))
2019-03-18 23:20:16 +01:00
$isEditing = false;
}
}
2018-10-27 20:50:34 +02:00
// TODO: create user counters so these can be statically kept
$profileStats = new stdClass;
$profileStats->forum_topic_count = $msz->forumCtx->countTotalUserTopics($userInfo);
$profileStats->forum_post_count = $msz->forumCtx->countTotalUserPosts($userInfo);
$profileStats->comments_count = $msz->comments->countPosts(userInfo: $userInfo, deleted: false);
2018-10-27 20:50:34 +02:00
2023-08-28 01:17:34 +00:00
if(!$viewingAsGuest) {
Template::set('profile_warnings', iterator_to_array($msz->usersCtx->warnings->getWarningsWithDefaultBacklog($userInfo)));
2019-03-10 22:18:33 +01:00
2023-08-28 01:17:34 +00:00
if((!$isBanned || $canEdit)) {
2024-12-02 02:28:08 +00:00
$unranked = $msz->config->getValues([
2023-08-28 01:17:34 +00:00
'forum_leader.unranked.forum:a',
'forum_leader.unranked.topic:a',
2019-03-10 22:18:33 +01:00
]);
$activeCategoryStats = $msz->forumCtx->categories->getMostActiveCategoryInfo(
2023-08-28 01:17:34 +00:00
$userInfo,
$unranked['forum_leader.unranked.forum'],
$unranked['forum_leader.unranked.topic'],
deleted: false
);
$activeCategoryInfo = $activeCategoryStats->success ? $msz->forumCtx->categories->getCategory(categoryId: $activeCategoryStats->categoryId) : null;
2019-05-09 18:50:18 +02:00
$activeTopicStats = $msz->forumCtx->topics->getMostActiveTopicInfo(
2023-08-28 01:17:34 +00:00
$userInfo,
$unranked['forum_leader.unranked.forum'],
$unranked['forum_leader.unranked.topic'],
deleted: false
);
$activeTopicInfo = $activeTopicStats->success ? $msz->forumCtx->topics->getTopic(topicId: $activeTopicStats->topicId) : null;
2023-07-20 19:36:43 +00:00
$profileFieldValues = iterator_to_array($msz->profileCtx->fields->getFieldValues($userInfo));
$profileFieldInfos = $profileFieldInfos ?? iterator_to_array($msz->profileCtx->fields->getFields(fieldValueInfos: $isEditing ? null : $profileFieldValues));
$profileFieldFormats = iterator_to_array($msz->profileCtx->fields->getFieldFormats(fieldValueInfos: $profileFieldValues));
2023-07-20 19:36:43 +00:00
2023-08-28 01:17:34 +00:00
$profileFieldRawValues = [];
$profileFieldLinkValues = [];
$profileFieldDisplayValues = [];
2023-08-28 01:17:34 +00:00
// using field infos as the basis for now, uses the correct ordering
foreach($profileFieldInfos as $fieldInfo) {
unset($fieldValue);
2023-07-20 19:36:43 +00:00
2023-08-28 01:17:34 +00:00
foreach($profileFieldValues as $fieldValueTest)
if($fieldValueTest->fieldId === $fieldInfo->id) {
2023-08-28 01:17:34 +00:00
$fieldValue = $fieldValueTest;
break;
}
2023-07-26 22:43:50 +00:00
$fieldName = $fieldInfo->name;
2023-07-26 22:43:50 +00:00
2023-08-28 01:17:34 +00:00
if(isset($fieldValue)) {
foreach($profileFieldFormats as $fieldFormatTest)
if($fieldFormatTest->id === $fieldValue->formatId) {
2023-08-28 01:17:34 +00:00
$fieldFormat = $fieldFormatTest;
break;
2023-07-26 22:43:50 +00:00
}
2023-07-20 19:36:43 +00:00
2024-12-02 02:28:08 +00:00
if(!isset($fieldFormat))
continue;
$profileFieldRawValues[$fieldName] = $fieldValue->value;
$profileFieldDisplayValues[$fieldName] = $fieldFormat->formatDisplay($fieldValue->value);
if($fieldFormat->linkFormat !== null)
$profileFieldLinkValues[$fieldName] = $fieldFormat->formatLink($fieldValue->value);
2023-07-26 22:43:50 +00:00
}
2023-07-20 19:36:43 +00:00
}
2023-08-28 01:17:34 +00:00
Template::set([
'profile_active_category_stats' => $activeCategoryStats,
'profile_active_category_info' => $activeCategoryInfo,
'profile_active_topic_stats' => $activeTopicStats,
'profile_active_topic_info' => $activeTopicInfo,
'profile_fields_infos' => $profileFieldInfos,
'profile_fields_raw_values' => $profileFieldRawValues,
'profile_fields_display_values' => $profileFieldDisplayValues,
'profile_fields_link_values' => $profileFieldLinkValues,
]);
}
}
2023-08-28 01:17:34 +00:00
Template::render('profile.index', [
'profile_viewer' => $viewerInfo,
'profile_user' => $userInfo,
'profile_colour' => $msz->usersCtx->getUserColour($userInfo),
2023-08-28 01:17:34 +00:00
'profile_stats' => $profileStats,
'profile_mode' => $profileMode,
'profile_notices' => $notices,
'profile_can_edit' => $canEdit,
'profile_is_editing' => $isEditing,
'profile_is_banned' => $isBanned,
'profile_is_guest' => $viewingAsGuest,
'profile_is_deleted' => false,
'profile_ban_info' => $activeBanInfo,
'profile_avatar_asset' => $avatarAsset,
2023-08-28 01:17:34 +00:00
'profile_background_info' => $backgroundInfo,
'profile_background_asset' => $backgroundAsset,
2024-01-30 23:47:02 +00:00
'profile_can_send_messages' => $viewerPermsGlobal->check(Perm::G_MESSAGES_SEND),
2025-02-08 02:43:54 +00:00
'profile_age' => $msz->usersCtx->birthdates->getUserAge($userInfo),
2023-08-28 01:17:34 +00:00
]);