added user option to support message sort by internal date instead of the
[squirrelmail.git] / functions / imap_messages.php
1 <?php
2
3 /**
4 * imap_messages.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This implements functions that manipulate messages
10 *
11 * $Id$
12 */
13
14 /****************************************************************************
15 ** Copies specified messages to specified folder
16 ****************************************************************************/
17 function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) {
18 $read = sqimap_run_command ($imap_stream, "COPY $start:$end \"$mailbox\"", true, $response, $message);
19 }
20
21 /****************************************************************************
22 ** Deletes specified messages and moves them to trash if possible
23 ****************************************************************************/
24 function sqimap_messages_delete ($imap_stream, $start, $end, $mailbox) {
25 global $move_to_trash, $trash_folder, $auto_expunge;
26
27 if (($move_to_trash == true) && (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder))) {
28 sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder);
29 sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
30 } else {
31 sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
32 }
33 }
34
35 /****************************************************************************
36 ** Sets the specified messages with specified flag
37 ****************************************************************************/
38 function sqimap_messages_flag ($imap_stream, $start, $end, $flag) {
39 $read = sqimap_run_command ($imap_stream, "STORE $start:$end +FLAGS (\\$flag)", true, $response, $message);
40 }
41
42
43 /****************************************************************************
44 ** Remove specified flag from specified messages
45 ****************************************************************************/
46 function sqimap_messages_remove_flag ($imap_stream, $start, $end, $flag) {
47 $read = sqimap_run_command ($imap_stream, "STORE $start:$end -FLAGS (\\$flag)", true, $response, $message);
48 }
49
50
51 /****************************************************************************
52 ** Returns some general header information -- FROM, DATE, and SUBJECT
53 ****************************************************************************/
54 class small_header {
55 var $from = '', $subject = '', $date = '', $to = '',
56 $priority = 0, $message_id = 0, $cc = '';
57 }
58
59 function sqimap_get_small_header ($imap_stream, $id, $sent) {
60 $res = sqimap_get_small_header_list($imap_stream, array($id), $sent);
61 return $res[0];
62 }
63
64 /* Sort the message list and crunch to be as small as possible
65 * (overflow could happen, so make it small if possible)
66 */
67 function sqimap_message_list_squisher($messages_array) {
68 if( !is_array( $messages_array ) ) {
69 return;
70 }
71 sort($messages_array, SORT_NUMERIC);
72 $msgs_str = '';
73 while ($messages_array) {
74 $start = array_shift($messages_array);
75 $end = $start;
76 while (isset($messages_array[0]) && $messages_array[0] == $end + 1) {
77 $end = array_shift($messages_array);
78 }
79 if ($msgs_str != '') {
80 $msgs_str .= ',';
81 }
82 $msgs_str .= $start;
83 if ($start != $end) {
84 $msgs_str .= ':' . $end;
85 }
86 }
87
88 return $msgs_str;
89 }
90
91 function sqimap_get_small_header_list ($imap_stream, $msg_list, $issent) {
92 global $squirrelmail_language, $color;
93
94 /* Get the small headers for each message in $msg_list */
95 $sid = sqimap_session_id();
96 $maxmsg = sizeof($msg_list);
97 $msgs_str = sqimap_message_list_squisher($msg_list);
98 $results = array();
99 $read_list = array();
100 $sizes_list = array();
101
102 /**
103 * We need to return the data in the same order as the caller supplied
104 * in $msg_list, but IMAP servers are free to return responses in
105 * whatever order they wish... So we need to re-sort manually
106 */
107 for ($i = 0; $i < sizeof($msg_list); $i++) {
108 $id2index[$msg_list[$i]] = $i;
109 }
110
111 $query = "$sid FETCH $msgs_str BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject Message-Id X-Priority Content-Type)]\r\n";
112 fputs ($imap_stream, $query);
113 $readin_list = sqimap_read_data_list($imap_stream, $sid, true, $response, $message);
114
115 foreach ($readin_list as $r) {
116 if (!eregi("^\\* ([0-9]+) FETCH", $r[0], $regs)) {
117 set_up_language($squirrelmail_language);
118 echo '<br><b><font color=$color[2]>' .
119 _("ERROR : Could not complete request.") .
120 '</b><br>' .
121 _("Unknown response from IMAP server: ") . ' 1.' .
122 $r[0] . "</font><br>\n";
123 /* exit; */
124 } else if (! isset($id2index[$regs[1]]) || !count($id2index[$regs[1]])) {
125 set_up_language($squirrelmail_language);
126 echo '<br><b><font color=$color[2]>' .
127 _("ERROR : Could not complete request.") .
128 '</b><br>' .
129 _("Unknown message number in reply from server: ") .
130 $regs[1] . "</font><br>\n";
131 /* exit */
132 } else {
133 $read_list[$id2index[$regs[1]]] = $r;
134 }
135 }
136 arsort($read_list);
137
138 $query = "$sid FETCH $msgs_str RFC822.SIZE\r\n";
139 fputs ($imap_stream, $query);
140 $sizesin_list = sqimap_read_data_list($imap_stream, $sid, true, $response, $message);
141
142 foreach ($sizesin_list as $r) {
143 if (!eregi("^\\* ([0-9]+) FETCH", $r[0], $regs)) {
144 set_up_language($squirrelmail_language);
145 echo "<br><b><font color=$color[2]>\n";
146 echo _("ERROR : Could not complete request.");
147 echo "</b><br>\n";
148 echo _("Unknown response from IMAP server: ") . ' 2.';
149 echo $r[0] . "</font><br>\n";
150 exit;
151 }
152 if (!count($id2index[$regs[1]])) {
153 set_up_language($squirrelmail_language);
154 echo "<br><b><font color=$color[2]>\n";
155 echo _("ERROR : Could not complete request.");
156 echo "</b><br>\n";
157 echo _("Unknown messagenumber in reply from server: ");
158 echo $regs[1] . "</font><br>\n";
159 exit;
160 }
161 $sizes_list[$id2index[$regs[1]]] = $r;
162 }
163 arsort($sizes_list);
164
165 for ($msgi = 0; $msgi < $maxmsg; $msgi++) {
166 $subject = _("(no subject)");
167 $from = _("Unknown Sender");
168 $priority = 0;
169 $messageid = "<>";
170 $cc = "";
171 $to = "";
172 $date = "";
173 $type[0] = "";
174 $type[1] = "";
175 $read = $read_list[$msgi];
176
177 for ($i = 0; $i < count($read); $i++) {
178 if (eregi ("^to:(.*)$", $read[$i], $regs)) {
179 /*$to = sqimap_find_displayable_name(substr($read[$i], 3));*/
180 $to = $regs[1];
181 } else if (eregi ("^from:(.*)$", $read[$i], $regs)) {
182 /*$from = sqimap_find_displayable_name(substr($read[$i], 5));*/
183 $from = $regs[1];
184 } else if (eregi ("^x-priority:(.*)$", $read[$i], $regs)) {
185 $priority = trim($regs[1]);
186 } else if (eregi ("^message-id:(.*)$", $read[$i], $regs)) {
187 $messageid = trim($regs[1]);
188 } else if (eregi ("^cc:(.*)$", $read[$i], $regs)) {
189 $cc = $regs[1];
190 } else if (eregi ("^date:(.*)$", $read[$i], $regs)) {
191 $date = $regs[1];
192 } else if (eregi ("^subject:(.*)$", $read[$i], $regs)) {
193 $subject = htmlspecialchars(trim($regs[1]));
194 if ($subject == "") {
195 $subject = _("(no subject)");
196 }
197 } else if (eregi ("^content-type:(.*)$", $read[$i], $regs)) {
198 $type = strtolower(trim($regs[1]));
199 if ($pos = strpos($type, ";")) {
200 $type = substr($type, 0, $pos);
201 }
202 $type = explode("/", $type);
203 if (! isset($type[1])) {
204 $type[1] = '';
205 }
206 }
207 }
208 if (trim($date) == "") {
209 fputs($imap_stream, "$sid FETCH $msg_list[$msgi] INTERNALDATE\r\n");
210 $readdate = sqimap_read_data($imap_stream, $sid, true, $response, $message);
211 if (eregi(".*INTERNALDATE \"(.*)\".*", $readdate[0], $regs)) {
212 $date_list = explode(" ", trim($regs[1]));
213 $date_list[0] = str_replace("-", " ", $date_list[0]);
214 $date = implode(" ", $date_list);
215 }
216 }
217 eregi("([0-9]+)[^0-9]*$", $sizes_list[$msgi][0], $regs);
218 $size = $regs[1];
219
220 $header = new small_header;
221 if ($issent == true) {
222 $header->from = (trim($to) != '' ? $to : '(' ._("No To Address") . ')');
223 } else {
224 $header->from = $from;
225 }
226
227 $header->date = $date;
228 $header->subject = $subject;
229 $header->to = $to;
230 $header->priority = $priority;
231 $header->message_id = $messageid;
232 $header->cc = $cc;
233 $header->size = $size;
234 $header->type0 = $type[0];
235 $header->type1 = $type[1];
236
237 $result[] = $header;
238 }
239 return $result;
240 }
241
242 /****************************************************************************
243 ** Returns the flags for the specified messages
244 ****************************************************************************/
245 function sqimap_get_flags ($imap_stream, $i) {
246 $read = sqimap_run_command ($imap_stream, "FETCH $i:$i FLAGS", true, $response, $message);
247 if (ereg("FLAGS(.*)", $read[0], $regs)) {
248 return explode(' ', trim(ereg_replace('[\\(\\)\\\\]', '', $regs[1])));
249 }
250 return Array('None');
251 }
252
253 function sqimap_get_flags_list ($imap_stream, $msg_list) {
254 $msgs_str = sqimap_message_list_squisher($msg_list);
255 for ($i = 0; $i < sizeof($msg_list); $i++) {
256 $id2index[$msg_list[$i]] = $i;
257 }
258 $result_list = sqimap_run_command_list ($imap_stream, "FETCH $msgs_str FLAGS", true, $response, $message);
259 $result_flags = array();
260
261 for ($i = 0; $i < sizeof($result_list); $i++) {
262 if (eregi("^\\* ([0-9]+).*FETCH.*FLAGS(.*)", $result_list[$i][0], $regs)
263 && isset($id2index[$regs[1]]) && count($id2index[$regs[1]])) {
264 $result_flags[$id2index[$regs[1]]] = explode(" ", trim(ereg_replace('[\\(\\)\\\\]', '', $regs[2])));
265 } else {
266 set_up_language($squirrelmail_language);
267 echo "<br><b><font color=$color[2]>\n" .
268 _("ERROR : Could not complete request.") .
269 "</b><br>\n" .
270 _("Unknown response from IMAP server: ") .
271 $result_list[$i][0] . "</font><br>\n";
272 exit;
273 }
274 }
275 arsort($result_flags);
276 return $result_flags;
277 }
278
279 /****************************************************************************
280 ** Returns a message array with all the information about a message. See
281 ** the documentation folder for more information about this array.
282 ****************************************************************************/
283 function sqimap_get_message ($imap_stream, $id, $mailbox) {
284 $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
285 return sqimap_get_message_body($imap_stream, $header);
286 }
287
288 /****************************************************************************
289 ** Wrapper function that reformats the header information.
290 ****************************************************************************/
291 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
292 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[HEADER]", true, $response, $message);
293
294 $header = sqimap_get_header($imap_stream, $read);
295 $header->id = $id;
296 $header->mailbox = $mailbox;
297
298 return $header;
299 }
300
301 /****************************************************************************
302 ** Wrapper function that reformats the entity header information.
303 ****************************************************************************/
304 function sqimap_get_ent_header ($imap_stream, $id, $mailbox, $ent) {
305 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[$ent.HEADER]", true, $response, $message);
306 $header = sqimap_get_header($imap_stream, $read);
307 $header->id = $id;
308 $header->mailbox = $mailbox;
309 return $header;
310 }
311
312
313 /****************************************************************************
314 ** Wrapper function that returns entity headers for use by decodeMime
315 ****************************************************************************/
316 /*
317 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
318 $header = sqimap_get_header($imap_stream, $read);
319 $type0 = $header["TYPE0"];
320 $type1 = $header["TYPE1"];
321 $bound = $header["BOUNDARY"];
322 $encoding = $header["ENCODING"];
323 $charset = $header["CHARSET"];
324 $filename = $header["FILENAME"];
325 }
326 */
327
328 /****************************************************************************
329 ** Queries the IMAP server and gets all header information.
330 ****************************************************************************/
331 function sqimap_get_header ($imap_stream, $read) {
332 global $where, $what;
333
334 $hdr = new msg_header();
335 $i = 0;
336 /* Set up some defaults */
337 $hdr->type0 = "text";
338 $hdr->type1 = "plain";
339 $hdr->charset = "us-ascii";
340
341 while ($i < count($read)) {
342 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
343 $hdr->mime = true;
344 $i++;
345 }
346
347 /** ENCODING TYPE **/
348 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
349 $hdr->encoding = strtolower(trim(substr($read[$i], 26)));
350 $i++;
351 }
352
353 /** CONTENT-TYPE **/
354 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
355 $cont = strtolower(trim(substr($read[$i], 13)));
356 if (strpos($cont, ";")) {
357 $cont = substr($cont, 0, strpos($cont, ";"));
358 }
359
360
361 if (strpos($cont, "/")) {
362 $hdr->type0 = substr($cont, 0, strpos($cont, "/"));
363 $hdr->type1 = substr($cont, strpos($cont, "/")+1);
364 } else {
365 $hdr->type0 = $cont;
366 }
367
368
369 $line = $read[$i];
370 $i++;
371 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
372 str_replace("\n", "", $line);
373 str_replace("\n", "", $read[$i]);
374 $line = "$line $read[$i]";
375 $i++;
376 }
377
378 /** Detect the boundary of a multipart message **/
379 if (eregi('boundary="([^"]+)"', $line, $regs)) {
380 $hdr->boundary = $regs[1];
381 }
382
383 /** Detect the charset **/
384 if (strpos(strtolower(trim($line)), "charset=")) {
385 $pos = strpos($line, "charset=") + 8;
386 $charset = trim($line);
387 if (strpos($line, ";", $pos) > 0) {
388 $charset = substr($charset, $pos, strpos($line, ";", $pos)-$pos);
389 } else {
390 $charset = substr($charset, $pos);
391 }
392 $charset = str_replace("\"", "", $charset);
393 $hdr->charset = $charset;
394 } else {
395 $hdr->charset = "us-ascii";
396 }
397 }
398
399 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
400 /** Add better dontent-disposition support **/
401
402 $line = $read[$i];
403 $i++;
404 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
405 str_replace("\n", "", $line);
406 str_replace("\n", "", $read[$i]);
407 $line = "$line $read[$i]";
408 $i++;
409 }
410
411 /** Detects filename if any **/
412 if (strpos(strtolower(trim($line)), "filename=")) {
413 $pos = strpos($line, "filename=") + 9;
414 $name = trim($line);
415 if (strpos($line, " ", $pos) > 0) {
416 $name = substr($name, $pos, strpos($line, " ", $pos));
417 } else {
418 $name = substr($name, $pos);
419 }
420 $name = str_replace("\"", "", $name);
421 $hdr->filename = $name;
422 }
423 }
424
425 /** REPLY-TO **/
426 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
427 $hdr->replyto = trim(substr($read[$i], 9, strlen($read[$i])));
428 $i++;
429 }
430
431 /** FROM **/
432 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
433 $hdr->from = trim(substr($read[$i], 5, strlen($read[$i]) - 6));
434 if (! isset($hdr->replyto) || $hdr->replyto == "") {
435 $hdr->replyto = $hdr->from;
436 }
437 $i++;
438 }
439 /** DATE **/
440 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
441 $d = substr($read[$i], 5);
442 $d = trim($d);
443 $d = strtr($d, array(' ' => ' '));
444 $d = explode(' ', $d);
445 $hdr->date = getTimeStamp($d);
446 $i++;
447 }
448 /** SUBJECT **/
449 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
450 $hdr->subject = trim(substr($read[$i], 8, strlen($read[$i]) - 9));
451 if (strlen(Chop($hdr->subject)) == 0) {
452 $hdr->subject = _("(no subject)");
453 }
454
455 /*
456 if ($where == 'SUBJECT') {
457 $hdr->subject = $what;
458 // $hdr->subject = eregi_replace($what, "<b>\\0</b>", $hdr->subject);
459 }
460 */
461
462 $i++;
463 }
464 /** CC **/
465 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
466 $pos = 0;
467 $hdr->cc[$pos] = trim(substr($read[$i], 4));
468 $i++;
469 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
470 $pos++;
471 $hdr->cc[$pos] = trim($read[$i]);
472 $i++;
473 }
474 }
475 /** BCC **/
476 else if (strtolower(substr($read[$i], 0, 4)) == "bcc:") {
477 $pos = 0;
478 $hdr->bcc[$pos] = trim(substr($read[$i], 5));
479 $i++;
480 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
481 $pos++;
482 $hdr->bcc[$pos] = trim($read[$i]);
483 $i++;
484 }
485 }
486 /** TO **/
487 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
488 $pos = 0;
489 $hdr->to[$pos] = trim(substr($read[$i], 4));
490 $i++;
491 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
492 $pos++;
493 $hdr->to[$pos] = trim($read[$i]);
494 $i++;
495 }
496 }
497 /** MESSAGE ID **/
498 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
499 $hdr->message_id = trim(substr($read[$i], 11));
500 $i++;
501 }
502
503
504 /** ERROR CORRECTION **/
505 else if (substr($read[$i], 0, 1) == ")") {
506 if (strlen(trim($hdr->subject)) == 0) {
507 $hdr->subject = _("(no subject)");
508 }
509 if (strlen(trim($hdr->from)) == 0) {
510 $hdr->from = _("(unknown sender)");
511 }
512 if (strlen(trim($hdr->date)) == 0) {
513 $hdr->date = time();
514 }
515 $i++;
516 }
517 /** X-PRIORITY **/
518 else if (strtolower(substr($read[$i], 0, 11)) == "x-priority:") {
519 $hdr->priority = trim(substr($read[$i], 11));
520 $i++;
521 }
522 else {
523 $i++;
524 }
525 }
526 return $hdr;
527 }
528
529
530 /****************************************************************************
531 ** Returns the body of a message.
532 ****************************************************************************/
533 function sqimap_get_message_body ($imap_stream, &$header) {
534 $id = $header->id;
535 return decodeMime($imap_stream, $header);
536 }
537
538
539 /****************************************************************************
540 ** Returns an array with the body structure
541 ****************************************************************************/
542 ?>