summaryrefslogtreecommitdiff
path: root/src/application/actions/profilepicture/get.php
blob: c4860f1b0bad8f099fcc3a9d21d5ab833267485b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php

use mystic\forum\orm\User;

$userId = $_GET["user"] ?? throw new Exception("Missing user id");
$user = new User();
$user->id = $userId;
if (!$db->fetch($user)) {
    http_response_code(404);
    msg_error(__("No user exists with this id"));
    exit;
}

$ifNoneMatch = $_SERVER["HTTP_IF_NONE_MATCH"] ?? null;
if ($ifNoneMatch !== null)
    $ifNoneMatch = trim($ifNoneMatch, '"');

if ($user->profilePicture === null) {
    $fallback = __ROOT__ . "/application/assets/user-fallback.jpg";
    $etag = md5("\0");
    header("Content-Type: image/jpeg");
    header("Content-Length: " . filesize($fallback));
    header("Cache-Control: no-cache");
    header("ETag: \"" . $etag . "\"");
    if ($ifNoneMatch === $etag)
        http_response_code(304);
    else
        readfile($fallback);
} else {
    $etag = md5($user->profilePicture);
    header("Content-Type: image/jpeg");
    header("Content-Length: " . strlen($user->profilePicture));
    header("Cache-Control: no-cache");
    header("ETag: \"" . $etag . "\"");
    if ($ifNoneMatch === $etag)
        http_response_code(304);
    else
        echo $user->profilePicture;
}