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