swiftmailer_generate_mimes_config.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #!/usr/bin/php
  2. <?php
  3. \define('APACHE_MIME_TYPES_URL', 'https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types');
  4. \define('FREEDESKTOP_XML_URL', 'https://raw.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 https://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 (false !== preg_match_all('/^#?([a-z0-9\-\+\/\.]+)[\t]+(.*)$/miu', $mime_types, $matches)) {
  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. 'zip' => 'application/zip',
  34. 'gif' => 'image/gif',
  35. 'png' => 'image/png',
  36. 'css' => 'text/css',
  37. 'js' => 'text/javascript',
  38. 'txt' => 'text/plain',
  39. 'aif' => 'audio/x-aiff',
  40. 'aiff' => 'audio/x-aiff',
  41. 'avi' => 'video/avi',
  42. 'bmp' => 'image/bmp',
  43. 'bz2' => 'application/x-bz2',
  44. 'csv' => 'text/csv',
  45. 'dmg' => 'application/x-apple-diskimage',
  46. 'doc' => 'application/msword',
  47. 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  48. 'eml' => 'message/rfc822',
  49. 'aps' => 'application/postscript',
  50. 'exe' => 'application/x-ms-dos-executable',
  51. 'flv' => 'video/x-flv',
  52. 'gz' => 'application/x-gzip',
  53. 'hqx' => 'application/stuffit',
  54. 'htm' => 'text/html',
  55. 'html' => 'text/html',
  56. 'jar' => 'application/x-java-archive',
  57. 'jpeg' => 'image/jpeg',
  58. 'jpg' => 'image/jpeg',
  59. 'm3u' => 'audio/x-mpegurl',
  60. 'm4a' => 'audio/mp4',
  61. 'mdb' => 'application/x-msaccess',
  62. 'mid' => 'audio/midi',
  63. 'midi' => 'audio/midi',
  64. 'mov' => 'video/quicktime',
  65. 'mp3' => 'audio/mpeg',
  66. 'mp4' => 'video/mp4',
  67. 'mpeg' => 'video/mpeg',
  68. 'mpg' => 'video/mpeg',
  69. 'odg' => 'vnd.oasis.opendocument.graphics',
  70. 'odp' => 'vnd.oasis.opendocument.presentation',
  71. 'odt' => 'vnd.oasis.opendocument.text',
  72. 'ods' => 'vnd.oasis.opendocument.spreadsheet',
  73. 'ogg' => 'audio/ogg',
  74. 'pdf' => 'application/pdf',
  75. 'ppt' => 'application/vnd.ms-powerpoint',
  76. 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  77. 'ps' => 'application/postscript',
  78. 'rar' => 'application/x-rar-compressed',
  79. 'rtf' => 'application/rtf',
  80. 'tar' => 'application/x-tar',
  81. 'sit' => 'application/x-stuffit',
  82. 'svg' => 'image/svg+xml',
  83. 'tif' => 'image/tiff',
  84. 'tiff' => 'image/tiff',
  85. 'ttf' => 'application/x-font-truetype',
  86. 'vcf' => 'text/x-vcard',
  87. 'wav' => 'audio/wav',
  88. 'wma' => 'audio/x-ms-wma',
  89. 'wmv' => 'audio/x-ms-wmv',
  90. 'xls' => 'application/vnd.ms-excel',
  91. 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  92. 'xml' => 'application/xml',
  93. ];
  94. // wrap array for generating file
  95. foreach ($valid_mime_types_preset as $extension => $mime_type) {
  96. // generate array for mimetype to extension resolver (only first match)
  97. $valid_mime_types[$extension] = "'{$extension}' => '{$mime_type}'";
  98. }
  99. // all extensions from second match
  100. foreach ($matches[2] as $i => $extensions) {
  101. // explode multiple extensions from string
  102. $extensions = explode(' ', strtolower($extensions ?? ''));
  103. // force array for foreach
  104. if (!\is_array($extensions)) {
  105. $extensions = [$extensions];
  106. }
  107. foreach ($extensions as $extension) {
  108. // get mime type
  109. $mime_type = $matches[1][$i];
  110. // check if string length lower than 10
  111. if (\strlen($extension) < 10) {
  112. if (!isset($valid_mime_types[$mime_type])) {
  113. // generate array for mimetype to extension resolver (only first match)
  114. $valid_mime_types[$extension] = "'{$extension}' => '{$mime_type}'";
  115. }
  116. }
  117. }
  118. }
  119. }
  120. $xml = simplexml_load_string($mime_xml);
  121. foreach ($xml as $node) {
  122. // check if there is no pattern
  123. if (!isset($node->glob['pattern'])) {
  124. continue;
  125. }
  126. // get all matching extensions from match
  127. foreach ((array) $node->glob['pattern'] as $extension) {
  128. // skip none glob extensions
  129. if (false === strpos($extension ?? '', '.')) {
  130. continue;
  131. }
  132. // remove get only last part
  133. $extension = explode('.', strtolower($extension ?? ''));
  134. $extension = end($extension);
  135. }
  136. if (isset($node->glob['pattern'][0])) {
  137. // mime type
  138. $mime_type = strtolower((string) $node['type'] ?? '');
  139. // get first extension
  140. $extension = strtolower(trim($node->glob['ddpattern'][0] ?? '', '*.'));
  141. // skip none glob extensions and check if string length between 1 and 10
  142. if (false !== strpos($extension, '.') || \strlen($extension) < 1 || \strlen($extension) > 9) {
  143. continue;
  144. }
  145. // check if string length lower than 10
  146. if (!isset($valid_mime_types[$mime_type])) {
  147. // generate array for mimetype to extension resolver (only first match)
  148. $valid_mime_types[$extension] = "'{$extension}' => '{$mime_type}'";
  149. }
  150. }
  151. }
  152. // full list of valid extensions only
  153. $valid_mime_types = array_unique($valid_mime_types);
  154. ksort($valid_mime_types);
  155. // combine mime types and extensions array
  156. $output = "$preamble\$swift_mime_types = array(\n ".implode(",\n ", $valid_mime_types)."\n);";
  157. // write mime_types.php config file
  158. @file_put_contents('./mime_types.php', $output);
  159. }
  160. generateUpToDateMimeArray();