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;
47 function sqimap_get_small_header ($imap_stream, $id, $sent) {
48 fputs ($imap_stream, "a001 FETCH $id BODY.PEEK[HEADER.FIELDS (Date To From Subject)]\r\n");
49 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
51 $subject = _("(no subject)");
52 $from = _("Unknown Sender");
54 for ($i = 0; $i < count($read); $i++
) {
56 if (eregi ("^to:", $read[$i])) {
57 $from = sqimap_find_displayable_name(substr($read[$i], 3));
60 if (eregi ("^from:", $read[$i])) {
61 $from = sqimap_find_displayable_name(substr($read[$i], 5));
65 if (eregi ("^date:", $read[$i])) {
66 $date = substr($read[$i], 5);
67 } else if (eregi ("^subject:", $read[$i])) {
68 $subject = htmlspecialchars(eregi_replace ("^subject: ", "", $read[$i]));
69 if (strlen(Chop($subject)) == 0)
70 $subject = _("(no subject)");
74 $header = new small_header
;
75 $header->from
= $from;
76 $header->date
= $date;
77 $header->subject
= $subject;
82 /******************************************************************************
83 ** Returns the flags for the specified messages
84 ******************************************************************************/
85 function sqimap_get_flags ($imap_stream, $i) {
86 fputs ($imap_stream, "a001 FETCH $i:$i FLAGS\r\n");
87 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
88 if (strpos($read[0], "FLAGS")) {
89 $tmp = ereg_replace("\(", "", $read[0]);
90 $tmp = ereg_replace("\)", "", $tmp);
91 $tmp = str_replace("\\", "", $tmp);
92 $tmp = substr($tmp, strpos($tmp, "FLAGS")+
6, strlen($tmp));
94 $flags = explode(" ", $tmp);
101 /******************************************************************************
102 ** Returns a message array with all the information about a message. See
103 ** the documentation folder for more information about this array.
104 ******************************************************************************/
105 function sqimap_get_message ($imap_stream, $id, $mailbox) {
107 $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
108 $msg = sqimap_get_message_body($imap_stream, &$header);
112 /******************************************************************************
113 ** Wrapper function that reformats the header information.
114 ******************************************************************************/
115 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
116 fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\r\n");
117 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
119 $header = sqimap_get_header($imap_stream, $read);
121 $header->mailbox
= $mailbox;
126 /******************************************************************************
127 ** Wrapper function that returns entity headers for use by decodeMime
128 ******************************************************************************/
130 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
131 $header = sqimap_get_header($imap_stream, $read);
132 $type0 = $header["TYPE0"];
133 $type1 = $header["TYPE1"];
134 $bound = $header["BOUNDARY"];
135 $encoding = $header["ENCODING"];
136 $charset = $header["CHARSET"];
137 $filename = $header["FILENAME"];
141 /******************************************************************************
142 ** Queries the IMAP server and gets all header information.
143 ******************************************************************************/
144 function sqimap_get_header ($imap_stream, $read) {
145 $hdr = new msg_header();
147 // Set up some defaults
148 $hdr->type0
= "text";
149 $hdr->type1
= "plain";
150 $hdr->charset
= "us-ascii";
152 while ($i < count($read)) {
153 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
158 /** ENCODING TYPE **/
159 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
160 $hdr->encoding
= strtolower(trim(substr($read[$i], 26)));
165 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
166 $cont = strtolower(trim(substr($read[$i], 13)));
167 if (strpos($cont, ";"))
168 $cont = substr($cont, 0, strpos($cont, ";"));
171 if (strpos($cont, "/")) {
172 $hdr->type0
= substr($cont, 0, strpos($cont, "/"));
173 $hdr->type1
= substr($cont, strpos($cont, "/")+
1);
181 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
182 str_replace("\n", "", $line);
183 str_replace("\n", "", $read[$i]);
184 $line = "$line $read[$i]";
188 /** Detect the boundary of a multipart message **/
189 if (eregi("boundary=\"([^\"]+)\"", $line, $regs)) {
190 $hdr->boundary
= $regs[1];
193 /** Detect the charset **/
194 if (strpos(strtolower(trim($line)), "charset=")) {
195 $pos = strpos($line, "charset=") +
8;
196 $charset = trim($line);
197 if (strpos($line, " ", $pos) > 0) {
198 $charset = substr($charset, $pos, strpos($line, " ", $pos));
200 $charset = substr($charset, $pos);
202 $charset = str_replace("\"", "", $charset);
203 $hdr->charset
= $charset;
205 $hdr->charset
= "us-ascii";
210 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
211 /** Add better dontent-disposition support **/
215 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
216 str_replace("\n", "", $line);
217 str_replace("\n", "", $read[$i]);
218 $line = "$line $read[$i]";
222 /** Detects filename if any **/
223 if (strpos(strtolower(trim($line)), "filename=")) {
224 $pos = strpos($line, "filename=") +
9;
226 if (strpos($line, " ", $pos) > 0) {
227 $name = substr($name, $pos, strpos($line, " ", $pos));
229 $name = substr($name, $pos);
231 $name = str_replace("\"", "", $name);
232 $hdr->filename
= $name;
237 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
238 $hdr->replyto
= trim(substr($read[$i], 9, strlen($read[$i])));
243 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
244 $hdr->from
= trim(substr($read[$i], 5, strlen($read[$i]) - 6));
245 if ($hdr->replyto
== "")
246 $hdr->replyto
= $hdr->from
;
250 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
251 $d = substr($read[$i], 5);
253 $d = ereg_replace(" ", " ", $d);
254 $d = explode(" ", $d);
255 $hdr->date
= getTimeStamp($d);
259 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
260 $hdr->subject
= trim(substr($read[$i], 8, strlen($read[$i]) - 9));
261 if (strlen(Chop($hdr->subject
)) == 0)
262 $hdr->subject
= _("(no subject)");
266 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
268 $hdr->cc
[$pos] = trim(substr($read[$i], 4));
270 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
272 $hdr->cc
[$pos] = trim($read[$i]);
277 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
279 $hdr->to
[$pos] = trim(substr($read[$i], 4));
281 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
283 $hdr->to
[$pos] = trim($read[$i]);
288 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
289 $hdr->message_id
= trim(substr($read[$i], 11));
294 /** ERROR CORRECTION **/
295 else if (substr($read[$i], 0, 1) == ")") {
296 if (strlen(trim($hdr->subject
)) == 0)
297 $hdr->subject
= _("(no subject)");
299 if (strlen(trim($hdr->from
)) == 0)
300 $hdr->from
= _("(unknown sender)");
302 if (strlen(trim($hdr->date
)) == 0)
314 /******************************************************************************
315 ** Returns the body of a message.
316 ******************************************************************************/
317 function sqimap_get_message_body ($imap_stream, &$header) {
319 //fputs ($imap_stream, "a001 FETCH $id:$id BODY[TEXT]\r\n");
320 //$read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
324 while ($i < count($read)-1) {
326 $body[$j] = $read[$i];
331 return decodeMime($body, &$header);
335 /******************************************************************************
336 ** Returns an array with the body structure
337 ******************************************************************************/