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