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