swiftmailer_generate_mimes_config.php 7.9 KB

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