35 'multipart/mixed' => array(self::LEVEL_TOP, self::LEVEL_MIXED),
36 'multipart/alternative' => array(self::LEVEL_MIXED, self::LEVEL_ALTERNATIVE),
37 'multipart/related' => array(self::LEVEL_ALTERNATIVE, self::LEVEL_RELATED),
62 'multipart/related' => 3,
83 $this->_cacheKey = md5(uniqid(getmypid().mt_rand(),
true));
84 $this->_cache = $cache;
85 $this->_headers = $headers;
86 $this->_grammar = $grammar;
88 $this->_headers->defineOrdering(array(
'Content-Type',
'Content-Transfer-Encoding'));
103 $this->_compoundLevelFilters = array(
104 (self::LEVEL_ALTERNATIVE + self::LEVEL_RELATED) => array(
105 self::LEVEL_ALTERNATIVE => array(
106 'text/plain' => self::LEVEL_ALTERNATIVE,
107 'text/html' => self::LEVEL_RELATED,
171 $this->_userContentType = $type;
200 $this->_headers->addIdHeader($this->
_getIdField(), $id);
231 $this->_headers->addTextHeader(
'Content-Description', $description);
258 $this->_maxLineLength = $length;
281 public function setChildren(array $children, $compoundLevel = null)
285 $compoundLevel = isset($compoundLevel)
290 $immediateChildren = array();
291 $grandchildren = array();
294 foreach ($children as $child) {
296 if (empty($immediateChildren)) {
298 $immediateChildren = array($child);
301 if ($nextLevel == $level) {
302 $immediateChildren[] = $child;
303 }
elseif ($level < $nextLevel) {
305 $grandchildren = array_merge($grandchildren, $immediateChildren);
307 $immediateChildren = array($child);
309 $grandchildren[] = $child;
314 if (!empty($immediateChildren)) {
319 foreach ($this->_compositeRanges as $mediaType => $range) {
320 if ($lowestLevel > $range[0]
321 && $lowestLevel <= $range[1]) {
322 $newContentType = $mediaType;
328 if (!empty($grandchildren)) {
330 $subentity->_setNestingLevel($lowestLevel);
331 $subentity->setChildren($grandchildren, $compoundLevel);
332 array_unshift($immediateChildren, $subentity);
336 $this->_immediateChildren = $immediateChildren;
337 $this->_children = $children;
366 public function setBody($body, $contentType = null)
368 if ($body !== $this->_body) {
372 $this->_body = $body;
373 if (isset($contentType)) {
399 if ($encoder !== $this->_encoder) {
403 $this->_encoder = $encoder;
417 if (!isset($this->_boundary)) {
418 $this->_boundary =
'_=_swift_v4_'.time().
'_'.md5(getmypid().mt_rand().uniqid(
'',
true)).
'_=_';
436 $this->_boundary = $boundary;
470 $string = $this->_headers->toString();
485 if (isset($this->_body) && empty($this->_immediateChildren)) {
486 if ($this->_cache->hasKey($this->_cacheKey,
'body')) {
487 $body = $this->_cache->getString($this->_cacheKey,
'body');
489 $body =
"\r\n".$this->_encoder->encodeString($this->
getBody(), 0,
492 $this->_cache->setString($this->_cacheKey,
'body', $body,
499 if (!empty($this->_immediateChildren)) {
500 foreach ($this->_immediateChildren as $child) {
501 $string .=
"\r\n\r\n--".$this->getBoundary().
"\r\n";
502 $string .= $child->toString();
504 $string .=
"\r\n\r\n--".$this->getBoundary().
"--\r\n";
529 $is->
write($this->_headers->toString());
542 if (empty($this->_immediateChildren)) {
543 if (isset($this->_body)) {
544 if ($this->_cache->hasKey($this->_cacheKey,
'body')) {
545 $this->_cache->exportToByteStream($this->_cacheKey,
'body', $is);
547 $cacheIs = $this->_cache->getInputByteStream($this->_cacheKey,
'body');
555 $this->_body->setReadPointer(0);
557 $this->_encoder->encodeByteStream($this->_body, $is, 0, $this->
getMaxLineLength());
569 if (!empty($this->_immediateChildren)) {
570 foreach ($this->_immediateChildren as $child) {
572 $child->toByteStream($is);
591 if ($this->_headers->has($field)) {
592 return $this->_headers->get($field)->getFieldBodyModel();
601 if ($this->_headers->has($field)) {
602 $this->_headers->get($field)->setFieldBodyModel($model);
615 if ($this->_headers->has($field)) {
616 return $this->_headers->get($field)->getParameter($parameter);
625 if ($this->_headers->has($field)) {
626 $this->_headers->get($field)->setParameter($parameter, $value);
639 if (count($this->_immediateChildren)) {
643 $this->_headers->remove(
'Content-Transfer-Encoding');
675 $this->_cache->clearKey($this->_cacheKey,
'body');
685 $idLeft = md5(getmypid().
'.'.time().
'.'.uniqid(mt_rand(),
true));
686 $idRight = !empty($_SERVER[
'SERVER_NAME']) ? $_SERVER[
'SERVER_NAME'] :
'swift.generated';
687 $id = $idLeft.
'@'.$idRight;
692 $id = $idLeft.
'@swift.generated';
701 while (
false !== $bytes = $os->
read(8192)) {
713 $this->_headers->addTextHeader(
'Content-Transfer-Encoding', $encoding);
720 '/^[a-z0-9\'\(\)\+_\-,\.\/:=\?\ ]{0,69}[a-z0-9\'\(\)\+_\-,\.\/:=\?]$/Di',
729 $this->_headers->addParameterizedHeader(
'Content-Type', $type);
735 $this->_nestingLevel = $level;
741 foreach ($children as $child) {
742 $level |= $child->getNestingLevel();
751 foreach ($this->_compoundLevelFilters as $bitmask => $rules) {
752 if (($compoundLevel & $bitmask) === $bitmask) {
753 $filter = $rules + $filter;
757 $realLevel = $child->getNestingLevel();
758 $lowercaseType = strtolower($child->getContentType());
760 if (isset($filter[$realLevel])
761 && isset($filter[$realLevel][$lowercaseType])) {
762 return $filter[$realLevel][$lowercaseType];
770 return new self($this->_headers->newInstance(),
776 foreach ($this->_immediateChildren as $child) {
777 $child->encoderChanged($encoder);
783 $this->_encoder->charsetChanged($charset);
784 $this->_headers->charsetChanged($charset);
785 foreach ($this->_immediateChildren as $child) {
786 $child->charsetChanged($charset);
793 foreach ($this->_immediateChildren as $child) {
795 if ($child->getNestingLevel() == self::LEVEL_ALTERNATIVE) {
803 usort($this->_immediateChildren, array($this,
'_childSortAlgorithm'));
809 $typePrefs = array();
811 strtolower($a->getContentType()),
812 strtolower($b->getContentType()),
814 foreach ($types as $type) {
815 $typePrefs[] = (array_key_exists($type, $this->_alternativePartOrder))
816 ? $this->_alternativePartOrder[$type]
817 : (max($this->_alternativePartOrder) + 1);
820 return ($typePrefs[0] >= $typePrefs[1]) ? 1 : -1;
830 $this->_cache->clearAll($this->_cacheKey);
843 '/^'.$this->_grammar->getDefinition(
'id-left').
'@'.
844 $this->_grammar->getDefinition(
'id-right').
'$/D',
848 'Invalid ID given <'.$id.
'>'
860 $this->_cacheKey = uniqid();
862 foreach ($this->_children as $pos => $child) {
863 $children[$pos] = clone $child;