6 * Copyright (c) 1999-2003 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(SM_PATH
. 'functions/imap_utf7_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, $c = count($ary)-1; $i < $c; $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;
50 $mbx_parent->mbxs
[] = $mbx;
51 if ($mbx->is_special
&& $specialfirst) {
52 usort($mbx_parent->mbxs
, 'sortSpecialMbx');
57 function sortSpecialMbx($a, $b) {
59 $acmp = '0'. $a->mailboxname_full
;
60 } else if ($a->is_special
) {
61 $acmp = '1'. $a->mailboxname_full
;
63 $acmp = '2' . $a->mailboxname_full
;
66 $bcmp = '0'. $b->mailboxname_full
;
67 }else if ($b->is_special
) {
68 $bcmp = '1' . $b->mailboxname_full
;
70 $bcmp = '2' . $b->mailboxname_full
;
72 if ($acmp == $bcmp) return 0;
73 return ($acmp > $bcmp) ?
1: -1;
76 function find_mailbox_name ($mailbox) {
77 if (preg_match('/\*.+\"([^\r\n\"]*)\"[\s\r\n]*$/', $mailbox, $regs))
79 if (ereg(" *\"([^\r\n\"]*)\"[ \r\n]*$", $mailbox, $regs))
81 ereg(" *([^ \r\n\"]*)[ \r\n]*$",$mailbox,$regs);
85 function check_is_noselect ($lsub_line) {
86 return preg_match("/^\* LSUB \([^\)]*\\Noselect[^\)]*\)/i", $lsub_line);
90 * If $haystack is a full mailbox name, and $needle is the mailbox
91 * separator character, returns the second last part of the full
92 * mailbox name (i.e. the mailbox's parent mailbox)
94 function readMailboxParent($haystack, $needle) {
98 $parts = explode($needle, $haystack);
99 $elem = array_pop($parts);
100 while ($elem == '' && count($parts)) {
101 $elem = array_pop($parts);
103 $ret = join($needle, $parts);
109 * Check if $subbox is below the specified $parentbox
111 function isBoxBelow( $subbox, $parentbox ) {
112 global $delimiter, $folder_prefix, $imap_server_type;
115 * Eliminate the obvious mismatch, where the
116 * subfolder path is shorter than that of the potential parent
118 if ( strlen($subbox) < strlen($parentbox) ) {
122 if ( $imap_server_type == 'uw' ) {
124 $i = strpos( $subbox, $delimiter, strlen( $folder_prefix ) );
125 if ( $i === false ) {
126 $i = strlen( $parentbox );
129 if (substr($parentbox,0,strlen($subbox)) == $subbox) {
132 $boxs = $parentbox . $delimiter;
133 /* Skip next second delimiter */
134 $i = strpos( $subbox, $delimiter );
135 $i = strpos( $subbox, $delimiter, $i +
1 );
136 if ( $i === false ) {
137 $i = strlen( $parentbox );
142 return ( substr( $subbox, 0, $i ) == substr( $boxs, 0, $i ) );
145 /* Defines special mailboxes */
146 function isSpecialMailbox( $box ) {
147 global $trash_folder, $sent_folder, $draft_folder,
148 $move_to_trash, $move_to_sent, $save_as_draft;
150 $ret = ( (strtolower($box) == 'inbox') ||
151 isTrashMailbox($box) ||
isSentMailbox($box) ||
isDraftMailbox($box) );
154 $ret = do_hook_function( 'special_mailbox', $box );
159 function isTrashMailbox ($box) {
160 global $trash_folder, $move_to_trash;
161 return $move_to_trash && $trash_folder &&
162 ( $box == $trash_folder ||
isBoxBelow($box, $trash_folder) );
165 function isSentMailbox($box) {
166 global $sent_folder, $move_to_sent;
167 return $move_to_sent && $sent_folder &&
168 ( $box == $sent_folder ||
isBoxBelow($box, $sent_folder) );
171 function isDraftMailbox($box) {
172 global $draft_folder, $save_as_draft;
173 return $save_as_draft &&
174 ( $box == $draft_folder ||
isBoxBelow($box, $draft_folder) );
177 /* Expunges a mailbox */
178 function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true, $id='') {
182 $id = sqimap_message_list_squisher($id);
189 $read = sqimap_run_command($imap_stream, 'EXPUNGE'.$id, $handle_errors,
190 $response, $message, $uid);
193 if (is_array($read)) {
194 foreach ($read as $r) {
195 if (preg_match('/^\*\s[0-9]+\sEXPUNGE/AUi',$r,$regs)) {
203 /* Checks whether or not the specified mailbox exists */
204 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
205 if (!isset($mailbox)) {
208 $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
209 true, $response, $message);
210 return isset($mbx[0]);
213 /* Selects a mailbox */
214 function sqimap_mailbox_select ($imap_stream, $mailbox) {
215 global $auto_expunge;
217 if ($mailbox == 'None') {
221 $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
222 true, $response, $message);
224 for ($i = 0, $cnt = count($read); $i < $cnt; $i++
) {
225 if (preg_match('/^\*\s+OK\s\[(\w+)\s(\w+)\]/',$read[$i], $regs)) {
226 $result[strtoupper($regs[1])] = $regs[2];
227 } else if (preg_match('/^\*\s([0-9]+)\s(\w+)/',$read[$i], $regs)) {
228 $result[strtoupper($regs[2])] = $regs[1];
230 if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) {
231 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ;
232 $result['PERMANENTFLAGS'] = $regs[1];
233 } else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) {
234 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ;
235 $result['FLAGS'] = $regs[1];
239 if (preg_match('/^\[(.+)\]/',$message, $regs)) {
240 $result['RIGHTS']=$regs[1];
244 $tmp = sqimap_run_command($imap_stream, 'EXPUNGE', false, $a, $b);
249 /* Creates a folder */
250 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
252 if (strtolower($type) == 'noselect') {
253 $mailbox .= $delimiter;
256 $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
257 true, $response, $message);
258 sqimap_subscribe ($imap_stream, $mailbox);
261 /* Subscribes to an existing folder */
262 function sqimap_subscribe ($imap_stream, $mailbox) {
263 $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"",
264 true, $response, $message);
267 /* Unsubscribes to an existing folder */
268 function sqimap_unsubscribe ($imap_stream, $mailbox) {
269 $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"",
270 true, $response, $message);
273 /* Deletes the given folder */
274 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
275 global $data_dir, $username;
276 $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
277 true, $response, $message);
278 sqimap_unsubscribe ($imap_stream, $mailbox);
279 do_hook_function('rename_or_delete_folder', $args = array($mailbox, 'delete', ''));
280 removePref($data_dir, $username, "thread_$mailbox");
283 /* Determines if the user is subscribed to the folder or not */
284 function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
285 $boxesall = sqimap_mailbox_list ($imap_stream);
286 foreach ($boxesall as $ref) {
287 if ($ref['unformatted'] == $folder) {
294 /* Renames a mailbox */
295 function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
296 if ( $old_name != $new_name ) {
297 global $delimiter, $imap_server_type, $data_dir, $username;
298 if ( substr( $old_name, -1 ) == $delimiter ) {
299 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
300 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
301 $postfix = $delimiter;
306 $boxesall = sqimap_mailbox_list($imap_stream);
307 $cmd = 'RENAME "' . $old_name . '" "' . $new_name . '"';
308 $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message);
309 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
310 $oldpref = getPref($data_dir, $username, 'thread_'.$old_name.$postfix);
311 removePref($data_dir, $username, 'thread_'.$old_name.$postfix);
312 sqimap_subscribe($imap_stream, $new_name.$postfix);
313 setPref($data_dir, $username, 'thread_'.$new_name.$postfix, $oldpref);
314 do_hook_function('rename_or_delete_folder',$args = array($old_name, 'rename', $new_name));
315 $l = strlen( $old_name ) +
1;
318 foreach ($boxesall as $box) {
319 if (substr($box[$p], 0, $l) == $old_name . $delimiter) {
320 $new_sub = $new_name . $delimiter . substr($box[$p], $l);
321 if ($imap_server_type == 'cyrus') {
322 $cmd = 'RENAME "' . $box[$p] . '" "' . $new_sub . '"';
323 $data = sqimap_run_command($imap_stream, $cmd, true,
324 $response, $message);
326 sqimap_unsubscribe($imap_stream, $box[$p]);
327 $oldpref = getPref($data_dir, $username, 'thread_'.$box[$p]);
328 removePref($data_dir, $username, 'thread_'.$box[$p]);
329 sqimap_subscribe($imap_stream, $new_sub);
330 setPref($data_dir, $username, 'thread_'.$new_sub, $oldpref);
331 do_hook_function('rename_or_delete_folder',
332 $args = array($box[$p], 'rename', $new_sub));
339 * Formats a mailbox into 4 parts for the $boxesall array
341 * The four parts are:
343 * raw - Raw LIST/LSUB response from the IMAP server
344 * formatted - nicely formatted folder name
345 * unformatted - unformatted, but with delimiter at end removed
346 * unformatted-dm - folder name as it appears in raw response
347 * unformatted-disp - unformatted without $folder_prefix
349 function sqimap_mailbox_parse ($line, $line_lsub) {
350 global $folder_prefix, $delimiter;
352 /* Process each folder line */
353 for ($g = 0, $cnt = count($line); $g < $cnt; $g++
) {
354 /* Store the raw IMAP reply */
355 if (isset($line[$g])) {
356 $boxesall[$g]['raw'] = $line[$g];
358 $boxesall[$g]['raw'] = '';
361 /* Count number of delimiters ($delimiter) in folder name */
362 $mailbox = trim($line_lsub[$g]);
363 $dm_count = substr_count($mailbox, $delimiter);
364 if (substr($mailbox, -1) == $delimiter) {
365 /* If name ends in delimiter, decrement count by one */
369 /* Format folder name, but only if it's a INBOX.* or has a parent. */
370 $boxesallbyname[$mailbox] = $g;
371 $parentfolder = readMailboxParent($mailbox, $delimiter);
372 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
373 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
374 (isset($boxesallbyname[$parentfolder]) &&
375 (strlen($parentfolder) > 0) ) ) {
376 $indent = $dm_count - (substr_count($folder_prefix, $delimiter));
378 $boxesall[$g]['formatted'] = str_repeat(' ', $indent);
380 $boxesall[$g]['formatted'] = '';
382 $boxesall[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
384 $boxesall[$g]['formatted'] = imap_utf7_decode_local($mailbox);
387 $boxesall[$g]['unformatted-dm'] = $mailbox;
388 if (substr($mailbox, -1) == $delimiter) {
389 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
391 $boxesall[$g]['unformatted'] = $mailbox;
392 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
393 $mailbox = substr($mailbox, strlen($folder_prefix));
395 $boxesall[$g]['unformatted-disp'] = $mailbox;
396 $boxesall[$g]['id'] = $g;
398 $boxesall[$g]['flags'] = array();
399 if (isset($line[$g])) {
400 ereg("\(([^)]*)\)",$line[$g],$regs);
401 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
403 $boxesall[$g]['flags'] = explode(' ', $flags);
411 * Sorting function used to sort mailbox names.
412 * + Original patch from dave_michmerhuizen@yahoo.com
413 * + Allows case insensitivity when sorting folders
414 * + Takes care of the delimiter being sorted to the end, causing
415 * subfolders to be listed in below folders that are prefixed
416 * with their parent folders name.
418 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
419 * Without special sort function: foobar between foo and foo.bar
420 * With special sort function: foobar AFTER foo and foo.bar :)
422 function user_strcasecmp($a, $b) {
425 return strnatcasecmp($a, $b);
427 /* Calculate the length of some strings. */
428 $a_length = strlen($a);
429 $b_length = strlen($b);
430 $min_length = min($a_length, $b_length);
431 $delimiter_length = strlen($delimiter);
433 /* Set the initial result value. */
435 /* Check the strings... */
436 for ($c = 0; $c < $min_length; ++
$c) {
437 $a_del = substr($a, $c, $delimiter_length);
438 $b_del = substr($b, $c, $delimiter_length);
440 if (($a_del == $delimiter) && ($b_del == $delimiter)) {
442 } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
444 } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
447 $result = strcasecmp($a{$c}, $b{$c});
455 /* If one string is a prefix of the other... */
457 if ($a_length < $b_length) {
459 } else if ($a_length > $b_length) {
468 * Returns list of options (to be echoed into select statement
469 * based on available mailboxes and separators
470 * Caller should surround options with <SELECT..> </SELECT> and
472 * $imap_stream - $imapConnection to query for mailboxes
473 * $show_selected - array containing list of mailboxes to pre-select (0 if none)
474 * $folder_skip - array of folders to keep out of option list (compared in lower)
475 * $boxes - list of already fetched boxes (for places like folder panel, where
476 * you know these options will be shown 3 times in a row.. (most often unset).
478 function sqimap_mailbox_option_list($imap_stream, $show_selected = 0, $folder_skip = 0, $boxes = 0 ) {
479 global $username, $data_dir;
482 $shorten_box_names = getPref($data_dir, $username, 'mailbox_select_style', SMPREF_OFF
);
485 $boxes = sqimap_mailbox_list($imap_stream);
487 foreach ($boxes as $boxes_part) {
488 if (!in_array('noselect', $boxes_part['flags'])) {
489 $box = $boxes_part['unformatted'];
490 $lowerbox = strtolower($box);
492 if ($folder_skip != 0 && in_array($lowerbox, $folder_skip) ) {
495 if ($lowerbox == 'inbox'){
497 } else if ( $shorten_box_names == 2 ) { /* delimited, style = 2 */
498 $box2 = str_replace(' ', '. ', $boxes_part['formatted']);
499 } else if ( $shorten_box_names == 1 ) { /* indent, style = 1 */
500 $box2 = $boxes_part['formatted'];
501 } else { /* default, long names, style = 0 */
502 $box2 = str_replace(' ', ' ', imap_utf7_decode_local($boxes_part['unformatted-disp']));
504 if ($show_selected != 0 && in_array($lowerbox, $show_selected) ) {
505 $mbox_options .= '<OPTION VALUE="'.$box.'" SELECTED>'.$box2.'</OPTION>' . "\n";
507 $mbox_options .= '<OPTION VALUE="'.$box.'">'.$box2.'</OPTION>' . "\n";
511 return $mbox_options;
515 * Returns sorted mailbox lists in several different ways.
516 * See comment on sqimap_mailbox_parse() for info about the returned array.
518 function sqimap_mailbox_list($imap_stream) {
519 global $default_folder_prefix;
521 if (!isset($boxesnew)) {
522 global $data_dir, $username, $list_special_folders_first,
523 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
524 $move_to_trash, $move_to_sent, $save_as_draft,
525 $delimiter, $noselect_fix_enable;
527 $inbox_in_list = false;
528 $inbox_subscribed = false;
530 require_once(SM_PATH
. 'include/load_prefs.php');
532 if ($noselect_fix_enable) {
533 $lsub_args = "LSUB \"$folder_prefix\" \"*%\"";
535 $lsub_args = "LSUB \"$folder_prefix\" \"*\"";
538 $lsub_ary = sqimap_run_command ($imap_stream, $lsub_args,
539 true, $response, $message);
543 * Section about removing the last element was removed
544 * We don't return "* OK" anymore from sqimap_read_data
547 $sorted_lsub_ary = array();
548 for ($i = 0, $cnt = count($lsub_ary);$i < $cnt; $i++
) {
550 * Workaround for EIMS
551 * Doesn't work if the mailbox name is multiple lines
553 if (isset($lsub_ary[$i +
1]) &&
554 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
555 $lsub_ary[$i], $regs)) {
557 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
559 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
560 $sorted_lsub_ary[] = $temp_mailbox_name;
561 if (strtoupper($temp_mailbox_name) == 'INBOX') {
562 $inbox_subscribed = true;
566 for ($i = 0, $cnt = count($sorted_lsub_ary); $i < $cnt; $i++
) {
567 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
568 $new_ary[] = $sorted_lsub_ary[$i];
571 $sorted_lsub_ary = $new_ary;
572 if (isset($sorted_lsub_ary)) {
573 usort($sorted_lsub_ary, 'user_strcasecmp');
575 $sorted_list_ary = $sorted_lsub_ary;
577 /* $sorted_list_ary = array();
578 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
580 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
581 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
584 $mbx = $sorted_lsub_ary[$i];
587 $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
588 true, $response, $message);
590 /* Another workaround for EIMS */
592 if (isset($read[1]) &&
593 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
595 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) . '"' . $regs[2];
598 if (isset($sorted_list_ary[$i])) {
599 $sorted_list_ary[$i] = '';
602 if (isset($read[0])) {
603 $sorted_list_ary[$i] = $read[0];
606 $sorted_list_ary[$i] = '';
609 if (isset($sorted_list_ary[$i]) &&
610 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
611 $inbox_in_list = true;
615 /* $inbox_in_list = true; */
617 * Just in case they're not subscribed to their inbox,
618 * we'll get it for them anyway
620 if (!$inbox_subscribed) {/* || !$inbox_in_list) { */
621 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
622 true, $response, $message);
623 /* Another workaround for EIMS */
624 if (isset($inbox_ary[1]) &&
625 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
626 $inbox_ary[0], $regs)) {
627 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
631 $sorted_list_ary[] = $inbox_ary[0];
632 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
635 $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
637 /* Now, lets sort for special folders */
638 $boxesnew = $used = array();
641 $cnt = count($boxesall);
642 for($k = 0; $k < $cnt; $k++
) {
643 if (strtolower($boxesall[$k]['unformatted']) == 'inbox') {
644 $boxesnew[] = $boxesall[$k];
650 /* List special folders and their subfolders, if requested. */
651 if ($list_special_folders_first) {
652 for($k = 0; $k < $cnt; $k++
) {
653 if (!$used[$k] && isSpecialMailbox($boxesall[$k]['unformatted'])) {
654 $boxesnew[] = $boxesall[$k];
657 $spec_sub = str_replace(' ', '', $boxesall[$k]['formatted']);
658 $spec_sub = preg_replace("/(\*|\[|\]|\(|\)|\?|\+|\{|\}|\^|\\$)/", '\\\\'.'\\1', $spec_sub);
660 /* In case of problems with preg
661 here is a ereg version
662 if (!$used[$k] && ereg("^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$", $box['unformatted']) ) {
664 $match = "?^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$?";
665 if (!$used[$k] && preg_match($match, $boxesall[$k]['unformatted']) ) {
666 $boxesnew[] = $boxesall[$k];
672 /* Rest of the folders */
673 for($k = 0; $k < $cnt; $k++
) {
675 $boxesnew[] = $boxesall[$k];
683 * Returns a list of all folders, subscribed or not
685 function sqimap_mailbox_list_all($imap_stream) {
686 global $list_special_folders_first, $folder_prefix, $delimiter;
688 $ssid = sqimap_session_id();
689 $lsid = strlen( $ssid );
690 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
691 $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message);
694 $fld_pre_length = strlen($folder_prefix);
696 for ($i = 0, $cnt = count($read_ary); $i < $cnt; $i++
) {
697 /* Another workaround for EIMS */
698 if (isset($read_ary[$i +
1]) &&
699 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
700 $read_ary[$i], $regs)) {
702 $read_ary[$i] = $regs[1] . '"' . addslashes(trim($read_ary[$i])) . '"' . $regs[2];
704 if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
705 /* Store the raw IMAP reply */
706 $boxes[$g]['raw'] = $read_ary[$i];
708 /* Count number of delimiters ($delimiter) in folder name */
709 $mailbox = find_mailbox_name($read_ary[$i]);
710 $dm_count = substr_count($mailbox, $delimiter);
711 if (substr($mailbox, -1) == $delimiter) {
712 /* If name ends in delimiter - decrement count by one */
716 /* Format folder name, but only if it's a INBOX.* or has a parent. */
717 $boxesallbyname[$mailbox] = $g;
718 $parentfolder = readMailboxParent($mailbox, $delimiter);
719 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
720 (ereg('^'.$folder_prefix, $mailbox)) ||
721 ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
723 $boxes[$g]['formatted'] = str_repeat(' ', $dm_count);
725 $boxes[$g]['formatted'] = '';
727 $boxes[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
729 $boxes[$g]['formatted'] = imap_utf7_decode_local($mailbox);
732 $boxes[$g]['unformatted-dm'] = $mailbox;
733 if (substr($mailbox, -1) == $delimiter) {
734 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
736 $boxes[$g]['unformatted'] = $mailbox;
737 $boxes[$g]['unformatted-disp'] = substr($mailbox,$fld_pre_length);
739 $boxes[$g]['id'] = $g;
741 /* Now lets get the flags for this mailbox */
742 $read_mlbx = $read_ary[$i];
744 // $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
745 // true, $response, $message);
747 /* Another workaround for EIMS */
748 // if (isset($read_mlbx[1]) &&
749 // ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", $read_mlbx[0], $regs)) {
750 // $read_mlbx[0] = $regs[1] . '"' . addslashes(trim($read_mlbx[1])) . '"' . $regs[2];
752 // echo $read_mlbx[0] .' raw 2 <br>';
754 $flags = substr($read_mlbx, strpos($read_mlbx, '(')+
1);
755 $flags = substr($flags, 0, strpos($flags, ')'));
756 $flags = str_replace('\\', '', $flags);
757 $flags = trim(strtolower($flags));
759 $boxes[$g]['flags'] = explode(' ', $flags);
761 $boxes[$g]['flags'] = array();
766 if(is_array($boxes)) {
773 function sqimap_mailbox_tree($imap_stream) {
774 global $boxesnew, $default_folder_prefix, $unseen_notify, $unseen_type;
775 if (!isset($boxesnew)) {
777 global $data_dir, $username, $list_special_folders_first,
778 $folder_prefix, $delimiter, $trash_folder, $move_to_trash;
781 $inbox_in_list = false;
782 $inbox_subscribed = false;
784 require_once(SM_PATH
. 'include/load_prefs.php');
787 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
788 true, $response, $message);
791 * Section about removing the last element was removed
792 * We don't return "* OK" anymore from sqimap_read_data
794 $sorted_lsub_ary = array();
795 $cnt = count($lsub_ary);
796 for ($i = 0; $i < $cnt; $i++
) {
798 * Workaround for EIMS
799 * Doesn't work if the mailbox name is multiple lines
801 if (isset($lsub_ary[$i +
1]) &&
802 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
803 $lsub_ary[$i], $regs)) {
805 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
809 if (preg_match("/^\*\s+LSUB\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$lsub_ary[$i],$regs)) {
811 $mbx = trim($regs[3]);
812 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
815 $mbx = find_mailbox_name($lsub_ary[$i]);
816 $noselect = check_is_noselect($lsub_ary[$i]);
817 if (substr($mbx, -1) == $delimiter) {
818 $mbx = substr($mbx, 0, strlen($mbx) - 1);
820 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect);
822 array_multisort($sorted_lsub_ary, SORT_ASC
, SORT_REGULAR
);
824 for($i = 0; $i < $cnt; $i++
) {
825 if ($sorted_lsub_ary[$i]['mbx'] == 'INBOX') {
826 $inbox_in_list = true;
832 * Just in case they're not subscribed to their inbox,
833 * we'll get it for them anyway
835 if (!$inbox_in_list) {
836 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
837 true, $response, $message);
838 /* Another workaround for EIMS */
839 if (isset($inbox_ary[1]) &&
840 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
841 $inbox_ary[0], $regs)) {
842 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
845 $mbx = find_mailbox_name($inbox_ary[0]);
846 if (substr($mbx, -1) == $delimiter) {
847 $mbx = substr($mbx, 0, strlen($mbx) - 1);
849 if ($mbx == 'INBOX') {
850 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => '');
851 sqimap_subscribe($imap_stream, 'INBOX');
856 if (preg_match("/^\*\s+LIST\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$inbox_ary[0],$regs)) {
858 $mbx = trim($regs[3]);
859 if (substr($mbx, -1) == $delimiter) {
860 $mbx = substr($mbx, 0, strlen($mbx) - 1);
862 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
866 for ($i = 0 ; $i < $cnt; $i++
) {
867 $mbx = $sorted_lsub_ary[$i]['mbx'];
868 if (($unseen_notify == 2 && $mbx == 'INBOX') ||
869 ($unseen_notify == 3) ||
870 ($move_to_trash && ($mbx == $trash_folder))) {
871 if($sorted_lsub_ary[$i]['noselect']) {
872 $sorted_lsub_ary[$i]['unseen'] = 0;
874 $sorted_lsub_ary[$i]['unseen'] =
875 sqimap_unseen_messages($imap_stream, $mbx);
877 if (($unseen_type == 2) ||
878 ($move_to_trash && ($mbx == $trash_folder)) ||
879 ($mbx == $trash_folder)) {
880 if($sorted_lsub_ary[$i]['noselect']) {
881 $sorted_lsub_ary[$i]['nummessages'] = 0;
883 $sorted_lsub_ary[$i]['nummessages'] =
884 sqimap_get_num_messages($imap_stream, $mbx);
889 $boxesnew = sqimap_fill_mailbox_tree($sorted_lsub_ary);
895 function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false) {
896 global $data_dir, $username, $list_special_folders_first,
897 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
898 $move_to_trash, $move_to_sent, $save_as_draft,
901 $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
903 /* create virtual root node */
904 $mailboxes= new mailboxes();
905 $mailboxes->is_root
= true;
907 if (isset($folder_prefix) && $folder_prefix != '') {
908 $start = substr_count($folder_prefix,$delimiter);
909 if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
911 $mailboxes->mailboxname_full
= substr($folder_prefix,0, (strlen($folder_prefix)-1));
913 $mailboxes->mailboxname_full
= $folder_prefix;
916 $mailboxes->mailboxname_sub
= $mailboxes->mailboxname_full
;
920 $cnt = count($mbx_ary);
921 for ($i=0; $i < $cnt; $i++
) {
922 if ($mbx_ary[$i]['mbx'] !='' ) {
923 $mbx = new mailboxes();
924 $mailbox = $mbx_ary[$i]['mbx'];
927 $mbx->is_inbox
= true;
928 $mbx->is_special
= true;
931 $mbx->is_trash
= true;
932 $mbx->is_special
= true;
935 $mbx->is_sent
= true;
936 $mbx->is_special
= true;
939 $mbx->is_draft
= true;
940 $mbx->is_special
= true;
944 if (isset($mbx_ary[$i]['unseen'])) {
945 $mbx->unseen
= $mbx_ary[$i]['unseen'];
947 if (isset($mbx_ary[$i]['nummessages'])) {
948 $mbx->total
= $mbx_ary[$i]['nummessages'];
951 $mbx->is_noselect
= $mbx_ary[$i]['noselect'];
953 $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
955 $mbx->mailboxname_sub
= substr($mbx_ary[$i]['mbx'],$r_del_pos+
1);
956 } else { /* mailbox is root folder */
957 $mbx->mailboxname_sub
= $mbx_ary[$i]['mbx'];
959 $mbx->mailboxname_full
= $mbx_ary[$i]['mbx'];
960 $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);