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