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