Fixed bug #497181
[squirrelmail.git] / functions / imap_mailbox.php
1 <?php
2
3 /**
4 * imap_mailbox.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project 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 */
13
14 /*************************
15 ** Expunges a mailbox **
16 *************************/
17 function 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 ******************************************************************************/
28 function 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 ******************************************************************************/
42 function 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++) {
56 if (strpos(strtolower($read[$i]), 'recent')) {
57 $r = explode(' ', $read[$i]);
58 }
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 ******************************************************************************/
74 function 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 ******************************************************************************/
92 function 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 ******************************************************************************/
104 function 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 ******************************************************************************/
118 function 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 **********************************************************************/
129 function 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 ******************************************************************************/
154 function 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])) {
163 $boxes[$g]["raw"] = $line[$g];
164 }
165 else {
166 $boxes[$g]["raw"] = "";
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) ) ) {
186 $indent = $dm_count - (countCharInString($folder_prefix, $delimiter));
187 if ($indent > 0) {
188 $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $indent);
189 }
190 else {
191 $boxes[$g]["formatted"] = '';
192 }
193 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $delimiter);
194 }
195 else {
196 $boxes[$g]["formatted"] = $mailbox;
197 }
198
199 $boxes[$g]['unformatted-dm'] = $mailbox;
200 if (substr($mailbox, -1) == $delimiter) {
201 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
202 }
203 $boxes[$g]['unformatted'] = $mailbox;
204 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
205 $mailbox = substr($mailbox, strlen($folder_prefix));
206 }
207 $boxes[$g]['unformatted-disp'] = $mailbox;
208 $boxes[$g]['id'] = $g;
209
210 $boxes[$g]['flags'] = array();
211 if (isset($line[$g])) {
212 ereg("\(([^)]*)\)",$line[$g],$regs);
213 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
214 if ($flags) {
215 $boxes[$g]['flags'] = explode(' ', $flags);
216 }
217 }
218 }
219
220 return $boxes;
221 }
222
223 /**
224 * Sorting function used to sort mailbox names.
225 * + Original patch from dave_michmerhuizen@yahoo.com
226 * + Allows case insensitivity when sorting folders
227 * + Takes care of the delimiter being sorted to the end, causing
228 * subfolders to be listed in below folders that are prefixed
229 * with their parent folders name.
230 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
231 * Without special sort function: foobar between foo and foo.bar
232 * With special sort function: foobar AFTER foo and foo.bar :)
233 */
234 function user_strcasecmp($a, $b) {
235 global $delimiter;
236
237 /* Calculate the length of some strings. */
238 $a_length = strlen($a);
239 $b_length = strlen($b);
240 $min_length = min($a_length, $b_length);
241 $delimiter_length = strlen($delimiter);
242
243 /* Set the initial result value. */
244 $result = 0;
245
246 /* Check the strings... */
247 for ($c = 0; $c < $min_length; ++$c) {
248 $a_del = substr($a, $c, $delimiter_length);
249 $b_del = substr($b, $c, $delimiter_length);
250
251 if (($a_del == $delimiter) && ($b_del == $delimiter)) {
252 $result = 0;
253 } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
254 $result = -1;
255 } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
256 $result = 1;
257 } else {
258 $result = strcasecmp($a{$c}, $b{$c});
259 }
260
261 if ($result != 0) {
262 break;
263 }
264 }
265
266 /* If one string is a prefix of the other... */
267 if ($result == 0) {
268 if ($a_length < $b_length) {
269 $result = -1;
270 } else if ($a_length > $b_length) {
271 $result = 1;
272 }
273 }
274
275 return ($result);
276 }
277
278
279 /******************************************************************************
280 ** Returns sorted mailbox lists in several different ways.
281 ** See comment on sqimap_mailbox_parse() for info about the returned array.
282 ******************************************************************************/
283 function sqimap_mailbox_list ($imap_stream) {
284 global $data_dir, $username, $list_special_folders_first;
285 global $folder_prefix, $trash_folder, $sent_folder, $draft_folder;
286 global $move_to_trash, $move_to_sent, $save_as_draft;
287 global $delimiter;
288
289 $inbox_in_list = false;
290 $inbox_subscribed = false;
291
292 require_once('../src/load_prefs.php');
293 require_once('../functions/array.php');
294
295 /** LSUB array **/
296 fputs ($imap_stream, sqimap_session_id() .
297 " LSUB \"$folder_prefix\" \"*\"\r\n");
298 $lsub_ary = sqimap_read_data ($imap_stream, sqimap_session_id(),
299 true, $response, $message);
300
301 /* Section about removing the last element was removed */
302 /* We don't return "* OK" anymore from sqimap_read_data */
303
304 $sorted_lsub_ary = array();
305 for ($i=0;$i < count($lsub_ary); $i++) {
306 /* Workaround for EIMS */
307 /* Doesn't work if the mailbox name is multiple lines */
308 if (isset($lsub_ary[$i + 1]) &&
309 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
310 $lsub_ary[$i], $regs)) {
311 $i ++;
312 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) .
313 '"' . $regs[2];
314 }
315 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
316 $sorted_lsub_ary[] = $temp_mailbox_name;
317 if (strtoupper($temp_mailbox_name) == 'INBOX') {
318 $inbox_subscribed = true;
319 }
320 }
321 $new_ary = array();
322 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
323 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
324 $new_ary[] = $sorted_lsub_ary[$i];
325 }
326 }
327 $sorted_lsub_ary = $new_ary;
328 if (isset($sorted_lsub_ary)) {
329 usort($sorted_lsub_ary, 'user_strcasecmp');
330 }
331
332 /** LIST array **/
333 $sorted_list_ary = array();
334 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
335 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
336 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
337 }
338 else {
339 $mbx = $sorted_lsub_ary[$i];
340 }
341
342 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mbx\"\r\n");
343 $read = sqimap_read_data ($imap_stream, sqimap_session_id(),
344 true, $response, $message);
345 /* Another workaround for EIMS */
346 if (isset($read[1]) &&
347 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
348 $read[0], $regs)) {
349 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
350 '"' . $regs[2];
351 }
352
353 if (isset($sorted_list_ary[$i])) {
354 $sorted_list_ary[$i] = "";
355 }
356
357 if (isset($read[0])) {
358 $sorted_list_ary[$i] = $read[0];
359 }
360 else {
361 $sorted_list_ary[$i] = "";
362 }
363
364 if (isset($sorted_list_ary[$i]) &&
365 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == "INBOX") {
366 $inbox_in_list = true;
367 }
368 }
369
370 /**
371 * Just in case they're not subscribed to their inbox,
372 * we'll get it for them anyway
373 */
374 if ($inbox_subscribed == false || $inbox_in_list == false) {
375 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"INBOX\"\r\n");
376 $inbox_ary = sqimap_read_data ($imap_stream, sqimap_session_id(),
377 true, $response, $message);
378 /* Another workaround for EIMS */
379 if (isset($inbox_ary[1]) &&
380 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
381 $inbox_ary[0], $regs)) {
382 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
383 '"' . $regs[2];
384 }
385
386 $sorted_list_ary[] = $inbox_ary[0];
387 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
388 }
389
390 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
391
392 /** Now, lets sort for special folders **/
393 $boxesnew = Array();
394
395 /* Find INBOX */
396 for ($i = 0; $i < count($boxes); $i++) {
397 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
398 $boxesnew[] = $boxes[$i];
399 $used[$i] = true;
400 $i = count($boxes);
401 }
402 }
403
404 /* List special folders and their subfolders, if requested. */
405 if ($list_special_folders_first == true) {
406 /* First list the trash folder. */
407 for ($i = 0 ; $i < count($boxes) ; $i++) {
408 if ($move_to_trash &&
409 eregi('^' . quotemeta($trash_folder) . '(' .
410 quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
411 $boxesnew[] = $boxes[$i];
412 $used[$i] = true;
413 }
414 }
415
416 /* Then list the sent folder. */
417 for ($i = 0 ; $i < count($boxes) ; $i++) {
418 if ($move_to_sent &&
419 eregi('^' . quotemeta($sent_folder) . '(' .
420 quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
421 $boxesnew[] = $boxes[$i];
422 $used[$i] = true;
423 }
424 }
425
426 /* Lastly, list the list the draft folder. */
427 for ($i = 0 ; $i < count($boxes) ; $i++) {
428 if ($save_as_draft &&
429 eregi('^' . quotemeta($draft_folder) . '(' .
430 quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
431 $boxesnew[] = $boxes[$i];
432 $used[$i] = true;
433 }
434 }
435
436 /* Put INBOX.* folders ahead of the rest. */
437 for ($i = 0; $i < count($boxes); $i++) {
438 if (eregi('^inbox\\.', $boxes[$i]["unformatted"]) &&
439 (!isset($used[$i]) || $used[$i] == false)) {
440 $boxesnew[] = $boxes[$i];
441 $used[$i] = true;
442 }
443 }
444 }
445
446 /* Rest of the folders */
447 for ($i = 0; $i < count($boxes); $i++) {
448 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
449 (!isset($used[$i]) || $used[$i] == false)) {
450 $boxesnew[] = $boxes[$i];
451 $used[$i] = true;
452 }
453 }
454
455 return $boxesnew;
456 }
457
458 /******************************************************************************
459 ** Returns a list of all folders, subscribed or not
460 ******************************************************************************/
461 function sqimap_mailbox_list_all ($imap_stream)
462 {
463 global $list_special_folders_first, $folder_prefix;
464 global $delimiter;
465
466 if (!function_exists ("ary_sort")) {
467 include_once('../functions/array.php');
468 }
469
470 $ssid = sqimap_session_id();
471 $lsid = strlen( $ssid );
472 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
473 $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message);
474 $g = 0;
475 $phase = "inbox";
476
477 for ($i = 0; $i < count($read_ary); $i++) {
478 /* Another workaround for EIMS */
479 if (isset($read_ary[$i + 1]) &&
480 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
481 $read_ary[$i], $regs)) {
482 $i ++;
483 $read_ary[$i] = $regs[1] . '"' .
484 addslashes(trim($read_ary[$i])) .
485 '"' . $regs[2];
486 }
487 if (substr ($read_ary[$i], 0, $lsid) != $ssid ) {
488
489 /* Store the raw IMAP reply */
490 $boxes[$g]["raw"] = $read_ary[$i];
491
492 /* Count number of delimiters ($delimiter) in folder name */
493 $mailbox = find_mailbox_name($read_ary[$i]);
494 $dm_count = countCharInString($mailbox, $delimiter);
495 if (substr($mailbox, -1) == $delimiter) {
496 /* If name ends in delimiter - decrement count by one */
497 $dm_count--;
498 }
499
500 /* Format folder name, but only if it's a INBOX.* or have */
501 /* a parent. */
502 $boxesbyname[$mailbox] = $g;
503 $parentfolder = readMailboxParent($mailbox, $delimiter);
504 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
505 (ereg('^'.$folder_prefix, $mailbox)) ||
506 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
507 if ($dm_count) {
508 $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $dm_count);
509 }
510 else {
511 $boxes[$g]["formatted"] = '';
512 }
513 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $delimiter);
514 }
515 else {
516 $boxes[$g]["formatted"] = $mailbox;
517 }
518
519 $boxes[$g]["unformatted-dm"] = $mailbox;
520 if (substr($mailbox, -1) == $delimiter) {
521 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
522 }
523 $boxes[$g]["unformatted"] = $mailbox;
524 $boxes[$g]["unformatted-disp"] =
525 ereg_replace('^' . $folder_prefix, '', $mailbox);
526 $boxes[$g]["id"] = $g;
527
528 /** Now lets get the flags for this mailbox **/
529 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mailbox\"\r\n");
530 $read_mlbx = sqimap_read_data ($imap_stream, sqimap_session_id(),
531 true, $response, $message);
532
533 /* Another workaround for EIMS */
534 if (isset($read_mlbx[1]) &&
535 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
536 $read_mlbx[0], $regs)) {
537 $read_mlbx[0] = $regs[1] . '"' .
538 addslashes(trim($read_mlbx[1])) .
539 '"' . $regs[2];
540 }
541
542 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
543 $flags = substr($flags, 0, strpos($flags, ")"));
544 $flags = str_replace('\\', '', $flags);
545 $flags = trim(strtolower($flags));
546 if ($flags) {
547 $boxes[$g]['flags'] = explode(" ", $flags);
548 }
549 else {
550 $boxes[$g]['flags'] = array();
551 }
552 }
553 $g++;
554 }
555 if(is_array($boxes)) {
556 $boxes = ary_sort ($boxes, "unformatted", 1);
557 }
558
559 return $boxes;
560 }
561
562 ?>