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