1<?php
2/**
3 * Create MIME mapping file from data sources.
4 *
5 * Copyright 2001-2017 Horde LLC (http://www.horde.org/)
6 *
7 * See the enclosed file COPYING for license information (LGPL). If you
8 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
9 *
10 * @author    Anil Madhavapeddy <avsm@horde.org>
11 * @author    Michael Slusarz <slusarz@horde.org>
12 * @category  Horde
13 * @copyright 2001-2017 Horde LLC
14 * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
15 * @package   Mime
16 */
17
18/* Files containing MIME extensions (Apache format).
19 * https://github.com/apache/httpd/blob/trunk/docs/conf/mime.types */
20$files = array(
21    'mime.types',
22    'mime.types.horde'
23);
24
25/* Files contating MIME extensions (freedesktop.org format).
26 * http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec */
27$od_files = array(
28    'mime.globs'
29);
30
31$exts = array();
32$maxlength = strlen('__MAXPERIOD__');
33$maxperiod = 0;
34
35/* Map the mime extensions file(s) into the $exts hash. */
36foreach ($files as $val) {
37    /* Read file and remove trailing whitespace. */
38    $data = array_filter(array_map('rtrim', file($val)));
39
40    foreach ($data as $line) {
41        /* Skip comments. */
42        if ($line[0] === '#') {
43            continue;
44        }
45
46        /* These are tab-delimited files. Skip the entry if there is no
47         * extension information. */
48        $fields = preg_split("/\s+/", $line, 2);
49        if (!empty($fields[1])) {
50            foreach (preg_split("/\s+/", $fields[1]) as $val2) {
51                $exts[$val2] = $fields[0];
52                $maxlength = max(strlen($val2), $maxlength);
53            }
54        }
55    }
56}
57
58foreach ($od_files as $val) {
59    /* Read file and remove trailing whitespace. */
60    $data = array_filter(array_map('rtrim', file($val)));
61
62    foreach ($data as $line) {
63        /* Skip comments. */
64        if ($line[0] === '#') {
65            continue;
66        }
67
68        /* These are ':' delimited files. Skip the entry if this is not
69           an extension matching glob. */
70        $fields = explode(':', $line, 2);
71        $pos = strpos($fields[1], '*.');
72        if ($pos !== false) {
73            $val2 = substr($fields[1], $pos + 2);
74            if ((strpos($val2, '*') !== false) ||
75                (strpos($val2, '[') !== false) ||
76                isset($exts[$val2])) {
77                continue;
78            }
79            $maxperiod = max(substr_count($val2, '.'), $maxperiod);
80            $maxlength = max(strlen($val2), $maxlength);
81            $exts[$val2] = $fields[0];
82        }
83    }
84}
85
86/* Assemble/sort the extensions into an output array. */
87$output = array(
88    sprintf(
89        "'__MAXPERIOD__'%s => '%u'",
90        str_repeat(' ', $maxlength - strlen('__MAXPERIOD__')),
91        $maxperiod
92    )
93);
94
95ksort($exts);
96
97/* Special case: move .jpg to the first image/jpeg entry. */
98$first_jpeg = array_search('image/jpeg', $exts);
99$keys = array_keys($exts);
100$index1 = array_search($first_jpeg, $keys);
101$index2 = array_search('jpg', $keys);
102$keys[$index1] = 'jpg';
103$keys[$index2] = $first_jpeg;
104$exts = array_combine($keys, array_values($exts));
105
106foreach ($exts as $key => $val) {
107    $output[] = sprintf(
108        "'%s'%s => '%s'",
109        $key,
110        str_repeat(' ', $maxlength - strlen($key)),
111        $val
112    );
113}
114
115/* Generate the PHP output file. */
116$generated = sprintf(
117    '%s by %s on %s',
118    strftime('%D %T'),
119    $_SERVER['USER'],
120    $_SERVER['HOST']
121);
122$map = implode(",\n    ", $output);
123
124print <<<HEADER
125<?php
126/**
127 * This file contains a mapping of common file extensions to MIME types.
128 * It has been automatically generated.
129 * Any changes made directly to this file may/will be lost in the future.
130 *
131 * Any unknown file extensions will automatically be mapped to
132 * 'x-extension/<ext>' where <ext> is the unknown file extension.
133 *
134 * Generated: $generated
135 *
136 * @category Horde
137 * @package  Mime
138 */
139\$mime_extension_map = array(
140    $map
141);
142HEADER;
143