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