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