6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This impliments all functions that manipulate mailboxes
14 require_once('../functions/imap_utf7_encode_local.php');
15 require_once('../functions/imap_utf7_decode_local.php');
20 var $mailboxname_full = '', $mailboxname_sub= '', $is_noselect = false,
21 $is_special = false, $is_root = false, $is_inbox = false, $is_sent = false,
22 $is_trash = false, $is_draft = false, $mbxs = array(),
23 $unseen = false, $total = false;
25 function addMbx($mbx, $delimiter, $start, $specialfirst) {
26 $ary = explode($delimiter, $mbx->mailboxname_full
);
28 for ($i=$start; $i < (count($ary) -1); $i++
) {
29 $mbx_childs = &$mbx_parent->mbxs
;
31 foreach ($mbx_childs as $key => $parent) {
32 if ($parent->mailboxname_sub
== $ary[$i]) {
33 $mbx_parent = &$mbx_parent->mbxs
[$key];
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];
42 $no_select_mbx->mailboxname_full
= $ary[$i];
44 $no_select_mbx->mailboxname_sub
= $ary[$i];
45 $no_select_mbx->is_noselect
= true;
46 $mbx_parent->mbxs
[] = $no_select_mbx;
51 $mbx_parent->mbxs
[] = $mbx;
52 if ($mbx->is_special
&& $specialfirst) {
53 usort($mbx_parent->mbxs
, 'sortSpecialMbx');
59 function sortSpecialMbx($a, $b) {
61 $acmp = '0'. $a->mailboxname_full
;
62 } else if ($a->is_special
) {
63 $acmp = '1'. $a->mailboxname_full
;
65 $acmp = '2' . $a->mailboxname_full
;
68 $bcmp = '0'. $b->mailboxname_full
;
69 }else if ($b->is_special
) {
70 $bcmp = '1' . $b->mailboxname_full
;
72 $bcmp = '2' . $b->mailboxname_full
;
74 if ($acmp == $bcmp) return 0;
75 return ($acmp>$bcmp) ?
1: -1;
79 function find_mailbox_name ($mailbox) {
80 if (ereg(" *\"([^\r\n\"]*)\"[ \r\n]*$", $mailbox, $regs))
82 ereg(" *([^ \r\n\"]*)[ \r\n]*$",$mailbox,$regs);
88 * If $haystack is a full mailbox name, and $needle is the mailbox
89 * separator character, returns the second last part of the full
90 * mailbox name (i.e. the mailbox's parent mailbox)
92 function readMailboxParent($haystack, $needle) {
97 $parts = explode($needle, $haystack);
98 $elem = array_pop($parts);
99 while ($elem == '' && count($parts)) {
100 $elem = array_pop($parts);
102 $ret = join($needle, $parts);
108 function isBoxBelow( $box2, $box1 ) {
109 global $delimiter, $folder_prefix, $imap_server_type;
111 if ( $imap_server_type == 'uw' ) {
113 $i = strpos( $box1, $delimiter, strlen( $folder_prefix ) );
114 if ( $i === false ) {
115 $i = strlen( $box2 );
118 $boxs = $box2 . $delimiter;
119 /* Skip next second delimiter */
120 $i = strpos( $box1, $delimiter );
121 $i = strpos( $box1, $delimiter, $i +
1 );
122 if ( $i === false ) {
123 $i = strlen( $box2 );
129 return ( substr( $box1, 0, $i ) == substr( $boxs, 0, $i ) );
132 /* Defines special mailboxes */
133 function isSpecialMailbox( $box ) {
134 global $trash_folder, $sent_folder, $draft_folder,
135 $move_to_trash, $move_to_sent, $save_as_draft;
137 $ret = ( (strtolower($box) == 'inbox') ||
138 ( $move_to_trash && isBoxBelow( $box, $trash_folder ) ) ||
139 ( $move_to_sent && isBoxBelow( $box, $sent_folder )) ||
140 ($save_as_draft && $box == $draft_folder ) );
143 $ret = do_hook_function( 'special_mailbox', $box );
149 /* Expunges a mailbox */
150 function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true) {
151 $read = sqimap_run_command($imap_stream, 'EXPUNGE', $handle_errors,
152 $response, $message);
155 /* Checks whether or not the specified mailbox exists */
156 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
157 if (! isset($mailbox)) {
160 $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
161 true, $response, $message);
162 return isset($mbx[0]);
165 /* Selects a mailbox */
166 function sqimap_mailbox_select ($imap_stream, $mailbox,
167 $hide = true, $recent = false, $extrainfo = false) {
168 global $auto_expunge;
170 if ( $mailbox == 'None' ) {
174 $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
175 true, $response, $message);
177 for ($i=0; $i<count($read); $i++
) {
178 if (strpos(strtolower($read[$i]), 'recent')) {
179 $r = explode(' ', $read[$i]);
185 $tmp = sqimap_run_command($imap_stream, 'EXPUNGE', false, $a, $b);
187 if (isset( $extrainfo ) && $extrainfo) {
189 for ($i=0; $i<count($read); $i++
) {
190 if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) {
191 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ;
192 $result['PERMANENTFLAGS'] = $regs[1];
194 else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) {
195 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ;
196 $result['FLAGS'] = $regs[1];
198 else if (preg_match("/(.*)EXISTS/i",$read[$i], $regs)) {
199 $result['EXISTS']=trim($regs[1]);
201 else if (preg_match("/(.*)RECENT/i",$read[$i], $regs)) {
202 $result['RECENT']=trim($regs[1]);
204 else if (preg_match("/\[UNSEEN(.*)\]/i",$read[$i], $regs)) {
205 $result['UNSEEN']=trim($regs[1]);
214 /* Creates a folder */
215 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
217 if (strtolower($type) == 'noselect') {
218 $mailbox .= $delimiter;
221 $mailbox = imap_utf7_encode_local($mailbox);
223 $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
224 true, $response, $message);
225 sqimap_subscribe ($imap_stream, $mailbox);
228 /* Subscribes to an existing folder */
229 function sqimap_subscribe ($imap_stream, $mailbox) {
230 $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"",
231 true, $response, $message);
234 /* Unsubscribes to an existing folder */
235 function sqimap_unsubscribe ($imap_stream, $mailbox) {
236 global $imap_server_type;
237 $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"",
238 true, $response, $message);
241 /* Deletes the given folder */
242 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
243 global $data_dir, $username;
244 $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
245 true, $response, $message);
246 sqimap_unsubscribe ($imap_stream, $mailbox);
247 do_hook_function("rename_or_delete_folder", $args = array($mailbox, 'delete', ''));
248 removePref($data_dir, $username, "thread_$mailbox");
251 /* Determines if the user is subscribed to the folder or not */
252 function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
253 $boxesall = sqimap_mailbox_list ($imap_stream);
254 foreach ($boxesall as $ref) {
255 if ($ref['unformatted'] == $folder) {
262 /* Renames a mailbox */
263 function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
264 if ( $old_name != $new_name ) {
265 global $delimiter, $imap_server_type, $data_dir, $username;
266 if ( substr( $old_name, -1 ) == $delimiter ) {
267 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
268 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
269 $postfix = $delimiter;
273 $boxesall = sqimap_mailbox_list($imap_stream);
274 $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' . quoteIMAP($new_name) . '"';
275 $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message);
276 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
277 $oldpref = getPref($data_dir, $username, "thread_".$old_name.$postfix);
278 removePref($data_dir, $username, "thread_".$old_name.$postfix);
279 sqimap_subscribe($imap_stream, $new_name.$postfix);
280 setPref($data_dir, $username, "thread_".$new_name.$postfix, $oldpref);
281 do_hook_function("rename_or_delete_folder",$args = array($old_name, 'rename', $new_name));
282 $l = strlen( $old_name ) +
1;
284 foreach ( $boxesall as $box ) {
285 if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) {
286 $new_sub = $new_name . $delimiter . substr($box[$p], $l);
287 if ($imap_server_type == 'cyrus') {
288 $cmd = 'RENAME "' . quoteIMAP($box[$p]) . '" "' . quoteIMAP($new_sub) . '"';
289 $data = sqimap_run_command($imap_stream, $cmd, true,
290 $response, $message);
292 sqimap_unsubscribe($imap_stream, $box[$p]);
293 $oldpref = getPref($data_dir, $username, "thread_".$box[$p]);
294 removePref($data_dir, $username, "thread_".$box[$p]);
295 sqimap_subscribe($imap_stream, $new_sub);
296 setPref($data_dir, $username, "thread_".$new_sub, $oldpref);
297 do_hook_function("rename_or_delete_folder",
298 $args = array($box[$p], 'rename', $new_sub));
305 * Formats a mailbox into 4 parts for the $boxesall array
307 * The four parts are:
309 * raw - Raw LIST/LSUB response from the IMAP server
310 * formatted - nicely formatted folder name
311 * unformatted - unformatted, but with delimiter at end removed
312 * unformatted-dm - folder name as it appears in raw response
313 * unformatted-disp - unformatted without $folder_prefix
315 function sqimap_mailbox_parse ($line, $line_lsub) {
316 global $folder_prefix, $delimiter;
318 /* Process each folder line */
319 for ($g=0; $g < count($line); $g++
) {
321 /* Store the raw IMAP reply */
322 if (isset($line[$g])) {
323 $boxesall[$g]["raw"] = $line[$g];
326 $boxesall[$g]["raw"] = '';
330 /* Count number of delimiters ($delimiter) in folder name */
331 $mailbox = trim($line_lsub[$g]);
332 $dm_count = substr_count($mailbox, $delimiter);
333 if (substr($mailbox, -1) == $delimiter) {
334 /* If name ends in delimiter, decrement count by one */
338 /* Format folder name, but only if it's a INBOX.* or has a parent. */
339 $boxesallbyname[$mailbox] = $g;
340 $parentfolder = readMailboxParent($mailbox, $delimiter);
341 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
342 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
343 ( isset($boxesallbyname[$parentfolder]) &&
344 (strlen($parentfolder) > 0) ) ) {
345 $indent = $dm_count - ( substr_count($folder_prefix, $delimiter));
347 $boxesall[$g]['formatted'] = str_repeat(' ', $indent);
350 $boxesall[$g]['formatted'] = '';
352 $boxesall[$g]['formatted'] .= imap_utf7_decode_local(
353 readShortMailboxName($mailbox, $delimiter));
356 $boxesall[$g]['formatted'] = imap_utf7_decode_local($mailbox);
359 $boxesall[$g]['unformatted-dm'] = $mailbox;
360 if (substr($mailbox, -1) == $delimiter) {
361 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
363 $boxesall[$g]['unformatted'] = $mailbox;
364 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
365 $mailbox = substr($mailbox, strlen($folder_prefix));
367 $boxesall[$g]['unformatted-disp'] = $mailbox;
368 $boxesall[$g]['id'] = $g;
370 $boxesall[$g]['flags'] = array();
371 if (isset($line[$g])) {
372 ereg("\(([^)]*)\)",$line[$g],$regs);
373 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
375 $boxesall[$g]['flags'] = explode(' ', $flags);
384 * Sorting function used to sort mailbox names.
385 * + Original patch from dave_michmerhuizen@yahoo.com
386 * + Allows case insensitivity when sorting folders
387 * + Takes care of the delimiter being sorted to the end, causing
388 * subfolders to be listed in below folders that are prefixed
389 * with their parent folders name.
391 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
392 * Without special sort function: foobar between foo and foo.bar
393 * With special sort function: foobar AFTER foo and foo.bar :)
395 function user_strcasecmp($a, $b) {
398 /* Calculate the length of some strings. */
399 $a_length = strlen($a);
400 $b_length = strlen($b);
401 $min_length = min($a_length, $b_length);
402 $delimiter_length = strlen($delimiter);
404 /* Set the initial result value. */
407 /* Check the strings... */
408 for ($c = 0; $c < $min_length; ++
$c) {
409 $a_del = substr($a, $c, $delimiter_length);
410 $b_del = substr($b, $c, $delimiter_length);
412 if (($a_del == $delimiter) && ($b_del == $delimiter)) {
414 } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
416 } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
419 $result = strcasecmp($a{$c}, $b{$c});
427 /* If one string is a prefix of the other... */
429 if ($a_length < $b_length) {
431 } else if ($a_length > $b_length) {
441 * Returns sorted mailbox lists in several different ways.
442 * See comment on sqimap_mailbox_parse() for info about the returned array.
444 function sqimap_mailbox_list($imap_stream) {
445 global $boxesnew, $default_folder_prefix;
447 if ( !isset( $boxesnew ) ) {
449 global $data_dir, $username, $list_special_folders_first,
450 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
451 $move_to_trash, $move_to_sent, $save_as_draft,
454 $inbox_in_list = false;
455 $inbox_subscribed = false;
457 require_once('../src/load_prefs.php');
458 require_once('../functions/array.php');
461 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*%\"",
462 true, $response, $message);
465 * Section about removing the last element was removed
466 * We don't return "* OK" anymore from sqimap_read_data
469 $sorted_lsub_ary = array();
470 for ($i=0;$i < count($lsub_ary); $i++
) {
472 * Workaround for EIMS
473 * Doesn't work if the mailbox name is multiple lines
475 if (isset($lsub_ary[$i +
1]) &&
476 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
477 $lsub_ary[$i], $regs)) {
479 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
481 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
482 $sorted_lsub_ary[] = $temp_mailbox_name;
483 if (strtoupper($temp_mailbox_name) == 'INBOX') {
484 $inbox_subscribed = true;
488 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
489 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
490 $new_ary[] = $sorted_lsub_ary[$i];
493 $sorted_lsub_ary = $new_ary;
494 if (isset($sorted_lsub_ary)) {
495 usort($sorted_lsub_ary, 'user_strcasecmp');
499 $sorted_list_ary = array();
500 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
501 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
502 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
505 $mbx = $sorted_lsub_ary[$i];
508 $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
509 true, $response, $message);
510 /* Another workaround for EIMS */
511 if (isset($read[1]) &&
512 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
514 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) . '"' . $regs[2];
517 if (isset($sorted_list_ary[$i])) {
518 $sorted_list_ary[$i] = '';
521 if (isset($read[0])) {
522 $sorted_list_ary[$i] = $read[0];
525 $sorted_list_ary[$i] = '';
528 if (isset($sorted_list_ary[$i]) &&
529 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
530 $inbox_in_list = true;
535 * Just in case they're not subscribed to their inbox,
536 * we'll get it for them anyway
538 if (!$inbox_subscribed ||
!$inbox_in_list) {
539 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
540 true, $response, $message);
541 /* Another workaround for EIMS */
542 if (isset($inbox_ary[1]) &&
543 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
544 $inbox_ary[0], $regs)) {
545 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
549 $sorted_list_ary[] = $inbox_ary[0];
550 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
553 $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
555 /* Now, lets sort for special folders */
556 $boxesnew = $used = array();
559 foreach ( $boxesall as $k => $box ) {
560 if ( strtolower($box['unformatted']) == 'inbox') {
567 /* List special folders and their subfolders, if requested. */
568 if ($list_special_folders_first) {
569 foreach ( $boxesall as $k => $box ) {
570 if ( !$used[$k] && isSpecialMailbox( $box['unformatted'] ) ) {
574 $spec_sub = str_replace(' ', '', $box['formatted']);
576 /* In case of problems with preg
577 here is a ereg version
578 if (!$used[$k] && ereg("^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$", $box['unformatted']) ) { */
580 if (!$used[$k] && preg_match("?^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$?", $box['unformatted']) ) {
587 /* Rest of the folders */
588 foreach ( $boxesall as $k => $box ) {
598 * Returns a list of all folders, subscribed or not
600 function sqimap_mailbox_list_all($imap_stream) {
601 global $list_special_folders_first, $folder_prefix, $delimiter;
603 require_once('../functions/array.php');
605 $ssid = sqimap_session_id();
606 $lsid = strlen( $ssid );
607 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
608 $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message);
612 for ($i = 0; $i < count($read_ary); $i++
) {
613 /* Another workaround for EIMS */
614 if (isset($read_ary[$i +
1]) &&
615 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
616 $read_ary[$i], $regs)) {
618 $read_ary[$i] = $regs[1] . '"' . addslashes(trim($read_ary[$i])) . '"' . $regs[2];
620 if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
622 /* Store the raw IMAP reply */
623 $boxes[$g]['raw'] = $read_ary[$i];
625 /* Count number of delimiters ($delimiter) in folder name */
626 $mailbox = find_mailbox_name($read_ary[$i]);
627 $dm_count = substr_count($mailbox, $delimiter);
628 if (substr($mailbox, -1) == $delimiter) {
629 /* If name ends in delimiter - decrement count by one */
633 /* Format folder name, but only if it's a INBOX.* or has a parent. */
634 $boxesallbyname[$mailbox] = $g;
635 $parentfolder = readMailboxParent($mailbox, $delimiter);
636 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
637 (ereg('^'.$folder_prefix, $mailbox)) ||
638 ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
640 $boxes[$g]['formatted'] = str_repeat(' ', $dm_count);
643 $boxes[$g]['formatted'] = '';
645 $boxes[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
648 $boxes[$g]['formatted'] = imap_utf7_decode_local($mailbox);
651 $boxes[$g]['unformatted-dm'] = $mailbox;
652 if (substr($mailbox, -1) == $delimiter) {
653 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
655 $boxes[$g]['unformatted'] = $mailbox;
656 $boxes[$g]['unformatted-disp'] = ereg_replace('^' . $folder_prefix, '', $mailbox);
657 $boxes[$g]['id'] = $g;
659 /* Now lets get the flags for this mailbox */
660 $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
661 true, $response, $message);
663 /* Another workaround for EIMS */
664 if (isset($read_mlbx[1]) &&
665 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", $read_mlbx[0], $regs)) {
666 $read_mlbx[0] = $regs[1] . '"' . addslashes(trim($read_mlbx[1])) . '"' . $regs[2];
669 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+
1);
670 $flags = substr($flags, 0, strpos($flags, ')'));
671 $flags = str_replace('\\', '', $flags);
672 $flags = trim(strtolower($flags));
674 $boxes[$g]['flags'] = explode(' ', $flags);
676 $boxes[$g]['flags'] = array();
681 if(is_array($boxes)) {
682 $boxes = ary_sort ($boxes, 'unformatted', 1);
688 function sqimap_mailbox_tree($imap_stream) {
689 global $boxesnew, $default_folder_prefix, $unseen_notify, $unseen_type;
690 if ( !isset( $boxesnew ) ) {
692 global $data_dir, $username, $list_special_folders_first,
693 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
694 $move_to_trash, $move_to_sent, $save_as_draft,
698 $inbox_in_list = false;
699 $inbox_subscribed = false;
701 require_once('../src/load_prefs.php');
702 require_once('../functions/array.php');
705 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
706 true, $response, $message);
709 * Section about removing the last element was removed
710 * We don't return "* OK" anymore from sqimap_read_data
712 $sorted_lsub_ary = array();
714 for ($i=0;$i < count($lsub_ary); $i++
) {
716 * Workaround for EIMS
717 * Doesn't work if the mailbox name is multiple lines
719 if (isset($lsub_ary[$i +
1]) &&
720 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
721 $lsub_ary[$i], $regs)) {
723 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
726 if (preg_match("/^\*\s+LSUB\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$lsub_ary[$i],$regs)) {
728 $mbx = trim($regs[3]);
729 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
732 array_multisort($sorted_lsub_ary, SORT_ASC
, SORT_STRING
);
734 foreach ($sorted_lsub_ary as $mbx) {
735 if ($mbx['mbx'] == 'INBOX') {
736 $inbox_in_list = true;
742 * Just in case they're not subscribed to their inbox,
743 * we'll get it for them anyway
745 if (!$inbox_in_list) {
746 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
747 true, $response, $message);
748 /* Another workaround for EIMS */
749 if (isset($inbox_ary[1]) &&
750 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
751 $inbox_ary[0], $regs)) {
752 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
755 if (preg_match("/^\*\s+LIST\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$inbox_ary[0],$regs)) {
757 $mbx = trim($regs[3]);
758 if (substr($mbx, -1) == $delimiter) {
759 $mbx = substr($mbx, 0, strlen($mbx) - 1);
761 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
765 for ($i=0 ; $i < count($sorted_lsub_ary); $i++
) {
766 if (($unseen_notify == 2 && $sorted_lsub_ary[$i]['mbx'] == 'INBOX')
767 ||
$unseen_notify == 3
768 ||
($move_to_trash && ($sorted_lsub_ary[$i]['mbx'] == $trash_folder))) {
769 $sorted_lsub_ary[$i]['unseen'] = sqimap_unseen_messages($imap_stream, $mbx['mbx']);
770 if ($unseen_type == 2 ||
($move_to_trash
771 && ($sorted_lsub_ary[$i]['mbx'] == $trash_folder) )) {
772 $sorted_lsub_ary[$i]['nummessages'] = sqimap_get_num_messages($imap_stream, $mbx['mbx']);
774 if ($unseen_notify != 3 && !($move_to_trash
775 && ($sorted_lsub_ary[$i]['mbx'] == $trash_folder) )) break;
779 $boxesnew = sqimap_fill_mailbox_tree($sorted_lsub_ary);
785 function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false) {
786 global $data_dir, $username, $list_special_folders_first,
787 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
788 $move_to_trash, $move_to_sent, $save_as_draft,
791 $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
793 /* create virtual root node */
794 $mailboxes= new mailboxes();
795 $mailboxes->is_root
= true;
797 if (isset($folder_prefix) && $folder_prefix != '') {
798 $start = substr_count($folder_prefix,$delimiter);
799 if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
801 $mailboxes->mailboxname_full
= substr($folder_prefix,0, (strlen($folder_prefix)-1));
803 $mailboxes->mailboxname_full
= $folder_prefix;
806 $mailboxes->mailboxname_sub
= $mailboxes->mailboxname_full
;
809 for ($i=0; $i < count($mbx_ary); $i++
) {
810 if ($mbx_ary[$i]['mbx'] !='' ) {
811 $mbx = new mailboxes();
812 $mailbox = $mbx_ary[$i]['mbx'];
815 $mbx->is_inbox
= true;
816 $mbx->is_special
= true;
819 $mbx->is_trash
= true;
820 $mbx->is_special
= true;
823 $mbx->is_sent
= true;
824 $mbx->is_special
= true;
827 $mbx->is_draft
= true;
828 $mbx->is_special
= true;
832 if (isset($mbx_ary[$i]['unseen'])) {
833 $mbx->unseen
= $mbx_ary[$i]['unseen'];
835 if (isset($mbx_ary[$i]['nummessages'])) {
836 $mbx->total
= $mbx_ary[$i]['nummessages'];
839 $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
841 $mbx->mailboxname_sub
= substr($mbx_ary[$i]['mbx'],$r_del_pos+
1);
842 } else { /* mailbox is root folder */
843 $mbx->mailboxname_sub
= $mbx_ary[$i]['mbx'];
845 $mbx->mailboxname_full
= $mbx_ary[$i]['mbx'];
846 $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);