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); |
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) { |
26 | if (ereg ("$mailbox", $mbx[0])) { |
27 | return true; |
28 | } else { |
29 | return false; |
30 | } |
8e9e8afa |
31 | } |
8e9e8afa |
32 | } |
33 | |
a2790a61 |
34 | /****************************************************************************** |
35 | ** Closes an open mailbox |
36 | ******************************************************************************/ |
37 | function sqimap_mailbox_close ($imap_stream) { |
38 | fputs ($imap_stream, "a001 CLOSE\r\n"); |
c95df380 |
39 | $tmp = sqimap_read_data($imap_stream, "a001", false, $response, $message); |
a2790a61 |
40 | } |
8e9e8afa |
41 | |
8e9e8afa |
42 | /****************************************************************************** |
43 | ** Selects a mailbox |
44 | ******************************************************************************/ |
04632dbc |
45 | function sqimap_mailbox_select ($imap_stream, $mailbox, $hide=true, $recent=false) { |
3751dc7f |
46 | global $auto_expunge; |
47 | |
8e9e8afa |
48 | fputs ($imap_stream, "a001 SELECT \"$mailbox\"\r\n"); |
c5eb2c03 |
49 | $read = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
04632dbc |
50 | if ($recent) { |
51 | for ($i=0; $i<count($read); $i++) { |
52 | if (strpos(strtolower($read[$i]), "recent")) { |
53 | $r = explode(" ", $read[$i]); |
54 | } |
55 | } |
56 | return $r[1]; |
57 | } |
3751dc7f |
58 | if ($auto_expunge) { |
59 | fputs ($imap_stream, "a001 EXPUNGE\r\n"); |
60 | $tmp = sqimap_read_data($imap_stream, "a001", $a, $b, true); |
61 | } |
8e9e8afa |
62 | } |
63 | |
64 | |
65 | |
66 | /****************************************************************************** |
67 | ** Creates a folder |
68 | ******************************************************************************/ |
69 | function sqimap_mailbox_create ($imap_stream, $mailbox, $type) { |
70 | if (strtolower($type) == "noselect") { |
71 | $dm = sqimap_get_delimiter($imap_stream); |
72 | $mailbox = $mailbox.$dm; |
73 | } |
74 | fputs ($imap_stream, "a001 CREATE \"$mailbox\"\r\n"); |
75 | $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
76 | |
77 | sqimap_subscribe ($imap_stream, $mailbox); |
78 | } |
79 | |
80 | |
81 | |
82 | /****************************************************************************** |
83 | ** Subscribes to an existing folder |
84 | ******************************************************************************/ |
85 | function sqimap_subscribe ($imap_stream, $mailbox) { |
86 | fputs ($imap_stream, "a001 SUBSCRIBE \"$mailbox\"\r\n"); |
87 | $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
88 | } |
89 | |
90 | |
91 | |
92 | |
93 | /****************************************************************************** |
94 | ** Unsubscribes to an existing folder |
95 | ******************************************************************************/ |
96 | function sqimap_unsubscribe ($imap_stream, $mailbox) { |
cbdc5621 |
97 | global $imap_server_type; |
98 | |
8e9e8afa |
99 | fputs ($imap_stream, "a001 UNSUBSCRIBE \"$mailbox\"\r\n"); |
100 | $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
101 | } |
102 | |
103 | |
104 | |
105 | |
106 | /****************************************************************************** |
107 | ** This function simply deletes the given folder |
108 | ******************************************************************************/ |
109 | function sqimap_mailbox_delete ($imap_stream, $mailbox) { |
110 | fputs ($imap_stream, "a001 DELETE \"$mailbox\"\r\n"); |
111 | $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
112 | sqimap_unsubscribe ($imap_stream, $mailbox); |
113 | } |
114 | |
115 | |
116 | /****************************************************************************** |
117 | ** Formats a mailbox into 4 parts for the $boxes array |
118 | ******************************************************************************/ |
16530f8b |
119 | function sqimap_mailbox_parse ($line, $line_lsub, $dm) { |
aed206bf |
120 | global $folder_prefix; |
8e9e8afa |
121 | for ($g=0; $g < count($line); $g++) { |
122 | $boxes[$g]["raw"] = $line[$g]; |
123 | |
16530f8b |
124 | $mailbox = $line_lsub[$g]; |
8e9e8afa |
125 | $dm_count = countCharInString($mailbox, $dm); |
126 | if (substr($mailbox, -1) == $dm) |
127 | $dm_count--; |
128 | |
aed206bf |
129 | for ($j = 0; $j < $dm_count - (countCharInString($folder_prefix, $dm)); $j++) |
390372b4 |
130 | $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " "; |
8e9e8afa |
131 | $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm); |
132 | |
133 | $boxes[$g]["unformatted-dm"] = $mailbox; |
134 | if (substr($mailbox, -1) == $dm) |
135 | $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); |
136 | $boxes[$g]["unformatted"] = $mailbox; |
137 | $boxes[$g]["id"] = $g; |
138 | |
139 | $flags = substr($line[$g], strpos($line[$g], "(")+1); |
140 | $flags = substr($flags, 0, strpos($flags, ")")); |
141 | $flags = str_replace("\\", "", $flags); |
142 | $flags = trim(strtolower($flags)); |
143 | if ($flags) { |
144 | $boxes[$g]["flags"] = explode(" ", $flags); |
145 | } |
8e9e8afa |
146 | } |
147 | return $boxes; |
148 | } |
149 | |
150 | /****************************************************************************** |
151 | ** Returns sorted mailbox lists in several different ways. |
152 | ** The array returned looks like this: |
153 | ******************************************************************************/ |
154 | function sqimap_mailbox_list ($imap_stream) { |
aed206bf |
155 | global $load_prefs_php, $prefs_php, $config_php, $data_dir, $username, $list_special_folders_first; |
1e0628fb |
156 | global $trash_folder, $sent_folder; |
157 | global $move_to_trash, $move_to_sent; |
8e9e8afa |
158 | |
1590a668 |
159 | $inbox_in_list = false; |
160 | $inbox_subscribed = false; |
161 | |
8e9e8afa |
162 | if (!isset($load_prefs_php)) include "../src/load_prefs.php"; |
163 | else global $folder_prefix; |
164 | if (!function_exists ("ary_sort")) include "../functions/array.php"; |
165 | |
166 | $dm = sqimap_get_delimiter ($imap_stream); |
167 | |
8e9e8afa |
168 | /** LSUB array **/ |
169 | $inbox_subscribed = false; |
170 | fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n"); |
171 | $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
480feea7 |
172 | |
8e9e8afa |
173 | for ($i=0;$i < count($lsub_ary); $i++) { |
174 | $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]); |
8e9e8afa |
175 | if ($sorted_lsub_ary[$i] == "INBOX") |
176 | $inbox_subscribed = true; |
177 | } |
ee62ce13 |
178 | $new_ary = array(); |
179 | for ($i=0; $i < count($sorted_lsub_ary); $i++) { |
180 | if (!in_array($sorted_lsub_ary[$i], $new_ary)) { |
181 | $new_ary[] = $sorted_lsub_ary[$i]; |
182 | } |
183 | } |
184 | $sorted_lsub_ary = $new_ary; |
8e9e8afa |
185 | if (isset($sorted_lsub_ary)) { |
186 | sort($sorted_lsub_ary); |
187 | } |
188 | |
3cb866d7 |
189 | /** LIST array **/ |
190 | for ($i=0; $i < count($sorted_lsub_ary); $i++) { |
191 | if (substr($sorted_lsub_ary[$i], -1) == $dm) |
192 | $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1); |
193 | else |
194 | $mbx = $sorted_lsub_ary[$i]; |
195 | |
196 | fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n"); |
197 | $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
198 | $sorted_list_ary[$i] = $read[0]; |
199 | if (find_mailbox_name($sorted_list_ary[$i]) == "INBOX") |
200 | $inbox_in_list = true; |
201 | } |
202 | |
8e9e8afa |
203 | /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/ |
204 | if ($inbox_subscribed == false || $inbox_in_list == false) { |
205 | fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n"); |
206 | $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
591000ec |
207 | |
208 | $pos = count($sorted_list_ary); |
209 | $sorted_list_ary[$pos] = $inbox_ary[0]; |
210 | |
211 | $pos = count($sorted_lsub_ary); |
212 | $sorted_lsub_ary[$pos] = find_mailbox_name($inbox_ary[0]); |
8e9e8afa |
213 | } |
214 | |
16530f8b |
215 | $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm); |
aed206bf |
216 | |
217 | /** Now, lets sort for special folders **/ |
8e9e8afa |
218 | for ($i = 0; $i < count($boxes); $i++) { |
1e0628fb |
219 | if (strtolower($boxes[$i]["unformatted"]) == "inbox") { |
8e9e8afa |
220 | $boxesnew[0] = $boxes[$i]; |
221 | $boxes[$i]["used"] = true; |
c5eb2c03 |
222 | $i = count($boxes); |
8e9e8afa |
223 | } |
224 | } |
aed206bf |
225 | |
8e9e8afa |
226 | if ($list_special_folders_first == true) { |
c5eb2c03 |
227 | for ($i = count($boxes)-1; $i >= 0 ; $i--) { |
1e0628fb |
228 | if (($boxes[$i]["unformatted"] == $trash_folder) && ($move_to_trash)) { |
229 | $pos = count($boxesnew); |
230 | $boxesnew[$pos] = $boxes[$i]; |
231 | $boxes[$i]["used"] = true; |
c5eb2c03 |
232 | $trash_found = true; |
1e0628fb |
233 | } |
234 | else if (($boxes[$i]["unformatted"] == $sent_folder) && ($move_to_sent)) { |
235 | $pos = count($boxesnew); |
236 | $boxesnew[$pos] = $boxes[$i]; |
237 | $boxes[$i]["used"] = true; |
c5eb2c03 |
238 | $sent_found = true; |
8e9e8afa |
239 | } |
c5eb2c03 |
240 | |
241 | if (($sent_found && $trash_found) || ($sent_found && !$move_to_trash) || ($trash_found && !$move_to_sent) || (!$move_to_sent && !$move_to_trash)) |
242 | $i = -1; |
8e9e8afa |
243 | } |
244 | } |
c5eb2c03 |
245 | |
8e9e8afa |
246 | for ($i = 0; $i < count($boxes); $i++) { |
1e0628fb |
247 | if ((strtolower($boxes[$i]["unformatted"]) != "inbox") && |
8e9e8afa |
248 | ($boxes[$i]["used"] == false)) { |
249 | $pos = count($boxesnew); |
250 | $boxesnew[$pos] = $boxes[$i]; |
251 | $boxes[$i]["used"] = true; |
252 | } |
253 | } |
254 | |
255 | return $boxesnew; |
256 | } |
8e9e8afa |
257 | |
258 | /****************************************************************************** |
259 | ** Returns a list of all folders, subscribed or not |
260 | ******************************************************************************/ |
261 | function sqimap_mailbox_list_all ($imap_stream) { |
1e0628fb |
262 | global $list_special_folders_first, $folder_prefix; |
8e9e8afa |
263 | |
264 | if (!function_exists ("ary_sort")) |
265 | include ("../functions/array.php"); |
266 | |
267 | $dm = sqimap_get_delimiter ($imap_stream); |
268 | |
269 | fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n"); |
270 | $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
271 | $g = 0; |
272 | $phase = "inbox"; |
273 | for ($i = 0; $i < count($read_ary); $i++) { |
274 | if (substr ($read_ary[$i], 0, 4) != "a001") { |
275 | $boxes[$g]["raw"] = $read_ary[$i]; |
276 | |
277 | $mailbox = find_mailbox_name($read_ary[$i]); |
278 | $dm_count = countCharInString($mailbox, $dm); |
279 | if (substr($mailbox, -1) == $dm) |
280 | $dm_count--; |
281 | |
282 | for ($j = 0; $j < $dm_count; $j++) |
283 | $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " "; |
284 | $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm); |
285 | |
286 | $boxes[$g]["unformatted-dm"] = $mailbox; |
287 | if (substr($mailbox, -1) == $dm) |
288 | $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); |
289 | $boxes[$g]["unformatted"] = $mailbox; |
290 | $boxes[$g]["id"] = $g; |
291 | |
292 | /** Now lets get the flags for this mailbox **/ |
293 | fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n"); |
294 | $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message); |
295 | |
296 | $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1); |
297 | $flags = substr($flags, 0, strpos($flags, ")")); |
298 | $flags = str_replace("\\", "", $flags); |
299 | $flags = trim(strtolower($flags)); |
300 | if ($flags) { |
301 | $boxes[$g]["flags"] = explode(" ", $flags); |
302 | } |
303 | } |
304 | $g++; |
305 | } |
591000ec |
306 | if ($boxes) { |
307 | $boxes = ary_sort ($boxes, "unformatted", 1); |
308 | } |
8e9e8afa |
309 | return $boxes; |
310 | } |
311 | |
052e0c26 |
312 | ?> |