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