55932ca4cfbf470cd1b34a19cf664f3794a2d610
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This implements functions that manipulate messages
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);
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;
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);
26 sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
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);
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);
39 /* Returns some general header information -- FROM, DATE, and SUBJECT */
41 var $from = '', $subject = '', $date = '', $to = '',
42 $priority = 0, $message_id = 0, $cc = '';
45 function sqimap_get_small_header ($imap_stream, $id, $sent) {
46 $res = sqimap_get_small_header_list($imap_stream, array($id), $sent);
51 * Sort the message list and crunch to be as small as possible
52 * (overflow could happen, so make it small if possible)
54 function sqimap_message_list_squisher($messages_array) {
55 if( !is_array( $messages_array ) ) {
58 sort($messages_array, SORT_NUMERIC
);
60 while ($messages_array) {
61 $start = array_shift($messages_array);
63 while (isset($messages_array[0]) && $messages_array[0] == $end +
1) {
64 $end = array_shift($messages_array);
66 if ($msgs_str != '') {
71 $msgs_str .= ':' . $end;
78 /* returns the references header lines */
79 function get_reference_header ($imap_stream, $message) {
80 $responses = array ();
81 $sid = sqimap_session_id();
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 ();
93 function sqimap_get_small_header_list ($imap_stream, $msg_list, $issent) {
94 global $squirrelmail_language, $color, $data_dir, $username;
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);
101 $read_list = array();
102 $sizes_list = array();
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
109 for ($i = 0; $i < sizeof($msg_list); $i++
) {
110 $id2index[$msg_list[$i]] = $i;
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);
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.") .
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.") .
130 _("Unknown message number in reply from server: ") .
131 $regs[1] . "</font><br>\n";
133 $read_list[$id2index[$regs[1]]] = $r;
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);
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.");
148 echo _("Unknown response from IMAP server: ") . ' 2.';
149 echo $r[0] . "</font><br>\n";
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.");
157 echo _("Unknown messagenumber in reply from server: ");
158 echo $regs[1] . "</font><br>\n";
161 $sizes_list[$id2index[$regs[1]]] = $r;
165 for ($msgi = 0; $msgi < $maxmsg; $msgi++
) {
166 $subject = _("(no subject)");
167 $from = _("Unknown Sender");
176 $read = $read_list[$msgi];
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);
184 $prevline = $read_part;
185 if (eregi ("^to:(.*)$", $read_part, $regs)) {
187 } else if (eregi ("^from:(.*)$", $read_part, $regs)) {
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)) {
195 } else if (eregi ("^date:(.*)$", $read_part, $regs)) {
197 } else if (eregi ("^subject:(.*)$", $read_part, $regs)) {
198 $subject = htmlspecialchars(trim($regs[1]));
199 if ($subject == "") {
200 $subject = _("(no subject)");
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);
207 $type = explode("/", $type);
208 if (!isset($type[1])) {
211 } else if (eregi ("^in-reply-to:(.*)$", $read_part, $regs)) {
212 $inrepto = trim($regs[1]);
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);
225 eregi("([0-9]+)[^0-9]*$", $sizes_list[$msgi][0], $regs);
228 $header = new small_header
;
230 $header->from
= (trim($to) != '' ?
$to : '(' ._("No To Address") . ')');
232 $header->from
= $from;
235 $header->date
= $date;
236 $header->subject
= $subject;
238 $header->priority
= $priority;
239 $header->message_id
= $messageid;
241 $header->size
= $size;
242 $header->type0
= $type[0];
243 $header->type1
= $type[1];
244 $header->inrepto
= $inrepto;
250 /* Returns the flags for the specified messages */
251 function 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])));
256 return array('None');
259 function 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;
264 $result_list = sqimap_run_command_list ($imap_stream, "FETCH $msgs_str FLAGS", true, $response, $message);
265 $result_flags = array();
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])));
272 set_up_language($squirrelmail_language);
273 echo "<br><b><font color=$color[2]>\n" .
274 _("ERROR : Could not complete request.") .
276 _("Unknown response from IMAP server: ") .
277 $result_list[$i][0] . "</font><br>\n";
281 arsort($result_flags);
282 return $result_flags;
286 * Returns a message array with all the information about a message.
287 * See the documentation folder for more information about this array.
289 function 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);
294 /* Wrapper function that reformats the header information. */
295 function 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);
299 $header->mailbox
= $mailbox;
303 /* Wrapper function that reformats the entity header information. */
304 function 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);
308 $header->mailbox
= $mailbox;
313 /* Wrapper function that returns entity headers for use by decodeMime */
315 function 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"];
325 /* Queries the IMAP server and gets all header information. */
326 function sqimap_get_header ($imap_stream, $read) {
327 global $where, $what;
329 $hdr = new msg_header();
332 /* Set up some defaults */
333 $hdr->type0
= "text";
334 $hdr->type1
= "plain";
335 $hdr->charset
= "us-ascii";
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);
344 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
349 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
350 $hdr->encoding
= strtolower(trim(substr($read[$i], 26)));
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, ";"));
360 if (strpos($cont, "/")) {
361 $hdr->type0
= substr($cont, 0, strpos($cont, "/"));
362 $hdr->type1
= substr($cont, strpos($cont, "/")+
1);
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]";
376 /* Detect the boundary of a multipart message */
377 if (eregi('boundary="([^"]+)"', $line, $regs)) {
378 $hdr->boundary
= $regs[1];
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);
388 $charset = substr($charset, $pos);
390 $charset = str_replace("\"", "", $charset);
391 $hdr->charset
= $charset;
393 $hdr->charset
= "us-ascii";
396 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
397 /* Add better content-disposition support */
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]";
407 /* Detects filename if any */
408 if (strpos(strtolower(trim($line)), "filename=")) {
409 $pos = strpos($line, "filename=") +
9;
411 if (strpos($line, " ", $pos) > 0) {
412 $name = substr($name, $pos, strpos($line, " ", $pos));
414 $name = substr($name, $pos);
416 $name = str_replace("\"", "", $name);
417 $hdr->filename
= $name;
421 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
422 $hdr->replyto
= trim(substr($read[$i], 9, strlen($read[$i])));
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
;
434 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
435 $d = substr($read[$i], 5);
437 $d = strtr($d, array(' ' => ' '));
438 $d = explode(' ', $d);
439 $hdr->date
= getTimeStamp($d);
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)");
449 if ($where == 'SUBJECT') {
450 $hdr->subject = $what;
451 // $hdr->subject = eregi_replace($what, "<b>\\0</b>", $hdr->subject);
457 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
459 $hdr->cc
[$pos] = trim(substr($read[$i], 4));
461 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
463 $hdr->cc
[$pos] = trim($read[$i]);
468 else if (strtolower(substr($read[$i], 0, 4)) == "bcc:") {
470 $hdr->bcc
[$pos] = trim(substr($read[$i], 5));
472 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
474 $hdr->bcc
[$pos] = trim($read[$i]);
479 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
481 $hdr->to
[$pos] = trim(substr($read[$i], 4));
483 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
485 $hdr->to
[$pos] = trim($read[$i]);
490 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
491 $hdr->message_id
= trim(substr($read[$i], 11));
494 /* ERROR CORRECTION */
495 else if (substr($read[$i], 0, 1) == ")") {
496 if (strlen(trim($hdr->subject
)) == 0) {
497 $hdr->subject
= _("(no subject)");
499 if (strlen(trim($hdr->from
)) == 0) {
500 $hdr->from
= _("(unknown sender)");
502 if (strlen(trim($hdr->date
)) == 0) {
508 else if (strtolower(substr($read[$i], 0, 11)) == "x-priority:") {
509 $hdr->priority
= trim(substr($read[$i], 11));
519 /* Returns the body of a message. */
520 function sqimap_get_message_body ($imap_stream, &$header) {
522 return decodeMime($imap_stream, $header);