Sorry, a bug slipped into the CVS.
[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
f435778e 10 if (defined ('imap_mailbox_php'))
11 return;
12 define ('imap_mailbox_php', true);
13
8e9e8afa 14 /******************************************************************************
15 ** Expunges a mailbox
16 ******************************************************************************/
a7f3c40d 17 function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = true) {
8e9e8afa 18 fputs ($imap_stream, "a001 EXPUNGE\r\n");
8cf653ad 19 $read = sqimap_read_data($imap_stream, "a001", $handle_errors, $response, $message);
8e9e8afa 20 }
21
22
23 /******************************************************************************
24 ** Checks whether or not the specified mailbox exists
25 ******************************************************************************/
26 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
146e0c45 27 if (! isset($mailbox))
28 return false;
26511b3e 29 fputs ($imap_stream, "a001 LIST \"\" \"$mailbox\"\r\n");
30 $mbx = sqimap_read_data($imap_stream, "a001", true, $response, $message);
146e0c45 31 return isset($mbx[0]);
8e9e8afa 32 }
33
a2790a61 34 /******************************************************************************
8e9e8afa 35 ** Selects a mailbox
36 ******************************************************************************/
04632dbc 37 function sqimap_mailbox_select ($imap_stream, $mailbox, $hide=true, $recent=false) {
3751dc7f 38 global $auto_expunge;
39
8e9e8afa 40 fputs ($imap_stream, "a001 SELECT \"$mailbox\"\r\n");
2752bb16 41 $read = sqimap_read_data($imap_stream, "a001", true, $response, $message);
04632dbc 42 if ($recent) {
43 for ($i=0; $i<count($read); $i++) {
44 if (strpos(strtolower($read[$i]), "recent")) {
45 $r = explode(" ", $read[$i]);
46 }
47 }
48 return $r[1];
49 }
3751dc7f 50 if ($auto_expunge) {
51 fputs ($imap_stream, "a001 EXPUNGE\r\n");
8cf653ad 52 $tmp = sqimap_read_data($imap_stream, "a001", false, $a, $b);
3751dc7f 53 }
8e9e8afa 54 }
55
56
57
58 /******************************************************************************
59 ** Creates a folder
60 ******************************************************************************/
61 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
62 if (strtolower($type) == "noselect") {
63 $dm = sqimap_get_delimiter($imap_stream);
64 $mailbox = $mailbox.$dm;
65 }
66 fputs ($imap_stream, "a001 CREATE \"$mailbox\"\r\n");
67 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
68
69 sqimap_subscribe ($imap_stream, $mailbox);
70 }
71
72
73
74 /******************************************************************************
75 ** Subscribes to an existing folder
76 ******************************************************************************/
77 function sqimap_subscribe ($imap_stream, $mailbox) {
78 fputs ($imap_stream, "a001 SUBSCRIBE \"$mailbox\"\r\n");
79 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
80 }
81
82
83
84
85 /******************************************************************************
86 ** Unsubscribes to an existing folder
87 ******************************************************************************/
88 function sqimap_unsubscribe ($imap_stream, $mailbox) {
2752bb16 89 global $imap_server_type;
cbdc5621 90
8e9e8afa 91 fputs ($imap_stream, "a001 UNSUBSCRIBE \"$mailbox\"\r\n");
92 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
93 }
94
95
96
97
98 /******************************************************************************
99 ** This function simply deletes the given folder
100 ******************************************************************************/
101 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
102 fputs ($imap_stream, "a001 DELETE \"$mailbox\"\r\n");
103 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
104 sqimap_unsubscribe ($imap_stream, $mailbox);
105 }
8d636967 106
107 /***********************************************************************
108 ** Determines if the user is subscribed to the folder or not
109 **********************************************************************/
110 function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
111 $boxes = sqimap_mailbox_list ($imap_stream);
112 foreach ($boxes as $ref) {
113 if ($ref['unformatted'] == $folder)
114 return true;
115 }
116 return false;
117 }
118
8e9e8afa 119
120
121 /******************************************************************************
122 ** Formats a mailbox into 4 parts for the $boxes array
5bdd7223 123 **
124 ** The four parts are:
125 **
126 ** raw - Raw LIST/LSUB response from the IMAP server
127 ** formatted - nicely formatted folder name
128 ** unformatted - unformatted, but with delimiter at end removed
129 ** unformatted-dm - folder name as it appears in raw response
18a148f0 130 ** unformatted-disp - unformatted without $folder_prefix
5bdd7223 131 **
8e9e8afa 132 ******************************************************************************/
16530f8b 133 function sqimap_mailbox_parse ($line, $line_lsub, $dm) {
5bdd7223 134 global $folder_prefix;
135
136 // Process each folder line
8e9e8afa 137 for ($g=0; $g < count($line); $g++) {
5bdd7223 138
2752bb16 139 // Store the raw IMAP reply
a7ea7540 140 if (isset($line[$g]))
141 $boxes[$g]["raw"] = $line[$g];
142 else
143 $boxes[$g]["raw"] = "";
144
5bdd7223 145
2752bb16 146 // Count number of delimiters ($dm) in folder name
6477eb2d 147 $mailbox = trim($line_lsub[$g]);
8e9e8afa 148 $dm_count = countCharInString($mailbox, $dm);
149 if (substr($mailbox, -1) == $dm)
5bdd7223 150 $dm_count--; // If name ends in delimiter - decrement count by one
151
2752bb16 152 // Format folder name, but only if it's a INBOX.* or have
153 // a parent.
154 $boxesbyname[$mailbox] = $g;
155 $parentfolder = readMailboxParent($mailbox, $dm);
36dfb0c9 156 if((strtolower(substr($mailbox, 0, 5)) == "inbox") ||
157 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
158 (isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
2752bb16 159 $indent = $dm_count - (countCharInString($folder_prefix, $dm));
b1275991 160 if ($indent > 0)
0debfed6 161 $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $indent);
162 else
163 $boxes[$g]["formatted"] = '';
2752bb16 164 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
165 } else {
166 $boxes[$g]["formatted"] = $mailbox;
167 }
8e9e8afa 168
146e0c45 169 $boxes[$g]['unformatted-dm'] = $mailbox;
8e9e8afa 170 if (substr($mailbox, -1) == $dm)
171 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
146e0c45 172 $boxes[$g]['unformatted'] = $mailbox;
20eb2621 173 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix)
631b9da3 174 $mailbox = substr($mailbox, strlen($folder_prefix));
175 $boxes[$g]['unformatted-disp'] = $mailbox;
146e0c45 176 $boxes[$g]['id'] = $g;
8e9e8afa 177
1a7e1e97 178 $boxes[$g]['flags'] = array();
179 if (isset($line[$g])) {
36dfb0c9 180 ereg("\(([^)]*)\)",$line[$g],$regs);
1a7e1e97 181 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
182 if ($flags)
183 $boxes[$g]['flags'] = explode(' ', $flags);
184 }
8e9e8afa 185 }
5bdd7223 186
8e9e8afa 187 return $boxes;
188 }
1071ae95 189
190 /* Apparently you must call a user function with usort instead
191 * of calling a built-in directly. Stupid.
192 * Patch from dave_michmerhuizen@yahoo.com
193 * Allows case insensitivity when sorting folders
194 */
195 function user_strcasecmp($a, $b)
196 {
197 return strcasecmp($a, $b);
198 }
10359c65 199
6b8a49c9 200
8e9e8afa 201 /******************************************************************************
202 ** Returns sorted mailbox lists in several different ways.
5bdd7223 203 ** See comment on sqimap_mailbox_parse() for info about the returned array.
8e9e8afa 204 ******************************************************************************/
205 function sqimap_mailbox_list ($imap_stream) {
5bdd7223 206 global $data_dir, $username, $list_special_folders_first;
6477eb2d 207 global $trash_folder, $sent_folder;
f435778e 208 global $move_to_trash, $move_to_sent, $folder_prefix;
8e9e8afa 209
1590a668 210 $inbox_in_list = false;
211 $inbox_subscribed = false;
212
38dc768c 213 require_once('../src/load_prefs.php');
214 require_once('../functions/array.php');
8e9e8afa 215
216 $dm = sqimap_get_delimiter ($imap_stream);
217
8e9e8afa 218 /** LSUB array **/
44a3ca20 219 fputs ($imap_stream, "a001 LSUB \"$folder_prefix\" \"*\"\r\n");
8e9e8afa 220 $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
44a3ca20 221
222 // Section about removing the last element was removed
223 // We don't return "* OK" anymore from sqimap_read_data
6477eb2d 224
44a3ca20 225 $sorted_lsub_ary = array();
8e9e8afa 226 for ($i=0;$i < count($lsub_ary); $i++) {
44a3ca20 227 // Workaround for EIMS
228 // Doesn't work if the mailbox name is multiple lines
229 if (isset($lsub_ary[$i + 1]) &&
230 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
231 $lsub_ary[$i], $regs)) {
232 $i ++;
233 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) .
234 '"' . $regs[2];
235 }
236 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
237 $sorted_lsub_ary[] = $temp_mailbox_name;
238 if (strtoupper($temp_mailbox_name) == 'INBOX')
8e9e8afa 239 $inbox_subscribed = true;
240 }
ee62ce13 241 $new_ary = array();
242 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
243 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
244 $new_ary[] = $sorted_lsub_ary[$i];
245 }
246 }
247 $sorted_lsub_ary = $new_ary;
8e9e8afa 248 if (isset($sorted_lsub_ary)) {
44a3ca20 249 usort($sorted_lsub_ary, 'user_strcasecmp');
10359c65 250 //sort($sorted_lsub_ary);
8e9e8afa 251 }
252
3cb866d7 253 /** LIST array **/
44a3ca20 254 $sorted_list_ary = array();
f9b3e5d9 255 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
256 if (substr($sorted_lsub_ary[$i], -1) == $dm)
257 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
258 else
259 $mbx = $sorted_lsub_ary[$i];
260
261 fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n");
262 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
44a3ca20 263 // Another workaround for EIMS
264 if (isset($read[1]) &&
265 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
266 $read[0], $regs)) {
267 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
268 '"' . $regs[2];
269 }
a7ea7540 270 if (isset($sorted_list_ary[$i]))
271 $sorted_list_ary[$i] = "";
272 if (isset($read[0]))
44a3ca20 273 $sorted_list_ary[$i] = $read[0];
a7ea7540 274 else
44a3ca20 275 $sorted_list_ary[$i] = "";
276 if (isset($sorted_list_ary[$i]) &&
277 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == "INBOX")
3cb866d7 278 $inbox_in_list = true;
f9b3e5d9 279 }
2752bb16 280
44a3ca20 281 /** Just in case they're not subscribed to their inbox, we'll get it
282 for them anyway **/
8e9e8afa 283 if ($inbox_subscribed == false || $inbox_in_list == false) {
284 fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
285 $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
44a3ca20 286 // Another workaround for EIMS
287 if (isset($inbox_ary[1]) &&
288 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
289 $inbox_ary[0], $regs)) {
290 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
291 '"' . $regs[2];
292 }
293
294 $sorted_list_ary[] = $inbox_ary[0];
295 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
8e9e8afa 296 }
297
f9b3e5d9 298 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm);
299
5bdd7223 300
f9b3e5d9 301 /** Now, lets sort for special folders **/
5bdd7223 302
303 $boxesnew = Array();
304
305 // Find INBOX
8e9e8afa 306 for ($i = 0; $i < count($boxes); $i++) {
1e0628fb 307 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
5bdd7223 308 $boxesnew[] = $boxes[$i];
283db905 309 $used[$i] = true;
2752bb16 310 $i = count($boxes);
8e9e8afa 311 }
312 }
aed206bf 313
8e9e8afa 314 if ($list_special_folders_first == true) {
5bdd7223 315
2752bb16 316 // Then list special folders and their subfolders
245a6892 317 for ($i = 0 ; $i < count($boxes) ; $i++) {
23ed73eb 318 if ($move_to_trash &&
146e0c45 319 eregi('^' . quotemeta($trash_folder) . '(' .
320 quotemeta($dm) . '.*)?$', $boxes[$i]["unformatted"])) {
5bdd7223 321 $boxesnew[] = $boxes[$i];
283db905 322 $used[$i] = true;
1e0628fb 323 }
23ed73eb 324 elseif ($move_to_sent &&
146e0c45 325 eregi('^' . quotemeta($sent_folder) . '(' .
326 quotemeta($dm) . '.*)?$', $boxes[$i]["unformatted"])) {
5bdd7223 327 $boxesnew[] = $boxes[$i];
283db905 328 $used[$i] = true;
8e9e8afa 329 }
330 }
5bdd7223 331
2752bb16 332 // Put INBOX.* folders ahead of the rest
245a6892 333 for ($i = 0; $i < count($boxes); $i++) {
146e0c45 334 if (eregi('^inbox\\.', $boxes[$i]["unformatted"]) &&
283db905 335 (!isset($used[$i]) || $used[$i] == false)) {
2752bb16 336 $boxesnew[] = $boxes[$i];
283db905 337 $used[$i] = true;
2752bb16 338 }
339 }
5bdd7223 340
8e9e8afa 341 }
c5eb2c03 342
5bdd7223 343 // Rest of the folders
8e9e8afa 344 for ($i = 0; $i < count($boxes); $i++) {
1e0628fb 345 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
283db905 346 (!isset($used[$i]) || $used[$i] == false)) {
5bdd7223 347 $boxesnew[] = $boxes[$i];
283db905 348 $used[$i] = true;
8e9e8afa 349 }
350 }
351
352 return $boxesnew;
353 }
8e9e8afa 354
355 /******************************************************************************
356 ** Returns a list of all folders, subscribed or not
357 ******************************************************************************/
358 function sqimap_mailbox_list_all ($imap_stream) {
1e0628fb 359 global $list_special_folders_first, $folder_prefix;
8e9e8afa 360
38dc768c 361 if (!function_exists ("ary_sort"))
362 include_once('../functions/array.php');
363
8e9e8afa 364 $dm = sqimap_get_delimiter ($imap_stream);
365
366 fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
367 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
368 $g = 0;
369 $phase = "inbox";
5bdd7223 370
8e9e8afa 371 for ($i = 0; $i < count($read_ary); $i++) {
44a3ca20 372 // Another workaround for EIMS
373 if (isset($read_ary[$i + 1]) &&
374 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
375 $read_ary[$i], $regs)) {
376 $i ++;
377 $read_ary[$i] = $regs[1] . '"' .
378 addslashes(trim($read_ary[$i])) .
379 '"' . $regs[2];
380 }
8e9e8afa 381 if (substr ($read_ary[$i], 0, 4) != "a001") {
5bdd7223 382
2752bb16 383 // Store the raw IMAP reply
8e9e8afa 384 $boxes[$g]["raw"] = $read_ary[$i];
385
2752bb16 386 // Count number of delimiters ($dm) in folder name
8e9e8afa 387 $mailbox = find_mailbox_name($read_ary[$i]);
388 $dm_count = countCharInString($mailbox, $dm);
389 if (substr($mailbox, -1) == $dm)
5bdd7223 390 $dm_count--; // If name ends in delimiter - decrement count by one
2752bb16 391
392 // Format folder name, but only if it's a INBOX.* or have
393 // a parent.
394 $boxesbyname[$mailbox] = $g;
395 $parentfolder = readMailboxParent($mailbox, $dm);
146e0c45 396 if((eregi('^inbox'.quotemeta($dm), $mailbox)) ||
397 (ereg('^'.$folder_prefix, $mailbox)) ||
2752bb16 398 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
0debfed6 399 if ($dm_count)
400 $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $dm_count);
401 else
402 $boxes[$g]["formatted"] = '';
2752bb16 403 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
404 } else {
405 $boxes[$g]["formatted"] = $mailbox;
406 }
8e9e8afa 407
408 $boxes[$g]["unformatted-dm"] = $mailbox;
409 if (substr($mailbox, -1) == $dm)
410 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
411 $boxes[$g]["unformatted"] = $mailbox;
146e0c45 412 $boxes[$g]["unformatted-disp"] = ereg_replace('^' . $folder_prefix, '', $mailbox);
8e9e8afa 413 $boxes[$g]["id"] = $g;
414
415 /** Now lets get the flags for this mailbox **/
416 fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
417 $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
44a3ca20 418 // Another workaround for EIMS
419 if (isset($read_mlbx[1]) &&
420 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
421 $read_mlbx[0], $regs)) {
422 $read_mlbx[0] = $regs[1] . '"' .
423 addslashes(trim($read_mlbx[1])) .
424 '"' . $regs[2];
425 }
8e9e8afa 426
427 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
428 $flags = substr($flags, 0, strpos($flags, ")"));
146e0c45 429 $flags = str_replace('\\', '', $flags);
8e9e8afa 430 $flags = trim(strtolower($flags));
431 if ($flags) {
4afa7206 432 $boxes[$g]['flags'] = explode(" ", $flags);
8e9e8afa 433 }
12d61439 434 else
435 {
436 $boxes[$g]['flags'] = array();
437 }
8e9e8afa 438 }
439 $g++;
440 }
5bdd7223 441 if(is_array($boxes)) {
591000ec 442 $boxes = ary_sort ($boxes, "unformatted", 1);
443 }
5bdd7223 444
8e9e8afa 445 return $boxes;
446 }
447
0fc2aca0 448?>