3302d0d4 |
1 | <? |
2 | /** |
a09387f4 |
3 | ** mailbox.php |
3302d0d4 |
4 | ** |
5 | ** This contains functions that request information about a mailbox. Including |
6 | ** reading and parsing headers, getting folder information, etc. |
7 | ** |
8 | **/ |
9 | |
10 | function selectMailbox($imapConnection, $mailbox, &$numberOfMessages) { |
11 | // select mailbox |
12 | fputs($imapConnection, "mailboxSelect SELECT \"$mailbox\"\n"); |
d92b6f31 |
13 | $data = imapReadData($imapConnection, "mailboxSelect"); |
14 | for ($i = 0; $i < count($data); $i++) { |
15 | if (substr(Chop($data[$i]), -6) == "EXISTS") { |
16 | $array = explode(" ", $data[$i]); |
3302d0d4 |
17 | $numberOfMessages = $array[1]; |
18 | } |
3302d0d4 |
19 | } |
20 | } |
21 | |
ff89ac8a |
22 | function unseenMessages($imapConnection, &$numUnseen) { |
23 | fputs($imapConnection, "1 SEARCH UNSEEN NOT DELETED\n"); |
24 | $read = fgets($imapConnection, 1024); |
25 | $unseen = false; |
26 | |
27 | if (strlen($read) > 10) { |
28 | $unseen = true; |
29 | $ary = explode(" ", $read); |
30 | $numUnseen = count($ary) - 2; |
31 | } |
32 | else { |
33 | $unseen = false; |
34 | $numUnseen = 0; |
35 | } |
36 | |
37 | $read = fgets($imapConnection, 1024); |
38 | return $unseen; |
39 | } |
40 | |
0e919368 |
41 | /** This function sends a request to the IMAP server for headers, 50 at a time |
42 | ** until $end is reached. I originally had it do them all at one time, but found |
43 | ** it slightly faster to do it this way. |
44 | ** |
45 | ** Originally we had getMessageHeaders get the headers for one message at a time. |
46 | ** Doing it in bunches gave us a speed increase from 9 seconds (for a box of 800 |
47 | ** messages) to about 3.5 seconds. |
48 | **/ |
3e054c43 |
49 | function getMessageHeaders($imapConnection, $start, $end, &$from, &$subject, &$date) { |
3e054c43 |
50 | $rel_start = $start; |
3e054c43 |
51 | if (($start > $end) || ($start < 1)) { |
52 | echo "Error in message header fetching. Start message: $start, End message: $end<BR>"; |
53 | exit; |
54 | } |
3302d0d4 |
55 | |
e550d551 |
56 | $pos = 0; |
3e054c43 |
57 | while ($rel_start <= $end) { |
58 | if ($end - $rel_start > 50) { |
e550d551 |
59 | $rel_end = $rel_start + 49; |
3e054c43 |
60 | } else { |
61 | $rel_end = $end; |
62 | } |
8405ee35 |
63 | fputs($imapConnection, "messageFetch FETCH $rel_start:$rel_end RFC822.HEADER.LINES (From Subject Date)\n"); |
3302d0d4 |
64 | $read = fgets($imapConnection, 1024); |
3e054c43 |
65 | |
3e054c43 |
66 | while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) { |
e550d551 |
67 | |
3e054c43 |
68 | if (substr($read, 0, 5) == "From:") { |
7ce342dc |
69 | $read = encodeEmailAddr("$read"); |
e550d551 |
70 | $from[$pos] = substr($read, 5, strlen($read) - 6); |
3e054c43 |
71 | } |
72 | else if (substr($read, 0, 5) == "Date:") { |
df978e80 |
73 | $read = ereg_replace("<", "<", $read); |
74 | $read = ereg_replace(">", ">", $read); |
e550d551 |
75 | $date[$pos] = substr($read, 5, strlen($read) - 6); |
3e054c43 |
76 | } |
77 | else if (substr($read, 0, 8) == "Subject:") { |
df978e80 |
78 | $read = ereg_replace("<", "<", $read); |
79 | $read = ereg_replace(">", ">", $read); |
e550d551 |
80 | $subject[$pos] = substr($read, 8, strlen($read) - 9); |
81 | if (strlen(Chop($subject[$pos])) == 0) |
82 | $subject[$pos] = "(no subject)"; |
83 | } |
84 | else if (substr($read, 0, 1) == ")") { |
85 | if ($subject[$pos] == "") |
86 | $subject[$pos] = "(no subject)"; |
87 | else if ($from[$pos] == "") |
88 | $from[$pos] = "(unknown sender)"; |
89 | else if ($date[$pos] == "") |
90 | $from[$pos] = gettimeofday(); |
91 | |
92 | $pos++; |
3e054c43 |
93 | } |
e550d551 |
94 | |
3e054c43 |
95 | $read = fgets($imapConnection, 1024); |
96 | } |
97 | $rel_start = $rel_start + 50; |
3302d0d4 |
98 | } |
99 | } |
100 | |
7ce342dc |
101 | function encodeEmailAddr($string) { |
102 | $string = ereg_replace("<", "EMAILSTART--", $string); |
103 | $string = ereg_replace(">", "--EMAILEND", $string); |
104 | return $string; |
105 | } |
106 | |
61a4ac35 |
107 | function setMessageFlag($imapConnection, $i, $q, $flag) { |
108 | fputs($imapConnection, "messageStore STORE $i:$q +FLAGS (\\$flag)\n"); |
aa4c3749 |
109 | } |
110 | |
0e919368 |
111 | /** This function gets the flags for message $j. It does only one message at a |
112 | ** time, rather than doing groups of messages (like getMessageHeaders does). |
113 | ** I found it one or two seconds quicker (on a box of 800 messages) to do it |
114 | ** individually. I'm not sure why it happens like that, but that's what my |
115 | ** testing found. Perhaps later I will be proven wrong and this will change. |
116 | **/ |
3e054c43 |
117 | function getMessageFlags($imapConnection, $j, &$flags) { |
3302d0d4 |
118 | /** * 2 FETCH (FLAGS (\Answered \Seen)) */ |
3e054c43 |
119 | fputs($imapConnection, "messageFetch FETCH $j:$j FLAGS\n"); |
aa4c3749 |
120 | $read = fgets($imapConnection, 1024); |
3e054c43 |
121 | $count = 0; |
3302d0d4 |
122 | while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) { |
123 | if (strpos($read, "FLAGS")) { |
124 | $read = ereg_replace("\(", "", $read); |
125 | $read = ereg_replace("\)", "", $read); |
126 | $read = substr($read, strpos($read, "FLAGS")+6, strlen($read)); |
127 | $read = trim($read); |
128 | $flags = explode(" ", $read);; |
54a8ae32 |
129 | $s = 0; |
130 | while ($s < count($flags)) { |
131 | $flags[$s] = substr($flags[$s], 1, strlen($flags[$s])); |
132 | $s++; |
3302d0d4 |
133 | } |
134 | } else { |
135 | $flags[0] = "None"; |
136 | } |
3e054c43 |
137 | $count++; |
3302d0d4 |
138 | $read = fgets($imapConnection, 1024); |
139 | } |
140 | } |
141 | |
31f3d7c0 |
142 | function decodeEmailAddr($sender) { |
143 | $emailAddr = getEmailAddr($sender); |
40884065 |
144 | if (strpos($emailAddr, "EMAILSTART--")) { |
31f3d7c0 |
145 | |
40884065 |
146 | $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr); |
147 | $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr); |
148 | } else { |
149 | $emailAddr = $emailAddr; |
150 | } |
31f3d7c0 |
151 | return $emailAddr; |
152 | } |
153 | |
3302d0d4 |
154 | function getEmailAddr($sender) { |
155 | if (strpos($sender, "EMAILSTART--") == false) |
40884065 |
156 | return "$sender"; |
3302d0d4 |
157 | |
40884065 |
158 | $emailStart = strpos($sender, "EMAILSTART--") + 12; |
159 | $emailAddr = substr($sender, $emailStart, strlen($sender)); |
160 | $emailAddr = substr($emailAddr, 0, strpos($emailAddr, "--EMAILEND")); |
3302d0d4 |
161 | |
162 | return $emailAddr; |
163 | } |
164 | |
165 | function getSender($sender) { |
166 | if (strpos($sender, "EMAILSTART--") == false) |
40884065 |
167 | return "$sender"; |
3302d0d4 |
168 | |
169 | $first = substr($sender, 0, strpos($sender, "EMAILSTART--")); |
170 | $second = substr($sender, strpos($sender, "--EMAILEND") +10, strlen($sender)); |
40884065 |
171 | return "$first $second"; |
3302d0d4 |
172 | } |
173 | |
174 | function getSenderName($sender) { |
175 | $name = getSender($sender); |
176 | $emailAddr = getEmailAddr($sender); |
177 | $emailStart = strpos($emailAddr, "EMAILSTART--"); |
178 | $emailEnd = strpos($emailAddr, "--EMAILEND") - 10; |
179 | |
180 | if (($emailAddr == "") && ($name == "")) { |
181 | $from = $sender; |
182 | } |
183 | else if ((strstr($name, "?") != false) || (strstr($name, "$") != false) || (strstr($name, "%") != false)){ |
184 | $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr); |
185 | $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr); |
186 | $from = $emailAddr; |
187 | } |
188 | else if (strlen($name) > 0) { |
189 | $from = $name; |
190 | } |
191 | else if (strlen($emailAddr > 0)) { |
192 | $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr); |
193 | $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr); |
194 | $from = $emailAddr; |
195 | } |
196 | |
197 | $from = trim($from); |
198 | |
199 | // strip out any quotes if they exist |
200 | if ((strlen($from) > 0) && ($from[0] == "\"") && ($from[strlen($from) - 1] == "\"")) |
201 | $from = substr($from, 1, strlen($from) - 2); |
202 | |
203 | return $from; |
204 | } |
aa4c3749 |
205 | |
206 | /** returns "true" if the copy was completed successfully. |
207 | ** returns "false" with an error message if unsuccessful. |
208 | **/ |
209 | function copyMessages($imapConnection, $from_id, $to_id, $folder) { |
210 | fputs($imapConnection, "mailboxStore COPY $from_id:$to_id \"$folder\"\n"); |
211 | $read = fgets($imapConnection, 1024); |
212 | while ((substr($read, 0, 15) != "mailboxStore OK") && (substr($read, 0, 15) != "mailboxStore NO")) { |
213 | $read = fgets($imapConnection, 1024); |
214 | } |
215 | |
216 | if (substr($read, 0, 15) == "mailboxStore NO") { |
aa4c3749 |
217 | return false; |
218 | } else if (substr($read, 0, 15) == "mailboxStore OK") { |
219 | return true; |
220 | } |
221 | |
222 | echo "UNKNOWN ERROR copying messages $from_id to $to_id to folder $folder.<BR>"; |
223 | return false; |
224 | } |
61a4ac35 |
225 | |
226 | /** expunges a mailbox **/ |
227 | function expungeBox($imapConnection, $mailbox) { |
228 | selectMailbox($imapConnection, $mailbox, $num); |
229 | fputs($imapConnection, "1 EXPUNGE\n"); |
230 | } |
8c7dfc99 |
231 | |
d92b6f31 |
232 | function getFolderNameMinusINBOX($mailbox, $del) { |
233 | $inbox = "INBOX" . $del; |
234 | if (substr($mailbox, 0, strlen($inbox)) == $inbox) |
235 | $box = substr($mailbox, strlen($inbox), strlen($mailbox)); |
8c7dfc99 |
236 | else |
237 | $box = $mailbox; |
238 | |
239 | return $box; |
240 | } |
05207a68 |
241 | |
aceb0d5c |
242 | /** This function gets all the information about a message. Including Header and body **/ |
243 | function fetchMessage($imapConnection, $id) { |
244 | $message["HEADER"] = fetchHeader($imapConnection, $id); |
d4467150 |
245 | $message["ENTITIES"] = fetchBody($imapConnection, $message["HEADER"]["BOUNDARY"], $id, $message["HEADER"]["TYPE0"], $message["HEADER"]["TYPE1"]); |
aceb0d5c |
246 | |
247 | return $message; |
248 | } |
249 | |
250 | function fetchHeader($imapConnection, $id) { |
5c55c295 |
251 | fputs($imapConnection, "messageFetch FETCH $id:$id RFC822.HEADER\n"); |
aceb0d5c |
252 | $read = fgets($imapConnection, 1024); |
253 | |
254 | /** defaults... if the don't get overwritten, it will display text **/ |
d4467150 |
255 | $header["TYPE0"] = "text"; |
256 | $header["TYPE1"] = "plain"; |
257 | $header["ENCODING"] = "us-ascii"; |
aceb0d5c |
258 | while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) { |
259 | /** MIME-VERSION **/ |
260 | if (substr($read, 0, 17) == "MIME-Version: 1.0") { |
261 | $header["MIME"] = true; |
262 | $read = fgets($imapConnection, 1024); |
263 | } |
d4467150 |
264 | |
265 | /** ENCODING TYPE **/ |
266 | else if (substr($read[$i], 0, 26) == "Content-Transfer-Encoding:") { |
267 | $header["ENCODING"] = strtolower(trim(substr($read[$i], 26))); |
268 | } |
269 | |
aceb0d5c |
270 | /** CONTENT-TYPE **/ |
271 | else if (substr($read, 0, 13) == "Content-Type:") { |
d4467150 |
272 | $cont = strtolower(trim(substr($read, 13))); |
273 | if (strpos($cont, ";")) |
274 | $cont = substr($cont, 0, strpos($cont, ";")); |
8405ee35 |
275 | |
276 | if (strpos($cont, "/")) { |
277 | $header["TYPE0"] = substr($cont, 0, strpos($cont, "/")); |
278 | $header["TYPE1"] = substr($cont, strpos($cont, "/")+1); |
279 | } else { |
280 | $header["TYPE0"] = $cont; |
281 | } |
aceb0d5c |
282 | |
d4467150 |
283 | $line = $read; |
aceb0d5c |
284 | $read = fgets($imapConnection, 1024); |
d4467150 |
285 | while ( (substr(substr($read, 0, strpos($read, " ")), -1) != ":") && (trim($read) != "") && (trim($read) != ")")) { |
286 | str_replace("\n", "", $line); |
287 | str_replace("\n", "", $read); |
288 | $line = "$line $read"; |
289 | $read = fgets($imapConnection, 1024); |
290 | } |
291 | |
292 | /** Detect the boundary of a multipart message **/ |
293 | if (strpos(strtolower(trim($line)), "boundary=")) { |
294 | $pos = strpos($line, "boundary=") + 9; |
295 | $bound = trim($line); |
296 | if (strpos($line, " ", $pos) > 0) { |
297 | $bound = substr($bound, $pos, strpos($line, " ", $pos)); |
298 | } else { |
299 | $bound = substr($bound, $pos); |
300 | } |
aceb0d5c |
301 | $bound = str_replace("\"", "", $bound); |
302 | $header["BOUNDARY"] = $bound; |
aceb0d5c |
303 | } |
d4467150 |
304 | |
305 | /** Detect the charset **/ |
306 | if (strpos(strtolower(trim($line)), "charset=")) { |
307 | $pos = strpos($line, "charset=") + 8; |
308 | $charset = trim($line); |
309 | if (strpos($line, " ", $pos) > 0) { |
310 | $charset = substr($charset, $pos, strpos($line, " ", $pos)); |
311 | } else { |
312 | $charset = substr($charset, $pos); |
313 | } |
314 | $charset = str_replace("\"", "", $charset); |
315 | $header["CHARSET"] = $charset; |
316 | } else { |
317 | $header["CHARSET"] = "us-ascii"; |
318 | } |
319 | |
7c9499e1 |
320 | /** Detects filename if any **/ |
321 | if (strpos(strtolower(trim($line)), "name=")) { |
322 | $pos = strpos($line, "name=") + 5; |
323 | $name = trim($line); |
324 | if (strpos($line, " ", $pos) > 0) { |
325 | $name = substr($name, $pos, strpos($line, " ", $pos)); |
326 | } else { |
327 | $name = substr($name, $pos); |
328 | } |
329 | $name = str_replace("\"", "", $name); |
330 | $header["FILENAME"] = $name; |
331 | } |
aceb0d5c |
332 | } |
5c55c295 |
333 | |
334 | /** REPLY-TO **/ |
8405ee35 |
335 | else if (strtolower(substr($read, 0, 9)) == "reply-to:") { |
5c55c295 |
336 | $header["REPLYTO"] = trim(substr($read, 9, strlen($read))); |
337 | $read = fgets($imapConnection, 1024); |
338 | } |
339 | |
aceb0d5c |
340 | /** FROM **/ |
8405ee35 |
341 | else if (strtolower(substr($read, 0, 5)) == "from:") { |
aceb0d5c |
342 | $header["FROM"] = trim(substr($read, 5, strlen($read) - 6)); |
5c55c295 |
343 | if ($header["REPLYTO"] == "") |
344 | $header["REPLYTO"] = $header["FROM"]; |
aceb0d5c |
345 | $read = fgets($imapConnection, 1024); |
346 | } |
347 | /** DATE **/ |
8405ee35 |
348 | else if (strtolower(substr($read, 0, 5)) == "date:") { |
aceb0d5c |
349 | $d = substr($read, 5, strlen($read) - 6); |
350 | $d = trim($d); |
351 | $d = ereg_replace(" ", " ", $d); |
352 | $d = explode(" ", $d); |
353 | $header["DATE"] = getTimeStamp($d); |
354 | $read = fgets($imapConnection, 1024); |
355 | } |
356 | /** SUBJECT **/ |
8405ee35 |
357 | else if (strtolower(substr($read, 0, 8)) == "subject:") { |
aceb0d5c |
358 | $header["SUBJECT"] = trim(substr($read, 8, strlen($read) - 9)); |
359 | if (strlen(Chop($header["SUBJECT"])) == 0) |
360 | $header["SUBJECT"] = "(no subject)"; |
361 | $read = fgets($imapConnection, 1024); |
362 | } |
363 | /** CC **/ |
8405ee35 |
364 | else if (strtolower(substr($read, 0, 3)) == "cc:") { |
aceb0d5c |
365 | $pos = 0; |
366 | $header["CC"][$pos] = trim(substr($read, 4)); |
367 | $read = fgets($imapConnection, 1024); |
368 | while ((substr($read, 0, 1) == " ") && (trim($read) != "")) { |
369 | $pos++; |
370 | $header["CC"][$pos] = trim($read); |
371 | $read = fgets($imapConnection, 1024); |
372 | } |
373 | } |
374 | /** TO **/ |
8405ee35 |
375 | else if (strtolower(substr($read, 0, 3)) == "to:") { |
aceb0d5c |
376 | $pos = 0; |
377 | $header["TO"][$pos] = trim(substr($read, 4)); |
378 | $read = fgets($imapConnection, 1024); |
379 | while ((substr($read, 0, 1) == " ") && (trim($read) != "")){ |
380 | $pos++; |
381 | $header["TO"][$pos] = trim($read); |
382 | $read = fgets($imapConnection, 1024); |
383 | } |
384 | } |
385 | |
386 | /** ERROR CORRECTION **/ |
387 | else if (substr($read, 0, 1) == ")") { |
388 | if ($header["SUBJECT"] == "") |
389 | $header["SUBJECT"] = "(no subject)"; |
390 | |
391 | if ($header["FROM"] == "") |
392 | $header["FROM"] = "(unknown sender)"; |
393 | |
394 | if ($header["DATE"] == "") |
395 | $header["DATE"] = time(); |
396 | $read = fgets($imapConnection, 1024); |
397 | } |
398 | else { |
399 | $read = fgets($imapConnection, 1024); |
400 | } |
401 | } |
402 | return $header; |
403 | } |
404 | |
405 | function fetchBody($imapConnection, $bound, $id, $type0, $type1) { |
406 | /** This first part reads in the full body of the message **/ |
05207a68 |
407 | fputs($imapConnection, "messageFetch FETCH $id:$id BODY[TEXT]\n"); |
aceb0d5c |
408 | $read = fgets($imapConnection, 1024); |
409 | |
05207a68 |
410 | $count = 0; |
aceb0d5c |
411 | while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) { |
412 | $body[$count] = $read; |
05207a68 |
413 | $count++; |
aceb0d5c |
414 | |
415 | $read = fgets($imapConnection, 1024); |
05207a68 |
416 | } |
417 | |
aceb0d5c |
418 | /** this deletes the first line, and the last two (imap stuff we ignore) **/ |
e550d551 |
419 | $i = 0; |
420 | $j = 0; |
aceb0d5c |
421 | while ($i < count($body)) { |
422 | if ( ($i != 0) && ($i != count($body) - 1) && ($i != count($body)) ) { |
423 | $bodytmp[$j] = $body[$i]; |
e550d551 |
424 | $j++; |
425 | } |
426 | $i++; |
427 | } |
aceb0d5c |
428 | $body = $bodytmp; |
e550d551 |
429 | |
aceb0d5c |
430 | /** Now, lets work out the MIME stuff **/ |
431 | /** (needs mime.php included) **/ |
432 | return decodeMime($body, $bound, $type0, $type1); |
433 | } |
05207a68 |
434 | |
7c9499e1 |
435 | function fetchEntityHeader($imapConnection, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) { |
aceb0d5c |
436 | /** defaults... if the don't get overwritten, it will display text **/ |
437 | $type0 = "text"; |
438 | $type1 = "plain"; |
d4467150 |
439 | $encoding = "us-ascii"; |
aceb0d5c |
440 | $i = 0; |
441 | while (trim($read[$i]) != "") { |
d4467150 |
442 | if (substr($read[$i], 0, 26) == "Content-Transfer-Encoding:") { |
443 | $encoding = strtolower(trim(substr($read[$i], 26))); |
444 | |
445 | } else if (substr($read[$i], 0, 13) == "Content-Type:") { |
446 | $cont = strtolower(trim(substr($read[$i], 13))); |
447 | if (strpos($cont, ";")) |
448 | $cont = substr($cont, 0, strpos($cont, ";")); |
8405ee35 |
449 | |
450 | if (strpos($cont, "/")) { |
451 | $type0 = substr($cont, 0, strpos($cont, "/")); |
452 | $type1 = substr($cont, strpos($cont, "/")+1); |
453 | } else { |
454 | $type0 = $cont; |
455 | } |
aceb0d5c |
456 | |
7c9499e1 |
457 | $read[$i] = trim($read[$i]); |
d4467150 |
458 | $line = $read[$i]; |
7c9499e1 |
459 | $i++; |
d4467150 |
460 | while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) { |
461 | str_replace("\n", "", $line); |
462 | str_replace("\n", "", $read[$i]); |
463 | $line = "$line $read[$i]"; |
464 | $i++; |
7c9499e1 |
465 | $read[$i] = trim($read[$i]); |
d4467150 |
466 | } |
467 | |
468 | /** Detect the boundary of a multipart message **/ |
469 | if (strpos(strtolower(trim($line)), "boundary=")) { |
470 | $pos = strpos($line, "boundary=") + 9; |
471 | $bound = trim($line); |
472 | if (strpos($line, " ", $pos) > 0) { |
473 | $bound = substr($bound, $pos, strpos($line, " ", $pos)); |
474 | } else { |
475 | $bound = substr($bound, $pos); |
476 | } |
aceb0d5c |
477 | $bound = str_replace("\"", "", $bound); |
478 | } |
d4467150 |
479 | |
480 | /** Detect the charset **/ |
481 | if (strpos(strtolower(trim($line)), "charset=")) { |
482 | $pos = strpos($line, "charset=") + 8; |
483 | $charset = trim($line); |
484 | if (strpos($line, " ", $pos) > 0) { |
485 | $charset = substr($charset, $pos, strpos($line, " ", $pos)); |
486 | } else { |
487 | $charset = substr($charset, $pos); |
488 | } |
489 | $charset = str_replace("\"", "", $charset); |
490 | } |
7c9499e1 |
491 | |
492 | /** Detects filename if any **/ |
493 | if (strpos(strtolower(trim($line)), "name=")) { |
494 | $pos = strpos($line, "name=") + 5; |
495 | $name = trim($line); |
496 | if (strpos($line, " ", $pos) > 0) { |
497 | $name = substr($name, $pos, strpos($line, " ", $pos)); |
498 | } else { |
499 | $name = substr($name, $pos); |
500 | } |
501 | $name = str_replace("\"", "", $name); |
502 | $filename = $name; |
503 | } |
e550d551 |
504 | } |
aceb0d5c |
505 | $i++; |
506 | } |
05207a68 |
507 | |
aceb0d5c |
508 | /** remove the header from the entity **/ |
509 | $i = 0; |
510 | while (trim($read[$i]) != "") { |
511 | $i++; |
e550d551 |
512 | } |
aceb0d5c |
513 | $i++; |
05207a68 |
514 | |
aceb0d5c |
515 | for ($p = 0; $i < count($read); $p++) { |
516 | $entity[$p] = $read[$i]; |
517 | $i++; |
518 | } |
519 | |
520 | $read = $entity; |
e550d551 |
521 | } |
05207a68 |
522 | |
aceb0d5c |
523 | function parsePlainTextMessage($line) { |
e550d551 |
524 | $line = "^^$line"; |
05207a68 |
525 | |
e550d551 |
526 | if ((strpos(strtolower($line), "<!") == false) && |
527 | (strpos(strtolower($line), "<html>") == false) && |
528 | (strpos(strtolower($line), "</html>") == false)) { |
529 | $line = str_replace("<", "<", $line); |
530 | $line = str_replace(">", ">", $line); |
05207a68 |
531 | } |
532 | |
ff89ac8a |
533 | $wrap_at = 86; // Make this configurable int the config file some time |
8467bf00 |
534 | if (strlen($line) - 2 >= $wrap_at) // -2 because of the ^^ at the beginning |
535 | $line = wordWrap($line, $wrap_at); |
b8ea4ed6 |
536 | |
e550d551 |
537 | $line = str_replace(" ", " ", $line); |
538 | $line = str_replace("\t", " ", $line); |
8405ee35 |
539 | $line = str_replace("\n", "", $line); |
e550d551 |
540 | |
541 | /** if >> or > are found at the beginning of a line, I'll assume that was |
542 | replied text, so make it different colors **/ |
543 | if (strpos(trim(str_replace(" ", "", $line)), ">>") == 2) { |
544 | $line = substr($line, 2, strlen($line)); |
545 | $line = "<TT><FONT COLOR=FF0000>$line</FONT></TT><BR>\n"; |
546 | } else if (strpos(trim(str_replace(" ", "", $line)), ">") == 2) { |
547 | $line = substr($line, 2, strlen($line)); |
548 | $line = "<TT><FONT COLOR=800000>$line</FONT></TT><BR>\n"; |
549 | } else { |
550 | $line = substr($line, 2, strlen($line)); |
551 | $line = "<TT><FONT COLOR=000000>$line</FONT></TT><BR>\n"; |
552 | } |
553 | |
554 | /** This translates "http://" into a link. It could be made better to accept |
555 | "www" and "mailto" also. That should probably be added later. **/ |
556 | if (strpos(strtolower($line), "http://") != false) { |
9774bdef |
557 | $line = ereg_replace("<BR>", "", $line); |
e550d551 |
558 | $start = strpos(strtolower($line), "http://"); |
9774bdef |
559 | $link = substr($line, $start, strlen($line)); |
e550d551 |
560 | |
9774bdef |
561 | if (strpos($link, " ")) { |
562 | $end = strpos($link, " ")-1; |
563 | } |
564 | else if (strpos($link, " ")) { |
565 | $end = strpos($link, " ")-1; |
566 | } |
567 | else if (strpos($link, "<")) { |
e550d551 |
568 | $end = strpos($link, "<"); |
9774bdef |
569 | } |
570 | else if (strpos($link, ">")) { |
571 | $end = strpos($link, ">"); |
572 | } |
573 | else if (strpos($link, "(")) { |
574 | $end = strpos($link, "(")-1; |
575 | } |
576 | else if (strpos($link, ")")) { |
577 | $end = strpos($link, ")")-1; |
578 | } |
579 | else if (strpos($link, "{")) { |
580 | $end = strpos($link, "{")-1; |
581 | } |
582 | else if (strpos($link, "}")) { |
583 | $end = strpos($link, "}")-1; |
584 | } |
e550d551 |
585 | else |
586 | $end = strlen($link); |
587 | |
9774bdef |
588 | $link = substr($line, $start, $end); |
589 | $end = $end + $start; |
590 | $before = substr($line, 0, $start); |
591 | $after = substr($line, $end, strlen($line)); |
592 | |
593 | $line = "$before<A HREF=\"$link\" TARGET=_top>$link</A>$after<BR>"; |
e550d551 |
594 | } |
595 | |
596 | return $line; |
05207a68 |
597 | } |
a09387f4 |
598 | ?> |