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