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 ) {
114 * Eliminate the obvious mismatch, where the
115 * subfolder path is shorter than that of the potential parent
117 if ( strlen($subbox) < strlen($parentbox) ) {
120 /* check for delimiter */
121 if (!substr($parentbox,-1) == $delimiter) {
122 $parentbox.=$delimiter;
124 if (substr($subbox,0,strlen($parentbox)) == $parentbox) {
131 /* Defines special mailboxes */
132 function isSpecialMailbox( $box ) {
133 global $trash_folder, $sent_folder, $draft_folder,
134 $move_to_trash, $move_to_sent, $save_as_draft;
136 $ret = ( (strtolower($box) == 'inbox') ||
137 isTrashMailbox($box) ||
isSentMailbox($box) ||
isDraftMailbox($box) );
140 $ret = do_hook_function( 'special_mailbox', $box );
145 function isTrashMailbox ($box) {
146 global $trash_folder, $move_to_trash;
147 return $move_to_trash && $trash_folder &&
148 ( $box == $trash_folder ||
isBoxBelow($box, $trash_folder) );
151 function isSentMailbox($box) {
152 global $sent_folder, $move_to_sent;
153 return $move_to_sent && $sent_folder &&
154 ( $box == $sent_folder ||
isBoxBelow($box, $sent_folder) );
157 function isDraftMailbox($box) {
158 global $draft_folder, $save_as_draft;
159 return $save_as_draft &&
160 ( $box == $draft_folder ||
isBoxBelow($box, $draft_folder) );
163 /* Expunges a mailbox */
164 function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true, $id='') {
168 $id = sqimap_message_list_squisher($id);
175 $read = sqimap_run_command($imap_stream, 'EXPUNGE'.$id, $handle_errors,
176 $response, $message, $uid);
179 if (is_array($read)) {
180 foreach ($read as $r) {
181 if (preg_match('/^\*\s[0-9]+\sEXPUNGE/AUi',$r,$regs)) {
189 /* Checks whether or not the specified mailbox exists */
190 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
191 if (!isset($mailbox) ||
empty($mailbox)) {
194 $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
195 true, $response, $message);
196 return isset($mbx[0]);
199 /* Selects a mailbox */
200 function sqimap_mailbox_select ($imap_stream, $mailbox) {
201 global $auto_expunge;
203 if ($mailbox == 'None') {
207 $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
208 true, $response, $message);
210 for ($i = 0, $cnt = count($read); $i < $cnt; $i++
) {
211 if (preg_match('/^\*\s+OK\s\[(\w+)\s(\w+)\]/',$read[$i], $regs)) {
212 $result[strtoupper($regs[1])] = $regs[2];
213 } else if (preg_match('/^\*\s([0-9]+)\s(\w+)/',$read[$i], $regs)) {
214 $result[strtoupper($regs[2])] = $regs[1];
216 if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) {
217 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ;
218 $result['PERMANENTFLAGS'] = $regs[1];
219 } else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) {
220 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ;
221 $result['FLAGS'] = $regs[1];
225 if (preg_match('/^\[(.+)\]/',$message, $regs)) {
226 $result['RIGHTS']=$regs[1];
230 $tmp = sqimap_run_command($imap_stream, 'EXPUNGE', false, $a, $b);
235 /* Creates a folder */
236 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
238 if (strtolower($type) == 'noselect') {
239 $mailbox .= $delimiter;
242 $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
243 true, $response, $message);
244 sqimap_subscribe ($imap_stream, $mailbox);
247 /* Subscribes to an existing folder */
248 function sqimap_subscribe ($imap_stream, $mailbox) {
249 $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"",
250 true, $response, $message);
253 /* Unsubscribes to an existing folder */
254 function sqimap_unsubscribe ($imap_stream, $mailbox) {
255 $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"",
256 true, $response, $message);
259 /* Deletes the given folder */
260 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
261 global $data_dir, $username;
262 $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
263 true, $response, $message);
264 sqimap_unsubscribe ($imap_stream, $mailbox);
265 do_hook_function('rename_or_delete_folder', $args = array($mailbox, 'delete', ''));
266 removePref($data_dir, $username, "thread_$mailbox");
269 /* Determines if the user is subscribed to the folder or not */
270 function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
271 $boxesall = sqimap_mailbox_list ($imap_stream);
272 foreach ($boxesall as $ref) {
273 if ($ref['unformatted'] == $folder) {
280 /* Renames a mailbox */
281 function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
282 if ( $old_name != $new_name ) {
283 global $delimiter, $imap_server_type, $data_dir, $username;
284 if ( substr( $old_name, -1 ) == $delimiter ) {
285 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
286 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
287 $postfix = $delimiter;
292 $boxesall = sqimap_mailbox_list($imap_stream);
293 $cmd = 'RENAME "' . $old_name . '" "' . $new_name . '"';
294 $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message);
295 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
296 $oldpref = getPref($data_dir, $username, 'thread_'.$old_name.$postfix);
297 removePref($data_dir, $username, 'thread_'.$old_name.$postfix);
298 sqimap_subscribe($imap_stream, $new_name.$postfix);
299 setPref($data_dir, $username, 'thread_'.$new_name.$postfix, $oldpref);
300 do_hook_function('rename_or_delete_folder',$args = array($old_name, 'rename', $new_name));
301 $l = strlen( $old_name ) +
1;
304 foreach ($boxesall as $box) {
305 if (substr($box[$p], 0, $l) == $old_name . $delimiter) {
306 $new_sub = $new_name . $delimiter . substr($box[$p], $l);
307 if ($imap_server_type == 'cyrus') {
308 $cmd = 'RENAME "' . $box[$p] . '" "' . $new_sub . '"';
309 $data = sqimap_run_command($imap_stream, $cmd, true,
310 $response, $message);
312 sqimap_unsubscribe($imap_stream, $box[$p]);
313 $oldpref = getPref($data_dir, $username, 'thread_'.$box[$p]);
314 removePref($data_dir, $username, 'thread_'.$box[$p]);
315 sqimap_subscribe($imap_stream, $new_sub);
316 setPref($data_dir, $username, 'thread_'.$new_sub, $oldpref);
317 do_hook_function('rename_or_delete_folder',
318 $args = array($box[$p], 'rename', $new_sub));
325 * Formats a mailbox into 4 parts for the $boxesall array
327 * The four parts are:
329 * raw - Raw LIST/LSUB response from the IMAP server
330 * formatted - nicely formatted folder name
331 * unformatted - unformatted, but with delimiter at end removed
332 * unformatted-dm - folder name as it appears in raw response
333 * unformatted-disp - unformatted without $folder_prefix
335 function sqimap_mailbox_parse ($line, $line_lsub) {
336 global $folder_prefix, $delimiter;
338 /* Process each folder line */
339 for ($g = 0, $cnt = count($line); $g < $cnt; ++
$g) {
340 /* Store the raw IMAP reply */
341 if (isset($line[$g])) {
342 $boxesall[$g]['raw'] = $line[$g];
344 $boxesall[$g]['raw'] = '';
347 /* Count number of delimiters ($delimiter) in folder name */
348 $mailbox = trim($line_lsub[$g]);
349 $dm_count = substr_count($mailbox, $delimiter);
350 if (substr($mailbox, -1) == $delimiter) {
351 /* If name ends in delimiter, decrement count by one */
355 /* Format folder name, but only if it's a INBOX.* or has a parent. */
356 $boxesallbyname[$mailbox] = $g;
357 $parentfolder = readMailboxParent($mailbox, $delimiter);
358 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
359 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
360 (isset($boxesallbyname[$parentfolder]) &&
361 (strlen($parentfolder) > 0) ) ) {
362 $indent = $dm_count - (substr_count($folder_prefix, $delimiter));
364 $boxesall[$g]['formatted'] = str_repeat(' ', $indent);
366 $boxesall[$g]['formatted'] = '';
368 $boxesall[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
370 $boxesall[$g]['formatted'] = imap_utf7_decode_local($mailbox);
373 $boxesall[$g]['unformatted-dm'] = $mailbox;
374 if (substr($mailbox, -1) == $delimiter) {
375 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
377 $boxesall[$g]['unformatted'] = $mailbox;
378 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
379 $mailbox = substr($mailbox, strlen($folder_prefix));
381 $boxesall[$g]['unformatted-disp'] = $mailbox;
382 $boxesall[$g]['id'] = $g;
384 $boxesall[$g]['flags'] = array();
385 if (isset($line[$g])) {
386 ereg("\(([^)]*)\)",$line[$g],$regs);
387 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
389 $boxesall[$g]['flags'] = explode(' ', $flags);
397 * Sorting function used to sort mailbox names.
398 * + Original patch from dave_michmerhuizen@yahoo.com
399 * + Allows case insensitivity when sorting folders
400 * + Takes care of the delimiter being sorted to the end, causing
401 * subfolders to be listed in below folders that are prefixed
402 * with their parent folders name.
404 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
405 * Without special sort function: foobar between foo and foo.bar
406 * With special sort function: foobar AFTER foo and foo.bar :)
408 function user_strcasecmp($a, $b) {
409 return strnatcasecmp($a, $b);
413 * Returns list of options (to be echoed into select statement
414 * based on available mailboxes and separators
415 * Caller should surround options with <SELECT..> </SELECT> and
417 * $imap_stream - $imapConnection to query for mailboxes
418 * $show_selected - array containing list of mailboxes to pre-select (0 if none)
419 * $folder_skip - array of folders to keep out of option list (compared in lower)
420 * $boxes - list of already fetched boxes (for places like folder panel, where
421 * you know these options will be shown 3 times in a row.. (most often unset).
423 function sqimap_mailbox_option_list($imap_stream, $show_selected = 0, $folder_skip = 0, $boxes = 0 ) {
424 global $username, $data_dir;
427 $shorten_box_names = getPref($data_dir, $username, 'mailbox_select_style', SMPREF_OFF
);
430 $boxes = sqimap_mailbox_list($imap_stream);
432 foreach ($boxes as $boxes_part) {
433 if (!in_array('noselect', $boxes_part['flags'])) {
434 $box = $boxes_part['unformatted'];
435 $lowerbox = strtolower($box);
437 if ($folder_skip != 0 && in_array($lowerbox, $folder_skip) ) {
440 if ($lowerbox == 'inbox'){
442 } else if ( $shorten_box_names == 2 ) { /* delimited, style = 2 */
443 $box2 = str_replace(' ', '. ', $boxes_part['formatted']);
444 } else if ( $shorten_box_names == 1 ) { /* indent, style = 1 */
445 $box2 = $boxes_part['formatted'];
446 } else { /* default, long names, style = 0 */
447 $box2 = str_replace(' ', ' ', imap_utf7_decode_local($boxes_part['unformatted-disp']));
449 if ($show_selected != 0 && in_array($lowerbox, $show_selected) ) {
450 $mbox_options .= '<OPTION VALUE="'.$box.'" SELECTED>'.$box2.'</OPTION>' . "\n";
452 $mbox_options .= '<OPTION VALUE="'.$box.'">'.$box2.'</OPTION>' . "\n";
456 return $mbox_options;
460 * Returns sorted mailbox lists in several different ways.
461 * See comment on sqimap_mailbox_parse() for info about the returned array.
463 function sqimap_mailbox_list($imap_stream) {
464 global $default_folder_prefix;
466 if (!isset($boxesnew)) {
467 global $data_dir, $username, $list_special_folders_first,
468 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
469 $move_to_trash, $move_to_sent, $save_as_draft,
470 $delimiter, $noselect_fix_enable;
472 $inbox_in_list = false;
473 $inbox_subscribed = false;
475 require_once(SM_PATH
. 'include/load_prefs.php');
477 if ($noselect_fix_enable) {
478 $lsub_args = "LSUB \"$folder_prefix\" \"*%\"";
480 $lsub_args = "LSUB \"$folder_prefix\" \"*\"";
483 $lsub_ary = sqimap_run_command ($imap_stream, $lsub_args,
484 true, $response, $message);
486 $sorted_lsub_ary = array();
487 for ($i = 0, $cnt = count($lsub_ary);$i < $cnt; $i++
) {
489 * Workaround for mailboxes returned as literal
490 * Doesn't work if the mailbox name is multiple lines
491 * (larger then fgets buffer)
493 if (isset($lsub_ary[$i +
1]) && substr($lsub_ary[$i],-3) == "}\r\n") {
494 if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
495 $lsub_ary[$i], $regs)) {
497 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
500 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
501 $sorted_lsub_ary[] = $temp_mailbox_name;
502 if (!$inbox_subscribed && strtoupper($temp_mailbox_name) == 'INBOX') {
503 $inbox_subscribed = true;
506 /* remove duplicates */
507 $sorted_lsub_ary = array_unique($sorted_lsub_ary);
509 /* natural sort mailboxes */
510 if (isset($sorted_lsub_ary)) {
511 usort($sorted_lsub_ary, 'user_strcasecmp');
514 * The LSUB response doesn't provide us information about \Noselect
515 * mail boxes. The LIST response does, that's why we need to do a LIST
516 * call to retrieve the flags for the mailbox
517 * Note: according RFC2060 an imap server may provide \NoSelect flags in the LSUB response.
518 * in other words, we cannot rely on it.
520 $sorted_list_ary = array();
521 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
522 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
523 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
526 $mbx = $sorted_lsub_ary[$i];
529 $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
530 true, $response, $message);
532 /* Another workaround for literals */
534 if (isset($read[1]) && substr($read[1],-3) == "}\r\n") {
535 if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
537 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) . '"' . $regs[2];
541 if (isset($read[0])) {
542 $sorted_list_ary[$i] = $read[0];
544 $sorted_list_ary[$i] = '';
549 * Just in case they're not subscribed to their inbox,
550 * we'll get it for them anyway
552 if (!$inbox_subscribed) {
553 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
554 true, $response, $message);
555 /* Another workaround for literals */
556 if (isset($inbox_ary[1]) && substr($inbox_ary[0],-3) == "}\r\n") {
557 if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
558 $inbox_ary[0], $regs)) {
559 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
563 $sorted_list_ary[] = $inbox_ary[0];
564 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
567 $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
569 /* Now, lets sort for special folders */
570 $boxesnew = $used = array();
573 $cnt = count($boxesall);
574 $used = array_pad($used,$cnt,false);
575 for($k = 0; $k < $cnt; ++
$k) {
576 if (strtolower($boxesall[$k]['unformatted']) == 'inbox') {
577 $boxesnew[] = $boxesall[$k];
582 /* List special folders and their subfolders, if requested. */
583 if ($list_special_folders_first) {
584 for($k = 0; $k < $cnt; ++
$k) {
585 if (!$used[$k] && isSpecialMailbox($boxesall[$k]['unformatted'])) {
586 $boxesnew[] = $boxesall[$k];
592 /* Rest of the folders */
593 for($k = 0; $k < $cnt; $k++
) {
595 $boxesnew[] = $boxesall[$k];
603 * Returns a list of all folders, subscribed or not
605 function sqimap_mailbox_list_all($imap_stream) {
606 global $list_special_folders_first, $folder_prefix, $delimiter;
608 $ssid = sqimap_session_id();
609 $lsid = strlen( $ssid );
610 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
611 $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message);
614 $fld_pre_length = strlen($folder_prefix);
616 for ($i = 0, $cnt = count($read_ary); $i < $cnt; $i++
) {
617 /* Another workaround for EIMS */
618 if (isset($read_ary[$i +
1]) &&
619 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
620 $read_ary[$i], $regs)) {
622 $read_ary[$i] = $regs[1] . '"' . addslashes(trim($read_ary[$i])) . '"' . $regs[2];
624 if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
625 /* Store the raw IMAP reply */
626 $boxes[$g]['raw'] = $read_ary[$i];
628 /* Count number of delimiters ($delimiter) in folder name */
629 $mailbox = find_mailbox_name($read_ary[$i]);
630 $dm_count = substr_count($mailbox, $delimiter);
631 if (substr($mailbox, -1) == $delimiter) {
632 /* If name ends in delimiter - decrement count by one */
636 /* Format folder name, but only if it's a INBOX.* or has a parent. */
637 $boxesallbyname[$mailbox] = $g;
638 $parentfolder = readMailboxParent($mailbox, $delimiter);
639 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
640 (ereg('^'.$folder_prefix, $mailbox)) ||
641 ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
643 $boxes[$g]['formatted'] = str_repeat(' ', $dm_count);
645 $boxes[$g]['formatted'] = '';
647 $boxes[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
649 $boxes[$g]['formatted'] = imap_utf7_decode_local($mailbox);
652 $boxes[$g]['unformatted-dm'] = $mailbox;
653 if (substr($mailbox, -1) == $delimiter) {
654 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
656 $boxes[$g]['unformatted'] = $mailbox;
657 $boxes[$g]['unformatted-disp'] = substr($mailbox,$fld_pre_length);
659 $boxes[$g]['id'] = $g;
661 /* Now lets get the flags for this mailbox */
662 $read_mlbx = $read_ary[$i];
664 // $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
665 // true, $response, $message);
667 /* Another workaround for EIMS */
668 // if (isset($read_mlbx[1]) &&
669 // ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", $read_mlbx[0], $regs)) {
670 // $read_mlbx[0] = $regs[1] . '"' . addslashes(trim($read_mlbx[1])) . '"' . $regs[2];
672 // echo $read_mlbx[0] .' raw 2 <br>';
674 $flags = substr($read_mlbx, strpos($read_mlbx, '(')+
1);
675 $flags = substr($flags, 0, strpos($flags, ')'));
676 $flags = str_replace('\\', '', $flags);
677 $flags = trim(strtolower($flags));
679 $boxes[$g]['flags'] = explode(' ', $flags);
681 $boxes[$g]['flags'] = array();
686 if(is_array($boxes)) {
693 function sqimap_mailbox_tree($imap_stream) {
694 global $boxesnew, $default_folder_prefix, $unseen_notify, $unseen_type;
695 if (!isset($boxesnew)) {
697 global $data_dir, $username, $list_special_folders_first,
698 $folder_prefix, $delimiter, $trash_folder, $move_to_trash;
701 $inbox_in_list = false;
702 $inbox_subscribed = false;
704 require_once(SM_PATH
. 'include/load_prefs.php');
707 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
708 true, $response, $message);
711 * Section about removing the last element was removed
712 * We don't return "* OK" anymore from sqimap_read_data
714 $sorted_lsub_ary = array();
715 $cnt = count($lsub_ary);
716 for ($i = 0; $i < $cnt; $i++
) {
718 * Workaround for EIMS
719 * Doesn't work if the mailbox name is multiple lines
721 if (isset($lsub_ary[$i +
1]) &&
722 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
723 $lsub_ary[$i], $regs)) {
725 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
729 if (preg_match("/^\*\s+LSUB\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$lsub_ary[$i],$regs)) {
731 $mbx = trim($regs[3]);
732 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
735 $mbx = find_mailbox_name($lsub_ary[$i]);
736 $noselect = check_is_noselect($lsub_ary[$i]);
737 if (substr($mbx, -1) == $delimiter) {
738 $mbx = substr($mbx, 0, strlen($mbx) - 1);
740 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect);
742 array_multisort($sorted_lsub_ary, SORT_ASC
, SORT_REGULAR
);
744 for($i = 0; $i < $cnt; $i++
) {
745 if ($sorted_lsub_ary[$i]['mbx'] == 'INBOX') {
746 $inbox_in_list = true;
752 * Just in case they're not subscribed to their inbox,
753 * we'll get it for them anyway
755 if (!$inbox_in_list) {
756 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
757 true, $response, $message);
758 /* Another workaround for EIMS */
759 if (isset($inbox_ary[1]) &&
760 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
761 $inbox_ary[0], $regs)) {
762 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
765 $mbx = find_mailbox_name($inbox_ary[0]);
766 if (substr($mbx, -1) == $delimiter) {
767 $mbx = substr($mbx, 0, strlen($mbx) - 1);
769 if ($mbx == 'INBOX') {
770 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => '');
771 sqimap_subscribe($imap_stream, 'INBOX');
776 if (preg_match("/^\*\s+LIST\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$inbox_ary[0],$regs)) {
778 $mbx = trim($regs[3]);
779 if (substr($mbx, -1) == $delimiter) {
780 $mbx = substr($mbx, 0, strlen($mbx) - 1);
782 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
786 for ($i = 0 ; $i < $cnt; $i++
) {
787 $mbx = $sorted_lsub_ary[$i]['mbx'];
788 if (($unseen_notify == 2 && $mbx == 'INBOX') ||
789 ($unseen_notify == 3) ||
790 ($move_to_trash && ($mbx == $trash_folder))) {
791 if($sorted_lsub_ary[$i]['noselect']) {
792 $sorted_lsub_ary[$i]['unseen'] = 0;
794 $sorted_lsub_ary[$i]['unseen'] =
795 sqimap_unseen_messages($imap_stream, $mbx);
797 if (($unseen_type == 2) ||
798 ($move_to_trash && ($mbx == $trash_folder)) ||
799 ($mbx == $trash_folder)) {
800 if($sorted_lsub_ary[$i]['noselect']) {
801 $sorted_lsub_ary[$i]['nummessages'] = 0;
803 $sorted_lsub_ary[$i]['nummessages'] =
804 sqimap_get_num_messages($imap_stream, $mbx);
809 $boxesnew = sqimap_fill_mailbox_tree($sorted_lsub_ary);
815 function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false) {
816 global $data_dir, $username, $list_special_folders_first,
817 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
818 $move_to_trash, $move_to_sent, $save_as_draft,
821 $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
823 /* create virtual root node */
824 $mailboxes= new mailboxes();
825 $mailboxes->is_root
= true;
827 if (isset($folder_prefix) && $folder_prefix != '') {
828 $start = substr_count($folder_prefix,$delimiter);
829 if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
831 $mailboxes->mailboxname_full
= substr($folder_prefix,0, (strlen($folder_prefix)-1));
833 $mailboxes->mailboxname_full
= $folder_prefix;
836 $mailboxes->mailboxname_sub
= $mailboxes->mailboxname_full
;
840 $cnt = count($mbx_ary);
841 for ($i=0; $i < $cnt; $i++
) {
842 if ($mbx_ary[$i]['mbx'] !='' ) {
843 $mbx = new mailboxes();
844 $mailbox = $mbx_ary[$i]['mbx'];
847 $mbx->is_inbox
= true;
848 $mbx->is_special
= true;
851 $mbx->is_trash
= true;
852 $mbx->is_special
= true;
855 $mbx->is_sent
= true;
856 $mbx->is_special
= true;
859 $mbx->is_draft
= true;
860 $mbx->is_special
= true;
864 if (isset($mbx_ary[$i]['unseen'])) {
865 $mbx->unseen
= $mbx_ary[$i]['unseen'];
867 if (isset($mbx_ary[$i]['nummessages'])) {
868 $mbx->total
= $mbx_ary[$i]['nummessages'];
871 $mbx->is_noselect
= $mbx_ary[$i]['noselect'];
873 $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
875 $mbx->mailboxname_sub
= substr($mbx_ary[$i]['mbx'],$r_del_pos+
1);
876 } else { /* mailbox is root folder */
877 $mbx->mailboxname_sub
= $mbx_ary[$i]['mbx'];
879 $mbx->mailboxname_full
= $mbx_ary[$i]['mbx'];
880 $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);