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