TYPO3  7.6
swiftmailer_generate_mimes_config.php
Go to the documentation of this file.
1 #!/usr/bin/php
2 
3 <?php
4 define('APACHE_MIME_TYPES_URL', 'http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types');
5 define('FREEDESKTOP_XML_URL', 'https://raw2.github.com/minad/mimemagic/master/script/freedesktop.org.xml');
6 
8 {
9  $preamble = "<?php\n\n";
10  $preamble .= "/*\n";
11  $preamble .= " * This file is part of SwiftMailer.\n";
12  $preamble .= " * (c) 2004-2009 Chris Corbyn\n *\n";
13  $preamble .= " * For the full copyright and license information, please view the LICENSE\n";
14  $preamble .= " * file that was distributed with this source code.\n *\n";
15  $preamble .= " * autogenerated using http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types\n";
16  $preamble .= " * and https://raw.github.com/minad/mimemagic/master/script/freedesktop.org.xml\n";
17  $preamble .= " */\n\n";
18  $preamble .= "/*\n";
19  $preamble .= " * List of MIME type automatically detected in Swift Mailer.\n";
20  $preamble .= " */\n\n";
21  $preamble .= "// You may add or take away what you like (lowercase required)\n\n";
22 
23  // get current mime types files
24  $mime_types = @file_get_contents(APACHE_MIME_TYPES_URL);
25  $mime_xml = @file_get_contents(FREEDESKTOP_XML_URL);
26 
27  // prepare valid mime types
28  $valid_mime_types = array();
29 
30  // split mime type and extensions eg. "video/x-matroska mkv mk3d mks"
31  if (preg_match_all('/^#?([a-z0-9\-\+\/\.]+)[\t]+(.*)$/miu', $mime_types, $matches) !== false) {
32  // collection of predefined mimetypes (bugfix for wrong resolved or missing mime types)
33  $valid_mime_types_preset = array(
34  'php' => 'application/x-php',
35  'php3' => 'application/x-php',
36  'php4' => 'application/x-php',
37  'php5' => 'application/x-php',
38  'zip' => 'application/zip',
39  'gif' => 'image/gif',
40  'png' => 'image/png',
41  'css' => 'text/css',
42  'js' => 'text/javascript',
43  'txt' => 'text/plain',
44  'aif' => 'audio/x-aiff',
45  'aiff' => 'audio/x-aiff',
46  'avi' => 'video/avi',
47  'bmp' => 'image/bmp',
48  'bz2' => 'application/x-bz2',
49  'csv' => 'text/csv',
50  'dmg' => 'application/x-apple-diskimage',
51  'doc' => 'application/msword',
52  'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
53  'eml' => 'message/rfc822',
54  'aps' => 'application/postscript',
55  'exe' => 'application/x-ms-dos-executable',
56  'flv' => 'video/x-flv',
57  'gz' => 'application/x-gzip',
58  'hqx' => 'application/stuffit',
59  'htm' => 'text/html',
60  'html' => 'text/html',
61  'jar' => 'application/x-java-archive',
62  'jpeg' => 'image/jpeg',
63  'jpg' => 'image/jpeg',
64  'm3u' => 'audio/x-mpegurl',
65  'm4a' => 'audio/mp4',
66  'mdb' => 'application/x-msaccess',
67  'mid' => 'audio/midi',
68  'midi' => 'audio/midi',
69  'mov' => 'video/quicktime',
70  'mp3' => 'audio/mpeg',
71  'mp4' => 'video/mp4',
72  'mpeg' => 'video/mpeg',
73  'mpg' => 'video/mpeg',
74  'odg' => 'vnd.oasis.opendocument.graphics',
75  'odp' => 'vnd.oasis.opendocument.presentation',
76  'odt' => 'vnd.oasis.opendocument.text',
77  'ods' => 'vnd.oasis.opendocument.spreadsheet',
78  'ogg' => 'audio/ogg',
79  'pdf' => 'application/pdf',
80  'ppt' => 'application/vnd.ms-powerpoint',
81  'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
82  'ps' => 'application/postscript',
83  'rar' => 'application/x-rar-compressed',
84  'rtf' => 'application/rtf',
85  'tar' => 'application/x-tar',
86  'sit' => 'application/x-stuffit',
87  'svg' => 'image/svg+xml',
88  'tif' => 'image/tiff',
89  'tiff' => 'image/tiff',
90  'ttf' => 'application/x-font-truetype',
91  'vcf' => 'text/x-vcard',
92  'wav' => 'audio/wav',
93  'wma' => 'audio/x-ms-wma',
94  'wmv' => 'audio/x-ms-wmv',
95  'xls' => 'application/excel',
96  'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
97  'xml' => 'application/xml',
98  );
99 
100  // wrap array for generating file
101  foreach ($valid_mime_types_preset as $extension => $mime_type) {
102  // generate array for mimetype to extension resolver (only first match)
103  $valid_mime_types[$extension] = "'{$extension}' => '{$mime_type}'";
104  }
105 
106  // collect extensions
107  $valid_extensions = array();
108 
109  // all extensions from second match
110  foreach ($matches[2] as $i => $extensions) {
111  // explode multiple extensions from string
112  $extensions = explode(' ', strtolower($extensions));
113 
114  // force array for foreach
115  if (!is_array($extensions)) {
116  $extensions = array($extensions);
117  }
118 
119  foreach ($extensions as $extension) {
120  // get mime type
121  $mime_type = $matches[1][$i];
122 
123  // check if string length lower than 10
124  if (strlen($extension) < 10) {
125  // add extension
126  $valid_extensions[] = $extension;
127 
128  if (!isset($valid_mime_types[$mime_type])) {
129  // generate array for mimetype to extension resolver (only first match)
130  $valid_mime_types[$extension] = "'{$extension}' => '{$mime_type}'";
131  }
132  }
133  }
134  }
135  }
136 
137  $xml = simplexml_load_string($mime_xml);
138 
139  foreach ($xml as $node) {
140  // check if there is no pattern
141  if (!isset($node->glob['pattern'])) {
142  continue;
143  }
144 
145  // get all matching extensions from match
146  foreach ((array) $node->glob['pattern'] as $extension) {
147  // skip none glob extensions
148  if (strpos($extension, '.') === false) {
149  continue;
150  }
151 
152  // remove get only last part
153  $extension = explode('.', strtolower($extension));
154  $extension = end($extension);
155 
156  // maximum length in database column
157  if (strlen($extension) <= 9) {
158  $valid_extensions[] = $extension;
159  }
160  }
161 
162  if (isset($node->glob['pattern'][0])) {
163  // mime type
164  $mime_type = strtolower((string) $node['type']);
165 
166  // get first extension
167  $extension = strtolower(trim($node->glob['ddpattern'][0], '*.'));
168 
169  // skip none glob extensions and check if string length between 1 and 10
170  if (strpos($extension, '.') !== false || strlen($extension) < 1 || strlen($extension) > 9) {
171  continue;
172  }
173 
174  // check if string length lower than 10
175  if (!isset($valid_mime_types[$mime_type])) {
176  // generate array for mimetype to extension resolver (only first match)
177  $valid_mime_types[$extension] = "'{$extension}' => '{$mime_type}'";
178  }
179  }
180  }
181 
182  // full list of valid extensions only
183  $valid_mime_types = array_unique($valid_mime_types);
184  ksort($valid_mime_types);
185 
186  // combine mime types and extensions array
187  $output = "$preamble\$swift_mime_types = array(\n ".implode($valid_mime_types, ",\n ")."\n);";
188 
189  // write mime_types.php config file
190  @file_put_contents('./mime_types.php', $output);
191 }
192