Sent dumbfolders and special folders fixed for Cyrus. (Philippe Mingo
[squirrelmail.git] / functions / imap_mailbox.php
CommitLineData
59177427 1<?php
bccadd02 2
35586184 3/**
4 * imap_mailbox.php
5 *
15e6162e 6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This impliments all functions that manipulate mailboxes
10 *
11 * $Id$
12 */
f435778e 13
90de1755 14/*
15 Defines Special Mail Boxes
16*/
17
18function isSpecialMailbox( $box ) {
19
20 global $trash_folder, $sent_folder, $draft_folder,
21 $move_to_trash, $move_to_sent, $save_as_draft,
72b9aff9 22 $delimiter, $folder_prefix, $imap_server_type;
90de1755 23
7980d569 24 if ( $move_to_sent ) {
72b9aff9 25 if ( $imap_server_type == 'uw' ) {
26 $i = strpos( $sent_folder, $delimiter, strlen( $folder_prefix ) );
27 if ( $i === FALSE ) {
28 $i = strlen( $box );
29 }
30 } else {
4d6bd355 31 $i = strlen( $box );
32 }
7980d569 33 }
72b9aff9 34
90de1755 35 $ret = ( (strtolower($box) == 'inbox') ||
36 ($box == $trash_folder &&
37 $move_to_trash) ||
dc7a6d34 38 ( substr( $sent_folder, 0, $i ) == substr( $box, 0, $i ) &&
90de1755 39 $move_to_sent) ||
40 ($box == $draft_folder &&
41 $save_as_draft) );
42
43 return( $ret );
44
45}
46
35586184 47/*************************
48 ** Expunges a mailbox **
49 *************************/
90de1755 50function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = TRUE)
43b698c7 51{
1c72b151 52 $read = sqimap_run_command($imap_stream, 'EXPUNGE',
53 $handle_errors, $response, $message);
43b698c7 54}
55
56
57/******************************************************************************
58 ** Checks whether or not the specified mailbox exists
59 ******************************************************************************/
60function sqimap_mailbox_exists ($imap_stream, $mailbox)
61{
62 if (! isset($mailbox)) {
63 return false;
64 }
1c72b151 65 $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
90de1755 66 TRUE, $response, $message);
43b698c7 67 return isset($mbx[0]);
68}
69
70/******************************************************************************
71 ** Selects a mailbox
72 ******************************************************************************/
73function sqimap_mailbox_select ($imap_stream, $mailbox,
90de1755 74 $hide=TRUE, $recent=false)
43b698c7 75{
76 global $auto_expunge;
77
78 if ( $mailbox == 'None' ) {
79 return;
80 }
81
1c72b151 82 $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
90de1755 83 TRUE, $response, $message);
43b698c7 84 if ($recent) {
85 for ($i=0; $i<count($read); $i++) {
bccadd02 86 if (strpos(strtolower($read[$i]), 'recent')) {
43b698c7 87 $r = explode(' ', $read[$i]);
04632dbc 88 }
43b698c7 89 }
90 return $r[1];
91 }
92 if ($auto_expunge) {
1a16af11 93 $tmp = sqimap_run_command($imap_stream, 'EXPUNGE',
1c72b151 94 false, $a, $b);
43b698c7 95 }
96}
97
98
99
100/******************************************************************************
101 ** Creates a folder
102 ******************************************************************************/
103function sqimap_mailbox_create ($imap_stream, $mailbox, $type)
104{
105 global $delimiter;
106 if (strtolower($type) == 'noselect') {
107 $mailbox = $mailbox.$delimiter;
108 }
1c72b151 109 $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
90de1755 110 TRUE, $response, $message);
43b698c7 111
112 sqimap_subscribe ($imap_stream, $mailbox);
113}
114
115
116
117/******************************************************************************
118 ** Subscribes to an existing folder
119 ******************************************************************************/
120function sqimap_subscribe ($imap_stream, $mailbox)
121{
1a16af11 122 $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"",
90de1755 123 TRUE, $response, $message);
43b698c7 124}
125
126
127
128/******************************************************************************
129 ** Unsubscribes to an existing folder
130 ******************************************************************************/
131function sqimap_unsubscribe ($imap_stream, $mailbox)
132{
133 global $imap_server_type;
134
1a16af11 135 $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"",
90de1755 136 TRUE, $response, $message);
43b698c7 137}
138
139
140
141/******************************************************************************
142 ** This function simply deletes the given folder
143 ******************************************************************************/
144function sqimap_mailbox_delete ($imap_stream, $mailbox)
145{
1c72b151 146 $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
90de1755 147 TRUE, $response, $message);
43b698c7 148 sqimap_unsubscribe ($imap_stream, $mailbox);
149}
150
151/***********************************************************************
152 ** Determines if the user is subscribed to the folder or not
153 **********************************************************************/
154function sqimap_mailbox_is_subscribed($imap_stream, $folder)
155{
156 $boxes = sqimap_mailbox_list ($imap_stream);
157 foreach ($boxes as $ref) {
158 if ($ref['unformatted'] == $folder) {
90de1755 159 return TRUE;
43b698c7 160 }
161 }
162 return false;
163}
164
1c52ba77 165/*
166 Renames a mailbox
167*/
168function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
43b698c7 169
1c52ba77 170 if ( $old_name <> $new_name ) {
171
172 global $delimiter;
173
174 if ( substr( $old_name, -1 ) == $delimiter ) {
175 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
176 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
177 $postfix = $delimiter;
178 $boxes = sqimap_mailbox_list($imap_stream);
179 } else {
180 $postfix = '';
181 $boxes = FALSE;
182 }
183
1c72b151 184 $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' . quoteIMAP($new_name) . '"';
185 $data = sqimap_run_command($imap_stream, $cmd,
1c52ba77 186 TRUE, $response, $message);
187 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
188 sqimap_subscribe($imap_stream, $new_name.$postfix);
189
190 if ( $boxes ) {
191 // Sub-unsub subfolders
192 $l = strlen( $old_name ) + 1;
193 $p = 'unformatted';
194 foreach ( $boxes as $box ) {
195 if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) {
196 sqimap_unsubscribe($imap_stream, $box[$p]);
197 sqimap_subscribe($imap_stream,
198 $new_name . $delimiter . substr( $box[$p], $l ) );
199 }
200 }
201 }
202
203 }
204
205}
43b698c7 206
207/******************************************************************************
208 ** Formats a mailbox into 4 parts for the $boxes array
209 **
210 ** The four parts are:
211 **
212 ** raw - Raw LIST/LSUB response from the IMAP server
213 ** formatted - nicely formatted folder name
214 ** unformatted - unformatted, but with delimiter at end removed
215 ** unformatted-dm - folder name as it appears in raw response
216 ** unformatted-disp - unformatted without $folder_prefix
217 **
218 ******************************************************************************/
219function sqimap_mailbox_parse ($line, $line_lsub)
220{
221 global $folder_prefix, $delimiter;
222
223 /* Process each folder line */
224 for ($g=0; $g < count($line); $g++) {
225
226 /* Store the raw IMAP reply */
227 if (isset($line[$g])) {
a7ea7540 228 $boxes[$g]["raw"] = $line[$g];
43b698c7 229 }
230 else {
a7ea7540 231 $boxes[$g]["raw"] = "";
43b698c7 232 }
233
234
235 /* Count number of delimiters ($delimiter) in folder name */
236 $mailbox = trim($line_lsub[$g]);
602e1431 237 $dm_count = substr_count($mailbox, $delimiter);
43b698c7 238 if (substr($mailbox, -1) == $delimiter) {
239 /* If name ends in delimiter - decrement count by one */
240 $dm_count--;
241 }
242
243 /* Format folder name, but only if it's a INBOX.* or have */
244 /* a parent. */
245 $boxesbyname[$mailbox] = $g;
246 $parentfolder = readMailboxParent($mailbox, $delimiter);
247 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
248 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
249 ( isset($boxesbyname[$parentfolder]) &&
250 (strlen($parentfolder) > 0) ) ) {
602e1431 251 $indent = $dm_count - ( substr_count($folder_prefix, $delimiter));
43b698c7 252 if ($indent > 0) {
90de1755 253 $boxes[$g]['formatted'] = str_repeat("&nbsp;&nbsp;", $indent);
43b698c7 254 }
255 else {
90de1755 256 $boxes[$g]['formatted'] = '';
43b698c7 257 }
90de1755 258 $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
43b698c7 259 }
260 else {
90de1755 261 $boxes[$g]['formatted'] = $mailbox;
43b698c7 262 }
90de1755 263
43b698c7 264 $boxes[$g]['unformatted-dm'] = $mailbox;
265 if (substr($mailbox, -1) == $delimiter) {
8e9e8afa 266 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
43b698c7 267 }
268 $boxes[$g]['unformatted'] = $mailbox;
269 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
631b9da3 270 $mailbox = substr($mailbox, strlen($folder_prefix));
43b698c7 271 }
272 $boxes[$g]['unformatted-disp'] = $mailbox;
273 $boxes[$g]['id'] = $g;
90de1755 274
43b698c7 275 $boxes[$g]['flags'] = array();
276 if (isset($line[$g])) {
36dfb0c9 277 ereg("\(([^)]*)\)",$line[$g],$regs);
1a7e1e97 278 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
43b698c7 279 if ($flags) {
280 $boxes[$g]['flags'] = explode(' ', $flags);
281 }
282 }
283 }
90de1755 284
43b698c7 285 return $boxes;
286}
287
a3439b27 288/**
289 * Sorting function used to sort mailbox names.
290 * + Original patch from dave_michmerhuizen@yahoo.com
291 * + Allows case insensitivity when sorting folders
292 * + Takes care of the delimiter being sorted to the end, causing
293 * subfolders to be listed in below folders that are prefixed
294 * with their parent folders name.
295 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
296 * Without special sort function: foobar between foo and foo.bar
297 * With special sort function: foobar AFTER foo and foo.bar :)
43b698c7 298 */
a3439b27 299function user_strcasecmp($a, $b) {
300 global $delimiter;
301
302 /* Calculate the length of some strings. */
303 $a_length = strlen($a);
304 $b_length = strlen($b);
305 $min_length = min($a_length, $b_length);
306 $delimiter_length = strlen($delimiter);
307
308 /* Set the initial result value. */
309 $result = 0;
310
311 /* Check the strings... */
312 for ($c = 0; $c < $min_length; ++$c) {
313 $a_del = substr($a, $c, $delimiter_length);
314 $b_del = substr($b, $c, $delimiter_length);
315
316 if (($a_del == $delimiter) && ($b_del == $delimiter)) {
317 $result = 0;
318 } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
a3439b27 319 $result = -1;
c64c33f4 320 } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
321 $result = 1;
a3439b27 322 } else {
323 $result = strcasecmp($a{$c}, $b{$c});
324 }
325
326 if ($result != 0) {
327 break;
328 }
329 }
90de1755 330
a3439b27 331 /* If one string is a prefix of the other... */
332 if ($result == 0) {
333 if ($a_length < $b_length) {
334 $result = -1;
335 } else if ($a_length > $b_length) {
336 $result = 1;
337 }
338 }
339
340 return ($result);
43b698c7 341}
342
343
344/******************************************************************************
345 ** Returns sorted mailbox lists in several different ways.
346 ** See comment on sqimap_mailbox_parse() for info about the returned array.
347 ******************************************************************************/
a3439b27 348function sqimap_mailbox_list ($imap_stream) {
90de1755 349 global $data_dir, $username, $list_special_folders_first,
350 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
351 $move_to_trash, $move_to_sent, $save_as_draft,
352 $delimiter;
353
43b698c7 354 $inbox_in_list = false;
355 $inbox_subscribed = false;
90de1755 356
43b698c7 357 require_once('../src/load_prefs.php');
358 require_once('../functions/array.php');
90de1755 359
43b698c7 360 /** LSUB array **/
1c72b151 361 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
90de1755 362 TRUE, $response, $message);
363
43b698c7 364 /* Section about removing the last element was removed */
365 /* We don't return "* OK" anymore from sqimap_read_data */
90de1755 366
43b698c7 367 $sorted_lsub_ary = array();
368 for ($i=0;$i < count($lsub_ary); $i++) {
369 /* Workaround for EIMS */
370 /* Doesn't work if the mailbox name is multiple lines */
371 if (isset($lsub_ary[$i + 1]) &&
90de1755 372 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
43b698c7 373 $lsub_ary[$i], $regs)) {
374 $i ++;
375 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) .
376 '"' . $regs[2];
377 }
378 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
379 $sorted_lsub_ary[] = $temp_mailbox_name;
380 if (strtoupper($temp_mailbox_name) == 'INBOX') {
90de1755 381 $inbox_subscribed = TRUE;
43b698c7 382 }
383 }
384 $new_ary = array();
385 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
386 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
ee62ce13 387 $new_ary[] = $sorted_lsub_ary[$i];
43b698c7 388 }
389 }
390 $sorted_lsub_ary = $new_ary;
391 if (isset($sorted_lsub_ary)) {
392 usort($sorted_lsub_ary, 'user_strcasecmp');
90de1755 393 }
394
43b698c7 395 /** LIST array **/
396 $sorted_list_ary = array();
397 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
398 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
f9b3e5d9 399 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
43b698c7 400 }
401 else {
f9b3e5d9 402 $mbx = $sorted_lsub_ary[$i];
43b698c7 403 }
90de1755 404
1c72b151 405 $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
90de1755 406 TRUE, $response, $message);
43b698c7 407 /* Another workaround for EIMS */
90de1755 408 if (isset($read[1]) &&
409 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
43b698c7 410 $read[0], $regs)) {
411 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
90de1755 412 '"' . $regs[2];
43b698c7 413 }
90de1755 414
43b698c7 415 if (isset($sorted_list_ary[$i])) {
90de1755 416 $sorted_list_ary[$i] = '';
43b698c7 417 }
90de1755 418
43b698c7 419 if (isset($read[0])) {
44a3ca20 420 $sorted_list_ary[$i] = $read[0];
43b698c7 421 }
422 else {
90de1755 423 $sorted_list_ary[$i] = '';
43b698c7 424 }
90de1755 425
426 if (isset($sorted_list_ary[$i]) &&
427 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
428 $inbox_in_list = TRUE;
43b698c7 429 }
430 }
90de1755 431
43b698c7 432 /**
433 * Just in case they're not subscribed to their inbox,
434 * we'll get it for them anyway
435 */
436 if ($inbox_subscribed == false || $inbox_in_list == false) {
1c72b151 437 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
90de1755 438 TRUE, $response, $message);
43b698c7 439 /* Another workaround for EIMS */
440 if (isset($inbox_ary[1]) &&
90de1755 441 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
43b698c7 442 $inbox_ary[0], $regs)) {
443 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
444 '"' . $regs[2];
445 }
90de1755 446
43b698c7 447 $sorted_list_ary[] = $inbox_ary[0];
448 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
449 }
90de1755 450
43b698c7 451 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
90de1755 452
43b698c7 453 /** Now, lets sort for special folders **/
90de1755 454 $boxesnew = $used = array();
455
43b698c7 456 /* Find INBOX */
90de1755 457 foreach ( $boxes as $k => $box ) {
458 if ( strtolower($box['unformatted']) == 'inbox') {
459 $boxesnew[] = $box;
460 $used[$k] = TRUE;
461 } else {
462 $used[$k] = FALSE;
43b698c7 463 }
464 }
90de1755 465
43b698c7 466 /* List special folders and their subfolders, if requested. */
90de1755 467 if ($list_special_folders_first == TRUE) {
468
469 foreach ( $boxes as $k => $box ) {
470 if ( !$used[$k] &&
471 isSpecialMailbox( $box['unformatted'] ) ) {
472 $boxesnew[] = $box;
473 $used[$k] = TRUE;
2752bb16 474 }
43b698c7 475 }
90de1755 476
43b698c7 477 }
90de1755 478
43b698c7 479 /* Rest of the folders */
90de1755 480 foreach ( $boxes as $k => $box ) {
481 if ( !$used[$k] ) {
482 $boxesnew[] = $box;
43b698c7 483 }
484 }
90de1755 485
486 return( $boxesnew );
43b698c7 487}
488
90de1755 489/*
490 * Returns a list of all folders, subscribed or not
491 */
43b698c7 492function sqimap_mailbox_list_all ($imap_stream)
493{
494 global $list_special_folders_first, $folder_prefix;
495 global $delimiter;
90de1755 496
497 if (!function_exists('ary_sort')) {
43b698c7 498 include_once('../functions/array.php');
499 }
90de1755 500
43b698c7 501 $ssid = sqimap_session_id();
90de1755 502 $lsid = strlen( $ssid );
43b698c7 503 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
90de1755 504 $read_ary = sqimap_read_data ($imap_stream, $ssid, TRUE, $response, $message);
43b698c7 505 $g = 0;
90de1755 506 $phase = 'inbox';
507
43b698c7 508 for ($i = 0; $i < count($read_ary); $i++) {
509 /* Another workaround for EIMS */
510 if (isset($read_ary[$i + 1]) &&
90de1755 511 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
43b698c7 512 $read_ary[$i], $regs)) {
513 $i ++;
90de1755 514 $read_ary[$i] = $regs[1] . '"' .
1c52ba77 515 addslashes(trim($read_ary[$i])) .
516 '"' . $regs[2];
43b698c7 517 }
90de1755 518 if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
519
43b698c7 520 /* Store the raw IMAP reply */
8e9e8afa 521 $boxes[$g]["raw"] = $read_ary[$i];
1c52ba77 522
43b698c7 523 /* Count number of delimiters ($delimiter) in folder name */
8e9e8afa 524 $mailbox = find_mailbox_name($read_ary[$i]);
602e1431 525 $dm_count = substr_count($mailbox, $delimiter);
43b698c7 526 if (substr($mailbox, -1) == $delimiter) {
0cfd745a 527 /* If name ends in delimiter - decrement count by one */
90de1755 528 $dm_count--;
43b698c7 529 }
90de1755 530
43b698c7 531 /* Format folder name, but only if it's a INBOX.* or have */
532 /* a parent. */
2752bb16 533 $boxesbyname[$mailbox] = $g;
525b7ae6 534 $parentfolder = readMailboxParent($mailbox, $delimiter);
90de1755 535 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
146e0c45 536 (ereg('^'.$folder_prefix, $mailbox)) ||
2752bb16 537 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
43b698c7 538 if ($dm_count) {
90de1755 539 $boxes[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $dm_count);
43b698c7 540 }
541 else {
90de1755 542 $boxes[$g]['formatted'] = '';
43b698c7 543 }
90de1755 544 $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
2752bb16 545 }
43b698c7 546 else {
90de1755 547 $boxes[$g]['formatted'] = $mailbox;
43b698c7 548 }
90de1755 549
8e9e8afa 550 $boxes[$g]["unformatted-dm"] = $mailbox;
43b698c7 551 if (substr($mailbox, -1) == $delimiter) {
552 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
553 }
90de1755 554 $boxes[$g]['unformatted'] = $mailbox;
555 $boxes[$g]['unformatted-disp'] =
0cfd745a 556 ereg_replace('^' . $folder_prefix, '', $mailbox);
90de1755 557 $boxes[$g]['id'] = $g;
558
8e9e8afa 559 /** Now lets get the flags for this mailbox **/
1c72b151 560 $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
90de1755 561 TRUE, $response, $message);
562
43b698c7 563 /* Another workaround for EIMS */
564 if (isset($read_mlbx[1]) &&
90de1755 565 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
43b698c7 566 $read_mlbx[0], $regs)) {
1c52ba77 567 $read_mlbx[0] = $regs[1] . '"' .
568 addslashes(trim($read_mlbx[1])) .
569 '"' . $regs[2];
43b698c7 570 }
90de1755 571
572 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+1);
573 $flags = substr($flags, 0, strpos($flags, ')'));
146e0c45 574 $flags = str_replace('\\', '', $flags);
8e9e8afa 575 $flags = trim(strtolower($flags));
576 if ($flags) {
1c52ba77 577 $boxes[$g]['flags'] = explode(' ', $flags);
90de1755 578 } else {
43b698c7 579 $boxes[$g]['flags'] = array();
12d61439 580 }
43b698c7 581 }
582 $g++;
583 }
584 if(is_array($boxes)) {
1c52ba77 585 $boxes = ary_sort ($boxes, 'unformatted', 1);
43b698c7 586 }
90de1755 587
43b698c7 588 return $boxes;
589}
5bdd7223 590
f7b1b3b1 591?>