a411f6829d679e15257cb96f46e73d0ad748996a
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 /****************************************************************************
15 ** Copies specified messages to specified folder
16 ****************************************************************************/
17 function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) {
18 $read = sqimap_run_command ($imap_stream, "COPY $start:$end \"$mailbox\"", true, $response, $message);
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;
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");
31 sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
35 /****************************************************************************
36 ** Sets the specified messages with specified flag
37 ****************************************************************************/
38 function sqimap_messages_flag ($imap_stream, $start, $end, $flag) {
39 $read = sqimap_run_command ($imap_stream, "STORE $start:$end +FLAGS (\\$flag)", true, $response, $message);
43 /****************************************************************************
44 ** Remove specified flag from specified messages
45 ****************************************************************************/
46 function sqimap_messages_remove_flag ($imap_stream, $start, $end, $flag) {
47 $read = sqimap_run_command ($imap_stream, "STORE $start:$end -FLAGS (\\$flag)", true, $response, $message);
51 /****************************************************************************
52 ** Returns some general header information -- FROM, DATE, and SUBJECT
53 ****************************************************************************/
55 var $from = '', $subject = '', $date = '', $to = '',
56 $priority = 0, $message_id = 0, $cc = '';
59 function sqimap_get_small_header ($imap_stream, $id, $sent) {
60 $res = sqimap_get_small_header_list($imap_stream, array($id), $sent);
64 /* Sort the message list and crunch to be as small as possible
65 * (overflow could happen, so make it small if possible)
67 function sqimap_message_list_squisher($messages_array) {
68 if( !is_array( $messages_array ) ) {
71 sort($messages_array, SORT_NUMERIC
);
73 while ($messages_array) {
74 $start = array_shift($messages_array);
76 while (isset($messages_array[0]) && $messages_array[0] == $end +
1) {
77 $end = array_shift($messages_array);
79 if ($msgs_str != '') {
84 $msgs_str .= ':' . $end;
91 function sqimap_get_small_header_list ($imap_stream, $msg_list, $issent) {
92 global $squirrelmail_language, $color, $data_dir, $username;
94 /* Get the small headers for each message in $msg_list */
95 $sid = sqimap_session_id();
96 $maxmsg = sizeof($msg_list);
97 $msgs_str = sqimap_message_list_squisher($msg_list);
100 $sizes_list = array();
103 * We need to return the data in the same order as the caller supplied
104 * in $msg_list, but IMAP servers are free to return responses in
105 * whatever order they wish... So we need to re-sort manually
107 for ($i = 0; $i < sizeof($msg_list); $i++
) {
108 $id2index[$msg_list[$i]] = $i;
111 $query = "$sid FETCH $msgs_str BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject Message-Id X-Priority Content-Type)]\r\n";
112 fputs ($imap_stream, $query);
113 $readin_list = sqimap_read_data_list($imap_stream, $sid, true, $response, $message);
115 foreach ($readin_list as $r) {
116 if (!eregi("^\\* ([0-9]+) FETCH", $r[0], $regs)) {
117 set_up_language($squirrelmail_language);
118 echo '<br><b><font color=$color[2]>' .
119 _("ERROR : Could not complete request.") .
121 _("Unknown response from IMAP server: ") . ' 1.' .
122 $r[0] . "</font><br>\n";
124 } else if (! isset($id2index[$regs[1]]) ||
!count($id2index[$regs[1]])) {
125 set_up_language($squirrelmail_language);
126 echo '<br><b><font color=$color[2]>' .
127 _("ERROR : Could not complete request.") .
129 _("Unknown message number in reply from server: ") .
130 $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");
175 $read = $read_list[$msgi];
177 for ($i = 0; $i < count($read); $i++
) {
178 if (eregi ("^to:(.*)$", $read[$i], $regs)) {
179 /*$to = sqimap_find_displayable_name(substr($read[$i], 3));*/
181 } else if (eregi ("^from:(.*)$", $read[$i], $regs)) {
182 /*$from = sqimap_find_displayable_name(substr($read[$i], 5));*/
184 } else if (eregi ("^x-priority:(.*)$", $read[$i], $regs)) {
185 $priority = trim($regs[1]);
186 } else if (eregi ("^message-id:(.*)$", $read[$i], $regs)) {
187 $messageid = trim($regs[1]);
188 } else if (eregi ("^cc:(.*)$", $read[$i], $regs)) {
190 } else if (eregi ("^date:(.*)$", $read[$i], $regs)) {
192 } else if (eregi ("^subject:(.*)$", $read[$i], $regs)) {
193 $subject = htmlspecialchars(trim($regs[1]));
194 if ($subject == "") {
195 $subject = _("(no subject)");
197 } else if (eregi ("^content-type:(.*)$", $read[$i], $regs)) {
198 $type = strtolower(trim($regs[1]));
199 if ($pos = strpos($type, ";")) {
200 $type = substr($type, 0, $pos);
202 $type = explode("/", $type);
203 if (! isset($type[1])) {
208 $internaldate = getPref($data_dir, $username, 'internal_date_sort');
209 if (trim($date) == "" ||
$internaldate) {
210 fputs($imap_stream, "$sid FETCH $msg_list[$msgi] INTERNALDATE\r\n");
211 $readdate = sqimap_read_data($imap_stream, $sid, true, $response, $message);
212 if (eregi(".*INTERNALDATE \"(.*)\".*", $readdate[0], $regs)) {
213 $date_list = explode(" ", trim($regs[1]));
214 $date_list[0] = str_replace("-", " ", $date_list[0]);
215 $date = implode(" ", $date_list);
218 eregi("([0-9]+)[^0-9]*$", $sizes_list[$msgi][0], $regs);
221 $header = new small_header
;
222 if ($issent == true) {
223 $header->from
= (trim($to) != '' ?
$to : '(' ._("No To Address") . ')');
225 $header->from
= $from;
228 $header->date
= $date;
229 $header->subject
= $subject;
231 $header->priority
= $priority;
232 $header->message_id
= $messageid;
234 $header->size
= $size;
235 $header->type0
= $type[0];
236 $header->type1
= $type[1];
243 /****************************************************************************
244 ** Returns the flags for the specified messages
245 ****************************************************************************/
246 function sqimap_get_flags ($imap_stream, $i) {
247 $read = sqimap_run_command ($imap_stream, "FETCH $i:$i FLAGS", true, $response, $message);
248 if (ereg("FLAGS(.*)", $read[0], $regs)) {
249 return explode(' ', trim(ereg_replace('[\\(\\)\\\\]', '', $regs[1])));
251 return Array('None');
254 function sqimap_get_flags_list ($imap_stream, $msg_list) {
255 $msgs_str = sqimap_message_list_squisher($msg_list);
256 for ($i = 0; $i < sizeof($msg_list); $i++
) {
257 $id2index[$msg_list[$i]] = $i;
259 $result_list = sqimap_run_command_list ($imap_stream, "FETCH $msgs_str FLAGS", true, $response, $message);
260 $result_flags = array();
262 for ($i = 0; $i < sizeof($result_list); $i++
) {
263 if (eregi("^\\* ([0-9]+).*FETCH.*FLAGS(.*)", $result_list[$i][0], $regs)
264 && isset($id2index[$regs[1]]) && count($id2index[$regs[1]])) {
265 $result_flags[$id2index[$regs[1]]] = explode(" ", trim(ereg_replace('[\\(\\)\\\\]', '', $regs[2])));
267 set_up_language($squirrelmail_language);
268 echo "<br><b><font color=$color[2]>\n" .
269 _("ERROR : Could not complete request.") .
271 _("Unknown response from IMAP server: ") .
272 $result_list[$i][0] . "</font><br>\n";
276 arsort($result_flags);
277 return $result_flags;
280 /****************************************************************************
281 ** Returns a message array with all the information about a message. See
282 ** the documentation folder for more information about this array.
283 ****************************************************************************/
284 function sqimap_get_message ($imap_stream, $id, $mailbox) {
285 $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
286 return sqimap_get_message_body($imap_stream, $header);
289 /****************************************************************************
290 ** Wrapper function that reformats the header information.
291 ****************************************************************************/
292 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
293 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[HEADER]", true, $response, $message);
295 $header = sqimap_get_header($imap_stream, $read);
297 $header->mailbox
= $mailbox;
302 /****************************************************************************
303 ** Wrapper function that reformats the entity header information.
304 ****************************************************************************/
305 function sqimap_get_ent_header ($imap_stream, $id, $mailbox, $ent) {
306 $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[$ent.HEADER]", true, $response, $message);
307 $header = sqimap_get_header($imap_stream, $read);
309 $header->mailbox
= $mailbox;
314 /****************************************************************************
315 ** Wrapper function that returns entity headers for use by decodeMime
316 ****************************************************************************/
318 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
319 $header = sqimap_get_header($imap_stream, $read);
320 $type0 = $header["TYPE0"];
321 $type1 = $header["TYPE1"];
322 $bound = $header["BOUNDARY"];
323 $encoding = $header["ENCODING"];
324 $charset = $header["CHARSET"];
325 $filename = $header["FILENAME"];
329 /****************************************************************************
330 ** Queries the IMAP server and gets all header information.
331 ****************************************************************************/
332 function sqimap_get_header ($imap_stream, $read) {
333 global $where, $what;
335 $hdr = new msg_header();
337 /* Set up some defaults */
338 $hdr->type0
= "text";
339 $hdr->type1
= "plain";
340 $hdr->charset
= "us-ascii";
342 while ($i < count($read)) {
343 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
348 /** ENCODING TYPE **/
349 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
350 $hdr->encoding
= strtolower(trim(substr($read[$i], 26)));
355 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
356 $cont = strtolower(trim(substr($read[$i], 13)));
357 if (strpos($cont, ";")) {
358 $cont = substr($cont, 0, strpos($cont, ";"));
362 if (strpos($cont, "/")) {
363 $hdr->type0
= substr($cont, 0, strpos($cont, "/"));
364 $hdr->type1
= substr($cont, strpos($cont, "/")+
1);
372 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
373 str_replace("\n", "", $line);
374 str_replace("\n", "", $read[$i]);
375 $line = "$line $read[$i]";
379 /** Detect the boundary of a multipart message **/
380 if (eregi('boundary="([^"]+)"', $line, $regs)) {
381 $hdr->boundary
= $regs[1];
384 /** Detect the charset **/
385 if (strpos(strtolower(trim($line)), "charset=")) {
386 $pos = strpos($line, "charset=") +
8;
387 $charset = trim($line);
388 if (strpos($line, ";", $pos) > 0) {
389 $charset = substr($charset, $pos, strpos($line, ";", $pos)-$pos);
391 $charset = substr($charset, $pos);
393 $charset = str_replace("\"", "", $charset);
394 $hdr->charset
= $charset;
396 $hdr->charset
= "us-ascii";
400 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
401 /** Add better dontent-disposition support **/
405 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
406 str_replace("\n", "", $line);
407 str_replace("\n", "", $read[$i]);
408 $line = "$line $read[$i]";
412 /** Detects filename if any **/
413 if (strpos(strtolower(trim($line)), "filename=")) {
414 $pos = strpos($line, "filename=") +
9;
416 if (strpos($line, " ", $pos) > 0) {
417 $name = substr($name, $pos, strpos($line, " ", $pos));
419 $name = substr($name, $pos);
421 $name = str_replace("\"", "", $name);
422 $hdr->filename
= $name;
427 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
428 $hdr->replyto
= trim(substr($read[$i], 9, strlen($read[$i])));
433 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
434 $hdr->from
= trim(substr($read[$i], 5, strlen($read[$i]) - 6));
435 if (! isset($hdr->replyto
) ||
$hdr->replyto
== "") {
436 $hdr->replyto
= $hdr->from
;
441 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
442 $d = substr($read[$i], 5);
444 $d = strtr($d, array(' ' => ' '));
445 $d = explode(' ', $d);
446 $hdr->date
= getTimeStamp($d);
450 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
451 $hdr->subject
= trim(substr($read[$i], 8, strlen($read[$i]) - 9));
452 if (strlen(Chop($hdr->subject
)) == 0) {
453 $hdr->subject
= _("(no subject)");
457 if ($where == 'SUBJECT') {
458 $hdr->subject = $what;
459 // $hdr->subject = eregi_replace($what, "<b>\\0</b>", $hdr->subject);
466 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
468 $hdr->cc
[$pos] = trim(substr($read[$i], 4));
470 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
472 $hdr->cc
[$pos] = trim($read[$i]);
477 else if (strtolower(substr($read[$i], 0, 4)) == "bcc:") {
479 $hdr->bcc
[$pos] = trim(substr($read[$i], 5));
481 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
483 $hdr->bcc
[$pos] = trim($read[$i]);
488 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
490 $hdr->to
[$pos] = trim(substr($read[$i], 4));
492 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
494 $hdr->to
[$pos] = trim($read[$i]);
499 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
500 $hdr->message_id
= trim(substr($read[$i], 11));
505 /** ERROR CORRECTION **/
506 else if (substr($read[$i], 0, 1) == ")") {
507 if (strlen(trim($hdr->subject
)) == 0) {
508 $hdr->subject
= _("(no subject)");
510 if (strlen(trim($hdr->from
)) == 0) {
511 $hdr->from
= _("(unknown sender)");
513 if (strlen(trim($hdr->date
)) == 0) {
519 else if (strtolower(substr($read[$i], 0, 11)) == "x-priority:") {
520 $hdr->priority
= trim(substr($read[$i], 11));
531 /****************************************************************************
532 ** Returns the body of a message.
533 ****************************************************************************/
534 function sqimap_get_message_body ($imap_stream, &$header) {
536 return decodeMime($imap_stream, $header);
540 /****************************************************************************
541 ** Returns an array with the body structure
542 ****************************************************************************/