Fixed error 500 when trying to view a non-existent profile.

This commit is contained in:
flash 2023-07-25 15:03:25 +00:00
parent bd683d8404
commit 81f4dfce19
3 changed files with 16 additions and 8 deletions

View file

@ -14,26 +14,33 @@ $userId = !empty($_GET['u']) && is_string($_GET['u']) ? trim($_GET['u']) : 0;
$profileMode = !empty($_GET['m']) && is_string($_GET['m']) ? (string)$_GET['m'] : ''; $profileMode = !empty($_GET['m']) && is_string($_GET['m']) ? (string)$_GET['m'] : '';
$isEditing = !empty($_GET['edit']) && is_string($_GET['edit']) ? (bool)$_GET['edit'] : !empty($_POST) && is_array($_POST); $isEditing = !empty($_GET['edit']) && is_string($_GET['edit']) ? (bool)$_GET['edit'] : !empty($_POST) && is_array($_POST);
$currentUser = User::getCurrent();
$viewingAsGuest = $currentUser === null;
$currentUserId = $viewingAsGuest ? 0 : $currentUser->getId();
try { try {
$profileUser = User::findForProfile($userId); $profileUser = User::findForProfile($userId);
} catch(RuntimeException $ex) { } catch(RuntimeException $ex) {
http_response_code(404); http_response_code(404);
Template::render('profile.index'); Template::render('profile.index', [
'profile_is_guest' => $viewingAsGuest,
'profile_is_deleted' => true,
]);
return; return;
} }
if($profileUser->isDeleted()) { if($profileUser->isDeleted()) {
http_response_code(404); http_response_code(404);
Template::render('profile.index'); Template::render('profile.index', [
'profile_is_guest' => $viewingAsGuest,
'profile_is_deleted' => true,
]);
return; return;
} }
$notices = []; $notices = [];
$profileFields = new ProfileFields($db); $profileFields = new ProfileFields($db);
$currentUser = User::getCurrent();
$viewingAsGuest = $currentUser === null;
$currentUserId = $viewingAsGuest ? 0 : $currentUser->getId();
$viewingOwnProfile = $currentUserId === $profileUser->getId(); $viewingOwnProfile = $currentUserId === $profileUser->getId();
$isBanned = $profileUser->hasActiveWarning(); $isBanned = $profileUser->hasActiveWarning();
$userPerms = perms_get_user($currentUserId)[MSZ_PERMS_USER]; $userPerms = perms_get_user($currentUserId)[MSZ_PERMS_USER];
@ -443,5 +450,6 @@ if(!empty($template)) {
'profile_is_editing' => $isEditing, 'profile_is_editing' => $isEditing,
'profile_is_banned' => $isBanned, 'profile_is_banned' => $isBanned,
'profile_is_guest' => $viewingAsGuest, 'profile_is_guest' => $viewingAsGuest,
'profile_is_deleted' => false,
]); ]);
} }

View file

@ -1,7 +1,7 @@
{% from 'macros.twig' import avatar %} {% from 'macros.twig' import avatar %}
{% from '_layout/input.twig' import input_checkbox_raw %} {% from '_layout/input.twig' import input_checkbox_raw %}
<div class="container profile__header" style="--accent-colour: {{ profile_user.colour }}"> <div class="container profile__header"{% if profile_user is defined %} style="--accent-colour: {{ profile_user.colour }}"{% endif %}>
<div class="profile__header__background"></div> <div class="profile__header__background"></div>
<div class="profile__header__details"> <div class="profile__header__details">

View file

@ -77,7 +77,7 @@
{% endif %} {% endif %}
<div class="profile__content"> <div class="profile__content">
{% set show_profile_fields = not profile_is_guest and (profile_is_editing ? perms.edit_profile : profile_fields_display_values is not empty) %} {% set show_profile_fields = not profile_is_guest and (profile_is_editing ? perms.edit_profile : profile_fields_display_values|default([]) is not empty) %}
{% set show_background_settings = profile_is_editing and perms.edit_background %} {% set show_background_settings = profile_is_editing and perms.edit_background %}
{% set show_birthdate = profile_is_editing and perms.edit_birthdate %} {% set show_birthdate = profile_is_editing and perms.edit_birthdate %}
{% set show_active_forum_info = not profile_is_editing and (profile_active_category_info.forum_id|default(0) > 0 or profile_active_topic_info.topic_id|default(0) > 0) %} {% set show_active_forum_info = not profile_is_editing and (profile_active_category_info.forum_id|default(0) > 0 or profile_active_topic_info.topic_id|default(0) > 0) %}
@ -102,7 +102,7 @@
</div> </div>
</div> </div>
{% endif %} {% endif %}
{% if current_user is not defined %} {% if current_user is not defined and not profile_is_deleted %}
<div class="container profile__container"> <div class="container profile__container">
<div class="profile__accounts__notice"> <div class="profile__accounts__notice">
You must <a href="{{ url('auth-login') }}" class="profile__accounts__link">log in</a> to view full profiles! You must <a href="{{ url('auth-login') }}" class="profile__accounts__link">log in</a> to view full profiles!