1: <?php
  2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12: 
 13: 
 14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24: 
 25: class IMP_Ftree_Account_Imap extends IMP_Ftree_Account
 26: {
 27:     
 28:     const OTHER_KEY = "other\0";
 29:     const SHARED_KEY = "shared\0";
 30: 
 31:      32: 
 33:     public function __get($name)
 34:     {
 35:         switch ($name) {
 36:         case 'imp_imap':
 37:             return $GLOBALS['injector']->getInstance('IMP_Factory_Imap')->create($this->_id == IMP_Ftree::BASE_ELT ? null : $this->_id);
 38:         }
 39:     }
 40: 
 41:      42: 
 43:     public function getList($query = array(), $mask = 0)
 44:     {
 45:         global $prefs;
 46: 
 47:         $imp_imap = $this->imp_imap;
 48:         $ns = $imp_imap->getNamespaces();
 49:         $out = array();
 50: 
 51:         if ($mask & self::INIT) {
 52:             
 53:             if ($prefs->getValue('tree_view')) {
 54:                 foreach ($ns as $val) {
 55:                     $type = null;
 56: 
 57:                     switch ($val->type) {
 58:                     case $val::NS_OTHER:
 59:                         $attr = IMP_Ftree::ELT_NAMESPACE_OTHER;
 60:                         $type = self::OTHER_KEY;
 61:                         break;
 62: 
 63:                     case $val::NS_SHARED:
 64:                         $attr = IMP_Ftree::ELT_NAMESPACE_SHARED;
 65:                         $type = self::SHARED_KEY;
 66:                         break;
 67:                     }
 68: 
 69:                     if (!is_null($type)) {
 70:                         $out[$type] = array(
 71:                             'a' => $attr | IMP_Ftree::ELT_NOSELECT | IMP_Ftree::ELT_NONIMAP,
 72:                             'v' => $type
 73:                         );
 74:                     }
 75:                 }
 76:             }
 77: 
 78:             $query = array('INBOX');
 79:             foreach ($ns as $val) {
 80:                 $query[] = $val . '*';
 81:             }
 82: 
 83:             $lmquery = ($mask & self::UNSUB)
 84:                 ? Horde_Imap_Client::MBOX_ALL_SUBSCRIBED
 85:                 : Horde_Imap_Client::MBOX_SUBSCRIBED_EXISTS;
 86:         } elseif ($mask & self::UNSUB) {
 87:             $lmquery = Horde_Imap_Client::MBOX_UNSUBSCRIBED;
 88:             $query = array();
 89:             foreach ($ns as $val) {
 90:                 $query[] = $val . '*';
 91:             }
 92:         } elseif (empty($query)) {
 93:             return $out;
 94:         } else {
 95:             $lmquery = Horde_Imap_Client::MBOX_ALL_SUBSCRIBED;
 96:         }
 97: 
 98:         $res = $imp_imap->listMailboxes($query, $lmquery, array(
 99:             'attributes' => true,
100:             'delimiter' => true,
101:             'sort' => true
102:         ));
103: 
104:         foreach ($res as $val) {
105:             if (in_array('\nonexistent', $val['attributes'])) {
106:                 continue;
107:             }
108: 
109:             $mbox = strval($val['mailbox']);
110:             $ns_info = $imp_imap->getNamespace($mbox);
111:             $parent = null;
112: 
113:             114: 115: 
116:             if ($ns_info && strlen($ns_info->delimiter)) {
117:                 
118:                 if ($ns_info->type === $ns_info::NS_PERSONAL) {
119:                     $stripped = $ns_info->stripNamespace($mbox);
120:                     $parts = explode($ns_info->delimiter, $stripped);
121:                     if ($stripped != $mbox) {
122:                         $parts[0] = $ns_info->name . $parts[0];
123:                     }
124:                 } else {
125:                     $parts = explode($ns_info->delimiter, $mbox);
126:                 }
127: 
128:                 if ($prefs->getValue('tree_view')) {
129:                     switch ($ns_info->type) {
130:                     case $ns_info::NS_OTHER:
131:                         $parent = self::OTHER_KEY;
132:                         break;
133: 
134:                     case $ns_info::NS_SHARED:
135:                         $parent = self::SHARED_KEY;
136:                         break;
137:                     }
138:                 }
139:             } else {
140:                 $parts = array($mbox);
141:             }
142: 
143:             for ($i = 1, $p_count = count($parts); $i <= $p_count; ++$i) {
144:                 $part = implode($val['delimiter'], array_slice($parts, 0, $i));
145: 
146:                 if (!isset($out[$part])) {
147:                     if ($p_count == $i) {
148:                         $attr = 0;
149: 
150:                         if (in_array('\subscribed', $val['attributes'])) {
151:                             $attr |= IMP_Ftree::ELT_IS_SUBSCRIBED;
152:                         }
153: 
154:                         if (in_array('\noselect', $val['attributes'])) {
155:                             $attr |= IMP_Ftree::ELT_NOSELECT;
156:                         }
157: 
158:                         if (in_array('\noinferiors', $val['attributes'])) {
159:                             $attr |= IMP_Ftree::ELT_NOINFERIORS;
160:                         }
161:                     } else {
162:                         $attr = IMP_Ftree::ELT_NOSELECT;
163:                     }
164: 
165:                     $out[$part] = array(
166:                         'a' => $attr,
167:                         'v' => $part
168:                     );
169:                     if (!is_null($parent)) {
170:                         $out[$part]['p'] = $parent;
171:                     }
172:                 }
173: 
174:                 $parent = $part;
175:             }
176:         }
177: 
178:         return $out;
179:     }
180: 
181:     182: 
183:     public function delete(IMP_Ftree_Element $elt)
184:     {
185:         return ($elt->inbox || $elt->namespace)
186:             ? 0
187:             : self::DELETE_ELEMENT;
188:     }
189: 
190: }
191: