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);
40 /******************************************************************************
41 ** Returns some general header information -- FROM, DATE, and SUBJECT
42 ******************************************************************************/
44 var $from, $subject, $date, $to, $priority, $message_id;
47 function sqimap_get_small_header ($imap_stream, $id, $sent) {
49 fputs ($imap_stream, "a001 FETCH $id BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject Message-Id X-Priority Content-Type)]\r\n");
50 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
52 $subject = _("(no subject)");
53 $from = _("Unknown Sender");
58 for ($i = 0; $i < count($read); $i++
) {
59 if (eregi ("^to:", $read[$i])) {
60 //$to = sqimap_find_displayable_name(substr($read[$i], 3));
61 $to = substr($read[$i], 3);
62 } else if (eregi ("^from:", $read[$i])) {
63 //$from = sqimap_find_displayable_name(substr($read[$i], 5));
64 $from = substr($read[$i], 5);
65 } else if (eregi ("^x-priority:", $read[$i])) {
66 $priority = trim(substr($read[$i], 11));
67 } else if (eregi ("^message-id:", $read[$i])) {
68 $messageid = trim(substr($read[$i], 11));
69 } else if (eregi ("^cc:", $read[$i])) {
70 $cc = substr($read[$i], 3);
71 } else if (eregi ("^date:", $read[$i])) {
72 $date = substr($read[$i], 5);
73 } else if (eregi ("^subject:", $read[$i])) {
74 $subject = htmlspecialchars(eregi_replace ("^subject: ", "", $read[$i]));
75 if (trim($subject) == "")
76 $subject = _("(no subject)");
77 } else if (eregi ("^content-type:", $read[$i])) {
78 $type = substr($read[$i], 14);
79 $type = strtolower(trim($type));
80 if ($pos = strpos($type, ";"))
81 $type = substr($type, 0, $pos);
82 $type = explode("/", $type);
87 // If there isn't a date, it takes the internal date and uses
88 // that as the normal date.
89 if (trim($date) == "") {
90 fputs ($imap_stream, "a002 FETCH $id INTERNALDATE\r\n");
91 $internal_read = sqimap_read_data ($imap_stream, "a002", true, $r, $m);
93 // * 22 FETCH (INTERNALDATE " 8-Sep-2000 13:17:07 -0500")
94 $date = $internal_read[0];
95 $date = eregi_replace(".*internaldate \"", "", $date);
96 $date = eregi_replace("\".*", "", $date);
97 $date_ary = explode(" ", trim($date));
98 $date_ary[0] = str_replace("-", " ", $date_ary[0]);
99 $date = implode (" ", $date_ary);
102 fputs ($imap_stream, "a003 FETCH $id RFC822.SIZE\r\n");
103 $read = sqimap_read_data($imap_stream, "a003", true, $r, $m);
104 preg_match("/([0-9]+)\)\s*$/i", $read[0], $regs);
107 $header = new small_header
;
111 $header->from
= $from;
113 $header->date
= $date;
114 $header->subject
= $subject;
116 $header->priority
= $priority;
117 $header->message_id
= $messageid;
119 $header->size
= $size;
120 $header->type0
= $type[0];
121 $header->type1
= $type[1];
126 /******************************************************************************
127 ** Returns the flags for the specified messages
128 ******************************************************************************/
129 function sqimap_get_flags ($imap_stream, $i) {
130 fputs ($imap_stream, "a001 FETCH $i:$i FLAGS\r\n");
131 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
132 if (strpos($read[0], "FLAGS")) {
133 $tmp = ereg_replace("\(", "", $read[0]);
134 $tmp = ereg_replace("\)", "", $tmp);
135 $tmp = str_replace("\\", "", $tmp);
136 $tmp = substr($tmp, strpos($tmp, "FLAGS")+
6, strlen($tmp));
138 $flags = explode(" ", $tmp);
145 /******************************************************************************
146 ** Returns a message array with all the information about a message. See
147 ** the documentation folder for more information about this array.
148 ******************************************************************************/
149 function sqimap_get_message ($imap_stream, $id, $mailbox) {
150 $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
151 $msg = sqimap_get_message_body($imap_stream, &$header);
155 /******************************************************************************
156 ** Wrapper function that reformats the header information.
157 ******************************************************************************/
158 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
159 fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\r\n");
160 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
162 $header = sqimap_get_header($imap_stream, $read);
164 $header->mailbox
= $mailbox;
169 /******************************************************************************
170 ** Wrapper function that returns entity headers for use by decodeMime
171 ******************************************************************************/
173 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
174 $header = sqimap_get_header($imap_stream, $read);
175 $type0 = $header["TYPE0"];
176 $type1 = $header["TYPE1"];
177 $bound = $header["BOUNDARY"];
178 $encoding = $header["ENCODING"];
179 $charset = $header["CHARSET"];
180 $filename = $header["FILENAME"];
184 /******************************************************************************
185 ** Queries the IMAP server and gets all header information.
186 ******************************************************************************/
187 function sqimap_get_header ($imap_stream, $read) {
188 global $where, $what;
190 $hdr = new msg_header();
192 // Set up some defaults
193 $hdr->type0
= "text";
194 $hdr->type1
= "plain";
195 $hdr->charset
= "us-ascii";
197 preg_match("/\{([0-9]+)\}/", $read[$i], $regs);
198 preg_match("/[0-9]+/", $regs[0], $regs);
200 while ($i < count($read)) {
201 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
206 /** ENCODING TYPE **/
207 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
208 $hdr->encoding
= strtolower(trim(substr($read[$i], 26)));
213 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
214 $cont = strtolower(trim(substr($read[$i], 13)));
215 if (strpos($cont, ";"))
216 $cont = substr($cont, 0, strpos($cont, ";"));
219 if (strpos($cont, "/")) {
220 $hdr->type0
= substr($cont, 0, strpos($cont, "/"));
221 $hdr->type1
= substr($cont, strpos($cont, "/")+
1);
229 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
230 str_replace("\n", "", $line);
231 str_replace("\n", "", $read[$i]);
232 $line = "$line $read[$i]";
236 /** Detect the boundary of a multipart message **/
237 if (eregi("boundary=\"([^\"]+)\"", $line, $regs)) {
238 $hdr->boundary
= $regs[1];
241 /** Detect the charset **/
242 if (strpos(strtolower(trim($line)), "charset=")) {
243 $pos = strpos($line, "charset=") +
8;
244 $charset = trim($line);
245 if (strpos($line, " ", $pos) > 0) {
246 $charset = substr($charset, $pos, strpos($line, " ", $pos));
248 $charset = substr($charset, $pos);
250 $charset = str_replace("\"", "", $charset);
251 $hdr->charset
= $charset;
253 $hdr->charset
= "us-ascii";
258 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
259 /** Add better dontent-disposition support **/
263 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
264 str_replace("\n", "", $line);
265 str_replace("\n", "", $read[$i]);
266 $line = "$line $read[$i]";
270 /** Detects filename if any **/
271 if (strpos(strtolower(trim($line)), "filename=")) {
272 $pos = strpos($line, "filename=") +
9;
274 if (strpos($line, " ", $pos) > 0) {
275 $name = substr($name, $pos, strpos($line, " ", $pos));
277 $name = substr($name, $pos);
279 $name = str_replace("\"", "", $name);
280 $hdr->filename
= $name;
285 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
286 $hdr->replyto
= trim(substr($read[$i], 9, strlen($read[$i])));
291 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
292 $hdr->from
= trim(substr($read[$i], 5, strlen($read[$i]) - 6));
293 if ($hdr->replyto
== "")
294 $hdr->replyto
= $hdr->from
;
298 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
299 $d = substr($read[$i], 5);
301 $d = ereg_replace(" ", " ", $d);
302 $d = explode(" ", $d);
303 $hdr->date
= getTimeStamp($d);
307 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
308 $hdr->subject
= trim(substr($read[$i], 8, strlen($read[$i]) - 9));
309 if (strlen(Chop($hdr->subject
)) == 0)
310 $hdr->subject
= _("(no subject)");
312 if ($where == "SUBJECT") {
313 $hdr->subject
= eregi_replace($what, "<b>\\0</b>", $hdr->subject
);
318 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
320 $hdr->cc
[$pos] = trim(substr($read[$i], 4));
322 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
324 $hdr->cc
[$pos] = trim($read[$i]);
329 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
331 $hdr->to
[$pos] = trim(substr($read[$i], 4));
333 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
335 $hdr->to
[$pos] = trim($read[$i]);
340 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
341 $hdr->message_id
= trim(substr($read[$i], 11));
346 /** ERROR CORRECTION **/
347 else if (substr($read[$i], 0, 1) == ")") {
348 if (strlen(trim($hdr->subject
)) == 0)
349 $hdr->subject
= _("(no subject)");
351 if (strlen(trim($hdr->from
)) == 0)
352 $hdr->from
= _("(unknown sender)");
354 if (strlen(trim($hdr->date
)) == 0)
366 /******************************************************************************
367 ** Returns the body of a message.
368 ******************************************************************************/
369 function sqimap_get_message_body ($imap_stream, &$header) {
371 return decodeMime($imap_stream, $body, &$header);
375 /******************************************************************************
376 ** Returns an array with the body structure
377 ******************************************************************************/