710f6b11aa4426936ba360645fd675795db449f8
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];
178 foreach ($read as $read_part) {
179 if (eregi ("^to:(.*)$", $read_part, $regs)) {
181 } else if (eregi ("^from:(.*)$", $read_part, $regs)) {
183 } else if (eregi ("^x-priority:(.*)$", $read_part, $regs)) {
184 $priority = trim($regs[1]);
185 } else if (eregi ("^message-id:(.*)$", $read_part, $regs)) {
186 $messageid = trim($regs[1]);
187 } else if (eregi ("^cc:(.*)$", $read_part, $regs)) {
189 } else if (eregi ("^date:(.*)$", $read_part, $regs)) {
191 } else if (eregi ("^subject:(.*)$", $read_part, $regs)) {
192 $subject = htmlspecialchars(trim($regs[1]));
193 if ($subject == "") {
194 $subject = _("(no subject)");
196 } else if (eregi ("^content-type:(.*)$", $read_part, $regs)) {
197 $type = strtolower(trim($regs[1]));
198 if ($pos = strpos($type, ";")) {
199 $type = substr($type, 0, $pos);
201 $type = explode("/", $type);
202 if (!isset($type[1])) {
205 } else if (eregi ("^in-reply-to:(.*)$", $read_part, $regs)) {
206 $inrepto = trim($regs[1]);
209 $internaldate = getPref($data_dir, $username, 'internal_date_sort');
210 if (trim($date) == "" ||
$internaldate) {
211 fputs($imap_stream, "$sid FETCH $msg_list[$msgi] INTERNALDATE\r\n");
212 $readdate = sqimap_read_data($imap_stream, $sid, true, $response, $message);
213 if (eregi(".*INTERNALDATE \"(.*)\".*", $readdate[0], $regs)) {
214 $date_list = explode(' ', trim($regs[1]));
215 $date_list[0] = str_replace("-", ' ', $date_list[0]);
216 $date = implode(' ', $date_list);
219 eregi("([0-9]+)[^0-9]*$", $sizes_list[$msgi][0], $regs);
222 $header = new small_header
;
224 $header->from
= (trim($to) != '' ?
$to : '(' ._("No To Address") . ')');
226 $header->from
= $from;
229 $header->date
= $date;
230 $header->subject
= $subject;
232 $header->priority
= $priority;
233 $header->message_id
= $messageid;
235 $header->size
= $size;
236 $header->type0
= $type[0];
237 $header->type1
= $type[1];
238 $header->inrepto
= $inrepto;
244 /* Returns the flags for the specified messages */
245 function sqimap_get_flags ($imap_stream, $i) {
246 $read = sqimap_run_command ($imap_stream, "FETCH $i:$i FLAGS", true, $response, $message);
247 if (ereg('FLAGS(.*)', $read[0], $regs)) {
248 return explode(' ', trim(ereg_replace('[\\(\\)\\\\]', '', $regs[1])));
250 return array('None');
253 function sqimap_get_flags_list ($imap_stream, $msg_list) {
254 $msgs_str = sqimap_message_list_squisher($msg_list);
255 for ($i = 0; $i < sizeof($msg_list); $i++
) {
256 $id2index[$msg_list[$i]] = $i;
258 $result_list = sqimap_run_command_list ($imap_stream, "FETCH $msgs_str FLAGS", true, $response, $message);
259 $result_flags = array();
261 for ($i = 0; $i < sizeof($result_list); $i++
) {
262 if (eregi('^\* ([0-9]+).*FETCH.*FLAGS(.*)', $result_list[$i][0], $regs)
263 && isset($id2index[$regs[1]]) && count($id2index[$regs[1]])) {
264 $result_flags[$id2index[$regs[1]]] = explode(" ", trim(ereg_replace('[\\(\\)\\\\]', '', $regs[2])));
266 set_up_language($squirrelmail_language);
267 echo "<br><b><font color=$color[2]>\n" .
268 _("ERROR : Could not complete request.") .
270 _("Unknown response from IMAP server: ") .
271 $result_list[$i][0] . "</font><br>\n";
275 arsort($result_flags);
276 return $result_flags;
280 * Returns a message array with all the information about a message.
281 * See the documentation folder for more information about this array.
283 function sqimap_get_message ($imap_stream, $id, $mailbox) {
284 $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
285 return sqimap_get_message_body($imap_stream, $header);
288 /* Wrapper function that reformats the header information. */
289 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
290 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[HEADER]", true, $response, $message);
291 $header = sqimap_get_header($imap_stream, $read);
293 $header->mailbox
= $mailbox;
297 /* Wrapper function that reformats the entity header information. */
298 function sqimap_get_ent_header ($imap_stream, $id, $mailbox, $ent) {
299 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[$ent.HEADER]", true, $response, $message);
300 $header = sqimap_get_header($imap_stream, $read);
302 $header->mailbox
= $mailbox;
307 /* Wrapper function that returns entity headers for use by decodeMime */
309 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
310 $header = sqimap_get_header($imap_stream, $read);
311 $type0 = $header["TYPE0"];
312 $type1 = $header["TYPE1"];
313 $bound = $header["BOUNDARY"];
314 $encoding = $header["ENCODING"];
315 $charset = $header["CHARSET"];
316 $filename = $header["FILENAME"];
319 /* Queries the IMAP server and gets all header information. */
320 function sqimap_get_header ($imap_stream, $read) {
321 global $where, $what;
323 $hdr = new msg_header();
326 /* Set up some defaults */
327 $hdr->type0
= "text";
328 $hdr->type1
= "plain";
329 $hdr->charset
= "us-ascii";
331 while ($i < count($read)) {
332 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
337 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
338 $hdr->encoding
= strtolower(trim(substr($read[$i], 26)));
342 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
343 $cont = strtolower(trim(substr($read[$i], 13)));
344 if (strpos($cont, ";")) {
345 $cont = substr($cont, 0, strpos($cont, ";"));
348 if (strpos($cont, "/")) {
349 $hdr->type0
= substr($cont, 0, strpos($cont, "/"));
350 $hdr->type1
= substr($cont, strpos($cont, "/")+
1);
357 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
358 str_replace("\n", "", $line);
359 str_replace("\n", "", $read[$i]);
360 $line = "$line $read[$i]";
364 /* Detect the boundary of a multipart message */
365 if (eregi('boundary="([^"]+)"', $line, $regs)) {
366 $hdr->boundary
= $regs[1];
369 /* Detect the charset */
370 if (strpos(strtolower(trim($line)), "charset=")) {
371 $pos = strpos($line, "charset=") +
8;
372 $charset = trim($line);
373 if (strpos($line, ";", $pos) > 0) {
374 $charset = substr($charset, $pos, strpos($line, ";", $pos)-$pos);
376 $charset = substr($charset, $pos);
378 $charset = str_replace("\"", "", $charset);
379 $hdr->charset
= $charset;
381 $hdr->charset
= "us-ascii";
384 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
385 /* Add better content-disposition support */
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]";
395 /* Detects filename if any */
396 if (strpos(strtolower(trim($line)), "filename=")) {
397 $pos = strpos($line, "filename=") +
9;
399 if (strpos($line, " ", $pos) > 0) {
400 $name = substr($name, $pos, strpos($line, " ", $pos));
402 $name = substr($name, $pos);
404 $name = str_replace("\"", "", $name);
405 $hdr->filename
= $name;
409 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
410 $hdr->replyto
= trim(substr($read[$i], 9, strlen($read[$i])));
414 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
415 $hdr->from
= trim(substr($read[$i], 5, strlen($read[$i]) - 6));
416 if (! isset($hdr->replyto
) ||
$hdr->replyto
== "") {
417 $hdr->replyto
= $hdr->from
;
422 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
423 $d = substr($read[$i], 5);
425 $d = strtr($d, array(' ' => ' '));
426 $d = explode(' ', $d);
427 $hdr->date
= getTimeStamp($d);
431 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
432 $hdr->subject
= trim(substr($read[$i], 8, strlen($read[$i]) - 9));
433 if (strlen(Chop($hdr->subject
)) == 0) {
434 $hdr->subject
= _("(no subject)");
437 if ($where == 'SUBJECT') {
438 $hdr->subject = $what;
439 // $hdr->subject = eregi_replace($what, "<b>\\0</b>", $hdr->subject);
445 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
447 $hdr->cc
[$pos] = trim(substr($read[$i], 4));
449 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
451 $hdr->cc
[$pos] = trim($read[$i]);
456 else if (strtolower(substr($read[$i], 0, 4)) == "bcc:") {
458 $hdr->bcc
[$pos] = trim(substr($read[$i], 5));
460 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
462 $hdr->bcc
[$pos] = trim($read[$i]);
467 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
469 $hdr->to
[$pos] = trim(substr($read[$i], 4));
471 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
473 $hdr->to
[$pos] = trim($read[$i]);
478 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
479 $hdr->message_id
= trim(substr($read[$i], 11));
482 /* ERROR CORRECTION */
483 else if (substr($read[$i], 0, 1) == ")") {
484 if (strlen(trim($hdr->subject
)) == 0) {
485 $hdr->subject
= _("(no subject)");
487 if (strlen(trim($hdr->from
)) == 0) {
488 $hdr->from
= _("(unknown sender)");
490 if (strlen(trim($hdr->date
)) == 0) {
496 else if (strtolower(substr($read[$i], 0, 11)) == "x-priority:") {
497 $hdr->priority
= trim(substr($read[$i], 11));
507 /* Returns the body of a message. */
508 function sqimap_get_message_body ($imap_stream, &$header) {
510 return decodeMime($imap_stream, $header);