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