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"); |
13 | $read = fgets($imapConnection, 1024); |
14 | while ((substr($read, 0, 16) != "mailboxSelect OK") && (substr($read, 0, 17) != "mailboxSelect BAD")) { |
15 | if (substr(Chop($read), -6) == "EXISTS") { |
16 | $array = explode(" ", $read); |
17 | $numberOfMessages = $array[1]; |
18 | } |
19 | $read = fgets($imapConnection, 1024); |
20 | } |
21 | } |
22 | |
0e919368 |
23 | /** This function sends a request to the IMAP server for headers, 50 at a time |
24 | ** until $end is reached. I originally had it do them all at one time, but found |
25 | ** it slightly faster to do it this way. |
26 | ** |
27 | ** Originally we had getMessageHeaders get the headers for one message at a time. |
28 | ** Doing it in bunches gave us a speed increase from 9 seconds (for a box of 800 |
29 | ** messages) to about 3.5 seconds. |
30 | **/ |
3e054c43 |
31 | function getMessageHeaders($imapConnection, $start, $end, &$from, &$subject, &$date) { |
3e054c43 |
32 | $rel_start = $start; |
3e054c43 |
33 | if (($start > $end) || ($start < 1)) { |
34 | echo "Error in message header fetching. Start message: $start, End message: $end<BR>"; |
35 | exit; |
36 | } |
3302d0d4 |
37 | |
e550d551 |
38 | $pos = 0; |
3e054c43 |
39 | while ($rel_start <= $end) { |
40 | if ($end - $rel_start > 50) { |
e550d551 |
41 | $rel_end = $rel_start + 49; |
3e054c43 |
42 | } else { |
43 | $rel_end = $end; |
44 | } |
45 | fputs($imapConnection, "messageFetch FETCH $rel_start:$rel_end RFC822.HEADER.LINES (From Subject Date)\n"); |
3302d0d4 |
46 | $read = fgets($imapConnection, 1024); |
3e054c43 |
47 | |
3e054c43 |
48 | while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) { |
e550d551 |
49 | |
3e054c43 |
50 | if (substr($read, 0, 5) == "From:") { |
51 | $read = ereg_replace("<", "EMAILSTART--", $read); |
52 | $read = ereg_replace(">", "--EMAILEND", $read); |
e550d551 |
53 | $from[$pos] = substr($read, 5, strlen($read) - 6); |
3e054c43 |
54 | } |
55 | else if (substr($read, 0, 5) == "Date:") { |
df978e80 |
56 | $read = ereg_replace("<", "<", $read); |
57 | $read = ereg_replace(">", ">", $read); |
e550d551 |
58 | $date[$pos] = substr($read, 5, strlen($read) - 6); |
3e054c43 |
59 | } |
60 | else if (substr($read, 0, 8) == "Subject:") { |
df978e80 |
61 | $read = ereg_replace("<", "<", $read); |
62 | $read = ereg_replace(">", ">", $read); |
e550d551 |
63 | $subject[$pos] = substr($read, 8, strlen($read) - 9); |
64 | if (strlen(Chop($subject[$pos])) == 0) |
65 | $subject[$pos] = "(no subject)"; |
66 | } |
67 | else if (substr($read, 0, 1) == ")") { |
68 | if ($subject[$pos] == "") |
69 | $subject[$pos] = "(no subject)"; |
70 | else if ($from[$pos] == "") |
71 | $from[$pos] = "(unknown sender)"; |
72 | else if ($date[$pos] == "") |
73 | $from[$pos] = gettimeofday(); |
74 | |
75 | $pos++; |
3e054c43 |
76 | } |
e550d551 |
77 | |
3e054c43 |
78 | $read = fgets($imapConnection, 1024); |
79 | } |
80 | $rel_start = $rel_start + 50; |
3302d0d4 |
81 | } |
82 | } |
83 | |
61a4ac35 |
84 | function setMessageFlag($imapConnection, $i, $q, $flag) { |
85 | fputs($imapConnection, "messageStore STORE $i:$q +FLAGS (\\$flag)\n"); |
aa4c3749 |
86 | } |
87 | |
0e919368 |
88 | /** This function gets the flags for message $j. It does only one message at a |
89 | ** time, rather than doing groups of messages (like getMessageHeaders does). |
90 | ** I found it one or two seconds quicker (on a box of 800 messages) to do it |
91 | ** individually. I'm not sure why it happens like that, but that's what my |
92 | ** testing found. Perhaps later I will be proven wrong and this will change. |
93 | **/ |
3e054c43 |
94 | function getMessageFlags($imapConnection, $j, &$flags) { |
3302d0d4 |
95 | /** * 2 FETCH (FLAGS (\Answered \Seen)) */ |
3e054c43 |
96 | fputs($imapConnection, "messageFetch FETCH $j:$j FLAGS\n"); |
aa4c3749 |
97 | $read = fgets($imapConnection, 1024); |
3e054c43 |
98 | $count = 0; |
3302d0d4 |
99 | while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) { |
100 | if (strpos($read, "FLAGS")) { |
101 | $read = ereg_replace("\(", "", $read); |
102 | $read = ereg_replace("\)", "", $read); |
103 | $read = substr($read, strpos($read, "FLAGS")+6, strlen($read)); |
104 | $read = trim($read); |
105 | $flags = explode(" ", $read);; |
54a8ae32 |
106 | $s = 0; |
107 | while ($s < count($flags)) { |
108 | $flags[$s] = substr($flags[$s], 1, strlen($flags[$s])); |
109 | $s++; |
3302d0d4 |
110 | } |
111 | } else { |
112 | $flags[0] = "None"; |
113 | } |
3e054c43 |
114 | $count++; |
3302d0d4 |
115 | $read = fgets($imapConnection, 1024); |
116 | } |
117 | } |
118 | |
31f3d7c0 |
119 | function decodeEmailAddr($sender) { |
120 | $emailAddr = getEmailAddr($sender); |
121 | $emailStart = strpos($emailAddr, "EMAILSTART--"); |
122 | $emailEnd = strpos($emailAddr, "--EMAILEND") - 10; |
123 | |
124 | $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr); |
125 | $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr); |
126 | return $emailAddr; |
127 | } |
128 | |
3302d0d4 |
129 | function getEmailAddr($sender) { |
130 | if (strpos($sender, "EMAILSTART--") == false) |
131 | return ""; |
132 | |
133 | $start = strpos($sender, "EMAILSTART--"); |
134 | $emailAddr = substr($sender, $start, strlen($sender)); |
135 | |
136 | return $emailAddr; |
137 | } |
138 | |
139 | function getSender($sender) { |
140 | if (strpos($sender, "EMAILSTART--") == false) |
141 | return ""; |
142 | |
143 | $first = substr($sender, 0, strpos($sender, "EMAILSTART--")); |
144 | $second = substr($sender, strpos($sender, "--EMAILEND") +10, strlen($sender)); |
145 | return "$first$second"; |
146 | } |
147 | |
148 | function getSenderName($sender) { |
149 | $name = getSender($sender); |
150 | $emailAddr = getEmailAddr($sender); |
151 | $emailStart = strpos($emailAddr, "EMAILSTART--"); |
152 | $emailEnd = strpos($emailAddr, "--EMAILEND") - 10; |
153 | |
154 | if (($emailAddr == "") && ($name == "")) { |
155 | $from = $sender; |
156 | } |
157 | else if ((strstr($name, "?") != false) || (strstr($name, "$") != false) || (strstr($name, "%") != false)){ |
158 | $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr); |
159 | $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr); |
160 | $from = $emailAddr; |
161 | } |
162 | else if (strlen($name) > 0) { |
163 | $from = $name; |
164 | } |
165 | else if (strlen($emailAddr > 0)) { |
166 | $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr); |
167 | $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr); |
168 | $from = $emailAddr; |
169 | } |
170 | |
171 | $from = trim($from); |
172 | |
173 | // strip out any quotes if they exist |
174 | if ((strlen($from) > 0) && ($from[0] == "\"") && ($from[strlen($from) - 1] == "\"")) |
175 | $from = substr($from, 1, strlen($from) - 2); |
176 | |
177 | return $from; |
178 | } |
aa4c3749 |
179 | |
180 | /** returns "true" if the copy was completed successfully. |
181 | ** returns "false" with an error message if unsuccessful. |
182 | **/ |
183 | function copyMessages($imapConnection, $from_id, $to_id, $folder) { |
184 | fputs($imapConnection, "mailboxStore COPY $from_id:$to_id \"$folder\"\n"); |
185 | $read = fgets($imapConnection, 1024); |
e2370222 |
186 | echo ">>> $read<BR>"; |
aa4c3749 |
187 | while ((substr($read, 0, 15) != "mailboxStore OK") && (substr($read, 0, 15) != "mailboxStore NO")) { |
188 | $read = fgets($imapConnection, 1024); |
e2370222 |
189 | echo ">>> $read<BR>"; |
aa4c3749 |
190 | } |
191 | |
192 | if (substr($read, 0, 15) == "mailboxStore NO") { |
aa4c3749 |
193 | return false; |
194 | } else if (substr($read, 0, 15) == "mailboxStore OK") { |
195 | return true; |
196 | } |
197 | |
198 | echo "UNKNOWN ERROR copying messages $from_id to $to_id to folder $folder.<BR>"; |
199 | return false; |
200 | } |
61a4ac35 |
201 | |
202 | /** expunges a mailbox **/ |
203 | function expungeBox($imapConnection, $mailbox) { |
204 | selectMailbox($imapConnection, $mailbox, $num); |
205 | fputs($imapConnection, "1 EXPUNGE\n"); |
206 | } |
8c7dfc99 |
207 | |
208 | function getFolderNameMinusINBOX($mailbox) { |
209 | if (substr($mailbox, 0, 6) == "INBOX.") |
210 | $box = substr($mailbox, 6, strlen($mailbox)); |
211 | else |
212 | $box = $mailbox; |
213 | |
214 | return $box; |
215 | } |
05207a68 |
216 | |
e550d551 |
217 | /** This function will fetch the body of a given message and format |
218 | it into our standard format. **/ |
05207a68 |
219 | function fetchBody($imapConnection, $id) { |
220 | fputs($imapConnection, "messageFetch FETCH $id:$id BODY[TEXT]\n"); |
221 | $count = 0; |
222 | $read[$count] = fgets($imapConnection, 1024); |
223 | while ((substr($read[$count], 0, 15) != "messageFetch OK") && (substr($read[$count], 0, 16) != "messageFetch BAD")) { |
224 | $count++; |
225 | $read[$count] = fgets($imapConnection, 1024); |
226 | } |
227 | |
e550d551 |
228 | /** this loop removes the first line, and the last two which |
229 | are IMAP information that we don't need. **/ |
230 | $i = 0; |
231 | $j = 0; |
232 | while ($i < count($read)) { |
233 | if (($i != 0) && ($i != count($read) - 1) && ($i != count($read) - 2)){ |
234 | $readtmp[$j] = $read[$i]; |
235 | $j++; |
236 | } |
237 | $i++; |
238 | } |
239 | $read = $readtmp; |
240 | |
241 | /** This loop formats the text, creating links out of linkable stuff too **/ |
05207a68 |
242 | $count = 0; |
243 | $useHTML= false; |
244 | while ($count < count($read)) { |
245 | $read[$count] = "^^$read[$count]"; |
e550d551 |
246 | |
247 | if (strpos(strtolower($read[$count]), "<html>") == true) { |
05207a68 |
248 | $useHTML = true; |
e550d551 |
249 | } else if (strpos(strtolower($read[$count]), "</html>") == true) { |
250 | $useHTML = false; |
05207a68 |
251 | } |
e550d551 |
252 | |
05207a68 |
253 | $read[$count] = substr($read[$count], 2, strlen($read[$count])); |
254 | |
255 | if ($useHTML == false) { |
e550d551 |
256 | $read[$count] = parsePlainBodyText($read[$count]); |
257 | } else { |
258 | $read[$count] = parseHTMLBodyText($read[$count]); |
259 | } |
05207a68 |
260 | |
e550d551 |
261 | $count++; |
262 | } |
263 | return $read; |
264 | } |
05207a68 |
265 | |
e550d551 |
266 | function parseHTMLBodyText($line) { |
267 | return $line; |
268 | } |
05207a68 |
269 | |
e550d551 |
270 | function parsePlainBodyText($line) { |
271 | $line = "^^$line"; |
05207a68 |
272 | |
e550d551 |
273 | if ((strpos(strtolower($line), "<!") == false) && |
274 | (strpos(strtolower($line), "<html>") == false) && |
275 | (strpos(strtolower($line), "</html>") == false)) { |
276 | $line = str_replace("<", "<", $line); |
277 | $line = str_replace(">", ">", $line); |
05207a68 |
278 | } |
279 | |
8467bf00 |
280 | $wrap_at = 80; // Make this configurable int the config file some time |
281 | if (strlen($line) - 2 >= $wrap_at) // -2 because of the ^^ at the beginning |
282 | $line = wordWrap($line, $wrap_at); |
b8ea4ed6 |
283 | |
e550d551 |
284 | $line = str_replace(" ", " ", $line); |
285 | $line = str_replace("\t", " ", $line); |
9774bdef |
286 | $line = str_replace("\n", "", $line); |
287 | $line = str_replace("\r", "", $line); |
e550d551 |
288 | |
289 | /** if >> or > are found at the beginning of a line, I'll assume that was |
290 | replied text, so make it different colors **/ |
291 | if (strpos(trim(str_replace(" ", "", $line)), ">>") == 2) { |
292 | $line = substr($line, 2, strlen($line)); |
293 | $line = "<TT><FONT COLOR=FF0000>$line</FONT></TT><BR>\n"; |
294 | } else if (strpos(trim(str_replace(" ", "", $line)), ">") == 2) { |
295 | $line = substr($line, 2, strlen($line)); |
296 | $line = "<TT><FONT COLOR=800000>$line</FONT></TT><BR>\n"; |
297 | } else { |
298 | $line = substr($line, 2, strlen($line)); |
299 | $line = "<TT><FONT COLOR=000000>$line</FONT></TT><BR>\n"; |
300 | } |
301 | |
302 | /** This translates "http://" into a link. It could be made better to accept |
303 | "www" and "mailto" also. That should probably be added later. **/ |
304 | if (strpos(strtolower($line), "http://") != false) { |
9774bdef |
305 | $line = ereg_replace("<BR>", "", $line); |
e550d551 |
306 | $start = strpos(strtolower($line), "http://"); |
9774bdef |
307 | $link = substr($line, $start, strlen($line)); |
e550d551 |
308 | |
9774bdef |
309 | if (strpos($link, " ")) { |
310 | $end = strpos($link, " ")-1; |
311 | } |
312 | else if (strpos($link, " ")) { |
313 | $end = strpos($link, " ")-1; |
314 | } |
315 | else if (strpos($link, "<")) { |
e550d551 |
316 | $end = strpos($link, "<"); |
9774bdef |
317 | } |
318 | else if (strpos($link, ">")) { |
319 | $end = strpos($link, ">"); |
320 | } |
321 | else if (strpos($link, "(")) { |
322 | $end = strpos($link, "(")-1; |
323 | } |
324 | else if (strpos($link, ")")) { |
325 | $end = strpos($link, ")")-1; |
326 | } |
327 | else if (strpos($link, "{")) { |
328 | $end = strpos($link, "{")-1; |
329 | } |
330 | else if (strpos($link, "}")) { |
331 | $end = strpos($link, "}")-1; |
332 | } |
e550d551 |
333 | else |
334 | $end = strlen($link); |
335 | |
9774bdef |
336 | $link = substr($line, $start, $end); |
337 | $end = $end + $start; |
338 | $before = substr($line, 0, $start); |
339 | $after = substr($line, $end, strlen($line)); |
340 | |
341 | $line = "$before<A HREF=\"$link\" TARGET=_top>$link</A>$after<BR>"; |
e550d551 |
342 | } |
343 | |
344 | return $line; |
05207a68 |
345 | } |
2844086d |
346 | |
9774bdef |
347 | /* |
348 | $start = strpos(strtolower($line), "http://"); |
349 | $text = substr($line, $start, strlen($line)); |
350 | $linktext = substr($link, 0, $end); |
351 | $link = trim(ereg_replace("<BR>", "", $linktext)); |
352 | |
353 | |
354 | // $line = str_replace($text, "<A HREF=\"$link\" TARGET=_top>$link</A>", $line); |
355 | */ |
356 | |
2844086d |
357 | function getMessageHeadersTo($imapConnection, $start, $end, &$to) { |
358 | $rel_start = $start; |
359 | if (($start > $end) || ($start < 1)) { |
360 | echo "Error in message header fetching. Start message: $start, End message: $end<BR>"; |
361 | exit; |
362 | } |
363 | |
364 | $pos = 0; |
365 | while ($rel_start <= $end) { |
366 | if ($end - $rel_start > 50) { |
367 | $rel_end = $rel_start + 49; |
368 | } else { |
369 | $rel_end = $end; |
370 | } |
371 | fputs($imapConnection, "messageFetch FETCH $rel_start:$rel_end RFC822.HEADER.LINES (To)\n"); |
372 | $read = fgets($imapConnection, 1024); |
373 | |
374 | while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) { |
375 | if (substr($read, 0, 3) == "To:") { |
376 | $read = ereg_replace("<", "<", $read); |
377 | $read = ereg_replace(">", ">", $read); |
378 | $to[$pos] = substr($read, 3, strlen($read)); |
379 | if (strlen(Chop($to[$pos])) == 0) |
380 | $to[$pos] = "Unknown Recipients"; |
381 | } |
382 | else if (substr($read, 0, 1) == ")") { |
383 | if ($subject[$pos] == "") |
384 | $subject[$pos] = "Unknown Recipients"; |
385 | $pos++; |
386 | } |
387 | |
388 | $read = fgets($imapConnection, 1024); |
389 | } |
390 | $rel_start = $rel_start + 50; |
391 | } |
392 | } |
393 | |
a09387f4 |
394 | ?> |