MimePart.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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, Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache, Swift_Mime_Grammar $grammar, $charset = null)
  36. {
  37. parent::__construct($headers, $encoder, $cache, $grammar);
  38. $this->setContentType('text/plain');
  39. if (!is_null($charset)) {
  40. $this->setCharset($charset);
  41. }
  42. }
  43. /**
  44. * Set the body of this entity, either as a string, or as an instance of
  45. * {@link Swift_OutputByteStream}.
  46. *
  47. * @param mixed $body
  48. * @param string $contentType optional
  49. * @param string $charset optional
  50. * @param Swift_Mime_MimePart
  51. */
  52. public function setBody($body, $contentType = null, $charset = null)
  53. {
  54. if (isset($charset)) {
  55. $this->setCharset($charset);
  56. }
  57. $body = $this->_convertString($body);
  58. parent::setBody($body, $contentType);
  59. return $this;
  60. }
  61. /**
  62. * Get the character set of this entity.
  63. *
  64. * @return string
  65. */
  66. public function getCharset()
  67. {
  68. return $this->_getHeaderParameter('Content-Type', 'charset');
  69. }
  70. /**
  71. * Set the character set of this entity.
  72. *
  73. * @param string $charset
  74. * @param Swift_Mime_MimePart
  75. */
  76. public function setCharset($charset)
  77. {
  78. $this->_setHeaderParameter('Content-Type', 'charset', $charset);
  79. if ($charset !== $this->_userCharset) {
  80. $this->_clearCache();
  81. }
  82. $this->_userCharset = $charset;
  83. parent::charsetChanged($charset);
  84. return $this;
  85. }
  86. /**
  87. * Get the format of this entity (i.e. flowed or fixed).
  88. *
  89. * @return string
  90. */
  91. public function getFormat()
  92. {
  93. return $this->_getHeaderParameter('Content-Type', 'format');
  94. }
  95. /**
  96. * Set the format of this entity (flowed or fixed).
  97. *
  98. * @param string $format
  99. * @param Swift_Mime_MimePart
  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. * @param Swift_Mime_MimePart
  123. */
  124. public function setDelSp($delsp = true)
  125. {
  126. $this->_setHeaderParameter('Content-Type', 'delsp', $delsp ? 'yes' : null);
  127. $this->_userDelSp = $delsp;
  128. return $this;
  129. }
  130. /**
  131. * Get the nesting level of this entity.
  132. *
  133. * @return int
  134. * @see LEVEL_TOP, LEVEL_ALTERNATIVE, LEVEL_MIXED, LEVEL_RELATED
  135. */
  136. public function getNestingLevel()
  137. {
  138. return $this->_nestingLevel;
  139. }
  140. /**
  141. * Receive notification that the charset has changed on this document, or a
  142. * parent document.
  143. *
  144. * @param string $charset
  145. */
  146. public function charsetChanged($charset)
  147. {
  148. $this->setCharset($charset);
  149. }
  150. // -- Protected methods
  151. /** Fix the content-type and encoding of this entity */
  152. protected function _fixHeaders()
  153. {
  154. parent::_fixHeaders();
  155. if (count($this->getChildren())) {
  156. $this->_setHeaderParameter('Content-Type', 'charset', null);
  157. $this->_setHeaderParameter('Content-Type', 'format', null);
  158. $this->_setHeaderParameter('Content-Type', 'delsp', null);
  159. } else {
  160. $this->setCharset($this->_userCharset);
  161. $this->setFormat($this->_userFormat);
  162. $this->setDelSp($this->_userDelSp);
  163. }
  164. }
  165. /** Set the nesting level of this entity */
  166. protected function _setNestingLevel($level)
  167. {
  168. $this->_nestingLevel = $level;
  169. }
  170. /** Encode charset when charset is not utf-8 */
  171. protected function _convertString($string)
  172. {
  173. $charset = strtolower($this->getCharset());
  174. if (!in_array($charset, array('utf-8', 'iso-8859-1', ""))) {
  175. // mb_convert_encoding must be the first one to check, since iconv cannot convert some words.
  176. if (function_exists('mb_convert_encoding')) {
  177. $string = mb_convert_encoding($string, $charset, 'utf-8');
  178. } elseif (function_exists('iconv')) {
  179. $string = iconv($charset, 'utf-8//TRANSLIT//IGNORE', $string);
  180. } else {
  181. throw new Swift_SwiftException('No suitable convert encoding function (use UTF-8 as your harset or install the mbstring or iconv extension).');
  182. }
  183. return $string;
  184. }
  185. return $string;
  186. }
  187. }