undisclosed recipients fix
[squirrelmail.git] / class / mime / Rfc822Header.class.php
1 <?php
2
3 /**
4 * Rfc822Header.class.php
5 *
6 * Copyright (c) 2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This contains functions needed to handle mime messages.
10 *
11 * $Id$
12 */
13
14 /*
15 * rdc822_header class
16 * input: header_string or array
17 */
18 class Rfc822Header {
19 var $date = '',
20 $subject = '',
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 $references = '',
30 $mime = false,
31 $content_type = '',
32 $disposition = '',
33 $xmailer = '',
34 $priority = 3,
35 $dnt = '',
36 $mlist = array(),
37 $more_headers = array(); /* only needed for constructing headers
38 in smtp.php */
39 function parseHeader($hdr) {
40 if (is_array($hdr)) {
41 $hdr = implode('', $hdr);
42 }
43
44 /* First we unfold the header */
45 $hdr = trim(str_replace(array("\r\n\t", "\r\n "),array('', ''), $hdr));
46
47 /* Now we can make a new header array with */
48 /* each element representing a headerline */
49 $hdr = explode("\r\n" , $hdr);
50 foreach ($hdr as $line) {
51 $pos = strpos($line, ':');
52 if ($pos > 0) {
53 $field = substr($line, 0, $pos);
54 $value = trim(substr($line, $pos+1));
55 if(!preg_match('/^X.*/i', $field) &&
56 !preg_match('/^Subject/i', $field)) {
57 $value = $this->stripComments($value);
58 }
59 $this->parseField($field, $value);
60 }
61 }
62 if ($this->content_type == '') {
63 $this->parseContentType('text/plain; charset=us-ascii');
64 }
65 }
66
67 function stripComments($value) {
68 $result = '';
69
70 $cnt = strlen($value);
71 for ($i = 0; $i < $cnt; ++$i) {
72 switch ($value{$i}) {
73 case '"':
74 $result .= '"';
75 while ((++$i < $cnt) && ($value{$i} != '"')) {
76 if ($value{$i} == '\\') {
77 $result .= '\\';
78 ++$i;
79 }
80 $result .= $value{$i};
81 }
82 $result .= $value{$i};
83 break;
84 case '(':
85 $depth = 1;
86 while (($depth > 0) && (++$i < $cnt)) {
87 switch($value{$i}) {
88 case '\\':
89 ++$i;
90 break;
91 case '(':
92 ++$depth;
93 break;
94 case ')':
95 --$depth;
96 break;
97 default:
98 break;
99 }
100 }
101 break;
102 default:
103 $result .= $value{$i};
104 break;
105 }
106 }
107 return $result;
108 }
109
110 function parseField($field, $value) {
111 $field = strtolower($field);
112 switch($field) {
113 case 'date':
114 $d = strtr($value, array(' ' => ' '));
115 $d = explode(' ', $d);
116 $this->date = getTimeStamp($d);
117 break;
118 case 'subject':
119 $this->subject = $value;
120 break;
121 case 'from':
122 $this->from = $this->parseAddress($value,true);
123 break;
124 case 'sender':
125 $this->sender = $this->parseAddress($value);
126 break;
127 case 'reply-to':
128 $this->reply_to = $this->parseAddress($value, true);
129 break;
130 case 'to':
131 $this->to = $this->parseAddress($value, true);
132 break;
133 case 'cc':
134 $this->cc = $this->parseAddress($value, true);
135 break;
136 case 'bcc':
137 $this->bcc = $this->parseAddress($value, true);
138 break;
139 case 'in-reply-to':
140 $this->in_reply_to = $value;
141 break;
142 case 'message-id':
143 $this->message_id = $value;
144 break;
145 case 'references':
146 $this->references = $value;
147 break;
148 case 'disposition-notification-to':
149 $this->dnt = $this->parseAddress($value);
150 break;
151 case 'mime-version':
152 $value = str_replace(' ', '', $value);
153 $this->mime = ($value == '1.0' ? true : $this->mime);
154 break;
155 case 'content-type':
156 $this->parseContentType($value);
157 break;
158 case 'content-disposition':
159 $this->parseDisposition($value);
160 break;
161 case 'user-agent':
162 case 'x-mailer':
163 $this->xmailer = $value;
164 break;
165 case 'x-priority':
166 $this->priority = $value;
167 break;
168 case 'list-post':
169 $this->mlist('post', $value);
170 break;
171 case 'list-reply':
172 $this->mlist('reply', $value);
173 break;
174 case 'list-subscribe':
175 $this->mlist('subscribe', $value);
176 break;
177 case 'list-unsubscribe':
178 $this->mlist('unsubscribe', $value);
179 break;
180 case 'list-archive':
181 $this->mlist('archive', $value);
182 break;
183 case 'list-owner':
184 $this->mlist('owner', $value);
185 break;
186 case 'list-help':
187 $this->mlist('help', $value);
188 break;
189 case 'list-id':
190 $this->mlist('id', $value);
191 break;
192 default:
193 break;
194 }
195 }
196
197 function parseAddress
198 ($address, $ar=false, $addr_ar = array(), $group = '', $host='') {
199 $pos = 0;
200 $j = strlen($address);
201 $name = '';
202 $addr = '';
203 while ($pos < $j) {
204 switch ($address{$pos}) {
205 case '"': /* get the personal name */
206 if ($address{++$pos} == '"') {
207 ++$pos;
208 } else {
209 while ($pos < $j && $address{$pos} != '"') {
210 if ((substr($address, $pos, 2) == '\\"') ||
211 (substr($address, $pos, 2) == '\\\\')) {
212 $name .= $address{$pos++};
213 }
214 $name .= $address{$pos++};
215 }
216 }
217 ++$pos;
218 break;
219 case '<': /* get email address */
220 $addr_start = $pos++;
221 while ($pos < $j && $address{$pos} != '>') {
222 $addr .= $address{$pos++};
223 }
224 ++$pos;
225 break;
226 case '(': /* rip off comments */
227 $addr_start = $pos;
228 for (++$pos; ($pos < $j) && ($address{$pos} != ')'); ++$pos) {
229 $addr .= $address{$pos};
230 }
231 $address_start = substr($address, 0, $addr_start);
232 $address_end = substr($address, $pos + 1);
233 $address = $address_start . $address_end;
234 $j = strlen($address);
235 $pos = $addr_start + 1;
236 break;
237 case ',': /* we reached a delimiter */
238 //case ';':
239 if ($addr == '') {
240 $addr = substr($address, 0, $pos);
241 } else if ($name == '') {
242 $name = trim(substr($address, 0, $addr_start));
243 }
244
245 $at = strpos($addr, '@');
246 $addr_structure = new AddressStructure();
247 $addr_structure->personal = $name;
248 $addr_structure->group = $group;
249 if ($at) {
250 $addr_structure->mailbox = substr($addr, 0, $at);
251 $addr_structure->host = substr($addr, $at+1);
252 } else {
253 $addr_structure->mailbox = $addr;
254 if ($host) {
255 $addr_structure->host = $host;
256 }
257 }
258 $address = trim(substr($address, $pos+1));
259 $j = strlen($address);
260 $pos = 0;
261 $name = '';
262 $addr = '';
263 $addr_ar[] = $addr_structure;
264 break;
265 case ':': /* process the group addresses */
266 /* group marker */
267 $group = substr($address, 0, $pos);
268 $address = substr($address, $pos+1);
269 $result = $this->parseAddress($address, $ar, $addr_ar, $group);
270 $addr_ar = $result[0];
271 $pos = $result[1];
272 $address = substr($address, $pos++);
273 $j = strlen($address);
274 $group = '';
275 break;
276 case ';':
277 if ($group) {
278 $address = substr($address, 0, $pos - 1);
279 }
280 ++$pos;
281 break;
282 default:
283 ++$pos;
284 break;
285 }
286 }
287 if ($addr == '') {
288 $addr = substr($address, 0, $pos);
289 } else if ($name == '') {
290 $name = trim(substr($address, 0, $addr_start));
291 }
292 $at = strpos($addr, '@');
293 $addr_structure = new AddressStructure();
294 $addr_structure->group = $group;
295 if ($at) {
296 $addr_structure->mailbox = trim(substr($addr, 0, $at));
297 $addr_structure->host = trim(substr($addr, $at+1));
298 } else {
299 $addr_structure->mailbox = trim($addr);
300 if ($host) {
301 $addr_structure->host = $host;
302 }
303 }
304 if ($group && $addr == '') { /* no addresses found in group */
305 $name = "$group";
306 $addr_structure->personal = $name;
307 $addr_ar[] = $addr_structure;
308 return (array($addr_ar, $pos+1));
309 } else {
310 $addr_structure->personal = $name;
311 if ($name || $addr) {
312 $addr_ar[] = $addr_structure;
313 }
314 }
315 if ($ar) {
316 return ($addr_ar);
317 }
318 return ($addr_ar[0]);
319 }
320
321 function parseContentType($value) {
322 $pos = strpos($value, ';');
323 $props = '';
324 if ($pos > 0) {
325 $type = trim(substr($value, 0, $pos));
326 $props = trim(substr($type, $pos+1));
327 } else {
328 $type = $value;
329 }
330 $content_type = new ContentType($type);
331 if ($props) {
332 $properties = $this->parseProperties($props);
333 if (!isset($properties['charset'])) {
334 $properties['charset'] = 'us-ascii';
335 }
336 $content_type->properties = $this->parseProperties($props);
337 }
338 $this->content_type = $content_type;
339 }
340
341 function parseProperties($value) {
342 $propArray = explode(';', $value);
343 $propResultArray = array();
344 foreach ($propArray as $prop) {
345 $prop = trim($prop);
346 $pos = strpos($prop, '=');
347 if ($pos > 0) {
348 $key = trim(substr($prop, 0, $pos));
349 $val = trim(substr($prop, $pos+1));
350 if ($val{0} == '"') {
351 $val = substr($val, 1, -1);
352 }
353 $propResultArray[$key] = $val;
354 }
355 }
356 return $propResultArray;
357 }
358
359 function parseDisposition($value) {
360 $pos = strpos($value, ';');
361 $props = '';
362 if ($pos > 0) {
363 $name = trim(substr($value, 0, $pos));
364 $props = trim(substr($type, $pos+1));
365 } else {
366 $name = $value;
367 }
368 $props_a = $this->parseProperties($props);
369 $disp = new Disposition($name);
370 $disp->properties = $props_a;
371 $this->disposition = $disp;
372 }
373
374 function mlist($field, $value) {
375 $res_a = array();
376 $value_a = explode(',', $value);
377 foreach ($value_a as $val) {
378 $val = trim($val);
379 if ($val{0} == '<') {
380 $val = substr($val, 1, -1);
381 }
382 if (substr($val, 0, 7) == 'mailto:') {
383 $res_a['mailto'] = substr($val, 7);
384 } else {
385 $res_a['href'] = $val;
386 }
387 }
388 $this->mlist[$field] = $res_a;
389 }
390
391 /*
392 * function to get the addres strings out of the header.
393 * Arguments: string or array of strings !
394 * example1: header->getAddr_s('to').
395 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
396 */
397 function getAddr_s($arr, $separator = ',') {
398 $s = '';
399
400 if (is_array($arr)) {
401 foreach($arr as $arg) {
402 if ($this->getAddr_s($arg)) {
403 $s .= $separator . $result;
404 }
405 }
406 $s = ($s ? substr($s, 2) : $s);
407 } else {
408 eval('$addr = $this->' . $arr . ';') ;
409 if (is_array($addr)) {
410 foreach ($addr as $addr_o) {
411 if (is_object($addr_o)) {
412 $s .= $addr_o->getAddress() . $separator;
413 }
414 }
415 $s = substr($s, 0, -strlen($separator));
416 } else {
417 if (is_object($addr)) {
418 $s .= $addr->getAddress();
419 }
420 }
421 }
422 return $s;
423 }
424
425 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
426 if (is_array($arg)) {
427 foreach($arg as $argument) {
428 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
429 }
430 } else {
431 eval('$addr = $this->' . $arg . ';') ;
432 if (is_array($addr)) {
433 foreach ($addr as $next_addr) {
434 if (is_object($next_addr)) {
435 if (isset($next_addr->host) && ($next_addr->host != '')) {
436 $email = $next_addr->mailbox . '@' . $next_addr->host;
437 } else {
438 $email = $next_addr->mailbox;
439 }
440 $email = strtolower($email);
441 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
442 $arr[$email] = $next_addr->personal;
443 }
444 }
445 }
446 } else {
447 if (is_object($addr)) {
448 $email = $addr->mailbox;
449 $email .= (isset($addr->host) ? '@' . $addr->host : '');
450 $email = strtolower($email);
451 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
452 $arr[$email] = $addr->personal;
453 }
454 }
455 }
456 }
457 return $arr;
458 }
459
460 function getContentType($type0, $type1) {
461 $type0 = $this->content_type->type0;
462 $type1 = $this->content_type->type1;
463 return $this->content_type->properties;
464 }
465 }
466
467 ?>