mimemail.incoming.inc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * @file
  4. * Functions that handle inbound messages to mimemail.
  5. */
  6. /**
  7. * Receive messages POSTed from an external source.
  8. *
  9. * This function enables messages to be sent via POST or some other RFC822
  10. * source input (e.g. directly from a mail server).
  11. *
  12. * @return
  13. * The POSTed message.
  14. */
  15. function mimemail_post() {
  16. if (!isset($_POST['token']) || empty($_POST['token'])) {
  17. return drupal_access_denied();
  18. }
  19. if (isset($_POST['message']) && !empty($_POST['message'])) {
  20. $key = variable_get('mimemail_key', drupal_random_key());
  21. $hash = hash_hmac('sha1', $_POST['message'], $key);
  22. if ($hash != $_POST['token']) {
  23. watchdog('access denied', 'Authentication error for POST e-mail', WATCHDOG_WARNING);
  24. return drupal_access_denied();
  25. }
  26. else {
  27. return mimemail_incoming($_POST['message']);
  28. }
  29. }
  30. return drupal_access_denied();
  31. }
  32. /**
  33. * Parses an externally received message.
  34. *
  35. * @param $message
  36. * The message to parse.
  37. */
  38. function mimemail_incoming($message) {
  39. $mail = mimemail_parse($message);
  40. foreach (module_implements('mimemail_incoming_alter') as $module) {
  41. call_user_func_array($module . '_mimemail_incoming_alter', $mail);
  42. }
  43. module_invoke_all('mimemail_incoming', $mail);
  44. }
  45. /**
  46. * Parses a message into its parts.
  47. *
  48. * @param string $message
  49. * The message to parse.
  50. *
  51. * @return array
  52. * The parts of the message.
  53. */
  54. function mimemail_parse($message) {
  55. // Provides a "headers", "content-type" and "body" element.
  56. $mail = mimemail_parse_headers($message);
  57. // Get an address-only version of "From" (useful for user_load() and such).
  58. $mail['from'] = preg_replace('/.*\b([a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4})\b.*/i', '\1', drupal_strtolower($mail['headers']['From']));
  59. // Get a subject line, which may be cleaned up/modified later.
  60. $mail['subject'] = $mail['headers']['Subject'];
  61. // Make an array to hold any non-content attachments.
  62. $mail['attachments'] = array();
  63. // We're dealing with a multi-part message.
  64. $mail['parts'] = mimemail_parse_boundary($mail);
  65. foreach ($mail['parts'] as $i => $part_body) {
  66. $part = mimemail_parse_headers($part_body);
  67. $sub_parts = mimemail_parse_boundary($part);
  68. // Content is encoded in a multipart/alternative section.
  69. if (count($sub_parts) > 1) {
  70. foreach ($sub_parts as $j => $sub_part_body) {
  71. $sub_part = mimemail_parse_headers($sub_part_body);
  72. if ($sub_part['content-type'] == 'text/plain') {
  73. $mail['text'] = mimemail_parse_content($sub_part);
  74. }
  75. if ($sub_part['content-type'] == 'text/html') {
  76. $mail['html'] = mimemail_parse_content($sub_part);
  77. }
  78. else {
  79. $mail['attachments'][] = mimemail_parse_attachment($sub_part);
  80. }
  81. }
  82. }
  83. if (($part['content-type'] == 'text/plain') && !isset($mail['text'])) {
  84. $mail['text'] = mimemail_parse_content($part);
  85. }
  86. elseif (($part['content-type'] == 'text/html') && !isset($mail['html'])) {
  87. $mail['html'] = mimemail_parse_content($part);
  88. }
  89. else {
  90. $mail['attachments'][] = mimemail_parse_attachment($part);
  91. }
  92. }
  93. // Make sure our text and html parts are accounted for.
  94. if (isset($mail['html']) && !isset($mail['text'])) {
  95. $mail['text'] = preg_replace('|<style.*</style>|mis', '', $mail['html']);
  96. $mail['text'] = drupal_html_to_text($mail['text']);
  97. }
  98. elseif (isset($mail['text']) && !isset($mail['html'])) {
  99. $mail['html'] = check_markup($mail['text'], variable_get('mimemail_format', filter_fallback_format()));
  100. }
  101. // Last ditch attempt - use the body as-is.
  102. if (!isset($mail['text'])) {
  103. $mail['text'] = mimemail_parse_content($mail);
  104. $mail['html'] = check_markup($mail['text'], variable_get('mimemail_format', filter_fallback_format()));
  105. }
  106. return $mail;
  107. }
  108. /**
  109. * Split a multi-part message using MIME boundaries.
  110. */
  111. function mimemail_parse_boundary($part) {
  112. $m = array();
  113. if (preg_match('/.*boundary="?([^";]+)"?.*/', $part['headers']['Content-Type'], $m)) {
  114. $boundary = "\n--" . $m[1];
  115. $body = str_replace("$boundary--", '', $part['body']);
  116. return array_slice(explode($boundary, $body), 1);
  117. }
  118. return array($part['body']);
  119. }
  120. /**
  121. * Split a message (or message part) into its headers and body section.
  122. */
  123. function mimemail_parse_headers($message) {
  124. // Split out body and headers.
  125. if (preg_match("/^(.*?)\r?\n\r?\n(.*)/s", $message, $match)) {
  126. list($hdr, $body) = array($match[1], $match[2]);
  127. }
  128. // Un-fold the headers.
  129. $hdr = preg_replace(array("/\r/", "/\n(\t| )+/"), array('', ' '), $hdr);
  130. $headers = array();
  131. foreach (explode("\n", trim($hdr)) as $row) {
  132. $split = strpos($row, ':');
  133. $name = trim(drupal_substr($row, 0, $split));
  134. $val = trim(drupal_substr($row, $split+1));
  135. $headers[$name] = $val;
  136. }
  137. $type = (preg_replace('/\s*([^;]+).*/', '\1', $headers['Content-Type']));
  138. return array('headers' => $headers, 'body' => $body, 'content-type' => $type);
  139. }
  140. /**
  141. * Return a decoded MIME part in UTF-8.
  142. */
  143. function mimemail_parse_content($part) {
  144. $content = $part['body'];
  145. // Decode this part.
  146. if ($encoding = drupal_strtolower($part['headers']['Content-Transfer-Encoding'])) {
  147. switch ($encoding) {
  148. case 'base64':
  149. $content = base64_decode($content);
  150. break;
  151. case 'quoted-printable':
  152. $content = quoted_printable_decode($content);
  153. break;
  154. // 7bit is the RFC default.
  155. case '7bit':
  156. break;
  157. }
  158. }
  159. // Try to convert character set to UTF-8.
  160. if (preg_match('/.*charset="?([^";]+)"?.*/', $part['headers']['Content-Type'], $m)) {
  161. $content = drupal_convert_to_utf8($content, $m[1]);
  162. }
  163. return $content;
  164. }
  165. /**
  166. * Convert a MIME part into a file array.
  167. */
  168. function mimemail_parse_attachment($part) {
  169. $m = array();
  170. if (preg_match('/.*filename="?([^";])"?.*/', $part['headers']['Content-Disposition'], $m)) {
  171. $name = $m[1];
  172. }
  173. elseif (preg_match('/.*name="?([^";])"?.*/', $part['headers']['Content-Type'], $m)) {
  174. $name = $m[1];
  175. }
  176. return array(
  177. 'filename' => $name,
  178. 'filemime' => $part['content-type'],
  179. 'content' => mimemail_parse_content($part),
  180. );
  181. }