123b4e560b965902883cc077d74ce2f2c0738384
[squirrelmail.git] / functions / imap_messages.php
1 <?php
2 /**
3 ** imap_messages.php
4 **
5 ** This implements functions that manipulate messages
6 **
7 ** $Id$
8 **/
9
10 if (!isset($mime_php)) include "../functions/mime.php";
11
12 /******************************************************************************
13 ** Copies specified messages to specified folder
14 ******************************************************************************/
15 function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) {
16 fputs ($imap_stream, "a001 COPY $start:$end \"$mailbox\"\r\n");
17 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
18 }
19
20 /******************************************************************************
21 ** Deletes specified messages and moves them to trash if possible
22 ******************************************************************************/
23 function sqimap_messages_delete ($imap_stream, $start, $end, $mailbox) {
24 global $move_to_trash, $trash_folder, $auto_expunge;
25
26 if (($move_to_trash == true) && (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder))) {
27 sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder);
28 sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
29 } else {
30 sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
31 }
32 }
33
34 /******************************************************************************
35 ** Sets the specified messages with specified flag
36 ******************************************************************************/
37 function sqimap_messages_flag ($imap_stream, $start, $end, $flag) {
38 fputs ($imap_stream, "a001 STORE $start:$end +FLAGS (\\$flag)\r\n");
39 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
40 }
41
42
43 /******************************************************************************
44 ** Remove specified flag from specified messages
45 ******************************************************************************/
46 function sqimap_messages_remove_flag ($imap_stream, $start, $end, $flag) {
47 fputs ($imap_stream, "a001 STORE $start:$end -FLAGS (\\$flag)\r\n");
48 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
49 }
50
51
52 /******************************************************************************
53 ** Returns some general header information -- FROM, DATE, and SUBJECT
54 ******************************************************************************/
55 class small_header {
56 var $from = '', $subject = '', $date = '', $to = '',
57 $priority = 0, $message_id = 0, $cc = '';
58 }
59
60 function sqimap_get_small_header ($imap_stream, $id, $sent) {
61
62 fputs ($imap_stream, "a001 FETCH $id BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject Message-Id X-Priority Content-Type)]\r\n");
63 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
64
65 $subject = _("(no subject)");
66 $from = _("Unknown Sender");
67 $priority = "0";
68 $messageid = "<>";
69 $cc = "";
70 $to = "";
71 $date = "";
72 $type[0] = "";
73 $type[1] = "";
74
75 $g = 0;
76 for ($i = 0; $i < count($read); $i++) {
77 if (eregi ("^to:(.*)$", $read[$i], $regs)) {
78 //$to = sqimap_find_displayable_name(substr($read[$i], 3));
79 $to = $regs[1];
80 } else if (eregi ("^from:(.*)$", $read[$i], $regs)) {
81 //$from = sqimap_find_displayable_name(substr($read[$i], 5));
82 $from = $regs[1];
83 } else if (eregi ("^x-priority:(.*)$", $read[$i], $regs)) {
84 $priority = trim($regs[1]);
85 } else if (eregi ("^message-id:(.*)$", $read[$i], $regs)) {
86 $messageid = trim($regs[1]);
87 } else if (eregi ("^cc:(.*)$", $read[$i], $regs)) {
88 $cc = $regs[1];
89 } else if (eregi ("^date:(.*)$", $read[$i], $regs)) {
90 $date = $regs[1];
91 } else if (eregi ("^subject:(.*)$", $read[$i], $regs)) {
92 $subject = htmlspecialchars(trim($regs[1]));
93 if ($subject == "")
94 $subject = _("(no subject)");
95 } else if (eregi ("^content-type:(.*)$", $read[$i], $regs)) {
96 $type = strtolower(trim($regs[1]));
97 if ($pos = strpos($type, ";"))
98 $type = substr($type, 0, $pos);
99 $type = explode("/", $type);
100 if (! isset($type[1]))
101 $type[1] = '';
102 }
103
104 }
105
106 // If there isn't a date, it takes the internal date and uses
107 // that as the normal date.
108 if (trim($date) == "") {
109 fputs ($imap_stream, "a002 FETCH $id INTERNALDATE\r\n");
110 $internal_read = sqimap_read_data ($imap_stream, "a002", true, $r, $m);
111
112 // * 22 FETCH (INTERNALDATE " 8-Sep-2000 13:17:07 -0500")
113 $date = $internal_read[0];
114 $date = eregi_replace(".*internaldate \"", "", $date);
115 $date = eregi_replace("\".*", "", $date);
116 $date_ary = explode(" ", trim($date));
117 $date_ary[0] = str_replace("-", " ", $date_ary[0]);
118 $date = implode (" ", $date_ary);
119 }
120
121 fputs ($imap_stream, "a003 FETCH $id RFC822.SIZE\r\n");
122 $read = sqimap_read_data($imap_stream, "a003", true, $r, $m);
123 eregi("([0-9]+)[^0-9]*$", $read[0], $regs);
124 $size = $regs[1];
125
126 $header = new small_header;
127 if ($sent == true)
128 $header->from = (trim($to) != '')? $to : _("(only Cc/Bcc)");
129 else
130 $header->from = $from;
131
132 $header->date = $date;
133 $header->subject = $subject;
134 $header->to = $to;
135 $header->priority = $priority;
136 $header->message_id = $messageid;
137 $header->cc = $cc;
138 $header->size = $size;
139 $header->type0 = $type[0];
140 $header->type1 = $type[1];
141
142 return $header;
143 }
144
145 /******************************************************************************
146 ** Returns the flags for the specified messages
147 ******************************************************************************/
148 function sqimap_get_flags ($imap_stream, $i) {
149 fputs ($imap_stream, "a001 FETCH $i:$i FLAGS\r\n");
150 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
151 if (ereg("FLAGS(.*)", $read[0], $regs))
152 return explode(" ", trim(ereg_replace('[\\(\\)\\\\]', '', $regs[1])));
153 return Array('None');
154 }
155
156 /******************************************************************************
157 ** Returns a message array with all the information about a message. See
158 ** the documentation folder for more information about this array.
159 ******************************************************************************/
160 function sqimap_get_message ($imap_stream, $id, $mailbox) {
161 $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
162 return sqimap_get_message_body($imap_stream, $header);
163 }
164
165 /******************************************************************************
166 ** Wrapper function that reformats the header information.
167 ******************************************************************************/
168 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
169 fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\r\n");
170 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
171
172 $header = sqimap_get_header($imap_stream, $read);
173 $header->id = $id;
174 $header->mailbox = $mailbox;
175
176 return $header;
177 }
178
179 /******************************************************************************
180 ** Wrapper function that returns entity headers for use by decodeMime
181 ******************************************************************************/
182 /*
183 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
184 $header = sqimap_get_header($imap_stream, $read);
185 $type0 = $header["TYPE0"];
186 $type1 = $header["TYPE1"];
187 $bound = $header["BOUNDARY"];
188 $encoding = $header["ENCODING"];
189 $charset = $header["CHARSET"];
190 $filename = $header["FILENAME"];
191 }
192 */
193
194 /******************************************************************************
195 ** Queries the IMAP server and gets all header information.
196 ******************************************************************************/
197 function sqimap_get_header ($imap_stream, $read) {
198 global $where, $what;
199
200 $hdr = new msg_header();
201 $i = 0;
202 // Set up some defaults
203 $hdr->type0 = "text";
204 $hdr->type1 = "plain";
205 $hdr->charset = "us-ascii";
206
207 while ($i < count($read)) {
208 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
209 $hdr->mime = true;
210 $i++;
211 }
212
213 /** ENCODING TYPE **/
214 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
215 $hdr->encoding = strtolower(trim(substr($read[$i], 26)));
216 $i++;
217 }
218
219 /** CONTENT-TYPE **/
220 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
221 $cont = strtolower(trim(substr($read[$i], 13)));
222 if (strpos($cont, ";"))
223 $cont = substr($cont, 0, strpos($cont, ";"));
224
225
226 if (strpos($cont, "/")) {
227 $hdr->type0 = substr($cont, 0, strpos($cont, "/"));
228 $hdr->type1 = substr($cont, strpos($cont, "/")+1);
229 } else {
230 $hdr->type0 = $cont;
231 }
232
233
234 $line = $read[$i];
235 $i++;
236 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
237 str_replace("\n", "", $line);
238 str_replace("\n", "", $read[$i]);
239 $line = "$line $read[$i]";
240 $i++;
241 }
242
243 /** Detect the boundary of a multipart message **/
244 if (eregi('boundary="([^"]+)"', $line, $regs)) {
245 $hdr->boundary = $regs[1];
246 }
247
248 /** Detect the charset **/
249 if (strpos(strtolower(trim($line)), "charset=")) {
250 $pos = strpos($line, "charset=") + 8;
251 $charset = trim($line);
252 if (strpos($line, ";", $pos) > 0) {
253 $charset = substr($charset, $pos, strpos($line, ";", $pos)-$pos);
254 } else {
255 $charset = substr($charset, $pos);
256 }
257 $charset = str_replace("\"", "", $charset);
258 $hdr->charset = $charset;
259 } else {
260 $hdr->charset = "us-ascii";
261 }
262
263 }
264
265 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
266 /** Add better dontent-disposition support **/
267
268 $line = $read[$i];
269 $i++;
270 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
271 str_replace("\n", "", $line);
272 str_replace("\n", "", $read[$i]);
273 $line = "$line $read[$i]";
274 $i++;
275 }
276
277 /** Detects filename if any **/
278 if (strpos(strtolower(trim($line)), "filename=")) {
279 $pos = strpos($line, "filename=") + 9;
280 $name = trim($line);
281 if (strpos($line, " ", $pos) > 0) {
282 $name = substr($name, $pos, strpos($line, " ", $pos));
283 } else {
284 $name = substr($name, $pos);
285 }
286 $name = str_replace("\"", "", $name);
287 $hdr->filename = $name;
288 }
289 }
290
291 /** REPLY-TO **/
292 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
293 $hdr->replyto = trim(substr($read[$i], 9, strlen($read[$i])));
294 $i++;
295 }
296
297 /** FROM **/
298 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
299 $hdr->from = trim(substr($read[$i], 5, strlen($read[$i]) - 6));
300 if (! isset($hdr->replyto) || $hdr->replyto == "")
301 $hdr->replyto = $hdr->from;
302 $i++;
303 }
304 /** DATE **/
305 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
306 $d = substr($read[$i], 5);
307 $d = trim($d);
308 $d = strtr($d, array(' ' => ' '));
309 $d = explode(' ', $d);
310 $hdr->date = getTimeStamp($d);
311 $i++;
312 }
313 /** SUBJECT **/
314 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
315 $hdr->subject = trim(substr($read[$i], 8, strlen($read[$i]) - 9));
316 if (strlen(Chop($hdr->subject)) == 0)
317 $hdr->subject = _("(no subject)");
318
319 if ($where == "SUBJECT") {
320 $hdr->subject = eregi_replace($what, "<b>\\0</b>", $hdr->subject);
321 }
322 $i++;
323 }
324 /** CC **/
325 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
326 $pos = 0;
327 $hdr->cc[$pos] = trim(substr($read[$i], 4));
328 $i++;
329 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
330 $pos++;
331 $hdr->cc[$pos] = trim($read[$i]);
332 $i++;
333 }
334 }
335 /** TO **/
336 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
337 $pos = 0;
338 $hdr->to[$pos] = trim(substr($read[$i], 4));
339 $i++;
340 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
341 $pos++;
342 $hdr->to[$pos] = trim($read[$i]);
343 $i++;
344 }
345 }
346 /** MESSAGE ID **/
347 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
348 $hdr->message_id = trim(substr($read[$i], 11));
349 $i++;
350 }
351
352
353 /** ERROR CORRECTION **/
354 else if (substr($read[$i], 0, 1) == ")") {
355 if (strlen(trim($hdr->subject)) == 0)
356 $hdr->subject = _("(no subject)");
357
358 if (strlen(trim($hdr->from)) == 0)
359 $hdr->from = _("(unknown sender)");
360
361 if (strlen(trim($hdr->date)) == 0)
362 $hdr->date = time();
363 $i++;
364 }
365 else {
366 $i++;
367 }
368 }
369 return $hdr;
370 }
371
372
373 /******************************************************************************
374 ** Returns the body of a message.
375 ******************************************************************************/
376 function sqimap_get_message_body ($imap_stream, &$header) {
377 $id = $header->id;
378 return decodeMime($imap_stream, $header);
379 }
380
381
382 /******************************************************************************
383 ** Returns an array with the body structure
384 ******************************************************************************/
385 ?>