6 * Copyright (c) 1999-2001 The Squirrelmail Development Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This implements functions that manipulate messages
14 /*****************************************************************/
15 /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
16 /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
17 /*** + Base level indent should begin at left margin, as ***/
18 /*** the function definition starts below. ***/
19 /*** + All identation should consist of four space blocks ***/
20 /*** + Tab characters are evil. ***/
21 /*** + all comments should use "slash-star ... star-slash" ***/
22 /*** style -- no pound characters, no slash-slash style ***/
23 /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
24 /*** ALWAYS USE { AND } CHARACTERS!!! ***/
25 /*** + Please use ' instead of ", when possible. Note " ***/
26 /*** should always be used in _( ) function calls. ***/
27 /*** Thank you for your help making the SM code more readable. ***/
28 /*****************************************************************/
30 /****************************************************
31 ** Copies specified messages to specified folder **
32 ****************************************************/
33 function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) {
34 fputs ($imap_stream, sqimap_session_id() . " COPY $start:$end \"$mailbox\"\r\n");
35 $read = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
38 /******************************************************************************
39 ** Deletes specified messages and moves them to trash if possible
40 ******************************************************************************/
41 function sqimap_messages_delete ($imap_stream, $start, $end, $mailbox) {
42 global $move_to_trash, $trash_folder, $auto_expunge;
44 if (($move_to_trash == true) && (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder))) {
45 sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder);
46 sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
48 sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
52 /******************************************************************************
53 ** Sets the specified messages with specified flag
54 ******************************************************************************/
55 function sqimap_messages_flag ($imap_stream, $start, $end, $flag) {
56 fputs ($imap_stream, sqimap_session_id() . " STORE $start:$end +FLAGS (\\$flag)\r\n");
57 $read = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
61 /******************************************************************************
62 ** Remove specified flag from specified messages
63 ******************************************************************************/
64 function sqimap_messages_remove_flag ($imap_stream, $start, $end, $flag) {
65 fputs ($imap_stream, sqimap_session_id() . " STORE $start:$end -FLAGS (\\$flag)\r\n");
66 $read = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
70 /******************************************************************************
71 ** Returns some general header information -- FROM, DATE, and SUBJECT
72 ******************************************************************************/
74 var $from = '', $subject = '', $date = '', $to = '',
75 $priority = 0, $message_id = 0, $cc = '';
78 function sqimap_get_small_header ($imap_stream, $id, $sent) {
79 $res = sqimap_get_small_header_list($imap_stream, array($id), $sent);
83 // Sort the message list and crunch to be as small as possible
84 // (overflow could happen, so make it small if possible)
85 function sqimap_message_list_squisher($messages_array) {
86 if( !is_array( $messages_array ) ) return;
87 sort($messages_array, SORT_NUMERIC
);
89 while ($messages_array) {
90 $start = array_shift($messages_array);
92 while (isset($messages_array[0]) && $messages_array[0] == $end +
1)
93 $end = array_shift($messages_array);
98 $msgs_str .= ':' . $end;
104 function sqimap_get_small_header_list ($imap_stream, $msg_list, $issent) {
105 global $squirrelmail_language, $color;
107 /* Get the small headers for each message in $msg_list */
108 $sid = sqimap_session_id();
109 $maxmsg = sizeof($msg_list);
110 $msgs_str = sqimap_message_list_squisher($msg_list);
112 $read_list = array();
113 $sizes_list = array();
116 * We need to return the data in the same order as the caller supplied
117 * in $msg_list, but IMAP servers are free to return responses in
118 * whatever order they wish... So we need to re-sort manually
120 for ($i = 0; $i < sizeof($msg_list); $i++
) {
121 $id2index[$msg_list[$i]] = $i;
124 $query = "$sid FETCH $msgs_str BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject Message-Id X-Priority Content-Type)]\r\n";
125 fputs ($imap_stream, $query);
126 $readin_list = sqimap_read_data_list($imap_stream, $sid, true, $response, $message);
128 foreach ($readin_list as $r) {
129 if (!eregi("^\\* ([0-9]+) FETCH", $r[0], $regs)) {
130 set_up_language($squirrelmail_language);
131 echo '<br><b><font color=$color[2]>' .
132 _("ERROR : Could not complete request.") .
134 _("Unknown response from IMAP server: ") . ' 1.' .
135 $r[0] . "</font><br>\n";
137 } else if (! isset($id2index[$regs[1]]) ||
!count($id2index[$regs[1]])) {
138 set_up_language($squirrelmail_language);
139 echo '<br><b><font color=$color[2]>' .
140 _("ERROR : Could not complete request.") .
142 _("Unknown message number in reply from server: ") .
143 $regs[1] . "</font><br>\n";
146 $read_list[$id2index[$regs[1]]] = $r;
151 $query = "$sid FETCH $msgs_str RFC822.SIZE\r\n";
152 fputs ($imap_stream, $query);
153 $sizesin_list = sqimap_read_data_list($imap_stream, $sid, true, $response, $message);
155 foreach ($sizesin_list as $r) {
156 if (!eregi("^\\* ([0-9]+) FETCH", $r[0], $regs)) {
157 set_up_language($squirrelmail_language);
158 echo "<br><b><font color=$color[2]>\n";
159 echo _("ERROR : Could not complete request.");
161 echo _("Unknown response from IMAP server: ") . ' 2.';
162 echo $r[0] . "</font><br>\n";
165 if (!count($id2index[$regs[1]])) {
166 set_up_language($squirrelmail_language);
167 echo "<br><b><font color=$color[2]>\n";
168 echo _("ERROR : Could not complete request.");
170 echo _("Unknown messagenumber in reply from server: ");
171 echo $regs[1] . "</font><br>\n";
174 $sizes_list[$id2index[$regs[1]]] = $r;
178 for ($msgi = 0; $msgi < $maxmsg; $msgi++
) {
179 $subject = _("(no subject)");
180 $from = _("Unknown Sender");
188 $read = $read_list[$msgi];
190 for ($i = 0; $i < count($read); $i++
) {
191 if (eregi ("^to:(.*)$", $read[$i], $regs)) {
192 //$to = sqimap_find_displayable_name(substr($read[$i], 3));
194 } else if (eregi ("^from:(.*)$", $read[$i], $regs)) {
195 //$from = sqimap_find_displayable_name(substr($read[$i], 5));
197 } else if (eregi ("^x-priority:(.*)$", $read[$i], $regs)) {
198 $priority = trim($regs[1]);
199 } else if (eregi ("^message-id:(.*)$", $read[$i], $regs)) {
200 $messageid = trim($regs[1]);
201 } else if (eregi ("^cc:(.*)$", $read[$i], $regs)) {
203 } else if (eregi ("^date:(.*)$", $read[$i], $regs)) {
205 } else if (eregi ("^subject:(.*)$", $read[$i], $regs)) {
206 $subject = htmlspecialchars(trim($regs[1]));
208 $subject = _("(no subject)");
209 } else if (eregi ("^content-type:(.*)$", $read[$i], $regs)) {
210 $type = strtolower(trim($regs[1]));
211 if ($pos = strpos($type, ";"))
212 $type = substr($type, 0, $pos);
213 $type = explode("/", $type);
214 if (! isset($type[1]))
219 if (trim($date) == "") {
220 fputs($imap_stream, "$sid FETCH $msg_list[$msgi] INTERNALDATE\r\n");
221 $readdate = sqimap_read_data($imap_stream, $sid, true, $response, $message);
222 if (eregi(".*INTERNALDATE \"(.*)\".*", $readdate[0], $regs)) {
223 $date_list = explode(" ", trim($regs[1]));
224 $date_list[0] = str_replace("-", " ", $date_list[0]);
225 $date = implode(" ", $date_list);
228 eregi("([0-9]+)[^0-9]*$", $sizes_list[$msgi][0], $regs);
231 $header = new small_header
;
232 if ($issent == true) {
233 $header->from
= (trim($to) != '' ?
$to : '(' ._("No To Address") . ')');
235 $header->from
= $from;
238 $header->date
= $date;
239 $header->subject
= $subject;
241 $header->priority
= $priority;
242 $header->message_id
= $messageid;
244 $header->size
= $size;
245 $header->type0
= $type[0];
246 $header->type1
= $type[1];
253 /******************************************************************************
254 ** Returns the flags for the specified messages
255 ******************************************************************************/
256 function sqimap_get_flags ($imap_stream, $i) {
257 fputs ($imap_stream, sqimap_session_id() . " FETCH $i:$i FLAGS\r\n");
258 $read = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
259 if (ereg("FLAGS(.*)", $read[0], $regs))
260 return explode(" ", trim(ereg_replace('[\\(\\)\\\\]', '', $regs[1])));
261 return Array('None');
264 function sqimap_get_flags_list ($imap_stream, $msg_list) {
265 $msgs_str = sqimap_message_list_squisher($msg_list);
266 for ($i = 0; $i < sizeof($msg_list); $i++
) {
267 $id2index[$msg_list[$i]] = $i;
269 fputs ($imap_stream, sqimap_session_id() . " FETCH $msgs_str FLAGS\r\n");
270 $result_list = sqimap_read_data_list ($imap_stream, sqimap_session_id(), true, $response, $message);
271 $result_flags = array();
273 for ($i = 0; $i < sizeof($result_list); $i++
) {
274 if (eregi("^\\* ([0-9]+).*FETCH.*FLAGS(.*)", $result_list[$i][0], $regs)
275 && isset($id2index[$regs[1]]) && count($id2index[$regs[1]])) {
276 $result_flags[$id2index[$regs[1]]] = explode(" ", trim(ereg_replace('[\\(\\)\\\\]', '', $regs[2])));
278 set_up_language($squirrelmail_language);
279 echo "<br><b><font color=$color[2]>\n";
280 echo _("ERROR : Could not complete request.");
282 echo _("Unknown response from IMAP server: ");
283 echo $result_list[$i][0] . "</font><br>\n";
287 arsort($result_flags);
288 return $result_flags;
291 /******************************************************************************
292 ** Returns a message array with all the information about a message. See
293 ** the documentation folder for more information about this array.
294 ******************************************************************************/
295 function sqimap_get_message ($imap_stream, $id, $mailbox) {
296 $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
297 return sqimap_get_message_body($imap_stream, $header);
300 /******************************************************************************
301 ** Wrapper function that reformats the header information.
302 ******************************************************************************/
303 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
304 fputs ($imap_stream, sqimap_session_id() . " FETCH $id:$id BODY[HEADER]\r\n");
305 $read = sqimap_read_data ($imap_stream, sqimap_session_id(), 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, ";"));
361 if (strpos($cont, "/")) {
362 $hdr->type0
= substr($cont, 0, strpos($cont, "/"));
363 $hdr->type1
= substr($cont, strpos($cont, "/")+
1);
371 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
372 str_replace("\n", "", $line);
373 str_replace("\n", "", $read[$i]);
374 $line = "$line $read[$i]";
378 /** Detect the boundary of a multipart message **/
379 if (eregi('boundary="([^"]+)"', $line, $regs)) {
380 $hdr->boundary
= $regs[1];
383 /** Detect the charset **/
384 if (strpos(strtolower(trim($line)), "charset=")) {
385 $pos = strpos($line, "charset=") +
8;
386 $charset = trim($line);
387 if (strpos($line, ";", $pos) > 0) {
388 $charset = substr($charset, $pos, strpos($line, ";", $pos)-$pos);
390 $charset = substr($charset, $pos);
392 $charset = str_replace("\"", "", $charset);
393 $hdr->charset
= $charset;
395 $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
;
440 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
441 $d = substr($read[$i], 5);
443 $d = strtr($d, array(' ' => ' '));
444 $d = explode(' ', $d);
445 $hdr->date
= getTimeStamp($d);
449 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
450 $hdr->subject
= trim(substr($read[$i], 8, strlen($read[$i]) - 9));
451 if (strlen(Chop($hdr->subject
)) == 0)
452 $hdr->subject
= _("(no subject)");
454 if ($where == "SUBJECT") {
455 $hdr->subject
= eregi_replace($what, "<b>\\0</b>", $hdr->subject
);
460 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
462 $hdr->cc
[$pos] = trim(substr($read[$i], 4));
464 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
466 $hdr->cc
[$pos] = trim($read[$i]);
471 else if (strtolower(substr($read[$i], 0, 4)) == "bcc:") {
473 $hdr->bcc
[$pos] = trim(substr($read[$i], 5));
475 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
477 $hdr->bcc
[$pos] = trim($read[$i]);
482 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
484 $hdr->to
[$pos] = trim(substr($read[$i], 4));
486 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
488 $hdr->to
[$pos] = trim($read[$i]);
493 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
494 $hdr->message_id
= trim(substr($read[$i], 11));
499 /** ERROR CORRECTION **/
500 else if (substr($read[$i], 0, 1) == ")") {
501 if (strlen(trim($hdr->subject
)) == 0)
502 $hdr->subject
= _("(no subject)");
504 if (strlen(trim($hdr->from
)) == 0)
505 $hdr->from
= _("(unknown sender)");
507 if (strlen(trim($hdr->date
)) == 0)
512 else if (strtolower(substr($read[$i], 0, 11)) == "x-priority:") {
513 $hdr->priority
= trim(substr($read[$i], 11));
524 /******************************************************************************
525 ** Returns the body of a message.
526 ******************************************************************************/
527 function sqimap_get_message_body ($imap_stream, &$header) {
529 return decodeMime($imap_stream, $header);
533 /******************************************************************************
534 ** Returns an array with the body structure
535 ******************************************************************************/