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