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) {
48 fputs ($imap_stream, "a001 FETCH $id BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject Message-Id X-Priority)]\r\n");
49 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
51 $subject = _("(no subject)");
52 $from = _("Unknown Sender");
57 for ($i = 0; $i < count($read); $i++
) {
58 if (eregi ("^to:", $read[$i])) {
59 //$to = sqimap_find_displayable_name(substr($read[$i], 3));
60 $to = substr($read[$i], 3);
61 } else if (eregi ("^from:", $read[$i])) {
62 //$from = sqimap_find_displayable_name(substr($read[$i], 5));
63 $from = substr($read[$i], 5);
64 } else if (eregi ("^x-priority:", $read[$i])) {
65 $priority = trim(substr($read[$i], 11));
66 } else if (eregi ("^message-id:", $read[$i])) {
67 $messageid = trim(substr($read[$i], 11));
68 } else if (eregi ("^cc:", $read[$i])) {
69 $cc = substr($read[$i], 3);
70 } else if (eregi ("^date:", $read[$i])) {
71 $date = substr($read[$i], 5);
72 } else if (eregi ("^subject:", $read[$i])) {
73 $subject = htmlspecialchars(eregi_replace ("^subject: ", "", $read[$i]));
74 if (trim($subject) == "")
75 $subject = _("(no subject)");
79 $header = new small_header
;
83 $header->from
= $from;
85 $header->date
= $date;
86 $header->subject
= $subject;
88 $header->priority
= $priority;
89 $header->message_id
= $messageid;
95 /******************************************************************************
96 ** Returns the flags for the specified messages
97 ******************************************************************************/
98 function sqimap_get_flags ($imap_stream, $i) {
99 fputs ($imap_stream, "a001 FETCH $i:$i FLAGS\r\n");
100 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
101 if (strpos($read[0], "FLAGS")) {
102 $tmp = ereg_replace("\(", "", $read[0]);
103 $tmp = ereg_replace("\)", "", $tmp);
104 $tmp = str_replace("\\", "", $tmp);
105 $tmp = substr($tmp, strpos($tmp, "FLAGS")+
6, strlen($tmp));
107 $flags = explode(" ", $tmp);
114 /******************************************************************************
115 ** Returns a message array with all the information about a message. See
116 ** the documentation folder for more information about this array.
117 ******************************************************************************/
118 function sqimap_get_message ($imap_stream, $id, $mailbox) {
119 $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
120 $msg = sqimap_get_message_body($imap_stream, &$header);
124 /******************************************************************************
125 ** Wrapper function that reformats the header information.
126 ******************************************************************************/
127 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
128 fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\r\n");
129 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
131 $header = sqimap_get_header($imap_stream, $read);
133 $header->mailbox
= $mailbox;
138 /******************************************************************************
139 ** Wrapper function that returns entity headers for use by decodeMime
140 ******************************************************************************/
142 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
143 $header = sqimap_get_header($imap_stream, $read);
144 $type0 = $header["TYPE0"];
145 $type1 = $header["TYPE1"];
146 $bound = $header["BOUNDARY"];
147 $encoding = $header["ENCODING"];
148 $charset = $header["CHARSET"];
149 $filename = $header["FILENAME"];
153 /******************************************************************************
154 ** Queries the IMAP server and gets all header information.
155 ******************************************************************************/
156 function sqimap_get_header ($imap_stream, $read) {
157 $hdr = new msg_header();
159 // Set up some defaults
160 $hdr->type0
= "text";
161 $hdr->type1
= "plain";
162 $hdr->charset
= "us-ascii";
164 while ($i < count($read)) {
165 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
170 /** ENCODING TYPE **/
171 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
172 $hdr->encoding
= strtolower(trim(substr($read[$i], 26)));
177 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
178 $cont = strtolower(trim(substr($read[$i], 13)));
179 if (strpos($cont, ";"))
180 $cont = substr($cont, 0, strpos($cont, ";"));
183 if (strpos($cont, "/")) {
184 $hdr->type0
= substr($cont, 0, strpos($cont, "/"));
185 $hdr->type1
= substr($cont, strpos($cont, "/")+
1);
193 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
194 str_replace("\n", "", $line);
195 str_replace("\n", "", $read[$i]);
196 $line = "$line $read[$i]";
200 /** Detect the boundary of a multipart message **/
201 if (eregi("boundary=\"([^\"]+)\"", $line, $regs)) {
202 $hdr->boundary
= $regs[1];
205 /** Detect the charset **/
206 if (strpos(strtolower(trim($line)), "charset=")) {
207 $pos = strpos($line, "charset=") +
8;
208 $charset = trim($line);
209 if (strpos($line, " ", $pos) > 0) {
210 $charset = substr($charset, $pos, strpos($line, " ", $pos));
212 $charset = substr($charset, $pos);
214 $charset = str_replace("\"", "", $charset);
215 $hdr->charset
= $charset;
217 $hdr->charset
= "us-ascii";
222 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
223 /** Add better dontent-disposition support **/
227 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
228 str_replace("\n", "", $line);
229 str_replace("\n", "", $read[$i]);
230 $line = "$line $read[$i]";
234 /** Detects filename if any **/
235 if (strpos(strtolower(trim($line)), "filename=")) {
236 $pos = strpos($line, "filename=") +
9;
238 if (strpos($line, " ", $pos) > 0) {
239 $name = substr($name, $pos, strpos($line, " ", $pos));
241 $name = substr($name, $pos);
243 $name = str_replace("\"", "", $name);
244 $hdr->filename
= $name;
249 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
250 $hdr->replyto
= trim(substr($read[$i], 9, strlen($read[$i])));
255 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
256 $hdr->from
= trim(substr($read[$i], 5, strlen($read[$i]) - 6));
257 if ($hdr->replyto
== "")
258 $hdr->replyto
= $hdr->from
;
262 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
263 $d = substr($read[$i], 5);
265 $d = ereg_replace(" ", " ", $d);
266 $d = explode(" ", $d);
267 $hdr->date
= getTimeStamp($d);
271 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
272 $hdr->subject
= trim(substr($read[$i], 8, strlen($read[$i]) - 9));
273 if (strlen(Chop($hdr->subject
)) == 0)
274 $hdr->subject
= _("(no subject)");
278 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
280 $hdr->cc
[$pos] = trim(substr($read[$i], 4));
282 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
284 $hdr->cc
[$pos] = trim($read[$i]);
289 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
291 $hdr->to
[$pos] = trim(substr($read[$i], 4));
293 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
295 $hdr->to
[$pos] = trim($read[$i]);
300 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
301 $hdr->message_id
= trim(substr($read[$i], 11));
306 /** ERROR CORRECTION **/
307 else if (substr($read[$i], 0, 1) == ")") {
308 if (strlen(trim($hdr->subject
)) == 0)
309 $hdr->subject
= _("(no subject)");
311 if (strlen(trim($hdr->from
)) == 0)
312 $hdr->from
= _("(unknown sender)");
314 if (strlen(trim($hdr->date
)) == 0)
326 /******************************************************************************
327 ** Returns the body of a message.
328 ******************************************************************************/
329 function sqimap_get_message_body ($imap_stream, &$header) {
331 //fputs ($imap_stream, "a001 FETCH $id:$id BODY[TEXT]\r\n");
332 //$read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
337 while ($i < count($read)-1) {
339 $body[$j] = $read[$i];
345 return decodeMime($imap_stream, $body, &$header);
349 /******************************************************************************
350 ** Returns an array with the body structure
351 ******************************************************************************/