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