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