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 | |
05207a68 |
38 | $from_pos = 0; |
39 | $date_pos = 0; |
40 | $subj_pos = 0; |
3e054c43 |
41 | while ($rel_start <= $end) { |
42 | if ($end - $rel_start > 50) { |
43 | $rel_end = $rel_start + 50; |
44 | } else { |
45 | $rel_end = $end; |
46 | } |
47 | fputs($imapConnection, "messageFetch FETCH $rel_start:$rel_end RFC822.HEADER.LINES (From Subject Date)\n"); |
3302d0d4 |
48 | $read = fgets($imapConnection, 1024); |
3e054c43 |
49 | |
3e054c43 |
50 | while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) { |
51 | if (substr($read, 0, 5) == "From:") { |
52 | $read = ereg_replace("<", "EMAILSTART--", $read); |
53 | $read = ereg_replace(">", "--EMAILEND", $read); |
05207a68 |
54 | $from[$from_pos] = substr($read, 5, strlen($read) - 6); |
55 | $from_pos++; |
3e054c43 |
56 | } |
57 | else if (substr($read, 0, 5) == "Date:") { |
df978e80 |
58 | $read = ereg_replace("<", "<", $read); |
59 | $read = ereg_replace(">", ">", $read); |
05207a68 |
60 | $date[$date_pos] = substr($read, 5, strlen($read) - 6); |
61 | $date_pos++; |
3e054c43 |
62 | } |
63 | else if (substr($read, 0, 8) == "Subject:") { |
df978e80 |
64 | $read = ereg_replace("<", "<", $read); |
65 | $read = ereg_replace(">", ">", $read); |
05207a68 |
66 | $subject[$subj_pos] = substr($read, 8, strlen($read) - 9); |
67 | if (strlen(Chop($subject[$subj_pos])) == 0) |
68 | $subject[$subj_pos] = "(no subject)"; |
69 | $subj_pos++; |
3e054c43 |
70 | } |
71 | $read = fgets($imapConnection, 1024); |
72 | } |
73 | $rel_start = $rel_start + 50; |
3302d0d4 |
74 | } |
75 | } |
76 | |
61a4ac35 |
77 | function setMessageFlag($imapConnection, $i, $q, $flag) { |
78 | fputs($imapConnection, "messageStore STORE $i:$q +FLAGS (\\$flag)\n"); |
aa4c3749 |
79 | } |
80 | |
0e919368 |
81 | /** This function gets the flags for message $j. It does only one message at a |
82 | ** time, rather than doing groups of messages (like getMessageHeaders does). |
83 | ** I found it one or two seconds quicker (on a box of 800 messages) to do it |
84 | ** individually. I'm not sure why it happens like that, but that's what my |
85 | ** testing found. Perhaps later I will be proven wrong and this will change. |
86 | **/ |
3e054c43 |
87 | function getMessageFlags($imapConnection, $j, &$flags) { |
3302d0d4 |
88 | /** * 2 FETCH (FLAGS (\Answered \Seen)) */ |
3e054c43 |
89 | fputs($imapConnection, "messageFetch FETCH $j:$j FLAGS\n"); |
aa4c3749 |
90 | $read = fgets($imapConnection, 1024); |
3e054c43 |
91 | $count = 0; |
3302d0d4 |
92 | while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) { |
93 | if (strpos($read, "FLAGS")) { |
94 | $read = ereg_replace("\(", "", $read); |
95 | $read = ereg_replace("\)", "", $read); |
96 | $read = substr($read, strpos($read, "FLAGS")+6, strlen($read)); |
97 | $read = trim($read); |
98 | $flags = explode(" ", $read);; |
54a8ae32 |
99 | $s = 0; |
100 | while ($s < count($flags)) { |
101 | $flags[$s] = substr($flags[$s], 1, strlen($flags[$s])); |
102 | $s++; |
3302d0d4 |
103 | } |
104 | } else { |
105 | $flags[0] = "None"; |
106 | } |
3e054c43 |
107 | $count++; |
3302d0d4 |
108 | $read = fgets($imapConnection, 1024); |
109 | } |
110 | } |
111 | |
112 | function getEmailAddr($sender) { |
113 | if (strpos($sender, "EMAILSTART--") == false) |
114 | return ""; |
115 | |
116 | $start = strpos($sender, "EMAILSTART--"); |
117 | $emailAddr = substr($sender, $start, strlen($sender)); |
118 | |
119 | return $emailAddr; |
120 | } |
121 | |
122 | function getSender($sender) { |
123 | if (strpos($sender, "EMAILSTART--") == false) |
124 | return ""; |
125 | |
126 | $first = substr($sender, 0, strpos($sender, "EMAILSTART--")); |
127 | $second = substr($sender, strpos($sender, "--EMAILEND") +10, strlen($sender)); |
128 | return "$first$second"; |
129 | } |
130 | |
131 | function getSenderName($sender) { |
132 | $name = getSender($sender); |
133 | $emailAddr = getEmailAddr($sender); |
134 | $emailStart = strpos($emailAddr, "EMAILSTART--"); |
135 | $emailEnd = strpos($emailAddr, "--EMAILEND") - 10; |
136 | |
137 | if (($emailAddr == "") && ($name == "")) { |
138 | $from = $sender; |
139 | } |
140 | else if ((strstr($name, "?") != false) || (strstr($name, "$") != false) || (strstr($name, "%") != false)){ |
141 | $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr); |
142 | $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr); |
143 | $from = $emailAddr; |
144 | } |
145 | else if (strlen($name) > 0) { |
146 | $from = $name; |
147 | } |
148 | else if (strlen($emailAddr > 0)) { |
149 | $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr); |
150 | $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr); |
151 | $from = $emailAddr; |
152 | } |
153 | |
154 | $from = trim($from); |
155 | |
156 | // strip out any quotes if they exist |
157 | if ((strlen($from) > 0) && ($from[0] == "\"") && ($from[strlen($from) - 1] == "\"")) |
158 | $from = substr($from, 1, strlen($from) - 2); |
159 | |
160 | return $from; |
161 | } |
aa4c3749 |
162 | |
163 | /** returns "true" if the copy was completed successfully. |
164 | ** returns "false" with an error message if unsuccessful. |
165 | **/ |
166 | function copyMessages($imapConnection, $from_id, $to_id, $folder) { |
167 | fputs($imapConnection, "mailboxStore COPY $from_id:$to_id \"$folder\"\n"); |
168 | $read = fgets($imapConnection, 1024); |
169 | while ((substr($read, 0, 15) != "mailboxStore OK") && (substr($read, 0, 15) != "mailboxStore NO")) { |
170 | $read = fgets($imapConnection, 1024); |
171 | } |
172 | |
173 | if (substr($read, 0, 15) == "mailboxStore NO") { |
174 | echo "ERROR... $read<BR>"; |
175 | return false; |
176 | } else if (substr($read, 0, 15) == "mailboxStore OK") { |
177 | return true; |
178 | } |
179 | |
180 | echo "UNKNOWN ERROR copying messages $from_id to $to_id to folder $folder.<BR>"; |
181 | return false; |
182 | } |
61a4ac35 |
183 | |
184 | /** expunges a mailbox **/ |
185 | function expungeBox($imapConnection, $mailbox) { |
186 | selectMailbox($imapConnection, $mailbox, $num); |
187 | fputs($imapConnection, "1 EXPUNGE\n"); |
188 | } |
8c7dfc99 |
189 | |
190 | function getFolderNameMinusINBOX($mailbox) { |
191 | if (substr($mailbox, 0, 6) == "INBOX.") |
192 | $box = substr($mailbox, 6, strlen($mailbox)); |
193 | else |
194 | $box = $mailbox; |
195 | |
196 | return $box; |
197 | } |
05207a68 |
198 | |
199 | function fetchBody($imapConnection, $id) { |
200 | fputs($imapConnection, "messageFetch FETCH $id:$id BODY[TEXT]\n"); |
201 | $count = 0; |
202 | $read[$count] = fgets($imapConnection, 1024); |
203 | while ((substr($read[$count], 0, 15) != "messageFetch OK") && (substr($read[$count], 0, 16) != "messageFetch BAD")) { |
204 | $count++; |
205 | $read[$count] = fgets($imapConnection, 1024); |
206 | } |
207 | |
208 | $count = 0; |
209 | $useHTML= false; |
210 | while ($count < count($read)) { |
211 | $read[$count] = "^^$read[$count]"; |
212 | if (strpos($read[$count], "<html>") == true) { |
213 | $useHTML = true; |
214 | } else if (strpos(strtolower($read[$count]), "</html") == true) { |
215 | $useHTML= false; |
216 | } |
217 | $read[$count] = substr($read[$count], 2, strlen($read[$count])); |
218 | |
219 | if ($useHTML == false) { |
220 | $read[$count] = str_replace(" ", " ", $read[$count]); |
221 | $read[$count] = str_replace("\n", "", $read[$count]); |
222 | $read[$count] = str_replace("\r", "", $read[$count]); |
223 | $read[$count] = str_replace("\t", " ", $read[$count]); |
224 | |
225 | $read[$count] = "^^$read[$count]"; |
226 | if (strpos(trim(str_replace(" ", "", $read[$count])), ">>") == 2) { |
227 | $read[$count] = substr($read[$count], 2, strlen($read[$count])); |
228 | $read[$count] = "<FONT FACE=\"Fixed\" COLOR=FF0000>$read[$count]</FONT>\n"; |
229 | } else if (strpos(trim(str_replace(" ", "", $read[$count])), ">") == 2) { |
230 | $read[$count] = substr($read[$count], 2, strlen($read[$count])); |
231 | $read[$count] = "<FONT FACE=\"Fixed\" COLOR=800000>$read[$count]</FONT>\n"; |
232 | } else { |
233 | $read[$count] = substr($read[$count], 2, strlen($read[$count])); |
234 | $read[$count] = "<FONT FACE=\"Fixed\" COLOR=000000>$read[$count]</FONT>\n"; |
235 | } |
236 | |
237 | if (strpos(strtolower($read[$count]), "http://") != false) { |
238 | $start = strpos(strtolower($read[$count]), "http://"); |
239 | $link = substr($read[$count], $start, strlen($read[$count])); |
240 | |
241 | if (strpos($link, " ")) |
242 | $end = strpos($link, " "); |
243 | else if (strpos($link, "<")) |
244 | $end = strpos($link, "<"); |
245 | else |
246 | $end = strlen($link); |
247 | |
248 | $link = substr($link, 0, $end); |
249 | |
250 | $read[$count] = str_replace($link, "<A HREF=\"$link\" TARGET=_top>$link</A>", $read[$count]); |
251 | } |
252 | } |
253 | $count++; |
254 | } |
255 | |
256 | return $read; |
257 | } |
a09387f4 |
258 | ?> |