d29aac0e |
1 | <? |
2 | /** |
3 | ** imap_mailbox.php |
4 | ** |
5 | ** This impliments all functions that manipulate mailboxes |
6 | **/ |
7 | |
d29aac0e |
8 | /****************************************************************************** |
9 | ** Expunges a mailbox |
10 | ******************************************************************************/ |
11 | function sqimap_mailbox_expunge ($imap_stream, $mailbox) { |
12 | sqimap_mailbox_select ($imap_stream, $mailbox); |
2c898a11 |
13 | fputs ($imap_stream, "a001 EXPUNGE\r\n"); |
d29aac0e |
14 | $read = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
15 | } |
16 | |
17 | |
18 | /****************************************************************************** |
19 | ** Checks whether or not the specified mailbox exists |
20 | ******************************************************************************/ |
21 | function sqimap_mailbox_exists ($imap_stream, $mailbox) { |
22 | $boxes = sqimap_mailbox_list ($imap_stream); |
23 | $found = false; |
24 | for ($i = 0; $i < count ($boxes); $i++) { |
25 | if ($boxes[$i]["unformatted"] == $mailbox) |
26 | $found = true; |
27 | } |
28 | return $found; |
29 | } |
30 | |
31 | |
32 | |
33 | /****************************************************************************** |
34 | ** Selects a mailbox |
35 | ******************************************************************************/ |
36 | function sqimap_mailbox_select ($imap_stream, $mailbox) { |
2c898a11 |
37 | fputs ($imap_stream, "a001 SELECT \"$mailbox\"\r\n"); |
d29aac0e |
38 | $read = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
39 | } |
40 | |
41 | |
42 | |
43 | /****************************************************************************** |
44 | ** Creates a folder |
45 | ******************************************************************************/ |
46 | function sqimap_mailbox_create ($imap_stream, $mailbox, $type) { |
47 | if (strtolower($type) == "noselect") { |
48 | $dm = sqimap_get_delimiter($imap_stream); |
49 | $mailbox = $mailbox.$dm; |
50 | } |
2c898a11 |
51 | fputs ($imap_stream, "a001 CREATE \"$mailbox\"\r\n"); |
d29aac0e |
52 | $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
53 | |
54 | sqimap_subscribe ($imap_stream, $mailbox); |
55 | } |
56 | |
57 | |
58 | |
59 | /****************************************************************************** |
60 | ** Subscribes to an existing folder |
61 | ******************************************************************************/ |
62 | function sqimap_subscribe ($imap_stream, $mailbox) { |
2c898a11 |
63 | fputs ($imap_stream, "a001 SUBSCRIBE \"$mailbox\"\r\n"); |
d29aac0e |
64 | $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
65 | } |
66 | |
67 | |
68 | |
69 | |
70 | /****************************************************************************** |
71 | ** Unsubscribes to an existing folder |
72 | ******************************************************************************/ |
73 | function sqimap_unsubscribe ($imap_stream, $mailbox) { |
2c898a11 |
74 | fputs ($imap_stream, "a001 UNSUBSCRIBE \"$mailbox\"\r\n"); |
d29aac0e |
75 | $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
76 | } |
77 | |
78 | |
79 | |
80 | |
81 | /****************************************************************************** |
4df5cf20 |
82 | ** This function simply deletes the given folder |
d29aac0e |
83 | ******************************************************************************/ |
84 | function sqimap_mailbox_delete ($imap_stream, $mailbox) { |
2c898a11 |
85 | fputs ($imap_stream, "a001 DELETE \"$mailbox\"\r\n"); |
d29aac0e |
86 | $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
abddc974 |
87 | sqimap_unsubscribe ($imap_stream, $mailbox); |
d29aac0e |
88 | } |
89 | |
90 | |
91 | |
92 | /****************************************************************************** |
93 | ** Returns sorted mailbox lists in several different ways. |
94 | ** The array returned looks like this: |
95 | ******************************************************************************/ |
96 | function sqimap_mailbox_list ($imap_stream) { |
4ca45d7b |
97 | global $special_folders, $list_special_folders_first; |
98 | |
d29aac0e |
99 | if (!function_exists ("ary_sort")) |
100 | include ("../functions/array.php"); |
101 | |
102 | $dm = sqimap_get_delimiter ($imap_stream); |
103 | |
2c898a11 |
104 | fputs ($imap_stream, "a001 LIST \"\" INBOX\r\n"); |
d29aac0e |
105 | $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
106 | $g = 0; |
107 | $phase = "inbox"; |
108 | for ($i = 0; $i < count($read_ary); $i++) { |
109 | if (substr ($read_ary[$i], 0, 4) != "a001") { |
110 | $boxes[$g]["raw"] = $read_ary[$i]; |
111 | |
112 | $mailbox = find_mailbox_name($read_ary[$i]); |
113 | $dm_count = countCharInString($mailbox, $dm); |
114 | if (substr($mailbox, -1) == $dm) |
115 | $dm_count--; |
116 | |
117 | for ($j = 0; $j < $dm_count; $j++) |
118 | $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " "; |
119 | $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm); |
120 | |
abddc974 |
121 | $boxes[$g]["unformatted-dm"] = $mailbox; |
d29aac0e |
122 | if (substr($mailbox, -1) == $dm) |
123 | $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); |
124 | $boxes[$g]["unformatted"] = $mailbox; |
125 | $boxes[$g]["id"] = $g; |
126 | |
127 | /** Now lets get the flags for this mailbox **/ |
2c898a11 |
128 | fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n"); |
d29aac0e |
129 | $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message); |
130 | |
131 | $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1); |
132 | $flags = substr($flags, 0, strpos($flags, ")")); |
133 | $flags = str_replace("\\", "", $flags); |
134 | $flags = trim(strtolower($flags)); |
135 | if ($flags) { |
136 | $boxes[$g]["flags"] = explode(" ", $flags); |
137 | } |
138 | } |
139 | $g++; |
140 | |
141 | if (!$read_ary[$i+1]) { |
142 | if ($phase == "inbox") { |
2c898a11 |
143 | fputs ($imap_stream, "a001 LSUB \"\" *\r\n"); |
d29aac0e |
144 | $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
145 | $phase = "lsub"; |
146 | $i--; |
147 | } |
148 | } |
149 | } |
150 | |
151 | $original = $boxes; |
4ca45d7b |
152 | |
153 | /** Get the folders into lower case so sorting is not case sensative */ |
154 | for ($i = 0; $i < count($original); $i++) { |
155 | $boxes[$i]["unformatted"] = strtolower($boxes[$i]["unformatted"]); |
156 | } |
157 | |
158 | /** Sort them **/ |
d29aac0e |
159 | $boxes = ary_sort($boxes, "unformatted", 1); |
4ca45d7b |
160 | |
161 | /** Get them back from the original array, still sorted by the id **/ |
d29aac0e |
162 | for ($i = 0; $i < count($boxes); $i++) { |
163 | for ($j = 0; $j < count($original); $j++) { |
164 | if ($boxes[$i]["id"] == $original[$j]["id"]) { |
165 | $boxes[$i] = $original[$j]; |
166 | } |
167 | } |
168 | } |
169 | |
4ca45d7b |
170 | |
d29aac0e |
171 | for ($i = 0; $i < count($boxes); $i++) { |
172 | if ($boxes[$i]["unformatted"] == $special_folders[0]) { |
173 | $boxesnew[0] = $boxes[$i]; |
4ca45d7b |
174 | $boxes[$i]["used"] = true; |
d29aac0e |
175 | } |
176 | } |
4ca45d7b |
177 | |
d29aac0e |
178 | if ($list_special_folders_first == true) { |
179 | for ($i = 0; $i < count($boxes); $i++) { |
180 | for ($j = 1; $j < count($special_folders); $j++) { |
181 | if (substr($boxes[$i]["unformatted"], 0, strlen($special_folders[$j])) == $special_folders[$j]) { |
182 | $pos = count($boxesnew); |
183 | $boxesnew[$pos] = $boxes[$i]; |
184 | $boxes[$i]["used"] = true; |
185 | } |
186 | } |
187 | } |
188 | } |
4ca45d7b |
189 | |
d29aac0e |
190 | for ($i = 0; $i < count($boxes); $i++) { |
191 | if (($boxes[$i]["unformatted"] != $special_folders[0]) && |
192 | ($boxes[$i]["used"] == false)) { |
193 | $pos = count($boxesnew); |
194 | $boxesnew[$pos] = $boxes[$i]; |
195 | $boxes[$i]["used"] = true; |
196 | } |
197 | } |
198 | |
4ca45d7b |
199 | return $boxesnew; |
d29aac0e |
200 | } |
37578167 |
201 | |
202 | |
203 | /****************************************************************************** |
204 | ** Returns a list of all folders, subscribed or not |
205 | ******************************************************************************/ |
206 | function sqimap_mailbox_list_all ($imap_stream) { |
207 | global $special_folders, $list_special_folders_first; |
208 | |
209 | if (!function_exists ("ary_sort")) |
210 | include ("../functions/array.php"); |
211 | |
212 | $dm = sqimap_get_delimiter ($imap_stream); |
213 | |
81a897dc |
214 | fputs ($imap_stream, "a001 LIST \"INBOX\" *\r\n"); |
37578167 |
215 | $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
216 | $g = 0; |
217 | $phase = "inbox"; |
218 | for ($i = 0; $i < count($read_ary); $i++) { |
219 | if (substr ($read_ary[$i], 0, 4) != "a001") { |
220 | $boxes[$g]["raw"] = $read_ary[$i]; |
221 | |
222 | $mailbox = find_mailbox_name($read_ary[$i]); |
223 | $dm_count = countCharInString($mailbox, $dm); |
224 | if (substr($mailbox, -1) == $dm) |
225 | $dm_count--; |
226 | |
227 | for ($j = 0; $j < $dm_count; $j++) |
228 | $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " "; |
229 | $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm); |
230 | |
231 | $boxes[$g]["unformatted-dm"] = $mailbox; |
232 | if (substr($mailbox, -1) == $dm) |
233 | $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); |
234 | $boxes[$g]["unformatted"] = $mailbox; |
235 | $boxes[$g]["id"] = $g; |
236 | |
237 | /** Now lets get the flags for this mailbox **/ |
238 | fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n"); |
239 | $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message); |
240 | |
241 | $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1); |
242 | $flags = substr($flags, 0, strpos($flags, ")")); |
243 | $flags = str_replace("\\", "", $flags); |
244 | $flags = trim(strtolower($flags)); |
245 | if ($flags) { |
246 | $boxes[$g]["flags"] = explode(" ", $flags); |
247 | } |
248 | } |
249 | $g++; |
250 | } |
251 | $boxes = ary_sort ($boxes, "unformatted", 1); |
252 | return $boxes; |
253 | } |
d29aac0e |
254 | |
4ca45d7b |
255 | ?> |