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 function sqimap_get_small_header_list ($imap_stream, $msg_list, $issent) {
79 global $squirrelmail_language, $color, $data_dir, $username;
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);
87 $sizes_list = array();
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
94 for ($i = 0; $i < sizeof($msg_list); $i++
) {
95 $id2index[$msg_list[$i]] = $i;
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);
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.") .
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.") .
115 _("Unknown message number in reply from server: ") .
116 $regs[1] . "</font><br>\n";
118 $read_list[$id2index[$regs[1]]] = $r;
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);
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.");
133 echo _("Unknown response from IMAP server: ") . ' 2.';
134 echo $r[0] . "</font><br>\n";
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.");
142 echo _("Unknown messagenumber in reply from server: ");
143 echo $regs[1] . "</font><br>\n";
146 $sizes_list[$id2index[$regs[1]]] = $r;
150 for ($msgi = 0; $msgi < $maxmsg; $msgi++
) {
151 $subject = _("(no subject)");
152 $from = _("Unknown Sender");
160 $read = $read_list[$msgi];
162 foreach ($read as $read_part) {
163 if (eregi ("^to:(.*)$", $read_part, $regs)) {
165 } else if (eregi ("^from:(.*)$", $read_part, $regs)) {
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)) {
173 } else if (eregi ("^date:(.*)$", $read_part, $regs)) {
175 } else if (eregi ("^subject:(.*)$", $read_part, $regs)) {
176 $subject = htmlspecialchars(trim($regs[1]));
177 if ($subject == "") {
178 $subject = _("(no subject)");
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);
185 $type = explode("/", $type);
186 if (!isset($type[1])) {
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);
201 eregi("([0-9]+)[^0-9]*$", $sizes_list[$msgi][0], $regs);
204 $header = new small_header
;
206 $header->from
= (trim($to) != '' ?
$to : '(' ._("No To Address") . ')');
208 $header->from
= $from;
211 $header->date
= $date;
212 $header->subject
= $subject;
214 $header->priority
= $priority;
215 $header->message_id
= $messageid;
217 $header->size
= $size;
218 $header->type0
= $type[0];
219 $header->type1
= $type[1];
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])));
232 return array('None');
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;
240 $result_list = sqimap_run_command_list ($imap_stream, "FETCH $msgs_str FLAGS", true, $response, $message);
241 $result_flags = array();
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])));
248 set_up_language($squirrelmail_language);
249 echo "<br><b><font color=$color[2]>\n" .
250 _("ERROR : Could not complete request.") .
252 _("Unknown response from IMAP server: ") .
253 $result_list[$i][0] . "</font><br>\n";
257 arsort($result_flags);
258 return $result_flags;
262 * Returns a message array with all the information about a message.
263 * See the documentation folder for more information about this array.
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);
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);
275 $header->mailbox
= $mailbox;
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);
284 $header->mailbox
= $mailbox;
289 /* Wrapper function that returns entity headers for use by decodeMime */
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"];
301 /* Queries the IMAP server and gets all header information. */
302 function sqimap_get_header ($imap_stream, $read) {
303 global $where, $what;
305 $hdr = new msg_header();
308 /* Set up some defaults */
309 $hdr->type0
= "text";
310 $hdr->type1
= "plain";
311 $hdr->charset
= "us-ascii";
313 while ($i < count($read)) {
314 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
319 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
320 $hdr->encoding
= strtolower(trim(substr($read[$i], 26)));
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, ";"));
330 if (strpos($cont, "/")) {
331 $hdr->type0
= substr($cont, 0, strpos($cont, "/"));
332 $hdr->type1
= substr($cont, strpos($cont, "/")+
1);
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]";
346 /* Detect the boundary of a multipart message */
347 if (eregi('boundary="([^"]+)"', $line, $regs)) {
348 $hdr->boundary
= $regs[1];
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);
358 $charset = substr($charset, $pos);
360 $charset = str_replace("\"", "", $charset);
361 $hdr->charset
= $charset;
363 $hdr->charset
= "us-ascii";
366 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
367 /* Add better content-disposition support */
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]";
377 /* Detects filename if any */
378 if (strpos(strtolower(trim($line)), "filename=")) {
379 $pos = strpos($line, "filename=") +
9;
381 if (strpos($line, " ", $pos) > 0) {
382 $name = substr($name, $pos, strpos($line, " ", $pos));
384 $name = substr($name, $pos);
386 $name = str_replace("\"", "", $name);
387 $hdr->filename
= $name;
391 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
392 $hdr->replyto
= trim(substr($read[$i], 9, strlen($read[$i])));
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
;
404 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
405 $d = substr($read[$i], 5);
407 $d = strtr($d, array(' ' => ' '));
408 $d = explode(' ', $d);
409 $hdr->date
= getTimeStamp($d);
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)");
419 if ($where == 'SUBJECT') {
420 $hdr->subject = $what;
421 // $hdr->subject = eregi_replace($what, "<b>\\0</b>", $hdr->subject);
427 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
429 $hdr->cc
[$pos] = trim(substr($read[$i], 4));
431 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
433 $hdr->cc
[$pos] = trim($read[$i]);
438 else if (strtolower(substr($read[$i], 0, 4)) == "bcc:") {
440 $hdr->bcc
[$pos] = trim(substr($read[$i], 5));
442 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
444 $hdr->bcc
[$pos] = trim($read[$i]);
449 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
451 $hdr->to
[$pos] = trim(substr($read[$i], 4));
453 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
455 $hdr->to
[$pos] = trim($read[$i]);
460 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
461 $hdr->message_id
= trim(substr($read[$i], 11));
464 /* ERROR CORRECTION */
465 else if (substr($read[$i], 0, 1) == ")") {
466 if (strlen(trim($hdr->subject
)) == 0) {
467 $hdr->subject
= _("(no subject)");
469 if (strlen(trim($hdr->from
)) == 0) {
470 $hdr->from
= _("(unknown sender)");
472 if (strlen(trim($hdr->date
)) == 0) {
478 else if (strtolower(substr($read[$i], 0, 11)) == "x-priority:") {
479 $hdr->priority
= trim(substr($read[$i], 11));
489 /* Returns the body of a message. */
490 function sqimap_get_message_body ($imap_stream, &$header) {
492 return decodeMime($imap_stream, $header);