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, 3, strlen($line) - 4)), 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->dnt
= 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 (!is_object($hdr->from
) && 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));
219 } else if (strtolower(substr($line,0,9)) == 'x-mailer:') {
220 $hdr->xmailer
= trim(substr($line, 9));
226 if (strtolower(substr($line,0,10)) == 'user-agent') {
227 $hdr->xmailer
= trim(substr($line, 10));
240 * function to process addresses.
242 function sqimap_parse_address($address, $ar, $addr_ar = array(), $group = '') {
244 $j = strlen( $address );
248 while ( $pos < $j ) {
249 if ($address{$pos} == '"') { /* get the personal name */
251 while ( $address{$pos} != '"' &&
253 if (substr($address, $pos, 2) == '\\"') {
254 $name .= $address{$pos};
256 } elseif (substr($address, $pos, 2) == '\\\\') {
257 $name .= $address{$pos};
260 $name .= $address{$pos};
263 } elseif ($address{$pos} == '<') { /* get email address */
266 while ( $address{$pos} != '>' &&
268 $addr .= $address{$pos};
271 } elseif ($address{$pos} == '(') { /* rip off comments */
274 while ( $address{$pos} != ')' &&
276 $addr .= $address{$pos};
279 $address_start = substr($address,0,$addr_start);
280 $address_end = substr($address,$pos+
1);
281 $address = $address_start . $address_end;
282 $j = strlen( $address );
284 } elseif ( $address{$pos} == ',' ) { /* we reached a delimiter */
286 $addr = substr($address,0,$pos);
287 } elseif ($name == '') {
288 $name = substr($address,0,$addr_start);
290 $at = strpos($addr, '@');
291 $addr_structure = new AddressStructure();
292 $addr_structure->personal
= $name;
293 $addr_structure->group
= $group;
296 $addr_structure->mailbox
= substr($addr,0,$at);
297 $addr_structure->host
= substr($addr,$at+
1);
299 $addr_structure->mailbox
= $addr;
301 $address = substr($address,$pos+
1);
302 $j = strlen( $address );
306 $addr_ar[] = $addr_structure;
308 } elseif ( $address{$pos} == ":" ) { /* process the group addresses */
310 $group = substr($address,0,$pos);
311 $address = substr($address,$pos+
1);
312 $result = sqimap_parse_address($address, $ar, $addr_ar, $group);
313 $addr_ar = $result[0];
315 $address = substr($address,$pos);
316 $j = strlen( $address );
318 } elseif ($address{$pos} == ';' && $group ) {
319 $address = substr($address, 0, $pos-1);
325 $addr = substr($address,0,$pos);
326 } elseif ($name == '') {
327 $name = substr($address,0,$addr_start);
329 $at = strpos($addr, '@');
330 $addr_structure = new AddressStructure();
331 $addr_structure->group
= $group;
333 $addr_structure->mailbox
= trim(substr($addr,0,$at));
334 $addr_structure->host
= trim(substr($addr,$at+
1));
336 $addr_structure->mailbox
= trim($addr);
339 if ($group && $addr == '') { /* no addresses found in group */
340 $name = "$group: Undisclosed recipients;";
341 $addr_structure->personal
= $name;
342 $addr_ar[] = $addr_structure;
343 return (array($addr_ar,$pos+
1));
345 $addr_structure->personal
= $name;
346 if ($name ||
$addr) {
347 $addr_ar[] = $addr_structure;
353 return ($addr_ar[0]);