1#!/usr/bin/env php
2<?php
3
4// Check if we are being given a mime.types file or if we should use the
5// default URL.
6$source = count($argv) > 1 ? $argv[1] : 'https://cdn.jsdelivr.net/gh/jshttp/mime-db@v1.45.0/db.json';
7
8// See if we can actually load it.
9$data = @file_get_contents($source);
10if ($data === false) {
11    fprintf(STDERR, "Error: unable to read $source\n");
12    exit(1);
13}
14
15// Source preference from lowest to highest
16$prefs = ['nginx' => 1, 'apache' => 2, 'custom' => 3, 'iana' => 4];
17
18$extensions = [];
19$types = json_decode($data, true);
20foreach ($types as $mime => $info) {
21    $source = $info['source'] ?? 'custom';
22    $pref = $prefs[$source];
23    foreach ($info['extensions'] ?? [] as $extension) {
24        if (isset($extensions[$extension])) {
25            // Use same preference rules as jshttp/mime-types
26            [$oldMime, $oldPref] = $extensions[$extension];
27            if ($oldMime !== 'application/octet-stream'
28                && ($oldPref > $pref
29                    || ($oldPref === $pref && substr($oldMime, 0, 12) === 'application/'))) {
30                continue;
31            }
32        }
33
34        $extensions[$extension] = [$mime, $pref];
35    }
36}
37
38// Only keep the MIME type.
39$extensions = array_map(function($x) { return $x[0]; }, $extensions);
40
41$additional_mime_maps = [
42    "jsm" => "application/javascript",
43];
44
45foreach($additional_mime_maps as $ext => $mime) {
46    if (!isset($extensions[$ext])) {
47        $extensions[$ext] = $mime;
48    } else {
49        fprintf(STDERR, "Ignored exist mime type: $ext => $mime\n");
50    }
51}
52
53uksort($extensions, function($ext1, $ext2) {
54    return strcmp($ext1, $ext2);
55});
56
57echo <<<HEADER
58/*
59   +----------------------------------------------------------------------+
60   | Copyright (c) The PHP Group                                          |
61   +----------------------------------------------------------------------+
62   | This source file is subject to version 3.01 of the PHP license,      |
63   | that is bundled with this package in the file LICENSE, and is        |
64   | available through the world-wide-web at the following url:           |
65   | http://www.php.net/license/3_01.txt                                  |
66   | If you did not receive a copy of the PHP license and are unable to   |
67   | obtain it through the world-wide-web, please send a note to          |
68   | license@php.net so we can mail you a copy immediately.               |
69   +----------------------------------------------------------------------+
70   | Author: Moriyoshi Koizumi <moriyoshi@php.net>                        |
71   +----------------------------------------------------------------------+
72*/
73
74/* This is a generated file. Rather than modifying it, please run
75 * "php generate_mime_type_map.php > mime_type_map.h" to regenerate the file. */
76
77#ifndef PHP_CLI_SERVER_MIME_TYPE_MAP_H
78#define PHP_CLI_SERVER_MIME_TYPE_MAP_H
79
80typedef struct php_cli_server_ext_mime_type_pair {
81\tconst char *ext;
82\tconst char *mime_type;
83} php_cli_server_ext_mime_type_pair;
84
85static const php_cli_server_ext_mime_type_pair mime_type_map[] = {
86
87HEADER;
88
89foreach ($extensions as $extension => $mime) {
90    echo "\t{ \"" .addcslashes($extension, "\0..\37!@\@\177..\377") . "\", \""
91        . addcslashes($mime, "\0..\37!@\@\177..\377") . "\" },\n";
92}
93
94echo <<<FOOTER
95\t{ NULL, NULL }
96};
97
98#endif /* PHP_CLI_SERVER_MIME_TYPE_MAP_H */
99
100FOOTER;
101