+/**
+ * Mailboxes class
+ *
+ * FIXME. This class should be extracted and placed in a separate file that
+ * can be included before we start the session. That makes caching of the tree
+ * possible. On a refresh mailboxes from left_main.php the only function that
+ * should be called is the sqimap_get_status_mbx_tree. In case of subscribe
+ * / rename / delete / new we have to create methods for adding/changing the
+ * mailbox in the mbx_tree without the need for a refresh.
+ * @package squirrelmail
+*/
+
+class mailboxes {
+ var $mailboxname_full = '', $mailboxname_sub= '', $is_noselect = false, $is_noinferiors = false,
+ $is_special = false, $is_root = false, $is_inbox = false, $is_sent = false,
+ $is_trash = false, $is_draft = false, $mbxs = array(),
+ $unseen = false, $total = false;
+
+ function addMbx($mbx, $delimiter, $start, $specialfirst) {
+ $ary = explode($delimiter, $mbx->mailboxname_full);
+ $mbx_parent =& $this;
+ for ($i = $start, $c = count($ary)-1; $i < $c; $i++) {
+ $mbx_childs =& $mbx_parent->mbxs;
+ $found = false;
+ if ($mbx_childs) {
+ foreach ($mbx_childs as $key => $parent) {
+ if ($parent->mailboxname_sub == $ary[$i]) {
+ $mbx_parent =& $mbx_parent->mbxs[$key];
+ $found = true;
+ break;
+ }
+ }
+ }
+ if (!$found) {
+ $no_select_mbx = new mailboxes();
+ if (isset($mbx_parent->mailboxname_full) && $mbx_parent->mailboxname_full != '') {
+ $no_select_mbx->mailboxname_full = $mbx_parent->mailboxname_full.$delimiter.$ary[$i];
+ } else {
+ $no_select_mbx->mailboxname_full = $ary[$i];
+ }
+ $no_select_mbx->mailboxname_sub = $ary[$i];
+ $no_select_mbx->is_noselect = true;
+ $mbx_parent->mbxs[] = $no_select_mbx;
+ $i--;
+ }
+ }
+ $mbx_parent->mbxs[] = $mbx;
+ if ($mbx->is_special && $specialfirst) {
+ usort($mbx_parent->mbxs, 'sortSpecialMbx');
+ }
+ }
+}
+
+function sortSpecialMbx($a, $b) {
+ if ($a->is_inbox) {
+ $acmp = '0'. $a->mailboxname_full;
+ } else if ($a->is_special) {
+ $acmp = '1'. $a->mailboxname_full;
+ } else {
+ $acmp = '2' . $a->mailboxname_full;
+ }
+ if ($b->is_inbox) {
+ $bcmp = '0'. $b->mailboxname_full;
+ }else if ($b->is_special) {
+ $bcmp = '1' . $b->mailboxname_full;
+ } else {
+ $bcmp = '2' . $b->mailboxname_full;
+ }
+ return strnatcasecmp($acmp, $bcmp);
+}
+
+function compact_mailboxes_response($ary)
+{
+ /*
+ * Workaround for mailboxes returned as literal
+ * FIXME : Doesn't work if the mailbox name is multiple lines
+ * (larger then fgets buffer)
+ */
+ for ($i = 0, $iCnt=count($ary); $i < $iCnt; $i++) {
+ if (isset($ary[$i + 1]) && substr($ary[$i], -3) == "}\r\n") {
+ if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
+ $ary[$i], $regs)) {
+ $ary[$i] = $regs[1] . '"' . addslashes(trim($ary[$i+1])) . '"' . $regs[2];
+ array_splice($ary, $i+1, 2);
+ }
+ }
+ }
+ /* remove duplicates and ensure array is contiguous */
+ return array_values(array_unique($ary));
+}
+
+/**
+ * Extract the mailbox name from an untagged LIST (7.2.2) or LSUB (7.2.3) answer
+ * (LIST|LSUB) (<Flags list>) (NIL|"<separator atom>") <mailbox name string>\r\n
+ * mailbox name in quoted string MUST be unquoted and stripslashed (sm API)
+ */
+function find_mailbox_name($line)
+{
+ if (preg_match('/^\* (?:LIST|LSUB) \([^\)]*\) (?:NIL|\"[^\"]*\") ([^\r\n]*)[\r\n]*$/i', $line, $regs)) {
+ if (substr($regs[1], 0, 1) == '"')
+ return stripslashes(substr($regs[1], 1, -1));
+ return $regs[1];
+ }
+ return '';
+}
+
+/**
+ * @return bool whether this is a Noselect mailbox.
+ */
+function check_is_noselect ($lsub_line) {
+ return preg_match("/^\* (LSUB|LIST) \([^\)]*\\\\Noselect[^\)]*\)/i", $lsub_line);
+}
+
+/**
+ * @return bool whether this is a Noinferiors mailbox.
+ */
+function check_is_noinferiors ($lsub_line) {
+ return preg_match("/^\* (LSUB|LIST) \([^\)]*\\\\Noinferiors[^\)]*\)/i", $lsub_line);
+}
+