5 ** This implements functions that manipulate messages
8 if (!$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 $header = new small_header
;
84 $header->from
= $from;
86 $header->date
= $date;
87 $header->subject
= $subject;
89 $header->priority
= $priority;
90 $header->message_id
= $messageid;
96 /******************************************************************************
97 ** Returns the flags for the specified messages
98 ******************************************************************************/
99 function sqimap_get_flags ($imap_stream, $i) {
100 fputs ($imap_stream, "a001 FETCH $i:$i FLAGS\r\n");
101 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
102 if (strpos($read[0], "FLAGS")) {
103 $tmp = ereg_replace("\(", "", $read[0]);
104 $tmp = ereg_replace("\)", "", $tmp);
105 $tmp = str_replace("\\", "", $tmp);
106 $tmp = substr($tmp, strpos($tmp, "FLAGS")+
6, strlen($tmp));
108 $flags = explode(" ", $tmp);
115 /******************************************************************************
116 ** Returns a message array with all the information about a message. See
117 ** the documentation folder for more information about this array.
118 ******************************************************************************/
119 function sqimap_get_message ($imap_stream, $id, $mailbox) {
120 $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
121 $msg = sqimap_get_message_body($imap_stream, &$header);
125 /******************************************************************************
126 ** Wrapper function that reformats the header information.
127 ******************************************************************************/
128 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
129 fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\r\n");
130 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
132 $header = sqimap_get_header($imap_stream, $read);
134 $header->mailbox
= $mailbox;
139 /******************************************************************************
140 ** Wrapper function that returns entity headers for use by decodeMime
141 ******************************************************************************/
143 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
144 $header = sqimap_get_header($imap_stream, $read);
145 $type0 = $header["TYPE0"];
146 $type1 = $header["TYPE1"];
147 $bound = $header["BOUNDARY"];
148 $encoding = $header["ENCODING"];
149 $charset = $header["CHARSET"];
150 $filename = $header["FILENAME"];
154 /******************************************************************************
155 ** Queries the IMAP server and gets all header information.
156 ******************************************************************************/
157 function sqimap_get_header ($imap_stream, $read) {
158 global $where, $what;
160 $hdr = new msg_header();
162 // Set up some defaults
163 $hdr->type0
= "text";
164 $hdr->type1
= "plain";
165 $hdr->charset
= "us-ascii";
167 while ($i < count($read)) {
168 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
173 /** ENCODING TYPE **/
174 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
175 $hdr->encoding
= strtolower(trim(substr($read[$i], 26)));
180 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
181 $cont = strtolower(trim(substr($read[$i], 13)));
182 if (strpos($cont, ";"))
183 $cont = substr($cont, 0, strpos($cont, ";"));
186 if (strpos($cont, "/")) {
187 $hdr->type0
= substr($cont, 0, strpos($cont, "/"));
188 $hdr->type1
= substr($cont, strpos($cont, "/")+
1);
196 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
197 str_replace("\n", "", $line);
198 str_replace("\n", "", $read[$i]);
199 $line = "$line $read[$i]";
203 /** Detect the boundary of a multipart message **/
204 if (eregi("boundary=\"([^\"]+)\"", $line, $regs)) {
205 $hdr->boundary
= $regs[1];
208 /** Detect the charset **/
209 if (strpos(strtolower(trim($line)), "charset=")) {
210 $pos = strpos($line, "charset=") +
8;
211 $charset = trim($line);
212 if (strpos($line, " ", $pos) > 0) {
213 $charset = substr($charset, $pos, strpos($line, " ", $pos));
215 $charset = substr($charset, $pos);
217 $charset = str_replace("\"", "", $charset);
218 $hdr->charset
= $charset;
220 $hdr->charset
= "us-ascii";
225 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
226 /** Add better dontent-disposition support **/
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 /** Detects filename if any **/
238 if (strpos(strtolower(trim($line)), "filename=")) {
239 $pos = strpos($line, "filename=") +
9;
241 if (strpos($line, " ", $pos) > 0) {
242 $name = substr($name, $pos, strpos($line, " ", $pos));
244 $name = substr($name, $pos);
246 $name = str_replace("\"", "", $name);
247 $hdr->filename
= $name;
252 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
253 $hdr->replyto
= trim(substr($read[$i], 9, strlen($read[$i])));
258 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
259 $hdr->from
= trim(substr($read[$i], 5, strlen($read[$i]) - 6));
260 if ($hdr->replyto
== "")
261 $hdr->replyto
= $hdr->from
;
265 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
266 $d = substr($read[$i], 5);
268 $d = ereg_replace(" ", " ", $d);
269 $d = explode(" ", $d);
270 $hdr->date
= getTimeStamp($d);
274 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
275 $hdr->subject
= trim(substr($read[$i], 8, strlen($read[$i]) - 9));
276 if (strlen(Chop($hdr->subject
)) == 0)
277 $hdr->subject
= _("(no subject)");
279 if ($where == "SUBJECT") {
280 $hdr->subject
= eregi_replace($what, "<b>\\0</b>", $hdr->subject
);
285 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
287 $hdr->cc
[$pos] = trim(substr($read[$i], 4));
289 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
291 $hdr->cc
[$pos] = trim($read[$i]);
296 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
298 $hdr->to
[$pos] = trim(substr($read[$i], 4));
300 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
302 $hdr->to
[$pos] = trim($read[$i]);
307 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
308 $hdr->message_id
= trim(substr($read[$i], 11));
313 /** ERROR CORRECTION **/
314 else if (substr($read[$i], 0, 1) == ")") {
315 if (strlen(trim($hdr->subject
)) == 0)
316 $hdr->subject
= _("(no subject)");
318 if (strlen(trim($hdr->from
)) == 0)
319 $hdr->from
= _("(unknown sender)");
321 if (strlen(trim($hdr->date
)) == 0)
333 /******************************************************************************
334 ** Returns the body of a message.
335 ******************************************************************************/
336 function sqimap_get_message_body ($imap_stream, &$header) {
338 return decodeMime($imap_stream, $body, &$header);
342 /******************************************************************************
343 ** Returns an array with the body structure
344 ******************************************************************************/