PageRenderTime 45ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/swiftmailer/swiftmailer/lib/swiftmailer_generate_mimes_config.php

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