19d470aa |
1 | <?php |
2 | |
3 | /** |
4 | * Rfc822Header.class.php |
5 | * |
6c84ba1e |
6 | * Copyright (c) 2003-2005 The SquirrelMail Project Team |
19d470aa |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
8 | * |
9 | * This contains functions needed to handle mime messages. |
10 | * |
883d9cd3 |
11 | * @version $Id$ |
2b646597 |
12 | * @package squirrelmail |
19d470aa |
13 | */ |
14 | |
2b646597 |
15 | /** |
19d470aa |
16 | * input: header_string or array |
2b646597 |
17 | * @package squirrelmail |
19d470aa |
18 | */ |
19 | class Rfc822Header { |
2a9b0fad |
20 | var $date = -1, |
19d470aa |
21 | $subject = '', |
22 | $from = array(), |
23 | $sender = '', |
24 | $reply_to = array(), |
b268e66b |
25 | $mail_followup_to = array(), |
19d470aa |
26 | $to = array(), |
27 | $cc = array(), |
28 | $bcc = array(), |
29 | $in_reply_to = '', |
30 | $message_id = '', |
340d67c2 |
31 | $references = '', |
19d470aa |
32 | $mime = false, |
33 | $content_type = '', |
34 | $disposition = '', |
35 | $xmailer = '', |
36 | $priority = 3, |
37 | $dnt = '', |
f1232547 |
38 | $encoding = '', |
cfebb724 |
39 | $content_id = '', |
40 | $content_desc = '', |
19d470aa |
41 | $mlist = array(), |
42 | $more_headers = array(); /* only needed for constructing headers |
43 | in smtp.php */ |
44 | function parseHeader($hdr) { |
45 | if (is_array($hdr)) { |
46 | $hdr = implode('', $hdr); |
47 | } |
2a9b0fad |
48 | /* First we replace \r\n by \n and unfold the header */ |
49 | $hdr = trim(str_replace(array("\r\n", "\n\t", "\n "),array("\n", ' ', ' '), $hdr)); |
19d470aa |
50 | |
51 | /* Now we can make a new header array with */ |
52 | /* each element representing a headerline */ |
2a9b0fad |
53 | $hdr = explode("\n" , $hdr); |
19d470aa |
54 | foreach ($hdr as $line) { |
55 | $pos = strpos($line, ':'); |
56 | if ($pos > 0) { |
57 | $field = substr($line, 0, $pos); |
340d67c2 |
58 | if (!strstr($field,' ')) { /* valid field */ |
59 | $value = trim(substr($line, $pos+1)); |
60 | $this->parseField($field, $value); |
61 | } |
19d470aa |
62 | } |
63 | } |
64 | if ($this->content_type == '') { |
65 | $this->parseContentType('text/plain; charset=us-ascii'); |
66 | } |
67 | } |
68 | |
69 | function stripComments($value) { |
70 | $result = ''; |
19d470aa |
71 | $cnt = strlen($value); |
72 | for ($i = 0; $i < $cnt; ++$i) { |
73 | switch ($value{$i}) { |
74 | case '"': |
75 | $result .= '"'; |
76 | while ((++$i < $cnt) && ($value{$i} != '"')) { |
77 | if ($value{$i} == '\\') { |
78 | $result .= '\\'; |
79 | ++$i; |
80 | } |
81 | $result .= $value{$i}; |
82 | } |
83 | $result .= $value{$i}; |
84 | break; |
85 | case '(': |
86 | $depth = 1; |
87 | while (($depth > 0) && (++$i < $cnt)) { |
88 | switch($value{$i}) { |
89 | case '\\': |
90 | ++$i; |
91 | break; |
92 | case '(': |
93 | ++$depth; |
94 | break; |
95 | case ')': |
96 | --$depth; |
97 | break; |
98 | default: |
99 | break; |
100 | } |
101 | } |
102 | break; |
103 | default: |
104 | $result .= $value{$i}; |
105 | break; |
106 | } |
107 | } |
108 | return $result; |
109 | } |
110 | |
111 | function parseField($field, $value) { |
112 | $field = strtolower($field); |
113 | switch($field) { |
114 | case 'date': |
340d67c2 |
115 | $value = $this->stripComments($value); |
19d470aa |
116 | $d = strtr($value, array(' ' => ' ')); |
117 | $d = explode(' ', $d); |
118 | $this->date = getTimeStamp($d); |
119 | break; |
120 | case 'subject': |
121 | $this->subject = $value; |
122 | break; |
123 | case 'from': |
124 | $this->from = $this->parseAddress($value,true); |
125 | break; |
126 | case 'sender': |
127 | $this->sender = $this->parseAddress($value); |
128 | break; |
129 | case 'reply-to': |
130 | $this->reply_to = $this->parseAddress($value, true); |
131 | break; |
b268e66b |
132 | case 'mail-followup-to': |
133 | $this->mail_followup_to = $this->parseAddress($value, true); |
134 | break; |
19d470aa |
135 | case 'to': |
136 | $this->to = $this->parseAddress($value, true); |
137 | break; |
138 | case 'cc': |
139 | $this->cc = $this->parseAddress($value, true); |
140 | break; |
141 | case 'bcc': |
142 | $this->bcc = $this->parseAddress($value, true); |
143 | break; |
144 | case 'in-reply-to': |
145 | $this->in_reply_to = $value; |
146 | break; |
147 | case 'message-id': |
340d67c2 |
148 | $value = $this->stripComments($value); |
19d470aa |
149 | $this->message_id = $value; |
150 | break; |
340d67c2 |
151 | case 'references': |
152 | $value = $this->stripComments($value); |
153 | $this->references = $value; |
154 | break; |
155 | case 'x-confirm-reading-to': |
156 | case 'return-receipt-to': |
19d470aa |
157 | case 'disposition-notification-to': |
340d67c2 |
158 | $value = $this->stripComments($value); |
19d470aa |
159 | $this->dnt = $this->parseAddress($value); |
160 | break; |
161 | case 'mime-version': |
340d67c2 |
162 | $value = $this->stripComments($value); |
19d470aa |
163 | $value = str_replace(' ', '', $value); |
164 | $this->mime = ($value == '1.0' ? true : $this->mime); |
165 | break; |
166 | case 'content-type': |
340d67c2 |
167 | $value = $this->stripComments($value); |
19d470aa |
168 | $this->parseContentType($value); |
169 | break; |
170 | case 'content-disposition': |
340d67c2 |
171 | $value = $this->stripComments($value); |
19d470aa |
172 | $this->parseDisposition($value); |
173 | break; |
cfebb724 |
174 | case 'content-transfer-encoding': |
175 | $this->encoding = $value; |
176 | break; |
177 | case 'content-description': |
178 | $this->content_desc = $value; |
179 | break; |
180 | case 'content-id': |
181 | $value = $this->stripComments($value); |
182 | $this->content_id = $value; |
183 | break; |
19d470aa |
184 | case 'user-agent': |
185 | case 'x-mailer': |
340d67c2 |
186 | $this->xmailer = $value; |
19d470aa |
187 | break; |
188 | case 'x-priority': |
bddb3448 |
189 | case 'importance': |
190 | case 'priority': |
191 | $this->priority = $this->parsePriority($value); |
19d470aa |
192 | break; |
193 | case 'list-post': |
340d67c2 |
194 | $value = $this->stripComments($value); |
19d470aa |
195 | $this->mlist('post', $value); |
196 | break; |
197 | case 'list-reply': |
cfebb724 |
198 | $value = $this->stripComments($value); |
19d470aa |
199 | $this->mlist('reply', $value); |
200 | break; |
201 | case 'list-subscribe': |
cfebb724 |
202 | $value = $this->stripComments($value); |
19d470aa |
203 | $this->mlist('subscribe', $value); |
204 | break; |
205 | case 'list-unsubscribe': |
340d67c2 |
206 | $value = $this->stripComments($value); |
19d470aa |
207 | $this->mlist('unsubscribe', $value); |
208 | break; |
209 | case 'list-archive': |
340d67c2 |
210 | $value = $this->stripComments($value); |
19d470aa |
211 | $this->mlist('archive', $value); |
212 | break; |
213 | case 'list-owner': |
340d67c2 |
214 | $value = $this->stripComments($value); |
19d470aa |
215 | $this->mlist('owner', $value); |
216 | break; |
217 | case 'list-help': |
340d67c2 |
218 | $value = $this->stripComments($value); |
19d470aa |
219 | $this->mlist('help', $value); |
220 | break; |
ba4d5a32 |
221 | case 'list-id': |
222 | $value = $this->stripComments($value); |
223 | $this->mlist('id', $value); |
224 | break; |
19d470aa |
225 | default: |
226 | break; |
227 | } |
228 | } |
14882b16 |
229 | |
230 | function getAddressTokens($address) { |
231 | $aTokens = array(); |
14882b16 |
232 | $aSpecials = array('(' ,'<' ,',' ,';' ,':'); |
233 | $aReplace = array(' (',' <',' ,',' ;',' :'); |
234 | $address = str_replace($aSpecials,$aReplace,$address); |
55243181 |
235 | $iCnt = strlen($address); |
14882b16 |
236 | $i = 0; |
237 | while ($i < $iCnt) { |
238 | $cChar = $address{$i}; |
239 | switch($cChar) |
240 | { |
241 | case '<': |
242 | $iEnd = strpos($address,'>',$i+1); |
243 | if (!$iEnd) { |
244 | $sToken = substr($address,$i); |
245 | $i = $iCnt; |
246 | } else { |
247 | $sToken = substr($address,$i,$iEnd - $i +1); |
248 | $i = $iEnd; |
249 | } |
250 | $sToken = str_replace($aReplace, $aSpecials,$sToken); |
cfebb724 |
251 | if ($sToken) $aTokens[] = $sToken; |
14882b16 |
252 | break; |
253 | case '"': |
254 | $iEnd = strpos($address,$cChar,$i+1); |
0b4d4be7 |
255 | if ($iEnd) { |
256 | // skip escaped quotes |
257 | $prev_char = $address{$iEnd-1}; |
258 | while ($prev_char === '\\' && substr($address,$iEnd-2,2) !== '\\\\') { |
259 | $iEnd = strpos($address,$cChar,$iEnd+1); |
260 | if ($iEnd) { |
261 | $prev_char = $address{$iEnd-1}; |
262 | } else { |
263 | $prev_char = false; |
264 | } |
265 | } |
266 | } |
14882b16 |
267 | if (!$iEnd) { |
268 | $sToken = substr($address,$i); |
269 | $i = $iCnt; |
270 | } else { |
271 | // also remove the surrounding quotes |
272 | $sToken = substr($address,$i+1,$iEnd - $i -1); |
273 | $i = $iEnd; |
274 | } |
275 | $sToken = str_replace($aReplace, $aSpecials,$sToken); |
72956ab6 |
276 | if ($sToken) $aTokens[] = $sToken; |
14882b16 |
277 | break; |
278 | case '(': |
0b4d4be7 |
279 | array_pop($aTokens); //remove inserted space |
14882b16 |
280 | $iEnd = strpos($address,')',$i); |
281 | if (!$iEnd) { |
282 | $sToken = substr($address,$i); |
283 | $i = $iCnt; |
284 | } else { |
55243181 |
285 | $iDepth = 1; |
286 | $iComment = $i; |
287 | while (($iDepth > 0) && (++$iComment < $iCnt)) { |
288 | $cCharComment = $address{$iComment}; |
289 | switch($cCharComment) { |
290 | case '\\': |
291 | ++$iComment; |
292 | break; |
293 | case '(': |
294 | ++$iDepth; |
295 | break; |
296 | case ')': |
297 | --$iDepth; |
298 | break; |
299 | default: |
300 | break; |
301 | } |
302 | } |
303 | if ($iDepth == 0) { |
304 | $sToken = substr($address,$i,$iComment - $i +1); |
305 | $i = $iComment; |
306 | } else { |
307 | $sToken = substr($address,$i,$iEnd - $i + 1); |
308 | $i = $iEnd; |
309 | } |
14882b16 |
310 | } |
0b4d4be7 |
311 | // check the next token in case comments appear in the middle of email addresses |
312 | $prevToken = end($aTokens); |
313 | if (!in_array($prevToken,$aSpecials,true)) { |
43c0e295 |
314 | if ($i+1<strlen($address) && !in_array($address{$i+1},$aSpecials,true)) { |
0b4d4be7 |
315 | $iEnd = strpos($address,' ',$i+1); |
316 | if ($iEnd) { |
317 | $sNextToken = trim(substr($address,$i+1,$iEnd - $i -1)); |
318 | $i = $iEnd-1; |
319 | } else { |
a5ae4bc8 |
320 | $sNextToken = trim(substr($address,$i+1)); |
0b4d4be7 |
321 | $i = $iCnt; |
322 | } |
323 | // remove the token |
324 | array_pop($aTokens); |
325 | // create token and add it again |
326 | $sNewToken = $prevToken . $sNextToken; |
1f0269d6 |
327 | if($sNewToken) $aTokens[] = $sNewToken; |
0b4d4be7 |
328 | } |
329 | } |
14882b16 |
330 | $sToken = str_replace($aReplace, $aSpecials,$sToken); |
cfebb724 |
331 | if ($sToken) $aTokens[] = $sToken; |
14882b16 |
332 | break; |
333 | case ',': |
0b4d4be7 |
334 | case ':': |
14882b16 |
335 | case ';': |
336 | case ' ': |
337 | $aTokens[] = $cChar; |
338 | break; |
339 | default: |
340 | $iEnd = strpos($address,' ',$i+1); |
341 | if ($iEnd) { |
342 | $sToken = trim(substr($address,$i,$iEnd - $i)); |
343 | $i = $iEnd-1; |
344 | } else { |
345 | $sToken = trim(substr($address,$i)); |
346 | $i = $iCnt; |
347 | } |
348 | if ($sToken) $aTokens[] = $sToken; |
349 | } |
350 | ++$i; |
351 | } |
352 | return $aTokens; |
353 | } |
354 | function createAddressObject(&$aStack,&$aComment,&$sEmail,$sGroup='') { |
0b4d4be7 |
355 | //$aStack=explode(' ',implode('',$aStack)); |
14882b16 |
356 | if (!$sEmail) { |
357 | while (count($aStack) && !$sEmail) { |
358 | $sEmail = trim(array_pop($aStack)); |
359 | } |
360 | } |
361 | if (count($aStack)) { |
362 | $sPersonal = trim(implode('',$aStack)); |
cfebb724 |
363 | } else { |
14882b16 |
364 | $sPersonal = ''; |
365 | } |
366 | if (!$sPersonal && count($aComment)) { |
367 | $sComment = trim(implode(' ',$aComment)); |
368 | $sPersonal .= $sComment; |
369 | } |
370 | $oAddr =& new AddressStructure(); |
371 | if ($sPersonal && substr($sPersonal,0,2) == '=?') { |
372 | $oAddr->personal = encodeHeader($sPersonal); |
373 | } else { |
374 | $oAddr->personal = $sPersonal; |
375 | } |
0b4d4be7 |
376 | // $oAddr->group = $sGroup; |
14882b16 |
377 | $iPosAt = strpos($sEmail,'@'); |
378 | if ($iPosAt) { |
379 | $oAddr->mailbox = substr($sEmail, 0, $iPosAt); |
380 | $oAddr->host = substr($sEmail, $iPosAt+1); |
381 | } else { |
382 | $oAddr->mailbox = $sEmail; |
383 | $oAddr->host = false; |
384 | } |
14882b16 |
385 | $sEmail = ''; |
386 | $aStack = $aComment = array(); |
387 | return $oAddr; |
388 | } |
389 | |
e74ba378 |
390 | /* |
cfebb724 |
391 | * parseAddress: recursive function for parsing address strings and store |
e74ba378 |
392 | * them in an address stucture object. |
393 | * input: $address = string |
394 | * $ar = boolean (return array instead of only the |
395 | * first element) |
14882b16 |
396 | * $addr_ar = array with parsed addresses // obsolete |
397 | * $group = string // obsolete |
cfebb724 |
398 | * $host = string (default domainname in case of |
e74ba378 |
399 | * addresses without a domainname) |
400 | * $lookup = callback function (for lookup address |
401 | * strings which are probably nicks |
cfebb724 |
402 | * (without @ ) ) |
e74ba378 |
403 | * output: array with addressstructure objects or only one |
404 | * address_structure object. |
340d67c2 |
405 | * personal name: encoded: =?charset?Q|B?string?= |
406 | * quoted: "string" |
407 | * normal: string |
408 | * email : <mailbox@host> |
409 | * : mailbox@host |
410 | * This function is also used for validating addresses returned from compose |
14882b16 |
411 | * That's also the reason that the function became a little bit huge |
e74ba378 |
412 | */ |
340d67c2 |
413 | |
14882b16 |
414 | function parseAddress($address,$ar=false,$aAddress=array(),$sGroup='',$sHost='',$lookup=false) { |
415 | $aTokens = $this->getAddressTokens($address); |
8d8da447 |
416 | $sPersonal = $sEmail = $sGroup = ''; |
14882b16 |
417 | $aStack = $aComment = array(); |
418 | foreach ($aTokens as $sToken) { |
419 | $cChar = $sToken{0}; |
420 | switch ($cChar) |
340d67c2 |
421 | { |
422 | case '=': |
14882b16 |
423 | case '"': |
424 | case ' ': |
cfebb724 |
425 | $aStack[] = $sToken; |
340d67c2 |
426 | break; |
14882b16 |
427 | case '(': |
428 | $aComment[] = substr($sToken,1,-1); |
340d67c2 |
429 | break; |
3fcadedb |
430 | case ';': |
14882b16 |
431 | if ($sGroup) { |
29f842a4 |
432 | $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup); |
14882b16 |
433 | $oAddr = end($aAddress); |
0b4d4be7 |
434 | if(!$oAddr || ((isset($oAddr)) && !$oAddr->mailbox && !$oAddr->personal)) { |
435 | $sEmail = $sGroup . ':;'; |
cfebb724 |
436 | } |
0b4d4be7 |
437 | $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup); |
14882b16 |
438 | $sGroup = ''; |
439 | $aStack = $aComment = array(); |
440 | break; |
340d67c2 |
441 | } |
14882b16 |
442 | case ',': |
443 | $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail,$sGroup); |
444 | break; |
cfebb724 |
445 | case ':': |
29f842a4 |
446 | $sGroup = trim(implode(' ',$aStack)); |
0b4d4be7 |
447 | $sGroup = preg_replace('/\s+/',' ',$sGroup); |
14882b16 |
448 | $aStack = array(); |
449 | break; |
450 | case '<': |
451 | $sEmail = trim(substr($sToken,1,-1)); |
452 | break; |
453 | case '>': |
454 | /* skip */ |
cfebb724 |
455 | break; |
14882b16 |
456 | default: $aStack[] = $sToken; break; |
19d470aa |
457 | } |
458 | } |
14882b16 |
459 | /* now do the action again for the last address */ |
460 | $aAddress[] = $this->createAddressObject($aStack,$aComment,$sEmail); |
461 | /* try to lookup the addresses in case of invalid email addresses */ |
462 | $aProcessedAddress = array(); |
463 | foreach ($aAddress as $oAddr) { |
464 | $aAddrBookAddress = array(); |
465 | if (!$oAddr->host) { |
466 | $grouplookup = false; |
340d67c2 |
467 | if ($lookup) { |
14882b16 |
468 | $aAddr = call_user_func_array($lookup,array($oAddr->mailbox)); |
469 | if (isset($aAddr['email'])) { |
470 | if (strpos($aAddr['email'],',')) { |
471 | $grouplookup = true; |
472 | $aAddrBookAddress = $this->parseAddress($aAddr['email'],true); |
473 | } else { |
474 | $iPosAt = strpos($aAddr['email'], '@'); |
475 | $oAddr->mailbox = substr($aAddr['email'], 0, $iPosAt); |
476 | $oAddr->host = substr($aAddr['email'], $iPosAt+1); |
477 | if (isset($aAddr['name'])) { |
478 | $oAddr->personal = $aAddr['name']; |
479 | } else { |
480 | $oAddr->personal = encodeHeader($sPersonal); |
481 | } |
482 | } |
483 | } |
340d67c2 |
484 | } |
14882b16 |
485 | if (!$grouplookup && !$oAddr->mailbox) { |
486 | $oAddr->mailbox = trim($sEmail); |
487 | if ($sHost && $oAddr->mailbox) { |
488 | $oAddr->host = $sHost; |
340d67c2 |
489 | } |
8b53b4ba |
490 | } else if (!$grouplookup && !$oAddr->host) { |
491 | if ($sHost && $oAddr->mailbox) { |
492 | $oAddr->host = $sHost; |
493 | } |
cfebb724 |
494 | } |
14882b16 |
495 | } |
496 | if (!$aAddrBookAddress && $oAddr->mailbox) { |
497 | $aProcessedAddress[] = $oAddr; |
498 | } else { |
cfebb724 |
499 | $aProcessedAddress = array_merge($aProcessedAddress,$aAddrBookAddress); |
14882b16 |
500 | } |
340d67c2 |
501 | } |
cfebb724 |
502 | if ($ar) { |
14882b16 |
503 | return $aProcessedAddress; |
19d470aa |
504 | } else { |
14882b16 |
505 | return $aProcessedAddress[0]; |
19d470aa |
506 | } |
cfebb724 |
507 | } |
19d470aa |
508 | |
bddb3448 |
509 | /** |
510 | * Normalise the different Priority headers into a uniform value, |
511 | * namely that of the X-Priority header (1, 3, 5). Supports: |
512 | * Prioirty, X-Priority, Importance. |
513 | * X-MS-Mail-Priority is not parsed because it always coincides |
514 | * with one of the other headers. |
515 | * |
516 | * NOTE: this is actually a duplicate from the function in |
517 | * functions/imap_messages. I'm not sure if it's ok here to call |
518 | * that function? |
519 | */ |
520 | function parsePriority($value) { |
521 | $value = strtolower(array_shift(split('/\w/',trim($value)))); |
522 | if ( is_numeric($value) ) { |
523 | return $value; |
524 | } |
525 | if ( $value == 'urgent' || $value == 'high' ) { |
526 | return 1; |
527 | } elseif ( $value == 'non-urgent' || $value == 'low' ) { |
528 | return 5; |
529 | } |
530 | // default is normal priority |
531 | return 3; |
532 | } |
533 | |
19d470aa |
534 | function parseContentType($value) { |
535 | $pos = strpos($value, ';'); |
536 | $props = ''; |
537 | if ($pos > 0) { |
538 | $type = trim(substr($value, 0, $pos)); |
38d6fba7 |
539 | $props = trim(substr($value, $pos+1)); |
19d470aa |
540 | } else { |
541 | $type = $value; |
542 | } |
543 | $content_type = new ContentType($type); |
544 | if ($props) { |
545 | $properties = $this->parseProperties($props); |
546 | if (!isset($properties['charset'])) { |
547 | $properties['charset'] = 'us-ascii'; |
548 | } |
549 | $content_type->properties = $this->parseProperties($props); |
550 | } |
551 | $this->content_type = $content_type; |
552 | } |
cfebb724 |
553 | |
2ddf00ae |
554 | /* RFC2184 */ |
cfebb724 |
555 | function processParameters($aParameters) { |
2ddf00ae |
556 | $aResults = array(); |
cfebb724 |
557 | $aCharset = array(); |
558 | // handle multiline parameters |
2ddf00ae |
559 | foreach($aParameters as $key => $value) { |
cfebb724 |
560 | if ($iPos = strpos($key,'*')) { |
561 | $sKey = substr($key,0,$iPos); |
562 | if (!isset($aResults[$sKey])) { |
563 | $aResults[$sKey] = $value; |
564 | if (substr($key,-1) == '*') { // parameter contains language/charset info |
565 | $aCharset[] = $sKey; |
566 | } |
567 | } else { |
568 | $aResults[$sKey] .= $value; |
569 | } |
570 | } else { |
571 | $aResults[$key] = $value; |
572 | } |
573 | } |
574 | foreach ($aCharset as $key) { |
575 | $value = $aResults[$key]; |
576 | // extract the charset & language |
577 | $charset = substr($value,0,strpos($value,"'")); |
578 | $value = substr($value,strlen($charset)+1); |
579 | $language = substr($value,0,strpos($value,"'")); |
580 | $value = substr($value,strlen($charset)+1); |
581 | // FIX ME What's the status of charset decode with language information ???? |
582 | $value = charset_decode($charset,$value); |
583 | $aResults[$key] = $value; |
2ddf00ae |
584 | } |
cfebb724 |
585 | return $aResults; |
2ddf00ae |
586 | } |
19d470aa |
587 | |
588 | function parseProperties($value) { |
589 | $propArray = explode(';', $value); |
590 | $propResultArray = array(); |
591 | foreach ($propArray as $prop) { |
592 | $prop = trim($prop); |
593 | $pos = strpos($prop, '='); |
594 | if ($pos > 0) { |
595 | $key = trim(substr($prop, 0, $pos)); |
596 | $val = trim(substr($prop, $pos+1)); |
bbb92b4c |
597 | if (strlen($val) > 0 && $val{0} == '"') { |
19d470aa |
598 | $val = substr($val, 1, -1); |
599 | } |
600 | $propResultArray[$key] = $val; |
601 | } |
602 | } |
2ddf00ae |
603 | return $this->processParameters($propResultArray); |
19d470aa |
604 | } |
605 | |
606 | function parseDisposition($value) { |
607 | $pos = strpos($value, ';'); |
608 | $props = ''; |
609 | if ($pos > 0) { |
610 | $name = trim(substr($value, 0, $pos)); |
fc9269ec |
611 | $props = trim(substr($value, $pos+1)); |
19d470aa |
612 | } else { |
613 | $name = $value; |
614 | } |
615 | $props_a = $this->parseProperties($props); |
616 | $disp = new Disposition($name); |
617 | $disp->properties = $props_a; |
618 | $this->disposition = $disp; |
619 | } |
620 | |
621 | function mlist($field, $value) { |
622 | $res_a = array(); |
623 | $value_a = explode(',', $value); |
624 | foreach ($value_a as $val) { |
625 | $val = trim($val); |
626 | if ($val{0} == '<') { |
627 | $val = substr($val, 1, -1); |
628 | } |
629 | if (substr($val, 0, 7) == 'mailto:') { |
630 | $res_a['mailto'] = substr($val, 7); |
631 | } else { |
632 | $res_a['href'] = $val; |
633 | } |
634 | } |
635 | $this->mlist[$field] = $res_a; |
636 | } |
637 | |
638 | /* |
639 | * function to get the addres strings out of the header. |
640 | * Arguments: string or array of strings ! |
641 | * example1: header->getAddr_s('to'). |
642 | * example2: header->getAddr_s(array('to', 'cc', 'bcc')) |
643 | */ |
2c9ecd11 |
644 | function getAddr_s($arr, $separator = ',',$encoded=false) { |
19d470aa |
645 | $s = ''; |
646 | |
647 | if (is_array($arr)) { |
648 | foreach($arr as $arg) { |
2c9ecd11 |
649 | if ($this->getAddr_s($arg, $separator, $encoded)) { |
8d8da447 |
650 | $s .= $separator; |
19d470aa |
651 | } |
652 | } |
653 | $s = ($s ? substr($s, 2) : $s); |
654 | } else { |
2c9ecd11 |
655 | $addr = $this->{$arr}; |
19d470aa |
656 | if (is_array($addr)) { |
657 | foreach ($addr as $addr_o) { |
658 | if (is_object($addr_o)) { |
2c9ecd11 |
659 | if ($encoded) { |
660 | $s .= $addr_o->getEncodedAddress() . $separator; |
661 | } else { |
662 | $s .= $addr_o->getAddress() . $separator; |
663 | } |
19d470aa |
664 | } |
665 | } |
666 | $s = substr($s, 0, -strlen($separator)); |
667 | } else { |
668 | if (is_object($addr)) { |
2c9ecd11 |
669 | if ($encoded) { |
670 | $s .= $addr->getEncodedAddress(); |
671 | } else { |
672 | $s .= $addr->getAddress(); |
673 | } |
19d470aa |
674 | } |
675 | } |
676 | } |
677 | return $s; |
678 | } |
679 | |
680 | function getAddr_a($arg, $excl_arr = array(), $arr = array()) { |
681 | if (is_array($arg)) { |
682 | foreach($arg as $argument) { |
683 | $arr = $this->getAddr_a($argument, $excl_arr, $arr); |
684 | } |
685 | } else { |
340d67c2 |
686 | $addr = $this->{$arg}; |
19d470aa |
687 | if (is_array($addr)) { |
688 | foreach ($addr as $next_addr) { |
689 | if (is_object($next_addr)) { |
690 | if (isset($next_addr->host) && ($next_addr->host != '')) { |
691 | $email = $next_addr->mailbox . '@' . $next_addr->host; |
692 | } else { |
693 | $email = $next_addr->mailbox; |
694 | } |
695 | $email = strtolower($email); |
696 | if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) { |
697 | $arr[$email] = $next_addr->personal; |
698 | } |
699 | } |
700 | } |
701 | } else { |
702 | if (is_object($addr)) { |
703 | $email = $addr->mailbox; |
704 | $email .= (isset($addr->host) ? '@' . $addr->host : ''); |
705 | $email = strtolower($email); |
706 | if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) { |
707 | $arr[$email] = $addr->personal; |
708 | } |
709 | } |
710 | } |
711 | } |
712 | return $arr; |
713 | } |
cfebb724 |
714 | |
d0719411 |
715 | function findAddress($address, $recurs = false) { |
340d67c2 |
716 | $result = false; |
d0719411 |
717 | if (is_array($address)) { |
340d67c2 |
718 | $i=0; |
d0719411 |
719 | foreach($address as $argument) { |
720 | $match = $this->findAddress($argument, true); |
340d67c2 |
721 | if ($match[1]) { |
722 | return $i; |
723 | } else { |
724 | if (count($match[0]) && !$result) { |
725 | $result = $i; |
726 | } |
727 | } |
cfebb724 |
728 | ++$i; |
340d67c2 |
729 | } |
730 | } else { |
731 | if (!is_array($this->cc)) $this->cc = array(); |
732 | $srch_addr = $this->parseAddress($address); |
733 | $results = array(); |
734 | foreach ($this->to as $to) { |
735 | if ($to->host == $srch_addr->host) { |
736 | if ($to->mailbox == $srch_addr->mailbox) { |
737 | $results[] = $srch_addr; |
738 | if ($to->personal == $srch_addr->personal) { |
739 | if ($recurs) { |
740 | return array($results, true); |
741 | } else { |
742 | return true; |
743 | } |
744 | } |
745 | } |
746 | } |
d0719411 |
747 | } |
340d67c2 |
748 | foreach ($this->cc as $cc) { |
749 | if ($cc->host == $srch_addr->host) { |
750 | if ($cc->mailbox == $srch_addr->mailbox) { |
751 | $results[] = $srch_addr; |
752 | if ($cc->personal == $srch_addr->personal) { |
753 | if ($recurs) { |
754 | return array($results, true); |
755 | } else { |
756 | return true; |
757 | } |
758 | } |
759 | } |
760 | } |
761 | } |
762 | if ($recurs) { |
763 | return array($results, false); |
764 | } elseif (count($result)) { |
765 | return true; |
766 | } else { |
767 | return false; |
cfebb724 |
768 | } |
340d67c2 |
769 | } |
1465f80c |
770 | //exit; |
340d67c2 |
771 | return $result; |
d0719411 |
772 | } |
19d470aa |
773 | |
774 | function getContentType($type0, $type1) { |
775 | $type0 = $this->content_type->type0; |
776 | $type1 = $this->content_type->type1; |
777 | return $this->content_type->properties; |
778 | } |
779 | } |
780 | |
e50f5ac2 |
781 | ?> |