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