Subversion Repositories fileformats

Rev

Rev 3 | Rev 5 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3 Rev 4
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 Daniel Marschall, ViaThinkSoft
6
 *
6
 *
7
 *    Revision 2020-05-17
7
 *    Revision 2020-05-17
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
                include __DIR__ . '/mimetype_lookup.inc.php';
25
                include __DIR__ . '/mimetype_lookup.inc.php';
26
 
26
 
27
                foreach ($mime_types as $ext => $mime) {
27
                foreach ($mime_types as $ext => $mime) {
28
                        if (strtoupper(substr($filename, -strlen($ext)-1)) == strtoupper('.'.$ext)) {
28
                        if (strtoupper(substr($filename, -strlen($ext)-1)) == strtoupper('.'.$ext)) {
29
                                return $mime;
29
                                return $mime;
30
                        }
30
                        }
31
                }
31
                }
32
 
32
 
33
                return false;
33
                return false;
34
        }
34
        }
35
 
35
 
36
        public static function getDescription($file, $filenames=array(__DIR__.'/filetypes.local', __DIR__.'/filetypes.conf')) {
36
        public static function getDescription($file, $filenames=array(__DIR__.'/filetypes.local', __DIR__.'/filetypes.conf')) {
37
                // TODO: Make it multi-lang
37
                // TODO: Make it multi-lang
38
 
38
 
39
                $inis = array();
39
                $inis = array();
40
                foreach ($filenames as $num => $filename) {
40
                foreach ($filenames as $num => $filename) {
41
                        $inis[$num] = !file_exists($filename) ? array() : parse_ini_file($filename, true, INI_SCANNER_RAW);
41
                        $inis[$num] = !file_exists($filename) ? array() : parse_ini_file($filename, true, INI_SCANNER_RAW);
42
                        if (!isset($inis[$num]['OidHeader']))     $inis[$num]['OidHeader']     = array();
42
                        if (!isset($inis[$num]['OidHeader']))     $inis[$num]['OidHeader']     = array();
43
                        if (!isset($inis[$num]['GuidHeader']))    $inis[$num]['GuidHeader']    = array();
43
                        if (!isset($inis[$num]['GuidHeader']))    $inis[$num]['GuidHeader']    = array();
44
                        if (!isset($inis[$num]['FileExtension'])) $inis[$num]['FileExtension'] = array();
44
                        if (!isset($inis[$num]['FileExtension'])) $inis[$num]['FileExtension'] = array();
45
                        if (!isset($inis[$num]['MimeType']))      $inis[$num]['MimeType']      = array();
45
                        if (!isset($inis[$num]['MimeType']))      $inis[$num]['MimeType']      = array();
46
                }
46
                }
47
 
47
 
48
                if (is_readable($file)) {
48
                if (is_readable($file)) {
49
                        $h = fopen($file, 'r');
49
                        $h = fopen($file, 'r');
50
                        $line = trim(fgets($h, 128));
50
                        $line = trim(fgets($h, 128));
51
                        if (($line[0] == '[') && ($line[strlen($line)-1] == ']')) {
51
                        if ((substr($line,0,1) == '[') && ($line[strlen($line)-1] == ']')) {
52
                                $line = substr($line, 1, strlen($line)-2);
52
                                $line = substr($line, 1, strlen($line)-2);
53
                                foreach ($inis as $ini) {
53
                                foreach ($inis as $ini) {
54
                                        if (isset($ini['OidHeader'][$line]))  return $ini['OidHeader'][$line];
54
                                        if (isset($ini['OidHeader'][$line]))  return $ini['OidHeader'][$line];
55
                                }
55
                                }
56
                                foreach ($inis as $ini) {
56
                                foreach ($inis as $ini) {
57
                                        if (isset($ini['GuidHeader'][$line])) return $ini['GuidHeader'][$line];
57
                                        if (isset($ini['GuidHeader'][$line])) return $ini['GuidHeader'][$line];
58
                                }
58
                                }
59
                        }
59
                        }
60
                        fclose($h);
60
                        fclose($h);
61
                }
61
                }
62
 
62
 
63
                foreach ($inis as $ini) {
63
                foreach ($inis as $ini) {
64
                        foreach ($ini['FileExtension'] as $ext => $name) {
64
                        foreach ($ini['FileExtension'] as $ext => $name) {
65
                                if (strtoupper(substr($file, -strlen($ext)-1)) == strtoupper('.'.$ext)) {
65
                                if (strtoupper(substr($file, -strlen($ext)-1)) == strtoupper('.'.$ext)) {
66
                                        return $name;
66
                                        return $name;
67
                                }
67
                                }
68
                        }
68
                        }
69
                }
69
                }
70
 
70
 
71
                $mime = false;
71
                $mime = false;
72
                if (function_exists('mime_content_type')) {
72
                if (function_exists('mime_content_type')) {
73
                        $mime = @mime_content_type($file);
73
                        $mime = @mime_content_type($file);
74
                }
74
                }
75
                if (!$mime) {
75
                if (!$mime) {
76
                        $mime = self::getMimeType($file);
76
                        $mime = self::getMimeType($file);
77
                }
77
                }
78
                if ($mime) {
78
                if ($mime) {
79
                        foreach ($inis as $ini) {
79
                        foreach ($inis as $ini) {
80
                                if (isset($ini['MimeType'][$mime]))  return $ini['MimeType'][$mime];
80
                                if (isset($ini['MimeType'][$mime]))  return $ini['MimeType'][$mime];
81
                        }
81
                        }
82
                }
82
                }
83
 
83
 
84
                return $ini['Static']['LngUnknown'];
84
                return $ini['Static']['LngUnknown'];
85
        }
85
        }
86
 
86
 
87
}
87
}
88
 
88