8f6b86cc |
1 | <?php |
2 | |
3 | /** |
1a09384c |
4 | * mime.class |
8f6b86cc |
5 | * |
6 | * Copyright (c) 2002 The SquirrelMail Project Team |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
8 | * |
8f6b86cc |
9 | * This contains functions needed to handle mime messages. |
10 | * |
11 | * $Id$ |
12 | */ |
13 | |
e219fb97 |
14 | /* |
e8bedb2d |
15 | * rdc822_header class |
16 | * input: header_string or array |
17 | */ |
b65ca362 |
18 | class rfc822_header { |
e8bedb2d |
19 | var $date = '', |
20 | $subject = '', |
b65ca362 |
21 | $from = array(), |
22 | $sender = '', |
23 | $reply_to = array(), |
24 | $to = array(), |
25 | $cc = array(), |
26 | $bcc = array(), |
27 | $in_reply_to = '', |
28 | $message_id = '', |
29 | $mime = false, |
30 | $content_type = '', |
31 | $disposition = '', |
32 | $xmailer = '', |
33 | $priority = 3, |
34 | $dnt = '', |
35 | $mlist = array(), |
e219fb97 |
36 | $more_headers = array(); /* only needed for constructing headers |
b65ca362 |
37 | in smtp.php */ |
38 | function parseHeader($hdr) { |
39 | if (is_array($hdr)) { |
40 | $hdr = implode('', $hdr); |
1d9392e4 |
41 | } |
b65ca362 |
42 | |
43 | /* First we unfold the header */ |
44 | $hdr = trim(str_replace(array("\r\n\t", "\r\n "),array('', ''), $hdr)); |
45 | |
46 | /* Now we can make a new header array with */ |
47 | /* each element representing a headerline */ |
e219fb97 |
48 | $hdr = explode("\r\n" , $hdr); |
b65ca362 |
49 | foreach ($hdr as $line) { |
50 | $pos = strpos($line, ':'); |
51 | if ($pos > 0) { |
52 | $field = substr($line, 0, $pos); |
53 | $value = trim(substr($line, $pos+1)); |
54 | if(!preg_match('/^X.*/i', $field) && |
4b8699db |
55 | !preg_match('/^Subject/i', $field)) { |
1d9392e4 |
56 | $value = $this->stripComments($value); |
57 | } |
b65ca362 |
58 | $this->parseField($field, $value); |
1d9392e4 |
59 | } |
60 | } |
b65ca362 |
61 | if ($this->content_type == '') { |
1d9392e4 |
62 | $this->parseContentType('text/plain; charset=us-ascii'); |
63 | } |
e8bedb2d |
64 | } |
e219fb97 |
65 | |
b65ca362 |
66 | function stripComments($value) { |
67 | $result = ''; |
68 | |
e8bedb2d |
69 | $cnt = strlen($value); |
20760c79 |
70 | for ($i = 0; $i < $cnt; $i++) { |
e219fb97 |
71 | switch ($value{$i}) { |
72 | case '"': |
73 | $result .= '"'; |
20760c79 |
74 | while ((++$i < $cnt) && ($value{$i} != '"')) { |
e219fb97 |
75 | if ($value{$i} == '\\') { |
76 | $result .= '\\'; |
20760c79 |
77 | $i++; |
e219fb97 |
78 | } |
79 | $result .= $value{$i}; |
e219fb97 |
80 | } |
81 | $result .= $value{$i}; |
82 | break; |
83 | case '(': |
20760c79 |
84 | $depth = 1; |
85 | while (($depth > 0) && (++$i < $cnt)) { |
86 | switch($value{$i}) { |
87 | case '\\': |
88 | $i++; |
89 | break; |
90 | case '(': |
91 | $depth++; |
92 | break; |
93 | case ')': |
94 | $depth--; |
95 | break; |
96 | default: |
97 | break; |
98 | } |
e219fb97 |
99 | } |
100 | break; |
101 | default: |
102 | $result .= $value{$i}; |
103 | break; |
104 | } |
105 | } |
106 | return $result; |
4b8699db |
107 | } |
e219fb97 |
108 | |
b65ca362 |
109 | function parseField($field, $value) { |
e8bedb2d |
110 | $field = strtolower($field); |
b65ca362 |
111 | switch($field) { |
112 | case 'date': |
113 | $d = strtr($value, array(' ' => ' ')); |
114 | $d = explode(' ', $d); |
115 | $this->date = getTimeStamp($d); |
116 | break; |
117 | case 'subject': |
118 | $this->subject = $value; |
119 | break; |
120 | case 'from': |
121 | $this->from = $this->parseAddress($value,true); |
122 | break; |
123 | case 'sender': |
124 | $this->sender = $this->parseAddress($value); |
125 | break; |
126 | case 'reply-to': |
127 | $this->reply_to = $this->parseAddress($value, true); |
128 | break; |
129 | case 'to': |
130 | $this->to = $this->parseAddress($value, true); |
131 | break; |
132 | case 'cc': |
133 | $this->cc = $this->parseAddress($value, true); |
134 | break; |
135 | case 'bcc': |
136 | $this->bcc = $this->parseAddress($value, true); |
137 | break; |
138 | case 'in-reply-to': |
139 | $this->in_reply_to = $value; |
140 | break; |
e219fb97 |
141 | case 'message-id': |
b65ca362 |
142 | $this->message_id = $value; |
143 | break; |
144 | case 'disposition-notification-to': |
145 | $this->dnt = $this->parseAddress($value); |
146 | break; |
e219fb97 |
147 | case 'mime-version': |
b65ca362 |
148 | $value = str_replace(' ', '', $value); |
149 | $this->mime = ($value == '1.0' ? true : $this->mime); |
150 | break; |
151 | case 'content-type': |
152 | $this->parseContentType($value); |
153 | break; |
154 | case 'content-disposition': |
155 | $this->parseDisposition($value); |
e219fb97 |
156 | break; |
b65ca362 |
157 | case 'user-agent': |
158 | case 'x-mailer': |
159 | $this->xmailer = $value; |
160 | break; |
161 | case 'x-priority': |
162 | $this->priority = $value; |
163 | break; |
164 | case 'list-post': |
165 | $this->mlist('post', $value); |
166 | break; |
167 | case 'list-reply': |
e219fb97 |
168 | $this->mlist('reply', $value); |
b65ca362 |
169 | break; |
170 | case 'list-subscribe': |
171 | $this->mlist('subscribe', $value); |
172 | break; |
173 | case 'list-unsubscribe': |
174 | $this->mlist('unsubscribe', $value); |
175 | break; |
176 | case 'list-archive': |
177 | $this->mlist('archive', $value); |
178 | break; |
179 | case 'list-owner': |
180 | $this->mlist('owner', $value); |
181 | break; |
182 | case 'list-help': |
183 | $this->mlist('help', $value); |
184 | break; |
185 | case 'list-id': |
186 | $this->mlist('id', $value); |
187 | break; |
188 | default: |
189 | break; |
190 | } |
e219fb97 |
191 | } |
e8bedb2d |
192 | |
b65ca362 |
193 | function parseAddress |
194 | ($address, $ar=false, $addr_ar = array(), $group = '') { |
e8bedb2d |
195 | $pos = 0; |
b65ca362 |
196 | $j = strlen($address); |
e8bedb2d |
197 | $name = ''; |
198 | $addr = ''; |
b65ca362 |
199 | while ($pos < $j) { |
200 | switch ($address{$pos}) { |
201 | case '"': /* get the personal name */ |
202 | if ($address{++$pos} == '"') { |
203 | ++$pos; |
204 | } else { |
205 | while ($pos < $j && $address{$pos} != '"') { |
206 | if ((substr($address, $pos, 2) == '\\"') || |
207 | (substr($address, $pos, 2) == '\\\\')) { |
208 | $name .= $address{$pos++}; |
209 | } |
210 | $name .= $address{$pos++}; |
211 | } |
212 | } |
213 | ++$pos; |
214 | break; |
215 | case '<': /* get email address */ |
216 | $addr_start = $pos++; |
217 | while ($pos < $j && $address{$pos} != '>') { |
218 | $addr .= $address{$pos++}; |
219 | } |
220 | ++$pos; |
221 | break; |
222 | case '(': /* rip off comments */ |
223 | $addr_start = $pos; |
224 | for (++$pos; $pos < $j && $address{$pos} != ')'; ++$pos) { |
e219fb97 |
225 | $addr .= $address{$pos}; |
b65ca362 |
226 | } |
227 | $address_start = substr($address, 0, $addr_start); |
228 | $address_end = substr($address, $pos + 1); |
e219fb97 |
229 | $address = $address_start . $address_end; |
b65ca362 |
230 | $j = strlen($address); |
231 | $pos = $addr_start + 1; |
232 | break; |
233 | case ',': /* we reached a delimiter */ |
234 | if ($addr == '') { |
235 | $addr = substr($address, 0, $pos); |
236 | } else if ($name == '') { |
237 | $name = trim(substr($address, 0, $addr_start)); |
238 | } |
239 | |
240 | $at = strpos($addr, '@'); |
241 | $addr_structure = new address_structure(); |
242 | $addr_structure->personal = $name; |
243 | $addr_structure->group = $group; |
244 | if ($at) { |
245 | $addr_structure->mailbox = substr($addr, 0, $at); |
246 | $addr_structure->host = substr($addr, $at+1); |
247 | } else { |
248 | $addr_structure->mailbox = $addr; |
249 | } |
250 | $address = trim(substr($address, $pos+1)); |
251 | $j = strlen($address); |
252 | $pos = 0; |
253 | $name = ''; |
254 | $addr = ''; |
255 | $addr_ar[] = $addr_structure; |
256 | break; |
257 | case ':': /* process the group addresses */ |
258 | /* group marker */ |
259 | $group = substr($address, 0, $pos); |
260 | $address = substr($address, $pos+1); |
261 | $result = $this->parseAddress($address, $ar, $addr_ar, $group); |
262 | $addr_ar = $result[0]; |
263 | $pos = $result[1]; |
264 | $address = substr($address, $pos++); |
e219fb97 |
265 | $j = strlen($address); |
b65ca362 |
266 | $group = ''; |
267 | break; |
268 | case ';': |
269 | if ($group) { |
270 | $address = substr($address, 0, $pos - 1); |
271 | } |
272 | ++$pos; |
273 | break; |
274 | default: |
275 | ++$pos; |
276 | break; |
277 | } |
e8bedb2d |
278 | } |
b65ca362 |
279 | if ($addr == '') { |
280 | $addr = substr($address, 0, $pos); |
281 | } else if ($name == '') { |
282 | $name = trim(substr($address, 0, $addr_start)); |
e8bedb2d |
283 | } |
284 | $at = strpos($addr, '@'); |
285 | $addr_structure = new address_structure(); |
286 | $addr_structure->group = $group; |
b65ca362 |
287 | if ($at) { |
288 | $addr_structure->mailbox = trim(substr($addr, 0, $at)); |
289 | $addr_structure->host = trim(substr($addr, $at+1)); |
290 | } else { |
291 | $addr_structure->mailbox = trim($addr); |
e8bedb2d |
292 | } |
b65ca362 |
293 | if ($group && $addr == '') { /* no addresses found in group */ |
294 | $name = "$group: Undisclosed recipients;"; |
295 | $addr_structure->personal = $name; |
e219fb97 |
296 | $addr_ar[] = $addr_structure; |
297 | return (array($addr_ar, $pos+1)); |
b65ca362 |
298 | } else { |
299 | $addr_structure->personal = $name; |
300 | if ($name || $addr) { |
301 | $addr_ar[] = $addr_structure; |
e219fb97 |
302 | } |
e8bedb2d |
303 | } |
b65ca362 |
304 | if ($ar) { |
305 | return ($addr_ar); |
306 | } |
b65ca362 |
307 | return ($addr_ar[0]); |
8f6b86cc |
308 | } |
e219fb97 |
309 | |
b65ca362 |
310 | function parseContentType($value) { |
311 | $pos = strpos($value, ';'); |
312 | $props = ''; |
313 | if ($pos > 0) { |
314 | $type = trim(substr($value, 0, $pos)); |
315 | $props = trim(substr($type, $pos+1)); |
316 | } else { |
317 | $type = $value; |
318 | } |
319 | $content_type = new content_type($type); |
320 | if ($props) { |
321 | $properties = $this->parseProperties($props); |
322 | if (!isset($properties['charset'])) { |
323 | $properties['charset'] = 'us-ascii'; |
324 | } |
325 | $content_type->properties = $this->parseProperties($props); |
326 | } |
327 | $this->content_type = $content_type; |
328 | } |
e219fb97 |
329 | |
b65ca362 |
330 | function parseProperties($value) { |
331 | $propArray = explode(';', $value); |
332 | $propResultArray = array(); |
333 | foreach ($propArray as $prop) { |
334 | $prop = trim($prop); |
335 | $pos = strpos($prop, '='); |
336 | if ($pos > 0) { |
337 | $key = trim(substr($prop, 0, $pos)); |
338 | $val = trim(substr($prop, $pos+1)); |
339 | if ($val{0} == '"') { |
340 | $val = substr($val, 1, -1); |
341 | } |
342 | $propResultArray[$key] = $val; |
343 | } |
344 | } |
345 | return $propResultArray; |
346 | } |
e219fb97 |
347 | |
b65ca362 |
348 | function parseDisposition($value) { |
349 | $pos = strpos($value, ';'); |
350 | $props = ''; |
351 | if ($pos > 0) { |
352 | $name = trim(substr($value, 0, $pos)); |
353 | $props = trim(substr($type, $pos+1)); |
354 | } else { |
355 | $name = $value; |
356 | } |
357 | $props_a = $this->parseProperties($props); |
358 | $disp = new disposition($name); |
359 | $disp->properties = $props_a; |
360 | $this->disposition = $disp; |
361 | } |
e219fb97 |
362 | |
b65ca362 |
363 | function mlist($field, $value) { |
364 | $res_a = array(); |
365 | $value_a = explode(',', $value); |
366 | foreach ($value_a as $val) { |
367 | $val = trim($val); |
368 | if ($val{0} == '<') { |
369 | $val = substr($val, 1, -1); |
370 | } |
371 | if (substr($val, 0, 7) == 'mailto:') { |
372 | $res_a['mailto'] = substr($val, 7); |
373 | } else { |
374 | $res_a['href'] = $val; |
375 | } |
376 | } |
377 | $this->mlist[$field] = $res_a; |
378 | } |
e8bedb2d |
379 | |
e219fb97 |
380 | /* |
b65ca362 |
381 | * function to get the addres strings out of the header. |
382 | * Arguments: string or array of strings ! |
383 | * example1: header->getAddr_s('to'). |
384 | * example2: header->getAddr_s(array('to', 'cc', 'bcc')) |
385 | */ |
386 | function getAddr_s($arr, $separator = ',') { |
387 | $s = ''; |
388 | |
389 | if (is_array($arr)) { |
390 | foreach($arr as $arg) { |
391 | if ($this->getAddr_s($arg)) { |
392 | $s .= $separator . $result; |
393 | } |
394 | } |
395 | $s = ($s ? substr($s, 2) : $s); |
396 | } else { |
397 | eval('$addr = $this->' . $arr . ';') ; |
398 | if (is_array($addr)) { |
399 | foreach ($addr as $addr_o) { |
400 | if (is_object($addr_o)) { |
401 | $s .= $addr_o->getAddress() . $separator; |
402 | } |
403 | } |
404 | $s = substr($s, 0, -strlen($separator)); |
405 | } else { |
406 | if (is_object($addr)) { |
407 | $s .= $addr->getAddress(); |
408 | } |
409 | } |
410 | } |
b65ca362 |
411 | return $s; |
8f6b86cc |
412 | } |
e219fb97 |
413 | |
b65ca362 |
414 | function getAddr_a($arg, $excl_arr = array(), $arr = array()) { |
415 | if (is_array($arg)) { |
416 | foreach($arg as $argument) { |
417 | $arr = $this->getAddr_a($argument, $excl_arr, $arr); |
418 | } |
419 | } else { |
420 | eval('$addr = $this->' . $arg . ';') ; |
421 | if (is_array($addr)) { |
422 | foreach ($addr as $next_addr) { |
423 | if (is_object($next_addr)) { |
424 | if (isset($next_addr->host) && ($next_addr->host != '')) { |
425 | $email = $next_addr->mailbox . '@' . $next_addr->host; |
426 | } else { |
427 | $email = $next_addr->mailbox; |
428 | } |
429 | $email = strtolower($email); |
430 | if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) { |
431 | $arr[$email] = $next_addr->personal; |
432 | } |
433 | } |
434 | } |
435 | } else { |
436 | if (is_object($addr)) { |
437 | $email = $addr->mailbox; |
438 | $email .= (isset($addr->host) ? '@' . $addr->host : ''); |
e219fb97 |
439 | $email = strtolower($email); |
b65ca362 |
440 | if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) { |
441 | $arr[$email] = $addr->personal; |
442 | } |
443 | } |
444 | } |
445 | } |
e219fb97 |
446 | return $arr; |
8f6b86cc |
447 | } |
e219fb97 |
448 | |
b65ca362 |
449 | function getContentType($type0, $type1) { |
e8bedb2d |
450 | $type0 = $this->content_type->type0; |
b65ca362 |
451 | $type1 = $this->content_type->type1; |
e219fb97 |
452 | return $this->content_type->properties; |
e8bedb2d |
453 | } |
8f6b86cc |
454 | } |
455 | |
b65ca362 |
456 | class msg_header { |
e8bedb2d |
457 | /** msg_header contains all variables available in a bodystructure **/ |
458 | /** entity like described in rfc2060 **/ |
459 | |
e219fb97 |
460 | var $type0 = '', |
461 | $type1 = '', |
b65ca362 |
462 | $parameters = array(), |
e219fb97 |
463 | $id = 0, |
464 | $description = '', |
465 | $encoding='', |
b65ca362 |
466 | $size = 0, |
e219fb97 |
467 | $md5='', |
468 | $disposition = '', |
b65ca362 |
469 | $language=''; |
e219fb97 |
470 | |
471 | /* |
e8bedb2d |
472 | * returns addres_list of supplied argument |
473 | * arguments: array('to', 'from', ...) or just a string like 'to'. |
474 | * result: string: address1, addres2, .... |
e219fb97 |
475 | */ |
476 | |
b65ca362 |
477 | function setVar($var, $value) { |
e8bedb2d |
478 | $this->{$var} = $value; |
479 | } |
e219fb97 |
480 | |
b65ca362 |
481 | function getParameter($p) { |
482 | $value = strtolower($p); |
483 | return (isset($this->parameters[$p]) ? $this->parameters[$p] : ''); |
e8bedb2d |
484 | } |
e219fb97 |
485 | |
b65ca362 |
486 | function setParameter($parameter, $value) { |
e8bedb2d |
487 | $this->parameters[strtolower($parameter)] = $value; |
488 | } |
489 | } |
490 | |
491 | |
492 | |
b65ca362 |
493 | class address_structure { |
494 | var $personal = '', |
495 | $adl = '', |
496 | $mailbox = '', |
497 | $host = '', |
498 | $group = ''; |
8f6b86cc |
499 | |
b65ca362 |
500 | function getAddress($full = true) { |
501 | $result = ''; |
502 | |
503 | if (is_object($this)) { |
20760c79 |
504 | if (isset($this->host) && ($this->host != '')) { |
b65ca362 |
505 | $email = $this->mailbox.'@'.$this->host; |
506 | } else { |
507 | $email = $this->mailbox; |
508 | } |
509 | if (trim($this->personal) != '') { |
510 | if ($email) { |
511 | $addr = '"' . $this->personal . '" <' .$email.'>'; |
512 | } else { |
513 | $addr = $this->personal; |
514 | } |
515 | $best_dpl = $this->personal; |
516 | } else { |
517 | $addr = $email; |
518 | $best_dpl = $email; |
519 | } |
520 | $result = ($full ? $addr : $best_dpl); |
319cf0d2 |
521 | } |
e219fb97 |
522 | return $result; |
b65ca362 |
523 | } |
8f6b86cc |
524 | } |
525 | |
b65ca362 |
526 | class message { |
8f6b86cc |
527 | /** message is the object that contains messages. It is a recursive |
b65ca362 |
528 | object in that through the $entities variable, it can contain |
529 | more objects of type message. See documentation in mime.txt for |
530 | a better description of how this works. |
8f6b86cc |
531 | **/ |
e219fb97 |
532 | var $rfc822_header = '', |
e8bedb2d |
533 | $mime_header = '', |
b65ca362 |
534 | $flags = '', |
e8bedb2d |
535 | $type0='', |
e219fb97 |
536 | $type1='', |
537 | $entities = array(), |
538 | $parent_ent, $entity, |
b65ca362 |
539 | $parent = '', $decoded_body='', |
540 | $is_seen = 0, $is_answered = 0, $is_deleted = 0, $is_flagged = 0, |
541 | $is_mdnsent = 0, |
542 | $body_part = '', |
543 | $offset = 0, /* for fetching body parts out of raw messages */ |
544 | $length = 0; /* for fetching body parts out of raw messages */ |
545 | |
546 | function setEnt($ent) { |
8f6b86cc |
547 | $this->entity_id= $ent; |
548 | } |
e219fb97 |
549 | |
b65ca362 |
550 | function addEntity ($msg) { |
8f6b86cc |
551 | $msg->parent = &$this; |
552 | $this->entities[] = $msg; |
553 | } |
554 | |
b65ca362 |
555 | function getFilename() { |
556 | $filename = ''; |
557 | |
558 | if (is_object($this->header->disposition)) { |
559 | $filename = $this->header->disposition->getproperty('filename'); |
560 | if (!$filename) { |
561 | $filename = $this->header->disposition->getproperty('name'); |
562 | } |
563 | } |
b65ca362 |
564 | if (!$filename) { |
565 | $filename = 'untitled-'.$this->entity_id; |
566 | } |
b65ca362 |
567 | return $filename; |
812843b7 |
568 | } |
569 | |
570 | |
b65ca362 |
571 | function addRFC822Header($read) { |
572 | $header = new rfc822_header(); |
573 | $this->rfc822_header = $header->parseHeader($read); |
8f6b86cc |
574 | } |
575 | |
b65ca362 |
576 | function getEntity($ent) { |
8f6b86cc |
577 | $cur_ent = $this->entity_id; |
b65ca362 |
578 | $msg = $this; |
20760c79 |
579 | if (($cur_ent == '') || ($cur_ent == '0')) { |
b65ca362 |
580 | $cur_ent_a = array(); |
581 | } else { |
582 | $cur_ent_a = explode('.', $this->entity_id); |
583 | } |
584 | $ent_a = explode('.', $ent); |
e219fb97 |
585 | |
b65ca362 |
586 | $cnt = count($ent_a); |
587 | |
588 | for ($i = 0; $i < $cnt -1; ++$i) { |
589 | if (isset($cur_ent_a[$i]) && ($cur_ent_a[$i] != $ent_a[$i])) { |
590 | $msg = $msg->parent; |
591 | $cur_ent_a = explode('.', $msg->entity_id); |
592 | --$i; |
593 | } else if (!isset($cur_ent_a[$i])) { |
594 | if (isset($msg->entities[($ent_a[$i]-1)])) { |
595 | $msg = $msg->entities[($ent_a[$i]-1)]; |
596 | } else { |
597 | $msg = $msg->entities[0]; |
598 | } |
599 | } |
600 | if (($msg->type0 == 'message') && ($msg->type1 == 'rfc822')) { |
601 | /*this is a header for a message/rfc822 entity */ |
602 | $msg = $msg->entities[0]; |
603 | } |
604 | } |
605 | |
606 | if (($msg->type0 == 'message') && ($msg->type1 == 'rfc822')) { |
607 | /*this is a header for a message/rfc822 entity */ |
608 | $msg = $msg->entities[0]; |
609 | } |
610 | |
611 | if (isset($msg->entities[($ent_a[$cnt-1])-1])) { |
612 | if (is_object($msg->entities[($ent_a[$cnt-1])-1])) { |
613 | $msg = $msg->entities[($ent_a[$cnt-1]-1)]; |
614 | } |
615 | } |
08c18186 |
616 | |
319cf0d2 |
617 | return $msg; |
8f6b86cc |
618 | } |
e8bedb2d |
619 | |
b65ca362 |
620 | function setBody($s) { |
e8bedb2d |
621 | $this->body_part = $s; |
622 | } |
e219fb97 |
623 | |
b65ca362 |
624 | function clean_up() { |
e8bedb2d |
625 | $msg = $this; |
b65ca362 |
626 | $msg->body_part = ''; |
627 | |
20760c79 |
628 | foreach ($msg->entities as $m) { |
629 | $m->clean_up(); |
b65ca362 |
630 | } |
e8bedb2d |
631 | } |
e219fb97 |
632 | |
b65ca362 |
633 | function getMailbox() { |
634 | $msg = $this; |
635 | while (is_object($msg->parent)) { |
636 | $msg = $msg->parent; |
637 | } |
638 | return $msg->mailbox; |
319cf0d2 |
639 | } |
e219fb97 |
640 | |
b65ca362 |
641 | function calcEntity($msg) { |
642 | if (($this->type0 == 'message') && ($this->type1 == 'rfc822')) { |
643 | $msg->entity_id = $this->entity_id .'.0'; /* header of message/rfc822 */ |
644 | } else if (isset($this->entity_id) && ($this->entity_id != '')) { |
645 | $ent_no = count($this->entities)+1; |
646 | $par_ent = substr($this->entity_id, -2); |
647 | if ($par_ent{0} == '.') { |
648 | $par_ent = $par_ent{1}; |
649 | } |
650 | if ($par_ent == '0') { |
651 | $ent_no = count($this->entities) + 1; |
652 | if ($ent_no > 0) { |
20760c79 |
653 | $ent = substr($this->entity_id, 0, strrpos($this->entity_id, '.')); |
b65ca362 |
654 | $ent = ($ent ? $ent . ".$ent_no" : $ent_no); |
655 | $msg->entity_id = $ent; |
656 | } else { |
657 | $msg->entity_id = $ent_no; |
e219fb97 |
658 | } |
b65ca362 |
659 | } else { |
660 | $ent = $this->entity_id . ".$ent_no"; |
661 | $msg->entity_id = $ent; |
662 | } |
663 | } else { |
664 | $msg->entity_id = '0'; |
665 | } |
666 | |
667 | return $msg->entity_id; |
c52ffd66 |
668 | } |
e219fb97 |
669 | |
670 | |
671 | /* |
672 | * Bodystructure parser, a recursive function for generating the |
8f6b86cc |
673 | * entity-tree with all the mime-parts. |
e219fb97 |
674 | * |
8f6b86cc |
675 | * It follows RFC2060 and stores all the described fields in the |
e219fb97 |
676 | * message object. |
8f6b86cc |
677 | * |
678 | * Question/Bugs: |
e219fb97 |
679 | * |
8f6b86cc |
680 | * Ask for me (Marc Groot Koerkamp, stekkel@users.sourceforge.net. |
681 | * |
682 | */ |
b65ca362 |
683 | function parseStructure($read, $i = 0) { |
684 | $arg_no = 0; |
685 | $arg_a = array(); |
686 | $cnt = strlen($read); |
687 | |
688 | for (; $i < $cnt; ++$i) { |
e219fb97 |
689 | $char = strtoupper($read{$i}); |
b65ca362 |
690 | switch ($char) { |
691 | case '(': |
692 | if ($arg_no == 0) { |
693 | if (!isset($msg)) { |
694 | $msg = new message(); |
695 | $hdr = new msg_header(); |
696 | $hdr->type0 = 'text'; |
697 | $hdr->type1 = 'plain'; |
698 | $hdr->encoding = 'us-ascii'; |
699 | $msg->entity_id = $this->calcEntity($msg); |
700 | } else { |
701 | $msg->header->type0 = 'multipart'; |
702 | $msg->type0 = 'multipart'; |
703 | while ($read{$i} == '(') { |
704 | $res = $msg->parseStructure($read, $i); |
705 | $i = $res[1]; |
706 | $msg->addEntity($res[0]); |
707 | } |
708 | } |
709 | } else { |
710 | switch ($arg_no) { |
711 | case 1: |
712 | /* multipart properties */ |
713 | ++$i; |
714 | $res = $this->parseProperties($read, $i); |
715 | $arg_a[] = $res[0]; |
716 | $i = $res[1]; |
717 | ++$arg_no; |
718 | break; |
719 | case 2: |
720 | if (isset($msg->type0) && ($msg->type0 == 'multipart')) { |
721 | ++$i; |
722 | $res = $msg->parseDisposition($read, $i); |
723 | $arg_a[] = $res[0]; |
724 | $i = $res[1]; |
725 | } else { /* properties */ |
726 | $res = $msg->parseProperties($read, $i); |
727 | $arg_a[] = $res[0]; |
728 | $i = $res[1]; |
729 | } |
730 | ++$arg_no; |
731 | break; |
732 | case 3: |
733 | if (isset($msg->type0) && ($msg->type0 == 'multipart')) { |
734 | ++$i; |
735 | $res= $msg->parseLanguage($read, $i); |
736 | $arg_a[] = $res[0]; |
737 | $i = $res[1]; |
738 | } |
739 | case 7: |
740 | if (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822')) { |
741 | $msg->header->type0 = $arg_a[0]; |
742 | $msg->type0 = $arg_a[0]; |
743 | $msg->header->type1 = $arg_a[1]; |
744 | $msg->type1 = $arg_a[1]; |
745 | $rfc822_hdr = new rfc822_header(); |
746 | $res = $msg->parseEnvelope($read, $i, $rfc822_hdr); |
747 | $i = $res[1] + 1; |
748 | $msg->rfc822_header = $res[0]; |
749 | while (($i < $cnt) && ($read{$i} != '(')) { |
750 | ++$i; |
751 | } |
752 | $res = $msg->parseStructure($read, $i); |
753 | $i = $res[1]; |
754 | $msg->addEntity($res[0]); |
755 | } |
756 | break; |
757 | case 8: |
758 | ++$i; |
759 | $res = $msg->parseDisposition($read, $i); |
760 | $arg_a[] = $res[0]; |
761 | $i = $res[1]; |
762 | ++$arg_no; |
763 | break; |
764 | case 9: |
765 | ++$i; |
766 | if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) { |
767 | $res = $msg->parseDisposition($read, $i); |
768 | } else { |
769 | $res = $msg->parseLanguage($read, $i); |
770 | } |
771 | $arg_a[] = $res[0]; |
772 | $i = $res[1]; |
773 | ++$arg_no; |
774 | break; |
775 | case 10: |
776 | if (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))) { |
777 | ++$i; |
778 | $res = $msg->parseLanguage($read, $i); |
779 | $arg_a[] = $res[0]; |
780 | $i = $res[1]; |
781 | } else { |
782 | $i = $msg->parseParenthesis($read, $i); |
783 | $arg_a[] = ''; /* not yet desribed in rfc2060 */ |
784 | } |
785 | ++$arg_no; |
e219fb97 |
786 | break; |
b65ca362 |
787 | default: |
788 | /* unknown argument, skip this part */ |
789 | $i = $msg->parseParenthesis($read, $i); |
790 | $arg_a[] = ''; |
791 | ++$arg_no; |
792 | break; |
793 | } /* switch */ |
794 | } |
795 | break; |
796 | case '"': |
797 | /* inside an entity -> start processing */ |
798 | $debug = substr($read, $i, 20); |
799 | $res = $msg->parseQuote($read, $i); |
800 | $arg_s = $res[0]; |
801 | $i = $res[1]; |
802 | ++$arg_no; |
803 | if ($arg_no < 3) { |
804 | $arg_s = strtolower($arg_s); /* type0 and type1 */ |
805 | } |
806 | $arg_a[] = $arg_s; |
807 | break; |
e219fb97 |
808 | case 'n': |
b65ca362 |
809 | case 'N': |
810 | /* probably NIL argument */ |
e219fb97 |
811 | if (strtoupper(substr($read, $i, 4)) == 'NIL ') { |
b65ca362 |
812 | $arg_a[] = ''; |
813 | ++$arg_no; |
814 | $i += 2; |
815 | } |
816 | break; |
817 | case '{': |
818 | /* process the literal value */ |
819 | $res = $msg->parseLiteral($read, $i); |
820 | $arg_s = $res[0]; |
821 | $i = $res[1]; |
822 | ++$arg_no; |
823 | break; |
824 | case is_numeric($read{$i}): |
825 | /* process integers */ |
826 | if ($read{$i} == ' ') { break; } |
827 | $arg_s = $read{$i};; |
828 | for (++$i; preg_match('/^[0-9]{1}$/', $read{$i}); ++$i) { |
829 | $arg_s .= $read{$i}; |
830 | } |
831 | ++$arg_no; |
832 | $arg_a[] = $arg_s; |
833 | break; |
834 | case ')': |
835 | $multipart = (isset($msg->type0) && ($msg->type0 == 'multipart')); |
836 | if (!$multipart) { |
837 | $shifted_args = (($arg_a[0] == 'text') || (($arg_a[0] == 'message') && ($arg_a[1] == 'rfc822'))); |
838 | $hdr->type0 = $arg_a[0]; |
e219fb97 |
839 | $hdr->type1 = $arg_a[1]; |
b65ca362 |
840 | |
841 | $msg->type0 = $arg_a[0]; |
842 | $msg->type1 = $arg_a[1]; |
843 | |
844 | $arr = $arg_a[2]; |
845 | if (is_array($arr)) { |
846 | $hdr->parameters = $arg_a[2]; |
847 | } |
848 | $hdr->id = str_replace('<', '', str_replace('>', '', $arg_a[3])); |
849 | $hdr->description = $arg_a[4]; |
850 | $hdr->encoding = strtolower($arg_a[5]); |
851 | $hdr->entity_id = $msg->entity_id; |
e219fb97 |
852 | $hdr->size = $arg_a[6]; |
b65ca362 |
853 | if ($shifted_args) { |
854 | $hdr->lines = $arg_a[7]; |
855 | $s = 1; |
856 | } else { |
857 | $s = 0; |
858 | } |
859 | $hdr->md5 = (isset($arg_a[7+$s]) ? $arg_a[7+$s] : $hdr->md5); |
860 | $hdr->disposition = (isset($arg_a[8+$s]) ? $arg_a[8+$s] : $hdr->disposition); |
861 | $hdr->language = (isset($arg_a[9+$s]) ? $arg_a[9+$s] : $hdr->language); |
862 | $msg->header = $hdr; |
bc185bd2 |
863 | // $arg_no = 0; |
b65ca362 |
864 | ++$i; |
865 | if ((substr($msg->entity_id, -2) == '.0') && ($msg->type0 !='multipart')) { |
866 | $msg->entity_id++; |
867 | } |
868 | } else { |
869 | $hdr->type0 = 'multipart'; |
870 | $hdr->type1 = $arg_a[0]; |
871 | $msg->type0 = 'multipart'; |
872 | $msg->type1 = $arg_a[0]; |
bc185bd2 |
873 | $hdr->parameters = (isset($arg_a[1]) ? $arg_a[1] : $hdr->parameters); |
874 | $hdr->disposition = (isset($arg_a[2]) ? $arg_a[2] : $hdr->disposition); |
875 | $hdr->language = (isset($arg_a[3]) ? $arg_a[3] : $hdr->language); |
b65ca362 |
876 | $msg->header = $hdr; |
877 | } |
878 | return (array($msg, $i)); |
879 | default: break; |
880 | } /* switch */ |
881 | |
882 | } /* for */ |
319cf0d2 |
883 | } /* parsestructure */ |
e219fb97 |
884 | |
b65ca362 |
885 | function parseProperties($read, $i) { |
886 | $properties = array(); |
887 | $prop_name = ''; |
888 | $arg_s = ''; |
889 | |
890 | for (; $read{$i} != ')'; ++$i) { |
891 | if ($read{$i} == '"') { |
892 | $res = $this->parseQuote($read, $i); |
893 | $arg_s = $res[0]; |
894 | $i = $res[1]; |
895 | } else if ($read{$i} == '{') { |
896 | $res = $this->parseLiteral($read, $i); |
897 | $arg_s = $res[0]; |
898 | $i = $res[1]; |
899 | } |
900 | |
901 | if (($prop_name == '') && $arg_s) { |
902 | $prop_name = strtolower($arg_s); |
903 | $properties[$prop_name] = ''; |
904 | $arg_s = ''; |
905 | } else if (($prop_name != '') && ($arg_s != '')) { |
906 | $properties[$prop_name] = $arg_s; |
907 | $prop_name = ''; |
908 | $arg_s = ''; |
909 | } |
910 | } |
911 | |
912 | return (array($properties, $i)); |
8f6b86cc |
913 | } |
e219fb97 |
914 | |
b65ca362 |
915 | function parseEnvelope($read, $i, $hdr) { |
8f6b86cc |
916 | $arg_no = 0; |
b65ca362 |
917 | $arg_a = array(); |
918 | $cnt = strlen($read); |
919 | |
920 | for (; ($i < $cnt) && ($read{$i} != ')'); ++$i) { |
921 | ++$i; |
922 | $char = strtoupper($read{$i}); |
923 | switch ($char) { |
924 | case '"': |
925 | $res = $this->parseQuote($read, $i); |
926 | $arg_a[] = $res[0]; |
927 | $i = $res[1]; |
928 | ++$arg_no; |
929 | break; |
930 | case '{': |
931 | $res = $this->parseLiteral($read, $i); |
932 | $arg_a[] = $res[0]; |
933 | $i = $res[1]; |
934 | ++$arg_no; |
935 | break; |
e219fb97 |
936 | case 'N': |
b65ca362 |
937 | /* probably NIL argument */ |
938 | if (strtoupper(substr($read, $i, 3)) == 'NIL') { |
939 | $arg_a[] = ''; |
940 | ++$arg_no; |
941 | $i += 2; |
e219fb97 |
942 | } |
b65ca362 |
943 | break; |
944 | case '(': |
945 | /* Address structure (with group support) |
946 | * Note: Group support is useless on SMTP connections |
947 | * because the protocol doesn't support it |
948 | */ |
949 | $addr_a = array(); |
950 | $group = ''; |
951 | $a=0; |
952 | for (; $i < $cnt && $read{$i} != ')'; ++$i) { |
953 | if ($read{$i} == '(') { |
954 | $res = $this->parseAddress($read, $i); |
955 | $addr = $res[0]; |
956 | $i = $res[1]; |
957 | if (($addr->host == '') && ($addr->mailbox != '')) { |
958 | /* start of group */ |
959 | $group = $addr->mailbox; |
960 | $group_addr = $addr; |
961 | $j = $a; |
962 | } else if ($group && ($addr->host == '') && ($addr->mailbox == '')) { |
963 | /* end group */ |
964 | if ($a == ($j+1)) { /* no group members */ |
965 | $group_addr->group = $group; |
966 | $group_addr->mailbox = ''; |
967 | $group_addr->personal = "$group: Undisclosed recipients;"; |
968 | $addr_a[] = $group_addr; |
969 | $group =''; |
970 | } |
971 | } else { |
972 | $addr->group = $group; |
973 | $addr_a[] = $addr; |
974 | } |
975 | ++$a; |
976 | } |
977 | } |
978 | $arg_a[] = $addr_a; |
979 | break; |
980 | default: break; |
981 | } |
982 | } |
983 | |
984 | if (count($arg_a) > 9) { |
985 | /* argument 1: date */ |
8f6b86cc |
986 | $d = strtr($arg_a[0], array(' ' => ' ')); |
987 | $d = explode(' ', $d); |
988 | $hdr->date = getTimeStamp($d); |
b65ca362 |
989 | |
990 | /* argument 2: subject */ |
991 | $arg_a[1] = (!trim($arg_a[1]) ? _("(no subject)") : $arg_a[1]); |
992 | $hdr->subject = $arg_a[1]; |
993 | |
994 | $hdr->from = $arg_a[2][0]; /* argument 3: from */ |
995 | $hdr->sender = $arg_a[3][0]; /* argument 4: sender */ |
996 | $hdr->replyto = $arg_a[4][0]; /* argument 5: reply-to */ |
997 | $hdr->to = $arg_a[5]; /* argument 6: to */ |
998 | $hdr->cc = $arg_a[6]; /* argument 7: cc */ |
999 | $hdr->bcc = $arg_a[7]; /* argument 8: bcc */ |
1000 | $hdr->inreplyto = $arg_a[8]; /* argument 9: in-reply-to */ |
1001 | $hdr->message_id = $arg_a[9]; /* argument 10: message-id */ |
1002 | } |
1003 | return (array($hdr, $i)); |
8f6b86cc |
1004 | } |
e219fb97 |
1005 | |
b65ca362 |
1006 | function parseLiteral($read, $i) { |
1007 | $lit_cnt = ''; |
1008 | for (++$i; $read{$i} != '}'; ++$i) { |
1009 | $lit_cnt .= $read{$i}; |
1010 | } |
1011 | |
1012 | $lit_cnt +=2; /* add the { and } characters */ |
1013 | $s = ''; |
1014 | for ($j = 0; $j < $lit_cnt; ++$j) { |
1015 | $s .= $read{++$i}; |
1016 | } |
1017 | return (array($s, $i)); |
8f6b86cc |
1018 | } |
e219fb97 |
1019 | |
b65ca362 |
1020 | function parseQuote($read, $i) { |
1021 | $s = ''; |
1022 | for (++$i; $read{$i} != '"'; ++$i) { |
1023 | if ($read{$i} == '\\') { |
1024 | ++$i; |
e219fb97 |
1025 | } |
b65ca362 |
1026 | $s .= $read{$i}; |
1027 | } |
e219fb97 |
1028 | return (array($s, $i)); |
8f6b86cc |
1029 | } |
e219fb97 |
1030 | |
b65ca362 |
1031 | function parseAddress($read, $i) { |
1032 | $arg_a = array(); |
1033 | |
1034 | for (; $read{$i} != ')'; ++$i) { |
1035 | $char = strtoupper($read{$i}); |
1036 | switch ($char) { |
1037 | case '"': |
e219fb97 |
1038 | case '{': |
b65ca362 |
1039 | $res = ($char == '"' ? $this->parseQuote($read, $i) : $this->parseLiteral($read, $i)); |
1040 | $arg_a[] = $res[0]; |
1041 | $i = $res[1]; |
1042 | break; |
1043 | case 'n': |
e219fb97 |
1044 | case 'N': |
b65ca362 |
1045 | if (strtoupper(substr($read, $i, 3)) == 'NIL') { |
1046 | $arg_a[] = ''; |
1047 | $i += 2; |
1048 | } |
1049 | break; |
1050 | default: break; |
1051 | } |
1052 | } |
1053 | |
1054 | if (count($arg_a) == 4) { |
1055 | $adr = new address_structure(); |
1056 | $adr->personal = $arg_a[0]; |
1057 | $adr->adl = $arg_a[1]; |
1058 | $adr->mailbox = $arg_a[2]; |
1059 | $adr->host = $arg_a[3]; |
1060 | } else { |
1061 | $adr = ''; |
1062 | } |
1063 | return (array($adr, $i)); |
8f6b86cc |
1064 | } |
e219fb97 |
1065 | |
b65ca362 |
1066 | function parseDisposition($read, $i) { |
8f6b86cc |
1067 | $arg_a = array(); |
b65ca362 |
1068 | |
1069 | for (; $read{$i} != ')'; ++$i) { |
1070 | switch ($read{$i}) { |
1071 | case '"': |
e219fb97 |
1072 | case '{': |
b65ca362 |
1073 | case '(': |
1074 | switch ($read{$i}) { |
1075 | case '"': $res = $this->parseQuote($read, $i); break; |
1076 | case '{': $res = $this->parseLiteral($read, $i); break; |
1077 | case '(': $res = $this->parseProperties($read, $i); break; |
1078 | } |
1079 | $arg_a[] = $res[0]; |
1080 | $i = $res[1]; |
1081 | break; |
1082 | default: break; |
1083 | } |
e02d0fa7 |
1084 | } |
b65ca362 |
1085 | |
1086 | if (isset($arg_a[0])) { |
1087 | $disp = new disposition($arg_a[0]); |
1088 | if (isset($arg_a[1])) { |
1089 | $disp->properties = $arg_a[1]; |
1090 | } |
1091 | } |
1092 | |
1093 | return (is_object($disp) ? array($disp, $i) : array('', $i)); |
8f6b86cc |
1094 | } |
e219fb97 |
1095 | |
b65ca362 |
1096 | function parseLanguage($read, $i) { |
08c18186 |
1097 | /* no idea how to process this one without examples */ |
31e0cfba |
1098 | $arg_a = array(); |
b65ca362 |
1099 | |
1100 | for (; $read{$i} != ')'; ++$i) { |
1101 | switch ($read{$i}) { |
1102 | case '"': |
e219fb97 |
1103 | case '{': |
b65ca362 |
1104 | case '(': |
1105 | switch ($read{$i}) { |
1106 | case '"': $res = $this->parseQuote($read, $i); break; |
1107 | case '{': $res = $this->parseLiteral($read, $i); break; |
1108 | case '(': $res = $this->parseProperties($read, $i); break; |
1109 | } |
1110 | $arg_a[] = $res[0]; |
1111 | $i = $res[1]; |
1112 | break; |
1113 | default: break; |
1114 | } |
08c18186 |
1115 | } |
b65ca362 |
1116 | |
1117 | if (isset($arg_a[0])) { |
1118 | $lang = new language($arg_a[0]); |
1119 | if (isset($arg_a[1])) { |
1120 | $lang->properties = $arg_a[1]; |
1121 | } |
31e0cfba |
1122 | } |
b65ca362 |
1123 | |
1124 | return (is_object($lang) ? array($lang, $i) : array('', $i)); |
8f6b86cc |
1125 | } |
e219fb97 |
1126 | |
b65ca362 |
1127 | function parseParenthesis($read, $i) { |
1128 | for (; $read{$i} != ')'; ++$i) { |
1129 | switch ($read{$i}) { |
1130 | case '"': |
e219fb97 |
1131 | case '{': |
b65ca362 |
1132 | case '(': |
1133 | switch ($read{$i}) { |
1134 | case '"': $res = $this->parseQuote($read, $i); break; |
1135 | case '{': $res = $this->parseLiteral($read, $i); break; |
1136 | case '(': $res = $this->parseProperties($read, $i); break; |
1137 | } |
1138 | $i = $res[1]; |
1139 | break; |
1140 | default: break; |
1141 | } |
e219fb97 |
1142 | } |
b65ca362 |
1143 | return $i; |
c52ffd66 |
1144 | } |
1145 | |
b65ca362 |
1146 | /* Function to fill the message structure in case the */ |
1147 | /* bodystructure is not available NOT FINISHED YET */ |
1148 | function parseMessage($read, $type0, $type1) { |
1149 | switch ($type0) { |
1150 | case 'message': |
1151 | $rfc822_header = true; |
1152 | $mime_header = false; |
1153 | break; |
1154 | case 'multipart': |
1155 | $rfc822_header = false; |
1156 | $mime_header = true; |
1157 | break; |
1158 | default: return $read; |
1159 | } |
1160 | |
1161 | for ($i = 1; $i < $count; ++$i) { |
1162 | $line = trim($body[$i]); |
e219fb97 |
1163 | if (($mime_header || $rfc822_header) && |
b65ca362 |
1164 | (preg_match("/^.*boundary=\"?(.+(?=\")|.+).*/i", $line, $reg))) { |
1165 | $bnd = $reg[1]; |
e219fb97 |
1166 | $bndreg = $bnd; |
1167 | $bndreg = str_replace("\\", "\\\\", $bndreg); |
b65ca362 |
1168 | $bndreg = str_replace("?", "\\?", $bndreg); |
e219fb97 |
1169 | $bndreg = str_replace("+", "\\+", $bndreg); |
1170 | $bndreg = str_replace(".", "\\.", $bndreg); |
b65ca362 |
1171 | $bndreg = str_replace("/", "\\/", $bndreg); |
1172 | $bndreg = str_replace("-", "\\-", $bndreg); |
1173 | $bndreg = str_replace("(", "\\(", $bndreg); |
1174 | $bndreg = str_replace(")", "\\)", $bndreg); |
1175 | } else if ($rfc822_header && $line == '') { |
1176 | $rfc822_header = false; |
1177 | if ($msg->type0 == 'multipart') { |
1178 | $mime_header = true; |
1179 | } |
e219fb97 |
1180 | } |
1181 | |
b65ca362 |
1182 | if ((($line{0} == '-') || $rfc822_header) && isset($boundaries[0])) { |
1183 | $cnt=count($boundaries)-1; |
1184 | $bnd = $boundaries[$cnt]['bnd']; |
1185 | $bndreg = $boundaries[$cnt]['bndreg']; |
e219fb97 |
1186 | |
b65ca362 |
1187 | $regstr = '/^--'."($bndreg)".".*".'/'; |
1188 | if (preg_match($regstr, $line, $reg)) { |
1189 | $bndlen = strlen($reg[1]); |
e219fb97 |
1190 | $bndend = false; |
b65ca362 |
1191 | if (strlen($line) > ($bndlen + 3)) { |
1192 | if (($line{$bndlen+2} == '-') && ($line{$bndlen+3} == '-')) { |
1193 | $bndend = true; |
1194 | } |
1195 | } |
1196 | if ($bndend) { |
1197 | /* calc offset and return $msg */ |
1198 | //$entStr = CalcEntity("$entStr", -1); |
1199 | array_pop($boundaries); |
1200 | $mime_header = true; |
1201 | $bnd_end = true; |
1202 | } else { |
1203 | $mime_header = true; |
1204 | $bnd_end = false; |
1205 | //$entStr = CalcEntity("$entStr", 0); |
1206 | ++$content_indx; |
1207 | } |
1208 | } else { |
1209 | if ($header) { } |
1210 | } |
e219fb97 |
1211 | } |
b65ca362 |
1212 | } |
319cf0d2 |
1213 | } |
1214 | |
b65ca362 |
1215 | function findDisplayEntity($entity = array(), $alt_order = array('text/plain', 'text/html')) { |
8f288482 |
1216 | $found = false; |
1217 | $type = $this->type0.'/'.$this->type1; |
b65ca362 |
1218 | if ($type == 'multipart/alternative') { |
8f288482 |
1219 | $msg = $this->findAlternativeEntity($alt_order); |
b65ca362 |
1220 | if (count($msg->entities) == 0) { |
8f288482 |
1221 | $entity[] = $msg->entity_id; |
b65ca362 |
1222 | } else { |
8f288482 |
1223 | $entity = $msg->findDisplayEntity($entity, $alt_order); |
1224 | } |
1225 | $found = true; |
b65ca362 |
1226 | } else if ($type == 'multipart/related') { |
319cf0d2 |
1227 | $msgs = $this->findRelatedEntity(); |
b65ca362 |
1228 | foreach ($msgs as $msg) { |
1229 | if (count($msg->entities) == 0) { |
8f288482 |
1230 | $entity[] = $msg->entity_id; |
b65ca362 |
1231 | } else { |
1232 | $entity = $msg->findDisplayEntity($entity, $alt_order); |
8f288482 |
1233 | } |
1234 | } |
1235 | if (count($msgs) > 0) { |
1236 | $found = true; |
1237 | } |
b65ca362 |
1238 | } else if (($this->type0 == 'text') && |
1239 | (($this->type1 == 'plain') || |
1240 | ($this->type1 == 'html') || |
1241 | ($this->type1 == 'message')) && |
1242 | isset($this->entity_id)) { |
1243 | if (count($this->entities) == 0) { |
1244 | if (strtolower($this->header->disposition->name) != 'attachment') { |
8f288482 |
1245 | $entity[] = $this->entity_id; |
1246 | } |
1247 | } |
1248 | } |
1249 | $i = 0; |
b65ca362 |
1250 | if (!$found) { |
8f288482 |
1251 | foreach ($this->entities as $ent) { |
b65ca362 |
1252 | if((strtolower($ent->header->disposition->name) != 'attachment') && |
1253 | (($ent->type0 != 'message') && ($ent->type1 != 'rfc822'))) { |
8f288482 |
1254 | $entity = $ent->findDisplayEntity($entity, $alt_order); |
1255 | } |
1256 | } |
1257 | } |
b65ca362 |
1258 | |
1259 | /* CAN THIS BE REMOVED? IF SO, DO IT!!! */ |
1260 | //while (isset($this->entities[$i]) && !$found && |
1261 | // (strtolower($this->entities[$i]->header->disposition->name) |
1262 | // != 'attachment') && |
1263 | // ($this->entities[$i]->type0 != 'message' && |
1264 | // $this->entities[$i]->type1 != 'rfc822') |
1265 | // ) { |
1266 | // $entity = $this->entities[$i]->findDisplayEntity($entity, $alt_order); |
1267 | // ++$i; |
1268 | //} |
1269 | |
1270 | return ($entity); |
8f6b86cc |
1271 | } |
319cf0d2 |
1272 | |
b65ca362 |
1273 | function findAlternativeEntity($alt_order) { |
1274 | /* If we are dealing with alternative parts then we */ |
1275 | /* choose the best viewable message supported by SM. */ |
319cf0d2 |
1276 | $best_view = 0; |
8f288482 |
1277 | $entity = array(); |
1278 | $altcount = count($alt_order); |
b65ca362 |
1279 | |
1280 | foreach($this->entities as $ent) { |
1281 | $type = $ent->header->type0 . '/' . $ent->header->type1; |
1282 | if ($type == 'multipart/related') { |
8f288482 |
1283 | $type = $ent->header->getParameter('type'); |
1284 | } |
b65ca362 |
1285 | for ($j = $best_view; $j < $altcount; ++$j) { |
1286 | if (($alt_order[$j] == $type) && ($j >= $best_view)) { |
8f288482 |
1287 | $best_view = $j; |
1288 | $entity = $ent; |
1289 | } |
1290 | } |
319cf0d2 |
1291 | } |
b65ca362 |
1292 | |
8f288482 |
1293 | return $entity; |
319cf0d2 |
1294 | } |
e219fb97 |
1295 | |
b65ca362 |
1296 | function findRelatedEntity() { |
e219fb97 |
1297 | $msgs = array(); |
8f288482 |
1298 | $entcount = count($this->entities); |
b65ca362 |
1299 | |
1300 | for ($i = 0; $i < $entcount; ++$i) { |
319cf0d2 |
1301 | $type = $this->entities[$i]->header->type0.'/'.$this->entities[$i]->header->type1; |
b65ca362 |
1302 | if ($this->header->getParameter('type') == $type) { |
8f288482 |
1303 | $msgs[] = $this->entities[$i]; |
1304 | } |
319cf0d2 |
1305 | } |
b65ca362 |
1306 | |
319cf0d2 |
1307 | return $msgs; |
1308 | } |
e219fb97 |
1309 | |
b65ca362 |
1310 | function getAttachments($exclude_id=array(), $result = array()) { |
1311 | if (($this->type0 == 'message') && ($this->type1 == 'rfc822')) { |
1312 | $this = $this->entities[0]; |
1313 | } |
1314 | |
1315 | if (count($this->entities)) { |
1316 | foreach ($this->entities as $entity) { |
1317 | $exclude = false; |
1318 | |
1319 | foreach ($exclude_id as $excl) { |
1320 | if ($entity->entity_id === $excl) { |
1321 | $exclude = true; |
1322 | } |
1323 | } |
1324 | |
1325 | if (!$exclude) { |
e219fb97 |
1326 | if (($entity->type0 == 'multipart') && |
b65ca362 |
1327 | ($entity->type1 != 'related')) { |
1328 | $result = $entity->getAttachments($exclude_id, $result); |
1329 | } else if ($entity->type0 != 'multipart') { |
1330 | $result[] = $entity; |
1331 | } |
1332 | } |
1333 | } |
1334 | } else { |
1335 | $exclude = false; |
1336 | foreach ($exclude_id as $excl) { |
1337 | $exclude = $exclude || ($this->entity_id == $excl); |
1338 | } |
1339 | |
1340 | if (!$exclude) { |
1341 | $result[] = $this; |
1342 | } |
1343 | } |
1344 | |
1345 | return $result; |
e219fb97 |
1346 | } |
8f6b86cc |
1347 | } |
1348 | |
b65ca362 |
1349 | class smime_message { |
1350 | |
c52ffd66 |
1351 | } |
1352 | |
b65ca362 |
1353 | class disposition { |
1354 | function disposition($name) { |
e8bedb2d |
1355 | $this->name = $name; |
1356 | $this->properties = array(); |
1357 | } |
924b0cac |
1358 | |
b65ca362 |
1359 | function getProperty($par) { |
924b0cac |
1360 | $value = strtolower($par); |
b65ca362 |
1361 | if (isset($this->properties[$par])) { |
1362 | return $this->properties[$par]; |
924b0cac |
1363 | } |
1364 | return ''; |
1365 | } |
8f6b86cc |
1366 | } |
1367 | |
b65ca362 |
1368 | class language { |
1369 | function language($name) { |
e8bedb2d |
1370 | $this->name = $name; |
1371 | $this->properties = array(); |
1372 | } |
1373 | } |
1374 | |
b65ca362 |
1375 | class content_type { |
e219fb97 |
1376 | var $type0 = 'text', |
1377 | $type1 = 'plain', |
b65ca362 |
1378 | $properties = ''; |
1379 | |
1380 | function content_type($type) { |
1381 | $pos = strpos($type, '/'); |
1382 | if ($pos > 0) { |
1383 | $this->type0 = substr($type, 0, $pos); |
1384 | $this->type1 = substr($type, $pos+1); |
1385 | } else { |
1386 | $this->type0 = $type; |
1387 | } |
1388 | $this->properties = array(); |
c52ffd66 |
1389 | } |
31e0cfba |
1390 | } |
1391 | |
4b8699db |
1392 | ?> |