5 ** This implements functions that manipulate messages
8 if (!isset($mime_php)) include "../functions/mime.php";
10 /******************************************************************************
11 ** Copies specified messages to specified folder
12 ******************************************************************************/
13 function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) {
14 fputs ($imap_stream, "a001 COPY $start:$end \"$mailbox\"\r\n");
15 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
18 /******************************************************************************
19 ** Deletes specified messages and moves them to trash if possible
20 ******************************************************************************/
21 function sqimap_messages_delete ($imap_stream, $start, $end, $mailbox) {
22 global $move_to_trash, $trash_folder, $auto_expunge;
24 if (($move_to_trash == true) && (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder))) {
25 sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder);
26 sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
28 sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
32 /******************************************************************************
33 ** Sets the specified messages with specified flag
34 ******************************************************************************/
35 function sqimap_messages_flag ($imap_stream, $start, $end, $flag) {
36 fputs ($imap_stream, "a001 STORE $start:$end +FLAGS (\\$flag)\r\n");
37 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
41 /******************************************************************************
42 ** Remove specified flag from specified messages
43 ******************************************************************************/
44 function sqimap_messages_remove_flag ($imap_stream, $start, $end, $flag) {
45 fputs ($imap_stream, "a001 STORE $start:$end -FLAGS (\\$flag)\r\n");
46 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
50 /******************************************************************************
51 ** Returns some general header information -- FROM, DATE, and SUBJECT
52 ******************************************************************************/
54 var $from, $subject, $date, $to, $priority, $message_id;
57 function sqimap_get_small_header ($imap_stream, $id, $sent) {
59 fputs ($imap_stream, "a001 FETCH $id BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject Message-Id X-Priority Content-Type)]\r\n");
60 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
62 $subject = _("(no subject)");
63 $from = _("Unknown Sender");
68 for ($i = 0; $i < count($read); $i++
) {
69 if (eregi ("^to:(.*)$", $read[$i], $regs)) {
70 //$to = sqimap_find_displayable_name(substr($read[$i], 3));
72 } else if (eregi ("^from:(.*)$", $read[$i], $regs)) {
73 //$from = sqimap_find_displayable_name(substr($read[$i], 5));
75 } else if (eregi ("^x-priority:(.*)$", $read[$i], $regs)) {
76 $priority = trim($regs[1]);
77 } else if (eregi ("^message-id:(.*)$", $read[$i], $regs)) {
78 $messageid = trim($regs[1]);
79 } else if (eregi ("^cc:(.*)$", $read[$i], $regs)) {
81 } else if (eregi ("^date:(.*)$", $read[$i], $regs)) {
83 } else if (eregi ("^subject:(.*)$", $read[$i], $regs)) {
84 $subject = htmlspecialchars(trim($regs[1]));
86 $subject = _("(no subject)");
87 } else if (eregi ("^content-type:(.*)$", $read[$i], $regs)) {
88 $type = strtolower(trim($regs[1]));
89 if ($pos = strpos($type, ";"))
90 $type = substr($type, 0, $pos);
91 $type = explode("/", $type);
96 // If there isn't a date, it takes the internal date and uses
97 // that as the normal date.
98 if (trim($date) == "") {
99 fputs ($imap_stream, "a002 FETCH $id INTERNALDATE\r\n");
100 $internal_read = sqimap_read_data ($imap_stream, "a002", true, $r, $m);
102 // * 22 FETCH (INTERNALDATE " 8-Sep-2000 13:17:07 -0500")
103 $date = $internal_read[0];
104 $date = eregi_replace(".*internaldate \"", "", $date);
105 $date = eregi_replace("\".*", "", $date);
106 $date_ary = explode(" ", trim($date));
107 $date_ary[0] = str_replace("-", " ", $date_ary[0]);
108 $date = implode (" ", $date_ary);
111 fputs ($imap_stream, "a003 FETCH $id RFC822.SIZE\r\n");
112 $read = sqimap_read_data($imap_stream, "a003", true, $r, $m);
113 preg_match("/([0-9]+)\)\s*$/i", $read[0], $regs);
116 $header = new small_header
;
118 $header->from
= (trim($to) != '')?
$to : _('(only Cc/Bcc)');
120 $header->from
= $from;
122 $header->date
= $date;
123 $header->subject
= $subject;
125 $header->priority
= $priority;
126 $header->message_id
= $messageid;
128 $header->size
= $size;
129 $header->type0
= $type[0];
130 $header->type1
= $type[1];
135 /******************************************************************************
136 ** Returns the flags for the specified messages
137 ******************************************************************************/
138 function sqimap_get_flags ($imap_stream, $i) {
139 fputs ($imap_stream, "a001 FETCH $i:$i FLAGS\r\n");
140 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
141 if (ereg("FLAGS(.*)", $read[0], $regs))
142 return explode(" ", trim(ereg_replace('[\(\)\\]', '', $regs[1])));
143 return Array('None');
146 /******************************************************************************
147 ** Returns a message array with all the information about a message. See
148 ** the documentation folder for more information about this array.
149 ******************************************************************************/
150 function sqimap_get_message ($imap_stream, $id, $mailbox) {
151 $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
152 $msg = sqimap_get_message_body($imap_stream, &$header);
156 /******************************************************************************
157 ** Wrapper function that reformats the header information.
158 ******************************************************************************/
159 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
160 fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\r\n");
161 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
163 $header = sqimap_get_header($imap_stream, $read);
165 $header->mailbox
= $mailbox;
170 /******************************************************************************
171 ** Wrapper function that returns entity headers for use by decodeMime
172 ******************************************************************************/
174 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
175 $header = sqimap_get_header($imap_stream, $read);
176 $type0 = $header["TYPE0"];
177 $type1 = $header["TYPE1"];
178 $bound = $header["BOUNDARY"];
179 $encoding = $header["ENCODING"];
180 $charset = $header["CHARSET"];
181 $filename = $header["FILENAME"];
185 /******************************************************************************
186 ** Queries the IMAP server and gets all header information.
187 ******************************************************************************/
188 function sqimap_get_header ($imap_stream, $read) {
189 global $where, $what;
191 $hdr = new msg_header();
193 // Set up some defaults
194 $hdr->type0
= "text";
195 $hdr->type1
= "plain";
196 $hdr->charset
= "us-ascii";
198 preg_match("/\{([0-9]+)\}/", $read[$i], $regs);
199 preg_match("/[0-9]+/", $regs[0], $regs);
201 while ($i < count($read)) {
202 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
207 /** ENCODING TYPE **/
208 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
209 $hdr->encoding
= strtolower(trim(substr($read[$i], 26)));
214 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
215 $cont = strtolower(trim(substr($read[$i], 13)));
216 if (strpos($cont, ";"))
217 $cont = substr($cont, 0, strpos($cont, ";"));
220 if (strpos($cont, "/")) {
221 $hdr->type0
= substr($cont, 0, strpos($cont, "/"));
222 $hdr->type1
= substr($cont, strpos($cont, "/")+
1);
230 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
231 str_replace("\n", "", $line);
232 str_replace("\n", "", $read[$i]);
233 $line = "$line $read[$i]";
237 /** Detect the boundary of a multipart message **/
238 if (eregi("boundary=\"([^\"]+)\"", $line, $regs)) {
239 $hdr->boundary
= $regs[1];
242 /** Detect the charset **/
243 if (strpos(strtolower(trim($line)), "charset=")) {
244 $pos = strpos($line, "charset=") +
8;
245 $charset = trim($line);
246 if (strpos($line, ";", $pos) > 0) {
247 $charset = substr($charset, $pos, strpos($line, ";", $pos)-$pos);
249 $charset = substr($charset, $pos);
251 $charset = str_replace("\"", "", $charset);
252 $hdr->charset
= $charset;
254 $hdr->charset
= "us-ascii";
259 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
260 /** Add better dontent-disposition support **/
264 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
265 str_replace("\n", "", $line);
266 str_replace("\n", "", $read[$i]);
267 $line = "$line $read[$i]";
271 /** Detects filename if any **/
272 if (strpos(strtolower(trim($line)), "filename=")) {
273 $pos = strpos($line, "filename=") +
9;
275 if (strpos($line, " ", $pos) > 0) {
276 $name = substr($name, $pos, strpos($line, " ", $pos));
278 $name = substr($name, $pos);
280 $name = str_replace("\"", "", $name);
281 $hdr->filename
= $name;
286 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
287 $hdr->replyto
= trim(substr($read[$i], 9, strlen($read[$i])));
292 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
293 $hdr->from
= trim(substr($read[$i], 5, strlen($read[$i]) - 6));
294 if ($hdr->replyto
== "")
295 $hdr->replyto
= $hdr->from
;
299 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
300 $d = substr($read[$i], 5);
302 $d = ereg_replace(" ", " ", $d);
303 $d = explode(" ", $d);
304 $hdr->date
= getTimeStamp($d);
308 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
309 $hdr->subject
= trim(substr($read[$i], 8, strlen($read[$i]) - 9));
310 if (strlen(Chop($hdr->subject
)) == 0)
311 $hdr->subject
= _("(no subject)");
313 if ($where == "SUBJECT") {
314 $hdr->subject
= eregi_replace($what, "<b>\\0</b>", $hdr->subject
);
319 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
321 $hdr->cc
[$pos] = trim(substr($read[$i], 4));
323 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
325 $hdr->cc
[$pos] = trim($read[$i]);
330 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
332 $hdr->to
[$pos] = trim(substr($read[$i], 4));
334 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
336 $hdr->to
[$pos] = trim($read[$i]);
341 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
342 $hdr->message_id
= trim(substr($read[$i], 11));
347 /** ERROR CORRECTION **/
348 else if (substr($read[$i], 0, 1) == ")") {
349 if (strlen(trim($hdr->subject
)) == 0)
350 $hdr->subject
= _("(no subject)");
352 if (strlen(trim($hdr->from
)) == 0)
353 $hdr->from
= _("(unknown sender)");
355 if (strlen(trim($hdr->date
)) == 0)
367 /******************************************************************************
368 ** Returns the body of a message.
369 ******************************************************************************/
370 function sqimap_get_message_body ($imap_stream, &$header) {
372 return decodeMime($imap_stream, $body, &$header);
376 /******************************************************************************
377 ** Returns an array with the body structure
378 ******************************************************************************/