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