1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12// load new map
13$data = file_get_contents('https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types');
14$new = [];
15foreach (explode("\n", $data) as $line) {
16    if (!$line || '#' == $line[0]) {
17        continue;
18    }
19    $mimeType = substr($line, 0, strpos($line, "\t"));
20    $extensions = explode(' ', substr($line, strrpos($line, "\t") + 1));
21    $new[$mimeType] = $extensions;
22}
23
24$xml = simplexml_load_string(file_get_contents('https://raw.github.com/minad/mimemagic/master/script/freedesktop.org.xml'));
25foreach ($xml as $node) {
26    $exts = [];
27    foreach ($node->glob as $glob) {
28        $pattern = (string) $glob['pattern'];
29        if ('*' != $pattern[0] || '.' != $pattern[1]) {
30            continue;
31        }
32
33        $exts[] = substr($pattern, 2);
34    }
35
36    if (!$exts) {
37        continue;
38    }
39
40    $mt = strtolower((string) $node['type']);
41    $new[$mt] = array_merge($new[$mt] ?? [], $exts);
42    foreach ($node->alias as $alias) {
43        $mt = strtolower((string) $alias['type']);
44        $new[$mt] = array_merge($new[$mt] ?? [], $exts);
45    }
46}
47
48// load current map
49$data = file_get_contents($output = __DIR__.'/../../MimeTypes.php');
50$current = [];
51$pre = '';
52$post = '';
53foreach (explode("\n", $data) as $line) {
54    if (!preg_match("{^        '([^']+/[^']+)' => \['(.+)'\],$}", $line, $matches)) {
55        if (!$current) {
56            $pre .= $line."\n";
57        } else {
58            $post .= $line."\n";
59        }
60        continue;
61    }
62    $current[$matches[1]] = explode("', '", $matches[2]);
63}
64
65// we merge the 2 maps (we never remove old mime types)
66$map = array_replace_recursive($current, $new);
67ksort($map);
68
69$data = $pre;
70foreach ($map as $mimeType => $exts) {
71    $data .= sprintf("        '%s' => ['%s'],\n", $mimeType, implode("', '", array_unique($exts)));
72}
73$data .= $post;
74
75// reverse map
76// we prefill the extensions with some preferences for content-types
77$exts = [
78    'aif' => ['audio/x-aiff'],
79    'aiff' => ['audio/x-aiff'],
80    'aps' => ['application/postscript'],
81    'avi' => ['video/avi'],
82    'bmp' => ['image/bmp'],
83    'bz2' => ['application/x-bz2'],
84    'css' => ['text/css'],
85    'csv' => ['text/csv'],
86    'dmg' => ['application/x-apple-diskimage'],
87    'doc' => ['application/msword'],
88    'docx' => ['application/vnd.openxmlformats-officedocument.wordprocessingml.document'],
89    'eml' => ['message/rfc822'],
90    'exe' => ['application/x-ms-dos-executable'],
91    'flv' => ['video/x-flv'],
92    'gif' => ['image/gif'],
93    'gz' => ['application/x-gzip'],
94    'hqx' => ['application/stuffit'],
95    'htm' => ['text/html'],
96    'html' => ['text/html'],
97    'jar' => ['application/x-java-archive'],
98    'jpeg' => ['image/jpeg'],
99    'jpg' => ['image/jpeg'],
100    'js' => ['text/javascript'],
101    'm3u' => ['audio/x-mpegurl'],
102    'm4a' => ['audio/mp4'],
103    'mdb' => ['application/x-msaccess'],
104    'mid' => ['audio/midi'],
105    'midi' => ['audio/midi'],
106    'mov' => ['video/quicktime'],
107    'mp3' => ['audio/mpeg'],
108    'mp4' => ['video/mp4'],
109    'mpeg' => ['video/mpeg'],
110    'mpg' => ['video/mpeg'],
111    'ogg' => ['audio/ogg'],
112    'pdf' => ['application/pdf'],
113    'php' => ['application/x-php'],
114    'php3' => ['application/x-php'],
115    'php4' => ['application/x-php'],
116    'php5' => ['application/x-php'],
117    'png' => ['image/png'],
118    'ppt' => ['application/vnd.ms-powerpoint'],
119    'pptx' => ['application/vnd.openxmlformats-officedocument.presentationml.presentation'],
120    'ps' => ['application/postscript'],
121    'rar' => ['application/x-rar-compressed'],
122    'rtf' => ['application/rtf'],
123    'sit' => ['application/x-stuffit'],
124    'svg' => ['image/svg+xml'],
125    'tar' => ['application/x-tar'],
126    'tif' => ['image/tiff'],
127    'tiff' => ['image/tiff'],
128    'ttf' => ['application/x-font-truetype'],
129    'txt' => ['text/plain'],
130    'vcf' => ['text/x-vcard'],
131    'wav' => ['audio/wav'],
132    'wma' => ['audio/x-ms-wma'],
133    'wmv' => ['audio/x-ms-wmv'],
134    'xls' => ['application/vnd.ms-excel'],
135    'xlsx' => ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
136    'xml' => ['application/xml'],
137    'zip' => ['application/zip'],
138];
139foreach ($map as $mimeType => $extensions) {
140    foreach ($extensions as $extension) {
141        $exts[$extension][] = $mimeType;
142    }
143}
144ksort($exts);
145
146$updated = '';
147$state = 0;
148foreach (explode("\n", $data) as $line) {
149    if (!preg_match("{^        '([^'/]+)' => \['(.+)'\],$}", $line, $matches)) {
150        if (1 === $state) {
151            $state = 2;
152            foreach ($exts as $ext => $mimeTypes) {
153                $updated .= sprintf("        '%s' => ['%s'],\n", $ext, implode("', '", array_unique($mimeTypes)));
154            }
155        }
156        $updated .= $line."\n";
157        continue;
158    }
159    $state = 1;
160}
161
162$updated = preg_replace('{Updated from upstream on .+?\.}', 'Updated from upstream on '.date('Y-m-d'), $updated, -1);
163
164file_put_contents($output, rtrim($updated, "\n")."\n");
165
166echo "Done.\n";
167