]> BookStack Code Mirror - bookstack/blob - app/Util/WebSafeMimeSniffer.php
Aligned notification capitalisation
[bookstack] / app / Util / WebSafeMimeSniffer.php
1 <?php
2
3 namespace BookStack\Util;
4
5 use finfo;
6
7 /**
8  * Helper class to sniff out the mime-type of content resulting in
9  * a mime-type that's relatively safe to serve to a browser.
10  */
11 class WebSafeMimeSniffer
12 {
13     /**
14      * @var string[]
15      */
16     protected $safeMimes = [
17         'application/json',
18         'application/octet-stream',
19         'application/pdf',
20         'image/bmp',
21         'image/jpeg',
22         'image/png',
23         'image/gif',
24         'image/webp',
25         'image/avif',
26         'image/heic',
27         'text/css',
28         'text/csv',
29         'text/javascript',
30         'text/json',
31         'text/plain',
32         'video/x-msvideo',
33         'video/mp4',
34         'video/mpeg',
35         'video/ogg',
36         'video/webm',
37         'video/vp9',
38         'video/h264',
39         'video/av1',
40     ];
41
42     /**
43      * Sniff the mime-type from the given file content while running the result
44      * through an allow-list to ensure a web-safe result.
45      * Takes the content as a reference since the value may be quite large.
46      */
47     public function sniff(string &$content): string
48     {
49         $fInfo = new finfo(FILEINFO_MIME_TYPE);
50         $mime = $fInfo->buffer($content) ?: 'application/octet-stream';
51
52         if (in_array($mime, $this->safeMimes)) {
53             return $mime;
54         }
55
56         [$category] = explode('/', $mime, 2);
57         if ($category === 'text') {
58             return 'text/plain';
59         }
60
61         return 'application/octet-stream';
62     }
63 }