Delete in Trash does Purge. OndraS.
[squirrelmail.git] / functions / imap_messages.php
1 <?php
2 /**
3 ** imap_messages.php
4 **
5 ** This implements functions that manipulate messages
6 **/
7
8 if (!isset($mime_php)) include "../functions/mime.php";
9
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);
16 }
17
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;
23
24 if (($move_to_trash == true) && (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder))) {
25 sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder);
26 sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
27 } else {
28 sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
29 }
30 }
31
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);
38 }
39
40 /******************************************************************************
41 ** Returns some general header information -- FROM, DATE, and SUBJECT
42 ******************************************************************************/
43 class small_header {
44 var $from, $subject, $date, $to, $priority, $message_id;
45 }
46
47 function sqimap_get_small_header ($imap_stream, $id, $sent) {
48
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);
51
52 $subject = _("(no subject)");
53 $from = _("Unknown Sender");
54 $priority = "0";
55 $messageid = "<>";
56
57 $g = 0;
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)");
77 }
78 }
79
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);
85
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);
93 }
94
95 $header = new small_header;
96 if ($sent == true)
97 $header->from = $to;
98 else
99 $header->from = $from;
100
101 $header->date = $date;
102 $header->subject = $subject;
103 $header->to = $to;
104 $header->priority = $priority;
105 $header->message_id = $messageid;
106 $header->cc = $cc;
107
108 return $header;
109 }
110
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));
122 $tmp = trim($tmp);
123 $flags = explode(" ", $tmp);
124 } else {
125 $flags[0] = "None";
126 }
127 return $flags;
128 }
129
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);
137 return $msg;
138 }
139
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);
146
147 $header = sqimap_get_header($imap_stream, $read);
148 $header->id = $id;
149 $header->mailbox = $mailbox;
150
151 return $header;
152 }
153
154 /******************************************************************************
155 ** Wrapper function that returns entity headers for use by decodeMime
156 ******************************************************************************/
157 /*
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"];
166 }
167 */
168
169 /******************************************************************************
170 ** Queries the IMAP server and gets all header information.
171 ******************************************************************************/
172 function sqimap_get_header ($imap_stream, $read) {
173 global $where, $what;
174
175 $hdr = new msg_header();
176 $i = 0;
177 // Set up some defaults
178 $hdr->type0 = "text";
179 $hdr->type1 = "plain";
180 $hdr->charset = "us-ascii";
181
182 while ($i < count($read)) {
183 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
184 $hdr->mime = true;
185 $i++;
186 }
187
188 /** ENCODING TYPE **/
189 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
190 $hdr->encoding = strtolower(trim(substr($read[$i], 26)));
191 $i++;
192 }
193
194 /** CONTENT-TYPE **/
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, ";"));
199
200
201 if (strpos($cont, "/")) {
202 $hdr->type0 = substr($cont, 0, strpos($cont, "/"));
203 $hdr->type1 = substr($cont, strpos($cont, "/")+1);
204 } else {
205 $hdr->type0 = $cont;
206 }
207
208
209 $line = $read[$i];
210 $i++;
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]";
215 $i++;
216 }
217
218 /** Detect the boundary of a multipart message **/
219 if (eregi("boundary=\"([^\"]+)\"", $line, $regs)) {
220 $hdr->boundary = $regs[1];
221 }
222
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));
229 } else {
230 $charset = substr($charset, $pos);
231 }
232 $charset = str_replace("\"", "", $charset);
233 $hdr->charset = $charset;
234 } else {
235 $hdr->charset = "us-ascii";
236 }
237
238 }
239
240 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
241 /** Add better dontent-disposition support **/
242
243 $line = $read[$i];
244 $i++;
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]";
249 $i++;
250 }
251
252 /** Detects filename if any **/
253 if (strpos(strtolower(trim($line)), "filename=")) {
254 $pos = strpos($line, "filename=") + 9;
255 $name = trim($line);
256 if (strpos($line, " ", $pos) > 0) {
257 $name = substr($name, $pos, strpos($line, " ", $pos));
258 } else {
259 $name = substr($name, $pos);
260 }
261 $name = str_replace("\"", "", $name);
262 $hdr->filename = $name;
263 }
264 }
265
266 /** REPLY-TO **/
267 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
268 $hdr->replyto = trim(substr($read[$i], 9, strlen($read[$i])));
269 $i++;
270 }
271
272 /** FROM **/
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;
277 $i++;
278 }
279 /** DATE **/
280 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
281 $d = substr($read[$i], 5);
282 $d = trim($d);
283 $d = ereg_replace(" ", " ", $d);
284 $d = explode(" ", $d);
285 $hdr->date = getTimeStamp($d);
286 $i++;
287 }
288 /** SUBJECT **/
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)");
293
294 if ($where == "SUBJECT") {
295 $hdr->subject = eregi_replace($what, "<b>\\0</b>", $hdr->subject);
296 }
297 $i++;
298 }
299 /** CC **/
300 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
301 $pos = 0;
302 $hdr->cc[$pos] = trim(substr($read[$i], 4));
303 $i++;
304 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
305 $pos++;
306 $hdr->cc[$pos] = trim($read[$i]);
307 $i++;
308 }
309 }
310 /** TO **/
311 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
312 $pos = 0;
313 $hdr->to[$pos] = trim(substr($read[$i], 4));
314 $i++;
315 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
316 $pos++;
317 $hdr->to[$pos] = trim($read[$i]);
318 $i++;
319 }
320 }
321 /** MESSAGE ID **/
322 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
323 $hdr->message_id = trim(substr($read[$i], 11));
324 $i++;
325 }
326
327
328 /** ERROR CORRECTION **/
329 else if (substr($read[$i], 0, 1) == ")") {
330 if (strlen(trim($hdr->subject)) == 0)
331 $hdr->subject = _("(no subject)");
332
333 if (strlen(trim($hdr->from)) == 0)
334 $hdr->from = _("(unknown sender)");
335
336 if (strlen(trim($hdr->date)) == 0)
337 $hdr->date = time();
338 $i++;
339 }
340 else {
341 $i++;
342 }
343 }
344 return $hdr;
345 }
346
347
348 /******************************************************************************
349 ** Returns the body of a message.
350 ******************************************************************************/
351 function sqimap_get_message_body ($imap_stream, &$header) {
352 $id = $header->id;
353 return decodeMime($imap_stream, $body, &$header);
354 }
355
356
357 /******************************************************************************
358 ** Returns an array with the body structure
359 ******************************************************************************/
360 ?>