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