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