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