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