PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/SwiftMailer/lib/classes/Swift/Mime/MimePart.php

http://github.com/snytkine/LampCMS
PHP | 224 lines | 114 code | 24 blank | 86 comment | 9 complexity | 93267acc49d0cb853be72829912e25c8 MD5 | raw file
Possible License(s): LGPL-3.0
  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. if (isset($charset))
  57. {
  58. $this->setCharset($charset);
  59. }
  60. $body = $this->_convertString($body);
  61. parent::setBody($body, $contentType);
  62. return $this;
  63. }
  64. /**
  65. * Get the character set of this entity.
  66. *
  67. * @return string
  68. */
  69. public function getCharset()
  70. {
  71. return $this->_getHeaderParameter('Content-Type', 'charset');
  72. }
  73. /**
  74. * Set the character set of this entity.
  75. *
  76. * @param string $charset
  77. * @param Swift_Mime_MimePart
  78. */
  79. public function setCharset($charset)
  80. {
  81. $this->_setHeaderParameter('Content-Type', 'charset', $charset);
  82. if ($charset !== $this->_userCharset)
  83. {
  84. $this->_clearCache();
  85. }
  86. $this->_userCharset = $charset;
  87. parent::charsetChanged($charset);
  88. return $this;
  89. }
  90. /**
  91. * Get the format of this entity (i.e. flowed or fixed).
  92. *
  93. * @return string
  94. */
  95. public function getFormat()
  96. {
  97. return $this->_getHeaderParameter('Content-Type', 'format');
  98. }
  99. /**
  100. * Set the format of this entity (flowed or fixed).
  101. *
  102. * @param string $format
  103. * @param Swift_Mime_MimePart
  104. */
  105. public function setFormat($format)
  106. {
  107. $this->_setHeaderParameter('Content-Type', 'format', $format);
  108. $this->_userFormat = $format;
  109. return $this;
  110. }
  111. /**
  112. * Test if delsp is being used for this entity.
  113. *
  114. * @return boolean
  115. */
  116. public function getDelSp()
  117. {
  118. return ($this->_getHeaderParameter('Content-Type', 'delsp') == 'yes')
  119. ? true
  120. : false;
  121. }
  122. /**
  123. * Turn delsp on or off for this entity.
  124. *
  125. * @param boolean $delsp
  126. * @param Swift_Mime_MimePart
  127. */
  128. public function setDelSp($delsp = true)
  129. {
  130. $this->_setHeaderParameter('Content-Type', 'delsp', $delsp ? 'yes' : null);
  131. $this->_userDelSp = $delsp;
  132. return $this;
  133. }
  134. /**
  135. * Get the nesting level of this entity.
  136. *
  137. * @return int
  138. * @see LEVEL_TOP, LEVEL_ALTERNATIVE, LEVEL_MIXED, LEVEL_RELATED
  139. */
  140. public function getNestingLevel()
  141. {
  142. return $this->_nestingLevel;
  143. }
  144. /**
  145. * Receive notification that the charset has changed on this document, or a
  146. * parent document.
  147. *
  148. * @param string $charset
  149. */
  150. public function charsetChanged($charset)
  151. {
  152. $this->setCharset($charset);
  153. }
  154. // -- Protected methods
  155. /** Fix the content-type and encoding of this entity */
  156. protected function _fixHeaders()
  157. {
  158. parent::_fixHeaders();
  159. if (count($this->getChildren()))
  160. {
  161. $this->_setHeaderParameter('Content-Type', 'charset', null);
  162. $this->_setHeaderParameter('Content-Type', 'format', null);
  163. $this->_setHeaderParameter('Content-Type', 'delsp', null);
  164. }
  165. else
  166. {
  167. $this->setCharset($this->_userCharset);
  168. $this->setFormat($this->_userFormat);
  169. $this->setDelSp($this->_userDelSp);
  170. }
  171. }
  172. /** Set the nesting level of this entity */
  173. protected function _setNestingLevel($level)
  174. {
  175. $this->_nestingLevel = $level;
  176. }
  177. /** Encode charset when charset is not utf-8 */
  178. protected function _convertString($string)
  179. {
  180. $charset = strtolower($this->getCharset());
  181. if (!in_array($charset, array('utf-8', 'iso-8859-1', "")))
  182. {
  183. // mb_convert_encoding must be the first one to check, since iconv cannot convert some words.
  184. if (function_exists('mb_convert_encoding'))
  185. {
  186. $string = mb_convert_encoding($string, $charset, 'utf-8');
  187. }
  188. else if (function_exists('iconv'))
  189. {
  190. $string = iconv($charset, 'utf-8//TRANSLIT//IGNORE', $string);
  191. }
  192. else
  193. {
  194. throw new Swift_SwiftException('No suitable convert encoding function (use UTF-8 as your harset or install the mbstring or iconv extension).');
  195. }
  196. return $string;
  197. }
  198. return $string;
  199. }
  200. }