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
13 require_once('../functions/imap_utf7_encode_local.php');
14 require_once('../functions/imap_utf7_decode_local.php');
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;
23 function addMbx($mbx, $delimiter, $start, $specialfirst) {
24 $ary = explode($delimiter, $mbx->mailboxname_full
);
26 for ($i=$start; $i < (count($ary) -1); $i++
) {
27 $mbx_childs = &$mbx_parent->mbxs
;
30 foreach ($mbx_childs as $key => $parent) {
31 if ($parent->mailboxname_sub
== $ary[$i]) {
32 $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 global $auto_expunge;
169 if ( $mailbox == 'None' ) {
173 $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
174 true, $response, $message);
176 for ($i=0; $i<count($read); $i++
) {
177 if (preg_match('/^\*\s+OK\s\[(\w+)\s(\w+)\]/',$read[$i], $regs)) {
178 $result[strtoupper($regs[1])] = $regs[2];
179 } else if (preg_match('/^\*\s([0-9]+)\s(\w+)/',$read[$i], $regs)) {
180 $result[strtoupper($regs[2])] = $regs[1];
182 if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) {
183 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ;
184 $result['PERMANENTFLAGS'] = $regs[1];
186 else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) {
187 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ;
188 $result['FLAGS'] = $regs[1];
192 if (preg_match('/^\[(.+)\]/',$message, $regs)) {
193 $result['RIGHTS']=$regs[1];
197 $tmp = sqimap_run_command($imap_stream, 'EXPUNGE', false, $a, $b);
202 /* Creates a folder */
203 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
205 if (strtolower($type) == 'noselect') {
206 $mailbox .= $delimiter;
208 $mailbox = imap_utf7_encode_local($mailbox);
209 $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
210 true, $response, $message);
211 sqimap_subscribe ($imap_stream, $mailbox);
214 /* Subscribes to an existing folder */
215 function sqimap_subscribe ($imap_stream, $mailbox) {
216 $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"",
217 true, $response, $message);
220 /* Unsubscribes to an existing folder */
221 function sqimap_unsubscribe ($imap_stream, $mailbox) {
222 global $imap_server_type;
223 $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"",
224 true, $response, $message);
227 /* Deletes the given folder */
228 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
229 global $data_dir, $username;
230 $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
231 true, $response, $message);
232 sqimap_unsubscribe ($imap_stream, $mailbox);
233 do_hook_function("rename_or_delete_folder", $args = array($mailbox, 'delete', ''));
234 removePref($data_dir, $username, "thread_$mailbox");
237 /* Determines if the user is subscribed to the folder or not */
238 function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
239 $boxesall = sqimap_mailbox_list ($imap_stream);
240 foreach ($boxesall as $ref) {
241 if ($ref['unformatted'] == $folder) {
248 /* Renames a mailbox */
249 function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
250 if ( $old_name != $new_name ) {
251 global $delimiter, $imap_server_type, $data_dir, $username;
252 if ( substr( $old_name, -1 ) == $delimiter ) {
253 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
254 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
255 $postfix = $delimiter;
259 $boxesall = sqimap_mailbox_list($imap_stream);
260 $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' . quoteIMAP($new_name) . '"';
261 $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message);
262 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
263 $oldpref = getPref($data_dir, $username, "thread_".$old_name.$postfix);
264 removePref($data_dir, $username, "thread_".$old_name.$postfix);
265 sqimap_subscribe($imap_stream, $new_name.$postfix);
266 setPref($data_dir, $username, "thread_".$new_name.$postfix, $oldpref);
267 do_hook_function("rename_or_delete_folder",$args = array($old_name, 'rename', $new_name));
268 $l = strlen( $old_name ) +
1;
270 foreach ( $boxesall as $box ) {
271 if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) {
272 $new_sub = $new_name . $delimiter . substr($box[$p], $l);
273 if ($imap_server_type == 'cyrus') {
274 $cmd = 'RENAME "' . quoteIMAP($box[$p]) . '" "' . quoteIMAP($new_sub) . '"';
275 $data = sqimap_run_command($imap_stream, $cmd, true,
276 $response, $message);
278 sqimap_unsubscribe($imap_stream, $box[$p]);
279 $oldpref = getPref($data_dir, $username, "thread_".$box[$p]);
280 removePref($data_dir, $username, "thread_".$box[$p]);
281 sqimap_subscribe($imap_stream, $new_sub);
282 setPref($data_dir, $username, "thread_".$new_sub, $oldpref);
283 do_hook_function("rename_or_delete_folder",
284 $args = array($box[$p], 'rename', $new_sub));
291 * Formats a mailbox into 4 parts for the $boxesall array
293 * The four parts are:
295 * raw - Raw LIST/LSUB response from the IMAP server
296 * formatted - nicely formatted folder name
297 * unformatted - unformatted, but with delimiter at end removed
298 * unformatted-dm - folder name as it appears in raw response
299 * unformatted-disp - unformatted without $folder_prefix
301 function sqimap_mailbox_parse ($line, $line_lsub) {
302 global $folder_prefix, $delimiter;
304 /* Process each folder line */
305 for ($g=0; $g < count($line); $g++
) {
307 /* Store the raw IMAP reply */
308 if (isset($line[$g])) {
309 $boxesall[$g]["raw"] = $line[$g];
312 $boxesall[$g]["raw"] = '';
316 /* Count number of delimiters ($delimiter) in folder name */
317 $mailbox = trim($line_lsub[$g]);
318 $dm_count = substr_count($mailbox, $delimiter);
319 if (substr($mailbox, -1) == $delimiter) {
320 /* If name ends in delimiter, decrement count by one */
324 /* Format folder name, but only if it's a INBOX.* or has a parent. */
325 $boxesallbyname[$mailbox] = $g;
326 $parentfolder = readMailboxParent($mailbox, $delimiter);
327 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
328 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
329 ( isset($boxesallbyname[$parentfolder]) &&
330 (strlen($parentfolder) > 0) ) ) {
331 $indent = $dm_count - ( substr_count($folder_prefix, $delimiter));
333 $boxesall[$g]['formatted'] = str_repeat(' ', $indent);
336 $boxesall[$g]['formatted'] = '';
338 $boxesall[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
341 $boxesall[$g]['formatted'] = imap_utf7_decode_local($mailbox);
344 $boxesall[$g]['unformatted-dm'] = $mailbox;
345 if (substr($mailbox, -1) == $delimiter) {
346 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
348 $boxesall[$g]['unformatted'] = $mailbox;
349 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
350 $mailbox = substr($mailbox, strlen($folder_prefix));
352 $boxesall[$g]['unformatted-disp'] = $mailbox;
353 $boxesall[$g]['id'] = $g;
355 $boxesall[$g]['flags'] = array();
356 if (isset($line[$g])) {
357 ereg("\(([^)]*)\)",$line[$g],$regs);
358 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
360 $boxesall[$g]['flags'] = explode(' ', $flags);
369 * Sorting function used to sort mailbox names.
370 * + Original patch from dave_michmerhuizen@yahoo.com
371 * + Allows case insensitivity when sorting folders
372 * + Takes care of the delimiter being sorted to the end, causing
373 * subfolders to be listed in below folders that are prefixed
374 * with their parent folders name.
376 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
377 * Without special sort function: foobar between foo and foo.bar
378 * With special sort function: foobar AFTER foo and foo.bar :)
380 function user_strcasecmp($a, $b) {
383 /* Calculate the length of some strings. */
384 $a_length = strlen($a);
385 $b_length = strlen($b);
386 $min_length = min($a_length, $b_length);
387 $delimiter_length = strlen($delimiter);
389 /* Set the initial result value. */
392 /* Check the strings... */
393 for ($c = 0; $c < $min_length; ++
$c) {
394 $a_del = substr($a, $c, $delimiter_length);
395 $b_del = substr($b, $c, $delimiter_length);
397 if (($a_del == $delimiter) && ($b_del == $delimiter)) {
399 } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
401 } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
404 $result = strcasecmp($a{$c}, $b{$c});
412 /* If one string is a prefix of the other... */
414 if ($a_length < $b_length) {
416 } else if ($a_length > $b_length) {
426 * Returns sorted mailbox lists in several different ways.
427 * See comment on sqimap_mailbox_parse() for info about the returned array.
429 function sqimap_mailbox_list($imap_stream) {
430 global $default_folder_prefix;
432 if ( !isset( $boxesnew ) ) {
434 global $data_dir, $username, $list_special_folders_first,
435 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
436 $move_to_trash, $move_to_sent, $save_as_draft,
437 $delimiter, $noselect_fix_enable;
439 $inbox_in_list = false;
440 $inbox_subscribed = false;
442 require_once('../src/load_prefs.php');
443 require_once('../functions/array.php');
445 if ($noselect_fix_enable) {
446 $lsub_args = "LSUB \"$folder_prefix\" \"*%\"";
449 $lsub_args = "LSUB \"$folder_prefix\" \"*\"";
452 $lsub_ary = sqimap_run_command ($imap_stream, $lsub_args,
453 true, $response, $message);
457 * Section about removing the last element was removed
458 * We don't return "* OK" anymore from sqimap_read_data
461 $sorted_lsub_ary = array();
462 for ($i=0;$i < count($lsub_ary); $i++
) {
464 * Workaround for EIMS
465 * Doesn't work if the mailbox name is multiple lines
467 if (isset($lsub_ary[$i +
1]) &&
468 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
469 $lsub_ary[$i], $regs)) {
471 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
473 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
474 $sorted_lsub_ary[] = $temp_mailbox_name;
475 if (strtoupper($temp_mailbox_name) == 'INBOX') {
476 $inbox_subscribed = true;
480 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
481 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
482 $new_ary[] = $sorted_lsub_ary[$i];
485 $sorted_lsub_ary = $new_ary;
486 if (isset($sorted_lsub_ary)) {
487 usort($sorted_lsub_ary, 'user_strcasecmp');
491 $sorted_list_ary = array();
492 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
493 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
494 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
497 $mbx = $sorted_lsub_ary[$i];
500 $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
501 true, $response, $message);
502 /* Another workaround for EIMS */
503 if (isset($read[1]) &&
504 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
506 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) . '"' . $regs[2];
509 if (isset($sorted_list_ary[$i])) {
510 $sorted_list_ary[$i] = '';
513 if (isset($read[0])) {
514 $sorted_list_ary[$i] = $read[0];
517 $sorted_list_ary[$i] = '';
520 if (isset($sorted_list_ary[$i]) &&
521 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
522 $inbox_in_list = true;
527 * Just in case they're not subscribed to their inbox,
528 * we'll get it for them anyway
530 if (!$inbox_subscribed ||
!$inbox_in_list) {
531 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
532 true, $response, $message);
533 /* Another workaround for EIMS */
534 if (isset($inbox_ary[1]) &&
535 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
536 $inbox_ary[0], $regs)) {
537 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
541 $sorted_list_ary[] = $inbox_ary[0];
542 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
545 $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
547 /* Now, lets sort for special folders */
548 $boxesnew = $used = array();
551 foreach ( $boxesall as $k => $box ) {
552 if ( strtolower($box['unformatted']) == 'inbox') {
559 /* List special folders and their subfolders, if requested. */
560 if ($list_special_folders_first) {
561 foreach ( $boxesall as $k => $box ) {
562 if ( !$used[$k] && isSpecialMailbox( $box['unformatted'] ) ) {
566 $spec_sub = str_replace(' ', '', $box['formatted']);
567 $spec_sub = str_replace('*', '', $spec_sub);
568 $spec_sub = str_replace('?', '\?', $spec_sub);
570 /* In case of problems with preg
571 here is a ereg version
572 if (!$used[$k] && ereg("^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$", $box['unformatted']) ) { */
574 if (!$used[$k] && preg_match("?^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$?", $box['unformatted']) ) {
581 /* Rest of the folders */
582 foreach ( $boxesall as $k => $box ) {
592 * Returns a list of all folders, subscribed or not
594 function sqimap_mailbox_list_all($imap_stream) {
595 global $list_special_folders_first, $folder_prefix, $delimiter;
597 require_once('../functions/array.php');
599 $ssid = sqimap_session_id();
600 $lsid = strlen( $ssid );
601 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
602 $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message);
606 for ($i = 0; $i < count($read_ary); $i++
) {
607 /* Another workaround for EIMS */
608 if (isset($read_ary[$i +
1]) &&
609 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
610 $read_ary[$i], $regs)) {
612 $read_ary[$i] = $regs[1] . '"' . addslashes(trim($read_ary[$i])) . '"' . $regs[2];
614 if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
616 /* Store the raw IMAP reply */
617 $boxes[$g]['raw'] = $read_ary[$i];
619 /* Count number of delimiters ($delimiter) in folder name */
620 $mailbox = find_mailbox_name($read_ary[$i]);
621 $dm_count = substr_count($mailbox, $delimiter);
622 if (substr($mailbox, -1) == $delimiter) {
623 /* If name ends in delimiter - decrement count by one */
627 /* Format folder name, but only if it's a INBOX.* or has a parent. */
628 $boxesallbyname[$mailbox] = $g;
629 $parentfolder = readMailboxParent($mailbox, $delimiter);
630 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
631 (ereg('^'.$folder_prefix, $mailbox)) ||
632 ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
634 $boxes[$g]['formatted'] = str_repeat(' ', $dm_count);
637 $boxes[$g]['formatted'] = '';
639 $boxes[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
642 $boxes[$g]['formatted'] = imap_utf7_decode_local($mailbox);
645 $boxes[$g]['unformatted-dm'] = $mailbox;
646 if (substr($mailbox, -1) == $delimiter) {
647 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
649 $boxes[$g]['unformatted'] = $mailbox;
650 $boxes[$g]['unformatted-disp'] = ereg_replace('^' . $folder_prefix, '', $mailbox);
651 $boxes[$g]['id'] = $g;
653 /* Now lets get the flags for this mailbox */
654 $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
655 true, $response, $message);
657 /* Another workaround for EIMS */
658 if (isset($read_mlbx[1]) &&
659 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", $read_mlbx[0], $regs)) {
660 $read_mlbx[0] = $regs[1] . '"' . addslashes(trim($read_mlbx[1])) . '"' . $regs[2];
663 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+
1);
664 $flags = substr($flags, 0, strpos($flags, ')'));
665 $flags = str_replace('\\', '', $flags);
666 $flags = trim(strtolower($flags));
668 $boxes[$g]['flags'] = explode(' ', $flags);
670 $boxes[$g]['flags'] = array();
675 if(is_array($boxes)) {
676 $boxes = ary_sort ($boxes, 'unformatted', 1);
682 function sqimap_mailbox_tree($imap_stream) {
683 global $boxesnew, $default_folder_prefix, $unseen_notify, $unseen_type;
684 if ( !isset( $boxesnew ) ) {
686 global $data_dir, $username, $list_special_folders_first,
687 $folder_prefix, $delimiter, $trash_folder, $move_to_trash;
690 $inbox_in_list = false;
691 $inbox_subscribed = false;
693 require_once('../src/load_prefs.php');
694 require_once('../functions/array.php');
697 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
698 true, $response, $message);
701 * Section about removing the last element was removed
702 * We don't return "* OK" anymore from sqimap_read_data
704 $sorted_lsub_ary = array();
705 $cnt = count($lsub_ary);
706 for ($i=0;$i < $cnt; $i++
) {
708 * Workaround for EIMS
709 * Doesn't work if the mailbox name is multiple lines
711 if (isset($lsub_ary[$i +
1]) &&
712 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
713 $lsub_ary[$i], $regs)) {
715 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
718 // if (preg_match("/^\*\s+LSUB\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$lsub_ary[$i],$regs)) {
720 // $mbx = trim($regs[3]);
721 // $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
723 $mbx = find_mailbox_name($lsub_ary[$i]);
724 if (substr($mbx, -1) == $delimiter) {
725 $mbx = substr($mbx, 0, strlen($mbx) - 1);
727 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => '');
729 array_multisort($sorted_lsub_ary, SORT_ASC
, SORT_REGULAR
);
731 foreach ($sorted_lsub_ary as $mbx) {
732 if ($mbx['mbx'] == 'INBOX') {
733 $inbox_in_list = true;
739 * Just in case they're not subscribed to their inbox,
740 * we'll get it for them anyway
742 if (!$inbox_in_list) {
743 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
744 true, $response, $message);
745 /* Another workaround for EIMS */
746 if (isset($inbox_ary[1]) &&
747 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
748 $inbox_ary[0], $regs)) {
749 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
752 $mbx = find_mailbox_name($inbox_ary[0]);
753 if (substr($mbx, -1) == $delimiter) {
754 $mbx = substr($mbx, 0, strlen($mbx) - 1);
756 if ( $mbx == 'INBOX') {
757 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => '');
758 sqimap_subscribe($imap_stream, 'INBOX');
761 // if (preg_match("/^\*\s+LIST\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$inbox_ary[0],$regs)) {
763 // $mbx = trim($regs[3]);
764 // if (substr($mbx, -1) == $delimiter) {
765 // $mbx = substr($mbx, 0, strlen($mbx) - 1);
767 // $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
770 $cnt = count($sorted_lsub_ary);
771 for ($i=0 ; $i < $cnt; $i++
) {
772 $mbx = $sorted_lsub_ary[$i]['mbx'];
773 if (($unseen_notify == 2 && $mbx == 'INBOX')
774 ||
$unseen_notify == 3
775 ||
($move_to_trash && ($mbx == $trash_folder))) {
776 $sorted_lsub_ary[$i]['unseen'] = sqimap_unseen_messages($imap_stream, $mbx);
777 if ($unseen_type == 2 ||
($move_to_trash
778 && ($mbx == $trash_folder) )) {
779 $sorted_lsub_ary[$i]['nummessages'] = sqimap_get_num_messages($imap_stream, $mbx);
781 if ($mbx == $trash_folder) {
782 $sorted_lsub_ary[$i]['nummessages'] = sqimap_get_num_messages($imap_stream, $mbx);
786 $boxesnew = sqimap_fill_mailbox_tree($sorted_lsub_ary);
792 function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false) {
793 global $data_dir, $username, $list_special_folders_first,
794 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
795 $move_to_trash, $move_to_sent, $save_as_draft,
798 $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
800 /* create virtual root node */
801 $mailboxes= new mailboxes();
802 $mailboxes->is_root
= true;
804 if (isset($folder_prefix) && $folder_prefix != '') {
805 $start = substr_count($folder_prefix,$delimiter);
806 if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
808 $mailboxes->mailboxname_full
= substr($folder_prefix,0, (strlen($folder_prefix)-1));
810 $mailboxes->mailboxname_full
= $folder_prefix;
813 $mailboxes->mailboxname_sub
= $mailboxes->mailboxname_full
;
815 $cnt = count($mbx_ary);
816 for ($i=0; $i < $cnt; $i++
) {
817 if ($mbx_ary[$i]['mbx'] !='' ) {
818 $mbx = new mailboxes();
819 $mailbox = $mbx_ary[$i]['mbx'];
822 $mbx->is_inbox
= true;
823 $mbx->is_special
= true;
826 $mbx->is_trash
= true;
827 $mbx->is_special
= true;
830 $mbx->is_sent
= true;
831 $mbx->is_special
= true;
834 $mbx->is_draft
= true;
835 $mbx->is_special
= true;
839 if (isset($mbx_ary[$i]['unseen'])) {
840 $mbx->unseen
= $mbx_ary[$i]['unseen'];
842 if (isset($mbx_ary[$i]['nummessages'])) {
843 $mbx->total
= $mbx_ary[$i]['nummessages'];
846 $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
848 $mbx->mailboxname_sub
= substr($mbx_ary[$i]['mbx'],$r_del_pos+
1);
849 } else { /* mailbox is root folder */
850 $mbx->mailboxname_sub
= $mbx_ary[$i]['mbx'];
852 $mbx->mailboxname_full
= $mbx_ary[$i]['mbx'];
853 $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);