If a message has attachments, it is shown in the message index by putting
[squirrelmail.git] / functions / imap_messages.php
... / ...
CommitLineData
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 Content-Type)]\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 } else if (eregi ("^content-type:", $read[$i])) {
78 $type = substr($read[$i], 14);
79 $type = strtolower(trim($type));
80 if ($pos = strpos($type, ";"))
81 $type = substr($type, 0, $pos);
82 $type = explode("/", $type);
83 }
84
85 }
86
87 // If there isn't a date, it takes the internal date and uses
88 // that as the normal date.
89 if (trim($date) == "") {
90 fputs ($imap_stream, "a002 FETCH $id INTERNALDATE\r\n");
91 $internal_read = sqimap_read_data ($imap_stream, "a002", true, $r, $m);
92
93 // * 22 FETCH (INTERNALDATE " 8-Sep-2000 13:17:07 -0500")
94 $date = $internal_read[0];
95 $date = eregi_replace(".*internaldate \"", "", $date);
96 $date = eregi_replace("\".*", "", $date);
97 $date_ary = explode(" ", trim($date));
98 $date_ary[0] = str_replace("-", " ", $date_ary[0]);
99 $date = implode (" ", $date_ary);
100 }
101
102 fputs ($imap_stream, "a003 FETCH $id RFC822.SIZE\r\n");
103 $read = sqimap_read_data($imap_stream, "a003", true, $r, $m);
104 preg_match("/([0-9]+)\)\s*$/i", $read[0], $regs);
105 $size = $regs[1];
106
107 $header = new small_header;
108 if ($sent == true)
109 $header->from = $to;
110 else
111 $header->from = $from;
112
113 $header->date = $date;
114 $header->subject = $subject;
115 $header->to = $to;
116 $header->priority = $priority;
117 $header->message_id = $messageid;
118 $header->cc = $cc;
119 $header->size = $size;
120 $header->type0 = $type[0];
121 $header->type1 = $type[1];
122
123 return $header;
124 }
125
126 /******************************************************************************
127 ** Returns the flags for the specified messages
128 ******************************************************************************/
129 function sqimap_get_flags ($imap_stream, $i) {
130 fputs ($imap_stream, "a001 FETCH $i:$i FLAGS\r\n");
131 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
132 if (strpos($read[0], "FLAGS")) {
133 $tmp = ereg_replace("\(", "", $read[0]);
134 $tmp = ereg_replace("\)", "", $tmp);
135 $tmp = str_replace("\\", "", $tmp);
136 $tmp = substr($tmp, strpos($tmp, "FLAGS")+6, strlen($tmp));
137 $tmp = trim($tmp);
138 $flags = explode(" ", $tmp);
139 } else {
140 $flags[0] = "None";
141 }
142 return $flags;
143 }
144
145 /******************************************************************************
146 ** Returns a message array with all the information about a message. See
147 ** the documentation folder for more information about this array.
148 ******************************************************************************/
149 function sqimap_get_message ($imap_stream, $id, $mailbox) {
150 $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
151 $msg = sqimap_get_message_body($imap_stream, &$header);
152 return $msg;
153 }
154
155 /******************************************************************************
156 ** Wrapper function that reformats the header information.
157 ******************************************************************************/
158 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
159 fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\r\n");
160 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
161
162 $header = sqimap_get_header($imap_stream, $read);
163 $header->id = $id;
164 $header->mailbox = $mailbox;
165
166 return $header;
167 }
168
169 /******************************************************************************
170 ** Wrapper function that returns entity headers for use by decodeMime
171 ******************************************************************************/
172 /*
173 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
174 $header = sqimap_get_header($imap_stream, $read);
175 $type0 = $header["TYPE0"];
176 $type1 = $header["TYPE1"];
177 $bound = $header["BOUNDARY"];
178 $encoding = $header["ENCODING"];
179 $charset = $header["CHARSET"];
180 $filename = $header["FILENAME"];
181 }
182 */
183
184 /******************************************************************************
185 ** Queries the IMAP server and gets all header information.
186 ******************************************************************************/
187 function sqimap_get_header ($imap_stream, $read) {
188 global $where, $what;
189
190 $hdr = new msg_header();
191 $i = 0;
192 // Set up some defaults
193 $hdr->type0 = "text";
194 $hdr->type1 = "plain";
195 $hdr->charset = "us-ascii";
196
197 preg_match("/\{([0-9]+)\}/", $read[$i], $regs);
198 preg_match("/[0-9]+/", $regs[0], $regs);
199
200 while ($i < count($read)) {
201 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
202 $hdr->mime = true;
203 $i++;
204 }
205
206 /** ENCODING TYPE **/
207 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
208 $hdr->encoding = strtolower(trim(substr($read[$i], 26)));
209 $i++;
210 }
211
212 /** CONTENT-TYPE **/
213 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
214 $cont = strtolower(trim(substr($read[$i], 13)));
215 if (strpos($cont, ";"))
216 $cont = substr($cont, 0, strpos($cont, ";"));
217
218
219 if (strpos($cont, "/")) {
220 $hdr->type0 = substr($cont, 0, strpos($cont, "/"));
221 $hdr->type1 = substr($cont, strpos($cont, "/")+1);
222 } else {
223 $hdr->type0 = $cont;
224 }
225
226
227 $line = $read[$i];
228 $i++;
229 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
230 str_replace("\n", "", $line);
231 str_replace("\n", "", $read[$i]);
232 $line = "$line $read[$i]";
233 $i++;
234 }
235
236 /** Detect the boundary of a multipart message **/
237 if (eregi("boundary=\"([^\"]+)\"", $line, $regs)) {
238 $hdr->boundary = $regs[1];
239 }
240
241 /** Detect the charset **/
242 if (strpos(strtolower(trim($line)), "charset=")) {
243 $pos = strpos($line, "charset=") + 8;
244 $charset = trim($line);
245 if (strpos($line, " ", $pos) > 0) {
246 $charset = substr($charset, $pos, strpos($line, " ", $pos));
247 } else {
248 $charset = substr($charset, $pos);
249 }
250 $charset = str_replace("\"", "", $charset);
251 $hdr->charset = $charset;
252 } else {
253 $hdr->charset = "us-ascii";
254 }
255
256 }
257
258 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
259 /** Add better dontent-disposition support **/
260
261 $line = $read[$i];
262 $i++;
263 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
264 str_replace("\n", "", $line);
265 str_replace("\n", "", $read[$i]);
266 $line = "$line $read[$i]";
267 $i++;
268 }
269
270 /** Detects filename if any **/
271 if (strpos(strtolower(trim($line)), "filename=")) {
272 $pos = strpos($line, "filename=") + 9;
273 $name = trim($line);
274 if (strpos($line, " ", $pos) > 0) {
275 $name = substr($name, $pos, strpos($line, " ", $pos));
276 } else {
277 $name = substr($name, $pos);
278 }
279 $name = str_replace("\"", "", $name);
280 $hdr->filename = $name;
281 }
282 }
283
284 /** REPLY-TO **/
285 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
286 $hdr->replyto = trim(substr($read[$i], 9, strlen($read[$i])));
287 $i++;
288 }
289
290 /** FROM **/
291 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
292 $hdr->from = trim(substr($read[$i], 5, strlen($read[$i]) - 6));
293 if ($hdr->replyto == "")
294 $hdr->replyto = $hdr->from;
295 $i++;
296 }
297 /** DATE **/
298 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
299 $d = substr($read[$i], 5);
300 $d = trim($d);
301 $d = ereg_replace(" ", " ", $d);
302 $d = explode(" ", $d);
303 $hdr->date = getTimeStamp($d);
304 $i++;
305 }
306 /** SUBJECT **/
307 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
308 $hdr->subject = trim(substr($read[$i], 8, strlen($read[$i]) - 9));
309 if (strlen(Chop($hdr->subject)) == 0)
310 $hdr->subject = _("(no subject)");
311
312 if ($where == "SUBJECT") {
313 $hdr->subject = eregi_replace($what, "<b>\\0</b>", $hdr->subject);
314 }
315 $i++;
316 }
317 /** CC **/
318 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
319 $pos = 0;
320 $hdr->cc[$pos] = trim(substr($read[$i], 4));
321 $i++;
322 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
323 $pos++;
324 $hdr->cc[$pos] = trim($read[$i]);
325 $i++;
326 }
327 }
328 /** TO **/
329 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
330 $pos = 0;
331 $hdr->to[$pos] = trim(substr($read[$i], 4));
332 $i++;
333 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
334 $pos++;
335 $hdr->to[$pos] = trim($read[$i]);
336 $i++;
337 }
338 }
339 /** MESSAGE ID **/
340 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
341 $hdr->message_id = trim(substr($read[$i], 11));
342 $i++;
343 }
344
345
346 /** ERROR CORRECTION **/
347 else if (substr($read[$i], 0, 1) == ")") {
348 if (strlen(trim($hdr->subject)) == 0)
349 $hdr->subject = _("(no subject)");
350
351 if (strlen(trim($hdr->from)) == 0)
352 $hdr->from = _("(unknown sender)");
353
354 if (strlen(trim($hdr->date)) == 0)
355 $hdr->date = time();
356 $i++;
357 }
358 else {
359 $i++;
360 }
361 }
362 return $hdr;
363 }
364
365
366 /******************************************************************************
367 ** Returns the body of a message.
368 ******************************************************************************/
369 function sqimap_get_message_body ($imap_stream, &$header) {
370 $id = $header->id;
371 return decodeMime($imap_stream, $body, &$header);
372 }
373
374
375 /******************************************************************************
376 ** Returns an array with the body structure
377 ******************************************************************************/
378?>