Added new folders.
[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 } elseif ($group) {
310 $addr_structure->personal = $name;
311 $addr_ar[] = $addr_structure;
312 return (array($addr_ar,$pos+1 ));
313 } else {
314 $addr_structure->personal = $name;
315 if ($name || $addr) {
316 $addr_ar[] = $addr_structure;
317 }
318 }
319 if ($ar) {
320 return ($addr_ar);
321 }
322 return ($addr_ar[0]);
323 }
324
325 function parseContentType($value) {
326 $pos = strpos($value, ';');
327 $props = '';
328 if ($pos > 0) {
329 $type = trim(substr($value, 0, $pos));
330 $props = trim(substr($type, $pos+1));
331 } else {
332 $type = $value;
333 }
334 $content_type = new ContentType($type);
335 if ($props) {
336 $properties = $this->parseProperties($props);
337 if (!isset($properties['charset'])) {
338 $properties['charset'] = 'us-ascii';
339 }
340 $content_type->properties = $this->parseProperties($props);
341 }
342 $this->content_type = $content_type;
343 }
344
345 function parseProperties($value) {
346 $propArray = explode(';', $value);
347 $propResultArray = array();
348 foreach ($propArray as $prop) {
349 $prop = trim($prop);
350 $pos = strpos($prop, '=');
351 if ($pos > 0) {
352 $key = trim(substr($prop, 0, $pos));
353 $val = trim(substr($prop, $pos+1));
354 if ($val{0} == '"') {
355 $val = substr($val, 1, -1);
356 }
357 $propResultArray[$key] = $val;
358 }
359 }
360 return $propResultArray;
361 }
362
363 function parseDisposition($value) {
364 $pos = strpos($value, ';');
365 $props = '';
366 if ($pos > 0) {
367 $name = trim(substr($value, 0, $pos));
368 $props = trim(substr($value, $pos+1));
369 } else {
370 $name = $value;
371 }
372 $props_a = $this->parseProperties($props);
373 $disp = new Disposition($name);
374 $disp->properties = $props_a;
375 $this->disposition = $disp;
376 }
377
378 function mlist($field, $value) {
379 $res_a = array();
380 $value_a = explode(',', $value);
381 foreach ($value_a as $val) {
382 $val = trim($val);
383 if ($val{0} == '<') {
384 $val = substr($val, 1, -1);
385 }
386 if (substr($val, 0, 7) == 'mailto:') {
387 $res_a['mailto'] = substr($val, 7);
388 } else {
389 $res_a['href'] = $val;
390 }
391 }
392 $this->mlist[$field] = $res_a;
393 }
394
395 /*
396 * function to get the addres strings out of the header.
397 * Arguments: string or array of strings !
398 * example1: header->getAddr_s('to').
399 * example2: header->getAddr_s(array('to', 'cc', 'bcc'))
400 */
401 function getAddr_s($arr, $separator = ',') {
402 $s = '';
403
404 if (is_array($arr)) {
405 foreach($arr as $arg) {
406 if ($this->getAddr_s($arg)) {
407 $s .= $separator . $result;
408 }
409 }
410 $s = ($s ? substr($s, 2) : $s);
411 } else {
412 eval('$addr = $this->' . $arr . ';') ;
413 if (is_array($addr)) {
414 foreach ($addr as $addr_o) {
415 if (is_object($addr_o)) {
416 $s .= $addr_o->getAddress() . $separator;
417 }
418 }
419 $s = substr($s, 0, -strlen($separator));
420 } else {
421 if (is_object($addr)) {
422 $s .= $addr->getAddress();
423 }
424 }
425 }
426 return $s;
427 }
428
429 function getAddr_a($arg, $excl_arr = array(), $arr = array()) {
430 if (is_array($arg)) {
431 foreach($arg as $argument) {
432 $arr = $this->getAddr_a($argument, $excl_arr, $arr);
433 }
434 } else {
435 eval('$addr = $this->' . $arg . ';') ;
436 if (is_array($addr)) {
437 foreach ($addr as $next_addr) {
438 if (is_object($next_addr)) {
439 if (isset($next_addr->host) && ($next_addr->host != '')) {
440 $email = $next_addr->mailbox . '@' . $next_addr->host;
441 } else {
442 $email = $next_addr->mailbox;
443 }
444 $email = strtolower($email);
445 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
446 $arr[$email] = $next_addr->personal;
447 }
448 }
449 }
450 } else {
451 if (is_object($addr)) {
452 $email = $addr->mailbox;
453 $email .= (isset($addr->host) ? '@' . $addr->host : '');
454 $email = strtolower($email);
455 if ($email && !isset($arr[$email]) && !isset($excl_arr[$email])) {
456 $arr[$email] = $addr->personal;
457 }
458 }
459 }
460 }
461 return $arr;
462 }
463
464 function getContentType($type0, $type1) {
465 $type0 = $this->content_type->type0;
466 $type1 = $this->content_type->type1;
467 return $this->content_type->properties;
468 }
469 }
470
471 ?>