Added IMAP speed imrpovements from the guys at XS4ALL
[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 (defined ('imap_messages_php'))
11 return;
12 define ('imap_messages_php', true);
13
14 /******************************************************************************
15 ** Copies specified messages to specified folder
16 ******************************************************************************/
17 function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) {
18 fputs ($imap_stream, "a001 COPY $start:$end \"$mailbox\"\r\n");
19 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
20 }
21
22 /******************************************************************************
23 ** Deletes specified messages and moves them to trash if possible
24 ******************************************************************************/
25 function sqimap_messages_delete ($imap_stream, $start, $end, $mailbox) {
26 global $move_to_trash, $trash_folder, $auto_expunge;
27
28 if (($move_to_trash == true) && (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder))) {
29 sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder);
30 sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
31 } else {
32 sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
33 }
34 }
35
36 /******************************************************************************
37 ** Sets the specified messages with specified flag
38 ******************************************************************************/
39 function sqimap_messages_flag ($imap_stream, $start, $end, $flag) {
40 fputs ($imap_stream, "a001 STORE $start:$end +FLAGS (\\$flag)\r\n");
41 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
42 }
43
44
45 /******************************************************************************
46 ** Remove specified flag from specified messages
47 ******************************************************************************/
48 function sqimap_messages_remove_flag ($imap_stream, $start, $end, $flag) {
49 fputs ($imap_stream, "a001 STORE $start:$end -FLAGS (\\$flag)\r\n");
50 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
51 }
52
53
54 /******************************************************************************
55 ** Returns some general header information -- FROM, DATE, and SUBJECT
56 ******************************************************************************/
57 class small_header {
58 var $from = '', $subject = '', $date = '', $to = '',
59 $priority = 0, $message_id = 0, $cc = '';
60 }
61
62 function sqimap_get_small_header ($imap_stream, $id, $sent) {
63 $res = sqimap_get_small_header_list($imap_stream, array($id), $sent);
64 return $res[0];
65 }
66
67 function sqimap_get_small_header_list ($imap_stream, $msg_list, $issent) {
68
69 /* Get the small headers for each message in $msg_list */
70
71 $maxmsg = sizeof($msg_list);
72 $msgs_str = implode(",", $msg_list);
73 $results = array();
74 $read_list = array();
75 $sizes_list = array();
76
77 /* We need to return the data in the same order as the caller supplied
78 in $msg_list, but IMAP servers are free to return responses in
79 whatever order they wish... So we need to re-sort manually */
80
81 for ($i = 0; $i < sizeof($msg_list); $i++) {
82 $id2index[$msg_list[$i]] = $i;
83 }
84
85 $query = "a001 FETCH $msgs_str BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject Message-Id X-Priority Content-Type)]\r\n";
86 fputs ($imap_stream, $query);
87 $readin_list = sqimap_read_data_list($imap_stream, "a001", true, $response, $message);
88
89 foreach ($readin_list as $r) {
90 if (!eregi("^\\* ([0-9]+) FETCH", $r[0], $regs)) {
91 set_up_language($squirrelmail_language);
92 echo "<br><b><font color=$color[2]>\n";
93 echo _("ERROR : Could not complete request.");
94 echo "</b><br>\n";
95 echo _("Unknown response from IMAP server: ");
96 echo $r[0] . "</font><br>\n";
97 exit;
98 }
99 if (!count($id2index[$regs[1]])) {
100 set_up_language($squirrelmail_language);
101 echo "<br><b><font color=$color[2]>\n";
102 echo _("ERROR : Could not complete request.");
103 echo "</b><br>\n";
104 echo _("Unknown messagenumber in reply from server: ");
105 echo $regs[1] . "</font><br>\n";
106 exit;
107 }
108 $read_list[$id2index[$regs[1]]] = $r;
109 }
110 arsort($read_list);
111
112 $query = "a002 FETCH $msgs_str RFC822.SIZE\r\n";
113 fputs ($imap_stream, $query);
114 $sizesin_list = sqimap_read_data_list($imap_stream, "a002", true, $response, $message);
115
116 foreach ($sizesin_list as $r) {
117 if (!eregi("^\\* ([0-9]+) FETCH", $r[0], $regs)) {
118 set_up_language($squirrelmail_language);
119 echo "<br><b><font color=$color[2]>\n";
120 echo _("ERROR : Could not complete request.");
121 echo "</b><br>\n";
122 echo _("Unknown response from IMAP server: ");
123 echo $r[0] . "</font><br>\n";
124 exit;
125 }
126 if (!count($id2index[$regs[1]])) {
127 set_up_language($squirrelmail_language);
128 echo "<br><b><font color=$color[2]>\n";
129 echo _("ERROR : Could not complete request.");
130 echo "</b><br>\n";
131 echo _("Unknown messagenumber in reply from server: ");
132 echo $regs[1] . "</font><br>\n";
133 exit;
134 }
135 $sizes_list[$id2index[$regs[1]]] = $r;
136 }
137 arsort($sizes_list);
138
139 for ($msgi = 0; $msgi < $maxmsg; $msgi++) {
140 $subject = _("(no subject)");
141 $from = _("Unknown Sender");
142 $priority = 0;
143 $messageid = "<>";
144 $cc = "";
145 $to = "";
146 $date = "";
147 $type[0] = "";
148 $type[1] = "";
149 $read = $read_list[$msgi];
150
151 for ($i = 0; $i < count($read); $i++) {
152 if (eregi ("^to:(.*)$", $read[$i], $regs)) {
153 //$to = sqimap_find_displayable_name(substr($read[$i], 3));
154 $to = $regs[1];
155 } else if (eregi ("^from:(.*)$", $read[$i], $regs)) {
156 //$from = sqimap_find_displayable_name(substr($read[$i], 5));
157 $from = $regs[1];
158 } else if (eregi ("^x-priority:(.*)$", $read[$i], $regs)) {
159 $priority = trim($regs[1]);
160 } else if (eregi ("^message-id:(.*)$", $read[$i], $regs)) {
161 $messageid = trim($regs[1]);
162 } else if (eregi ("^cc:(.*)$", $read[$i], $regs)) {
163 $cc = $regs[1];
164 } else if (eregi ("^date:(.*)$", $read[$i], $regs)) {
165 $date = $regs[1];
166 } else if (eregi ("^subject:(.*)$", $read[$i], $regs)) {
167 $subject = htmlspecialchars(trim($regs[1]));
168 if ($subject == "")
169 $subject = _("(no subject)");
170 } else if (eregi ("^content-type:(.*)$", $read[$i], $regs)) {
171 $type = strtolower(trim($regs[1]));
172 if ($pos = strpos($type, ";"))
173 $type = substr($type, 0, $pos);
174 $type = explode("/", $type);
175 if (! isset($type[1]))
176 $type[1] = '';
177 }
178
179 }
180 if (trim($date) == "") {
181 fputs($imap_stream, "a002 FETCH $msg_list[$msgi] INTERNALDATE\r\n");
182 $readdate = sqimap_read_data($imap_stream, "a002", true, $response, $message);
183 if (eregi(".*INTERNALDATE \"(.*)\".*", $readdate[0], $regs)) {
184 $date_list = explode(" ", trim($regs[1]));
185 $date_list[0] = str_replace("-", " ", $date_list[0]);
186 $date = implode(" ", $date_list);
187 }
188 }
189 eregi("([0-9]+)[^0-9]*$", $sizes_list[$msgi][0], $regs);
190 $size = $regs[1];
191
192 $header = new small_header;
193 if ($issent == true)
194 $header->from = (trim($to) != '')? $to : _("(only Cc/Bcc)");
195 else
196 $header->from = $from;
197
198 $header->date = $date;
199 $header->subject = $subject;
200 $header->to = $to;
201 $header->priority = $priority;
202 $header->message_id = $messageid;
203 $header->cc = $cc;
204 $header->size = $size;
205 $header->type0 = $type[0];
206 $header->type1 = $type[1];
207
208 $result[] = $header;
209 }
210 return $result;
211 }
212
213 /******************************************************************************
214 ** Returns the flags for the specified messages
215 ******************************************************************************/
216 function sqimap_get_flags ($imap_stream, $i) {
217 fputs ($imap_stream, "a001 FETCH $i:$i FLAGS\r\n");
218 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
219 if (ereg("FLAGS(.*)", $read[0], $regs))
220 return explode(" ", trim(ereg_replace('[\\(\\)\\\\]', '', $regs[1])));
221 return Array('None');
222 }
223
224 function sqimap_get_flags_list ($imap_stream, $msg_list) {
225
226 $msgs_str = implode(",", $msg_list);
227 for ($i = 0; $i < sizeof($msg_list); $i++) {
228 $id2index[$msg_list[$i]] = $i;
229 }
230 fputs ($imap_stream, "a001 FETCH $msgs_str FLAGS\r\n");
231 $result_list = sqimap_read_data_list ($imap_stream, "a001", true, $response, $message);
232 $result_flags = array();
233
234 for ($i = 0; $i < sizeof($result_list); $i++) {
235 if (eregi("^\\* ([0-9]+).*FETCH.*FLAGS(.*)", $result_list[$i][0], $regs)
236 && count($id2index[$regs[1]])) {
237 $result_flags[$id2index[$regs[1]]] = explode(" ", trim(ereg_replace('[\\(\\)\\\\]', '', $regs[2])));
238 } else {
239 set_up_language($squirrelmail_language);
240 echo "<br><b><font color=$color[2]>\n";
241 echo _("ERROR : Could not complete request.");
242 echo "</b><br>\n";
243 echo _("Unknown response from IMAP server: ");
244 echo $result_list[$i][0] . "</font><br>\n";
245 exit;
246 }
247 }
248 arsort($result_flags);
249 return $result_flags;
250 }
251
252 /******************************************************************************
253 ** Returns a message array with all the information about a message. See
254 ** the documentation folder for more information about this array.
255 ******************************************************************************/
256 function sqimap_get_message ($imap_stream, $id, $mailbox) {
257 $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
258 return sqimap_get_message_body($imap_stream, $header);
259 }
260
261 /******************************************************************************
262 ** Wrapper function that reformats the header information.
263 ******************************************************************************/
264 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
265 fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\r\n");
266 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
267
268 $header = sqimap_get_header($imap_stream, $read);
269 $header->id = $id;
270 $header->mailbox = $mailbox;
271
272 return $header;
273 }
274
275 /******************************************************************************
276 ** Wrapper function that returns entity headers for use by decodeMime
277 ******************************************************************************/
278 /*
279 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
280 $header = sqimap_get_header($imap_stream, $read);
281 $type0 = $header["TYPE0"];
282 $type1 = $header["TYPE1"];
283 $bound = $header["BOUNDARY"];
284 $encoding = $header["ENCODING"];
285 $charset = $header["CHARSET"];
286 $filename = $header["FILENAME"];
287 }
288 */
289
290 /******************************************************************************
291 ** Queries the IMAP server and gets all header information.
292 ******************************************************************************/
293 function sqimap_get_header ($imap_stream, $read) {
294 global $where, $what;
295
296 $hdr = new msg_header();
297 $i = 0;
298 // Set up some defaults
299 $hdr->type0 = "text";
300 $hdr->type1 = "plain";
301 $hdr->charset = "us-ascii";
302
303 while ($i < count($read)) {
304 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
305 $hdr->mime = true;
306 $i++;
307 }
308
309 /** ENCODING TYPE **/
310 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
311 $hdr->encoding = strtolower(trim(substr($read[$i], 26)));
312 $i++;
313 }
314
315 /** CONTENT-TYPE **/
316 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
317 $cont = strtolower(trim(substr($read[$i], 13)));
318 if (strpos($cont, ";"))
319 $cont = substr($cont, 0, strpos($cont, ";"));
320
321
322 if (strpos($cont, "/")) {
323 $hdr->type0 = substr($cont, 0, strpos($cont, "/"));
324 $hdr->type1 = substr($cont, strpos($cont, "/")+1);
325 } else {
326 $hdr->type0 = $cont;
327 }
328
329
330 $line = $read[$i];
331 $i++;
332 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
333 str_replace("\n", "", $line);
334 str_replace("\n", "", $read[$i]);
335 $line = "$line $read[$i]";
336 $i++;
337 }
338
339 /** Detect the boundary of a multipart message **/
340 if (eregi('boundary="([^"]+)"', $line, $regs)) {
341 $hdr->boundary = $regs[1];
342 }
343
344 /** Detect the charset **/
345 if (strpos(strtolower(trim($line)), "charset=")) {
346 $pos = strpos($line, "charset=") + 8;
347 $charset = trim($line);
348 if (strpos($line, ";", $pos) > 0) {
349 $charset = substr($charset, $pos, strpos($line, ";", $pos)-$pos);
350 } else {
351 $charset = substr($charset, $pos);
352 }
353 $charset = str_replace("\"", "", $charset);
354 $hdr->charset = $charset;
355 } else {
356 $hdr->charset = "us-ascii";
357 }
358
359 }
360
361 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
362 /** Add better dontent-disposition support **/
363
364 $line = $read[$i];
365 $i++;
366 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
367 str_replace("\n", "", $line);
368 str_replace("\n", "", $read[$i]);
369 $line = "$line $read[$i]";
370 $i++;
371 }
372
373 /** Detects filename if any **/
374 if (strpos(strtolower(trim($line)), "filename=")) {
375 $pos = strpos($line, "filename=") + 9;
376 $name = trim($line);
377 if (strpos($line, " ", $pos) > 0) {
378 $name = substr($name, $pos, strpos($line, " ", $pos));
379 } else {
380 $name = substr($name, $pos);
381 }
382 $name = str_replace("\"", "", $name);
383 $hdr->filename = $name;
384 }
385 }
386
387 /** REPLY-TO **/
388 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
389 $hdr->replyto = trim(substr($read[$i], 9, strlen($read[$i])));
390 $i++;
391 }
392
393 /** FROM **/
394 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
395 $hdr->from = trim(substr($read[$i], 5, strlen($read[$i]) - 6));
396 if (! isset($hdr->replyto) || $hdr->replyto == "")
397 $hdr->replyto = $hdr->from;
398 $i++;
399 }
400 /** DATE **/
401 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
402 $d = substr($read[$i], 5);
403 $d = trim($d);
404 $d = strtr($d, array(' ' => ' '));
405 $d = explode(' ', $d);
406 $hdr->date = getTimeStamp($d);
407 $i++;
408 }
409 /** SUBJECT **/
410 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
411 $hdr->subject = trim(substr($read[$i], 8, strlen($read[$i]) - 9));
412 if (strlen(Chop($hdr->subject)) == 0)
413 $hdr->subject = _("(no subject)");
414
415 if ($where == "SUBJECT") {
416 $hdr->subject = eregi_replace($what, "<b>\\0</b>", $hdr->subject);
417 }
418 $i++;
419 }
420 /** CC **/
421 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
422 $pos = 0;
423 $hdr->cc[$pos] = trim(substr($read[$i], 4));
424 $i++;
425 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
426 $pos++;
427 $hdr->cc[$pos] = trim($read[$i]);
428 $i++;
429 }
430 }
431 /** TO **/
432 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
433 $pos = 0;
434 $hdr->to[$pos] = trim(substr($read[$i], 4));
435 $i++;
436 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
437 $pos++;
438 $hdr->to[$pos] = trim($read[$i]);
439 $i++;
440 }
441 }
442 /** MESSAGE ID **/
443 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
444 $hdr->message_id = trim(substr($read[$i], 11));
445 $i++;
446 }
447
448
449 /** ERROR CORRECTION **/
450 else if (substr($read[$i], 0, 1) == ")") {
451 if (strlen(trim($hdr->subject)) == 0)
452 $hdr->subject = _("(no subject)");
453
454 if (strlen(trim($hdr->from)) == 0)
455 $hdr->from = _("(unknown sender)");
456
457 if (strlen(trim($hdr->date)) == 0)
458 $hdr->date = time();
459 $i++;
460 }
461 else {
462 $i++;
463 }
464 }
465 return $hdr;
466 }
467
468
469 /******************************************************************************
470 ** Returns the body of a message.
471 ******************************************************************************/
472 function sqimap_get_message_body ($imap_stream, &$header) {
473 $id = $header->id;
474 return decodeMime($imap_stream, $header);
475 }
476
477
478 /******************************************************************************
479 ** Returns an array with the body structure
480 ******************************************************************************/
481 ?>