/** * Performs the actual encoding conversion. * * @param string $data * The data to convert. * @param string $source_encoding * The detected encoding. * * @return string * The encoded string. */ protected function doConvert($data, $source_encoding) { if (in_array(strtolower($source_encoding), self::$utf8Compatible)) { return $data; } $converted = drupal_convert_to_utf8($data, $source_encoding); if ($converted === FALSE) { return $data; } return $converted; }
/** * Returns the first part with the specified mime_type * * USAGE EXAMPLES - from php manual: imap_fetch_structure() comments * $data = get_part($stream, $msg_number, "TEXT/PLAIN"); // get plain text * $data = get_part($stream, $msg_number, "TEXT/HTML"); // get HTML text */ function get_part($stream, $msg_number, $mime_type, $structure = FALSE, $part_number = FALSE, $encoding) { if (!$structure) { $structure = imap_fetchstructure($stream, $msg_number); } if ($structure) { foreach ($structure->parameters as $parameter) { if (drupal_strtoupper($parameter->attribute) == 'CHARSET') { $encoding = $parameter->value; } } if ($mime_type == $this->get_mime_type($structure)) { if (!$part_number) { $part_number = '1'; } $text = imap_fetchbody($stream, $msg_number, $part_number, FT_PEEK); if ($structure->encoding == ENCBASE64) { return drupal_convert_to_utf8(imap_base64($text), $encoding); } elseif ($structure->encoding == ENCQUOTEDPRINTABLE) { return drupal_convert_to_utf8(quoted_printable_decode($text), $encoding); } else { return drupal_convert_to_utf8($text, $encoding); } } if ($structure->type == TYPEMULTIPART) { /* multipart */ $prefix = ''; while (list($index, $sub_structure) = each($structure->parts)) { if ($part_number) { $prefix = $part_number . '.'; } $data = $this->get_part($stream, $msg_number, $mime_type, $sub_structure, $prefix . ($index + 1), $encoding); if ($data) { return $data; } } } } return FALSE; }
/** * Gets a message part and adds it to $parts. * * @param object $stream * The IMAP stream from which to fetch data. * @param int $msg_number * The message number. * @param object $structure * The structure definition. * @param int $part_number * The part number to add. * @param array $parts * The array of added parts. */ public function get_part($stream, $msg_number, $structure, $part_number, &$parts) { // Get data. $data = ''; if ($part_number) { $data = imap_fetchbody($stream, $msg_number, $part_number, FT_PEEK); } else { $data = imap_body($stream, $msg_number, FT_PEEK); } // Get params. $params = array(); if ($structure->ifparameters) { foreach ($structure->parameters as $parameter) { $params[drupal_strtolower($parameter->attribute)] = $parameter->value; } } if ($structure->ifdparameters) { foreach ($structure->dparameters as $parameter) { $params[drupal_strtolower($parameter->attribute)] = $parameter->value; } } $charset = 'auto'; if (isset($params['charset'])) { $charset = $params['charset']; } // Decode data. switch ($structure->encoding) { case ENCQUOTEDPRINTABLE: $data = quoted_printable_decode($data); // Properly decode most Microsoft encodings (Hotmail and Windows). $data = drupal_convert_to_utf8($data, $charset); break; case ENCBASE64: $data = base64_decode($data); break; case ENC7BIT: case ENC8BIT: $data = imap_utf8($data); // Properly decode most Microsoft encodings (Hotmail and Windows). $data = drupal_convert_to_utf8($data, $charset); break; } // Get attachments. foreach ($params as $attribute => $value) { switch ($attribute) { case 'filename': case 'name': $attachment = new stdClass(); $attachment->filename = $value; $attachment->data = $data; if (isset($structure->id)) { $attachment->id = $structure->id; } if (!($attachment->filemime = $this->get_mime_type($structure))) { mailhandler_report('warning', 'Could not fetch mime type for message part. Defaulting to application/octet-stream.'); $attachment->filemime = 'application/octet-stream'; } $parts['attachments'][] = $attachment; break 2; } } // Get bodies. if (($structure->type == TYPETEXT || $structure->type == TYPEMESSAGE) && $data) { // Messages may be split in different parts because of inline attachments, // so append parts together with blank row. if (drupal_strtolower($structure->subtype) == 'plain') { $parts['text_body'] .= trim($data); } else { $parts['html_body'] .= $data; } } // Recurse for multi-part messages. if ($structure->type = TYPEMULTIPART && isset($structure->parts)) { foreach ($structure->parts as $index => $sub_structure) { $this->get_part($stream, $msg_number, $sub_structure, $part_number . '.' . ($index + 1), $parts); } } }