MimePart.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * A MIME part, in a multipart message.
  11. *
  12. * @package Swift
  13. * @subpackage Mime
  14. * @author Chris Corbyn
  15. */
  16. class Swift_Mime_MimePart extends Swift_Mime_SimpleMimeEntity
  17. {
  18. /** The format parameter last specified by the user */
  19. protected $_userFormat;
  20. /** The charset last specified by the user */
  21. protected $_userCharset;
  22. /** The delsp parameter last specified by the user */
  23. protected $_userDelSp;
  24. /** The nesting level of this MimePart */
  25. private $_nestingLevel = self::LEVEL_ALTERNATIVE;
  26. /**
  27. * Create a new MimePart with $headers, $encoder and $cache.
  28. *
  29. * @param Swift_Mime_HeaderSet $headers
  30. * @param Swift_Mime_ContentEncoder $encoder
  31. * @param Swift_KeyCache $cache
  32. * @param Swift_Mime_Grammar $grammar
  33. * @param string $charset
  34. */
  35. public function __construct(Swift_Mime_HeaderSet $headers,
  36. Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache, Swift_Mime_Grammar $grammar, $charset = null)
  37. {
  38. parent::__construct($headers, $encoder, $cache, $grammar);
  39. $this->setContentType('text/plain');
  40. if (!is_null($charset))
  41. {
  42. $this->setCharset($charset);
  43. }
  44. }
  45. /**
  46. * Set the body of this entity, either as a string, or as an instance of
  47. * {@link Swift_OutputByteStream}.
  48. *
  49. * @param mixed $body
  50. * @param string $contentType optional
  51. * @param string $charset optional
  52. */
  53. public function setBody($body, $contentType = null, $charset = null)
  54. {
  55. parent::setBody($body, $contentType);
  56. if (isset($charset))
  57. {
  58. $this->setCharset($charset);
  59. }
  60. return $this;
  61. }
  62. /**
  63. * Get the character set of this entity.
  64. *
  65. * @return string
  66. */
  67. public function getCharset()
  68. {
  69. return $this->_getHeaderParameter('Content-Type', 'charset');
  70. }
  71. /**
  72. * Set the character set of this entity.
  73. *
  74. * @param string $charset
  75. */
  76. public function setCharset($charset)
  77. {
  78. $this->_setHeaderParameter('Content-Type', 'charset', $charset);
  79. if ($charset !== $this->_userCharset)
  80. {
  81. $this->_clearCache();
  82. }
  83. $this->_userCharset = $charset;
  84. parent::charsetChanged($charset);
  85. return $this;
  86. }
  87. /**
  88. * Get the format of this entity (i.e. flowed or fixed).
  89. *
  90. * @return string
  91. */
  92. public function getFormat()
  93. {
  94. return $this->_getHeaderParameter('Content-Type', 'format');
  95. }
  96. /**
  97. * Set the format of this entity (flowed or fixed).
  98. *
  99. * @param string $format
  100. */
  101. public function setFormat($format)
  102. {
  103. $this->_setHeaderParameter('Content-Type', 'format', $format);
  104. $this->_userFormat = $format;
  105. return $this;
  106. }
  107. /**
  108. * Test if delsp is being used for this entity.
  109. *
  110. * @return boolean
  111. */
  112. public function getDelSp()
  113. {
  114. return ($this->_getHeaderParameter('Content-Type', 'delsp') == 'yes')
  115. ? true
  116. : false;
  117. }
  118. /**
  119. * Turn delsp on or off for this entity.
  120. *
  121. * @param boolean $delsp
  122. */
  123. public function setDelSp($delsp = true)
  124. {
  125. $this->_setHeaderParameter('Content-Type', 'delsp', $delsp ? 'yes' : null);
  126. $this->_userDelSp = $delsp;
  127. return $this;
  128. }
  129. /**
  130. * Get the nesting level of this entity.
  131. *
  132. * @return int
  133. * @see LEVEL_TOP, LEVEL_ALTERNATIVE, LEVEL_MIXED, LEVEL_RELATED
  134. */
  135. public function getNestingLevel()
  136. {
  137. return $this->_nestingLevel;
  138. }
  139. /**
  140. * Receive notification that the charset has changed on this document, or a
  141. * parent document.
  142. *
  143. * @param string $charset
  144. */
  145. public function charsetChanged($charset)
  146. {
  147. $this->setCharset($charset);
  148. }
  149. // -- Protected methods
  150. /** Fix the content-type and encoding of this entity */
  151. protected function _fixHeaders()
  152. {
  153. parent::_fixHeaders();
  154. if (count($this->getChildren()))
  155. {
  156. $this->_setHeaderParameter('Content-Type', 'charset', null);
  157. $this->_setHeaderParameter('Content-Type', 'format', null);
  158. $this->_setHeaderParameter('Content-Type', 'delsp', null);
  159. }
  160. else
  161. {
  162. $this->setCharset($this->_userCharset);
  163. $this->setFormat($this->_userFormat);
  164. $this->setDelSp($this->_userDelSp);
  165. }
  166. }
  167. /** Set the nesting level of this entity */
  168. protected function _setNestingLevel($level)
  169. {
  170. $this->_nestingLevel = $level;
  171. }
  172. }