Rev 5 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 5 | Rev 8 | ||
---|---|---|---|
1 | <?php |
1 | <?php |
2 | 2 | ||
3 | /* |
3 | /* |
4 | * File Type Detection for PHP |
4 | * File Type Detection for PHP |
5 | * Copyright 2020 Daniel Marschall, ViaThinkSoft |
5 | * Copyright 2020 - 2021 Daniel Marschall, ViaThinkSoft |
6 | * |
6 | * |
7 | * Revision 2020-05-17 |
7 | * Revision 2021-05-21 |
8 | * |
8 | * |
9 | * Licensed under the Apache License, Version 2.0 (the "License"); |
9 | * Licensed under the Apache License, Version 2.0 (the "License"); |
10 | * you may not use this file except in compliance with the License. |
10 | * you may not use this file except in compliance with the License. |
11 | * You may obtain a copy of the License at |
11 | * You may obtain a copy of the License at |
12 | * |
12 | * |
13 | * http://www.apache.org/licenses/LICENSE-2.0 |
13 | * http://www.apache.org/licenses/LICENSE-2.0 |
14 | * |
14 | * |
15 | * Unless required by applicable law or agreed to in writing, software |
15 | * Unless required by applicable law or agreed to in writing, software |
16 | * distributed under the License is distributed on an "AS IS" BASIS, |
16 | * distributed under the License is distributed on an "AS IS" BASIS, |
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
18 | * See the License for the specific language governing permissions and |
18 | * See the License for the specific language governing permissions and |
19 | * limitations under the License. |
19 | * limitations under the License. |
20 | */ |
20 | */ |
21 | 21 | ||
22 | class VtsFileTypeDetect { |
22 | class VtsFileTypeDetect { |
23 | 23 | ||
24 | public static function getMimeType($filename) { |
24 | public static function getMimeType($filename) { |
25 | $mime_types = array(); |
25 | $mime_types = array(); |
26 | 26 | ||
27 | include __DIR__ . '/mimetype_lookup.inc.php'; |
27 | include __DIR__ . '/mimetype_lookup.inc.php'; |
28 | 28 | ||
- | 29 | /** @phpstan-ignore-next-line */ |
|
29 | foreach ($mime_types as $ext => $mime) { |
30 | foreach ($mime_types as $ext => $mime) { |
30 | if (strtoupper(substr($filename, -strlen($ext)-1)) == strtoupper('.'.$ext)) { |
31 | if (strtoupper(substr($filename, -strlen($ext)-1)) == strtoupper('.'.$ext)) { |
31 | return $mime; |
32 | return $mime; |
32 | } |
33 | } |
33 | } |
34 | } |
34 | 35 | ||
35 | return false; |
36 | return false; |
36 | } |
37 | } |
37 | 38 | ||
38 | public static function getDescription($file, $filenames=array(__DIR__.'/filetypes.local', __DIR__.'/filetypes.conf')) { |
39 | public static function getDescription($file, $filenames=array(__DIR__.'/filetypes.local', __DIR__.'/filetypes.conf')) { |
39 | // TODO: Make it multi-lang |
40 | // TODO: Make it multi-lang |
40 | 41 | ||
41 | $inis = array(); |
42 | $inis = array(); |
42 | foreach ($filenames as $num => $filename) { |
43 | foreach ($filenames as $num => $filename) { |
43 | $inis[$num] = !file_exists($filename) ? array() : parse_ini_file($filename, true, INI_SCANNER_RAW); |
44 | $inis[$num] = !file_exists($filename) ? array() : parse_ini_file($filename, true, INI_SCANNER_RAW); |
44 | if (!isset($inis[$num]['OidHeader'])) $inis[$num]['OidHeader'] = array(); |
45 | if (!isset($inis[$num]['OidHeader'])) $inis[$num]['OidHeader'] = array(); |
45 | if (!isset($inis[$num]['GuidHeader'])) $inis[$num]['GuidHeader'] = array(); |
46 | if (!isset($inis[$num]['GuidHeader'])) $inis[$num]['GuidHeader'] = array(); |
46 | if (!isset($inis[$num]['FileExtension'])) $inis[$num]['FileExtension'] = array(); |
47 | if (!isset($inis[$num]['FileExtension'])) $inis[$num]['FileExtension'] = array(); |
47 | if (!isset($inis[$num]['MimeType'])) $inis[$num]['MimeType'] = array(); |
48 | if (!isset($inis[$num]['MimeType'])) $inis[$num]['MimeType'] = array(); |
48 | } |
49 | } |
49 | 50 | ||
50 | if (is_readable($file)) { |
51 | if (is_readable($file)) { |
51 | $h = fopen($file, 'r'); |
52 | $h = fopen($file, 'r'); |
52 | $line = trim(fgets($h, 128)); |
53 | $line = trim(fgets($h, 128)); |
53 | if ((substr($line,0,1) == '[') && ($line[strlen($line)-1] == ']')) { |
54 | if ((substr($line,0,1) == '[') && ($line[strlen($line)-1] == ']')) { |
54 | $line = substr($line, 1, strlen($line)-2); |
55 | $line = substr($line, 1, strlen($line)-2); |
55 | foreach ($inis as $ini) { |
56 | foreach ($inis as $ini) { |
56 | if (isset($ini['OidHeader'][$line])) return $ini['OidHeader'][$line]; |
57 | if (isset($ini['OidHeader'][$line])) return $ini['OidHeader'][$line]; |
57 | } |
58 | } |
58 | foreach ($inis as $ini) { |
59 | foreach ($inis as $ini) { |
59 | if (isset($ini['GuidHeader'][$line])) return $ini['GuidHeader'][$line]; |
60 | if (isset($ini['GuidHeader'][$line])) return $ini['GuidHeader'][$line]; |
60 | } |
61 | } |
61 | } |
62 | } |
62 | fclose($h); |
63 | fclose($h); |
63 | } |
64 | } |
64 | 65 | ||
65 | foreach ($inis as $ini) { |
66 | foreach ($inis as $ini) { |
66 | foreach ($ini['FileExtension'] as $ext => $name) { |
67 | foreach ($ini['FileExtension'] as $ext => $name) { |
67 | if (strtoupper(substr($file, -strlen($ext)-1)) == strtoupper('.'.$ext)) { |
68 | if (strtoupper(substr($file, -strlen($ext)-1)) == strtoupper('.'.$ext)) { |
68 | return $name; |
69 | return $name; |
69 | } |
70 | } |
70 | } |
71 | } |
71 | } |
72 | } |
72 | 73 | ||
73 | $mime = false; |
74 | $mime = false; |
74 | if (function_exists('mime_content_type')) { |
75 | if (function_exists('mime_content_type')) { |
75 | $mime = @mime_content_type($file); |
76 | $mime = @mime_content_type($file); |
76 | } |
77 | } |
77 | if (!$mime) { |
78 | if (!$mime) { |
78 | $mime = self::getMimeType($file); |
79 | $mime = self::getMimeType($file); |
79 | } |
80 | } |
80 | if ($mime) { |
81 | if ($mime) { |
81 | foreach ($inis as $ini) { |
82 | foreach ($inis as $ini) { |
82 | if (isset($ini['MimeType'][$mime])) return $ini['MimeType'][$mime]; |
83 | if (isset($ini['MimeType'][$mime])) return $ini['MimeType'][$mime]; |
83 | } |
84 | } |
84 | } |
85 | } |
85 | 86 | ||
- | 87 | foreach ($inis as $ini) { |
|
- | 88 | if (isset($ini['Static']['LngUnknown'])) { |
|
86 | return $ini['Static']['LngUnknown']; |
89 | return $ini['Static']['LngUnknown']; |
87 | } |
90 | } |
- | 91 | } |
|
- | 92 | return 'Unknown'; |
|
- | 93 | } |
|
88 | 94 | ||
89 | } |
95 | } |
90 | 96 |