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