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 FIXME. This class should be extracted and placed in a separate file that
19 can be included before we start the session. That makes caching of the tree
20 possible. On a refresh mailboxes from left_main.php the only function that
21 should be called is the sqimap_get_status_mbx_tree. In case of subscribe
22 / rename / delete / new we have to create methods for adding/changing the
23 mailbox in the mbx_tree without the need for a refresh.
27 var $mailboxname_full = '', $mailboxname_sub= '', $is_noselect = false,
28 $is_special = false, $is_root = false, $is_inbox = false, $is_sent = false,
29 $is_trash = false, $is_draft = false, $mbxs = array(),
30 $unseen = false, $total = false;
32 function addMbx($mbx, $delimiter, $start, $specialfirst) {
33 $ary = explode($delimiter, $mbx->mailboxname_full
);
35 for ($i = $start, $c = count($ary)-1; $i < $c; $i++
) {
36 $mbx_childs =& $mbx_parent->mbxs
;
39 foreach ($mbx_childs as $key => $parent) {
40 if ($parent->mailboxname_sub
== $ary[$i]) {
41 $mbx_parent =& $mbx_parent->mbxs
[$key];
48 $no_select_mbx = new mailboxes();
49 if (isset($mbx_parent->mailboxname_full
) && $mbx_parent->mailboxname_full
!= '') {
50 $no_select_mbx->mailboxname_full
= $mbx_parent->mailboxname_full
.$delimiter.$ary[$i];
52 $no_select_mbx->mailboxname_full
= $ary[$i];
54 $no_select_mbx->mailboxname_sub
= $ary[$i];
55 $no_select_mbx->is_noselect
= true;
56 $mbx_parent->mbxs
[] = $no_select_mbx;
60 $mbx_parent->mbxs
[] = $mbx;
61 if ($mbx->is_special
&& $specialfirst) {
62 usort($mbx_parent->mbxs
, 'sortSpecialMbx');
67 function sortSpecialMbx($a, $b) {
69 $acmp = '0'. $a->mailboxname_full
;
70 } else if ($a->is_special
) {
71 $acmp = '1'. $a->mailboxname_full
;
73 $acmp = '2' . $a->mailboxname_full
;
76 $bcmp = '0'. $b->mailboxname_full
;
77 }else if ($b->is_special
) {
78 $bcmp = '1' . $b->mailboxname_full
;
80 $bcmp = '2' . $b->mailboxname_full
;
82 if ($acmp == $bcmp) return 0;
83 return ($acmp > $bcmp) ?
1: -1;
86 function find_mailbox_name ($mailbox) {
87 if (preg_match('/\*.+\"([^\r\n\"]*)\"[\s\r\n]*$/', $mailbox, $regs))
89 if (ereg(" *\"([^\r\n\"]*)\"[ \r\n]*$", $mailbox, $regs))
91 ereg(" *([^ \r\n\"]*)[ \r\n]*$",$mailbox,$regs);
95 function check_is_noselect ($lsub_line) {
96 return preg_match("/^\* (LSUB|LIST) \([^\)]*\\\\Noselect[^\)]*\)/i", $lsub_line);
100 * If $haystack is a full mailbox name, and $needle is the mailbox
101 * separator character, returns the second last part of the full
102 * mailbox name (i.e. the mailbox's parent mailbox)
104 function readMailboxParent($haystack, $needle) {
108 $parts = explode($needle, $haystack);
109 $elem = array_pop($parts);
110 while ($elem == '' && count($parts)) {
111 $elem = array_pop($parts);
113 $ret = join($needle, $parts);
119 * Check if $subbox is below the specified $parentbox
121 function isBoxBelow( $subbox, $parentbox ) {
124 * Eliminate the obvious mismatch, where the
125 * subfolder path is shorter than that of the potential parent
127 if ( strlen($subbox) < strlen($parentbox) ) {
130 /* check for delimiter */
131 if (!substr($parentbox,-1) == $delimiter) {
132 $parentbox.=$delimiter;
134 if (substr($subbox,0,strlen($parentbox)) == $parentbox) {
141 /* Defines special mailboxes */
142 function isSpecialMailbox( $box ) {
143 global $trash_folder, $sent_folder, $draft_folder,
144 $move_to_trash, $move_to_sent, $save_as_draft;
146 $ret = ( (strtolower($box) == 'inbox') ||
147 isTrashMailbox($box) ||
isSentMailbox($box) ||
isDraftMailbox($box) );
150 $ret = do_hook_function( 'special_mailbox', $box );
155 function isTrashMailbox ($box) {
156 global $trash_folder, $move_to_trash;
157 return $move_to_trash && $trash_folder &&
158 ( $box == $trash_folder ||
isBoxBelow($box, $trash_folder) );
161 function isSentMailbox($box) {
162 global $sent_folder, $move_to_sent;
163 return $move_to_sent && $sent_folder &&
164 ( $box == $sent_folder ||
isBoxBelow($box, $sent_folder) );
167 function isDraftMailbox($box) {
168 global $draft_folder, $save_as_draft;
169 return $save_as_draft &&
170 ( $box == $draft_folder ||
isBoxBelow($box, $draft_folder) );
173 /* Expunges a mailbox */
174 function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true, $id='') {
178 $id = sqimap_message_list_squisher($id);
185 $read = sqimap_run_command($imap_stream, 'EXPUNGE'.$id, $handle_errors,
186 $response, $message, $uid);
189 if (is_array($read)) {
190 foreach ($read as $r) {
191 if (preg_match('/^\*\s[0-9]+\sEXPUNGE/AUi',$r,$regs)) {
199 /* Checks whether or not the specified mailbox exists */
200 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
201 if (!isset($mailbox) ||
empty($mailbox)) {
204 $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
205 true, $response, $message);
206 return isset($mbx[0]);
209 /* Selects a mailbox */
210 function sqimap_mailbox_select ($imap_stream, $mailbox) {
211 global $auto_expunge;
213 if ($mailbox == 'None') {
217 $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
218 true, $response, $message);
220 for ($i = 0, $cnt = count($read); $i < $cnt; $i++
) {
221 if (preg_match('/^\*\s+OK\s\[(\w+)\s(\w+)\]/',$read[$i], $regs)) {
222 $result[strtoupper($regs[1])] = $regs[2];
223 } else if (preg_match('/^\*\s([0-9]+)\s(\w+)/',$read[$i], $regs)) {
224 $result[strtoupper($regs[2])] = $regs[1];
226 if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) {
227 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ;
228 $result['PERMANENTFLAGS'] = $regs[1];
229 } else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) {
230 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ;
231 $result['FLAGS'] = $regs[1];
235 if (preg_match('/^\[(.+)\]/',$message, $regs)) {
236 $result['RIGHTS']=$regs[1];
240 $tmp = sqimap_run_command($imap_stream, 'EXPUNGE', false, $a, $b);
245 /* Creates a folder */
246 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
248 if (strtolower($type) == 'noselect') {
249 $mailbox .= $delimiter;
252 $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
253 true, $response, $message);
254 sqimap_subscribe ($imap_stream, $mailbox);
257 /* Subscribes to an existing folder */
258 function sqimap_subscribe ($imap_stream, $mailbox) {
259 $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"",
260 true, $response, $message);
263 /* Unsubscribes to an existing folder */
264 function sqimap_unsubscribe ($imap_stream, $mailbox) {
265 $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"",
266 true, $response, $message);
269 /* Deletes the given folder */
270 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
271 global $data_dir, $username;
272 $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
273 true, $response, $message);
274 sqimap_unsubscribe ($imap_stream, $mailbox);
275 do_hook_function('rename_or_delete_folder', $args = array($mailbox, 'delete', ''));
276 removePref($data_dir, $username, "thread_$mailbox");
279 /* Determines if the user is subscribed to the folder or not */
280 function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
281 $boxesall = sqimap_mailbox_list ($imap_stream);
282 foreach ($boxesall as $ref) {
283 if ($ref['unformatted'] == $folder) {
290 /* Renames a mailbox */
291 function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
292 if ( $old_name != $new_name ) {
293 global $delimiter, $imap_server_type, $data_dir, $username;
294 if ( substr( $old_name, -1 ) == $delimiter ) {
295 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
296 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
297 $postfix = $delimiter;
302 $boxesall = sqimap_mailbox_list($imap_stream);
303 $cmd = 'RENAME "' . $old_name . '" "' . $new_name . '"';
304 $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message);
305 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
306 $oldpref = getPref($data_dir, $username, 'thread_'.$old_name.$postfix);
307 removePref($data_dir, $username, 'thread_'.$old_name.$postfix);
308 sqimap_subscribe($imap_stream, $new_name.$postfix);
309 setPref($data_dir, $username, 'thread_'.$new_name.$postfix, $oldpref);
310 do_hook_function('rename_or_delete_folder',$args = array($old_name, 'rename', $new_name));
311 $l = strlen( $old_name ) +
1;
314 foreach ($boxesall as $box) {
315 if (substr($box[$p], 0, $l) == $old_name . $delimiter) {
316 $new_sub = $new_name . $delimiter . substr($box[$p], $l);
317 if ($imap_server_type == 'cyrus') {
318 $cmd = 'RENAME "' . $box[$p] . '" "' . $new_sub . '"';
319 $data = sqimap_run_command($imap_stream, $cmd, true,
320 $response, $message);
322 sqimap_unsubscribe($imap_stream, $box[$p]);
323 $oldpref = getPref($data_dir, $username, 'thread_'.$box[$p]);
324 removePref($data_dir, $username, 'thread_'.$box[$p]);
325 sqimap_subscribe($imap_stream, $new_sub);
326 setPref($data_dir, $username, 'thread_'.$new_sub, $oldpref);
327 do_hook_function('rename_or_delete_folder',
328 $args = array($box[$p], 'rename', $new_sub));
335 * Formats a mailbox into 4 parts for the $boxesall array
337 * The four parts are:
339 * raw - Raw LIST/LSUB response from the IMAP server
340 * formatted - nicely formatted folder name
341 * unformatted - unformatted, but with delimiter at end removed
342 * unformatted-dm - folder name as it appears in raw response
343 * unformatted-disp - unformatted without $folder_prefix
345 function sqimap_mailbox_parse ($line, $line_lsub) {
346 global $folder_prefix, $delimiter;
348 /* Process each folder line */
349 for ($g = 0, $cnt = count($line); $g < $cnt; ++
$g) {
350 /* Store the raw IMAP reply */
351 if (isset($line[$g])) {
352 $boxesall[$g]['raw'] = $line[$g];
354 $boxesall[$g]['raw'] = '';
357 /* Count number of delimiters ($delimiter) in folder name */
358 $mailbox = trim($line_lsub[$g]);
359 $dm_count = substr_count($mailbox, $delimiter);
360 if (substr($mailbox, -1) == $delimiter) {
361 /* If name ends in delimiter, decrement count by one */
365 /* Format folder name, but only if it's a INBOX.* or has a parent. */
366 $boxesallbyname[$mailbox] = $g;
367 $parentfolder = readMailboxParent($mailbox, $delimiter);
368 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
369 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
370 (isset($boxesallbyname[$parentfolder]) &&
371 (strlen($parentfolder) > 0) ) ) {
372 $indent = $dm_count - (substr_count($folder_prefix, $delimiter));
374 $boxesall[$g]['formatted'] = str_repeat(' ', $indent);
376 $boxesall[$g]['formatted'] = '';
378 $boxesall[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
380 $boxesall[$g]['formatted'] = imap_utf7_decode_local($mailbox);
383 $boxesall[$g]['unformatted-dm'] = $mailbox;
384 if (substr($mailbox, -1) == $delimiter) {
385 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
387 $boxesall[$g]['unformatted'] = $mailbox;
388 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
389 $mailbox = substr($mailbox, strlen($folder_prefix));
391 $boxesall[$g]['unformatted-disp'] = $mailbox;
392 $boxesall[$g]['id'] = $g;
394 $boxesall[$g]['flags'] = array();
395 if (isset($line[$g])) {
396 ereg("\(([^)]*)\)",$line[$g],$regs);
397 // FIXME Flags do contain the \ character. \NoSelect \NoInferiors
398 // and $MDNSent <= last one doesn't have the \
399 // It's better to follow RFC3501 instead of using our own naming.
400 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
402 $boxesall[$g]['flags'] = explode(' ', $flags);
410 * Sorting function used to sort mailbox names.
411 * + Original patch from dave_michmerhuizen@yahoo.com
412 * + Allows case insensitivity when sorting folders
413 * + Takes care of the delimiter being sorted to the end, causing
414 * subfolders to be listed in below folders that are prefixed
415 * with their parent folders name.
417 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
418 * Without special sort function: foobar between foo and foo.bar
419 * With special sort function: foobar AFTER foo and foo.bar :)
421 function user_strcasecmp($a, $b) {
422 return strnatcasecmp($a, $b);
426 * Returns list of options (to be echoed into select statement
427 * based on available mailboxes and separators
428 * Caller should surround options with <SELECT..> </SELECT> and
430 * $imap_stream - $imapConnection to query for mailboxes
431 * $show_selected - array containing list of mailboxes to pre-select (0 if none)
432 * $folder_skip - array of folders to keep out of option list (compared in lower)
433 * $boxes - list of already fetched boxes (for places like folder panel, where
434 * you know these options will be shown 3 times in a row.. (most often unset).
435 * $flag - flag to check for in mailbox flags, used to filter out mailboxes.
436 * 'noselect' by default to remove unselectable mailboxes.
437 * 'noinferiors' used to filter out folders that can not contain subfolders.
438 * NULL to avoid flag check entirely.
439 * NOTE: noselect and noiferiors are used internally. The IMAP representation is
440 * \NoSelect and \NoInferiors
441 * $use_long_format - override folder display preference and always show full folder name.
443 function sqimap_mailbox_option_list($imap_stream, $show_selected = 0, $folder_skip = 0, $boxes = 0,
444 $flag = 'noselect', $use_long_format = false ) {
445 global $username, $data_dir;
447 if ( $use_long_format ) {
448 $shorten_box_names = 0;
450 $shorten_box_names = getPref($data_dir, $username, 'mailbox_select_style', SMPREF_OFF
);
454 $boxes = sqimap_mailbox_list($imap_stream);
457 foreach ($boxes as $boxes_part) {
458 if ($flag == NULL ||
!in_array($flag, $boxes_part['flags'])) {
459 $box = $boxes_part['unformatted'];
461 if ($folder_skip != 0 && in_array($box, $folder_skip) ) {
464 $lowerbox = strtolower($box);
465 // mailboxes are casesensitive => inbox.sent != inbox.Sent
466 // nevermind, to many dependencies this should be fixed!
468 if (strtolower($box) == 'inbox') { // inbox is special and not casesensitive
471 switch ($shorten_box_names)
473 case 2: /* delimited, style = 2 */
474 $box2 = str_replace(' ', '. ', $boxes_part['formatted']);
476 case 1: /* indent, style = 1 */
477 $box2 = $boxes_part['formatted'];
479 default: /* default, long names, style = 0 */
480 $box2 = str_replace(' ', ' ', imap_utf7_decode_local($boxes_part['unformatted-disp']));
484 if ($show_selected != 0 && in_array($lowerbox, $show_selected) ) {
485 $mbox_options .= '<OPTION VALUE="'.$box.'" SELECTED>'.$box2.'</OPTION>' . "\n";
487 $mbox_options .= '<OPTION VALUE="'.$box.'">'.$box2.'</OPTION>' . "\n";
491 return $mbox_options;
495 * Returns sorted mailbox lists in several different ways.
496 * See comment on sqimap_mailbox_parse() for info about the returned array.
498 function sqimap_mailbox_list($imap_stream) {
499 global $default_folder_prefix;
501 if (!isset($boxesnew)) {
502 global $data_dir, $username, $list_special_folders_first,
503 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
504 $move_to_trash, $move_to_sent, $save_as_draft,
505 $delimiter, $noselect_fix_enable;
507 $inbox_in_list = false;
508 $inbox_subscribed = false;
510 require_once(SM_PATH
. 'include/load_prefs.php');
512 if ($noselect_fix_enable) {
513 $lsub_args = "LSUB \"$folder_prefix\" \"*%\"";
515 $lsub_args = "LSUB \"$folder_prefix\" \"*\"";
518 $lsub_ary = sqimap_run_command ($imap_stream, $lsub_args,
519 true, $response, $message);
521 $sorted_lsub_ary = array();
522 for ($i = 0, $cnt = count($lsub_ary);$i < $cnt; $i++
) {
524 * Workaround for mailboxes returned as literal
525 * Doesn't work if the mailbox name is multiple lines
526 * (larger then fgets buffer)
528 if (isset($lsub_ary[$i +
1]) && substr($lsub_ary[$i],-3) == "}\r\n") {
529 if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
530 $lsub_ary[$i], $regs)) {
532 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
535 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
536 $sorted_lsub_ary[] = $temp_mailbox_name;
537 if (!$inbox_subscribed && strtoupper($temp_mailbox_name) == 'INBOX') {
538 $inbox_subscribed = true;
541 /* remove duplicates */
542 $sorted_lsub_ary = array_unique($sorted_lsub_ary);
544 /* natural sort mailboxes */
545 if (isset($sorted_lsub_ary)) {
546 usort($sorted_lsub_ary, 'user_strcasecmp');
549 * The LSUB response doesn't provide us information about \Noselect
550 * mail boxes. The LIST response does, that's why we need to do a LIST
551 * call to retrieve the flags for the mailbox
552 * Note: according RFC2060 an imap server may provide \NoSelect flags in the LSUB response.
553 * in other words, we cannot rely on it.
555 $sorted_list_ary = array();
556 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
557 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
558 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
561 $mbx = $sorted_lsub_ary[$i];
564 $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
565 true, $response, $message);
567 /* Another workaround for literals */
569 if (isset($read[1]) && substr($read[1],-3) == "}\r\n") {
570 if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
572 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) . '"' . $regs[2];
576 if (isset($read[0])) {
577 $sorted_list_ary[$i] = $read[0];
579 $sorted_list_ary[$i] = '';
584 * Just in case they're not subscribed to their inbox,
585 * we'll get it for them anyway
587 if (!$inbox_subscribed) {
588 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
589 true, $response, $message);
590 /* Another workaround for literals */
591 if (isset($inbox_ary[1]) && substr($inbox_ary[0],-3) == "}\r\n") {
592 if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
593 $inbox_ary[0], $regs)) {
594 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
598 $sorted_list_ary[] = $inbox_ary[0];
599 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
602 $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
604 /* Now, lets sort for special folders */
605 $boxesnew = $used = array();
608 $cnt = count($boxesall);
609 $used = array_pad($used,$cnt,false);
610 for($k = 0; $k < $cnt; ++
$k) {
611 if (strtolower($boxesall[$k]['unformatted']) == 'inbox') {
612 $boxesnew[] = $boxesall[$k];
617 /* List special folders and their subfolders, if requested. */
618 if ($list_special_folders_first) {
619 for($k = 0; $k < $cnt; ++
$k) {
620 if (!$used[$k] && isSpecialMailbox($boxesall[$k]['unformatted'])) {
621 $boxesnew[] = $boxesall[$k];
627 /* Rest of the folders */
628 for($k = 0; $k < $cnt; $k++
) {
630 $boxesnew[] = $boxesall[$k];
638 * Returns a list of all folders, subscribed or not
640 function sqimap_mailbox_list_all($imap_stream) {
641 global $list_special_folders_first, $folder_prefix, $delimiter;
642 $read_ary = sqimap_run_command($imap_stream,"LIST \"$folder_prefix\" *",true,$response, $message,false);
645 $fld_pre_length = strlen($folder_prefix);
646 for ($i = 0, $cnt = count($read_ary); $i < $cnt; $i++
) {
647 /* Another workaround for EIMS */
648 if (isset($read_ary[$i +
1]) &&
649 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
650 $read_ary[$i], $regs)) {
652 $read_ary[$i] = $regs[1] . '"' . addslashes(trim($read_ary[$i])) . '"' . $regs[2];
654 /* Store the raw IMAP reply */
655 $boxes[$g]['raw'] = $read_ary[$i];
657 /* Count number of delimiters ($delimiter) in folder name */
658 $mailbox = find_mailbox_name($read_ary[$i]);
659 $dm_count = substr_count($mailbox, $delimiter);
660 if (substr($mailbox, -1) == $delimiter) {
661 /* If name ends in delimiter - decrement count by one */
665 /* Format folder name, but only if it's a INBOX.* or has a parent. */
666 $boxesallbyname[$mailbox] = $g;
667 $parentfolder = readMailboxParent($mailbox, $delimiter);
668 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
669 (ereg('^'.$folder_prefix, $mailbox)) ||
670 ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
672 $boxes[$g]['formatted'] = str_repeat(' ', $dm_count);
674 $boxes[$g]['formatted'] = '';
676 $boxes[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
678 $boxes[$g]['formatted'] = imap_utf7_decode_local($mailbox);
681 $boxes[$g]['unformatted-dm'] = $mailbox;
682 if (substr($mailbox, -1) == $delimiter) {
683 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
685 $boxes[$g]['unformatted'] = $mailbox;
686 $boxes[$g]['unformatted-disp'] = substr($mailbox,$fld_pre_length);
688 $boxes[$g]['id'] = $g;
690 /* Now lets get the flags for this mailbox */
691 $read_mlbx = $read_ary[$i];
692 $flags = substr($read_mlbx, strpos($read_mlbx, '(')+
1);
693 $flags = substr($flags, 0, strpos($flags, ')'));
694 $flags = str_replace('\\', '', $flags);
695 $flags = trim(strtolower($flags));
697 $boxes[$g]['flags'] = explode(' ', $flags);
699 $boxes[$g]['flags'] = array();
703 if(is_array($boxes)) {
710 function sqimap_mailbox_tree($imap_stream) {
711 global $boxesnew, $default_folder_prefix, $unseen_notify, $unseen_type;
712 if (!isset($boxesnew)) {
714 global $data_dir, $username, $list_special_folders_first,
715 $folder_prefix, $delimiter, $trash_folder, $move_to_trash,
719 $inbox_in_list = false;
720 $inbox_subscribed = false;
722 require_once(SM_PATH
. 'include/load_prefs.php');
725 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
726 true, $response, $message);
728 /* Check to see if we have an INBOX */
731 for ($i = 0, $cnt = count($lsub_ary); $i < $cnt; $i++
) {
732 if (preg_match("/^\*\s+LSUB\s+(.*)\"?INBOX\"?[^(\/\.)].*$/",$lsub_ary[$i])) {
738 if ($has_inbox == false) {
739 $lsub_ibx = sqimap_run_command( $imap_stream, "LSUB \"\" \"INBOX\"", true, $response, $message );
740 if (isset($lsub_ibx[0]) && (preg_match("/^\*\s+LSUB\s+(.*)\"?INBOX\"?[^(\/\.)].*$/",$lsub_ibx[0]))) {
741 $lsub_ary[] = $lsub_ibx[0];
743 $lsub_ibx = sqimap_run_command( $imap_stream, "LIST \"\" \"INBOX\"", true, $response, $message );
744 if (preg_match("/^\*\s+LIST\s+(.*)\"?INBOX\"?[^(\/\.)].*$/",$lsub_ibx[0])) {
745 sqimap_run_command( $imap_stream, "SUBSCRIBE \"INBOX\"", true, $response, $message );
746 $lsub_ibx[0] = str_replace("LIST","LSUB",$lsub_ibx[0]);
747 $lsub_ary[] = $lsub_ibx[0];
753 * Section about removing the last element was removed
754 * We don't return "* OK" anymore from sqimap_read_data
756 $sorted_lsub_ary = array();
757 $cnt = count($lsub_ary);
759 for ($i = 0; $i < $cnt; $i++
) {
761 * Workaround for EIMS
762 * Doesn't work if the mailbox name is multiple lines
764 if (isset($lsub_ary[$i +
1]) &&
765 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
766 $lsub_ary[$i], $regs)) {
768 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
771 $mbx = find_mailbox_name($lsub_ary[$i]);
773 // only do the noselect test if !uw, is checked later. FIX ME see conf.pl setting
774 if ($imap_server_type != "uw") {
775 $noselect = check_is_noselect($lsub_ary[$i]);
777 if (substr($mbx, -1) == $delimiter) {
778 $mbx = substr($mbx, 0, strlen($mbx) - 1);
780 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect);
782 // FIX ME this requires a config setting inside conf.pl instead of checking on server type
783 if ($imap_server_type == "uw") {
786 // prepare an array with queries
787 foreach ($sorted_lsub_ary as $aMbx) {
789 $query = "LIST \"\" \"$mbx\"";
790 sqimap_prepare_pipelined_query($query,$tag,$aQuery,false);
793 $sorted_lsub_ary = array();
794 // execute all the queries at once
795 $aResponse = sqimap_run_pipelined_command ($imap_stream, $aQuery, false, $aServerResponse, $aServerMessage);
796 foreach($aTag as $tag => $mbx) {
797 if ($aServerResponse[$tag] == 'OK') {
798 $sResponse = implode('', $aResponse[$tag]);
799 $noselect = check_is_noselect($sResponse);
800 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect);
803 $cnt = count($sorted_lsub_ary);
806 $sorted_lsub_ary = array_values($sorted_lsub_ary);
807 array_multisort($sorted_lsub_ary, SORT_ASC
, SORT_REGULAR
);
808 $boxesnew = sqimap_fill_mailbox_tree($sorted_lsub_ary,false,$imap_stream);
813 function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false,$imap_stream) {
814 global $data_dir, $username, $list_special_folders_first,
815 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
816 $move_to_trash, $move_to_sent, $save_as_draft,
817 $delimiter, $imap_server_type;
819 $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
821 /* create virtual root node */
822 $mailboxes= new mailboxes();
823 $mailboxes->is_root
= true;
828 if (isset($folder_prefix) && ($folder_prefix != '')) {
829 $start = substr_count($folder_prefix,$delimiter);
830 if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
832 $mailboxes->mailboxname_full
= substr($folder_prefix,0, (strlen($folder_prefix)-1));
834 $mailboxes->mailboxname_full
= $folder_prefix;
837 $mailboxes->mailboxname_sub
= $mailboxes->mailboxname_full
;
842 $cnt = count($mbx_ary);
843 for ($i=0; $i < $cnt; $i++
) {
844 if ($mbx_ary[$i]['mbx'] !='' ) {
845 $mbx = new mailboxes();
846 $mailbox = $mbx_ary[$i]['mbx'];
849 $mbx->is_inbox
= true;
850 $mbx->is_special
= true;
853 $mbx->is_trash
= true;
854 $mbx->is_special
= true;
857 $mbx->is_sent
= true;
858 $mbx->is_special
= true;
861 $mbx->is_draft
= true;
862 $mbx->is_special
= true;
866 if (isset($mbx_ary[$i]['unseen'])) {
867 $mbx->unseen
= $mbx_ary[$i]['unseen'];
869 if (isset($mbx_ary[$i]['nummessages'])) {
870 $mbx->total
= $mbx_ary[$i]['nummessages'];
873 $mbx->is_noselect
= $mbx_ary[$i]['noselect'];
875 $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
877 $mbx->mailboxname_sub
= substr($mbx_ary[$i]['mbx'],$r_del_pos+
1);
878 } else { /* mailbox is root folder */
879 $mbx->mailboxname_sub
= $mbx_ary[$i]['mbx'];
881 $mbx->mailboxname_full
= $mbx_ary[$i]['mbx'];
883 $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);
886 sqimap_utf7_decode_mbx_tree($mailboxes);
887 sqimap_get_status_mbx_tree($imap_stream,$mailboxes);
891 function sqimap_utf7_decode_mbx_tree(&$mbx_tree) {
892 $mbx_tree->mailboxname_sub
=imap_utf7_decode_local($mbx_tree->mailboxname_sub
);
893 if ($mbx_tree->mbxs
) {
894 $iCnt = count($mbx_tree->mbxs
);
895 for ($i=0;$i<$iCnt;++
$i) {
896 $mbxs_tree->mbxs
[$i] = sqimap_utf7_decode_mbx_tree($mbx_tree->mbxs
[$i]);
902 function sqimap_tree_to_ref_array(&$mbx_tree,&$aMbxs) {
904 $aMbxs[] =& $mbx_tree;
905 if ($mbx_tree->mbxs
) {
906 $iCnt = count($mbx_tree->mbxs
);
907 for ($i=0;$i<$iCnt;++
$i) {
908 sqimap_tree_to_ref_array($mbx_tree->mbxs
[$i],$aMbxs);
914 /* Define preferences for folder settings. */
915 /* FIXME, we should load constants.php
917 define('SMPREF_UNSEEN_NONE', 1);
918 define('SMPREF_UNSEEN_INBOX', 2);
919 define('SMPREF_UNSEEN_ALL', 3);
921 define('SMPREF_UNSEEN_SPECIAL', 4); // Only special folders
922 define('SMPREF_UNSEEN_NORMAL', 5); // Only normal folders
925 define('SMPREF_UNSEEN_ONLY', 1);
926 define('SMPREF_UNSEEN_TOTAL', 2);
929 function sqimap_get_status_mbx_tree($imap_stream,&$mbx_tree) {
930 global $unseen_notify, $unseen_type, $trash_folder,$move_to_trash;
931 $aMbxs = $aQuery = $aTag = array();
932 sqimap_tree_to_ref_array($mbx_tree,$aMbxs);
933 // remove the root node
936 if($unseen_notify == 3) {
937 $cnt = count($aMbxs);
938 for($i=0;$i<$cnt;++
$i) {
940 if (!$oMbx->is_noselect
) {
941 $mbx = $oMbx->mailboxname_full
;
942 if ($unseen_type == 2 ||
943 ($move_to_trash && $oMbx->mailboxname_full
== $trash_folder)) {
944 $query = "STATUS \"$mbx\" (MESSAGES UNSEEN)";
946 $query = "STATUS \"$mbx\" (UNSEEN)";
948 sqimap_prepare_pipelined_query($query,$tag,$aQuery,false);
950 $oMbx->unseen
= $oMbx->total
= false;
956 // execute all the queries at once
957 $aResponse = sqimap_run_pipelined_command ($imap_stream, $aQuery, false, $aServerResponse, $aServerMessage);
958 $cnt = count($aMbxs);
959 for($i=0;$i<$cnt;++
$i) {
962 if ($tag && $aServerResponse[$tag] == 'OK') {
963 $sResponse = implode('', $aResponse[$tag]);
964 if (preg_match('/UNSEEN\s+([0-9]+)/i', $sResponse, $regs)) {
965 $oMbx->unseen
= $regs[1];
967 if (preg_match('/MESSAGES\s+([0-9]+)/i', $sResponse, $regs)) {
968 $oMbx->total
= $regs[1];
973 } else if ($unseen_notify == 2) { // INBOX only
974 $cnt = count($aMbxs);
975 for($i=0;$i<$cnt;++
$i) {
977 if (strtoupper($oMbx->mailboxname_full
) == 'INBOX' ||
978 ($move_to_trash && $oMbx->mailboxname_full
== $trash_folder)) {
979 if ($unseen_type == 2 ||
980 ($oMbx->mailboxname_full
== $trash_folder && $move_to_trash)) {
981 $aStatus = sqimap_status_messages($imap_stream,$oMbx->mailboxname_full
);
982 $oMbx->unseen
= $aStatus['UNSEEN'];
983 $oMbx->total
= $aStatus['MESSAGES'];
985 $oMbx->unseen
= sqimap_unseen_messages($imap_stream,$oMbx->mailboxname_full
);
988 if (!$move_to_trash && $trash_folder) {
991 // trash comes after INBOX
992 if ($oMbx->mailboxname_full
== $trash_folder) {