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