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