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) { |
74 | fputs ($imap_stream, "a001 UNSUBSCRIBE \"$mailbox\"\r\n"); |
75 | $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
76 | } |
77 | |
78 | |
79 | |
80 | |
81 | /****************************************************************************** |
82 | ** This function simply deletes the given folder |
83 | ******************************************************************************/ |
84 | function sqimap_mailbox_delete ($imap_stream, $mailbox) { |
85 | fputs ($imap_stream, "a001 DELETE \"$mailbox\"\r\n"); |
86 | $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
87 | sqimap_unsubscribe ($imap_stream, $mailbox); |
88 | } |
89 | |
90 | |
91 | /****************************************************************************** |
92 | ** Formats a mailbox into 4 parts for the $boxes array |
93 | ******************************************************************************/ |
94 | function sqimap_mailbox_parse ($line, $dm) { |
95 | for ($g=0; $g < count($line); $g++) { |
96 | $boxes[$g]["raw"] = $line[$g]; |
97 | |
98 | $mailbox = find_mailbox_name($line[$g]); |
99 | $dm_count = countCharInString($mailbox, $dm); |
100 | if (substr($mailbox, -1) == $dm) |
101 | $dm_count--; |
102 | |
103 | for ($j = 0; $j < $dm_count; $j++) |
104 | $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " "; |
105 | $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm); |
106 | |
107 | $boxes[$g]["unformatted-dm"] = $mailbox; |
108 | if (substr($mailbox, -1) == $dm) |
109 | $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); |
110 | $boxes[$g]["unformatted"] = $mailbox; |
111 | $boxes[$g]["id"] = $g; |
112 | |
113 | $flags = substr($line[$g], strpos($line[$g], "(")+1); |
114 | $flags = substr($flags, 0, strpos($flags, ")")); |
115 | $flags = str_replace("\\", "", $flags); |
116 | $flags = trim(strtolower($flags)); |
117 | if ($flags) { |
118 | $boxes[$g]["flags"] = explode(" ", $flags); |
119 | } |
120 | |
121 | } |
122 | return $boxes; |
123 | } |
124 | |
125 | /****************************************************************************** |
126 | ** Returns sorted mailbox lists in several different ways. |
127 | ** The array returned looks like this: |
128 | ******************************************************************************/ |
129 | function sqimap_mailbox_list ($imap_stream) { |
130 | global $load_prefs_php, $prefs_php, $config_php, $data_dir, $username; |
131 | |
132 | if (!isset($load_prefs_php)) include "../src/load_prefs.php"; |
133 | else global $folder_prefix; |
134 | if (!function_exists ("ary_sort")) include "../functions/array.php"; |
135 | |
136 | $dm = sqimap_get_delimiter ($imap_stream); |
137 | |
138 | /** LIST array **/ |
139 | fputs ($imap_stream, "a001 LIST \"\" \"$folder_prefix*\"\r\n"); |
140 | $list_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
141 | for ($i=0;$i < count($list_ary); $i++) { |
142 | $sorted_list_ary[$i]["name"] = find_mailbox_name($list_ary[$i]); |
143 | $sorted_list_ary[$i]["raw"] = $list_ary[$i]; |
144 | if ($sorged_list_ary[$i]["name"] == "INBOX") |
145 | $inbox_in_list = true; |
146 | } |
147 | if (isset($sorted_list_ary)) { |
148 | $list_sorted = array_cleave ($sorted_list_ary, "name"); |
149 | asort($list_sorted); |
150 | } |
151 | |
8e9e8afa |
152 | |
153 | /** LSUB array **/ |
154 | $inbox_subscribed = false; |
155 | fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n"); |
156 | $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
157 | for ($i=0;$i < count($lsub_ary); $i++) { |
158 | $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]); |
159 | if (substr($sorted_lsub_ary[$i], -1) == $dm) |
160 | $sorted_lsub_ary[$i] = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1); |
161 | if ($sorted_lsub_ary[$i] == "INBOX") |
162 | $inbox_subscribed = true; |
163 | } |
164 | if (isset($sorted_lsub_ary)) { |
165 | sort($sorted_lsub_ary); |
166 | } |
167 | |
168 | |
169 | /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/ |
170 | if ($inbox_subscribed == false || $inbox_in_list == false) { |
171 | fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n"); |
172 | $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
173 | $merged[0] = $inbox_ary[0]; |
174 | $k = 1; |
175 | } else { |
176 | $k = 0; |
177 | } |
178 | |
179 | $i = $j = 0; |
180 | |
181 | if (isset($list_sorted) && isset($sorted_lsub_ary)) { |
182 | reset ($list_sorted); |
183 | for (reset($list_sorted); $key = key($list_sorted), isset($key);) { |
184 | if ($sorted_lsub_ary[$i] == $list_sorted[$key]) { |
185 | $merged[$k] = $sorted_list_ary[$key]["raw"]; |
186 | $k++; |
187 | $i++; |
188 | next($list_sorted); |
189 | } else if ($sorted_lsub_ary[$i] < $list_sorted[$key]) { |
190 | $i++; |
191 | } else { |
192 | next($list_sorted); |
193 | } |
194 | } |
195 | } |
196 | |
8e9e8afa |
197 | return sqimap_mailbox_parse ($merged, $dm); |
198 | } |
199 | |
5b6ae78a |
200 | /* |
8e9e8afa |
201 | $original = $boxes; |
5b6ae78a |
202 | |
8e9e8afa |
203 | for ($i = 0; $i < count($original); $i++) { |
204 | $boxes[$i]["unformatted"] = strtolower($boxes[$i]["unformatted"]); |
205 | } |
5b6ae78a |
206 | |
8e9e8afa |
207 | $boxes = ary_sort($boxes, "unformatted", 1); |
5b6ae78a |
208 | |
8e9e8afa |
209 | for ($i = 0; $i < count($boxes); $i++) { |
210 | for ($j = 0; $j < count($original); $j++) { |
211 | if ($boxes[$i]["id"] == $original[$j]["id"]) { |
212 | $boxes[$i] = $original[$j]; |
213 | } |
214 | } |
215 | } |
052e0c26 |
216 | |
8e9e8afa |
217 | |
218 | for ($i = 0; $i < count($boxes); $i++) { |
219 | if ($boxes[$i]["unformatted"] == $special_folders[0]) { |
220 | $boxesnew[0] = $boxes[$i]; |
221 | $boxes[$i]["used"] = true; |
222 | } |
223 | } |
224 | |
225 | if ($list_special_folders_first == true) { |
226 | for ($i = 0; $i < count($boxes); $i++) { |
227 | for ($j = 1; $j < count($special_folders); $j++) { |
228 | if (substr($boxes[$i]["unformatted"], 0, strlen($special_folders[$j])) == $special_folders[$j]) { |
229 | $pos = count($boxesnew); |
230 | $boxesnew[$pos] = $boxes[$i]; |
231 | $boxes[$i]["used"] = true; |
232 | } |
233 | } |
234 | } |
235 | } |
236 | |
237 | for ($i = 0; $i < count($boxes); $i++) { |
238 | if (($boxes[$i]["unformatted"] != $special_folders[0]) && |
239 | ($boxes[$i]["used"] == false)) { |
240 | $pos = count($boxesnew); |
241 | $boxesnew[$pos] = $boxes[$i]; |
242 | $boxes[$i]["used"] = true; |
243 | } |
244 | } |
245 | |
246 | return $boxesnew; |
247 | } |
248 | */ |
249 | |
250 | /****************************************************************************** |
251 | ** Returns a list of all folders, subscribed or not |
252 | ******************************************************************************/ |
253 | function sqimap_mailbox_list_all ($imap_stream) { |
254 | global $special_folders, $list_special_folders_first, $folder_prefix; |
255 | |
256 | if (!function_exists ("ary_sort")) |
257 | include ("../functions/array.php"); |
258 | |
259 | $dm = sqimap_get_delimiter ($imap_stream); |
260 | |
261 | fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n"); |
262 | $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
263 | $g = 0; |
264 | $phase = "inbox"; |
265 | for ($i = 0; $i < count($read_ary); $i++) { |
266 | if (substr ($read_ary[$i], 0, 4) != "a001") { |
267 | $boxes[$g]["raw"] = $read_ary[$i]; |
268 | |
269 | $mailbox = find_mailbox_name($read_ary[$i]); |
270 | $dm_count = countCharInString($mailbox, $dm); |
271 | if (substr($mailbox, -1) == $dm) |
272 | $dm_count--; |
273 | |
274 | for ($j = 0; $j < $dm_count; $j++) |
275 | $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " "; |
276 | $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm); |
277 | |
278 | $boxes[$g]["unformatted-dm"] = $mailbox; |
279 | if (substr($mailbox, -1) == $dm) |
280 | $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); |
281 | $boxes[$g]["unformatted"] = $mailbox; |
282 | $boxes[$g]["id"] = $g; |
283 | |
284 | /** Now lets get the flags for this mailbox **/ |
285 | fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n"); |
286 | $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message); |
287 | |
288 | $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1); |
289 | $flags = substr($flags, 0, strpos($flags, ")")); |
290 | $flags = str_replace("\\", "", $flags); |
291 | $flags = trim(strtolower($flags)); |
292 | if ($flags) { |
293 | $boxes[$g]["flags"] = explode(" ", $flags); |
294 | } |
295 | } |
296 | $g++; |
297 | } |
298 | $boxes = ary_sort ($boxes, "unformatted", 1); |
299 | return $boxes; |
300 | } |
301 | |
052e0c26 |
302 | ?> |