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