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