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