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