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