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