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