Cleaned up regexp's.
[squirrelmail.git] / functions / imap_mailbox.php
CommitLineData
59177427 1<?php
8e9e8afa 2 /**
3 ** imap_mailbox.php
4 **
5 ** This impliments all functions that manipulate mailboxes
245a6892 6 **
7 ** $Id$
8e9e8afa 8 **/
9
10 /******************************************************************************
11 ** Expunges a mailbox
12 ******************************************************************************/
a7f3c40d 13 function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = true) {
8e9e8afa 14 sqimap_mailbox_select ($imap_stream, $mailbox);
15 fputs ($imap_stream, "a001 EXPUNGE\r\n");
8cf653ad 16 $read = sqimap_read_data($imap_stream, "a001", $handle_errors, $response, $message);
8e9e8afa 17 }
18
19
20 /******************************************************************************
21 ** Checks whether or not the specified mailbox exists
22 ******************************************************************************/
23 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
26511b3e 24 fputs ($imap_stream, "a001 LIST \"\" \"$mailbox\"\r\n");
25 $mbx = sqimap_read_data($imap_stream, "a001", true, $response, $message);
23fd3c8e 26 if (isset($mailbox) && isset($mbx[0])) {
f9b3e5d9 27 return !!(ereg ("$mailbox", $mbx[0])); // To force into true/false
8e9e8afa 28 }
8e9e8afa 29 }
30
a2790a61 31 /******************************************************************************
8e9e8afa 32 ** Selects a mailbox
33 ******************************************************************************/
04632dbc 34 function sqimap_mailbox_select ($imap_stream, $mailbox, $hide=true, $recent=false) {
3751dc7f 35 global $auto_expunge;
36
8e9e8afa 37 fputs ($imap_stream, "a001 SELECT \"$mailbox\"\r\n");
2752bb16 38 $read = sqimap_read_data($imap_stream, "a001", true, $response, $message);
04632dbc 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 }
3751dc7f 47 if ($auto_expunge) {
48 fputs ($imap_stream, "a001 EXPUNGE\r\n");
8cf653ad 49 $tmp = sqimap_read_data($imap_stream, "a001", false, $a, $b);
3751dc7f 50 }
8e9e8afa 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) {
2752bb16 86 global $imap_server_type;
cbdc5621 87
8e9e8afa 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
5bdd7223 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
18a148f0 114 ** unformatted-disp - unformatted without $folder_prefix
5bdd7223 115 **
8e9e8afa 116 ******************************************************************************/
16530f8b 117 function sqimap_mailbox_parse ($line, $line_lsub, $dm) {
5bdd7223 118 global $folder_prefix;
119
120 // Process each folder line
8e9e8afa 121 for ($g=0; $g < count($line); $g++) {
5bdd7223 122
2752bb16 123 // Store the raw IMAP reply
a7ea7540 124 if (isset($line[$g]))
125 $boxes[$g]["raw"] = $line[$g];
126 else
127 $boxes[$g]["raw"] = "";
128
5bdd7223 129
2752bb16 130 // Count number of delimiters ($dm) in folder name
6477eb2d 131 $mailbox = trim($line_lsub[$g]);
8e9e8afa 132 $dm_count = countCharInString($mailbox, $dm);
133 if (substr($mailbox, -1) == $dm)
5bdd7223 134 $dm_count--; // If name ends in delimiter - decrement count by one
135
2752bb16 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((eregi("^inbox".quotemeta($dm), $mailbox)) ||
18a148f0 141 (ereg("^".$folder_prefix, $mailbox)) ||
2752bb16 142 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
143 $indent = $dm_count - (countCharInString($folder_prefix, $dm));
0debfed6 144 if ($indent)
145 $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $indent);
146 else
147 $boxes[$g]["formatted"] = '';
2752bb16 148 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
149 } else {
150 $boxes[$g]["formatted"] = $mailbox;
151 }
8e9e8afa 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;
2752bb16 157 $boxes[$g]["unformatted-disp"] = ereg_replace("^" . $folder_prefix, "", $mailbox);
8e9e8afa 158 $boxes[$g]["id"] = $g;
159
a7ea7540 160 if (isset($line[$g]))
f9b3e5d9 161 ereg("\(([^)]*)\)",$line[$g],$regs);
162 $flags = trim(strtolower(str_replace("\\", "",$regs[1])));
8e9e8afa 163 if ($flags) {
164 $boxes[$g]["flags"] = explode(" ", $flags);
165 }
8e9e8afa 166 }
5bdd7223 167
8e9e8afa 168 return $boxes;
169 }
1071ae95 170
171 /* Apparently you must call a user function with usort instead
172 * of calling a built-in directly. Stupid.
173 * Patch from dave_michmerhuizen@yahoo.com
174 * Allows case insensitivity when sorting folders
175 */
176 function user_strcasecmp($a, $b)
177 {
178 return strcasecmp($a, $b);
179 }
10359c65 180
6b8a49c9 181
8e9e8afa 182 /******************************************************************************
183 ** Returns sorted mailbox lists in several different ways.
5bdd7223 184 ** See comment on sqimap_mailbox_parse() for info about the returned array.
8e9e8afa 185 ******************************************************************************/
186 function sqimap_mailbox_list ($imap_stream) {
5bdd7223 187 global $load_prefs_php, $prefs_php, $config_php;
188 global $data_dir, $username, $list_special_folders_first;
6477eb2d 189 global $trash_folder, $sent_folder;
f9b3e5d9 190 global $move_to_trash, $move_to_sent;
8e9e8afa 191
1590a668 192 $inbox_in_list = false;
193 $inbox_subscribed = false;
194
8e9e8afa 195 if (!isset($load_prefs_php)) include "../src/load_prefs.php";
196 else global $folder_prefix;
197 if (!function_exists ("ary_sort")) include "../functions/array.php";
198
199 $dm = sqimap_get_delimiter ($imap_stream);
200
8e9e8afa 201 /** LSUB array **/
202 $inbox_subscribed = false;
6477eb2d 203 fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n");
8e9e8afa 204 $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
6477eb2d 205
18a148f0 206 /** OS: we don't want to parse last element of array, 'cause it is OK command, so we unset it **/
207 /** LUKE: This introduced errors.. do a check first **/
208 if (substr($lsub_ary[count($lsub_ary)-1], 0, 4) == "* OK") {
209 unset($lsub_ary[count($lsub_ary)-1]);
210 }
a3db804c 211
8e9e8afa 212 for ($i=0;$i < count($lsub_ary); $i++) {
213 $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
8e9e8afa 214 if ($sorted_lsub_ary[$i] == "INBOX")
215 $inbox_subscribed = true;
216 }
ee62ce13 217 $new_ary = array();
218 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
219 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
220 $new_ary[] = $sorted_lsub_ary[$i];
221 }
222 }
223 $sorted_lsub_ary = $new_ary;
8e9e8afa 224 if (isset($sorted_lsub_ary)) {
1071ae95 225 usort($sorted_lsub_ary, "user_strcasecmp");
10359c65 226 //sort($sorted_lsub_ary);
8e9e8afa 227 }
228
3cb866d7 229 /** LIST array **/
f9b3e5d9 230 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
231 if (substr($sorted_lsub_ary[$i], -1) == $dm)
232 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
233 else
234 $mbx = $sorted_lsub_ary[$i];
235
236 fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n");
237 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
a7ea7540 238 if (isset($sorted_list_ary[$i]))
239 $sorted_list_ary[$i] = "";
240 if (isset($read[0]))
f9b3e5d9 241 $sorted_list_ary[$i] = $read[0];
a7ea7540 242 else
243 $sorget_list_ary[$i] = "";
244 if (isset($sorted_list_ary[$i]) && find_mailbox_name($sorted_list_ary[$i]) == "INBOX")
3cb866d7 245 $inbox_in_list = true;
f9b3e5d9 246 }
2752bb16 247
8e9e8afa 248 /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/
249 if ($inbox_subscribed == false || $inbox_in_list == false) {
250 fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
251 $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
591000ec 252
253 $pos = count($sorted_list_ary);
254 $sorted_list_ary[$pos] = $inbox_ary[0];
255
256 $pos = count($sorted_lsub_ary);
257 $sorted_lsub_ary[$pos] = find_mailbox_name($inbox_ary[0]);
8e9e8afa 258 }
259
f9b3e5d9 260 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm);
261
5bdd7223 262
f9b3e5d9 263 /** Now, lets sort for special folders **/
5bdd7223 264
265 $boxesnew = Array();
266
267 // Find INBOX
8e9e8afa 268 for ($i = 0; $i < count($boxes); $i++) {
1e0628fb 269 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
5bdd7223 270 $boxesnew[] = $boxes[$i];
283db905 271 $used[$i] = true;
2752bb16 272 $i = count($boxes);
8e9e8afa 273 }
274 }
aed206bf 275
8e9e8afa 276 if ($list_special_folders_first == true) {
5bdd7223 277
2752bb16 278 // Then list special folders and their subfolders
245a6892 279 for ($i = 0 ; $i < count($boxes) ; $i++) {
23ed73eb 280 if ($move_to_trash &&
18a148f0 281 eregi("^" . quotemeta($trash_folder) . "(" .
282 quotemeta($dm) . ".*)?$", $boxes[$i]["unformatted"])) {
5bdd7223 283 $boxesnew[] = $boxes[$i];
283db905 284 $used[$i] = true;
1e0628fb 285 }
23ed73eb 286 elseif ($move_to_sent &&
18a148f0 287 eregi("^" . quotemeta($sent_folder) . "(" .
288 quotemeta($dm) . ".*)?$", $boxes[$i]["unformatted"])) {
5bdd7223 289 $boxesnew[] = $boxes[$i];
283db905 290 $used[$i] = true;
8e9e8afa 291 }
292 }
5bdd7223 293
2752bb16 294 // Put INBOX.* folders ahead of the rest
245a6892 295 for ($i = 0; $i < count($boxes); $i++) {
2752bb16 296 if (eregi("^inbox\.", $boxes[$i]["unformatted"]) &&
283db905 297 (!isset($used[$i]) || $used[$i] == false)) {
2752bb16 298 $boxesnew[] = $boxes[$i];
283db905 299 $used[$i] = true;
2752bb16 300 }
301 }
5bdd7223 302
8e9e8afa 303 }
c5eb2c03 304
5bdd7223 305 // Rest of the folders
8e9e8afa 306 for ($i = 0; $i < count($boxes); $i++) {
1e0628fb 307 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
283db905 308 (!isset($used[$i]) || $used[$i] == false)) {
5bdd7223 309 $boxesnew[] = $boxes[$i];
283db905 310 $used[$i] = true;
8e9e8afa 311 }
312 }
313
314 return $boxesnew;
315 }
8e9e8afa 316
317 /******************************************************************************
318 ** Returns a list of all folders, subscribed or not
319 ******************************************************************************/
320 function sqimap_mailbox_list_all ($imap_stream) {
1e0628fb 321 global $list_special_folders_first, $folder_prefix;
8e9e8afa 322
323 if (!function_exists ("ary_sort"))
324 include ("../functions/array.php");
325
326 $dm = sqimap_get_delimiter ($imap_stream);
327
328 fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
329 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
330 $g = 0;
331 $phase = "inbox";
5bdd7223 332
8e9e8afa 333 for ($i = 0; $i < count($read_ary); $i++) {
334 if (substr ($read_ary[$i], 0, 4) != "a001") {
5bdd7223 335
2752bb16 336 // Store the raw IMAP reply
8e9e8afa 337 $boxes[$g]["raw"] = $read_ary[$i];
338
2752bb16 339 // Count number of delimiters ($dm) in folder name
8e9e8afa 340 $mailbox = find_mailbox_name($read_ary[$i]);
341 $dm_count = countCharInString($mailbox, $dm);
342 if (substr($mailbox, -1) == $dm)
5bdd7223 343 $dm_count--; // If name ends in delimiter - decrement count by one
2752bb16 344
345 // Format folder name, but only if it's a INBOX.* or have
346 // a parent.
347 $boxesbyname[$mailbox] = $g;
348 $parentfolder = readMailboxParent($mailbox, $dm);
349 if((eregi("^inbox".quotemeta($dm), $mailbox)) ||
350 (ereg("^".$folder_prefix, $mailbox)) ||
351 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
0debfed6 352 if ($dm_count)
353 $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $dm_count);
354 else
355 $boxes[$g]["formatted"] = '';
2752bb16 356 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
357 } else {
358 $boxes[$g]["formatted"] = $mailbox;
359 }
8e9e8afa 360
361 $boxes[$g]["unformatted-dm"] = $mailbox;
362 if (substr($mailbox, -1) == $dm)
363 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
364 $boxes[$g]["unformatted"] = $mailbox;
2752bb16 365 $boxes[$g]["unformatted-disp"] = ereg_replace("^" . $folder_prefix, "", $mailbox);
8e9e8afa 366 $boxes[$g]["id"] = $g;
367
368 /** Now lets get the flags for this mailbox **/
369 fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
370 $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
371
372 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
373 $flags = substr($flags, 0, strpos($flags, ")"));
374 $flags = str_replace("\\", "", $flags);
375 $flags = trim(strtolower($flags));
376 if ($flags) {
377 $boxes[$g]["flags"] = explode(" ", $flags);
378 }
379 }
380 $g++;
381 }
5bdd7223 382 if(is_array($boxes)) {
591000ec 383 $boxes = ary_sort ($boxes, "unformatted", 1);
384 }
5bdd7223 385
8e9e8afa 386 return $boxes;
387 }
388
052e0c26 389?>