fe7e7cbcfb7975351f8e02cf79ea9cf5ad85ca7e
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) {
50 fputs ($imap_stream, "a001 FETCH $id BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject Message-Id X-Priority)]\r\n");
51 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
53 $subject = _("(no subject)");
54 $from = _("Unknown Sender");
59 for ($i = 0; $i < count($read); $i++
) {
60 if (eregi ("^to:", $read[$i])) {
61 //$to = sqimap_find_displayable_name(substr($read[$i], 3));
62 $to = substr($read[$i], 3);
63 } else if (eregi ("^from:", $read[$i])) {
64 //$from = sqimap_find_displayable_name(substr($read[$i], 5));
65 $from = substr($read[$i], 5);
66 } else if (eregi ("^x-priority:", $read[$i])) {
67 $priority = trim(substr($read[$i], 11));
68 } else if (eregi ("^message-id:", $read[$i])) {
69 $messageid = trim(substr($read[$i], 11));
70 } else if (eregi ("^cc:", $read[$i])) {
71 $cc = substr($read[$i], 3);
72 } else if (eregi ("^date:", $read[$i])) {
73 $date = substr($read[$i], 5);
74 } else if (eregi ("^subject:", $read[$i])) {
75 $subject = htmlspecialchars(eregi_replace ("^subject: ", "", $read[$i]));
76 if (trim($subject) == "")
77 $subject = _("(no subject)");
81 if ($where == "SUBJECT") {
82 $subject = eregi_replace($what, "<b>\\0</b>", $subject);
85 $header = new small_header
;
89 $header->from
= $from;
91 $header->date
= $date;
92 $header->subject
= $subject;
94 $header->priority
= $priority;
95 $header->message_id
= $messageid;
101 /******************************************************************************
102 ** Returns the flags for the specified messages
103 ******************************************************************************/
104 function sqimap_get_flags ($imap_stream, $i) {
105 fputs ($imap_stream, "a001 FETCH $i:$i FLAGS\r\n");
106 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
107 if (strpos($read[0], "FLAGS")) {
108 $tmp = ereg_replace("\(", "", $read[0]);
109 $tmp = ereg_replace("\)", "", $tmp);
110 $tmp = str_replace("\\", "", $tmp);
111 $tmp = substr($tmp, strpos($tmp, "FLAGS")+
6, strlen($tmp));
113 $flags = explode(" ", $tmp);
120 /******************************************************************************
121 ** Returns a message array with all the information about a message. See
122 ** the documentation folder for more information about this array.
123 ******************************************************************************/
124 function sqimap_get_message ($imap_stream, $id, $mailbox) {
125 $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
126 $msg = sqimap_get_message_body($imap_stream, &$header);
130 /******************************************************************************
131 ** Wrapper function that reformats the header information.
132 ******************************************************************************/
133 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
134 fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\r\n");
135 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
137 $header = sqimap_get_header($imap_stream, $read);
139 $header->mailbox
= $mailbox;
144 /******************************************************************************
145 ** Wrapper function that returns entity headers for use by decodeMime
146 ******************************************************************************/
148 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
149 $header = sqimap_get_header($imap_stream, $read);
150 $type0 = $header["TYPE0"];
151 $type1 = $header["TYPE1"];
152 $bound = $header["BOUNDARY"];
153 $encoding = $header["ENCODING"];
154 $charset = $header["CHARSET"];
155 $filename = $header["FILENAME"];
159 /******************************************************************************
160 ** Queries the IMAP server and gets all header information.
161 ******************************************************************************/
162 function sqimap_get_header ($imap_stream, $read) {
163 global $where, $what;
165 $hdr = new msg_header();
167 // Set up some defaults
168 $hdr->type0
= "text";
169 $hdr->type1
= "plain";
170 $hdr->charset
= "us-ascii";
172 while ($i < count($read)) {
173 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
178 /** ENCODING TYPE **/
179 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
180 $hdr->encoding
= strtolower(trim(substr($read[$i], 26)));
185 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
186 $cont = strtolower(trim(substr($read[$i], 13)));
187 if (strpos($cont, ";"))
188 $cont = substr($cont, 0, strpos($cont, ";"));
191 if (strpos($cont, "/")) {
192 $hdr->type0
= substr($cont, 0, strpos($cont, "/"));
193 $hdr->type1
= substr($cont, strpos($cont, "/")+
1);
201 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
202 str_replace("\n", "", $line);
203 str_replace("\n", "", $read[$i]);
204 $line = "$line $read[$i]";
208 /** Detect the boundary of a multipart message **/
209 if (eregi("boundary=\"([^\"]+)\"", $line, $regs)) {
210 $hdr->boundary
= $regs[1];
213 /** Detect the charset **/
214 if (strpos(strtolower(trim($line)), "charset=")) {
215 $pos = strpos($line, "charset=") +
8;
216 $charset = trim($line);
217 if (strpos($line, " ", $pos) > 0) {
218 $charset = substr($charset, $pos, strpos($line, " ", $pos));
220 $charset = substr($charset, $pos);
222 $charset = str_replace("\"", "", $charset);
223 $hdr->charset
= $charset;
225 $hdr->charset
= "us-ascii";
230 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
231 /** Add better dontent-disposition support **/
235 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
236 str_replace("\n", "", $line);
237 str_replace("\n", "", $read[$i]);
238 $line = "$line $read[$i]";
242 /** Detects filename if any **/
243 if (strpos(strtolower(trim($line)), "filename=")) {
244 $pos = strpos($line, "filename=") +
9;
246 if (strpos($line, " ", $pos) > 0) {
247 $name = substr($name, $pos, strpos($line, " ", $pos));
249 $name = substr($name, $pos);
251 $name = str_replace("\"", "", $name);
252 $hdr->filename
= $name;
257 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
258 $hdr->replyto
= trim(substr($read[$i], 9, strlen($read[$i])));
263 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
264 $hdr->from
= trim(substr($read[$i], 5, strlen($read[$i]) - 6));
265 if ($hdr->replyto
== "")
266 $hdr->replyto
= $hdr->from
;
270 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
271 $d = substr($read[$i], 5);
273 $d = ereg_replace(" ", " ", $d);
274 $d = explode(" ", $d);
275 $hdr->date
= getTimeStamp($d);
279 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
280 $hdr->subject
= trim(substr($read[$i], 8, strlen($read[$i]) - 9));
281 if (strlen(Chop($hdr->subject
)) == 0)
282 $hdr->subject
= _("(no subject)");
284 if ($where == "SUBJECT") {
285 $hdr->subject
= eregi_replace($what, "<b>\\0</b>", $hdr->subject
);
290 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
292 $hdr->cc
[$pos] = trim(substr($read[$i], 4));
294 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
296 $hdr->cc
[$pos] = trim($read[$i]);
301 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
303 $hdr->to
[$pos] = trim(substr($read[$i], 4));
305 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
307 $hdr->to
[$pos] = trim($read[$i]);
312 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
313 $hdr->message_id
= trim(substr($read[$i], 11));
318 /** ERROR CORRECTION **/
319 else if (substr($read[$i], 0, 1) == ")") {
320 if (strlen(trim($hdr->subject
)) == 0)
321 $hdr->subject
= _("(no subject)");
323 if (strlen(trim($hdr->from
)) == 0)
324 $hdr->from
= _("(unknown sender)");
326 if (strlen(trim($hdr->date
)) == 0)
338 /******************************************************************************
339 ** Returns the body of a message.
340 ******************************************************************************/
341 function sqimap_get_message_body ($imap_stream, &$header) {
343 return decodeMime($imap_stream, $body, &$header);
347 /******************************************************************************
348 ** Returns an array with the body structure
349 ******************************************************************************/