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