b5fa548d97dc81e213b64158b1aed165f6ac38e8
[squirrelmail.git] / functions / mailbox.php
1 <?
2 /**
3 ** mailbox.php
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
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 **/
31 function getMessageHeaders($imapConnection, $start, $end, &$from, &$subject, &$date) {
32 $rel_start = $start;
33 if (($start > $end) || ($start < 1)) {
34 echo "Error in message header fetching. Start message: $start, End message: $end<BR>";
35 exit;
36 }
37
38 $pos = 0;
39 while ($rel_start <= $end) {
40 if ($end - $rel_start > 50) {
41 $rel_end = $rel_start + 49;
42 } else {
43 $rel_end = $end;
44 }
45 fputs($imapConnection, "messageFetch FETCH $rel_start:$rel_end RFC822.HEADER.LINES (From Subject Date)\n");
46 $read = fgets($imapConnection, 1024);
47
48 while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
49
50 if (substr($read, 0, 5) == "From:") {
51 $read = ereg_replace("<", "EMAILSTART--", $read);
52 $read = ereg_replace(">", "--EMAILEND", $read);
53 $from[$pos] = substr($read, 5, strlen($read) - 6);
54 }
55 else if (substr($read, 0, 5) == "Date:") {
56 $read = ereg_replace("<", "&lt;", $read);
57 $read = ereg_replace(">", "&gt;", $read);
58 $date[$pos] = substr($read, 5, strlen($read) - 6);
59 }
60 else if (substr($read, 0, 8) == "Subject:") {
61 $read = ereg_replace("<", "&lt;", $read);
62 $read = ereg_replace(">", "&gt;", $read);
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++;
76 }
77
78 $read = fgets($imapConnection, 1024);
79 }
80 $rel_start = $rel_start + 50;
81 }
82 }
83
84 function setMessageFlag($imapConnection, $i, $q, $flag) {
85 fputs($imapConnection, "messageStore STORE $i:$q +FLAGS (\\$flag)\n");
86 }
87
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 **/
94 function getMessageFlags($imapConnection, $j, &$flags) {
95 /** * 2 FETCH (FLAGS (\Answered \Seen)) */
96 fputs($imapConnection, "messageFetch FETCH $j:$j FLAGS\n");
97 $read = fgets($imapConnection, 1024);
98 $count = 0;
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);;
106 $s = 0;
107 while ($s < count($flags)) {
108 $flags[$s] = substr($flags[$s], 1, strlen($flags[$s]));
109 $s++;
110 }
111 } else {
112 $flags[0] = "None";
113 }
114 $count++;
115 $read = fgets($imapConnection, 1024);
116 }
117 }
118
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
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 }
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);
186 echo ">>> $read<BR>";
187 while ((substr($read, 0, 15) != "mailboxStore OK") && (substr($read, 0, 15) != "mailboxStore NO")) {
188 $read = fgets($imapConnection, 1024);
189 echo ">>> $read<BR>";
190 }
191
192 if (substr($read, 0, 15) == "mailboxStore NO") {
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 }
201
202 /** expunges a mailbox **/
203 function expungeBox($imapConnection, $mailbox) {
204 selectMailbox($imapConnection, $mailbox, $num);
205 fputs($imapConnection, "1 EXPUNGE\n");
206 }
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 }
216
217 /** This function will fetch the body of a given message and format
218 it into our standard format. **/
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
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 **/
242 $count = 0;
243 $useHTML= false;
244 while ($count < count($read)) {
245 $read[$count] = "^^$read[$count]";
246
247 if (strpos(strtolower($read[$count]), "<html>") == true) {
248 $useHTML = true;
249 } else if (strpos(strtolower($read[$count]), "</html>") == true) {
250 $useHTML = false;
251 }
252
253 $read[$count] = substr($read[$count], 2, strlen($read[$count]));
254
255 if ($useHTML == false) {
256 $read[$count] = parsePlainBodyText($read[$count]);
257 } else {
258 $read[$count] = parseHTMLBodyText($read[$count]);
259 }
260
261 $count++;
262 }
263 return $read;
264 }
265
266 function parseHTMLBodyText($line) {
267 return $line;
268 }
269
270 function parsePlainBodyText($line) {
271 $line = "^^$line";
272
273 if ((strpos(strtolower($line), "<!") == false) &&
274 (strpos(strtolower($line), "<html>") == false) &&
275 (strpos(strtolower($line), "</html>") == false)) {
276 $line = str_replace("<", "&lt;", $line);
277 $line = str_replace(">", "&gt;", $line);
278 }
279
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);
283
284 $line = str_replace(" ", "&nbsp;", $line);
285 $line = str_replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", $line);
286 $line = str_replace("\n", "", $line);
287 $line = str_replace("\r", "", $line);
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("&nbsp;", "", $line)), "&gt;&gt;") == 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("&nbsp;", "", $line)), "&gt;") == 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) {
305 $line = ereg_replace("<BR>", "", $line);
306 $start = strpos(strtolower($line), "http://");
307 $link = substr($line, $start, strlen($line));
308
309 if (strpos($link, " ")) {
310 $end = strpos($link, " ")-1;
311 }
312 else if (strpos($link, "&nbsp;")) {
313 $end = strpos($link, "&nbsp;")-1;
314 }
315 else if (strpos($link, "<")) {
316 $end = strpos($link, "<");
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 }
333 else
334 $end = strlen($link);
335
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>";
342 }
343
344 return $line;
345 }
346
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
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("<", "&lt;", $read);
377 $read = ereg_replace(">", "&gt;", $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
394 ?>