/
Imap.php
184 lines (158 loc) · 5.68 KB
/
Imap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* Copyright 2013 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.horde.org/licenses/gpl.
*
* @category Horde
* @copyright 2013 Horde LLC
* @license http://www.horde.org/licenses/gpl GPL
* @package IMP
*/
/**
* Implementation of the account object for an IMAP server.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2013 Horde LLC
* @license http://www.horde.org/licenses/gpl GPL
* @package IMP
*
* @property-read IMP_Imap $imp_imap IMP IMAP object.
*/
class IMP_Ftree_Account_Imap extends IMP_Ftree_Account
{
/**
*/
public function __get($name)
{
switch ($name) {
case 'imp_imap':
return $GLOBALS['injector']->getInstance('IMP_Factory_Imap')->create($this->_id == IMP_Ftree::BASE_ELT ? null : $this->_id);
}
}
/**
*/
public function getList($query = null)
{
global $prefs;
$imp_imap = $this->imp_imap;
$out = $searches = array();
$unsub = false;
if (is_integer($query)) {
$ns = $imp_imap->getNamespaceList();
if ($query & self::INIT) {
/* Add namespace elements. */
if ($prefs->getValue('tree_view')) {
foreach ($ns as $val) {
$type = null;
switch ($val['type']) {
case Horde_Imap_Client::NS_OTHER:
$type = IMP_Ftree::OTHER_KEY;
break;
case Horde_Imap_Client::NS_SHARED:
$type = IMP_Ftree::SHARED_KEY;
break;
}
if (!is_null($type)) {
$out[$type] = array(
'a' => IMP_Ftree::ELT_NOSELECT | IMP_Ftree::ELT_NAMESPACE | IMP_Ftree::ELT_NONIMAP,
'v' => $type
);
}
}
}
$searches[] = 'INBOX';
} elseif ($query & self::UNSUB) {
$unsub = true;
}
foreach (array_keys($ns) as $val) {
$searches[] = $val . '*';
}
} else {
$searches[] = $query;
$unsub = true;
}
$res = $imp_imap->listMailboxes($searches, $unsub ? Horde_Imap_Client::MBOX_UNSUBSCRIBED : Horde_Imap_Client::MBOX_SUBSCRIBED_EXISTS, array(
'attributes' => true,
'delimiter' => true,
'sort' => true
));
foreach ($res as $val) {
if (in_array('\nonexistent', $val['attributes'])) {
continue;
}
$mbox = strval($val['mailbox']);
$ns_info = $imp_imap->getNamespace($mbox);
$parent = null;
/* Break apart the name via the delimiter and go step by
* step through the name to make sure all subfolders exist
* in the tree. */
if (strlen($val['delimiter'])) {
/* Strip personal namespace. */
if (!empty($ns_info['name']) &&
(strpos($mbox, $ns_info['name']) === 0)) {
$parts = explode($val['delimiter'], substr($mbox, strlen($ns_info['name'])));
$parts[0] = $ns_info['name'] . $parts[0];
} else {
$parts = explode($val['delimiter'], $mbox);
}
switch ($ns_info['type']) {
case Horde_Imap_Client::NS_OTHER:
case Horde_Imap_Client::NS_SHARED:
if ($prefs->getValue('tree_view')) {
$parent = $ns_info['type']
? IMP_Ftree::OTHER_KEY
: IMP_Ftree::SHARED_KEY;
}
break;
}
} else {
$parts = array($mbox);
}
for ($i = 1, $p_count = count($parts); $i <= $p_count; ++$i) {
$part = implode($val['delimiter'], array_slice($parts, 0, $i));
if (!isset($out[$part])) {
if ($p_count == $i) {
$attr = 0;
if (!$unsub ||
in_array('\subscribed', $val['attributes'])) {
$attr |= IMP_Ftree::ELT_IS_SUBSCRIBED;
}
if (in_array('\noselect', $val['attributes'])) {
$attr |= IMP_Ftree::ELT_NOSELECT;
}
if (in_array('\noinferiors', $val['attributes'])) {
$attr |= IMP_Ftree::ELT_NOINFERIORS;
}
} else {
$attr = IMP_Ftree::ELT_NOSELECT;
}
$out[$part] = array(
'a' => $attr,
'v' => $part
);
if (!is_null($parent)) {
$out[$part]['p'] = $parent;
}
}
$parent = $part;
}
}
if (is_integer($query) &&
($query & self::INIT) &&
($query & self::UNSUB)) {
$out = array_merge($out, $this->getList(self::UNSUB));
}
return $out;
}
/**
*/
public function delete(IMP_Ftree_Element $elt)
{
return ($elt->inbox || $elt->namespace)
? 0
: self::DELETE_ELEMENT;
}
}