1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26: class NMailMimePart extends NObject
27: {
28:
29: const ENCODING_BASE64 = 'base64',
30: ENCODING_7BIT = '7bit',
31: ENCODING_8BIT = '8bit',
32: ENCODING_QUOTED_PRINTABLE = 'quoted-printable';
33:
34:
35: const EOL = "\r\n";
36: const LINE_LENGTH = 76;
37:
38:
39: private = array();
40:
41:
42: private $parts = array();
43:
44:
45: private $body;
46:
47:
48:
49: 50: 51: 52: 53: 54: 55:
56: public function ($name, $value, $append = FALSE)
57: {
58: if (!$name || preg_match('#[^a-z0-9-]#i', $name)) {
59: throw new InvalidArgumentException("Header name must be non-empty alphanumeric string, '$name' given.");
60: }
61:
62: if ($value == NULL) {
63: if (!$append) {
64: unset($this->headers[$name]);
65: }
66:
67: } elseif (is_array($value)) {
68: $tmp = & $this->headers[$name];
69: if (!$append || !is_array($tmp)) {
70: $tmp = array();
71: }
72:
73: foreach ($value as $email => $recipient) {
74: if ($recipient !== NULL && !NStrings::checkEncoding($recipient)) {
75: NValidators::assert($recipient, 'unicode', "header '$name'");
76: }
77: if (preg_match('#[\r\n]#', $recipient)) {
78: throw new InvalidArgumentException("Name must not contain line separator.");
79: }
80: NValidators::assert($email, 'email', "header '$name'");
81: $tmp[$email] = $recipient;
82: }
83:
84: } else {
85: $value = (string) $value;
86: if (!NStrings::checkEncoding($value)) {
87: throw new InvalidArgumentException("Header is not valid UTF-8 string.");
88: }
89: $this->headers[$name] = preg_replace('#[\r\n]+#', ' ', $value);
90: }
91: return $this;
92: }
93:
94:
95:
96: 97: 98: 99: 100:
101: public function ($name)
102: {
103: return isset($this->headers[$name]) ? $this->headers[$name] : NULL;
104: }
105:
106:
107:
108: 109: 110: 111: 112:
113: public function ($name)
114: {
115: unset($this->headers[$name]);
116: return $this;
117: }
118:
119:
120:
121: 122: 123: 124: 125: 126:
127: public function ($name)
128: {
129: $offset = strlen($name) + 2;
130:
131: if (!isset($this->headers[$name])) {
132: return NULL;
133:
134: } elseif (is_array($this->headers[$name])) {
135: $s = '';
136: foreach ($this->headers[$name] as $email => $name) {
137: if ($name != NULL) {
138: $s .= self::encodeHeader(
139: strpbrk($name, '.,;<@>()[]"=?') ? '"' . addcslashes($name, '"\\') . '"' : $name,
140: $offset
141: );
142: $email = " <$email>";
143: }
144: $email .= ',';
145: if ($s !== '' && $offset + strlen($email) > self::LINE_LENGTH) {
146: $s .= self::EOL . "\t";
147: $offset = 1;
148: }
149: $s .= $email;
150: $offset += strlen($email);
151: }
152: return substr($s, 0, -1);
153:
154: } elseif (preg_match('#^(\S+; (?:file)?name=)"(.*)"\z#', $this->headers[$name], $m)) {
155: $offset += strlen($m[1]);
156: return $m[1] . '"' . self::encodeHeader($m[2], $offset) . '"';
157:
158: } else {
159: return self::encodeHeader($this->headers[$name], $offset);
160: }
161: }
162:
163:
164:
165: 166: 167: 168:
169: public function ()
170: {
171: return $this->headers;
172: }
173:
174:
175:
176: 177: 178: 179: 180: 181:
182: public function setContentType($contentType, $charset = NULL)
183: {
184: $this->setHeader('Content-Type', $contentType . ($charset ? "; charset=$charset" : ''));
185: return $this;
186: }
187:
188:
189:
190: 191: 192: 193: 194:
195: public function setEncoding($encoding)
196: {
197: $this->setHeader('Content-Transfer-Encoding', $encoding);
198: return $this;
199: }
200:
201:
202:
203: 204: 205: 206:
207: public function getEncoding()
208: {
209: return $this->getHeader('Content-Transfer-Encoding');
210: }
211:
212:
213:
214: 215: 216: 217:
218: public function addPart(NMailMimePart $part = NULL)
219: {
220: return $this->parts[] = $part === NULL ? new self : $part;
221: }
222:
223:
224:
225: 226: 227: 228:
229: public function setBody($body)
230: {
231: $this->body = $body;
232: return $this;
233: }
234:
235:
236:
237: 238: 239: 240:
241: public function getBody()
242: {
243: return $this->body;
244: }
245:
246:
247:
248:
249:
250:
251:
252: 253: 254: 255:
256: public function generateMessage()
257: {
258: $output = '';
259: $boundary = '--------' . NStrings::random();
260:
261: foreach ($this->headers as $name => $value) {
262: $output .= $name . ': ' . $this->getEncodedHeader($name);
263: if ($this->parts && $name === 'Content-Type') {
264: $output .= ';' . self::EOL . "\tboundary=\"$boundary\"";
265: }
266: $output .= self::EOL;
267: }
268: $output .= self::EOL;
269:
270: $body = (string) $this->body;
271: if ($body !== '') {
272: switch ($this->getEncoding()) {
273: case self::ENCODING_QUOTED_PRINTABLE:
274: $output .= function_exists('quoted_printable_encode') ? quoted_printable_encode($body) : self::encodeQuotedPrintable($body);
275: break;
276:
277: case self::ENCODING_BASE64:
278: $output .= rtrim(chunk_split(base64_encode($body), self::LINE_LENGTH, self::EOL));
279: break;
280:
281: case self::ENCODING_7BIT:
282: $body = preg_replace('#[\x80-\xFF]+#', '', $body);
283:
284:
285: case self::ENCODING_8BIT:
286: $body = str_replace(array("\x00", "\r"), '', $body);
287: $body = str_replace("\n", self::EOL, $body);
288: $output .= $body;
289: break;
290:
291: default:
292: throw new InvalidStateException('Unknown encoding.');
293: }
294: }
295:
296: if ($this->parts) {
297: if (substr($output, -strlen(self::EOL)) !== self::EOL) {
298: $output .= self::EOL;
299: }
300: foreach ($this->parts as $part) {
301: $output .= '--' . $boundary . self::EOL . $part->generateMessage() . self::EOL;
302: }
303: $output .= '--' . $boundary.'--';
304: }
305:
306: return $output;
307: }
308:
309:
310:
311:
312:
313:
314:
315: 316: 317: 318: 319: 320: 321:
322: private static function ($s, & $offset = 0)
323: {
324: $o = '';
325: if ($offset >= 55) {
326: $o = self::EOL . "\t";
327: $offset = 1;
328: }
329:
330: if (strspn($s, "!\"#$%&\'()*+,-./0123456789:;<>@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^`abcdefghijklmnopqrstuvwxyz{|}=? _\r\n\t") === strlen($s)
331: && ($offset + strlen($s) <= self::LINE_LENGTH)
332: ) {
333: $offset += strlen($s);
334: return $o . $s;
335: }
336:
337: $o .= str_replace("\n ", "\n\t", substr(iconv_mime_encode(str_repeat(' ', $offset), $s, array(
338: 'scheme' => 'B',
339: 'input-charset' => 'UTF-8',
340: 'output-charset' => 'UTF-8',
341: )), $offset + 2));
342:
343: $offset = strlen($o) - strrpos($o, "\n");
344: return $o;
345: }
346:
347:
348:
349: 350: 351: 352: 353: public static function encodeQuotedPrintable($s)
354: {
355: $range = '!"#$%&\'()*+,-./0123456789:;<>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}';
356: $pos = 0;
357: $len = 0;
358: $o = '';
359: $size = strlen($s);
360: while ($pos < $size) {
361: if ($l = strspn($s, $range, $pos)) {
362: while ($len + $l > self::LINE_LENGTH - 1) {
363: $lx = self::LINE_LENGTH - $len - 1;
364: $o .= substr($s, $pos, $lx) . '=' . self::EOL;
365: $pos += $lx;
366: $l -= $lx;
367: $len = 0;
368: }
369: $o .= substr($s, $pos, $l);
370: $len += $l;
371: $pos += $l;
372:
373: } else {
374: $len += 3;
375: if ($len > self::LINE_LENGTH - 1) {
376: $o .= '=' . self::EOL;
377: $len = 3;
378: }
379: $o .= '=' . strtoupper(bin2hex($s[$pos]));
380: $pos++;
381: }
382: }
383: return rtrim($o, '=' . self::EOL);
384: }
385:
386: }
387: