Little bit of tidying 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/* Copies specified messages to specified folder */
15function 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 */
20function 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 */
30function 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 */
35function 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 */
40class small_header {
41 var $from = '', $subject = '', $date = '', $to = '',
42 $priority = 0, $message_id = 0, $cc = '';
43}
44
45function 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 */
54function 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 */
79function 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
93function 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 $prevline = false;
179 foreach ($read as $read_part) {
180 //unfold multi-line headers
181 while ($prevline && strspn($read_part, "\t ") > 0) {
182 $read_part = substr($prevline, 0, -2) . ' ' . ltrim($read_part);
183 }
184 $prevline = $read_part;
185 if (eregi ("^to:(.*)$", $read_part, $regs)) {
186 $to = $regs[1];
187 } else if (eregi ("^from:(.*)$", $read_part, $regs)) {
188 $from = $regs[1];
189 } else if (eregi ("^x-priority:(.*)$", $read_part, $regs)) {
190 $priority = trim($regs[1]);
191 } else if (eregi ("^message-id:(.*)$", $read_part, $regs)) {
192 $messageid = trim($regs[1]);
193 } else if (eregi ("^cc:(.*)$", $read_part, $regs)) {
194 $cc = $regs[1];
195 } else if (eregi ("^date:(.*)$", $read_part, $regs)) {
196 $date = $regs[1];
197 } else if (eregi ("^subject:(.*)$", $read_part, $regs)) {
198 $subject = htmlspecialchars(trim($regs[1]));
199 if ($subject == "") {
200 $subject = _("(no subject)");
201 }
202 } else if (eregi ("^content-type:(.*)$", $read_part, $regs)) {
203 $type = strtolower(trim($regs[1]));
204 if ($pos = strpos($type, ";")) {
205 $type = substr($type, 0, $pos);
206 }
207 $type = explode("/", $type);
208 if (!isset($type[1])) {
209 $type[1] = '';
210 }
211 } else if (eregi ("^in-reply-to:(.*)$", $read_part, $regs)) {
212 $inrepto = trim($regs[1]);
213 }
214 }
215 $internaldate = getPref($data_dir, $username, 'internal_date_sort');
216 if (trim($date) == "" || $internaldate) {
217 fputs($imap_stream, "$sid FETCH $msg_list[$msgi] INTERNALDATE\r\n");
218 $readdate = sqimap_read_data($imap_stream, $sid, true, $response, $message);
219 if (eregi(".*INTERNALDATE \"(.*)\".*", $readdate[0], $regs)) {
220 $date_list = explode(' ', trim($regs[1]));
221 $date_list[0] = str_replace("-", ' ', $date_list[0]);
222 $date = implode(' ', $date_list);
223 }
224 }
225 eregi("([0-9]+)[^0-9]*$", $sizes_list[$msgi][0], $regs);
226 $size = $regs[1];
227
228 $header = new small_header;
229 if ($issent) {
230 $header->from = (trim($to) != '' ? $to : '(' ._("No To Address") . ')');
231 } else {
232 $header->from = $from;
233 }
234
235 $header->date = $date;
236 $header->subject = $subject;
237 $header->to = $to;
238 $header->priority = $priority;
239 $header->message_id = $messageid;
240 $header->cc = $cc;
241 $header->size = $size;
242 $header->type0 = $type[0];
243 $header->type1 = $type[1];
244 $header->inrepto = $inrepto;
245 $result[] = $header;
246 }
247 return $result;
248}
249
250/* Returns the flags for the specified messages */
251function sqimap_get_flags ($imap_stream, $i) {
252 $read = sqimap_run_command ($imap_stream, "FETCH $i:$i FLAGS", true, $response, $message);
253 if (ereg('FLAGS(.*)', $read[0], $regs)) {
254 return explode(' ', trim(ereg_replace('[\\(\\)\\\\]', '', $regs[1])));
255 }
256 return array('None');
257}
258
259function sqimap_get_flags_list ($imap_stream, $msg_list) {
260 $msgs_str = sqimap_message_list_squisher($msg_list);
261 for ($i = 0; $i < sizeof($msg_list); $i++) {
262 $id2index[$msg_list[$i]] = $i;
263 }
264 $result_list = sqimap_run_command_list ($imap_stream, "FETCH $msgs_str FLAGS", true, $response, $message);
265 $result_flags = array();
266
267 for ($i = 0; $i < sizeof($result_list); $i++) {
268 if (eregi('^\* ([0-9]+).*FETCH.*FLAGS(.*)', $result_list[$i][0], $regs)
269 && isset($id2index[$regs[1]]) && count($id2index[$regs[1]])) {
270 $result_flags[$id2index[$regs[1]]] = explode(" ", trim(ereg_replace('[\\(\\)\\\\]', '', $regs[2])));
271 } else {
272 set_up_language($squirrelmail_language);
273 echo "<br><b><font color=$color[2]>\n" .
274 _("ERROR : Could not complete request.") .
275 "</b><br>\n" .
276 _("Unknown response from IMAP server: ") .
277 $result_list[$i][0] . "</font><br>\n";
278 exit;
279 }
280 }
281 arsort($result_flags);
282 return $result_flags;
283}
284
285/*
286 * Returns a message array with all the information about a message.
287 * See the documentation folder for more information about this array.
288 */
289function sqimap_get_message ($imap_stream, $id, $mailbox) {
290 $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
291 return sqimap_get_message_body($imap_stream, $header);
292}
293
294/* Wrapper function that reformats the header information. */
295function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
296 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[HEADER]", true, $response, $message);
297 $header = sqimap_get_header($imap_stream, $read);
298 $header->id = $id;
299 $header->mailbox = $mailbox;
300 return $header;
301}
302
303/* Wrapper function that reformats the entity header information. */
304function 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/* Wrapper function that returns entity headers for use by decodeMime */
314/*
315function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
316 $header = sqimap_get_header($imap_stream, $read);
317 $type0 = $header["TYPE0"];
318 $type1 = $header["TYPE1"];
319 $bound = $header["BOUNDARY"];
320 $encoding = $header["ENCODING"];
321 $charset = $header["CHARSET"];
322 $filename = $header["FILENAME"];
323}
324*/
325/* Queries the IMAP server and gets all header information. */
326function sqimap_get_header ($imap_stream, $read) {
327 global $where, $what;
328
329 $hdr = new msg_header();
330 $i = 0;
331
332 /* Set up some defaults */
333 $hdr->type0 = "text";
334 $hdr->type1 = "plain";
335 $hdr->charset = "us-ascii";
336
337 while ($i < count($read)) {
338 //unfold multi-line headers
339 while ($i + 1 < count($read) && strspn($read[$i + 1], "\t ") > 0) {
340 $read[$i + 1] = substr($read[$i], 0, -2) . ' ' . ltrim($read[$i + 1]);
341 array_splice($read, $i, 1);
342 }
343
344 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
345 $hdr->mime = true;
346 $i++;
347 }
348 /* ENCODING TYPE */
349 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
350 $hdr->encoding = strtolower(trim(substr($read[$i], 26)));
351 $i++;
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 if (strpos($cont, "/")) {
361 $hdr->type0 = substr($cont, 0, strpos($cont, "/"));
362 $hdr->type1 = substr($cont, strpos($cont, "/")+1);
363 } else {
364 $hdr->type0 = $cont;
365 }
366
367 $line = $read[$i];
368 $i++;
369 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
370 str_replace("\n", "", $line);
371 str_replace("\n", "", $read[$i]);
372 $line = "$line $read[$i]";
373 $i++;
374 }
375
376 /* Detect the boundary of a multipart message */
377 if (eregi('boundary="([^"]+)"', $line, $regs)) {
378 $hdr->boundary = $regs[1];
379 }
380
381 /* Detect the charset */
382 if (strpos(strtolower(trim($line)), "charset=")) {
383 $pos = strpos($line, "charset=") + 8;
384 $charset = trim($line);
385 if (strpos($line, ";", $pos) > 0) {
386 $charset = substr($charset, $pos, strpos($line, ";", $pos)-$pos);
387 } else {
388 $charset = substr($charset, $pos);
389 }
390 $charset = str_replace("\"", "", $charset);
391 $hdr->charset = $charset;
392 } else {
393 $hdr->charset = "us-ascii";
394 }
395 }
396 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
397 /* Add better content-disposition support */
398 $line = $read[$i];
399 $i++;
400 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
401 str_replace("\n", "", $line);
402 str_replace("\n", "", $read[$i]);
403 $line = "$line $read[$i]";
404 $i++;
405 }
406
407 /* Detects filename if any */
408 if (strpos(strtolower(trim($line)), "filename=")) {
409 $pos = strpos($line, "filename=") + 9;
410 $name = trim($line);
411 if (strpos($line, " ", $pos) > 0) {
412 $name = substr($name, $pos, strpos($line, " ", $pos));
413 } else {
414 $name = substr($name, $pos);
415 }
416 $name = str_replace("\"", "", $name);
417 $hdr->filename = $name;
418 }
419 }
420 /* REPLY-TO */
421 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
422 $hdr->replyto = trim(substr($read[$i], 9, strlen($read[$i])));
423 $i++;
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 if ($where == 'SUBJECT') {
450 $hdr->subject = $what;
451 // $hdr->subject = eregi_replace($what, "<b>\\0</b>", $hdr->subject);
452 }
453 */
454 $i++;
455 }
456 /* CC */
457 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
458 $pos = 0;
459 $hdr->cc[$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->cc[$pos] = trim($read[$i]);
464 $i++;
465 }
466 }
467 /* BCC */
468 else if (strtolower(substr($read[$i], 0, 4)) == "bcc:") {
469 $pos = 0;
470 $hdr->bcc[$pos] = trim(substr($read[$i], 5));
471 $i++;
472 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
473 $pos++;
474 $hdr->bcc[$pos] = trim($read[$i]);
475 $i++;
476 }
477 }
478 /* TO */
479 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
480 $pos = 0;
481 $hdr->to[$pos] = trim(substr($read[$i], 4));
482 $i++;
483 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
484 $pos++;
485 $hdr->to[$pos] = trim($read[$i]);
486 $i++;
487 }
488 }
489 /* MESSAGE ID */
490 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
491 $hdr->message_id = trim(substr($read[$i], 11));
492 $i++;
493 }
494 /* ERROR CORRECTION */
495 else if (substr($read[$i], 0, 1) == ")") {
496 if (strlen(trim($hdr->subject)) == 0) {
497 $hdr->subject = _("(no subject)");
498 }
499 if (strlen(trim($hdr->from)) == 0) {
500 $hdr->from = _("(unknown sender)");
501 }
502 if (strlen(trim($hdr->date)) == 0) {
503 $hdr->date = time();
504 }
505 $i++;
506 }
507 /* X-PRIORITY */
508 else if (strtolower(substr($read[$i], 0, 11)) == "x-priority:") {
509 $hdr->priority = trim(substr($read[$i], 11));
510 $i++;
511 }
512 else {
513 $i++;
514 }
515 }
516 return $hdr;
517}
518
519/* Returns the body of a message. */
520function sqimap_get_message_body ($imap_stream, &$header) {
521 $id = $header->id;
522 return decodeMime($imap_stream, $header);
523}
524
525?>