Files
breadcrumb-the-shire/pages/admin/users/avatar-file().php

47 lines
1.5 KiB
PHP
Raw Normal View History

2026-02-04 23:31:53 +01:00
<?php
use MintyPHP\Http\SessionStoreInterface;
use MintyPHP\Service\Access\UserAuthorizationPolicy;
2026-02-04 23:31:53 +01:00
use MintyPHP\Support\Guard;
$session = app(SessionStoreInterface::class)->all();
2026-02-11 19:28:12 +01:00
define('MINTY_ALLOW_OUTPUT', true);
2026-02-04 23:31:53 +01:00
Guard::requireLogin();
2026-03-04 15:56:58 +01:00
$authorizationService = app(\MintyPHP\Service\Access\AuthorizationService::class);
$userAccountService = app(\MintyPHP\Service\User\UserAccountService::class);
$userAvatarService = app(\MintyPHP\Service\User\UserAvatarService::class);
2026-02-04 23:31:53 +01:00
2026-03-04 15:56:58 +01:00
$uuid = trim((string) (requestInput()->queryAll()['uuid'] ?? ''));
$size = isset(requestInput()->queryAll()['size']) ? (int) requestInput()->queryAll()['size'] : null;
2026-02-23 12:58:19 +01:00
if (!$userAvatarService->isValidUuid($uuid)) {
2026-02-04 23:31:53 +01:00
http_response_code(404);
return;
}
$currentUserId = (int) ($session['user']['id'] ?? 0);
2026-02-23 12:58:19 +01:00
$user = $uuid !== '' ? $userAccountService->findByUuid($uuid) : null;
2026-02-04 23:31:53 +01:00
$userId = (int) ($user['id'] ?? 0);
$decision = $authorizationService->authorize(UserAuthorizationPolicy::ABILITY_ADMIN_USERS_AVATAR_VIEW, [
'actor_user_id' => $currentUserId,
'target_user_id' => $userId,
]);
if (!$decision->isAllowed()) {
2026-02-04 23:31:53 +01:00
http_response_code(403);
return;
}
2026-02-23 12:58:19 +01:00
$path = $userAvatarService->findAvatarPath($uuid, $size);
2026-02-04 23:31:53 +01:00
if (!$path || !is_file($path)) {
http_response_code(404);
return;
}
2026-02-23 12:58:19 +01:00
$mime = $userAvatarService->detectMime($path);
2026-02-04 23:31:53 +01:00
header('Content-Type: ' . $mime);
header('X-Content-Type-Options: nosniff');
header('Content-Security-Policy: sandbox');
header('Cache-Control: private, max-age=300');
header('Content-Length: ' . filesize($path));
readfile($path);