fix for bug #512056, update filters when renaming/deleting 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 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 filter_check($mailbox, 'delete');
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 /* fix filters when renaming/deleting folders */
205
206 function filter_check ($old_folder, $action, $new_folder = 'INBOX') {
207 global $plugins, $data_dir, $username;
208 $filters = array();
209 $count = count($plugins);
210 for ($i=0;$i<$count;$i++) {
211 if ($plugins[$i] == 'filters') {
212 require_once("../plugins/filters/filters.php");
213 $filters = load_filters();
214 break;
215 }
216 }
217 if (empty($filters)) {
218 return;
219 }
220 $filter_count = count($filters);
221 $p=0;
222 for ($i=0;$i<$filter_count;$i++) {
223 if ($old_folder == $filters[$i]['folder']) {
224 if ($action == 'rename') {
225 $filters[$i]['folder'] = $new_folder;
226 setPref($data_dir, $username, "filter".$i, $filters[$i]['where'].",".$filters[$i]['what'].",".$new_folder);
227 }
228 elseif ($action == 'delete') {
229 remove_filter($p);
230 $p = $p-1;
231 }
232 }
233 $p++;
234 }
235 }
236
237
238 /*
239 Renames a mailbox
240 */
241 function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
242
243 if ( $old_name <> $new_name ) {
244 global $delimiter, $imap_server_type;
245 if ( substr( $old_name, -1 ) == $delimiter ) {
246 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
247 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
248 $postfix = $delimiter;
249 } else {
250 $postfix = '';
251 }
252 $boxesall = sqimap_mailbox_list($imap_stream);
253 $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' . quoteIMAP($new_name) . '"';
254 $data = sqimap_run_command($imap_stream, $cmd,
255 TRUE, $response, $message);
256 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
257 sqimap_subscribe($imap_stream, $new_name.$postfix);
258 filter_check($old_name, 'rename', $new_name);
259 $l = strlen( $old_name ) + 1;
260 $p = 'unformatted';
261 foreach ( $boxesall as $box ) {
262 if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) {
263 $new_sub = $new_name . $delimiter . substr($box[$p], $l);
264 if ($imap_server_type == 'cyrus') {
265 $cmd = 'RENAME "' . quoteIMAP($box[$p]) . '" "' . quoteIMAP($new_sub) . '"';
266 $data = sqimap_run_command($imap_stream, $cmd, TRUE,
267 $response, $message);
268 }
269 sqimap_unsubscribe($imap_stream, $box[$p]);
270 sqimap_subscribe($imap_stream, $new_sub);
271 filter_check($box[$p], 'rename', $new_sub);
272 }
273 }
274 }
275
276 }
277
278 /******************************************************************************
279 ** Formats a mailbox into 4 parts for the $boxesall array
280 **
281 ** The four parts are:
282 **
283 ** raw - Raw LIST/LSUB response from the IMAP server
284 ** formatted - nicely formatted folder name
285 ** unformatted - unformatted, but with delimiter at end removed
286 ** unformatted-dm - folder name as it appears in raw response
287 ** unformatted-disp - unformatted without $folder_prefix
288 **
289 ******************************************************************************/
290 function sqimap_mailbox_parse ($line, $line_lsub)
291 {
292 global $folder_prefix, $delimiter;
293
294 /* Process each folder line */
295 for ($g=0; $g < count($line); $g++) {
296
297 /* Store the raw IMAP reply */
298 if (isset($line[$g])) {
299 $boxesall[$g]["raw"] = $line[$g];
300 }
301 else {
302 $boxesall[$g]["raw"] = "";
303 }
304
305
306 /* Count number of delimiters ($delimiter) in folder name */
307 $mailbox = trim($line_lsub[$g]);
308 $dm_count = substr_count($mailbox, $delimiter);
309 if (substr($mailbox, -1) == $delimiter) {
310 /* If name ends in delimiter - decrement count by one */
311 $dm_count--;
312 }
313
314 /* Format folder name, but only if it's a INBOX.* or have */
315 /* a parent. */
316 $boxesallbyname[$mailbox] = $g;
317 $parentfolder = readMailboxParent($mailbox, $delimiter);
318 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
319 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
320 ( isset($boxesallbyname[$parentfolder]) &&
321 (strlen($parentfolder) > 0) ) ) {
322 $indent = $dm_count - ( substr_count($folder_prefix, $delimiter));
323 if ($indent > 0) {
324 $boxesall[$g]['formatted'] = str_repeat("&nbsp;&nbsp;", $indent);
325 }
326 else {
327 $boxesall[$g]['formatted'] = '';
328 }
329 $boxesall[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
330 }
331 else {
332 $boxesall[$g]['formatted'] = $mailbox;
333 }
334
335 $boxesall[$g]['unformatted-dm'] = $mailbox;
336 if (substr($mailbox, -1) == $delimiter) {
337 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
338 }
339 $boxesall[$g]['unformatted'] = $mailbox;
340 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
341 $mailbox = substr($mailbox, strlen($folder_prefix));
342 }
343 $boxesall[$g]['unformatted-disp'] = $mailbox;
344 $boxesall[$g]['id'] = $g;
345
346 $boxesall[$g]['flags'] = array();
347 if (isset($line[$g])) {
348 ereg("\(([^)]*)\)",$line[$g],$regs);
349 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
350 if ($flags) {
351 $boxesall[$g]['flags'] = explode(' ', $flags);
352 }
353 }
354 }
355
356 return $boxesall;
357 }
358
359 /**
360 * Sorting function used to sort mailbox names.
361 * + Original patch from dave_michmerhuizen@yahoo.com
362 * + Allows case insensitivity when sorting folders
363 * + Takes care of the delimiter being sorted to the end, causing
364 * subfolders to be listed in below folders that are prefixed
365 * with their parent folders name.
366 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
367 * Without special sort function: foobar between foo and foo.bar
368 * With special sort function: foobar AFTER foo and foo.bar :)
369 */
370 function user_strcasecmp($a, $b) {
371 global $delimiter;
372
373 /* Calculate the length of some strings. */
374 $a_length = strlen($a);
375 $b_length = strlen($b);
376 $min_length = min($a_length, $b_length);
377 $delimiter_length = strlen($delimiter);
378
379 /* Set the initial result value. */
380 $result = 0;
381
382 /* Check the strings... */
383 for ($c = 0; $c < $min_length; ++$c) {
384 $a_del = substr($a, $c, $delimiter_length);
385 $b_del = substr($b, $c, $delimiter_length);
386
387 if (($a_del == $delimiter) && ($b_del == $delimiter)) {
388 $result = 0;
389 } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
390 $result = -1;
391 } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
392 $result = 1;
393 } else {
394 $result = strcasecmp($a{$c}, $b{$c});
395 }
396
397 if ($result != 0) {
398 break;
399 }
400 }
401
402 /* If one string is a prefix of the other... */
403 if ($result == 0) {
404 if ($a_length < $b_length) {
405 $result = -1;
406 } else if ($a_length > $b_length) {
407 $result = 1;
408 }
409 }
410
411 return ($result);
412 }
413
414
415 /******************************************************************************
416 ** Returns sorted mailbox lists in several different ways.
417 ** See comment on sqimap_mailbox_parse() for info about the returned array.
418 ******************************************************************************/
419 function sqimap_mailbox_list($imap_stream) {
420
421 GLOBAL $boxesnew;
422
423 if ( !isset( $boxesnew ) ) {
424
425 GLOBAL $data_dir, $username, $list_special_folders_first,
426 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
427 $move_to_trash, $move_to_sent, $save_as_draft,
428 $delimiter;
429
430 $inbox_in_list = $inbox_subscribed = FALSE;
431
432 require_once('../src/load_prefs.php');
433 require_once('../functions/array.php');
434
435 /** LSUB array **/
436 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
437 TRUE, $response, $message);
438
439 /* Section about removing the last element was removed */
440 /* We don't return "* OK" anymore from sqimap_read_data */
441
442 $sorted_lsub_ary = array();
443 for ($i=0;$i < count($lsub_ary); $i++) {
444 /* Workaround for EIMS */
445 /* Doesn't work if the mailbox name is multiple lines */
446 if (isset($lsub_ary[$i + 1]) &&
447 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
448 $lsub_ary[$i], $regs)) {
449 $i ++;
450 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) .
451 '"' . $regs[2];
452 }
453 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
454 $sorted_lsub_ary[] = $temp_mailbox_name;
455 if (strtoupper($temp_mailbox_name) == 'INBOX') {
456 $inbox_subscribed = TRUE;
457 }
458 }
459 $new_ary = array();
460 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
461 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
462 $new_ary[] = $sorted_lsub_ary[$i];
463 }
464 }
465 $sorted_lsub_ary = $new_ary;
466 if (isset($sorted_lsub_ary)) {
467 usort($sorted_lsub_ary, 'user_strcasecmp');
468 }
469
470 /** LIST array **/
471 $sorted_list_ary = array();
472 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
473 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
474 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
475 }
476 else {
477 $mbx = $sorted_lsub_ary[$i];
478 }
479
480 $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
481 TRUE, $response, $message);
482 /* Another workaround for EIMS */
483 if (isset($read[1]) &&
484 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
485 $read[0], $regs)) {
486 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
487 '"' . $regs[2];
488 }
489
490 if (isset($sorted_list_ary[$i])) {
491 $sorted_list_ary[$i] = '';
492 }
493
494 if (isset($read[0])) {
495 $sorted_list_ary[$i] = $read[0];
496 }
497 else {
498 $sorted_list_ary[$i] = '';
499 }
500
501 if (isset($sorted_list_ary[$i]) &&
502 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
503 $inbox_in_list = TRUE;
504 }
505 }
506
507 /**
508 * Just in case they're not subscribed to their inbox,
509 * we'll get it for them anyway
510 */
511 if ($inbox_subscribed == false || $inbox_in_list == false) {
512 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
513 TRUE, $response, $message);
514 /* Another workaround for EIMS */
515 if (isset($inbox_ary[1]) &&
516 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
517 $inbox_ary[0], $regs)) {
518 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
519 '"' . $regs[2];
520 }
521
522 $sorted_list_ary[] = $inbox_ary[0];
523 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
524 }
525
526 $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
527
528 /** Now, lets sort for special folders **/
529 $boxesnew = $used = array();
530
531 /* Find INBOX */
532 foreach ( $boxesall as $k => $box ) {
533 if ( strtolower($box['unformatted']) == 'inbox') {
534 $boxesnew[] = $box;
535 $used[$k] = TRUE;
536 } else {
537 $used[$k] = FALSE;
538 }
539 }
540
541 /* List special folders and their subfolders, if requested. */
542 if ($list_special_folders_first == TRUE) {
543
544 foreach ( $boxesall as $k => $box ) {
545 if ( !$used[$k] &&
546 isSpecialMailbox( $box['unformatted'] ) ) {
547 $boxesnew[] = $box;
548 $used[$k] = TRUE;
549 }
550 }
551
552 }
553
554 /* Rest of the folders */
555 foreach ( $boxesall as $k => $box ) {
556 if ( !$used[$k] ) {
557 $boxesnew[] = $box;
558 }
559 }
560 }
561
562 return( $boxesnew );
563
564 }
565
566 /*
567 * Returns a list of all folders, subscribed or not
568 */
569 function sqimap_mailbox_list_all($imap_stream) {
570
571
572 GLOBAL $list_special_folders_first, $folder_prefix,
573 $delimiter;
574
575 require_once('../functions/array.php');
576
577 $ssid = sqimap_session_id();
578 $lsid = strlen( $ssid );
579 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
580 $read_ary = sqimap_read_data ($imap_stream, $ssid, TRUE, $response, $message);
581 $g = 0;
582 $phase = 'inbox';
583
584 for ($i = 0; $i < count($read_ary); $i++) {
585 /* Another workaround for EIMS */
586 if (isset($read_ary[$i + 1]) &&
587 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
588 $read_ary[$i], $regs)) {
589 $i ++;
590 $read_ary[$i] = $regs[1] . '"' .
591 addslashes(trim($read_ary[$i])) .
592 '"' . $regs[2];
593 }
594 if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
595
596 /* Store the raw IMAP reply */
597 $boxes[$g]['raw'] = $read_ary[$i];
598
599 /* Count number of delimiters ($delimiter) in folder name */
600 $mailbox = find_mailbox_name($read_ary[$i]);
601 $dm_count = substr_count($mailbox, $delimiter);
602 if (substr($mailbox, -1) == $delimiter) {
603 /* If name ends in delimiter - decrement count by one */
604 $dm_count--;
605 }
606
607 /* Format folder name, but only if it's a INBOX.* or have */
608 /* a parent. */
609 $boxesallbyname[$mailbox] = $g;
610 $parentfolder = readMailboxParent($mailbox, $delimiter);
611 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
612 (ereg('^'.$folder_prefix, $mailbox)) ||
613 ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
614 if ($dm_count) {
615 $boxes[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $dm_count);
616 }
617 else {
618 $boxes[$g]['formatted'] = '';
619 }
620 $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
621 }
622 else {
623 $boxes[$g]['formatted'] = $mailbox;
624 }
625
626 $boxes[$g]['unformatted-dm'] = $mailbox;
627 if (substr($mailbox, -1) == $delimiter) {
628 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
629 }
630 $boxes[$g]['unformatted'] = $mailbox;
631 $boxes[$g]['unformatted-disp'] =
632 ereg_replace('^' . $folder_prefix, '', $mailbox);
633 $boxes[$g]['id'] = $g;
634
635 /** Now lets get the flags for this mailbox **/
636 $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
637 TRUE, $response, $message);
638
639 /* Another workaround for EIMS */
640 if (isset($read_mlbx[1]) &&
641 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
642 $read_mlbx[0], $regs)) {
643 $read_mlbx[0] = $regs[1] . '"' .
644 addslashes(trim($read_mlbx[1])) .
645 '"' . $regs[2];
646 }
647
648 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+1);
649 $flags = substr($flags, 0, strpos($flags, ')'));
650 $flags = str_replace('\\', '', $flags);
651 $flags = trim(strtolower($flags));
652 if ($flags) {
653 $boxes[$g]['flags'] = explode(' ', $flags);
654 } else {
655 $boxes[$g]['flags'] = array();
656 }
657 }
658 $g++;
659 }
660 if(is_array($boxes)) {
661 $boxes = ary_sort ($boxes, 'unformatted', 1);
662 }
663
664 return $boxes;
665 }
666
667 ?>