Get a content type parameter from this part.
public getContentTypeParameter ( string $label ) : string | ||
$label | string | The content type parameter label. |
return | string | The data requested. Returns null if $label is not set. |
/** * Constructor. * * @param Horde_Mime_Part $mime_part A MIME part object. Must be of * type multipart/related. */ public function __construct(Horde_Mime_Part $mime_part) { if ($mime_part->getType() != 'multipart/related') { throw new InvalidArgumentException('MIME part must be of type multipart/related'); } $ids = array_keys($mime_part->contentTypeMap()); $related_id = $mime_part->getMimeId(); $id = null; /* Build a list of parts -> CIDs. */ foreach ($ids as $val) { if (strcmp($related_id, $val) !== 0 && ($cid = $mime_part->getPart($val)->getContentId())) { $this->_cids[$val] = $cid; } } /* Look at the 'start' parameter to determine which part to start * with. If no 'start' parameter, use the first part (RFC 2387 * [3.1]). */ $start = $mime_part->getContentTypeParameter('start'); if (!empty($start)) { $id = $this->cidSearch($start); } if (empty($id)) { reset($ids); $id = next($ids); } $this->_start = $id; }
/** * Return the body text of the original email from a smart request. * * @param array $body_data The body data array of the source msg. * @param Horde_Mime_Part $part The body mime part of the email to send. * @param boolean $html Do we want an html body? * @param boolean $flow Should the body be flowed? * * @return string The properly formatted/flowed message body. */ protected function _msgBody(array $body_data, Horde_Mime_Part $part, $html, $flow = false) { $subtype = $html == true ? 'html' : 'plain'; $msg = Horde_String::convertCharset($body_data[$subtype]['body'], $body_data[$subtype]['charset'], 'UTF-8'); trim($msg); if (!$html) { if ($part->getContentTypeParameter('format') == 'flowed') { $flowed = new Horde_Text_Flowed($msg, 'UTF-8'); if (Horde_String::lower($part->getContentTypeParameter('delsp')) == 'yes') { $flowed->setDelSp(true); } $flowed->setMaxLength(0); $msg = $flowed->toFixed(false); } else { // If not flowed, remove padding at eol $msg = preg_replace("/\\s*\n/U", "\n", $msg); } if ($flow) { $flowed = new Horde_Text_Flowed($msg, 'UTF-8'); $msg = $flowed->toFlowed(true); } } return $msg; }
/** * Creates a MIME object from the text of one part of a MIME message. * * @param string $header The header text. * @param string $body The body text. * @param array $opts Additional options: * <pre> * - ctype: (string) The default content-type. * - forcemime: (boolean) If true, the message data is assumed to be * MIME data. If not, a MIME-Version header must exist to * be parsed as a MIME message. * - level: (integer) Current nesting level. * - no_body: (boolean) If true, don't set body contents of parts. * </pre> * * @return Horde_Mime_Part The MIME part object. */ protected static function _getStructure($header, $body, array $opts = array()) { $opts = array_merge(array('ctype' => 'text/plain', 'forcemime' => false, 'level' => 0, 'no_body' => false), $opts); /* Parse headers text into a Horde_Mime_Headers object. */ $hdrs = Horde_Mime_Headers::parseHeaders($header); $ob = new Horde_Mime_Part(); /* This is not a MIME message. */ if (!$opts['forcemime'] && !isset($hdrs['MIME-Version'])) { $ob->setType('text/plain'); if ($len = strlen($body)) { if ($opts['no_body']) { $ob->setBytes($len); } else { $ob->setContents($body); } } return $ob; } /* Content type. */ if ($tmp = $hdrs['Content-Type']) { $ob->setType($tmp->value); foreach ($tmp->params as $key => $val) { $ob->setContentTypeParameter($key, $val); } } else { $ob->setType($opts['ctype']); } /* Content transfer encoding. */ if ($tmp = $hdrs['Content-Transfer-Encoding']) { $ob->setTransferEncoding(strval($tmp)); } /* Content-Description. */ if ($tmp = $hdrs['Content-Description']) { $ob->setDescription(strval($tmp)); } /* Content-Disposition. */ if ($tmp = $hdrs['Content-Disposition']) { $ob->setDisposition($tmp->value); foreach ($tmp->params as $key => $val) { $ob->setDispositionParameter($key, $val); } } /* Content-Duration */ if ($tmp = $hdrs['Content-Duration']) { $ob->setDuration(strval($tmp)); } /* Content-ID. */ if ($tmp = $hdrs['Content-Id']) { $ob->setContentId(strval($tmp)); } if (($len = strlen($body)) && $ob->getPrimaryType() != 'multipart') { if ($opts['no_body']) { $ob->setBytes($len); } else { $ob->setContents($body); } } if (++$opts['level'] >= self::NESTING_LIMIT) { return $ob; } /* Process subparts. */ switch ($ob->getPrimaryType()) { case 'message': if ($ob->getSubType() == 'rfc822') { $ob[] = self::parseMessage($body, array('forcemime' => true, 'no_body' => $opts['no_body'])); } break; case 'multipart': $boundary = $ob->getContentTypeParameter('boundary'); if (!is_null($boundary)) { foreach (self::_findBoundary($body, 0, $boundary) as $val) { if (!isset($val['length'])) { break; } $subpart = substr($body, $val['start'], $val['length']); $hdr_pos = self::_findHeader($subpart, self::EOL); $ob[] = self::_getStructure(substr($subpart, 0, $hdr_pos), substr($subpart, $hdr_pos + 2), array('ctype' => $ob->getSubType() == 'digest' ? 'message/rfc822' : 'text/plain', 'forcemime' => true, 'level' => $opts['level'], 'no_body' => $opts['no_body'])); } } break; } return $ob; }
public function testNullCharactersNotAllowedInMimeHeaderData() { $part = new Horde_Mime_Part(); $part->setType("text/plain"); $this->assertEquals('text/plain', $part->getType()); $part->setDisposition("inline"); $this->assertEquals('inline', $part->getDisposition()); $part->setDispositionParameter('size', '123' . "" . '456'); $this->assertEquals(123456, $part->getDispositionParameter('size')); $part->setDispositionParameter('foo', "foobar"); $this->assertEquals('foobar', $part->getDispositionParameter('foo')); $part->setCharset("utf-8"); $this->assertEquals('utf-8', $part->getCharset()); $part->setName("foobar"); $this->assertEquals('foobar', $part->getName()); $this->assertEquals('foobar', $part->getDispositionParameter('filename')); $this->assertEquals('foobar', $part->getContentTypeParameter('name')); $part->setLanguage("en"); $this->assertEquals(array('en'), $part->getLanguage()); $part->setLanguage(array("en", "de")); $this->assertEquals(array('en', 'de'), $part->getLanguage()); $part->setDuration('123' . "" . '456'); $this->assertEquals(123456, $part->getDuration()); $part->setBytes('123' . "" . '456'); $this->assertEquals(123456, $part->getBytes()); $part->setDescription("foobar"); $this->assertEquals('foobar', $part->getDescription()); $part->setContentTypeParameter('foo', "foobar"); $this->assertEquals('foobar', $part->getContentTypeParameter('foo')); $part->setContentId("foobar"); $this->assertEquals('foobar', $part->getContentId()); }
/** * Determines the S/MIME type of a part. Uses the smime-type content * parameter (if it exists), and falls back to ASN.1 parsing of data if * it doesn't exist. * * @param Horde_Mime_Part $part MIME part with S/MIME data. * * @return string 'signed-data', 'enveloped-data', or null. */ protected function _getSmimeType(Horde_Mime_Part $part) { if ($type = $part->getContentTypeParameter('smime-type')) { return strtolower($type); } if (!class_exists('File_ASN1')) { return null; } $asn1 = new File_ASN1(); $decoded = $asn1->decodeBER($part->getContents()); foreach ($decoded as $val) { if ($val['type'] == FILE_ASN1_TYPE_SEQUENCE) { foreach ($val['content'] as $val2) { if ($val2['type'] == FILE_ASN1_TYPE_OBJECT_IDENTIFIER) { /* ASN.1 values from STD 70/RFC 5652 - CMS syntax */ switch ($val2['content']) { case '1.2.840.113549.1.7.2': return 'signed-data'; case '1.2.840.113549.1.7.3': return 'enveloped-data'; default: // Other types not supported as of now. return null; } } } } } return null; }
/** * Return the body text of the original email from a smart request. * * @param array $body_data The body data array of the source msg. * @param Horde_Mime_Part $part The body mime part of the email to send. * @param boolean $html Do we want an html body? * @param boolean $flow Should the body be flowed? * * @return string The properly formatted/flowed message body. */ protected function _msgBody(array $body_data, Horde_Mime_Part $part, $html, $flow = false) { $subtype = $html == true ? 'html' : 'plain'; $msg = Horde_String::convertCharset((string) $body_data[$subtype]['body'], $body_data[$subtype]['charset'], 'UTF-8'); if (!$html) { if ($part->getContentTypeParameter('format') == 'flowed') { $flowed = new Horde_Text_Flowed($msg, 'UTF-8'); if (Horde_String::lower($part->getContentTypeParameter('delsp')) == 'yes') { $flowed->setDelSp(true); } $flowed->setMaxLength(0); $msg = $flowed->toFixed(false); } else { // If not flowed, remove padding at eol $msg = preg_replace("/\\s*\n/U", "\n", $msg); } if ($flow) { $flowed = new Horde_Text_Flowed($msg, 'UTF-8'); $msg = $flowed->toFlowed(true); } } else { // This filter requires the tidy extenstion. if (Horde_Util::extensionExists('tidy')) { return Horde_Text_Filter::filter($msg, 'Cleanhtml', array('body_only' => true)); } else { // If no tidy, use Horde_Dom. $dom = new Horde_Domhtml($msg, 'UTF-8'); return $dom->returnBody(); } } return $msg; }