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