59177427 |
1 | <?php |
8e9e8afa |
2 | /** |
3 | ** imap_mailbox.php |
4 | ** |
5 | ** This impliments all functions that manipulate mailboxes |
245a6892 |
6 | ** |
7 | ** $Id$ |
8e9e8afa |
8 | **/ |
9 | |
10 | /****************************************************************************** |
11 | ** Expunges a mailbox |
12 | ******************************************************************************/ |
a7f3c40d |
13 | function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = true) { |
8e9e8afa |
14 | sqimap_mailbox_select ($imap_stream, $mailbox); |
15 | fputs ($imap_stream, "a001 EXPUNGE\r\n"); |
8cf653ad |
16 | $read = sqimap_read_data($imap_stream, "a001", $handle_errors, $response, $message); |
8e9e8afa |
17 | } |
18 | |
19 | |
20 | /****************************************************************************** |
21 | ** Checks whether or not the specified mailbox exists |
22 | ******************************************************************************/ |
23 | function sqimap_mailbox_exists ($imap_stream, $mailbox) { |
26511b3e |
24 | fputs ($imap_stream, "a001 LIST \"\" \"$mailbox\"\r\n"); |
25 | $mbx = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
44f642f5 |
26 | if ($mailbox) { |
f9b3e5d9 |
27 | return !!(ereg ("$mailbox", $mbx[0])); // To force into true/false |
8e9e8afa |
28 | } |
8e9e8afa |
29 | } |
30 | |
8e9e8afa |
31 | /****************************************************************************** |
32 | ** Selects a mailbox |
33 | ******************************************************************************/ |
04632dbc |
34 | function sqimap_mailbox_select ($imap_stream, $mailbox, $hide=true, $recent=false) { |
3751dc7f |
35 | global $auto_expunge; |
36 | |
8e9e8afa |
37 | fputs ($imap_stream, "a001 SELECT \"$mailbox\"\r\n"); |
2752bb16 |
38 | $read = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
04632dbc |
39 | if ($recent) { |
40 | for ($i=0; $i<count($read); $i++) { |
41 | if (strpos(strtolower($read[$i]), "recent")) { |
42 | $r = explode(" ", $read[$i]); |
43 | } |
44 | } |
45 | return $r[1]; |
46 | } |
3751dc7f |
47 | if ($auto_expunge) { |
48 | fputs ($imap_stream, "a001 EXPUNGE\r\n"); |
8cf653ad |
49 | $tmp = sqimap_read_data($imap_stream, "a001", false, $a, $b); |
3751dc7f |
50 | } |
8e9e8afa |
51 | } |
52 | |
53 | |
54 | |
55 | /****************************************************************************** |
56 | ** Creates a folder |
57 | ******************************************************************************/ |
58 | function sqimap_mailbox_create ($imap_stream, $mailbox, $type) { |
59 | if (strtolower($type) == "noselect") { |
60 | $dm = sqimap_get_delimiter($imap_stream); |
61 | $mailbox = $mailbox.$dm; |
62 | } |
63 | fputs ($imap_stream, "a001 CREATE \"$mailbox\"\r\n"); |
64 | $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
65 | |
66 | sqimap_subscribe ($imap_stream, $mailbox); |
67 | } |
68 | |
69 | |
70 | |
71 | /****************************************************************************** |
72 | ** Subscribes to an existing folder |
73 | ******************************************************************************/ |
74 | function sqimap_subscribe ($imap_stream, $mailbox) { |
75 | fputs ($imap_stream, "a001 SUBSCRIBE \"$mailbox\"\r\n"); |
76 | $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
77 | } |
78 | |
79 | |
80 | |
81 | |
82 | /****************************************************************************** |
83 | ** Unsubscribes to an existing folder |
84 | ******************************************************************************/ |
85 | function sqimap_unsubscribe ($imap_stream, $mailbox) { |
2752bb16 |
86 | global $imap_server_type; |
cbdc5621 |
87 | |
8e9e8afa |
88 | fputs ($imap_stream, "a001 UNSUBSCRIBE \"$mailbox\"\r\n"); |
89 | $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
90 | } |
91 | |
92 | |
93 | |
94 | |
95 | /****************************************************************************** |
96 | ** This function simply deletes the given folder |
97 | ******************************************************************************/ |
98 | function sqimap_mailbox_delete ($imap_stream, $mailbox) { |
99 | fputs ($imap_stream, "a001 DELETE \"$mailbox\"\r\n"); |
100 | $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
101 | sqimap_unsubscribe ($imap_stream, $mailbox); |
102 | } |
103 | |
104 | |
105 | /****************************************************************************** |
106 | ** Formats a mailbox into 4 parts for the $boxes array |
5bdd7223 |
107 | ** |
108 | ** The four parts are: |
109 | ** |
110 | ** raw - Raw LIST/LSUB response from the IMAP server |
111 | ** formatted - nicely formatted folder name |
112 | ** unformatted - unformatted, but with delimiter at end removed |
113 | ** unformatted-dm - folder name as it appears in raw response |
2752bb16 |
114 | ** unformatted-disp - unformatted without $folder_prefix |
5bdd7223 |
115 | ** |
8e9e8afa |
116 | ******************************************************************************/ |
16530f8b |
117 | function sqimap_mailbox_parse ($line, $line_lsub, $dm) { |
5bdd7223 |
118 | global $folder_prefix; |
119 | |
120 | // Process each folder line |
8e9e8afa |
121 | for ($g=0; $g < count($line); $g++) { |
5bdd7223 |
122 | |
2752bb16 |
123 | // Store the raw IMAP reply |
8e9e8afa |
124 | $boxes[$g]["raw"] = $line[$g]; |
5bdd7223 |
125 | |
2752bb16 |
126 | // Count number of delimiters ($dm) in folder name |
6477eb2d |
127 | $mailbox = trim($line_lsub[$g]); |
8e9e8afa |
128 | $dm_count = countCharInString($mailbox, $dm); |
129 | if (substr($mailbox, -1) == $dm) |
5bdd7223 |
130 | $dm_count--; // If name ends in delimiter - decrement count by one |
131 | |
2752bb16 |
132 | // Format folder name, but only if it's a INBOX.* or have |
133 | // a parent. |
134 | $boxesbyname[$mailbox] = $g; |
135 | $parentfolder = readMailboxParent($mailbox, $dm); |
136 | if((eregi("^inbox".quotemeta($dm), $mailbox)) || |
137 | (ereg("^".$folder_prefix, $mailbox)) || |
138 | ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) { |
139 | $indent = $dm_count - (countCharInString($folder_prefix, $dm)); |
0debfed6 |
140 | if ($indent) |
141 | $boxes[$g]["formatted"] = str_repeat(" ", $indent); |
142 | else |
143 | $boxes[$g]["formatted"] = ''; |
2752bb16 |
144 | $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm); |
145 | } else { |
146 | $boxes[$g]["formatted"] = $mailbox; |
147 | } |
8e9e8afa |
148 | |
149 | $boxes[$g]["unformatted-dm"] = $mailbox; |
150 | if (substr($mailbox, -1) == $dm) |
151 | $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); |
152 | $boxes[$g]["unformatted"] = $mailbox; |
2752bb16 |
153 | $boxes[$g]["unformatted-disp"] = ereg_replace("^" . $folder_prefix, "", $mailbox); |
8e9e8afa |
154 | $boxes[$g]["id"] = $g; |
155 | |
f9b3e5d9 |
156 | ereg("\(([^)]*)\)",$line[$g],$regs); |
157 | $flags = trim(strtolower(str_replace("\\", "",$regs[1]))); |
8e9e8afa |
158 | if ($flags) { |
159 | $boxes[$g]["flags"] = explode(" ", $flags); |
160 | } |
8e9e8afa |
161 | } |
5bdd7223 |
162 | |
8e9e8afa |
163 | return $boxes; |
164 | } |
10359c65 |
165 | |
5bdd7223 |
166 | /* patch from dave_michmerhuizen@yahoo.com |
167 | * allows case insensativity when sorting folders |
168 | */ |
169 | function _icmp ($a, $b) { |
170 | return strcasecmp($a, $b); |
171 | } |
8e9e8afa |
172 | |
173 | /****************************************************************************** |
174 | ** Returns sorted mailbox lists in several different ways. |
5bdd7223 |
175 | ** See comment on sqimap_mailbox_parse() for info about the returned array. |
8e9e8afa |
176 | ******************************************************************************/ |
177 | function sqimap_mailbox_list ($imap_stream) { |
5bdd7223 |
178 | global $load_prefs_php, $prefs_php, $config_php; |
179 | global $data_dir, $username, $list_special_folders_first; |
6477eb2d |
180 | global $trash_folder, $sent_folder; |
f9b3e5d9 |
181 | global $move_to_trash, $move_to_sent; |
8e9e8afa |
182 | |
1590a668 |
183 | $inbox_in_list = false; |
184 | $inbox_subscribed = false; |
185 | |
8e9e8afa |
186 | if (!isset($load_prefs_php)) include "../src/load_prefs.php"; |
187 | else global $folder_prefix; |
188 | if (!function_exists ("ary_sort")) include "../functions/array.php"; |
189 | |
190 | $dm = sqimap_get_delimiter ($imap_stream); |
191 | |
8e9e8afa |
192 | /** LSUB array **/ |
193 | $inbox_subscribed = false; |
6477eb2d |
194 | fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n"); |
8e9e8afa |
195 | $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
6477eb2d |
196 | |
a3db804c |
197 | /** OS: we don't want to parse last element of array, 'cause it is OK command, so we unset it **/ |
198 | unset($lsub_ary[count($lsub_ary)-1]); |
199 | |
8e9e8afa |
200 | for ($i=0;$i < count($lsub_ary); $i++) { |
201 | $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]); |
8e9e8afa |
202 | if ($sorted_lsub_ary[$i] == "INBOX") |
203 | $inbox_subscribed = true; |
204 | } |
ee62ce13 |
205 | $new_ary = array(); |
206 | for ($i=0; $i < count($sorted_lsub_ary); $i++) { |
207 | if (!in_array($sorted_lsub_ary[$i], $new_ary)) { |
208 | $new_ary[] = $sorted_lsub_ary[$i]; |
209 | } |
210 | } |
211 | $sorted_lsub_ary = $new_ary; |
8e9e8afa |
212 | if (isset($sorted_lsub_ary)) { |
2752bb16 |
213 | usort($sorted_lsub_ary, "_icmp"); |
10359c65 |
214 | //sort($sorted_lsub_ary); |
8e9e8afa |
215 | } |
216 | |
3cb866d7 |
217 | /** LIST array **/ |
f9b3e5d9 |
218 | for ($i=0; $i < count($sorted_lsub_ary); $i++) { |
219 | if (substr($sorted_lsub_ary[$i], -1) == $dm) |
220 | $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1); |
221 | else |
222 | $mbx = $sorted_lsub_ary[$i]; |
223 | |
224 | fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n"); |
225 | $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
226 | $sorted_list_ary[$i] = $read[0]; |
3cb866d7 |
227 | if (find_mailbox_name($sorted_list_ary[$i]) == "INBOX") |
228 | $inbox_in_list = true; |
f9b3e5d9 |
229 | } |
2752bb16 |
230 | |
8e9e8afa |
231 | /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/ |
232 | if ($inbox_subscribed == false || $inbox_in_list == false) { |
233 | fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n"); |
234 | $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
591000ec |
235 | |
236 | $pos = count($sorted_list_ary); |
237 | $sorted_list_ary[$pos] = $inbox_ary[0]; |
238 | |
239 | $pos = count($sorted_lsub_ary); |
240 | $sorted_lsub_ary[$pos] = find_mailbox_name($inbox_ary[0]); |
8e9e8afa |
241 | } |
242 | |
f9b3e5d9 |
243 | $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm); |
244 | |
5bdd7223 |
245 | |
f9b3e5d9 |
246 | /** Now, lets sort for special folders **/ |
5bdd7223 |
247 | |
248 | $boxesnew = Array(); |
249 | |
250 | // Find INBOX |
8e9e8afa |
251 | for ($i = 0; $i < count($boxes); $i++) { |
1e0628fb |
252 | if (strtolower($boxes[$i]["unformatted"]) == "inbox") { |
5bdd7223 |
253 | $boxesnew[] = $boxes[$i]; |
8e9e8afa |
254 | $boxes[$i]["used"] = true; |
2752bb16 |
255 | $i = count($boxes); |
8e9e8afa |
256 | } |
257 | } |
aed206bf |
258 | |
8e9e8afa |
259 | if ($list_special_folders_first == true) { |
5bdd7223 |
260 | |
2752bb16 |
261 | // Then list special folders and their subfolders |
245a6892 |
262 | for ($i = 0 ; $i < count($boxes) ; $i++) { |
23ed73eb |
263 | if ($move_to_trash && |
264 | eregi("^" . quotemeta($trash_folder) . "(" . |
265 | quotemeta($dm) . ".*)?$", $boxes[$i]["unformatted"])) { |
5bdd7223 |
266 | $boxesnew[] = $boxes[$i]; |
1e0628fb |
267 | $boxes[$i]["used"] = true; |
268 | } |
23ed73eb |
269 | elseif ($move_to_sent && |
270 | eregi("^" . quotemeta($sent_folder) . "(" . |
271 | quotemeta($dm) . ".*)?$", $boxes[$i]["unformatted"])) { |
5bdd7223 |
272 | $boxesnew[] = $boxes[$i]; |
1e0628fb |
273 | $boxes[$i]["used"] = true; |
8e9e8afa |
274 | } |
275 | } |
5bdd7223 |
276 | |
2752bb16 |
277 | // Put INBOX.* folders ahead of the rest |
245a6892 |
278 | for ($i = 0; $i < count($boxes); $i++) { |
2752bb16 |
279 | if (eregi("^inbox\.", $boxes[$i]["unformatted"]) && |
245a6892 |
280 | (!isset($boxes[$i]["used"]) || $boxes[$i]["used"] == false)) { |
2752bb16 |
281 | $boxesnew[] = $boxes[$i]; |
282 | $boxes[$i]["used"] = true; |
283 | } |
284 | } |
5bdd7223 |
285 | |
8e9e8afa |
286 | } |
c5eb2c03 |
287 | |
5bdd7223 |
288 | // Rest of the folders |
8e9e8afa |
289 | for ($i = 0; $i < count($boxes); $i++) { |
1e0628fb |
290 | if ((strtolower($boxes[$i]["unformatted"]) != "inbox") && |
8e9e8afa |
291 | ($boxes[$i]["used"] == false)) { |
5bdd7223 |
292 | $boxesnew[] = $boxes[$i]; |
8e9e8afa |
293 | $boxes[$i]["used"] = true; |
294 | } |
295 | } |
296 | |
297 | return $boxesnew; |
298 | } |
8e9e8afa |
299 | |
300 | /****************************************************************************** |
301 | ** Returns a list of all folders, subscribed or not |
302 | ******************************************************************************/ |
303 | function sqimap_mailbox_list_all ($imap_stream) { |
1e0628fb |
304 | global $list_special_folders_first, $folder_prefix; |
8e9e8afa |
305 | |
306 | if (!function_exists ("ary_sort")) |
307 | include ("../functions/array.php"); |
308 | |
309 | $dm = sqimap_get_delimiter ($imap_stream); |
310 | |
311 | fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n"); |
312 | $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
313 | $g = 0; |
314 | $phase = "inbox"; |
5bdd7223 |
315 | |
8e9e8afa |
316 | for ($i = 0; $i < count($read_ary); $i++) { |
317 | if (substr ($read_ary[$i], 0, 4) != "a001") { |
5bdd7223 |
318 | |
2752bb16 |
319 | // Store the raw IMAP reply |
8e9e8afa |
320 | $boxes[$g]["raw"] = $read_ary[$i]; |
321 | |
2752bb16 |
322 | // Count number of delimiters ($dm) in folder name |
8e9e8afa |
323 | $mailbox = find_mailbox_name($read_ary[$i]); |
324 | $dm_count = countCharInString($mailbox, $dm); |
325 | if (substr($mailbox, -1) == $dm) |
5bdd7223 |
326 | $dm_count--; // If name ends in delimiter - decrement count by one |
2752bb16 |
327 | |
328 | // Format folder name, but only if it's a INBOX.* or have |
329 | // a parent. |
330 | $boxesbyname[$mailbox] = $g; |
331 | $parentfolder = readMailboxParent($mailbox, $dm); |
332 | if((eregi("^inbox".quotemeta($dm), $mailbox)) || |
333 | (ereg("^".$folder_prefix, $mailbox)) || |
334 | ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) { |
0debfed6 |
335 | if ($dm_count) |
336 | $boxes[$g]["formatted"] = str_repeat(" ", $dm_count); |
337 | else |
338 | $boxes[$g]["formatted"] = ''; |
2752bb16 |
339 | $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm); |
340 | } else { |
341 | $boxes[$g]["formatted"] = $mailbox; |
342 | } |
8e9e8afa |
343 | |
344 | $boxes[$g]["unformatted-dm"] = $mailbox; |
345 | if (substr($mailbox, -1) == $dm) |
346 | $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); |
347 | $boxes[$g]["unformatted"] = $mailbox; |
2752bb16 |
348 | $boxes[$g]["unformatted-disp"] = ereg_replace("^" . $folder_prefix, "", $mailbox); |
8e9e8afa |
349 | $boxes[$g]["id"] = $g; |
350 | |
351 | /** Now lets get the flags for this mailbox **/ |
352 | fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n"); |
353 | $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message); |
354 | |
355 | $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1); |
356 | $flags = substr($flags, 0, strpos($flags, ")")); |
357 | $flags = str_replace("\\", "", $flags); |
358 | $flags = trim(strtolower($flags)); |
359 | if ($flags) { |
360 | $boxes[$g]["flags"] = explode(" ", $flags); |
361 | } |
362 | } |
363 | $g++; |
364 | } |
5bdd7223 |
365 | if(is_array($boxes)) { |
591000ec |
366 | $boxes = ary_sort ($boxes, "unformatted", 1); |
367 | } |
5bdd7223 |
368 | |
8e9e8afa |
369 | return $boxes; |
370 | } |
371 | |
052e0c26 |
372 | ?> |