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