/** * Takes an address object, as returned by imap_header() for example, and * formats it as a string. * * Object format for the address "John Doe <*****@*****.**>" is: * <pre> * $object->personal = Personal name ("John Doe") * $object->mailbox = The user's mailbox ("john_doe") * $object->host = The host the mailbox is on ("example.com") * </pre> * * @param stdClass $ob The address object to be turned into a string. * @param mixed $filter A user@example.com style bare address to ignore. * Either single string or an array of strings. If * the address matches $filter, an empty string will * be returned. * * @return string The formatted address (Example: John Doe * <*****@*****.**>). */ public function addrObject2String($ob, $filter = '') { /* If the personal name is set, decode it. */ $ob->personal = isset($ob->personal) ? Horde_MIME::decode($ob->personal) : ''; /* If both the mailbox and the host are empty, return an empty string. If we just let this case fall through, the call to MIME::rfc822WriteAddress() will end up return just a '@', which is undesirable. */ if (empty($ob->mailbox) && empty($ob->host)) { return ''; } /* Make sure these two variables have some sort of value. */ if (!isset($ob->mailbox)) { $ob->mailbox = ''; } elseif ($ob->mailbox == 'undisclosed-recipients') { return ''; } if (!isset($ob->host)) { $ob->host = ''; } /* Filter out unwanted addresses based on the $filter string. */ if ($filter) { if (!is_array($filter)) { $filter = array($filter); } foreach ($filter as $f) { if (strcasecmp($f, $ob->mailbox . '@' . $ob->host) == 0) { return ''; } } } /* Return the trimmed, formatted email address. */ return Horde_MIME::trimEmailAddress(Horde_MIME::rfc822WriteAddress($ob->mailbox, $ob->host, $ob->personal)); }
/** * Builds an array consisting of MIME header/value pairs. * * @param string $headers A text string containing the headers (e.g. * output from imap_fetchheader()). * @param boolean $decode Should the headers be decoded? * @param boolean $lowercase Should the keys be in lowercase? * * @return array An array consisting of the header name as the key and * the header value as the value. * A header with multiple entries will be stored in * 'value' as an array. */ public static function parseMIMEHeaders($headers, $decode = true, $lowercase = false) { $header = $headval = ''; $ob = $toprocess = array(); foreach (explode("\n", $headers) as $val) { $val = rtrim($val); if (preg_match("/^([^\\s]+)\\:\\s*(.*)/", $val, $matches)) { if (!empty($header)) { $toprocess[] = array($header, $headval); } $header = $matches[1]; $headval = $matches[2]; } else { $val = ltrim($val); if ($val) { $headval .= ' ' . ltrim($val); } else { break; } } } if (!empty($header)) { $toprocess[] = array($header, $headval); } foreach ($toprocess as $val) { if ($decode) { // Fields defined in RFC 2822 that contain address information if (in_array(Horde_String::lower($val[0]), array('from', 'to', 'cc', 'bcc', 'reply-to', 'resent-to', 'resent-cc', 'resent-bcc', 'resent-from', 'sender'))) { $val[1] = Horde_MIME::decodeAddrString($val[1]); } else { $val[1] = Horde_MIME::decode($val[1]); } } if (isset($ob[$val[0]])) { if (!is_array($ob[$val[0]])) { $temp = $ob[$val[0]]; $ob[$val[0]] = array(); $ob[$val[0]][] = $temp; } $ob[$val[0]][] = $val[1]; } else { $ob[$val[0]] = $val[1]; } } return $lowercase ? array_change_key_case($ob, CASE_LOWER) : $ob; }
/** * Get the description of this part. * * @param boolean $decode MIME decode description? * @param boolean $default If the name parameter doesn't exist, should we * use the default name from the description * parameter? * * @return string The description of this part. */ public function getDescription($decode = false, $default = false) { $desc = $this->_description; if ($default && empty($desc)) { $desc = $this->getName(); if (empty($desc)) { $desc = MIME_DEFAULT_DESCRIPTION; } } if ($decode) { return Horde_MIME::decode($desc); } else { return $desc; } }