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