Disable the show images stuff while reading html messages.
[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
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
35586184 38/*************************
39 ** Expunges a mailbox **
40 *************************/
90de1755 41function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = TRUE)
43b698c7 42{
1c72b151 43 $read = sqimap_run_command($imap_stream, 'EXPUNGE',
44 $handle_errors, $response, $message);
43b698c7 45}
46
47
48/******************************************************************************
49 ** Checks whether or not the specified mailbox exists
50 ******************************************************************************/
51function sqimap_mailbox_exists ($imap_stream, $mailbox)
52{
53 if (! isset($mailbox)) {
54 return false;
55 }
1c72b151 56 $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
90de1755 57 TRUE, $response, $message);
43b698c7 58 return isset($mbx[0]);
59}
60
61/******************************************************************************
62 ** Selects a mailbox
63 ******************************************************************************/
64function sqimap_mailbox_select ($imap_stream, $mailbox,
90de1755 65 $hide=TRUE, $recent=false)
43b698c7 66{
67 global $auto_expunge;
68
69 if ( $mailbox == 'None' ) {
70 return;
71 }
72
1c72b151 73 $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
90de1755 74 TRUE, $response, $message);
43b698c7 75 if ($recent) {
76 for ($i=0; $i<count($read); $i++) {
bccadd02 77 if (strpos(strtolower($read[$i]), 'recent')) {
43b698c7 78 $r = explode(' ', $read[$i]);
04632dbc 79 }
43b698c7 80 }
81 return $r[1];
82 }
83 if ($auto_expunge) {
1a16af11 84 $tmp = sqimap_run_command($imap_stream, 'EXPUNGE',
1c72b151 85 false, $a, $b);
43b698c7 86 }
87}
88
89
90
91/******************************************************************************
92 ** Creates a folder
93 ******************************************************************************/
94function sqimap_mailbox_create ($imap_stream, $mailbox, $type)
95{
96 global $delimiter;
97 if (strtolower($type) == 'noselect') {
98 $mailbox = $mailbox.$delimiter;
99 }
1c72b151 100 $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
90de1755 101 TRUE, $response, $message);
43b698c7 102
103 sqimap_subscribe ($imap_stream, $mailbox);
104}
105
106
107
108/******************************************************************************
109 ** Subscribes to an existing folder
110 ******************************************************************************/
111function sqimap_subscribe ($imap_stream, $mailbox)
112{
1a16af11 113 $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"",
90de1755 114 TRUE, $response, $message);
43b698c7 115}
116
117
118
119/******************************************************************************
120 ** Unsubscribes to an existing folder
121 ******************************************************************************/
122function sqimap_unsubscribe ($imap_stream, $mailbox)
123{
124 global $imap_server_type;
125
1a16af11 126 $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"",
90de1755 127 TRUE, $response, $message);
43b698c7 128}
129
130
131
132/******************************************************************************
133 ** This function simply deletes the given folder
134 ******************************************************************************/
135function sqimap_mailbox_delete ($imap_stream, $mailbox)
136{
1c72b151 137 $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
90de1755 138 TRUE, $response, $message);
43b698c7 139 sqimap_unsubscribe ($imap_stream, $mailbox);
140}
141
142/***********************************************************************
143 ** Determines if the user is subscribed to the folder or not
144 **********************************************************************/
145function 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) {
90de1755 150 return TRUE;
43b698c7 151 }
152 }
153 return false;
154}
155
1c52ba77 156/*
157 Renames a mailbox
158*/
159function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
43b698c7 160
1c52ba77 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
1c72b151 175 $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' . quoteIMAP($new_name) . '"';
176 $data = sqimap_run_command($imap_stream, $cmd,
1c52ba77 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}
43b698c7 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 ******************************************************************************/
210function 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])) {
a7ea7540 219 $boxes[$g]["raw"] = $line[$g];
43b698c7 220 }
221 else {
a7ea7540 222 $boxes[$g]["raw"] = "";
43b698c7 223 }
224
225
226 /* Count number of delimiters ($delimiter) in folder name */
227 $mailbox = trim($line_lsub[$g]);
602e1431 228 $dm_count = substr_count($mailbox, $delimiter);
43b698c7 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) ) ) {
602e1431 242 $indent = $dm_count - ( substr_count($folder_prefix, $delimiter));
43b698c7 243 if ($indent > 0) {
90de1755 244 $boxes[$g]['formatted'] = str_repeat("&nbsp;&nbsp;", $indent);
43b698c7 245 }
246 else {
90de1755 247 $boxes[$g]['formatted'] = '';
43b698c7 248 }
90de1755 249 $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
43b698c7 250 }
251 else {
90de1755 252 $boxes[$g]['formatted'] = $mailbox;
43b698c7 253 }
90de1755 254
43b698c7 255 $boxes[$g]['unformatted-dm'] = $mailbox;
256 if (substr($mailbox, -1) == $delimiter) {
8e9e8afa 257 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
43b698c7 258 }
259 $boxes[$g]['unformatted'] = $mailbox;
260 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
631b9da3 261 $mailbox = substr($mailbox, strlen($folder_prefix));
43b698c7 262 }
263 $boxes[$g]['unformatted-disp'] = $mailbox;
264 $boxes[$g]['id'] = $g;
90de1755 265
43b698c7 266 $boxes[$g]['flags'] = array();
267 if (isset($line[$g])) {
36dfb0c9 268 ereg("\(([^)]*)\)",$line[$g],$regs);
1a7e1e97 269 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
43b698c7 270 if ($flags) {
271 $boxes[$g]['flags'] = explode(' ', $flags);
272 }
273 }
274 }
90de1755 275
43b698c7 276 return $boxes;
277}
278
a3439b27 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 :)
43b698c7 289 */
a3439b27 290function 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)) {
a3439b27 310 $result = -1;
c64c33f4 311 } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
312 $result = 1;
a3439b27 313 } else {
314 $result = strcasecmp($a{$c}, $b{$c});
315 }
316
317 if ($result != 0) {
318 break;
319 }
320 }
90de1755 321
a3439b27 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);
43b698c7 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 ******************************************************************************/
a3439b27 339function sqimap_mailbox_list ($imap_stream) {
90de1755 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
43b698c7 345 $inbox_in_list = false;
346 $inbox_subscribed = false;
90de1755 347
43b698c7 348 require_once('../src/load_prefs.php');
349 require_once('../functions/array.php');
90de1755 350
43b698c7 351 /** LSUB array **/
1c72b151 352 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
90de1755 353 TRUE, $response, $message);
354
43b698c7 355 /* Section about removing the last element was removed */
356 /* We don't return "* OK" anymore from sqimap_read_data */
90de1755 357
43b698c7 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]) &&
90de1755 363 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
43b698c7 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') {
90de1755 372 $inbox_subscribed = TRUE;
43b698c7 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)) {
ee62ce13 378 $new_ary[] = $sorted_lsub_ary[$i];
43b698c7 379 }
380 }
381 $sorted_lsub_ary = $new_ary;
382 if (isset($sorted_lsub_ary)) {
383 usort($sorted_lsub_ary, 'user_strcasecmp');
90de1755 384 }
385
43b698c7 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) {
f9b3e5d9 390 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
43b698c7 391 }
392 else {
f9b3e5d9 393 $mbx = $sorted_lsub_ary[$i];
43b698c7 394 }
90de1755 395
1c72b151 396 $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
90de1755 397 TRUE, $response, $message);
43b698c7 398 /* Another workaround for EIMS */
90de1755 399 if (isset($read[1]) &&
400 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
43b698c7 401 $read[0], $regs)) {
402 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
90de1755 403 '"' . $regs[2];
43b698c7 404 }
90de1755 405
43b698c7 406 if (isset($sorted_list_ary[$i])) {
90de1755 407 $sorted_list_ary[$i] = '';
43b698c7 408 }
90de1755 409
43b698c7 410 if (isset($read[0])) {
44a3ca20 411 $sorted_list_ary[$i] = $read[0];
43b698c7 412 }
413 else {
90de1755 414 $sorted_list_ary[$i] = '';
43b698c7 415 }
90de1755 416
417 if (isset($sorted_list_ary[$i]) &&
418 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
419 $inbox_in_list = TRUE;
43b698c7 420 }
421 }
90de1755 422
43b698c7 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) {
1c72b151 428 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
90de1755 429 TRUE, $response, $message);
43b698c7 430 /* Another workaround for EIMS */
431 if (isset($inbox_ary[1]) &&
90de1755 432 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
43b698c7 433 $inbox_ary[0], $regs)) {
434 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
435 '"' . $regs[2];
436 }
90de1755 437
43b698c7 438 $sorted_list_ary[] = $inbox_ary[0];
439 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
440 }
90de1755 441
43b698c7 442 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
90de1755 443
43b698c7 444 /** Now, lets sort for special folders **/
90de1755 445 $boxesnew = $used = array();
446
43b698c7 447 /* Find INBOX */
90de1755 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;
43b698c7 454 }
455 }
90de1755 456
43b698c7 457 /* List special folders and their subfolders, if requested. */
90de1755 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;
2752bb16 465 }
43b698c7 466 }
90de1755 467
43b698c7 468 }
90de1755 469
43b698c7 470 /* Rest of the folders */
90de1755 471 foreach ( $boxes as $k => $box ) {
472 if ( !$used[$k] ) {
473 $boxesnew[] = $box;
43b698c7 474 }
475 }
90de1755 476
477 return( $boxesnew );
43b698c7 478}
479
90de1755 480/*
481 * Returns a list of all folders, subscribed or not
482 */
43b698c7 483function sqimap_mailbox_list_all ($imap_stream)
484{
485 global $list_special_folders_first, $folder_prefix;
486 global $delimiter;
90de1755 487
488 if (!function_exists('ary_sort')) {
43b698c7 489 include_once('../functions/array.php');
490 }
90de1755 491
43b698c7 492 $ssid = sqimap_session_id();
90de1755 493 $lsid = strlen( $ssid );
43b698c7 494 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
90de1755 495 $read_ary = sqimap_read_data ($imap_stream, $ssid, TRUE, $response, $message);
43b698c7 496 $g = 0;
90de1755 497 $phase = 'inbox';
498
43b698c7 499 for ($i = 0; $i < count($read_ary); $i++) {
500 /* Another workaround for EIMS */
501 if (isset($read_ary[$i + 1]) &&
90de1755 502 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
43b698c7 503 $read_ary[$i], $regs)) {
504 $i ++;
90de1755 505 $read_ary[$i] = $regs[1] . '"' .
1c52ba77 506 addslashes(trim($read_ary[$i])) .
507 '"' . $regs[2];
43b698c7 508 }
90de1755 509 if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
510
43b698c7 511 /* Store the raw IMAP reply */
8e9e8afa 512 $boxes[$g]["raw"] = $read_ary[$i];
1c52ba77 513
43b698c7 514 /* Count number of delimiters ($delimiter) in folder name */
8e9e8afa 515 $mailbox = find_mailbox_name($read_ary[$i]);
602e1431 516 $dm_count = substr_count($mailbox, $delimiter);
43b698c7 517 if (substr($mailbox, -1) == $delimiter) {
0cfd745a 518 /* If name ends in delimiter - decrement count by one */
90de1755 519 $dm_count--;
43b698c7 520 }
90de1755 521
43b698c7 522 /* Format folder name, but only if it's a INBOX.* or have */
523 /* a parent. */
2752bb16 524 $boxesbyname[$mailbox] = $g;
525b7ae6 525 $parentfolder = readMailboxParent($mailbox, $delimiter);
90de1755 526 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
146e0c45 527 (ereg('^'.$folder_prefix, $mailbox)) ||
2752bb16 528 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
43b698c7 529 if ($dm_count) {
90de1755 530 $boxes[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $dm_count);
43b698c7 531 }
532 else {
90de1755 533 $boxes[$g]['formatted'] = '';
43b698c7 534 }
90de1755 535 $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
2752bb16 536 }
43b698c7 537 else {
90de1755 538 $boxes[$g]['formatted'] = $mailbox;
43b698c7 539 }
90de1755 540
8e9e8afa 541 $boxes[$g]["unformatted-dm"] = $mailbox;
43b698c7 542 if (substr($mailbox, -1) == $delimiter) {
543 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
544 }
90de1755 545 $boxes[$g]['unformatted'] = $mailbox;
546 $boxes[$g]['unformatted-disp'] =
0cfd745a 547 ereg_replace('^' . $folder_prefix, '', $mailbox);
90de1755 548 $boxes[$g]['id'] = $g;
549
8e9e8afa 550 /** Now lets get the flags for this mailbox **/
1c72b151 551 $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
90de1755 552 TRUE, $response, $message);
553
43b698c7 554 /* Another workaround for EIMS */
555 if (isset($read_mlbx[1]) &&
90de1755 556 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
43b698c7 557 $read_mlbx[0], $regs)) {
1c52ba77 558 $read_mlbx[0] = $regs[1] . '"' .
559 addslashes(trim($read_mlbx[1])) .
560 '"' . $regs[2];
43b698c7 561 }
90de1755 562
563 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+1);
564 $flags = substr($flags, 0, strpos($flags, ')'));
146e0c45 565 $flags = str_replace('\\', '', $flags);
8e9e8afa 566 $flags = trim(strtolower($flags));
567 if ($flags) {
1c52ba77 568 $boxes[$g]['flags'] = explode(' ', $flags);
90de1755 569 } else {
43b698c7 570 $boxes[$g]['flags'] = array();
12d61439 571 }
43b698c7 572 }
573 $g++;
574 }
575 if(is_array($boxes)) {
1c52ba77 576 $boxes = ary_sort ($boxes, 'unformatted', 1);
43b698c7 577 }
90de1755 578
43b698c7 579 return $boxes;
580}
5bdd7223 581
f7b1b3b1 582?>