21ee5a80e3db2bf5fc36fa1a5c4cf8ad31031747
[squirrelmail.git] / functions / imap_mailbox.php
1 <?php
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,$handle_errors = true) {
12 sqimap_mailbox_select ($imap_stream, $mailbox);
13 fputs ($imap_stream, "a001 EXPUNGE\r\n");
14 $read = sqimap_read_data($imap_stream, "a001", $handle_errors, $response, $message);
15 sqimap_mailbox_close ($imap_stream);
16 }
17
18
19 /******************************************************************************
20 ** Checks whether or not the specified mailbox exists
21 ******************************************************************************/
22 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
23 fputs ($imap_stream, "a001 LIST \"\" \"$mailbox\"\r\n");
24 $mbx = sqimap_read_data($imap_stream, "a001", true, $response, $message);
25 if ($mailbox) {
26 return !!(ereg ("$mailbox", $mbx[0])); // To force into true/false
27 }
28 }
29
30 /******************************************************************************
31 ** Closes an open mailbox
32 ******************************************************************************/
33 function sqimap_mailbox_close ($imap_stream) {
34 fputs ($imap_stream, "a001 CLOSE\r\n");
35 $tmp = sqimap_read_data($imap_stream, "a001", false, $response, $message);
36 }
37
38 /******************************************************************************
39 ** Selects a mailbox
40 ******************************************************************************/
41 function sqimap_mailbox_select ($imap_stream, $mailbox, $hide=true, $recent=false) {
42 global $auto_expunge;
43
44 fputs ($imap_stream, "a001 SELECT \"$mailbox\"\r\n");
45 $read = sqimap_read_data($imap_stream, "a001", true, $response, $message);
46 if ($recent) {
47 for ($i=0; $i<count($read); $i++) {
48 if (strpos(strtolower($read[$i]), "recent")) {
49 $r = explode(" ", $read[$i]);
50 }
51 }
52 return $r[1];
53 }
54 if ($auto_expunge) {
55 fputs ($imap_stream, "a001 EXPUNGE\r\n");
56 $tmp = sqimap_read_data($imap_stream, "a001", false, $a, $b);
57 }
58 }
59
60
61
62 /******************************************************************************
63 ** Creates a folder
64 ******************************************************************************/
65 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
66 if (strtolower($type) == "noselect") {
67 $dm = sqimap_get_delimiter($imap_stream);
68 $mailbox = $mailbox.$dm;
69 }
70 fputs ($imap_stream, "a001 CREATE \"$mailbox\"\r\n");
71 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
72
73 sqimap_subscribe ($imap_stream, $mailbox);
74 }
75
76
77
78 /******************************************************************************
79 ** Subscribes to an existing folder
80 ******************************************************************************/
81 function sqimap_subscribe ($imap_stream, $mailbox) {
82 fputs ($imap_stream, "a001 SUBSCRIBE \"$mailbox\"\r\n");
83 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
84 }
85
86
87
88
89 /******************************************************************************
90 ** Unsubscribes to an existing folder
91 ******************************************************************************/
92 function sqimap_unsubscribe ($imap_stream, $mailbox) {
93 global $imap_server_type;
94
95 fputs ($imap_stream, "a001 UNSUBSCRIBE \"$mailbox\"\r\n");
96 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
97 }
98
99
100
101
102 /******************************************************************************
103 ** This function simply deletes the given folder
104 ******************************************************************************/
105 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
106 fputs ($imap_stream, "a001 DELETE \"$mailbox\"\r\n");
107 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
108 sqimap_unsubscribe ($imap_stream, $mailbox);
109 }
110
111
112 /******************************************************************************
113 ** Formats a mailbox into 4 parts for the $boxes array
114 **
115 ** The four parts are:
116 **
117 ** raw - Raw LIST/LSUB response from the IMAP server
118 ** formatted - nicely formatted folder name
119 ** unformatted - unformatted, but with delimiter at end removed
120 ** unformatted-dm - folder name as it appears in raw response
121 ** unformatted-disp - unformatted without $folder_prefix
122 **
123 ******************************************************************************/
124 function sqimap_mailbox_parse ($line, $line_lsub, $dm) {
125 global $folder_prefix;
126
127 // Process each folder line
128 for ($g=0; $g < count($line); $g++) {
129
130 // Store the raw IMAP reply
131 $boxes[$g]["raw"] = $line[$g];
132
133 // Count number of delimiters ($dm) in folder name
134 $mailbox = trim($line_lsub[$g]);
135 $dm_count = countCharInString($mailbox, $dm);
136 if (substr($mailbox, -1) == $dm)
137 $dm_count--; // If name ends in delimiter - decrement count by one
138
139 // Format folder name, but only if it's a INBOX.* or have
140 // a parent.
141 $boxesbyname[$mailbox] = $g;
142 $parentfolder = readMailboxParent($mailbox, $dm);
143 if((eregi("^inbox".quotemeta($dm), $mailbox)) ||
144 (ereg("^".$folder_prefix, $mailbox)) ||
145 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
146 $indent = $dm_count - (countCharInString($folder_prefix, $dm));
147 $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $indent);
148 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
149 } else {
150 $boxes[$g]["formatted"] = $mailbox;
151 }
152
153 $boxes[$g]["unformatted-dm"] = $mailbox;
154 if (substr($mailbox, -1) == $dm)
155 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
156 $boxes[$g]["unformatted"] = $mailbox;
157 $boxes[$g]["unformatted-disp"] = ereg_replace("^" . $folder_prefix, "", $mailbox);
158 $boxes[$g]["id"] = $g;
159
160 ereg("\(([^)]*)\)",$line[$g],$regs);
161 $flags = trim(strtolower(str_replace("\\", "",$regs[1])));
162 if ($flags) {
163 $boxes[$g]["flags"] = explode(" ", $flags);
164 }
165 }
166
167 return $boxes;
168 }
169
170 /* patch from dave_michmerhuizen@yahoo.com
171 * allows case insensativity when sorting folders
172 */
173 function _icmp ($a, $b) {
174 return strcasecmp($a, $b);
175 }
176
177 /******************************************************************************
178 ** Returns sorted mailbox lists in several different ways.
179 ** See comment on sqimap_mailbox_parse() for info about the returned array.
180 ******************************************************************************/
181 function sqimap_mailbox_list ($imap_stream) {
182 global $load_prefs_php, $prefs_php, $config_php;
183 global $data_dir, $username, $list_special_folders_first;
184 global $trash_folder, $sent_folder;
185 global $move_to_trash, $move_to_sent;
186
187 $inbox_in_list = false;
188 $inbox_subscribed = false;
189
190 if (!isset($load_prefs_php)) include "../src/load_prefs.php";
191 else global $folder_prefix;
192 if (!function_exists ("ary_sort")) include "../functions/array.php";
193
194 $dm = sqimap_get_delimiter ($imap_stream);
195
196 /** LSUB array **/
197 $inbox_subscribed = false;
198 fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n");
199 $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
200
201 for ($i=0;$i < count($lsub_ary); $i++) {
202 $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
203 if ($sorted_lsub_ary[$i] == "INBOX")
204 $inbox_subscribed = true;
205 }
206 $new_ary = array();
207 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
208 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
209 $new_ary[] = $sorted_lsub_ary[$i];
210 }
211 }
212 $sorted_lsub_ary = $new_ary;
213 if (isset($sorted_lsub_ary)) {
214 usort($sorted_lsub_ary, "_icmp");
215 //sort($sorted_lsub_ary);
216 }
217
218 /** LIST array **/
219 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
220 if (substr($sorted_lsub_ary[$i], -1) == $dm)
221 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
222 else
223 $mbx = $sorted_lsub_ary[$i];
224
225 fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n");
226 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
227 $sorted_list_ary[$i] = $read[0];
228 if (find_mailbox_name($sorted_list_ary[$i]) == "INBOX")
229 $inbox_in_list = true;
230 }
231
232 /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/
233 if ($inbox_subscribed == false || $inbox_in_list == false) {
234 fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
235 $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
236
237 $pos = count($sorted_list_ary);
238 $sorted_list_ary[$pos] = $inbox_ary[0];
239
240 $pos = count($sorted_lsub_ary);
241 $sorted_lsub_ary[$pos] = find_mailbox_name($inbox_ary[0]);
242 }
243
244 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm);
245
246
247 /** Now, lets sort for special folders **/
248
249 $boxesnew = Array();
250
251 // Find INBOX
252 for ($i = 0; $i < count($boxes); $i++) {
253 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
254 $boxesnew[] = $boxes[$i];
255 $boxes[$i]["used"] = true;
256 $i = count($boxes);
257 }
258 }
259
260 if ($list_special_folders_first == true) {
261
262 // Then list special folders and their subfolders
263 for ($i = 0 ; $i <= count($boxes) ; $i++) {
264 if((eregi("^".$trash_folder.'$', $boxes[$i]["unformatted"]) ||
265 eregi("^".$trash_folder.quotemeta($dm), $boxes[$i]["unformatted"]) ) &&
266 ($move_to_trash)) {
267 $boxesnew[] = $boxes[$i];
268 $boxes[$i]["used"] = true;
269 }
270 else if((eregi("^".$sent_folder.'$', $boxes[$i]["unformatted"]) ||
271 eregi("^".$sent_folder.quotemeta($dm), $boxes[$i]["unformatted"]) ) &&
272 ($move_to_sent)) {
273 $boxesnew[] = $boxes[$i];
274 $boxes[$i]["used"] = true;
275 }
276 }
277
278 // Put INBOX.* folders ahead of the rest
279 for ($i = 0; $i <= count($boxes); $i++) {
280 if (eregi("^inbox\.", $boxes[$i]["unformatted"]) &&
281 ($boxes[$i]["used"] == false)) {
282 $boxesnew[] = $boxes[$i];
283 $boxes[$i]["used"] = true;
284 }
285 }
286
287 }
288
289 // Rest of the folders
290 for ($i = 0; $i < count($boxes); $i++) {
291 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
292 ($boxes[$i]["used"] == false)) {
293 $boxesnew[] = $boxes[$i];
294 $boxes[$i]["used"] = true;
295 }
296 }
297
298 return $boxesnew;
299 }
300
301 /******************************************************************************
302 ** Returns a list of all folders, subscribed or not
303 ******************************************************************************/
304 function sqimap_mailbox_list_all ($imap_stream) {
305 global $list_special_folders_first, $folder_prefix;
306
307 if (!function_exists ("ary_sort"))
308 include ("../functions/array.php");
309
310 $dm = sqimap_get_delimiter ($imap_stream);
311
312 fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
313 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
314 $g = 0;
315 $phase = "inbox";
316
317 for ($i = 0; $i < count($read_ary); $i++) {
318 if (substr ($read_ary[$i], 0, 4) != "a001") {
319
320 // Store the raw IMAP reply
321 $boxes[$g]["raw"] = $read_ary[$i];
322
323 // Count number of delimiters ($dm) in folder name
324 $mailbox = find_mailbox_name($read_ary[$i]);
325 $dm_count = countCharInString($mailbox, $dm);
326 if (substr($mailbox, -1) == $dm)
327 $dm_count--; // If name ends in delimiter - decrement count by one
328
329 // Format folder name, but only if it's a INBOX.* or have
330 // a parent.
331 $boxesbyname[$mailbox] = $g;
332 $parentfolder = readMailboxParent($mailbox, $dm);
333 if((eregi("^inbox".quotemeta($dm), $mailbox)) ||
334 (ereg("^".$folder_prefix, $mailbox)) ||
335 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
336 $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $dm_count);
337 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
338 } else {
339 $boxes[$g]["formatted"] = $mailbox;
340 }
341
342 $boxes[$g]["unformatted-dm"] = $mailbox;
343 if (substr($mailbox, -1) == $dm)
344 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
345 $boxes[$g]["unformatted"] = $mailbox;
346 $boxes[$g]["unformatted-disp"] = ereg_replace("^" . $folder_prefix, "", $mailbox);
347 $boxes[$g]["id"] = $g;
348
349 /** Now lets get the flags for this mailbox **/
350 fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
351 $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
352
353 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
354 $flags = substr($flags, 0, strpos($flags, ")"));
355 $flags = str_replace("\\", "", $flags);
356 $flags = trim(strtolower($flags));
357 if ($flags) {
358 $boxes[$g]["flags"] = explode(" ", $flags);
359 }
360 }
361 $g++;
362 }
363 if(is_array($boxes)) {
364 $boxes = ary_sort ($boxes, "unformatted", 1);
365 }
366
367 return $boxes;
368 }
369
370 ?>