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
17 var $mailboxname_full = '', $mailboxname_sub= '', $is_noselect = false,
18 $is_special = false, $is_root = false, $is_inbox = false, $is_sent = false,
19 $is_trash = false, $is_draft = false, $mbxs = array(),
20 $unseen = false, $total = false;
22 function addMbx($mbx, $delimiter, $start, $specialfirst) {
23 $ary = explode($delimiter, $mbx->mailboxname_full
);
25 for ($i=$start; $i < (count($ary) -1); $i++
) {
26 $mbx_childs = &$mbx_parent->mbxs
;
28 foreach ($mbx_childs as $key => $parent) {
29 if ($parent->mailboxname_sub
== $ary[$i]) {
30 $mbx_parent = &$mbx_parent->mbxs
[$key];
35 $no_select_mbx = new mailboxes();
36 if (isset($mbx_parent->mailboxname_full
) && $mbx_parent->mailboxname_full
!= '') {
37 $no_select_mbx->mailboxname_full
= $mbx_parent->mailboxname_full
.$delimiter.$ary[$i];
39 $no_select_mbx->mailboxname_full
= $ary[$i];
41 $no_select_mbx->mailboxname_sub
= $ary[$i];
42 $no_select_mbx->is_noselect
= true;
43 $mbx_parent->mbxs
[] = $no_select_mbx;
48 $mbx_parent->mbxs
[] = $mbx;
49 if ($mbx->is_special
&& $specialfirst) {
50 usort($mbx_parent->mbxs
, 'sortSpecialMbx');
56 function sortSpecialMbx($a, $b) {
58 $acmp = '0'. $a->mailboxname_full
;
59 } else if ($a->is_special
) {
60 $acmp = '1'. $a->mailboxname_full
;
62 $acmp = '2' . $a->mailboxname_full
;
65 $bcmp = '0'. $b->mailboxname_full
;
66 }else if ($b->is_special
) {
67 $bcmp = '1' . $b->mailboxname_full
;
69 $bcmp = '2' . $b->mailboxname_full
;
71 if ($acmp == $bcmp) return 0;
72 return ($acmp>$bcmp) ?
1: -1;
76 function find_mailbox_name ($mailbox) {
77 if (ereg(" *\"([^\r\n\"]*)\"[ \r\n]*$", $mailbox, $regs))
79 ereg(" *([^ \r\n\"]*)[ \r\n]*$",$mailbox,$regs);
85 * If $haystack is a full mailbox name, and $needle is the mailbox
86 * separator character, returns the second last part of the full
87 * mailbox name (i.e. the mailbox's parent mailbox)
89 function readMailboxParent($haystack, $needle) {
94 $parts = explode($needle, $haystack);
95 $elem = array_pop($parts);
96 while ($elem == '' && count($parts)) {
97 $elem = array_pop($parts);
99 $ret = join($needle, $parts);
105 function isBoxBelow( $box2, $box1 ) {
106 global $delimiter, $folder_prefix, $imap_server_type;
108 if ( $imap_server_type == 'uw' ) {
110 $i = strpos( $box1, $delimiter, strlen( $folder_prefix ) );
111 if ( $i === false ) {
112 $i = strlen( $box2 );
115 $boxs = $box2 . $delimiter;
116 /* Skip next second delimiter */
117 $i = strpos( $box1, $delimiter );
118 $i = strpos( $box1, $delimiter, $i +
1 );
119 if ( $i === false ) {
120 $i = strlen( $box2 );
126 return ( substr( $box1, 0, $i ) == substr( $boxs, 0, $i ) );
129 /* Defines special mailboxes */
130 function isSpecialMailbox( $box ) {
131 global $trash_folder, $sent_folder, $draft_folder,
132 $move_to_trash, $move_to_sent, $save_as_draft;
134 $ret = ( (strtolower($box) == 'inbox') ||
135 ( $move_to_trash && isBoxBelow( $box, $trash_folder ) ) ||
136 ( $move_to_sent && isBoxBelow( $box, $sent_folder )) ||
137 ($save_as_draft && $box == $draft_folder ) );
140 $ret = do_hook_function( 'special_mailbox', $box );
146 /* Expunges a mailbox */
147 function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true) {
148 $read = sqimap_run_command($imap_stream, 'EXPUNGE', $handle_errors,
149 $response, $message);
152 /* Checks whether or not the specified mailbox exists */
153 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
154 if (! isset($mailbox)) {
157 $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
158 true, $response, $message);
159 return isset($mbx[0]);
162 /* Selects a mailbox */
163 function sqimap_mailbox_select ($imap_stream, $mailbox,
164 $hide = true, $recent = false, $extrainfo = false) {
165 global $auto_expunge;
167 if ( $mailbox == 'None' ) {
171 $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
172 true, $response, $message);
174 for ($i=0; $i<count($read); $i++
) {
175 if (strpos(strtolower($read[$i]), 'recent')) {
176 $r = explode(' ', $read[$i]);
182 $tmp = sqimap_run_command($imap_stream, 'EXPUNGE', false, $a, $b);
184 if (isset( $extrainfo ) && $extrainfo) {
186 for ($i=0; $i<count($read); $i++
) {
187 if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) {
188 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ;
189 $result['PERMANENTFLAGS'] = $regs[1];
191 else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) {
192 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ;
193 $result['FLAGS'] = $regs[1];
195 else if (preg_match("/(.*)EXISTS/i",$read[$i], $regs)) {
196 $result['EXISTS']=trim($regs[1]);
198 else if (preg_match("/(.*)RECENT/i",$read[$i], $regs)) {
199 $result['RECENT']=trim($regs[1]);
201 else if (preg_match("/\[UNSEEN(.*)\]/i",$read[$i], $regs)) {
202 $result['UNSEEN']=trim($regs[1]);
211 /* Creates a folder */
212 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
214 if (strtolower($type) == 'noselect') {
215 $mailbox .= $delimiter;
217 $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
218 true, $response, $message);
219 sqimap_subscribe ($imap_stream, $mailbox);
222 /* Subscribes to an existing folder */
223 function sqimap_subscribe ($imap_stream, $mailbox) {
224 $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"",
225 true, $response, $message);
228 /* Unsubscribes to an existing folder */
229 function sqimap_unsubscribe ($imap_stream, $mailbox) {
230 global $imap_server_type;
231 $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"",
232 true, $response, $message);
235 /* Deletes the given folder */
236 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
237 global $data_dir, $username;
238 $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
239 true, $response, $message);
240 sqimap_unsubscribe ($imap_stream, $mailbox);
241 do_hook_function("rename_or_delete_folder", $args = array($mailbox, 'delete', ''));
242 removePref($data_dir, $username, "thread_$mailbox");
245 /* Determines if the user is subscribed to the folder or not */
246 function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
247 $boxesall = sqimap_mailbox_list ($imap_stream);
248 foreach ($boxesall as $ref) {
249 if ($ref['unformatted'] == $folder) {
256 /* Renames a mailbox */
257 function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
258 if ( $old_name != $new_name ) {
259 global $delimiter, $imap_server_type, $data_dir, $username;
260 if ( substr( $old_name, -1 ) == $delimiter ) {
261 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
262 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
263 $postfix = $delimiter;
267 $boxesall = sqimap_mailbox_list($imap_stream);
268 $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' . quoteIMAP($new_name) . '"';
269 $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message);
270 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
271 $oldpref = getPref($data_dir, $username, "thread_".$old_name.$postfix);
272 removePref($data_dir, $username, "thread_".$old_name.$postfix);
273 sqimap_subscribe($imap_stream, $new_name.$postfix);
274 setPref($data_dir, $username, "thread_".$new_name.$postfix, $oldpref);
275 do_hook_function("rename_or_delete_folder",$args = array($old_name, 'rename', $new_name));
276 $l = strlen( $old_name ) +
1;
278 foreach ( $boxesall as $box ) {
279 if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) {
280 $new_sub = $new_name . $delimiter . substr($box[$p], $l);
281 if ($imap_server_type == 'cyrus') {
282 $cmd = 'RENAME "' . quoteIMAP($box[$p]) . '" "' . quoteIMAP($new_sub) . '"';
283 $data = sqimap_run_command($imap_stream, $cmd, true,
284 $response, $message);
286 sqimap_unsubscribe($imap_stream, $box[$p]);
287 $oldpref = getPref($data_dir, $username, "thread_".$box[$p]);
288 removePref($data_dir, $username, "thread_".$box[$p]);
289 sqimap_subscribe($imap_stream, $new_sub);
290 setPref($data_dir, $username, "thread_".$new_sub, $oldpref);
291 do_hook_function("rename_or_delete_folder",
292 $args = array($box[$p], 'rename', $new_sub));
299 * Formats a mailbox into 4 parts for the $boxesall array
301 * The four parts are:
303 * raw - Raw LIST/LSUB response from the IMAP server
304 * formatted - nicely formatted folder name
305 * unformatted - unformatted, but with delimiter at end removed
306 * unformatted-dm - folder name as it appears in raw response
307 * unformatted-disp - unformatted without $folder_prefix
309 function sqimap_mailbox_parse ($line, $line_lsub) {
310 global $folder_prefix, $delimiter;
312 /* Process each folder line */
313 for ($g=0; $g < count($line); $g++
) {
315 /* Store the raw IMAP reply */
316 if (isset($line[$g])) {
317 $boxesall[$g]["raw"] = $line[$g];
320 $boxesall[$g]["raw"] = '';
324 /* Count number of delimiters ($delimiter) in folder name */
325 $mailbox = trim($line_lsub[$g]);
326 $dm_count = substr_count($mailbox, $delimiter);
327 if (substr($mailbox, -1) == $delimiter) {
328 /* If name ends in delimiter, decrement count by one */
332 /* Format folder name, but only if it's a INBOX.* or has a parent. */
333 $boxesallbyname[$mailbox] = $g;
334 $parentfolder = readMailboxParent($mailbox, $delimiter);
335 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
336 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
337 ( isset($boxesallbyname[$parentfolder]) &&
338 (strlen($parentfolder) > 0) ) ) {
339 $indent = $dm_count - ( substr_count($folder_prefix, $delimiter));
341 $boxesall[$g]['formatted'] = str_repeat(' ', $indent);
344 $boxesall[$g]['formatted'] = '';
346 $boxesall[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
349 $boxesall[$g]['formatted'] = $mailbox;
352 $boxesall[$g]['unformatted-dm'] = $mailbox;
353 if (substr($mailbox, -1) == $delimiter) {
354 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
356 $boxesall[$g]['unformatted'] = $mailbox;
357 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
358 $mailbox = substr($mailbox, strlen($folder_prefix));
360 $boxesall[$g]['unformatted-disp'] = $mailbox;
361 $boxesall[$g]['id'] = $g;
363 $boxesall[$g]['flags'] = array();
364 if (isset($line[$g])) {
365 ereg("\(([^)]*)\)",$line[$g],$regs);
366 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
368 $boxesall[$g]['flags'] = explode(' ', $flags);
377 * Sorting function used to sort mailbox names.
378 * + Original patch from dave_michmerhuizen@yahoo.com
379 * + Allows case insensitivity when sorting folders
380 * + Takes care of the delimiter being sorted to the end, causing
381 * subfolders to be listed in below folders that are prefixed
382 * with their parent folders name.
384 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
385 * Without special sort function: foobar between foo and foo.bar
386 * With special sort function: foobar AFTER foo and foo.bar :)
388 function user_strcasecmp($a, $b) {
391 /* Calculate the length of some strings. */
392 $a_length = strlen($a);
393 $b_length = strlen($b);
394 $min_length = min($a_length, $b_length);
395 $delimiter_length = strlen($delimiter);
397 /* Set the initial result value. */
400 /* Check the strings... */
401 for ($c = 0; $c < $min_length; ++
$c) {
402 $a_del = substr($a, $c, $delimiter_length);
403 $b_del = substr($b, $c, $delimiter_length);
405 if (($a_del == $delimiter) && ($b_del == $delimiter)) {
407 } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
409 } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
412 $result = strcasecmp($a{$c}, $b{$c});
420 /* If one string is a prefix of the other... */
422 if ($a_length < $b_length) {
424 } else if ($a_length > $b_length) {
434 * Returns sorted mailbox lists in several different ways.
435 * See comment on sqimap_mailbox_parse() for info about the returned array.
437 function sqimap_mailbox_list($imap_stream) {
438 global $boxesnew, $default_folder_prefix;
440 if ( !isset( $boxesnew ) ) {
442 global $data_dir, $username, $list_special_folders_first,
443 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
444 $move_to_trash, $move_to_sent, $save_as_draft,
447 $inbox_in_list = false;
448 $inbox_subscribed = false;
450 require_once('../src/load_prefs.php');
451 require_once('../functions/array.php');
454 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*%\"",
455 true, $response, $message);
458 * Section about removing the last element was removed
459 * We don't return "* OK" anymore from sqimap_read_data
462 $sorted_lsub_ary = array();
463 for ($i=0;$i < count($lsub_ary); $i++
) {
465 * Workaround for EIMS
466 * Doesn't work if the mailbox name is multiple lines
468 if (isset($lsub_ary[$i +
1]) &&
469 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
470 $lsub_ary[$i], $regs)) {
472 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
474 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
475 $sorted_lsub_ary[] = $temp_mailbox_name;
476 if (strtoupper($temp_mailbox_name) == 'INBOX') {
477 $inbox_subscribed = true;
481 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
482 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
483 $new_ary[] = $sorted_lsub_ary[$i];
486 $sorted_lsub_ary = $new_ary;
487 if (isset($sorted_lsub_ary)) {
488 usort($sorted_lsub_ary, 'user_strcasecmp');
492 $sorted_list_ary = array();
493 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
494 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
495 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
498 $mbx = $sorted_lsub_ary[$i];
501 $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
502 true, $response, $message);
503 /* Another workaround for EIMS */
504 if (isset($read[1]) &&
505 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
507 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) . '"' . $regs[2];
510 if (isset($sorted_list_ary[$i])) {
511 $sorted_list_ary[$i] = '';
514 if (isset($read[0])) {
515 $sorted_list_ary[$i] = $read[0];
518 $sorted_list_ary[$i] = '';
521 if (isset($sorted_list_ary[$i]) &&
522 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
523 $inbox_in_list = true;
528 * Just in case they're not subscribed to their inbox,
529 * we'll get it for them anyway
531 if (!$inbox_subscribed ||
!$inbox_in_list) {
532 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
533 true, $response, $message);
534 /* Another workaround for EIMS */
535 if (isset($inbox_ary[1]) &&
536 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
537 $inbox_ary[0], $regs)) {
538 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
542 $sorted_list_ary[] = $inbox_ary[0];
543 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
546 $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
548 /* Now, lets sort for special folders */
549 $boxesnew = $used = array();
552 foreach ( $boxesall as $k => $box ) {
553 if ( strtolower($box['unformatted']) == 'inbox') {
560 /* List special folders and their subfolders, if requested. */
561 if ($list_special_folders_first) {
562 foreach ( $boxesall as $k => $box ) {
563 if ( !$used[$k] && isSpecialMailbox( $box['unformatted'] ) ) {
567 $spec_sub = str_replace(' ', '', $box['formatted']);
569 /* In case of problems with preg
570 here is a ereg version
571 if (!$used[$k] && ereg("^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$", $box['unformatted']) ) { */
573 if (!$used[$k] && preg_match("?^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$?", $box['unformatted']) ) {
580 /* Rest of the folders */
581 foreach ( $boxesall as $k => $box ) {
591 * Returns a list of all folders, subscribed or not
593 function sqimap_mailbox_list_all($imap_stream) {
594 global $list_special_folders_first, $folder_prefix, $delimiter;
596 require_once('../functions/array.php');
598 $ssid = sqimap_session_id();
599 $lsid = strlen( $ssid );
600 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
601 $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message);
605 for ($i = 0; $i < count($read_ary); $i++
) {
606 /* Another workaround for EIMS */
607 if (isset($read_ary[$i +
1]) &&
608 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
609 $read_ary[$i], $regs)) {
611 $read_ary[$i] = $regs[1] . '"' . addslashes(trim($read_ary[$i])) . '"' . $regs[2];
613 if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
615 /* Store the raw IMAP reply */
616 $boxes[$g]['raw'] = $read_ary[$i];
618 /* Count number of delimiters ($delimiter) in folder name */
619 $mailbox = find_mailbox_name($read_ary[$i]);
620 $dm_count = substr_count($mailbox, $delimiter);
621 if (substr($mailbox, -1) == $delimiter) {
622 /* If name ends in delimiter - decrement count by one */
626 /* Format folder name, but only if it's a INBOX.* or has a parent. */
627 $boxesallbyname[$mailbox] = $g;
628 $parentfolder = readMailboxParent($mailbox, $delimiter);
629 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
630 (ereg('^'.$folder_prefix, $mailbox)) ||
631 ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
633 $boxes[$g]['formatted'] = str_repeat(' ', $dm_count);
636 $boxes[$g]['formatted'] = '';
638 $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
641 $boxes[$g]['formatted'] = $mailbox;
644 $boxes[$g]['unformatted-dm'] = $mailbox;
645 if (substr($mailbox, -1) == $delimiter) {
646 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
648 $boxes[$g]['unformatted'] = $mailbox;
649 $boxes[$g]['unformatted-disp'] = ereg_replace('^' . $folder_prefix, '', $mailbox);
650 $boxes[$g]['id'] = $g;
652 /* Now lets get the flags for this mailbox */
653 $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
654 true, $response, $message);
656 /* Another workaround for EIMS */
657 if (isset($read_mlbx[1]) &&
658 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", $read_mlbx[0], $regs)) {
659 $read_mlbx[0] = $regs[1] . '"' . addslashes(trim($read_mlbx[1])) . '"' . $regs[2];
662 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+
1);
663 $flags = substr($flags, 0, strpos($flags, ')'));
664 $flags = str_replace('\\', '', $flags);
665 $flags = trim(strtolower($flags));
667 $boxes[$g]['flags'] = explode(' ', $flags);
669 $boxes[$g]['flags'] = array();
674 if(is_array($boxes)) {
675 $boxes = ary_sort ($boxes, 'unformatted', 1);
681 function sqimap_mailbox_tree($imap_stream) {
682 global $boxesnew, $default_folder_prefix, $unseen_notify, $unseen_type;
683 if ( !isset( $boxesnew ) ) {
685 global $data_dir, $username, $list_special_folders_first,
686 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
687 $move_to_trash, $move_to_sent, $save_as_draft,
691 $inbox_in_list = false;
692 $inbox_subscribed = false;
694 require_once('../src/load_prefs.php');
695 require_once('../functions/array.php');
698 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"\" \"*\"",
699 true, $response, $message);
702 * Section about removing the last element was removed
703 * We don't return "* OK" anymore from sqimap_read_data
705 $sorted_lsub_ary = array();
707 for ($i=0;$i < count($lsub_ary); $i++
) {
709 * Workaround for EIMS
710 * Doesn't work if the mailbox name is multiple lines
712 if (isset($lsub_ary[$i +
1]) &&
713 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
714 $lsub_ary[$i], $regs)) {
716 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
719 if (preg_match("/^\*\s+LSUB\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$lsub_ary[$i],$regs)) {
721 $mbx = trim($regs[3]);
722 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
725 array_multisort($sorted_lsub_ary, SORT_ASC
, SORT_STRING
);
727 foreach ($sorted_lsub_ary as $mbx) {
728 if ($mbx['mbx'] == 'INBOX') {
729 $inbox_in_list = true;
735 * Just in case they're not subscribed to their inbox,
736 * we'll get it for them anyway
738 if (!$inbox_in_list) {
739 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
740 true, $response, $message);
741 /* Another workaround for EIMS */
742 if (isset($inbox_ary[1]) &&
743 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
744 $inbox_ary[0], $regs)) {
745 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
748 if (preg_match("/^\*\s+LIST\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$inbox_ary[0],$regs)) {
750 $mbx = trim($regs[3]);
751 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
755 for ($i=0 ; $i < count($sorted_lsub_ary); $i++
) {
756 if (($unseen_notify == 2 && $sorted_lsub_ary[$i]['mbx'] == 'INBOX')
757 ||
$unseen_notify == 3
758 ||
($move_to_trash && ($sorted_lsub_ary[$i]['mbx'] == $trash_folder))) {
759 $sorted_lsub_ary[$i]['unseen'] = sqimap_unseen_messages($imap_stream, $mbx['mbx']);
760 if ($unseen_type == 2 ||
($move_to_trash
761 && ($sorted_lsub_ary[$i]['mbx'] == $trash_folder) )) {
762 $sorted_lsub_ary[$i]['nummessages'] = sqimap_get_num_messages($imap_stream, $mbx['mbx']);
764 if ($unseen_notify != 3 && !($move_to_trash
765 && ($sorted_lsub_ary[$i]['mbx'] == $trash_folder) )) break;
769 $boxesnew = sqimap_fill_mailbox_tree($sorted_lsub_ary);
775 function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false) {
776 global $data_dir, $username, $list_special_folders_first,
777 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
778 $move_to_trash, $move_to_sent, $save_as_draft,
781 $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
783 /* create virtual root node */
784 $mailboxes= new mailboxes();
785 $mailboxes->is_root
= true;
787 if (isset($folder_prefix) && $folder_prefix != '') {
788 $start = substr_count($folder_prefix,$delimiter);
789 if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
791 $mailboxes->mailboxname_full
= substr($folder_prefix,0, (strlen($folder_prefix)-1));
793 $mailboxes->mailboxname_full
= $folder_prefix;
796 $mailboxes->mailboxname_sub
= $mailboxes->mailboxname_full
;
799 for ($i=0; $i < count($mbx_ary); $i++
) {
800 if ($mbx_ary[$i]['mbx'] !='' ) {
801 $mbx = new mailboxes();
802 $mailbox = $mbx_ary[$i]['mbx'];
805 $mbx->is_inbox
= true;
806 $mbx->is_special
= true;
809 $mbx->is_trash
= true;
810 $mbx->is_special
= true;
813 $mbx->is_sent
= true;
814 $mbx->is_special
= true;
817 $mbx->is_draft
= true;
818 $mbx->is_special
= true;
822 if (isset($mbx_ary[$i]['unseen'])) {
823 $mbx->unseen
= $mbx_ary[$i]['unseen'];
825 if (isset($mbx_ary[$i]['nummessages'])) {
826 $mbx->total
= $mbx_ary[$i]['nummessages'];
829 $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
831 $mbx->mailboxname_sub
= substr($mbx_ary[$i]['mbx'],$r_del_pos+
1);
832 } else { /* mailbox is root folder */
833 $mbx->mailboxname_sub
= $mbx_ary[$i]['mbx'];
835 $mbx->mailboxname_full
= $mbx_ary[$i]['mbx'];
836 $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);