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