710f6b11aa4426936ba360645fd675795db449f8
[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 /* returns the references header lines */
79 function get_reference_header ($imap_stream, $message) {
80 $responses = array ();
81 $sid = sqimap_session_id();
82 $results = array();
83 $references = "";
84 $query = "$sid FETCH $message BODY.PEEK[HEADER.FIELDS (References)]\r\n";
85 fputs ($imap_stream, $query);
86 $responses = sqimap_read_data_list($imap_stream, $sid, true, $responses, $message);
87 if (!eregi("^\\* ([0-9]+) FETCH", $responses[0][0], $regs)) {
88 $responses = array ();
89 }
90 return $responses;
91 }
92
93 function sqimap_get_small_header_list ($imap_stream, $msg_list, $issent) {
94 global $squirrelmail_language, $color, $data_dir, $username;
95
96 /* Get the small headers for each message in $msg_list */
97 $sid = sqimap_session_id();
98 $maxmsg = sizeof($msg_list);
99 $msgs_str = sqimap_message_list_squisher($msg_list);
100 $results = array();
101 $read_list = array();
102 $sizes_list = array();
103
104 /*
105 * We need to return the data in the same order as the caller supplied
106 * in $msg_list, but IMAP servers are free to return responses in
107 * whatever order they wish... So we need to re-sort manually
108 */
109 for ($i = 0; $i < sizeof($msg_list); $i++) {
110 $id2index[$msg_list[$i]] = $i;
111 }
112
113 $query = "$sid FETCH $msgs_str BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject Message-Id X-Priority Content-Type In-Reply-To)]\r\n";
114 fputs ($imap_stream, $query);
115 $readin_list = sqimap_read_data_list($imap_stream, $sid, true, $response, $message);
116
117 foreach ($readin_list as $r) {
118 if (!eregi("^\\* ([0-9]+) FETCH", $r[0], $regs)) {
119 set_up_language($squirrelmail_language);
120 echo '<br><b><font color=$color[2]>' .
121 _("ERROR : Could not complete request.") .
122 '</b><br>' .
123 _("Unknown response from IMAP server: ") . ' 1.' .
124 $r[0] . "</font><br>\n";
125 } else if (! isset($id2index[$regs[1]]) || !count($id2index[$regs[1]])) {
126 set_up_language($squirrelmail_language);
127 echo '<br><b><font color=$color[2]>' .
128 _("ERROR : Could not complete request.") .
129 '</b><br>' .
130 _("Unknown message number in reply from server: ") .
131 $regs[1] . "</font><br>\n";
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 $inrepto = "";
176 $read = $read_list[$msgi];
177
178 foreach ($read as $read_part) {
179 if (eregi ("^to:(.*)$", $read_part, $regs)) {
180 $to = $regs[1];
181 } else if (eregi ("^from:(.*)$", $read_part, $regs)) {
182 $from = $regs[1];
183 } else if (eregi ("^x-priority:(.*)$", $read_part, $regs)) {
184 $priority = trim($regs[1]);
185 } else if (eregi ("^message-id:(.*)$", $read_part, $regs)) {
186 $messageid = trim($regs[1]);
187 } else if (eregi ("^cc:(.*)$", $read_part, $regs)) {
188 $cc = $regs[1];
189 } else if (eregi ("^date:(.*)$", $read_part, $regs)) {
190 $date = $regs[1];
191 } else if (eregi ("^subject:(.*)$", $read_part, $regs)) {
192 $subject = htmlspecialchars(trim($regs[1]));
193 if ($subject == "") {
194 $subject = _("(no subject)");
195 }
196 } else if (eregi ("^content-type:(.*)$", $read_part, $regs)) {
197 $type = strtolower(trim($regs[1]));
198 if ($pos = strpos($type, ";")) {
199 $type = substr($type, 0, $pos);
200 }
201 $type = explode("/", $type);
202 if (!isset($type[1])) {
203 $type[1] = '';
204 }
205 } else if (eregi ("^in-reply-to:(.*)$", $read_part, $regs)) {
206 $inrepto = trim($regs[1]);
207 }
208 }
209 $internaldate = getPref($data_dir, $username, 'internal_date_sort');
210 if (trim($date) == "" || $internaldate) {
211 fputs($imap_stream, "$sid FETCH $msg_list[$msgi] INTERNALDATE\r\n");
212 $readdate = sqimap_read_data($imap_stream, $sid, true, $response, $message);
213 if (eregi(".*INTERNALDATE \"(.*)\".*", $readdate[0], $regs)) {
214 $date_list = explode(' ', trim($regs[1]));
215 $date_list[0] = str_replace("-", ' ', $date_list[0]);
216 $date = implode(' ', $date_list);
217 }
218 }
219 eregi("([0-9]+)[^0-9]*$", $sizes_list[$msgi][0], $regs);
220 $size = $regs[1];
221
222 $header = new small_header;
223 if ($issent) {
224 $header->from = (trim($to) != '' ? $to : '(' ._("No To Address") . ')');
225 } else {
226 $header->from = $from;
227 }
228
229 $header->date = $date;
230 $header->subject = $subject;
231 $header->to = $to;
232 $header->priority = $priority;
233 $header->message_id = $messageid;
234 $header->cc = $cc;
235 $header->size = $size;
236 $header->type0 = $type[0];
237 $header->type1 = $type[1];
238 $header->inrepto = $inrepto;
239 $result[] = $header;
240 }
241 return $result;
242 }
243
244 /* Returns the flags for the specified messages */
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.
281 * See 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 /* Wrapper function that reformats the header information. */
289 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
290 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[HEADER]", true, $response, $message);
291 $header = sqimap_get_header($imap_stream, $read);
292 $header->id = $id;
293 $header->mailbox = $mailbox;
294 return $header;
295 }
296
297 /* Wrapper function that reformats the entity header information. */
298 function sqimap_get_ent_header ($imap_stream, $id, $mailbox, $ent) {
299 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[$ent.HEADER]", true, $response, $message);
300 $header = sqimap_get_header($imap_stream, $read);
301 $header->id = $id;
302 $header->mailbox = $mailbox;
303 return $header;
304 }
305
306
307 /* Wrapper function that returns entity headers for use by decodeMime */
308 /*
309 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
310 $header = sqimap_get_header($imap_stream, $read);
311 $type0 = $header["TYPE0"];
312 $type1 = $header["TYPE1"];
313 $bound = $header["BOUNDARY"];
314 $encoding = $header["ENCODING"];
315 $charset = $header["CHARSET"];
316 $filename = $header["FILENAME"];
317 }
318 */
319 /* Queries the IMAP server and gets all header information. */
320 function sqimap_get_header ($imap_stream, $read) {
321 global $where, $what;
322
323 $hdr = new msg_header();
324 $i = 0;
325
326 /* Set up some defaults */
327 $hdr->type0 = "text";
328 $hdr->type1 = "plain";
329 $hdr->charset = "us-ascii";
330
331 while ($i < count($read)) {
332 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
333 $hdr->mime = true;
334 $i++;
335 }
336 /* ENCODING TYPE */
337 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
338 $hdr->encoding = strtolower(trim(substr($read[$i], 26)));
339 $i++;
340 }
341 /* CONTENT-TYPE */
342 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
343 $cont = strtolower(trim(substr($read[$i], 13)));
344 if (strpos($cont, ";")) {
345 $cont = substr($cont, 0, strpos($cont, ";"));
346 }
347
348 if (strpos($cont, "/")) {
349 $hdr->type0 = substr($cont, 0, strpos($cont, "/"));
350 $hdr->type1 = substr($cont, strpos($cont, "/")+1);
351 } else {
352 $hdr->type0 = $cont;
353 }
354
355 $line = $read[$i];
356 $i++;
357 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
358 str_replace("\n", "", $line);
359 str_replace("\n", "", $read[$i]);
360 $line = "$line $read[$i]";
361 $i++;
362 }
363
364 /* Detect the boundary of a multipart message */
365 if (eregi('boundary="([^"]+)"', $line, $regs)) {
366 $hdr->boundary = $regs[1];
367 }
368
369 /* Detect the charset */
370 if (strpos(strtolower(trim($line)), "charset=")) {
371 $pos = strpos($line, "charset=") + 8;
372 $charset = trim($line);
373 if (strpos($line, ";", $pos) > 0) {
374 $charset = substr($charset, $pos, strpos($line, ";", $pos)-$pos);
375 } else {
376 $charset = substr($charset, $pos);
377 }
378 $charset = str_replace("\"", "", $charset);
379 $hdr->charset = $charset;
380 } else {
381 $hdr->charset = "us-ascii";
382 }
383 }
384 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
385 /* Add better content-disposition support */
386 $line = $read[$i];
387 $i++;
388 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
389 str_replace("\n", "", $line);
390 str_replace("\n", "", $read[$i]);
391 $line = "$line $read[$i]";
392 $i++;
393 }
394
395 /* Detects filename if any */
396 if (strpos(strtolower(trim($line)), "filename=")) {
397 $pos = strpos($line, "filename=") + 9;
398 $name = trim($line);
399 if (strpos($line, " ", $pos) > 0) {
400 $name = substr($name, $pos, strpos($line, " ", $pos));
401 } else {
402 $name = substr($name, $pos);
403 }
404 $name = str_replace("\"", "", $name);
405 $hdr->filename = $name;
406 }
407 }
408 /* REPLY-TO */
409 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
410 $hdr->replyto = trim(substr($read[$i], 9, strlen($read[$i])));
411 $i++;
412 }
413 /* FROM */
414 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
415 $hdr->from = trim(substr($read[$i], 5, strlen($read[$i]) - 6));
416 if (! isset($hdr->replyto) || $hdr->replyto == "") {
417 $hdr->replyto = $hdr->from;
418 }
419 $i++;
420 }
421 /* DATE */
422 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
423 $d = substr($read[$i], 5);
424 $d = trim($d);
425 $d = strtr($d, array(' ' => ' '));
426 $d = explode(' ', $d);
427 $hdr->date = getTimeStamp($d);
428 $i++;
429 }
430 /* SUBJECT */
431 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
432 $hdr->subject = trim(substr($read[$i], 8, strlen($read[$i]) - 9));
433 if (strlen(Chop($hdr->subject)) == 0) {
434 $hdr->subject = _("(no subject)");
435 }
436 /*
437 if ($where == 'SUBJECT') {
438 $hdr->subject = $what;
439 // $hdr->subject = eregi_replace($what, "<b>\\0</b>", $hdr->subject);
440 }
441 */
442 $i++;
443 }
444 /* CC */
445 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
446 $pos = 0;
447 $hdr->cc[$pos] = trim(substr($read[$i], 4));
448 $i++;
449 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
450 $pos++;
451 $hdr->cc[$pos] = trim($read[$i]);
452 $i++;
453 }
454 }
455 /* BCC */
456 else if (strtolower(substr($read[$i], 0, 4)) == "bcc:") {
457 $pos = 0;
458 $hdr->bcc[$pos] = trim(substr($read[$i], 5));
459 $i++;
460 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
461 $pos++;
462 $hdr->bcc[$pos] = trim($read[$i]);
463 $i++;
464 }
465 }
466 /* TO */
467 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
468 $pos = 0;
469 $hdr->to[$pos] = trim(substr($read[$i], 4));
470 $i++;
471 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
472 $pos++;
473 $hdr->to[$pos] = trim($read[$i]);
474 $i++;
475 }
476 }
477 /* MESSAGE ID */
478 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
479 $hdr->message_id = trim(substr($read[$i], 11));
480 $i++;
481 }
482 /* ERROR CORRECTION */
483 else if (substr($read[$i], 0, 1) == ")") {
484 if (strlen(trim($hdr->subject)) == 0) {
485 $hdr->subject = _("(no subject)");
486 }
487 if (strlen(trim($hdr->from)) == 0) {
488 $hdr->from = _("(unknown sender)");
489 }
490 if (strlen(trim($hdr->date)) == 0) {
491 $hdr->date = time();
492 }
493 $i++;
494 }
495 /* X-PRIORITY */
496 else if (strtolower(substr($read[$i], 0, 11)) == "x-priority:") {
497 $hdr->priority = trim(substr($read[$i], 11));
498 $i++;
499 }
500 else {
501 $i++;
502 }
503 }
504 return $hdr;
505 }
506
507 /* Returns the body of a message. */
508 function sqimap_get_message_body ($imap_stream, &$header) {
509 $id = $header->id;
510 return decodeMime($imap_stream, $header);
511 }
512
513 ?>