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