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