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