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 | ******************************************************************************/ |
11 | function sqimap_mailbox_expunge ($imap_stream, $mailbox) { |
12 | sqimap_mailbox_select ($imap_stream, $mailbox); |
13 | fputs ($imap_stream, "a001 EXPUNGE\r\n"); |
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) { |
37 | fputs ($imap_stream, "a001 SELECT \"$mailbox\"\r\n"); |
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 | } |
51 | fputs ($imap_stream, "a001 CREATE \"$mailbox\"\r\n"); |
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) { |
63 | fputs ($imap_stream, "a001 SUBSCRIBE \"$mailbox\"\r\n"); |
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) { |
cbdc5621 |
74 | global $imap_server_type; |
75 | |
76 | /** This is a hack for UW server |
77 | ** Sometimes a folder will have a / at the end. If that's the case, |
78 | ** the unsubscribe doesn't work for a box named "mailbox/". We have |
79 | ** to strip off the / at the end. There may be a better way of doing |
80 | ** this, but this is the best I've found so far. (lme - April 26, 2000) |
81 | **/ |
82 | if ($imap_server_type == "uw") { |
83 | if (substr($mailbox, -1) == "/") { |
84 | $mailbox = substr($mailbox, 0, strlen($mailbox)-1); |
85 | } |
86 | } |
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 |
107 | ******************************************************************************/ |
108 | function sqimap_mailbox_parse ($line, $dm) { |
aed206bf |
109 | global $folder_prefix; |
8e9e8afa |
110 | for ($g=0; $g < count($line); $g++) { |
111 | $boxes[$g]["raw"] = $line[$g]; |
112 | |
113 | $mailbox = find_mailbox_name($line[$g]); |
114 | $dm_count = countCharInString($mailbox, $dm); |
115 | if (substr($mailbox, -1) == $dm) |
116 | $dm_count--; |
117 | |
aed206bf |
118 | for ($j = 0; $j < $dm_count - (countCharInString($folder_prefix, $dm)); $j++) |
8e9e8afa |
119 | $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " "; |
120 | $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm); |
121 | |
122 | $boxes[$g]["unformatted-dm"] = $mailbox; |
123 | if (substr($mailbox, -1) == $dm) |
124 | $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); |
125 | $boxes[$g]["unformatted"] = $mailbox; |
126 | $boxes[$g]["id"] = $g; |
127 | |
128 | $flags = substr($line[$g], strpos($line[$g], "(")+1); |
129 | $flags = substr($flags, 0, strpos($flags, ")")); |
130 | $flags = str_replace("\\", "", $flags); |
131 | $flags = trim(strtolower($flags)); |
132 | if ($flags) { |
133 | $boxes[$g]["flags"] = explode(" ", $flags); |
134 | } |
cbdc5621 |
135 | /**** I'm not sure why this was even in here to begin with.. (lme) |
cbd55894 |
136 | for ($i=0; $i < count($boxes[$g]["flags"]); $i++) { |
137 | if ($boxes[$g]["flags"][$i] == "noselect") { |
138 | $boxes[$g]["unformatted-dm"] = $boxes[$g]["unformatted-dm"].$dm; |
96ba6315 |
139 | // echo $boxes[$g]["unformatted-dm"]." - debug<br>"; |
cbd55894 |
140 | } |
141 | } |
cbdc5621 |
142 | ****/ |
8e9e8afa |
143 | } |
144 | return $boxes; |
145 | } |
146 | |
147 | /****************************************************************************** |
148 | ** Returns sorted mailbox lists in several different ways. |
149 | ** The array returned looks like this: |
150 | ******************************************************************************/ |
151 | function sqimap_mailbox_list ($imap_stream) { |
aed206bf |
152 | global $load_prefs_php, $prefs_php, $config_php, $data_dir, $username, $list_special_folders_first; |
1e0628fb |
153 | global $trash_folder, $sent_folder; |
154 | global $move_to_trash, $move_to_sent; |
8e9e8afa |
155 | |
1590a668 |
156 | $inbox_in_list = false; |
157 | $inbox_subscribed = false; |
158 | |
8e9e8afa |
159 | if (!isset($load_prefs_php)) include "../src/load_prefs.php"; |
160 | else global $folder_prefix; |
161 | if (!function_exists ("ary_sort")) include "../functions/array.php"; |
162 | |
163 | $dm = sqimap_get_delimiter ($imap_stream); |
164 | |
165 | /** LIST array **/ |
166 | fputs ($imap_stream, "a001 LIST \"\" \"$folder_prefix*\"\r\n"); |
167 | $list_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
aed206bf |
168 | |
8e9e8afa |
169 | for ($i=0;$i < count($list_ary); $i++) { |
170 | $sorted_list_ary[$i]["name"] = find_mailbox_name($list_ary[$i]); |
171 | $sorted_list_ary[$i]["raw"] = $list_ary[$i]; |
1590a668 |
172 | if ($sorted_list_ary[$i]["name"] == "INBOX") |
8e9e8afa |
173 | $inbox_in_list = true; |
174 | } |
175 | if (isset($sorted_list_ary)) { |
176 | $list_sorted = array_cleave ($sorted_list_ary, "name"); |
177 | asort($list_sorted); |
178 | } |
179 | |
8e9e8afa |
180 | |
181 | /** LSUB array **/ |
182 | $inbox_subscribed = false; |
183 | fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n"); |
184 | $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
185 | for ($i=0;$i < count($lsub_ary); $i++) { |
186 | $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]); |
187 | if (substr($sorted_lsub_ary[$i], -1) == $dm) |
188 | $sorted_lsub_ary[$i] = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1); |
189 | if ($sorted_lsub_ary[$i] == "INBOX") |
190 | $inbox_subscribed = true; |
191 | } |
192 | if (isset($sorted_lsub_ary)) { |
193 | sort($sorted_lsub_ary); |
194 | } |
195 | |
196 | |
197 | /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/ |
198 | if ($inbox_subscribed == false || $inbox_in_list == false) { |
199 | fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n"); |
200 | $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
201 | $merged[0] = $inbox_ary[0]; |
202 | $k = 1; |
203 | } else { |
204 | $k = 0; |
205 | } |
206 | |
207 | $i = $j = 0; |
208 | |
209 | if (isset($list_sorted) && isset($sorted_lsub_ary)) { |
210 | reset ($list_sorted); |
211 | for (reset($list_sorted); $key = key($list_sorted), isset($key);) { |
212 | if ($sorted_lsub_ary[$i] == $list_sorted[$key]) { |
213 | $merged[$k] = $sorted_list_ary[$key]["raw"]; |
214 | $k++; |
215 | $i++; |
216 | next($list_sorted); |
217 | } else if ($sorted_lsub_ary[$i] < $list_sorted[$key]) { |
218 | $i++; |
219 | } else { |
220 | next($list_sorted); |
221 | } |
222 | } |
223 | } |
224 | |
aed206bf |
225 | $boxes = sqimap_mailbox_parse ($merged, $dm); |
226 | |
227 | /** Now, lets sort for special folders **/ |
8e9e8afa |
228 | for ($i = 0; $i < count($boxes); $i++) { |
1e0628fb |
229 | if (strtolower($boxes[$i]["unformatted"]) == "inbox") { |
8e9e8afa |
230 | $boxesnew[0] = $boxes[$i]; |
231 | $boxes[$i]["used"] = true; |
232 | } |
233 | } |
aed206bf |
234 | |
8e9e8afa |
235 | if ($list_special_folders_first == true) { |
236 | for ($i = 0; $i < count($boxes); $i++) { |
1e0628fb |
237 | if (($boxes[$i]["unformatted"] == $trash_folder) && ($move_to_trash)) { |
238 | $pos = count($boxesnew); |
239 | $boxesnew[$pos] = $boxes[$i]; |
240 | $boxes[$i]["used"] = true; |
241 | } |
242 | else if (($boxes[$i]["unformatted"] == $sent_folder) && ($move_to_sent)) { |
243 | $pos = count($boxesnew); |
244 | $boxesnew[$pos] = $boxes[$i]; |
245 | $boxes[$i]["used"] = true; |
8e9e8afa |
246 | } |
247 | } |
248 | } |
aed206bf |
249 | |
8e9e8afa |
250 | for ($i = 0; $i < count($boxes); $i++) { |
1e0628fb |
251 | if ((strtolower($boxes[$i]["unformatted"]) != "inbox") && |
8e9e8afa |
252 | ($boxes[$i]["used"] == false)) { |
253 | $pos = count($boxesnew); |
254 | $boxesnew[$pos] = $boxes[$i]; |
255 | $boxes[$i]["used"] = true; |
256 | } |
257 | } |
258 | |
259 | return $boxesnew; |
260 | } |
8e9e8afa |
261 | |
262 | /****************************************************************************** |
263 | ** Returns a list of all folders, subscribed or not |
264 | ******************************************************************************/ |
265 | function sqimap_mailbox_list_all ($imap_stream) { |
1e0628fb |
266 | global $list_special_folders_first, $folder_prefix; |
8e9e8afa |
267 | |
268 | if (!function_exists ("ary_sort")) |
269 | include ("../functions/array.php"); |
270 | |
271 | $dm = sqimap_get_delimiter ($imap_stream); |
272 | |
273 | fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n"); |
274 | $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
275 | $g = 0; |
276 | $phase = "inbox"; |
277 | for ($i = 0; $i < count($read_ary); $i++) { |
278 | if (substr ($read_ary[$i], 0, 4) != "a001") { |
279 | $boxes[$g]["raw"] = $read_ary[$i]; |
280 | |
281 | $mailbox = find_mailbox_name($read_ary[$i]); |
282 | $dm_count = countCharInString($mailbox, $dm); |
283 | if (substr($mailbox, -1) == $dm) |
284 | $dm_count--; |
285 | |
286 | for ($j = 0; $j < $dm_count; $j++) |
287 | $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " "; |
288 | $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm); |
289 | |
290 | $boxes[$g]["unformatted-dm"] = $mailbox; |
291 | if (substr($mailbox, -1) == $dm) |
292 | $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); |
293 | $boxes[$g]["unformatted"] = $mailbox; |
294 | $boxes[$g]["id"] = $g; |
295 | |
296 | /** Now lets get the flags for this mailbox **/ |
297 | fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n"); |
298 | $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message); |
299 | |
300 | $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1); |
301 | $flags = substr($flags, 0, strpos($flags, ")")); |
302 | $flags = str_replace("\\", "", $flags); |
303 | $flags = trim(strtolower($flags)); |
304 | if ($flags) { |
305 | $boxes[$g]["flags"] = explode(" ", $flags); |
306 | } |
307 | } |
308 | $g++; |
309 | } |
310 | $boxes = ary_sort ($boxes, "unformatted", 1); |
311 | return $boxes; |
312 | } |
313 | |
052e0c26 |
314 | ?> |