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) { |
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) { |
25 | if (ereg ("$mailbox", $mbx[0])) { |
26 | return true; |
27 | } else { |
28 | return false; |
29 | } |
8e9e8afa |
30 | } |
8e9e8afa |
31 | } |
32 | |
33 | |
34 | |
35 | /****************************************************************************** |
36 | ** Selects a mailbox |
37 | ******************************************************************************/ |
04632dbc |
38 | function sqimap_mailbox_select ($imap_stream, $mailbox, $hide=true, $recent=false) { |
8e9e8afa |
39 | fputs ($imap_stream, "a001 SELECT \"$mailbox\"\r\n"); |
c5eb2c03 |
40 | $read = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
04632dbc |
41 | if ($recent) { |
42 | for ($i=0; $i<count($read); $i++) { |
43 | if (strpos(strtolower($read[$i]), "recent")) { |
44 | $r = explode(" ", $read[$i]); |
45 | } |
46 | } |
47 | return $r[1]; |
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) { |
cbdc5621 |
84 | global $imap_server_type; |
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 |
105 | ******************************************************************************/ |
16530f8b |
106 | function sqimap_mailbox_parse ($line, $line_lsub, $dm) { |
aed206bf |
107 | global $folder_prefix; |
8e9e8afa |
108 | for ($g=0; $g < count($line); $g++) { |
109 | $boxes[$g]["raw"] = $line[$g]; |
110 | |
16530f8b |
111 | $mailbox = $line_lsub[$g]; |
8e9e8afa |
112 | $dm_count = countCharInString($mailbox, $dm); |
113 | if (substr($mailbox, -1) == $dm) |
114 | $dm_count--; |
115 | |
aed206bf |
116 | for ($j = 0; $j < $dm_count - (countCharInString($folder_prefix, $dm)); $j++) |
390372b4 |
117 | $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " "; |
8e9e8afa |
118 | $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm); |
119 | |
120 | $boxes[$g]["unformatted-dm"] = $mailbox; |
121 | if (substr($mailbox, -1) == $dm) |
122 | $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); |
123 | $boxes[$g]["unformatted"] = $mailbox; |
124 | $boxes[$g]["id"] = $g; |
125 | |
126 | $flags = substr($line[$g], strpos($line[$g], "(")+1); |
127 | $flags = substr($flags, 0, strpos($flags, ")")); |
128 | $flags = str_replace("\\", "", $flags); |
129 | $flags = trim(strtolower($flags)); |
130 | if ($flags) { |
131 | $boxes[$g]["flags"] = explode(" ", $flags); |
132 | } |
8e9e8afa |
133 | } |
134 | return $boxes; |
135 | } |
136 | |
137 | /****************************************************************************** |
138 | ** Returns sorted mailbox lists in several different ways. |
139 | ** The array returned looks like this: |
140 | ******************************************************************************/ |
141 | function sqimap_mailbox_list ($imap_stream) { |
aed206bf |
142 | global $load_prefs_php, $prefs_php, $config_php, $data_dir, $username, $list_special_folders_first; |
1e0628fb |
143 | global $trash_folder, $sent_folder; |
144 | global $move_to_trash, $move_to_sent; |
8e9e8afa |
145 | |
1590a668 |
146 | $inbox_in_list = false; |
147 | $inbox_subscribed = false; |
148 | |
8e9e8afa |
149 | if (!isset($load_prefs_php)) include "../src/load_prefs.php"; |
150 | else global $folder_prefix; |
151 | if (!function_exists ("ary_sort")) include "../functions/array.php"; |
152 | |
153 | $dm = sqimap_get_delimiter ($imap_stream); |
154 | |
8e9e8afa |
155 | /** LSUB array **/ |
156 | $inbox_subscribed = false; |
157 | fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n"); |
158 | $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
480feea7 |
159 | |
8e9e8afa |
160 | for ($i=0;$i < count($lsub_ary); $i++) { |
161 | $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]); |
8e9e8afa |
162 | if ($sorted_lsub_ary[$i] == "INBOX") |
163 | $inbox_subscribed = true; |
164 | } |
165 | if (isset($sorted_lsub_ary)) { |
166 | sort($sorted_lsub_ary); |
167 | } |
168 | |
3cb866d7 |
169 | /** LIST array **/ |
170 | for ($i=0; $i < count($sorted_lsub_ary); $i++) { |
171 | if (substr($sorted_lsub_ary[$i], -1) == $dm) |
172 | $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1); |
173 | else |
174 | $mbx = $sorted_lsub_ary[$i]; |
175 | |
176 | fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n"); |
177 | $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
178 | $sorted_list_ary[$i] = $read[0]; |
179 | if (find_mailbox_name($sorted_list_ary[$i]) == "INBOX") |
180 | $inbox_in_list = true; |
181 | } |
182 | |
8e9e8afa |
183 | /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/ |
184 | if ($inbox_subscribed == false || $inbox_in_list == false) { |
185 | fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n"); |
186 | $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
591000ec |
187 | |
188 | $pos = count($sorted_list_ary); |
189 | $sorted_list_ary[$pos] = $inbox_ary[0]; |
190 | |
191 | $pos = count($sorted_lsub_ary); |
192 | $sorted_lsub_ary[$pos] = find_mailbox_name($inbox_ary[0]); |
8e9e8afa |
193 | } |
194 | |
16530f8b |
195 | $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm); |
aed206bf |
196 | |
197 | /** Now, lets sort for special folders **/ |
8e9e8afa |
198 | for ($i = 0; $i < count($boxes); $i++) { |
1e0628fb |
199 | if (strtolower($boxes[$i]["unformatted"]) == "inbox") { |
8e9e8afa |
200 | $boxesnew[0] = $boxes[$i]; |
201 | $boxes[$i]["used"] = true; |
c5eb2c03 |
202 | $i = count($boxes); |
8e9e8afa |
203 | } |
204 | } |
aed206bf |
205 | |
8e9e8afa |
206 | if ($list_special_folders_first == true) { |
c5eb2c03 |
207 | for ($i = count($boxes)-1; $i >= 0 ; $i--) { |
1e0628fb |
208 | if (($boxes[$i]["unformatted"] == $trash_folder) && ($move_to_trash)) { |
209 | $pos = count($boxesnew); |
210 | $boxesnew[$pos] = $boxes[$i]; |
211 | $boxes[$i]["used"] = true; |
c5eb2c03 |
212 | $trash_found = true; |
1e0628fb |
213 | } |
214 | else if (($boxes[$i]["unformatted"] == $sent_folder) && ($move_to_sent)) { |
215 | $pos = count($boxesnew); |
216 | $boxesnew[$pos] = $boxes[$i]; |
217 | $boxes[$i]["used"] = true; |
c5eb2c03 |
218 | $sent_found = true; |
8e9e8afa |
219 | } |
c5eb2c03 |
220 | |
221 | if (($sent_found && $trash_found) || ($sent_found && !$move_to_trash) || ($trash_found && !$move_to_sent) || (!$move_to_sent && !$move_to_trash)) |
222 | $i = -1; |
8e9e8afa |
223 | } |
224 | } |
c5eb2c03 |
225 | |
8e9e8afa |
226 | for ($i = 0; $i < count($boxes); $i++) { |
1e0628fb |
227 | if ((strtolower($boxes[$i]["unformatted"]) != "inbox") && |
8e9e8afa |
228 | ($boxes[$i]["used"] == false)) { |
229 | $pos = count($boxesnew); |
230 | $boxesnew[$pos] = $boxes[$i]; |
231 | $boxes[$i]["used"] = true; |
232 | } |
233 | } |
234 | |
235 | return $boxesnew; |
236 | } |
8e9e8afa |
237 | |
238 | /****************************************************************************** |
239 | ** Returns a list of all folders, subscribed or not |
240 | ******************************************************************************/ |
241 | function sqimap_mailbox_list_all ($imap_stream) { |
1e0628fb |
242 | global $list_special_folders_first, $folder_prefix; |
8e9e8afa |
243 | |
244 | if (!function_exists ("ary_sort")) |
245 | include ("../functions/array.php"); |
246 | |
247 | $dm = sqimap_get_delimiter ($imap_stream); |
248 | |
249 | fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n"); |
250 | $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
251 | $g = 0; |
252 | $phase = "inbox"; |
253 | for ($i = 0; $i < count($read_ary); $i++) { |
254 | if (substr ($read_ary[$i], 0, 4) != "a001") { |
255 | $boxes[$g]["raw"] = $read_ary[$i]; |
256 | |
257 | $mailbox = find_mailbox_name($read_ary[$i]); |
258 | $dm_count = countCharInString($mailbox, $dm); |
259 | if (substr($mailbox, -1) == $dm) |
260 | $dm_count--; |
261 | |
262 | for ($j = 0; $j < $dm_count; $j++) |
263 | $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " "; |
264 | $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm); |
265 | |
266 | $boxes[$g]["unformatted-dm"] = $mailbox; |
267 | if (substr($mailbox, -1) == $dm) |
268 | $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); |
269 | $boxes[$g]["unformatted"] = $mailbox; |
270 | $boxes[$g]["id"] = $g; |
271 | |
272 | /** Now lets get the flags for this mailbox **/ |
273 | fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n"); |
274 | $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message); |
275 | |
276 | $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1); |
277 | $flags = substr($flags, 0, strpos($flags, ")")); |
278 | $flags = str_replace("\\", "", $flags); |
279 | $flags = trim(strtolower($flags)); |
280 | if ($flags) { |
281 | $boxes[$g]["flags"] = explode(" ", $flags); |
282 | } |
283 | } |
284 | $g++; |
285 | } |
591000ec |
286 | if ($boxes) { |
287 | $boxes = ary_sort ($boxes, "unformatted", 1); |
288 | } |
8e9e8afa |
289 | return $boxes; |
290 | } |
291 | |
052e0c26 |
292 | ?> |