forked from wapmorgan/FileTypeDetector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable-generator
90 lines (73 loc) · 2.38 KB
/
table-generator
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env php
<?php
require __DIR__.'/../vendor/autoload.php';
use wapmorgan\FileTypeDetector\Detector;
$class = new ReflectionClass('wapmorgan\FileTypeDetector\Detector');
$properties = $class->getDefaultProperties();
$all_formats = $properties['extensions'];
$all_types = $properties['types'];
function bytesArray2String(array $bytes) {
$output = null;
foreach ($bytes as $byte) {
$output .= dechex($byte);
}
return $output;
}
if (isset($argv[1]) && in_array($argv[1], array('types', 'support'))) {
$table = $argv[1];
} else {
echo 'Table type (write "types" or "support"): ';
$input = trim(fgets(STDIN));
if (!in_array($input, array('types', 'support')))
die('Sorry, but input is invalid'.PHP_EOL);
$table = $input;
}
switch ($table) {
case 'types':
ksort($all_types);
$i = 0;
foreach ($all_types as $type => $type_formats) {
echo ($i++ > 0 ? ',' : null).ucfirst($type);
sort($type_formats);
$all_types[$type] = $type_formats;
}
echo PHP_EOL;
$max_count = max(array_map(function ($formats) { return count($formats); }, $all_types));
for ($i = 0; $i < $max_count; $i++) {
$j = 0;
foreach ($all_types as $type => $type_formats) {
echo ($j++ > 0 ? ',' : null).(isset($type_formats[$i]) ? $type_formats[$i] : null);
}
echo PHP_EOL;
}
break;
case 'support':
$mimetypes = $properties['mimeTypes'];
$signatures = $properties['signatures'];
echo 'Format,Extension,"Detection by content",MimeType,Signature'.PHP_EOL;
asort($all_formats);
foreach ($all_formats as $extension => $format) {
echo ucfirst($format).','.$extension.','.(isset($signatures[$format]) ? '+' : '-').','.(isset($mimetypes[$format]) ? $mimetypes[$format] : '-').',';
if (isset($signatures[$format])) {
foreach ($signatures[$format] as $j => $format_signature) {
if ($j > 0) echo ' / ';
$i = 0;
foreach ($format_signature as $offset => $signature_part) {
if ($i++ > 0) echo ' & ';
echo 'at ['.$offset.']: (';
if (is_string($signature_part)) echo '\''.$signature_part.'\'';
else {
if (isset($signature_part['bytes'])) {
echo 'fuzzy search \''.implode(null, $signature_part['bytes']).'\'';
}
else if (is_integer($signature_part[0])) echo '0x'.bytesArray2String($signature_part);
else echo '\''.implode(null, $signature_part).'\'';
}
echo ') ';
}
}
}
echo PHP_EOL;
}
break;
}