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))) {
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)]\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)");
80 // If there isn't a date, it takes the internal date and uses
81 // that as the normal date.
82 if (trim($date) == "") {
83 fputs ($imap_stream, "a002 FETCH $id INTERNALDATE\r\n");
84 $internal_read = sqimap_read_data ($imap_stream, "a002", true, $r, $m);
86 // * 22 FETCH (INTERNALDATE " 8-Sep-2000 13:17:07 -0500")
87 $date = $internal_read[0];
88 $date = eregi_replace(".*internaldate \"", "", $date);
89 $date = eregi_replace("\".*", "", $date);
90 $date_ary = explode(" ", trim($date));
91 $date_ary[0] = str_replace("-", " ", $date_ary[0]);
92 $date = implode (" ", $date_ary);
95 $header = new small_header
;
99 $header->from
= $from;
101 $header->date
= $date;
102 $header->subject
= $subject;
104 $header->priority
= $priority;
105 $header->message_id
= $messageid;
111 /******************************************************************************
112 ** Returns the flags for the specified messages
113 ******************************************************************************/
114 function sqimap_get_flags ($imap_stream, $i) {
115 fputs ($imap_stream, "a001 FETCH $i:$i FLAGS\r\n");
116 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
117 if (strpos($read[0], "FLAGS")) {
118 $tmp = ereg_replace("\(", "", $read[0]);
119 $tmp = ereg_replace("\)", "", $tmp);
120 $tmp = str_replace("\\", "", $tmp);
121 $tmp = substr($tmp, strpos($tmp, "FLAGS")+
6, strlen($tmp));
123 $flags = explode(" ", $tmp);
130 /******************************************************************************
131 ** Returns a message array with all the information about a message. See
132 ** the documentation folder for more information about this array.
133 ******************************************************************************/
134 function sqimap_get_message ($imap_stream, $id, $mailbox) {
135 $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
136 $msg = sqimap_get_message_body($imap_stream, &$header);
140 /******************************************************************************
141 ** Wrapper function that reformats the header information.
142 ******************************************************************************/
143 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
144 fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\r\n");
145 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
147 $header = sqimap_get_header($imap_stream, $read);
149 $header->mailbox
= $mailbox;
154 /******************************************************************************
155 ** Wrapper function that returns entity headers for use by decodeMime
156 ******************************************************************************/
158 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
159 $header = sqimap_get_header($imap_stream, $read);
160 $type0 = $header["TYPE0"];
161 $type1 = $header["TYPE1"];
162 $bound = $header["BOUNDARY"];
163 $encoding = $header["ENCODING"];
164 $charset = $header["CHARSET"];
165 $filename = $header["FILENAME"];
169 /******************************************************************************
170 ** Queries the IMAP server and gets all header information.
171 ******************************************************************************/
172 function sqimap_get_header ($imap_stream, $read) {
173 global $where, $what;
175 $hdr = new msg_header();
177 // Set up some defaults
178 $hdr->type0
= "text";
179 $hdr->type1
= "plain";
180 $hdr->charset
= "us-ascii";
182 while ($i < count($read)) {
183 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
188 /** ENCODING TYPE **/
189 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
190 $hdr->encoding
= strtolower(trim(substr($read[$i], 26)));
195 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
196 $cont = strtolower(trim(substr($read[$i], 13)));
197 if (strpos($cont, ";"))
198 $cont = substr($cont, 0, strpos($cont, ";"));
201 if (strpos($cont, "/")) {
202 $hdr->type0
= substr($cont, 0, strpos($cont, "/"));
203 $hdr->type1
= substr($cont, strpos($cont, "/")+
1);
211 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
212 str_replace("\n", "", $line);
213 str_replace("\n", "", $read[$i]);
214 $line = "$line $read[$i]";
218 /** Detect the boundary of a multipart message **/
219 if (eregi("boundary=\"([^\"]+)\"", $line, $regs)) {
220 $hdr->boundary
= $regs[1];
223 /** Detect the charset **/
224 if (strpos(strtolower(trim($line)), "charset=")) {
225 $pos = strpos($line, "charset=") +
8;
226 $charset = trim($line);
227 if (strpos($line, " ", $pos) > 0) {
228 $charset = substr($charset, $pos, strpos($line, " ", $pos));
230 $charset = substr($charset, $pos);
232 $charset = str_replace("\"", "", $charset);
233 $hdr->charset
= $charset;
235 $hdr->charset
= "us-ascii";
240 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
241 /** Add better dontent-disposition support **/
245 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
246 str_replace("\n", "", $line);
247 str_replace("\n", "", $read[$i]);
248 $line = "$line $read[$i]";
252 /** Detects filename if any **/
253 if (strpos(strtolower(trim($line)), "filename=")) {
254 $pos = strpos($line, "filename=") +
9;
256 if (strpos($line, " ", $pos) > 0) {
257 $name = substr($name, $pos, strpos($line, " ", $pos));
259 $name = substr($name, $pos);
261 $name = str_replace("\"", "", $name);
262 $hdr->filename
= $name;
267 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
268 $hdr->replyto
= trim(substr($read[$i], 9, strlen($read[$i])));
273 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
274 $hdr->from
= trim(substr($read[$i], 5, strlen($read[$i]) - 6));
275 if ($hdr->replyto
== "")
276 $hdr->replyto
= $hdr->from
;
280 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
281 $d = substr($read[$i], 5);
283 $d = ereg_replace(" ", " ", $d);
284 $d = explode(" ", $d);
285 $hdr->date
= getTimeStamp($d);
289 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
290 $hdr->subject
= trim(substr($read[$i], 8, strlen($read[$i]) - 9));
291 if (strlen(Chop($hdr->subject
)) == 0)
292 $hdr->subject
= _("(no subject)");
294 if ($where == "SUBJECT") {
295 $hdr->subject
= eregi_replace($what, "<b>\\0</b>", $hdr->subject
);
300 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
302 $hdr->cc
[$pos] = trim(substr($read[$i], 4));
304 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
306 $hdr->cc
[$pos] = trim($read[$i]);
311 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
313 $hdr->to
[$pos] = trim(substr($read[$i], 4));
315 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
317 $hdr->to
[$pos] = trim($read[$i]);
322 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
323 $hdr->message_id
= trim(substr($read[$i], 11));
328 /** ERROR CORRECTION **/
329 else if (substr($read[$i], 0, 1) == ")") {
330 if (strlen(trim($hdr->subject
)) == 0)
331 $hdr->subject
= _("(no subject)");
333 if (strlen(trim($hdr->from
)) == 0)
334 $hdr->from
= _("(unknown sender)");
336 if (strlen(trim($hdr->date
)) == 0)
348 /******************************************************************************
349 ** Returns the body of a message.
350 ******************************************************************************/
351 function sqimap_get_message_body ($imap_stream, &$header) {
353 return decodeMime($imap_stream, $body, &$header);
357 /******************************************************************************
358 ** Returns an array with the body structure
359 ******************************************************************************/