summaryrefslogtreecommitdiff
path: root/src/application/actions/profilepicture/get.php
blob: 94f474a331975cb94081e636c03af0474253c4e0 (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
40
41
42
43
<?php

use mystic\forum\orm\User;

$userId = $_GET["user"] ?? call_user_func(function() {
    http_response_code(400);
    msg_error(__("Missing user id"));
    exit;
});
$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;
}