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