diff --git a/assets/typescript/Comments.ts b/assets/typescript/Comments.ts index 1e486e28..db08dfe6 100644 --- a/assets/typescript/Comments.ts +++ b/assets/typescript/Comments.ts @@ -203,7 +203,7 @@ function commentConstruct(comment: CommentPostInfo, layer: number = 0): HTMLElem const commentAvatar: HTMLAnchorElement = commentContainer.appendChild(document.createElement('a')); commentAvatar.className = 'avatar comment__avatar'; commentAvatar.href = '/profile.php?u=' + comment.user_id; - commentAvatar.style.backgroundImage = `url('/profile.php?m=avatar&u=${comment.user_id}')`; + commentAvatar.style.backgroundImage = `url('/user-assets.php?m=avatar&u=${comment.user_id}')`; const commentContent: HTMLDivElement = commentContainer.appendChild(document.createElement('div')); commentContent.className = 'comment__content'; @@ -303,7 +303,7 @@ function commentConstruct(comment: CommentPostInfo, layer: number = 0): HTMLElem // reply container const replyAvatar: HTMLDivElement = replyContainer.appendChild(document.createElement('div')); replyAvatar.className = 'avatar comment__avatar'; - replyAvatar.style.backgroundImage = `url('/profile.php?m=avatar&u=${comment.user_id}')`; + replyAvatar.style.backgroundImage = `url('/user-assets.php?m=avatar&u=${comment.user_id}')`; const replyContent: HTMLDivElement = replyContainer.appendChild(document.createElement('div')); replyContent.className = 'comment__content'; diff --git a/assets/typescript/misuzu.ts b/assets/typescript/misuzu.ts index 0e041e2f..b4aaee7a 100644 --- a/assets/typescript/misuzu.ts +++ b/assets/typescript/misuzu.ts @@ -74,7 +74,7 @@ function loginFormUpdateAvatar(avatarElement: HTMLElement, usernameElement: HTML if (xhr.readyState !== 4) return; - avatarElement.style.backgroundImage = `url('/profile.php?m=avatar&u=${xhr.responseText}')`; + avatarElement.style.backgroundImage = `url('/user-assets.php?m=avatar&u=${xhr.responseText}')`; }); xhr.open('GET', `/auth.php?m=get_user&u=${encodeURI(usernameElement.value)}`); xhr.send(); @@ -162,7 +162,7 @@ function loginModal(): boolean { const authAvatar: HTMLDivElement = titleHeader.appendChild(document.createElement('div')); authAvatar.className = 'avatar auth__avatar'; - authAvatar.style.backgroundImage = "url('/profile.php?u=0&m=avatar')"; + authAvatar.style.backgroundImage = "url('/user-assets.php?u=0&m=avatar')"; const hiddenMode: HTMLInputElement = container.appendChild(document.createElement('input')); hiddenMode.type = 'hidden'; diff --git a/public/profile.php b/public/profile.php index 9000a0c4..9d0cacce 100644 --- a/public/profile.php +++ b/public/profile.php @@ -5,100 +5,6 @@ $misuzuBypassLockdown = $mode === 'avatar'; require_once '../misuzu.php'; switch ($mode) { - case 'avatar': - $userId = (int)($_GET['u'] ?? 0); - - if (user_warning_check_expiration($userId, MSZ_WARN_BAN) > 0 && !( - parse_url($_SERVER['HTTP_REFERER'], PHP_URL_PATH) === '/profile.php' - && perms_check( - perms_get_user( - MSZ_PERMS_USER, - user_session_current('user_id', 0) - ), - MSZ_PERM_USER_MANAGE_USERS - ) - )) { - $avatarFilename = config_get_default(MSZ_ROOT . '/public/images/banned-avatar.png', 'Avatar', 'banned_path'); - } else { - $avatarFilename = config_get_default(MSZ_ROOT . '/public/images/no-avatar.png', 'Avatar', 'default_path'); - $userAvatar = "{$userId}.msz"; - $storageDir = MSZ_STORAGE . '/avatars/200x200'; - $croppedAvatar = $storageDir . '/' . $userAvatar; - - if (is_file($croppedAvatar)) { - $avatarFilename = $croppedAvatar; - } else { - $originalAvatar = MSZ_STORAGE . '/avatars/original/' . $userAvatar; - - if (is_file($originalAvatar)) { - try { - mkdirs($storageDir, true); - - file_put_contents( - $croppedAvatar, - crop_image_centred_path($originalAvatar, 200, 200)->getImagesBlob(), - LOCK_EX - ); - - $avatarFilename = $croppedAvatar; - } catch (Exception $ex) { - } - } - } - } - - $fileTime = filemtime($avatarFilename); - $entityTag = "\"avatar-{$userId}-{$fileTime}\""; - - if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) && strtolower($_SERVER['HTTP_IF_NONE_MATCH']) === $entityTag) { - http_response_code(304); - break; - } - - header('Content-Type: ' . mime_content_type($avatarFilename)); - header("ETag: {$entityTag}"); - echo file_get_contents($avatarFilename); - break; - - case 'background': - $userId = (int)($_GET['u'] ?? 0); - - if (user_warning_check_expiration($userId, MSZ_WARN_BAN) > 0 && !( - parse_url($_SERVER['HTTP_REFERER'], PHP_URL_PATH) === '/profile.php' - && perms_check( - perms_get_user( - MSZ_PERMS_USER, - user_session_current('user_id', 0) - ), - MSZ_PERM_USER_MANAGE_USERS - ) - )) { - echo render_error(404); - break; - } - - $storageDir = MSZ_STORAGE . '/backgrounds/original'; - $userBackground = "{$storageDir}/{$userId}.msz"; - mkdirs($storageDir, true); - - if (!is_file($userBackground)) { - echo render_error(404); - break; - } - - $fileTime = filemtime($userBackground); - $entityTag = "\"background-{$userId}-{$fileTime}\""; - - if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) && strtolower($_SERVER['HTTP_IF_NONE_MATCH']) === $entityTag) { - http_response_code(304); - break; - } - - header('Content-Type: ' . mime_content_type($userBackground)); - header("ETag: {$entityTag}"); - echo file_get_contents($userBackground); - break; - case 'following': $userId = (int)($_GET['u'] ?? 0); diff --git a/public/user-assets.php b/public/user-assets.php new file mode 100644 index 00000000..0ac7f30a --- /dev/null +++ b/public/user-assets.php @@ -0,0 +1,87 @@ +getImagesBlob(), + LOCK_EX + ); + + $filename = $avatarCropped; + } catch (Exception $ex) { + } + } + } + break; + + case 'background': + if (!$canViewImages && !$userExists) { + break; + } + + $backgroundStorage = sprintf('%s/backgrounds/original', MSZ_STORAGE); + $filename = sprintf('%s/%d.msz', $backgroundStorage, $userId); + mkdirs($backgroundStorage, true); + break; +} + +if (empty($filename) || !is_file($filename)) { + http_response_code(404); + return; +} + +$entityTag = sprintf('W/"{%s-%d-%d}"', $userAssetsMode, $userId, filemtime($filename)); + +if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] === $entityTag) { + http_response_code(304); + return; +} + +http_response_code(200); +header(sprintf('Content-Type: %s', mime_content_type($filename))); +header(sprintf('ETag: %s', $entityTag)); +echo file_get_contents($filename); diff --git a/src/url.php b/src/url.php index 97a65586..3fc4cf0a 100644 --- a/src/url.php +++ b/src/url.php @@ -42,12 +42,10 @@ define('MSZ_URLS', [ 'user-profile' => ['/profile.php', ['u' => '']], 'user-profile-edit' => ['/profile.php', ['u' => '', 'm' => 'edit']], - 'user-avatar' => ['/profile.php', ['u' => '', 'm' => 'avatar']], - 'user-background' => ['/profile.php', ['u' => '', 'm' => 'background']], + 'user-avatar' => ['/user-assets.php', ['u' => '', 'm' => 'avatar']], + 'user-background' => ['/user-assets.php', ['u' => '', 'm' => 'background']], 'user-account-standing' => ['/profile.php', ['u' => ''], 'account-standing'], - 'guest-avatar' => ['/profile.php', ['m' => 'avatar']], - 'user-relation-none' => ['/relations.php', ['u' => '', 'm' => '[MSZ_USER_RELATION_NONE]', 'c' => '{user_relation}']], 'user-relation-follow' => ['/relations.php', ['u' => '', 'm' => '[MSZ_USER_RELATION_FOLLOW]', 'c' => '{user_relation}']], diff --git a/templates/_layout/comments.twig b/templates/_layout/comments.twig index 8e59c6af..b4ee38bd 100644 --- a/templates/_layout/comments.twig +++ b/templates/_layout/comments.twig @@ -15,7 +15,7 @@
+ style="background-image:url('{{ url('user-avatar', {'user':user.user_id}) }}')">