Apparently usort() REQUIRES a user-defined function instead of a PHP
[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 fputs ($imap_stream, "a001 LIST \"\" \"$mailbox\"\r\n");
25 $mbx = sqimap_read_data($imap_stream, "a001", true, $response, $message);
26 if (isset($mailbox) && isset($mbx[0])) {
27 return !!(ereg ("$mailbox", $mbx[0])); // To force into true/false
28 }
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 $boxes[$g]["raw"] = $line[$g];
125
126 // Count number of delimiters ($dm) in folder name
127 $mailbox = trim($line_lsub[$g]);
128 $dm_count = countCharInString($mailbox, $dm);
129 if (substr($mailbox, -1) == $dm)
130 $dm_count--; // If name ends in delimiter - decrement count by one
131
132 // Format folder name, but only if it's a INBOX.* or have
133 // a parent.
134 $boxesbyname[$mailbox] = $g;
135 $parentfolder = readMailboxParent($mailbox, $dm);
136 if((eregi("^inbox".quotemeta($dm), $mailbox)) ||
137 (ereg("^".$folder_prefix, $mailbox)) ||
138 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
139 $indent = $dm_count - (countCharInString($folder_prefix, $dm));
140 if ($indent)
141 $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $indent);
142 else
143 $boxes[$g]["formatted"] = '';
144 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
145 } else {
146 $boxes[$g]["formatted"] = $mailbox;
147 }
148
149 $boxes[$g]["unformatted-dm"] = $mailbox;
150 if (substr($mailbox, -1) == $dm)
151 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
152 $boxes[$g]["unformatted"] = $mailbox;
153 $boxes[$g]["unformatted-disp"] = ereg_replace("^" . $folder_prefix, "", $mailbox);
154 $boxes[$g]["id"] = $g;
155
156 ereg("\(([^)]*)\)",$line[$g],$regs);
157 $flags = trim(strtolower(str_replace("\\", "",$regs[1])));
158 if ($flags) {
159 $boxes[$g]["flags"] = explode(" ", $flags);
160 }
161 }
162
163 return $boxes;
164 }
165
166 /* Apparently you must call a user function with usort instead
167 * of calling a built-in directly. Stupid.
168 * Patch from dave_michmerhuizen@yahoo.com
169 * Allows case insensitivity when sorting folders
170 */
171 function user_strcasecmp($a, $b)
172 {
173 return strcasecmp($a, $b);
174 }
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 /** OS: we don't want to parse last element of array, 'cause it is OK command, so we unset it **/
202 unset($lsub_ary[count($lsub_ary)-1]);
203
204 for ($i=0;$i < count($lsub_ary); $i++) {
205 $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
206 if ($sorted_lsub_ary[$i] == "INBOX")
207 $inbox_subscribed = true;
208 }
209 $new_ary = array();
210 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
211 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
212 $new_ary[] = $sorted_lsub_ary[$i];
213 }
214 }
215 $sorted_lsub_ary = $new_ary;
216 if (isset($sorted_lsub_ary)) {
217 usort($sorted_lsub_ary, "user_strcasecmp");
218 //sort($sorted_lsub_ary);
219 }
220
221 /** LIST array **/
222 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
223 if (substr($sorted_lsub_ary[$i], -1) == $dm)
224 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
225 else
226 $mbx = $sorted_lsub_ary[$i];
227
228 fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n");
229 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
230 $sorted_list_ary[$i] = $read[0];
231 if (find_mailbox_name($sorted_list_ary[$i]) == "INBOX")
232 $inbox_in_list = true;
233 }
234
235 /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/
236 if ($inbox_subscribed == false || $inbox_in_list == false) {
237 fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
238 $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
239
240 $pos = count($sorted_list_ary);
241 $sorted_list_ary[$pos] = $inbox_ary[0];
242
243 $pos = count($sorted_lsub_ary);
244 $sorted_lsub_ary[$pos] = find_mailbox_name($inbox_ary[0]);
245 }
246
247 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm);
248
249
250 /** Now, lets sort for special folders **/
251
252 $boxesnew = Array();
253
254 // Find INBOX
255 for ($i = 0; $i < count($boxes); $i++) {
256 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
257 $boxesnew[] = $boxes[$i];
258 $used[$i] = true;
259 $i = count($boxes);
260 }
261 }
262
263 if ($list_special_folders_first == true) {
264
265 // Then list special folders and their subfolders
266 for ($i = 0 ; $i < count($boxes) ; $i++) {
267 if ($move_to_trash &&
268 eregi("^" . quotemeta($trash_folder) . "(" .
269 quotemeta($dm) . ".*)?$", $boxes[$i]["unformatted"])) {
270 $boxesnew[] = $boxes[$i];
271 $used[$i] = true;
272 }
273 elseif ($move_to_sent &&
274 eregi("^" . quotemeta($sent_folder) . "(" .
275 quotemeta($dm) . ".*)?$", $boxes[$i]["unformatted"])) {
276 $boxesnew[] = $boxes[$i];
277 $used[$i] = true;
278 }
279 }
280
281 // Put INBOX.* folders ahead of the rest
282 for ($i = 0; $i < count($boxes); $i++) {
283 if (eregi("^inbox\.", $boxes[$i]["unformatted"]) &&
284 (!isset($used[$i]) || $used[$i] == false)) {
285 $boxesnew[] = $boxes[$i];
286 $used[$i] = true;
287 }
288 }
289
290 }
291
292 // Rest of the folders
293 for ($i = 0; $i < count($boxes); $i++) {
294 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
295 (!isset($used[$i]) || $used[$i] == false)) {
296 $boxesnew[] = $boxes[$i];
297 $used[$i] = true;
298 }
299 }
300
301 return $boxesnew;
302 }
303
304 /******************************************************************************
305 ** Returns a list of all folders, subscribed or not
306 ******************************************************************************/
307 function sqimap_mailbox_list_all ($imap_stream) {
308 global $list_special_folders_first, $folder_prefix;
309
310 if (!function_exists ("ary_sort"))
311 include ("../functions/array.php");
312
313 $dm = sqimap_get_delimiter ($imap_stream);
314
315 fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
316 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
317 $g = 0;
318 $phase = "inbox";
319
320 for ($i = 0; $i < count($read_ary); $i++) {
321 if (substr ($read_ary[$i], 0, 4) != "a001") {
322
323 // Store the raw IMAP reply
324 $boxes[$g]["raw"] = $read_ary[$i];
325
326 // Count number of delimiters ($dm) in folder name
327 $mailbox = find_mailbox_name($read_ary[$i]);
328 $dm_count = countCharInString($mailbox, $dm);
329 if (substr($mailbox, -1) == $dm)
330 $dm_count--; // If name ends in delimiter - decrement count by one
331
332 // Format folder name, but only if it's a INBOX.* or have
333 // a parent.
334 $boxesbyname[$mailbox] = $g;
335 $parentfolder = readMailboxParent($mailbox, $dm);
336 if((eregi("^inbox".quotemeta($dm), $mailbox)) ||
337 (ereg("^".$folder_prefix, $mailbox)) ||
338 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
339 if ($dm_count)
340 $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $dm_count);
341 else
342 $boxes[$g]["formatted"] = '';
343 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
344 } else {
345 $boxes[$g]["formatted"] = $mailbox;
346 }
347
348 $boxes[$g]["unformatted-dm"] = $mailbox;
349 if (substr($mailbox, -1) == $dm)
350 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
351 $boxes[$g]["unformatted"] = $mailbox;
352 $boxes[$g]["unformatted-disp"] = ereg_replace("^" . $folder_prefix, "", $mailbox);
353 $boxes[$g]["id"] = $g;
354
355 /** Now lets get the flags for this mailbox **/
356 fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
357 $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
358
359 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
360 $flags = substr($flags, 0, strpos($flags, ")"));
361 $flags = str_replace("\\", "", $flags);
362 $flags = trim(strtolower($flags));
363 if ($flags) {
364 $boxes[$g]["flags"] = explode(" ", $flags);
365 }
366 }
367 $g++;
368 }
369 if(is_array($boxes)) {
370 $boxes = ary_sort ($boxes, "unformatted", 1);
371 }
372
373 return $boxes;
374 }
375
376 ?>