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