summaryrefslogtreecommitdiff
path: root/src/font.php
blob: d12ceb84948a3e34bfe4c2e8dd43d0db5be8beff (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
44
45
46
47
48
49
50
<?php
/* font.php — serve font files from the server's font directories */

$encoded = $_GET['f'] ?? '';
if (empty($encoded)) {
    http_response_code(400);
    exit('Missing parameter');
}

$file = base64_decode($encoded, true);
if ($file === false || !file_exists($file)) {
    http_response_code(404);
    exit('Font not found');
}

$real    = realpath($file);
$allowed = ['/usr/share/fonts', '/usr/local/share/fonts'];

foreach (glob('/home/*', GLOB_ONLYDIR) as $home) {
    $allowed[] = $home . '/.local/share/fonts';
    $allowed[] = $home . '/.fonts';
}

$ok = false;

foreach ($allowed as $dir) {
    if (str_starts_with($real, realpath($dir) ?: $dir)) {
        $ok = true;
        break;
    }
}

if (!$ok) {
    http_response_code(403);
    exit('Access denied');
}

$ext  = strtolower(pathinfo($file, PATHINFO_EXTENSION));
$mime = match ($ext) {
    'ttf'   => 'font/ttf',
    'otf'   => 'font/otf',
    'woff'  => 'font/woff',
    'woff2' => 'font/woff2',
    default => 'application/octet-stream',
};

header("Content-Type: $mime");
header('Cache-Control: public, max-age=31536000, immutable');
header('Content-Length: ' . filesize($file));
readfile($file);