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