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