increased performance of reading mailboxes 20-30% by doing 2 things:
[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");
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)) {
5823fa43 52 echo _("Error in message header fetching. Start message: "). $start, _("End message: "). "$end<BR>";
3e054c43 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("<", "&lt;", $read);
74 $read = ereg_replace(">", "&gt;", $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("<", "&lt;", $read);
79 $read = ereg_replace(">", "&gt;", $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 **/
5e90d34a 117 function getMessageFlags($imapConnection, $low, $high, &$flags) {
3302d0d4 118 /** * 2 FETCH (FLAGS (\Answered \Seen)) */
5e90d34a 119 fputs($imapConnection, "messageFetch FETCH $low:$high 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);
5e90d34a 126 $read = str_replace("\\", "", $read);
3302d0d4 127 $read = substr($read, strpos($read, "FLAGS")+6, strlen($read));
128 $read = trim($read);
5e90d34a 129 $flags[$count] = explode(" ", $read);;
3302d0d4 130 } else {
5e90d34a 131 $flags[$count][0] = "None";
3302d0d4 132 }
3e054c43 133 $count++;
3302d0d4 134 $read = fgets($imapConnection, 1024);
135 }
136 }
137
31f3d7c0 138 function decodeEmailAddr($sender) {
139 $emailAddr = getEmailAddr($sender);
40884065 140 if (strpos($emailAddr, "EMAILSTART--")) {
31f3d7c0 141
40884065 142 $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
143 $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
144 } else {
145 $emailAddr = $emailAddr;
146 }
31f3d7c0 147 return $emailAddr;
148 }
149
3302d0d4 150 function getEmailAddr($sender) {
151 if (strpos($sender, "EMAILSTART--") == false)
40884065 152 return "$sender";
3302d0d4 153
40884065 154 $emailStart = strpos($sender, "EMAILSTART--") + 12;
155 $emailAddr = substr($sender, $emailStart, strlen($sender));
156 $emailAddr = substr($emailAddr, 0, strpos($emailAddr, "--EMAILEND"));
3302d0d4 157
158 return $emailAddr;
159 }
160
161 function getSender($sender) {
162 if (strpos($sender, "EMAILSTART--") == false)
40884065 163 return "$sender";
3302d0d4 164
165 $first = substr($sender, 0, strpos($sender, "EMAILSTART--"));
166 $second = substr($sender, strpos($sender, "--EMAILEND") +10, strlen($sender));
40884065 167 return "$first $second";
3302d0d4 168 }
169
170 function getSenderName($sender) {
171 $name = getSender($sender);
172 $emailAddr = getEmailAddr($sender);
173 $emailStart = strpos($emailAddr, "EMAILSTART--");
174 $emailEnd = strpos($emailAddr, "--EMAILEND") - 10;
175
176 if (($emailAddr == "") && ($name == "")) {
177 $from = $sender;
178 }
179 else if ((strstr($name, "?") != false) || (strstr($name, "$") != false) || (strstr($name, "%") != false)){
180 $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
181 $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
182 $from = $emailAddr;
183 }
184 else if (strlen($name) > 0) {
185 $from = $name;
186 }
187 else if (strlen($emailAddr > 0)) {
188 $emailAddr = ereg_replace("EMAILSTART--", "", $emailAddr);
189 $emailAddr = ereg_replace("--EMAILEND", "", $emailAddr);
190 $from = $emailAddr;
191 }
192
193 $from = trim($from);
194
195 // strip out any quotes if they exist
196 if ((strlen($from) > 0) && ($from[0] == "\"") && ($from[strlen($from) - 1] == "\""))
197 $from = substr($from, 1, strlen($from) - 2);
198
199 return $from;
200 }
aa4c3749 201
202 /** returns "true" if the copy was completed successfully.
203 ** returns "false" with an error message if unsuccessful.
204 **/
205 function copyMessages($imapConnection, $from_id, $to_id, $folder) {
206 fputs($imapConnection, "mailboxStore COPY $from_id:$to_id \"$folder\"\n");
207 $read = fgets($imapConnection, 1024);
208 while ((substr($read, 0, 15) != "mailboxStore OK") && (substr($read, 0, 15) != "mailboxStore NO")) {
209 $read = fgets($imapConnection, 1024);
210 }
211
212 if (substr($read, 0, 15) == "mailboxStore NO") {
aa4c3749 213 return false;
214 } else if (substr($read, 0, 15) == "mailboxStore OK") {
215 return true;
216 }
217
218 echo "UNKNOWN ERROR copying messages $from_id to $to_id to folder $folder.<BR>";
219 return false;
220 }
61a4ac35 221
222 /** expunges a mailbox **/
223 function expungeBox($imapConnection, $mailbox) {
224 selectMailbox($imapConnection, $mailbox, $num);
225 fputs($imapConnection, "1 EXPUNGE\n");
5e3fe357 226 imapReadData($imapConnection, "1", true, $response, $message);
61a4ac35 227 }
8c7dfc99 228
d92b6f31 229 function getFolderNameMinusINBOX($mailbox, $del) {
230 $inbox = "INBOX" . $del;
231 if (substr($mailbox, 0, strlen($inbox)) == $inbox)
232 $box = substr($mailbox, strlen($inbox), strlen($mailbox));
8c7dfc99 233 else
234 $box = $mailbox;
235
236 return $box;
237 }
05207a68 238
aceb0d5c 239 /** This function gets all the information about a message. Including Header and body **/
97be2168 240 function fetchMessage($imapConnection, $id, $mailbox) {
241 $message["INFO"]["ID"] = $id;
242 $message["INFO"]["MAILBOX"] = $mailbox;
aceb0d5c 243 $message["HEADER"] = fetchHeader($imapConnection, $id);
d4467150 244 $message["ENTITIES"] = fetchBody($imapConnection, $message["HEADER"]["BOUNDARY"], $id, $message["HEADER"]["TYPE0"], $message["HEADER"]["TYPE1"]);
aceb0d5c 245 return $message;
246 }
247
248 function fetchHeader($imapConnection, $id) {
5c55c295 249 fputs($imapConnection, "messageFetch FETCH $id:$id RFC822.HEADER\n");
aceb0d5c 250 $read = fgets($imapConnection, 1024);
251
252 /** defaults... if the don't get overwritten, it will display text **/
d4467150 253 $header["TYPE0"] = "text";
254 $header["TYPE1"] = "plain";
255 $header["ENCODING"] = "us-ascii";
aceb0d5c 256 while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
257 /** MIME-VERSION **/
258 if (substr($read, 0, 17) == "MIME-Version: 1.0") {
259 $header["MIME"] = true;
260 $read = fgets($imapConnection, 1024);
261 }
d4467150 262
263 /** ENCODING TYPE **/
7831268e 264 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
d4467150 265 $header["ENCODING"] = strtolower(trim(substr($read[$i], 26)));
266 }
267
aceb0d5c 268 /** CONTENT-TYPE **/
269 else if (substr($read, 0, 13) == "Content-Type:") {
d4467150 270 $cont = strtolower(trim(substr($read, 13)));
271 if (strpos($cont, ";"))
272 $cont = substr($cont, 0, strpos($cont, ";"));
8405ee35 273
274 if (strpos($cont, "/")) {
275 $header["TYPE0"] = substr($cont, 0, strpos($cont, "/"));
276 $header["TYPE1"] = substr($cont, strpos($cont, "/")+1);
277 } else {
278 $header["TYPE0"] = $cont;
279 }
aceb0d5c 280
d4467150 281 $line = $read;
aceb0d5c 282 $read = fgets($imapConnection, 1024);
d4467150 283 while ( (substr(substr($read, 0, strpos($read, " ")), -1) != ":") && (trim($read) != "") && (trim($read) != ")")) {
284 str_replace("\n", "", $line);
285 str_replace("\n", "", $read);
286 $line = "$line $read";
287 $read = fgets($imapConnection, 1024);
288 }
289
290 /** Detect the boundary of a multipart message **/
291 if (strpos(strtolower(trim($line)), "boundary=")) {
292 $pos = strpos($line, "boundary=") + 9;
293 $bound = trim($line);
294 if (strpos($line, " ", $pos) > 0) {
295 $bound = substr($bound, $pos, strpos($line, " ", $pos));
296 } else {
297 $bound = substr($bound, $pos);
298 }
aceb0d5c 299 $bound = str_replace("\"", "", $bound);
300 $header["BOUNDARY"] = $bound;
aceb0d5c 301 }
d4467150 302
303 /** Detect the charset **/
304 if (strpos(strtolower(trim($line)), "charset=")) {
305 $pos = strpos($line, "charset=") + 8;
306 $charset = trim($line);
307 if (strpos($line, " ", $pos) > 0) {
308 $charset = substr($charset, $pos, strpos($line, " ", $pos));
309 } else {
310 $charset = substr($charset, $pos);
311 }
312 $charset = str_replace("\"", "", $charset);
313 $header["CHARSET"] = $charset;
314 } else {
315 $header["CHARSET"] = "us-ascii";
316 }
317
7c9499e1 318 /** Detects filename if any **/
319 if (strpos(strtolower(trim($line)), "name=")) {
320 $pos = strpos($line, "name=") + 5;
321 $name = trim($line);
322 if (strpos($line, " ", $pos) > 0) {
323 $name = substr($name, $pos, strpos($line, " ", $pos));
324 } else {
325 $name = substr($name, $pos);
326 }
327 $name = str_replace("\"", "", $name);
328 $header["FILENAME"] = $name;
329 }
aceb0d5c 330 }
5c55c295 331
332 /** REPLY-TO **/
8405ee35 333 else if (strtolower(substr($read, 0, 9)) == "reply-to:") {
5c55c295 334 $header["REPLYTO"] = trim(substr($read, 9, strlen($read)));
335 $read = fgets($imapConnection, 1024);
336 }
337
aceb0d5c 338 /** FROM **/
8405ee35 339 else if (strtolower(substr($read, 0, 5)) == "from:") {
aceb0d5c 340 $header["FROM"] = trim(substr($read, 5, strlen($read) - 6));
5c55c295 341 if ($header["REPLYTO"] == "")
342 $header["REPLYTO"] = $header["FROM"];
aceb0d5c 343 $read = fgets($imapConnection, 1024);
344 }
345 /** DATE **/
8405ee35 346 else if (strtolower(substr($read, 0, 5)) == "date:") {
aceb0d5c 347 $d = substr($read, 5, strlen($read) - 6);
348 $d = trim($d);
349 $d = ereg_replace(" ", " ", $d);
350 $d = explode(" ", $d);
351 $header["DATE"] = getTimeStamp($d);
352 $read = fgets($imapConnection, 1024);
353 }
354 /** SUBJECT **/
8405ee35 355 else if (strtolower(substr($read, 0, 8)) == "subject:") {
aceb0d5c 356 $header["SUBJECT"] = trim(substr($read, 8, strlen($read) - 9));
357 if (strlen(Chop($header["SUBJECT"])) == 0)
358 $header["SUBJECT"] = "(no subject)";
359 $read = fgets($imapConnection, 1024);
360 }
361 /** CC **/
8405ee35 362 else if (strtolower(substr($read, 0, 3)) == "cc:") {
aceb0d5c 363 $pos = 0;
364 $header["CC"][$pos] = trim(substr($read, 4));
365 $read = fgets($imapConnection, 1024);
366 while ((substr($read, 0, 1) == " ") && (trim($read) != "")) {
367 $pos++;
368 $header["CC"][$pos] = trim($read);
369 $read = fgets($imapConnection, 1024);
370 }
371 }
372 /** TO **/
8405ee35 373 else if (strtolower(substr($read, 0, 3)) == "to:") {
aceb0d5c 374 $pos = 0;
375 $header["TO"][$pos] = trim(substr($read, 4));
376 $read = fgets($imapConnection, 1024);
377 while ((substr($read, 0, 1) == " ") && (trim($read) != "")){
378 $pos++;
379 $header["TO"][$pos] = trim($read);
380 $read = fgets($imapConnection, 1024);
381 }
382 }
383
384 /** ERROR CORRECTION **/
385 else if (substr($read, 0, 1) == ")") {
386 if ($header["SUBJECT"] == "")
387 $header["SUBJECT"] = "(no subject)";
388
389 if ($header["FROM"] == "")
390 $header["FROM"] = "(unknown sender)";
391
392 if ($header["DATE"] == "")
393 $header["DATE"] = time();
394 $read = fgets($imapConnection, 1024);
395 }
396 else {
397 $read = fgets($imapConnection, 1024);
398 }
399 }
400 return $header;
401 }
402
403 function fetchBody($imapConnection, $bound, $id, $type0, $type1) {
404 /** This first part reads in the full body of the message **/
05207a68 405 fputs($imapConnection, "messageFetch FETCH $id:$id BODY[TEXT]\n");
aceb0d5c 406 $read = fgets($imapConnection, 1024);
407
05207a68 408 $count = 0;
aceb0d5c 409 while ((substr($read, 0, 15) != "messageFetch OK") && (substr($read, 0, 16) != "messageFetch BAD")) {
410 $body[$count] = $read;
05207a68 411 $count++;
aceb0d5c 412
413 $read = fgets($imapConnection, 1024);
05207a68 414 }
415
aceb0d5c 416 /** this deletes the first line, and the last two (imap stuff we ignore) **/
e550d551 417 $i = 0;
418 $j = 0;
aceb0d5c 419 while ($i < count($body)) {
420 if ( ($i != 0) && ($i != count($body) - 1) && ($i != count($body)) ) {
421 $bodytmp[$j] = $body[$i];
e550d551 422 $j++;
423 }
424 $i++;
425 }
aceb0d5c 426 $body = $bodytmp;
e550d551 427
aceb0d5c 428 /** Now, lets work out the MIME stuff **/
429 /** (needs mime.php included) **/
430 return decodeMime($body, $bound, $type0, $type1);
431 }
05207a68 432
7c9499e1 433 function fetchEntityHeader($imapConnection, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
aceb0d5c 434 /** defaults... if the don't get overwritten, it will display text **/
435 $type0 = "text";
436 $type1 = "plain";
d4467150 437 $encoding = "us-ascii";
aceb0d5c 438 $i = 0;
439 while (trim($read[$i]) != "") {
7831268e 440 if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
d4467150 441 $encoding = strtolower(trim(substr($read[$i], 26)));
442
443 } else if (substr($read[$i], 0, 13) == "Content-Type:") {
444 $cont = strtolower(trim(substr($read[$i], 13)));
445 if (strpos($cont, ";"))
446 $cont = substr($cont, 0, strpos($cont, ";"));
8405ee35 447
448 if (strpos($cont, "/")) {
449 $type0 = substr($cont, 0, strpos($cont, "/"));
450 $type1 = substr($cont, strpos($cont, "/")+1);
451 } else {
452 $type0 = $cont;
453 }
aceb0d5c 454
7c9499e1 455 $read[$i] = trim($read[$i]);
d4467150 456 $line = $read[$i];
7c9499e1 457 $i++;
d4467150 458 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
459 str_replace("\n", "", $line);
460 str_replace("\n", "", $read[$i]);
461 $line = "$line $read[$i]";
462 $i++;
7c9499e1 463 $read[$i] = trim($read[$i]);
d4467150 464 }
7831268e 465 $i--;
d4467150 466
467 /** Detect the boundary of a multipart message **/
468 if (strpos(strtolower(trim($line)), "boundary=")) {
469 $pos = strpos($line, "boundary=") + 9;
470 $bound = trim($line);
471 if (strpos($line, " ", $pos) > 0) {
472 $bound = substr($bound, $pos, strpos($line, " ", $pos));
473 } else {
474 $bound = substr($bound, $pos);
475 }
aceb0d5c 476 $bound = str_replace("\"", "", $bound);
477 }
d4467150 478
479 /** Detect the charset **/
480 if (strpos(strtolower(trim($line)), "charset=")) {
481 $pos = strpos($line, "charset=") + 8;
482 $charset = trim($line);
483 if (strpos($line, " ", $pos) > 0) {
484 $charset = substr($charset, $pos, strpos($line, " ", $pos));
485 } else {
486 $charset = substr($charset, $pos);
487 }
488 $charset = str_replace("\"", "", $charset);
489 }
7c9499e1 490
491 /** Detects filename if any **/
492 if (strpos(strtolower(trim($line)), "name=")) {
493 $pos = strpos($line, "name=") + 5;
494 $name = trim($line);
495 if (strpos($line, " ", $pos) > 0) {
496 $name = substr($name, $pos, strpos($line, " ", $pos));
497 } else {
498 $name = substr($name, $pos);
499 }
500 $name = str_replace("\"", "", $name);
501 $filename = $name;
502 }
e550d551 503 }
aceb0d5c 504 $i++;
505 }
05207a68 506
aceb0d5c 507 /** remove the header from the entity **/
508 $i = 0;
509 while (trim($read[$i]) != "") {
510 $i++;
e550d551 511 }
aceb0d5c 512 $i++;
05207a68 513
aceb0d5c 514 for ($p = 0; $i < count($read); $p++) {
515 $entity[$p] = $read[$i];
516 $i++;
517 }
518
519 $read = $entity;
e550d551 520 }
05207a68 521
a09387f4 522?>