MimePart.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. * @param Swift_Mime_MimePart
  53. */
  54. public function setBody($body, $contentType = null, $charset = null)
  55. {
  56. parent::setBody($body, $contentType);
  57. if (isset($charset))
  58. {
  59. $this->setCharset($charset);
  60. }
  61. return $this;
  62. }
  63. /**
  64. * Get the character set of this entity.
  65. *
  66. * @return string
  67. */
  68. public function getCharset()
  69. {
  70. return $this->_getHeaderParameter('Content-Type', 'charset');
  71. }
  72. /**
  73. * Set the character set of this entity.
  74. *
  75. * @param string $charset
  76. * @param Swift_Mime_MimePart
  77. */
  78. public function setCharset($charset)
  79. {
  80. $this->_setHeaderParameter('Content-Type', 'charset', $charset);
  81. if ($charset !== $this->_userCharset)
  82. {
  83. $this->_clearCache();
  84. }
  85. $this->_userCharset = $charset;
  86. parent::charsetChanged($charset);
  87. return $this;
  88. }
  89. /**
  90. * Get the format of this entity (i.e. flowed or fixed).
  91. *
  92. * @return string
  93. */
  94. public function getFormat()
  95. {
  96. return $this->_getHeaderParameter('Content-Type', 'format');
  97. }
  98. /**
  99. * Set the format of this entity (flowed or fixed).
  100. *
  101. * @param string $format
  102. * @param Swift_Mime_MimePart
  103. */
  104. public function setFormat($format)
  105. {
  106. $this->_setHeaderParameter('Content-Type', 'format', $format);
  107. $this->_userFormat = $format;
  108. return $this;
  109. }
  110. /**
  111. * Test if delsp is being used for this entity.
  112. *
  113. * @return boolean
  114. */
  115. public function getDelSp()
  116. {
  117. return ($this->_getHeaderParameter('Content-Type', 'delsp') == 'yes')
  118. ? true
  119. : false;
  120. }
  121. /**
  122. * Turn delsp on or off for this entity.
  123. *
  124. * @param boolean $delsp
  125. * @param Swift_Mime_MimePart
  126. */
  127. public function setDelSp($delsp = true)
  128. {
  129. $this->_setHeaderParameter('Content-Type', 'delsp', $delsp ? 'yes' : null);
  130. $this->_userDelSp = $delsp;
  131. return $this;
  132. }
  133. /**
  134. * Get the nesting level of this entity.
  135. *
  136. * @return int
  137. * @see LEVEL_TOP, LEVEL_ALTERNATIVE, LEVEL_MIXED, LEVEL_RELATED
  138. */
  139. public function getNestingLevel()
  140. {
  141. return $this->_nestingLevel;
  142. }
  143. /**
  144. * Receive notification that the charset has changed on this document, or a
  145. * parent document.
  146. *
  147. * @param string $charset
  148. */
  149. public function charsetChanged($charset)
  150. {
  151. $this->setCharset($charset);
  152. }
  153. // -- Protected methods
  154. /** Fix the content-type and encoding of this entity */
  155. protected function _fixHeaders()
  156. {
  157. parent::_fixHeaders();
  158. if (count($this->getChildren()))
  159. {
  160. $this->_setHeaderParameter('Content-Type', 'charset', null);
  161. $this->_setHeaderParameter('Content-Type', 'format', null);
  162. $this->_setHeaderParameter('Content-Type', 'delsp', null);
  163. }
  164. else
  165. {
  166. $this->setCharset($this->_userCharset);
  167. $this->setFormat($this->_userFormat);
  168. $this->setDelSp($this->_userDelSp);
  169. }
  170. }
  171. /** Set the nesting level of this entity */
  172. protected function _setNestingLevel($level)
  173. {
  174. $this->_nestingLevel = $level;
  175. }
  176. }