FIxing bug #600369
[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 */
447b2166 13require_once('../functions/imap_utf7_encode_local.php');
14require_once('../functions/imap_utf7_decode_local.php');
3411d4ec 15global $boxesnew;
1da22cda 16
60b5724d 17class mailboxes {
18 var $mailboxname_full = '', $mailboxname_sub= '', $is_noselect = false,
19 $is_special = false, $is_root = false, $is_inbox = false, $is_sent = false,
20 $is_trash = false, $is_draft = false, $mbxs = array(),
21 $unseen = false, $total = false;
22
23 function addMbx($mbx, $delimiter, $start, $specialfirst) {
24 $ary = explode($delimiter, $mbx->mailboxname_full);
25 $mbx_parent = &$this;
26 for ($i=$start; $i < (count($ary) -1); $i++) {
27 $mbx_childs = &$mbx_parent->mbxs;
28 $found = false;
e4c6fe41 29 if ($mbx_childs) {
30 foreach ($mbx_childs as $key => $parent) {
60b5724d 31 if ($parent->mailboxname_sub == $ary[$i]) {
32 $mbx_parent = &$mbx_parent->mbxs[$key];
33 $found = true;
34 }
e4c6fe41 35 }
36 }
60b5724d 37 if (!$found) {
38 $no_select_mbx = new mailboxes();
39 if (isset($mbx_parent->mailboxname_full) && $mbx_parent->mailboxname_full != '') {
40 $no_select_mbx->mailboxname_full = $mbx_parent->mailboxname_full.$delimiter.$ary[$i];
41 } else {
42 $no_select_mbx->mailboxname_full = $ary[$i];
43 }
44 $no_select_mbx->mailboxname_sub = $ary[$i];
45 $no_select_mbx->is_noselect = true;
46 $mbx_parent->mbxs[] = $no_select_mbx;
47 $i--;
48 }
49
50 }
51 $mbx_parent->mbxs[] = $mbx;
52 if ($mbx->is_special && $specialfirst) {
53 usort($mbx_parent->mbxs, 'sortSpecialMbx');
54 }
55
56 }
57}
58
59function sortSpecialMbx($a, $b) {
60 if ($a->is_inbox) {
61 $acmp = '0'. $a->mailboxname_full;
62 } else if ($a->is_special) {
63 $acmp = '1'. $a->mailboxname_full;
64 } else {
65 $acmp = '2' . $a->mailboxname_full;
66 }
67 if ($b->is_inbox) {
68 $bcmp = '0'. $b->mailboxname_full;
69 }else if ($b->is_special) {
70 $bcmp = '1' . $b->mailboxname_full;
71 } else {
72 $bcmp = '2' . $b->mailboxname_full;
73 }
74 if ($acmp == $bcmp) return 0;
75 return ($acmp>$bcmp) ? 1: -1;
76}
77
78
79e07c7e 79function find_mailbox_name ($mailbox) {
80 if (ereg(" *\"([^\r\n\"]*)\"[ \r\n]*$", $mailbox, $regs))
81 return $regs[1];
82 ereg(" *([^ \r\n\"]*)[ \r\n]*$",$mailbox,$regs);
83 return $regs[1];
84
85}
86
97b1248c 87/**
88 * If $haystack is a full mailbox name, and $needle is the mailbox
89 * separator character, returns the second last part of the full
90 * mailbox name (i.e. the mailbox's parent mailbox)
91 */
92function readMailboxParent($haystack, $needle) {
93
94 if ($needle == '') {
95 $ret = '';
96 } else {
97 $parts = explode($needle, $haystack);
98 $elem = array_pop($parts);
99 while ($elem == '' && count($parts)) {
100 $elem = array_pop($parts);
101 }
102 $ret = join($needle, $parts);
103 }
104 return( $ret );
105}
106
107
1e18bf95 108function isBoxBelow( $box2, $box1 ) {
1e18bf95 109 global $delimiter, $folder_prefix, $imap_server_type;
90de1755 110
fdc05b69 111 if ( $imap_server_type == 'uw' ) {
1e18bf95 112 $boxs = $box2;
113 $i = strpos( $box1, $delimiter, strlen( $folder_prefix ) );
3411d4ec 114 if ( $i === false ) {
1e18bf95 115 $i = strlen( $box2 );
fdc05b69 116 }
117 } else {
1e18bf95 118 $boxs = $box2 . $delimiter;
3411d4ec 119 /* Skip next second delimiter */
1e18bf95 120 $i = strpos( $box1, $delimiter );
121 $i = strpos( $box1, $delimiter, $i + 1 );
3411d4ec 122 if ( $i === false ) {
1e18bf95 123 $i = strlen( $box2 );
72b9aff9 124 } else {
fdc05b69 125 $i++;
4d6bd355 126 }
7980d569 127 }
72b9aff9 128
3411d4ec 129 return ( substr( $box1, 0, $i ) == substr( $boxs, 0, $i ) );
1e18bf95 130}
131
3411d4ec 132/* Defines special mailboxes */
1e18bf95 133function isSpecialMailbox( $box ) {
1e18bf95 134 global $trash_folder, $sent_folder, $draft_folder,
65c3ec94 135 $move_to_trash, $move_to_sent, $save_as_draft;
1e18bf95 136
90de1755 137 $ret = ( (strtolower($box) == 'inbox') ||
1e18bf95 138 ( $move_to_trash && isBoxBelow( $box, $trash_folder ) ) ||
139 ( $move_to_sent && isBoxBelow( $box, $sent_folder )) ||
ce628f00 140 ($save_as_draft && $box == $draft_folder ) );
90de1755 141
2586d588 142 if ( !$ret ) {
31524bcd 143 $ret = do_hook_function( 'special_mailbox', $box );
2586d588 144 }
145
3411d4ec 146 return $ret;
90de1755 147}
148
3411d4ec 149/* Expunges a mailbox */
8f6505f6 150function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true, $id='') {
63240b90 151 global $uid_support;
06b5c3ff 152 if ($id) {
8f6505f6 153 if (is_array($id)) {
154 $id = sqimap_message_list_squisher($id);
155 }
156 $id = ' '.$id;
06b5c3ff 157 $uid = $uid_support;
158 } else {
159 $uid = false;
8f6505f6 160 }
06b5c3ff 161 $read = sqimap_run_command($imap_stream, 'EXPUNGE'.$id, $handle_errors,
162 $response, $message, $uid);
63240b90 163 $cnt = 0;
164
165 if ( is_array( $read ) ) {
166 foreach ($read as $r) {
167 if (preg_match('/^\*\s[0-9]+\sEXPUNGE/AUi',$r,$regs)) {
168 $cnt++;
169 }
170 }
8f6505f6 171 }
172 return $cnt;
43b698c7 173}
174
3411d4ec 175/* Checks whether or not the specified mailbox exists */
1da22cda 176function sqimap_mailbox_exists ($imap_stream, $mailbox) {
43b698c7 177 if (! isset($mailbox)) {
178 return false;
179 }
1c72b151 180 $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
3411d4ec 181 true, $response, $message);
43b698c7 182 return isset($mbx[0]);
183}
184
3411d4ec 185/* Selects a mailbox */
e4c6fe41 186function sqimap_mailbox_select ($imap_stream, $mailbox) {
43b698c7 187 global $auto_expunge;
f69feefe 188
43b698c7 189 if ( $mailbox == 'None' ) {
190 return;
191 }
f69feefe 192
1c72b151 193 $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
3411d4ec 194 true, $response, $message);
e4c6fe41 195 $result = array();
196 for ($i=0; $i<count($read); $i++) {
197 if (preg_match('/^\*\s+OK\s\[(\w+)\s(\w+)\]/',$read[$i], $regs)) {
198 $result[strtoupper($regs[1])] = $regs[2];
199 } else if (preg_match('/^\*\s([0-9]+)\s(\w+)/',$read[$i], $regs)) {
200 $result[strtoupper($regs[2])] = $regs[1];
201 } else {
f69feefe 202 if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) {
203 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ;
204 $result['PERMANENTFLAGS'] = $regs[1];
205 }
206 else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) {
207 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ;
1da22cda 208 $result['FLAGS'] = $regs[1];
f69feefe 209 }
e4c6fe41 210 }
211 }
212 if (preg_match('/^\[(.+)\]/',$message, $regs)) {
213 $result['RIGHTS']=$regs[1];
214 }
f69feefe 215
e4c6fe41 216 if ($auto_expunge) {
217 $tmp = sqimap_run_command($imap_stream, 'EXPUNGE', false, $a, $b);
43b698c7 218 }
e4c6fe41 219 return $result;
43b698c7 220}
221
3411d4ec 222/* Creates a folder */
223function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
43b698c7 224 global $delimiter;
225 if (strtolower($type) == 'noselect') {
3411d4ec 226 $mailbox .= $delimiter;
43b698c7 227 }
447b2166 228 $mailbox = imap_utf7_encode_local($mailbox);
1c72b151 229 $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
3411d4ec 230 true, $response, $message);
43b698c7 231 sqimap_subscribe ($imap_stream, $mailbox);
232}
233
3411d4ec 234/* Subscribes to an existing folder */
235function sqimap_subscribe ($imap_stream, $mailbox) {
1a16af11 236 $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"",
3411d4ec 237 true, $response, $message);
43b698c7 238}
239
3411d4ec 240/* Unsubscribes to an existing folder */
241function sqimap_unsubscribe ($imap_stream, $mailbox) {
43b698c7 242 global $imap_server_type;
1a16af11 243 $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"",
3411d4ec 244 true, $response, $message);
43b698c7 245}
246
3411d4ec 247/* Deletes the given folder */
248function sqimap_mailbox_delete ($imap_stream, $mailbox) {
78cc4b12 249 global $data_dir, $username;
1c72b151 250 $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
3411d4ec 251 true, $response, $message);
43b698c7 252 sqimap_unsubscribe ($imap_stream, $mailbox);
3411d4ec 253 do_hook_function("rename_or_delete_folder", $args = array($mailbox, 'delete', ''));
78cc4b12 254 removePref($data_dir, $username, "thread_$mailbox");
43b698c7 255}
256
3411d4ec 257/* Determines if the user is subscribed to the folder or not */
1da22cda 258function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
1da22cda 259 $boxesall = sqimap_mailbox_list ($imap_stream);
260 foreach ($boxesall as $ref) {
43b698c7 261 if ($ref['unformatted'] == $folder) {
3411d4ec 262 return true;
43b698c7 263 }
264 }
265 return false;
266}
267
3411d4ec 268/* Renames a mailbox */
1c52ba77 269function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
3411d4ec 270 if ( $old_name != $new_name ) {
78cc4b12 271 global $delimiter, $imap_server_type, $data_dir, $username;
1c52ba77 272 if ( substr( $old_name, -1 ) == $delimiter ) {
273 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
274 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
275 $postfix = $delimiter;
1c52ba77 276 } else {
277 $postfix = '';
1c52ba77 278 }
648713af 279 $boxesall = sqimap_mailbox_list($imap_stream);
1c72b151 280 $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' . quoteIMAP($new_name) . '"';
3411d4ec 281 $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message);
1c52ba77 282 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
78cc4b12 283 $oldpref = getPref($data_dir, $username, "thread_".$old_name.$postfix);
284 removePref($data_dir, $username, "thread_".$old_name.$postfix);
1c52ba77 285 sqimap_subscribe($imap_stream, $new_name.$postfix);
78cc4b12 286 setPref($data_dir, $username, "thread_".$new_name.$postfix, $oldpref);
f5ab1fb9 287 do_hook_function("rename_or_delete_folder",$args = array($old_name, 'rename', $new_name));
648713af 288 $l = strlen( $old_name ) + 1;
289 $p = 'unformatted';
290 foreach ( $boxesall as $box ) {
291 if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) {
292 $new_sub = $new_name . $delimiter . substr($box[$p], $l);
293 if ($imap_server_type == 'cyrus') {
294 $cmd = 'RENAME "' . quoteIMAP($box[$p]) . '" "' . quoteIMAP($new_sub) . '"';
3411d4ec 295 $data = sqimap_run_command($imap_stream, $cmd, true,
648713af 296 $response, $message);
1c52ba77 297 }
648713af 298 sqimap_unsubscribe($imap_stream, $box[$p]);
78cc4b12 299 $oldpref = getPref($data_dir, $username, "thread_".$box[$p]);
300 removePref($data_dir, $username, "thread_".$box[$p]);
648713af 301 sqimap_subscribe($imap_stream, $new_sub);
78cc4b12 302 setPref($data_dir, $username, "thread_".$new_sub, $oldpref);
3411d4ec 303 do_hook_function("rename_or_delete_folder",
304 $args = array($box[$p], 'rename', $new_sub));
1c52ba77 305 }
306 }
1c52ba77 307 }
1c52ba77 308}
43b698c7 309
3411d4ec 310/*
311 * Formats a mailbox into 4 parts for the $boxesall array
312 *
313 * The four parts are:
314 *
315 * raw - Raw LIST/LSUB response from the IMAP server
316 * formatted - nicely formatted folder name
317 * unformatted - unformatted, but with delimiter at end removed
318 * unformatted-dm - folder name as it appears in raw response
319 * unformatted-disp - unformatted without $folder_prefix
320 */
321function sqimap_mailbox_parse ($line, $line_lsub) {
43b698c7 322 global $folder_prefix, $delimiter;
3411d4ec 323
43b698c7 324 /* Process each folder line */
325 for ($g=0; $g < count($line); $g++) {
3411d4ec 326
43b698c7 327 /* Store the raw IMAP reply */
328 if (isset($line[$g])) {
1da22cda 329 $boxesall[$g]["raw"] = $line[$g];
43b698c7 330 }
331 else {
3411d4ec 332 $boxesall[$g]["raw"] = '';
43b698c7 333 }
3411d4ec 334
335
43b698c7 336 /* Count number of delimiters ($delimiter) in folder name */
337 $mailbox = trim($line_lsub[$g]);
602e1431 338 $dm_count = substr_count($mailbox, $delimiter);
43b698c7 339 if (substr($mailbox, -1) == $delimiter) {
3411d4ec 340 /* If name ends in delimiter, decrement count by one */
341 $dm_count--;
43b698c7 342 }
3411d4ec 343
344 /* Format folder name, but only if it's a INBOX.* or has a parent. */
1da22cda 345 $boxesallbyname[$mailbox] = $g;
43b698c7 346 $parentfolder = readMailboxParent($mailbox, $delimiter);
347 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
348 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
1da22cda 349 ( isset($boxesallbyname[$parentfolder]) &&
43b698c7 350 (strlen($parentfolder) > 0) ) ) {
602e1431 351 $indent = $dm_count - ( substr_count($folder_prefix, $delimiter));
43b698c7 352 if ($indent > 0) {
3411d4ec 353 $boxesall[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $indent);
43b698c7 354 }
355 else {
1da22cda 356 $boxesall[$g]['formatted'] = '';
43b698c7 357 }
447b2166 358 $boxesall[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
43b698c7 359 }
360 else {
447b2166 361 $boxesall[$g]['formatted'] = imap_utf7_decode_local($mailbox);
43b698c7 362 }
90de1755 363
1da22cda 364 $boxesall[$g]['unformatted-dm'] = $mailbox;
43b698c7 365 if (substr($mailbox, -1) == $delimiter) {
8e9e8afa 366 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
43b698c7 367 }
1da22cda 368 $boxesall[$g]['unformatted'] = $mailbox;
43b698c7 369 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
631b9da3 370 $mailbox = substr($mailbox, strlen($folder_prefix));
43b698c7 371 }
1da22cda 372 $boxesall[$g]['unformatted-disp'] = $mailbox;
373 $boxesall[$g]['id'] = $g;
90de1755 374
1da22cda 375 $boxesall[$g]['flags'] = array();
43b698c7 376 if (isset($line[$g])) {
36dfb0c9 377 ereg("\(([^)]*)\)",$line[$g],$regs);
1a7e1e97 378 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
43b698c7 379 if ($flags) {
1da22cda 380 $boxesall[$g]['flags'] = explode(' ', $flags);
43b698c7 381 }
382 }
383 }
90de1755 384
1da22cda 385 return $boxesall;
43b698c7 386}
387
3411d4ec 388/*
a3439b27 389 * Sorting function used to sort mailbox names.
3411d4ec 390 * + Original patch from dave_michmerhuizen@yahoo.com
391 * + Allows case insensitivity when sorting folders
392 * + Takes care of the delimiter being sorted to the end, causing
393 * subfolders to be listed in below folders that are prefixed
394 * with their parent folders name.
395 *
396 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
397 * Without special sort function: foobar between foo and foo.bar
398 * With special sort function: foobar AFTER foo and foo.bar :)
43b698c7 399 */
a3439b27 400function user_strcasecmp($a, $b) {
401 global $delimiter;
402
403 /* Calculate the length of some strings. */
404 $a_length = strlen($a);
405 $b_length = strlen($b);
406 $min_length = min($a_length, $b_length);
407 $delimiter_length = strlen($delimiter);
408
409 /* Set the initial result value. */
410 $result = 0;
411
412 /* Check the strings... */
413 for ($c = 0; $c < $min_length; ++$c) {
414 $a_del = substr($a, $c, $delimiter_length);
415 $b_del = substr($b, $c, $delimiter_length);
416
417 if (($a_del == $delimiter) && ($b_del == $delimiter)) {
418 $result = 0;
419 } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
a3439b27 420 $result = -1;
c64c33f4 421 } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
422 $result = 1;
a3439b27 423 } else {
424 $result = strcasecmp($a{$c}, $b{$c});
425 }
426
427 if ($result != 0) {
428 break;
429 }
430 }
90de1755 431
a3439b27 432 /* If one string is a prefix of the other... */
433 if ($result == 0) {
434 if ($a_length < $b_length) {
435 $result = -1;
436 } else if ($a_length > $b_length) {
437 $result = 1;
438 }
439 }
440
3411d4ec 441 return $result;
43b698c7 442}
443
444
3411d4ec 445/*
446 * Returns sorted mailbox lists in several different ways.
447 * See comment on sqimap_mailbox_parse() for info about the returned array.
448 */
1da22cda 449function sqimap_mailbox_list($imap_stream) {
4b2fe13a 450 global $default_folder_prefix;
7e235a1a 451
452 if ( !isset( $boxesnew ) ) {
453
3411d4ec 454 global $data_dir, $username, $list_special_folders_first,
7e235a1a 455 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
456 $move_to_trash, $move_to_sent, $save_as_draft,
ca85aabe 457 $delimiter, $noselect_fix_enable;
7e235a1a 458
3411d4ec 459 $inbox_in_list = false;
460 $inbox_subscribed = false;
7e235a1a 461
462 require_once('../src/load_prefs.php');
463 require_once('../functions/array.php');
464
ca85aabe 465 if ($noselect_fix_enable) {
466 $lsub_args = "LSUB \"$folder_prefix\" \"*%\"";
467 }
468 else {
469 $lsub_args = "LSUB \"$folder_prefix\" \"*\"";
470 }
3411d4ec 471 /* LSUB array */
ca85aabe 472 $lsub_ary = sqimap_run_command ($imap_stream, $lsub_args,
3411d4ec 473 true, $response, $message);
7e235a1a 474
ca85aabe 475
3411d4ec 476 /*
477 * Section about removing the last element was removed
478 * We don't return "* OK" anymore from sqimap_read_data
479 */
7e235a1a 480
481 $sorted_lsub_ary = array();
482 for ($i=0;$i < count($lsub_ary); $i++) {
3411d4ec 483 /*
484 * Workaround for EIMS
485 * Doesn't work if the mailbox name is multiple lines
486 */
7e235a1a 487 if (isset($lsub_ary[$i + 1]) &&
488 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
489 $lsub_ary[$i], $regs)) {
490 $i ++;
3411d4ec 491 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
7e235a1a 492 }
493 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
494 $sorted_lsub_ary[] = $temp_mailbox_name;
495 if (strtoupper($temp_mailbox_name) == 'INBOX') {
3411d4ec 496 $inbox_subscribed = true;
7e235a1a 497 }
498 }
499 $new_ary = array();
500 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
501 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
502 $new_ary[] = $sorted_lsub_ary[$i];
503 }
504 }
505 $sorted_lsub_ary = $new_ary;
506 if (isset($sorted_lsub_ary)) {
507 usort($sorted_lsub_ary, 'user_strcasecmp');
508 }
509
3411d4ec 510 /* LIST array */
7e235a1a 511 $sorted_list_ary = array();
512 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
513 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
514 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
515 }
516 else {
517 $mbx = $sorted_lsub_ary[$i];
518 }
519
520 $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
3411d4ec 521 true, $response, $message);
7e235a1a 522 /* Another workaround for EIMS */
523 if (isset($read[1]) &&
524 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
525 $read[0], $regs)) {
3411d4ec 526 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) . '"' . $regs[2];
7e235a1a 527 }
528
529 if (isset($sorted_list_ary[$i])) {
530 $sorted_list_ary[$i] = '';
531 }
532
533 if (isset($read[0])) {
534 $sorted_list_ary[$i] = $read[0];
535 }
536 else {
537 $sorted_list_ary[$i] = '';
538 }
539
540 if (isset($sorted_list_ary[$i]) &&
541 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
3411d4ec 542 $inbox_in_list = true;
7e235a1a 543 }
544 }
545
3411d4ec 546 /*
7e235a1a 547 * Just in case they're not subscribed to their inbox,
548 * we'll get it for them anyway
549 */
3411d4ec 550 if (!$inbox_subscribed || !$inbox_in_list) {
7e235a1a 551 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
3411d4ec 552 true, $response, $message);
7e235a1a 553 /* Another workaround for EIMS */
554 if (isset($inbox_ary[1]) &&
555 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
556 $inbox_ary[0], $regs)) {
557 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
558 '"' . $regs[2];
559 }
560
561 $sorted_list_ary[] = $inbox_ary[0];
562 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
563 }
564
565 $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
566
3411d4ec 567 /* Now, lets sort for special folders */
7e235a1a 568 $boxesnew = $used = array();
569
570 /* Find INBOX */
571 foreach ( $boxesall as $k => $box ) {
572 if ( strtolower($box['unformatted']) == 'inbox') {
573 $boxesnew[] = $box;
3411d4ec 574 $used[$k] = true;
7e235a1a 575 } else {
3411d4ec 576 $used[$k] = false;
7e235a1a 577 }
578 }
7e235a1a 579 /* List special folders and their subfolders, if requested. */
3411d4ec 580 if ($list_special_folders_first) {
7e235a1a 581 foreach ( $boxesall as $k => $box ) {
3411d4ec 582 if ( !$used[$k] && isSpecialMailbox( $box['unformatted'] ) ) {
7e235a1a 583 $boxesnew[] = $box;
3411d4ec 584 $used[$k] = true;
7e235a1a 585 }
6f369100 586 $spec_sub = str_replace('&nbsp;', '', $box['formatted']);
51aa728e 587 $spec_sub = preg_replace("/(\*|\[|\]|\(|\)|\?|\+|\{|\}|\^|\\$)/", '\\\\'.'\\1', $spec_sub);
3d1560e7 588
589 /* In case of problems with preg
590 here is a ereg version
591 if (!$used[$k] && ereg("^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$", $box['unformatted']) ) { */
592
593 if (!$used[$k] && preg_match("?^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$?", $box['unformatted']) ) {
7ce96393 594 $boxesnew[] = $box;
595 $used[$k] = true;
596 }
7e235a1a 597 }
598
599 }
7e235a1a 600 /* Rest of the folders */
601 foreach ( $boxesall as $k => $box ) {
602 if ( !$used[$k] ) {
603 $boxesnew[] = $box;
604 }
605 }
43b698c7 606 }
3411d4ec 607 return $boxesnew;
43b698c7 608}
609
90de1755 610/*
611 * Returns a list of all folders, subscribed or not
612 */
1da22cda 613function sqimap_mailbox_list_all($imap_stream) {
3411d4ec 614 global $list_special_folders_first, $folder_prefix, $delimiter;
90de1755 615
65c3ec94 616 require_once('../functions/array.php');
90de1755 617
43b698c7 618 $ssid = sqimap_session_id();
90de1755 619 $lsid = strlen( $ssid );
43b698c7 620 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
3411d4ec 621 $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message);
43b698c7 622 $g = 0;
90de1755 623 $phase = 'inbox';
624
43b698c7 625 for ($i = 0; $i < count($read_ary); $i++) {
626 /* Another workaround for EIMS */
627 if (isset($read_ary[$i + 1]) &&
90de1755 628 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
43b698c7 629 $read_ary[$i], $regs)) {
630 $i ++;
3411d4ec 631 $read_ary[$i] = $regs[1] . '"' . addslashes(trim($read_ary[$i])) . '"' . $regs[2];
43b698c7 632 }
90de1755 633 if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
634
43b698c7 635 /* Store the raw IMAP reply */
65c3ec94 636 $boxes[$g]['raw'] = $read_ary[$i];
1c52ba77 637
43b698c7 638 /* Count number of delimiters ($delimiter) in folder name */
8e9e8afa 639 $mailbox = find_mailbox_name($read_ary[$i]);
602e1431 640 $dm_count = substr_count($mailbox, $delimiter);
43b698c7 641 if (substr($mailbox, -1) == $delimiter) {
0cfd745a 642 /* If name ends in delimiter - decrement count by one */
90de1755 643 $dm_count--;
43b698c7 644 }
90de1755 645
3411d4ec 646 /* Format folder name, but only if it's a INBOX.* or has a parent. */
1da22cda 647 $boxesallbyname[$mailbox] = $g;
525b7ae6 648 $parentfolder = readMailboxParent($mailbox, $delimiter);
90de1755 649 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
146e0c45 650 (ereg('^'.$folder_prefix, $mailbox)) ||
1da22cda 651 ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
43b698c7 652 if ($dm_count) {
90de1755 653 $boxes[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $dm_count);
43b698c7 654 }
655 else {
90de1755 656 $boxes[$g]['formatted'] = '';
43b698c7 657 }
447b2166 658 $boxes[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
2752bb16 659 }
43b698c7 660 else {
447b2166 661 $boxes[$g]['formatted'] = imap_utf7_decode_local($mailbox);
43b698c7 662 }
90de1755 663
1da22cda 664 $boxes[$g]['unformatted-dm'] = $mailbox;
43b698c7 665 if (substr($mailbox, -1) == $delimiter) {
666 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
667 }
90de1755 668 $boxes[$g]['unformatted'] = $mailbox;
3411d4ec 669 $boxes[$g]['unformatted-disp'] = ereg_replace('^' . $folder_prefix, '', $mailbox);
90de1755 670 $boxes[$g]['id'] = $g;
671
3411d4ec 672 /* Now lets get the flags for this mailbox */
1c72b151 673 $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
3411d4ec 674 true, $response, $message);
90de1755 675
43b698c7 676 /* Another workaround for EIMS */
677 if (isset($read_mlbx[1]) &&
3411d4ec 678 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", $read_mlbx[0], $regs)) {
679 $read_mlbx[0] = $regs[1] . '"' . addslashes(trim($read_mlbx[1])) . '"' . $regs[2];
43b698c7 680 }
90de1755 681
682 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+1);
683 $flags = substr($flags, 0, strpos($flags, ')'));
146e0c45 684 $flags = str_replace('\\', '', $flags);
8e9e8afa 685 $flags = trim(strtolower($flags));
686 if ($flags) {
1c52ba77 687 $boxes[$g]['flags'] = explode(' ', $flags);
90de1755 688 } else {
43b698c7 689 $boxes[$g]['flags'] = array();
12d61439 690 }
43b698c7 691 }
692 $g++;
693 }
694 if(is_array($boxes)) {
1c52ba77 695 $boxes = ary_sort ($boxes, 'unformatted', 1);
43b698c7 696 }
90de1755 697
43b698c7 698 return $boxes;
699}
5bdd7223 700
60b5724d 701function sqimap_mailbox_tree($imap_stream) {
702 global $boxesnew, $default_folder_prefix, $unseen_notify, $unseen_type;
703 if ( !isset( $boxesnew ) ) {
704
705 global $data_dir, $username, $list_special_folders_first,
e4c6fe41 706 $folder_prefix, $delimiter, $trash_folder, $move_to_trash;
60b5724d 707
708
709 $inbox_in_list = false;
710 $inbox_subscribed = false;
711
712 require_once('../src/load_prefs.php');
713 require_once('../functions/array.php');
714
715 /* LSUB array */
2c617aa5 716 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
60b5724d 717 true, $response, $message);
718
719 /*
720 * Section about removing the last element was removed
721 * We don't return "* OK" anymore from sqimap_read_data
722 */
723 $sorted_lsub_ary = array();
e4c6fe41 724 $cnt = count($lsub_ary);
725 for ($i=0;$i < $cnt; $i++) {
60b5724d 726 /*
727 * Workaround for EIMS
728 * Doesn't work if the mailbox name is multiple lines
729 */
730 if (isset($lsub_ary[$i + 1]) &&
731 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
732 $lsub_ary[$i], $regs)) {
733 $i ++;
734 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
735 }
736
c388554f 737// if (preg_match("/^\*\s+LSUB\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$lsub_ary[$i],$regs)) {
738// $flag = $regs[1];
739// $mbx = trim($regs[3]);
740// $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
741// }
742 $mbx = find_mailbox_name($lsub_ary[$i]);
743 if (substr($mbx, -1) == $delimiter) {
744 $mbx = substr($mbx, 0, strlen($mbx) - 1);
745 }
746 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => '');
60b5724d 747 }
e4c6fe41 748 array_multisort($sorted_lsub_ary, SORT_ASC, SORT_REGULAR);
60b5724d 749
750 foreach ($sorted_lsub_ary as $mbx) {
751 if ($mbx['mbx'] == 'INBOX') {
752 $inbox_in_list = true;
753 break;
754 }
755 }
756
757 /*
758 * Just in case they're not subscribed to their inbox,
759 * we'll get it for them anyway
760 */
761 if (!$inbox_in_list) {
762 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
763 true, $response, $message);
764 /* Another workaround for EIMS */
765 if (isset($inbox_ary[1]) &&
766 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
767 $inbox_ary[0], $regs)) {
768 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
769 '"' . $regs[2];
770 }
c388554f 771 $mbx = find_mailbox_name($inbox_ary[0]);
772 if (substr($mbx, -1) == $delimiter) {
773 $mbx = substr($mbx, 0, strlen($mbx) - 1);
774 }
775 if ( $mbx == 'INBOX') {
776 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => '');
777 sqimap_subscribe($imap_stream, 'INBOX');
778 }
779
780// if (preg_match("/^\*\s+LIST\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$inbox_ary[0],$regs)) {
781// $flag = $regs[1];
782// $mbx = trim($regs[3]);
783// if (substr($mbx, -1) == $delimiter) {
784// $mbx = substr($mbx, 0, strlen($mbx) - 1);
785// }
786// $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
787// }
60b5724d 788 }
e4c6fe41 789 $cnt = count($sorted_lsub_ary);
790 for ($i=0 ; $i < $cnt; $i++) {
791 $mbx = $sorted_lsub_ary[$i]['mbx'];
792 if (($unseen_notify == 2 && $mbx == 'INBOX')
60b5724d 793 || $unseen_notify == 3
e4c6fe41 794 || ($move_to_trash && ($mbx == $trash_folder))) {
795 $sorted_lsub_ary[$i]['unseen'] = sqimap_unseen_messages($imap_stream, $mbx);
60b5724d 796 if ($unseen_type == 2 || ($move_to_trash
e4c6fe41 797 && ($mbx == $trash_folder) )) {
798 $sorted_lsub_ary[$i]['nummessages'] = sqimap_get_num_messages($imap_stream, $mbx);
60b5724d 799 }
e4c6fe41 800 if ($mbx == $trash_folder) {
801 $sorted_lsub_ary[$i]['nummessages'] = sqimap_get_num_messages($imap_stream, $mbx);
802 }
60b5724d 803 }
804 }
60b5724d 805 $boxesnew = sqimap_fill_mailbox_tree($sorted_lsub_ary);
806 return $boxesnew;
807 }
808}
809
810
811function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false) {
812 global $data_dir, $username, $list_special_folders_first,
813 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
814 $move_to_trash, $move_to_sent, $save_as_draft,
815 $delimiter;
816
817 $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
818
819 /* create virtual root node */
820 $mailboxes= new mailboxes();
821 $mailboxes->is_root = true;
822 $trail_del = false;
823 if (isset($folder_prefix) && $folder_prefix != '') {
824 $start = substr_count($folder_prefix,$delimiter);
825 if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
826 $trail_del = true;
827 $mailboxes->mailboxname_full = substr($folder_prefix,0, (strlen($folder_prefix)-1));
828 } else {
829 $mailboxes->mailboxname_full = $folder_prefix;
830 $start++;
831 }
832 $mailboxes->mailboxname_sub = $mailboxes->mailboxname_full;
833 } else $start = 0;
e4c6fe41 834 $cnt = count($mbx_ary);
835 for ($i=0; $i < $cnt; $i++) {
60b5724d 836 if ($mbx_ary[$i]['mbx'] !='' ) {
837 $mbx = new mailboxes();
838 $mailbox = $mbx_ary[$i]['mbx'];
839 switch ($mailbox) {
840 case 'INBOX':
841 $mbx->is_inbox = true;
842 $mbx->is_special = true;
843 break;
844 case $trash_folder:
845 $mbx->is_trash = true;
846 $mbx->is_special = true;
847 break;
848 case $sent_folder:
849 $mbx->is_sent = true;
850 $mbx->is_special = true;
851 break;
852 case $draft_folder:
853 $mbx->is_draft = true;
854 $mbx->is_special = true;
855 break;
856 }
857
858 if (isset($mbx_ary[$i]['unseen'])) {
859 $mbx->unseen = $mbx_ary[$i]['unseen'];
860 }
861 if (isset($mbx_ary[$i]['nummessages'])) {
862 $mbx->total = $mbx_ary[$i]['nummessages'];
863 }
864
865 $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
866 if ($r_del_pos) {
867 $mbx->mailboxname_sub = substr($mbx_ary[$i]['mbx'],$r_del_pos+1);
868 } else { /* mailbox is root folder */
869 $mbx->mailboxname_sub = $mbx_ary[$i]['mbx'];
870 }
871 $mbx->mailboxname_full = $mbx_ary[$i]['mbx'];
872 $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);
873 }
874 }
875
876 return $mailboxes;
877}
878
879
648713af 880?>