TYPO3  7.6
Mime/MimePart.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of SwiftMailer.
5  * (c) 2004-2009 Chris Corbyn
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10 
17 {
19  protected $_userFormat;
20 
22  protected $_userCharset;
23 
25  protected $_userDelSp;
26 
28  private $_nestingLevel = self::LEVEL_ALTERNATIVE;
29 
39  public function __construct(Swift_Mime_HeaderSet $headers, Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache, Swift_Mime_Grammar $grammar, $charset = null)
40  {
41  parent::__construct($headers, $encoder, $cache, $grammar);
42  $this->setContentType('text/plain');
43  if (!is_null($charset)) {
44  $this->setCharset($charset);
45  }
46  }
47 
58  public function setBody($body, $contentType = null, $charset = null)
59  {
60  if (isset($charset)) {
61  $this->setCharset($charset);
62  }
63  $body = $this->_convertString($body);
64 
65  parent::setBody($body, $contentType);
66 
67  return $this;
68  }
69 
75  public function getCharset()
76  {
77  return $this->_getHeaderParameter('Content-Type', 'charset');
78  }
79 
87  public function setCharset($charset)
88  {
89  $this->_setHeaderParameter('Content-Type', 'charset', $charset);
90  if ($charset !== $this->_userCharset) {
91  $this->_clearCache();
92  }
93  $this->_userCharset = $charset;
94  parent::charsetChanged($charset);
95 
96  return $this;
97  }
98 
104  public function getFormat()
105  {
106  return $this->_getHeaderParameter('Content-Type', 'format');
107  }
108 
116  public function setFormat($format)
117  {
118  $this->_setHeaderParameter('Content-Type', 'format', $format);
119  $this->_userFormat = $format;
120 
121  return $this;
122  }
123 
129  public function getDelSp()
130  {
131  return ($this->_getHeaderParameter('Content-Type', 'delsp') == 'yes')
132  ? true
133  : false;
134  }
135 
143  public function setDelSp($delsp = true)
144  {
145  $this->_setHeaderParameter('Content-Type', 'delsp', $delsp ? 'yes' : null);
146  $this->_userDelSp = $delsp;
147 
148  return $this;
149  }
150 
158  public function getNestingLevel()
159  {
160  return $this->_nestingLevel;
161  }
162 
169  public function charsetChanged($charset)
170  {
171  $this->setCharset($charset);
172  }
173 
175  protected function _fixHeaders()
176  {
177  parent::_fixHeaders();
178  if (count($this->getChildren())) {
179  $this->_setHeaderParameter('Content-Type', 'charset', null);
180  $this->_setHeaderParameter('Content-Type', 'format', null);
181  $this->_setHeaderParameter('Content-Type', 'delsp', null);
182  } else {
183  $this->setCharset($this->_userCharset);
184  $this->setFormat($this->_userFormat);
185  $this->setDelSp($this->_userDelSp);
186  }
187  }
188 
190  protected function _setNestingLevel($level)
191  {
192  $this->_nestingLevel = $level;
193  }
194 
196  protected function _convertString($string)
197  {
198  $charset = strtolower($this->getCharset());
199  if (!in_array($charset, array('utf-8', 'iso-8859-1', ''))) {
200  // mb_convert_encoding must be the first one to check, since iconv cannot convert some words.
201  if (function_exists('mb_convert_encoding')) {
202  $string = mb_convert_encoding($string, $charset, 'utf-8');
203  } elseif (function_exists('iconv')) {
204  $string = iconv('utf-8//TRANSLIT//IGNORE', $charset, $string);
205  } else {
206  throw new Swift_SwiftException('No suitable convert encoding function (use UTF-8 as your charset or install the mbstring or iconv extension).');
207  }
208 
209  return $string;
210  }
211 
212  return $string;
213  }
214 }