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