You are here

function _support_get_message_body_part in Support Ticketing System 7

Same name and namespace in other branches
  1. 6 support.module \_support_get_message_body_part()
1 call to _support_get_message_body_part()
_support_get_message_body in ./support.module

File

./support.module, line 1916
support.module

Code

function _support_get_message_body_part($stream, $message, $mime_type, $structure = FALSE, $part = FALSE) {
  if (!$structure) {
    $structure = imap_fetchstructure($stream, $message);
  }
  if (!empty($structure)) {
    foreach ($structure->parameters as $parameter) {
      if (strtoupper($parameter->attribute) == 'CHARSET') {
        $encoding = mb_decode_mimeheader($parameter->value);
        break;
      }
    }
    if ($structure->type == TYPEMULTIPART) {
      $prefix = '';
      while (list($index, $sub_structure) = each($structure->parts)) {
        if ($part) {
          $prefix = $part . '.';
        }
        $mime_type = _support_get_filemime($sub_structure);
        $data = _support_get_message_body_part($stream, $message, $mime_type, $sub_structure, $prefix . ($index + 1));
        if ($data) {
          return $data;
        }
      }
    }
    else {
      if ($mime_type == _support_get_filemime($structure)) {
        if (!$part) {
          $part = 1;
        }
        $body = imap_fetchbody($stream, $message, $part);
        switch ($structure->encoding) {
          case ENCBASE64:
            return drupal_convert_to_utf8(imap_base64($body), $encoding);
            break;
          case ENCQUOTEDPRINTABLE:
            return drupal_convert_to_utf8(quoted_printable_decode($body), $encoding);
            break;
          default:
            return drupal_convert_to_utf8($body, $encoding);
            break;
        }
      }
    }
  }
}