1: <?php
2:
3: 4: 5: 6: 7: 8: 9:
10:
11: 12: 13: 14: 15: 16: 17:
18: class Swift_Mime_MimePart extends Swift_Mime_SimpleMimeEntity
19: {
20:
21: protected $_userFormat;
22:
23:
24: protected $_userCharset;
25:
26:
27: protected $_userDelSp;
28:
29:
30: private $_nestingLevel = self::LEVEL_ALTERNATIVE;
31:
32: 33: 34: 35: 36: 37: 38: 39: 40:
41: public function __construct(Swift_Mime_HeaderSet $headers, Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache, Swift_Mime_Grammar $grammar, $charset = null)
42: {
43: parent::__construct($headers, $encoder, $cache, $grammar);
44: $this->setContentType('text/plain');
45: if (!is_null($charset)) {
46: $this->setCharset($charset);
47: }
48: }
49:
50: 51: 52: 53: 54: 55: 56: 57: 58:
59: public function setBody($body, $contentType = null, $charset = null)
60: {
61: if (isset($charset)) {
62: $this->setCharset($charset);
63: }
64: $body = $this->_convertString($body);
65:
66: parent::setBody($body, $contentType);
67:
68: return $this;
69: }
70:
71: 72: 73: 74: 75:
76: public function getCharset()
77: {
78: return $this->_getHeaderParameter('Content-Type', 'charset');
79: }
80:
81: 82: 83: 84: 85: 86:
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:
99: 100: 101: 102: 103:
104: public function getFormat()
105: {
106: return $this->_getHeaderParameter('Content-Type', 'format');
107: }
108:
109: 110: 111: 112: 113: 114:
115: public function setFormat($format)
116: {
117: $this->_setHeaderParameter('Content-Type', 'format', $format);
118: $this->_userFormat = $format;
119:
120: return $this;
121: }
122:
123: 124: 125: 126: 127:
128: public function getDelSp()
129: {
130: return ($this->_getHeaderParameter('Content-Type', 'delsp') == 'yes')
131: ? true
132: : false;
133: }
134:
135: 136: 137: 138: 139: 140:
141: public function setDelSp($delsp = true)
142: {
143: $this->_setHeaderParameter('Content-Type', 'delsp', $delsp ? 'yes' : null);
144: $this->_userDelSp = $delsp;
145:
146: return $this;
147: }
148:
149: 150: 151: 152: 153: 154:
155: public function getNestingLevel()
156: {
157: return $this->_nestingLevel;
158: }
159:
160: 161: 162: 163: 164: 165:
166: public function charsetChanged($charset)
167: {
168: $this->setCharset($charset);
169: }
170:
171:
172:
173:
174: protected function _fixHeaders()
175: {
176: parent::_fixHeaders();
177: if (count($this->getChildren())) {
178: $this->_setHeaderParameter('Content-Type', 'charset', null);
179: $this->_setHeaderParameter('Content-Type', 'format', null);
180: $this->_setHeaderParameter('Content-Type', 'delsp', null);
181: } else {
182: $this->setCharset($this->_userCharset);
183: $this->setFormat($this->_userFormat);
184: $this->setDelSp($this->_userDelSp);
185: }
186: }
187:
188:
189: protected function _setNestingLevel($level)
190: {
191: $this->_nestingLevel = $level;
192: }
193:
194:
195: protected function _convertString($string)
196: {
197: $charset = strtolower($this->getCharset());
198: if (!in_array($charset, array('utf-8', 'iso-8859-1', ""))) {
199:
200: if (function_exists('mb_convert_encoding')) {
201: $string = mb_convert_encoding($string, $charset, 'utf-8');
202: } elseif (function_exists('iconv')) {
203: $string = iconv($charset, 'utf-8//TRANSLIT//IGNORE', $string);
204: } else {
205: throw new Swift_SwiftException('No suitable convert encoding function (use UTF-8 as your harset or install the mbstring or iconv extension).');
206: }
207:
208: return $string;
209: }
210:
211: return $string;
212: }
213: }
214: