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