3 function sqimap_parse_RFC822Header ($read, $hdr) {
5 /* Set up some defaults */
8 $hdr->charset
= "us-ascii";
10 $count = count($read);
12 /* unfold multi-line headers */
13 while (($i +
1 < $count) && (strspn($read[$i +
1], "\t ") > 0) ) {
14 $read[$i +
1] = substr($read[$i], 0, -2) . ' ' . ltrim($read[$i+
1]);
15 array_splice($read, $i, 1);
19 $c = strtolower($line{0});
22 $c2 = strtolower($line{1});
25 if (substr($line, 0, 17) == "MIME-Version: 1.0") {
32 if (strtolower(substr($line, 0, 11)) == "message-id:") {
33 $hdr->message_id
= trim(substr($line, 11));
43 $c2 = strtolower($line{1});
46 /* Content-Transfer-Encoding */
47 if (substr(strtolower($line), 0, 26) == "content-transfer-encoding:") {
48 $hdr->encoding
= strtolower(trim(substr($line, 26)));
52 else if (strtolower(substr($line, 0, 13)) == "content-type:") {
53 $cont = strtolower(trim(substr($line, 13)));
54 if (strpos($cont, ";")) {
55 $cont = substr($cont, 0, strpos($cont, ";"));
57 if (strpos($cont, "/")) {
58 $hdr->type0
= substr($cont, 0, strpos($cont, "/"));
59 $hdr->type1
= substr($cont, strpos($cont, "/")+
1);
65 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
66 str_replace("\n", "", $line);
67 str_replace("\n", "", $read[$i]);
68 $line = "$line $read[$i]";
72 /* Detect the boundary of a multipart message */
73 if (eregi('boundary="([^"]+)"', $line, $regs)) {
74 $hdr->boundary
= $regs[1];
77 /* Detect the charset */
78 if (strpos(strtolower(trim($line)), "charset=")) {
79 $pos = strpos($line, "charset=") +
8;
80 $charset = trim($line);
81 if (strpos($line, ";", $pos) > 0) {
82 $charset = substr($charset, $pos, strpos($line, ";", $pos)-$pos);
84 $charset = substr($charset, $pos);
86 $charset = str_replace("\"", "", $charset);
87 $hdr->charset
= $charset;
89 $hdr->charset
= "us-ascii";
91 /* Detect type in case of multipart/related */
92 if (strpos(strtolower(trim($line)), "type=")) {
93 $pos = strpos($line, "type=") +
6;
95 if (strpos($line, ";", $pos) > 0) {
96 $type = substr($type, $pos, strpos($line, ";", $pos)-$pos);
98 $type = substr($type, $pos);
103 else if (strtolower(substr($line, 0, 20)) == "content-disposition:") {
104 /* Add better content-disposition support */
106 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
107 str_replace("\n", "", $line);
108 str_replace("\n", "", $read[$i]);
109 $line = "$line $read[$i]";
113 /* Detects filename if any */
114 if (strpos(strtolower(trim($line)), "filename=")) {
115 $pos = strpos($line, "filename=") +
9;
117 if (strpos($line, " ", $pos) > 0) {
118 $name = substr($name, $pos, strpos($line, " ", $pos));
120 $name = substr($name, $pos);
122 $name = str_replace("\"", "", $name);
123 $hdr->filename
= $name;
128 if (strtolower(substr($line, 0, 3)) == "cc:") {
129 $hdr->cc
= sqimap_parse_address(trim(substr($line, 9, strlen($line) - 10)), true);
138 case 'r': /* Reply-To */
139 if (strtolower(substr($line, 0, 9)) == "reply-to:") {
140 $hdr->replyto
= sqimap_parse_address(trim(substr($line, 9, strlen($line) - 10)), false);
145 if (strtolower(substr($line, 0, 5)) == "from:") {
146 $hdr->from
=sqimap_parse_address(trim(substr($line, 5, strlen($line) - 6)), false);
147 if (! isset($hdr->replyto
) ||
$hdr->replyto
== "") {
148 $hdr->replyto
= $hdr->from
;
154 $c2 = strtolower($line{1});
157 if (strtolower(substr($line, 0, 5)) == "date:") {
158 $d = substr($read[$i], 5);
160 $d = strtr($d, array(' ' => ' '));
161 $d = explode(' ', $d);
162 $hdr->date
= getTimeStamp($d);
166 case 'i': /* Disposition-Notification-To */
167 if (strtolower(substr($line, 0, 28)) == "disposition-notification-to:") {
168 $dnt = trim(substr($read[$i], 28));
169 $hdr->disposition
= sqimap_parse_address($dnt, false);
180 if (strtolower(substr($line, 0, 8)) == "subject:") {
181 $hdr->subject
= trim(substr($line, 8, strlen($line) - 9));
182 if (strlen(Chop($hdr->subject
)) == 0) {
183 $hdr->subject
= _("(no subject)");
190 if (strtolower(substr($line, 0, 4)) == "bcc:") {
191 $hdr->bcc
= sqimap_parse_address(trim(substr($line, 4, strlen($line) - 5)), true);
197 if (strtolower(substr($line, 0, 3)) == "to:") {
198 $hdr->to
= sqimap_parse_address(trim(substr($line, 3, strlen($line) - 4)), true);
203 /* ERROR CORRECTION */
204 if (strlen(trim($hdr->subject
)) == 0) {
205 $hdr->subject
= _("(no subject)");
207 if (strlen(trim($hdr->from
)) == 0) {
208 $hdr->from
= _("(unknown sender)");
210 if (strlen(trim($hdr->date
)) == 0) {
217 if (strtolower(substr($line, 0, 11)) == "x-priority:") {
218 $hdr->priority
= trim(substr($line, 11));
231 * function to process addresses.
233 function sqimap_parse_address($address, $ar, $addr_ar = array(), $group = '') {
235 $j = strlen( $address );
239 while ( $pos < $j ) {
240 if ($address{$pos} == '"') { /* get the personal name */
242 while ( $address{$pos} != '"' &&
244 if (substr($address, $pos, 2) == '\\"') {
245 $name .= $address{$pos};
247 } elseif (substr($address, $pos, 2) == '\\\\') {
248 $name .= $address{$pos};
251 $name .= $address{$pos};
254 } elseif ($address{$pos} == '<') { /* get email address */
257 while ( $address{$pos} != '>' &&
259 $addr .= $address{$pos};
262 } elseif ($address{$pos} == '(') { /* rip off comments */
265 while ( $address{$pos} != ')' &&
267 $addr .= $address{$pos};
270 $address_start = substr($address,0,$addr_start);
271 $address_end = substr($address,$pos+
1);
272 $address = $address_start . $address_end;
273 $j = strlen( $address );
275 } elseif ( $address{$pos} == ',' ) { /* we reached a delimiter */
277 $addr = substr($address,0,$pos);
278 } elseif ($name == '') {
279 $name = substr($address,0,$addr_start);
281 $at = strpos($addr, '@');
282 $addr_structure = new address_structure();
283 $addr_structure->personal
= $name;
284 $addr_structure->group
= $group;
287 $addr_structure->mailbox
= substr($addr,0,$at);
288 $addr_structure->host
= substr($addr,$at+
1);
290 $addr_structure->mailbox
= $addr;
292 $address = substr($address,$pos+
1);
293 $j = strlen( $address );
297 $addr_ar[] = $addr_structure;
299 } elseif ( $address{$pos} == ":" ) { /* process the group addresses */
301 $group = substr($address,0,$pos);
302 $address = substr($address,$pos+
1);
303 $result = sqimap_parse_address($address, $ar, $addr_ar, $group);
304 $addr_ar = $result[0];
306 $address = substr($address,$pos);
307 $j = strlen( $address );
309 } elseif ($address{$pos} == ';' && $group ) {
310 $address = substr($address, 0, $pos-1);
316 $addr = substr($address,0,$pos);
317 } elseif ($name == '') {
318 $name = substr($address,0,$addr_start);
320 $at = strpos($addr, '@');
321 $addr_structure = new address_structure();
322 $addr_structure->group
= $group;
324 $addr_structure->mailbox
= trim(substr($addr,0,$at));
325 $addr_structure->host
= trim(substr($addr,$at+
1));
327 $addr_structure->mailbox
= trim($addr);
330 if ($group && $addr == '') { /* no addresses found in group */
331 $name = "$group: Undisclosed recipients;";
332 $addr_structure->personal
= $name;
333 $addr_ar[] = $addr_structure;
334 return (array($addr_ar,$pos+
1));
336 $addr_structure->personal
= $name;
337 if ($name ||
$addr) {
338 $addr_ar[] = $addr_structure;
344 return ($addr_ar[0]);