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