Added TZ-fix for cyrus and mac os x mailserver aswell since these have been reported.
[squirrelmail.git] / functions / imap_mailbox.php
... / ...
CommitLineData
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
14require_once('../functions/imap_utf7_encode_local.php');
15require_once('../functions/imap_utf7_decode_local.php');
16
17global $boxesnew;
18
19class mailboxes {
20 var $mailboxname_full = '', $mailboxname_sub= '', $is_noselect = false,
21 $is_special = false, $is_root = false, $is_inbox = false, $is_sent = false,
22 $is_trash = false, $is_draft = false, $mbxs = array(),
23 $unseen = false, $total = false;
24
25 function addMbx($mbx, $delimiter, $start, $specialfirst) {
26 $ary = explode($delimiter, $mbx->mailboxname_full);
27 $mbx_parent = &$this;
28 for ($i=$start; $i < (count($ary) -1); $i++) {
29 $mbx_childs = &$mbx_parent->mbxs;
30 $found = false;
31 foreach ($mbx_childs as $key => $parent) {
32 if ($parent->mailboxname_sub == $ary[$i]) {
33 $mbx_parent = &$mbx_parent->mbxs[$key];
34 $found = true;
35 }
36 }
37 if (!$found) {
38 $no_select_mbx = new mailboxes();
39 if (isset($mbx_parent->mailboxname_full) && $mbx_parent->mailboxname_full != '') {
40 $no_select_mbx->mailboxname_full = $mbx_parent->mailboxname_full.$delimiter.$ary[$i];
41 } else {
42 $no_select_mbx->mailboxname_full = $ary[$i];
43 }
44 $no_select_mbx->mailboxname_sub = $ary[$i];
45 $no_select_mbx->is_noselect = true;
46 $mbx_parent->mbxs[] = $no_select_mbx;
47 $i--;
48 }
49
50 }
51 $mbx_parent->mbxs[] = $mbx;
52 if ($mbx->is_special && $specialfirst) {
53 usort($mbx_parent->mbxs, 'sortSpecialMbx');
54 }
55
56 }
57}
58
59function sortSpecialMbx($a, $b) {
60 if ($a->is_inbox) {
61 $acmp = '0'. $a->mailboxname_full;
62 } else if ($a->is_special) {
63 $acmp = '1'. $a->mailboxname_full;
64 } else {
65 $acmp = '2' . $a->mailboxname_full;
66 }
67 if ($b->is_inbox) {
68 $bcmp = '0'. $b->mailboxname_full;
69 }else if ($b->is_special) {
70 $bcmp = '1' . $b->mailboxname_full;
71 } else {
72 $bcmp = '2' . $b->mailboxname_full;
73 }
74 if ($acmp == $bcmp) return 0;
75 return ($acmp>$bcmp) ? 1: -1;
76}
77
78
79function find_mailbox_name ($mailbox) {
80 if (ereg(" *\"([^\r\n\"]*)\"[ \r\n]*$", $mailbox, $regs))
81 return $regs[1];
82 ereg(" *([^ \r\n\"]*)[ \r\n]*$",$mailbox,$regs);
83 return $regs[1];
84
85}
86
87/**
88 * If $haystack is a full mailbox name, and $needle is the mailbox
89 * separator character, returns the second last part of the full
90 * mailbox name (i.e. the mailbox's parent mailbox)
91 */
92function readMailboxParent($haystack, $needle) {
93
94 if ($needle == '') {
95 $ret = '';
96 } else {
97 $parts = explode($needle, $haystack);
98 $elem = array_pop($parts);
99 while ($elem == '' && count($parts)) {
100 $elem = array_pop($parts);
101 }
102 $ret = join($needle, $parts);
103 }
104 return( $ret );
105}
106
107
108function isBoxBelow( $box2, $box1 ) {
109 global $delimiter, $folder_prefix, $imap_server_type;
110
111 if ( $imap_server_type == 'uw' ) {
112 $boxs = $box2;
113 $i = strpos( $box1, $delimiter, strlen( $folder_prefix ) );
114 if ( $i === false ) {
115 $i = strlen( $box2 );
116 }
117 } else {
118 $boxs = $box2 . $delimiter;
119 /* Skip next second delimiter */
120 $i = strpos( $box1, $delimiter );
121 $i = strpos( $box1, $delimiter, $i + 1 );
122 if ( $i === false ) {
123 $i = strlen( $box2 );
124 } else {
125 $i++;
126 }
127 }
128
129 return ( substr( $box1, 0, $i ) == substr( $boxs, 0, $i ) );
130}
131
132/* Defines special mailboxes */
133function isSpecialMailbox( $box ) {
134 global $trash_folder, $sent_folder, $draft_folder,
135 $move_to_trash, $move_to_sent, $save_as_draft;
136
137 $ret = ( (strtolower($box) == 'inbox') ||
138 ( $move_to_trash && isBoxBelow( $box, $trash_folder ) ) ||
139 ( $move_to_sent && isBoxBelow( $box, $sent_folder )) ||
140 ($save_as_draft && $box == $draft_folder ) );
141
142 if ( !$ret ) {
143 $ret = do_hook_function( 'special_mailbox', $box );
144 }
145
146 return $ret;
147}
148
149/* Expunges a mailbox */
150function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true) {
151 $read = sqimap_run_command($imap_stream, 'EXPUNGE', $handle_errors,
152 $response, $message);
153}
154
155/* Checks whether or not the specified mailbox exists */
156function sqimap_mailbox_exists ($imap_stream, $mailbox) {
157 if (! isset($mailbox)) {
158 return false;
159 }
160 $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
161 true, $response, $message);
162 return isset($mbx[0]);
163}
164
165/* Selects a mailbox */
166function sqimap_mailbox_select ($imap_stream, $mailbox,
167 $hide = true, $recent = false, $extrainfo = false) {
168 global $auto_expunge;
169
170 if ( $mailbox == 'None' ) {
171 return;
172 }
173
174 $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
175 true, $response, $message);
176 if ($recent) {
177 for ($i=0; $i<count($read); $i++) {
178 if (strpos(strtolower($read[$i]), 'recent')) {
179 $r = explode(' ', $read[$i]);
180 }
181 }
182 return $r[1];
183 } else {
184 if ($auto_expunge) {
185 $tmp = sqimap_run_command($imap_stream, 'EXPUNGE', false, $a, $b);
186 }
187 if (isset( $extrainfo ) && $extrainfo) {
188 $result = array();
189 for ($i=0; $i<count($read); $i++) {
190 if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) {
191 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ;
192 $result['PERMANENTFLAGS'] = $regs[1];
193 }
194 else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) {
195 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ;
196 $result['FLAGS'] = $regs[1];
197 }
198 else if (preg_match("/(.*)EXISTS/i",$read[$i], $regs)) {
199 $result['EXISTS']=trim($regs[1]);
200 }
201 else if (preg_match("/(.*)RECENT/i",$read[$i], $regs)) {
202 $result['RECENT']=trim($regs[1]);
203 }
204 else if (preg_match("/\[UNSEEN(.*)\]/i",$read[$i], $regs)) {
205 $result['UNSEEN']=trim($regs[1]);
206 }
207
208 }
209 return( $result );
210 }
211 }
212}
213
214/* Creates a folder */
215function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
216 global $delimiter;
217 if (strtolower($type) == 'noselect') {
218 $mailbox .= $delimiter;
219 }
220
221 $mailbox = imap_utf7_encode_local($mailbox);
222
223 $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
224 true, $response, $message);
225 sqimap_subscribe ($imap_stream, $mailbox);
226}
227
228/* Subscribes to an existing folder */
229function sqimap_subscribe ($imap_stream, $mailbox) {
230 $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"",
231 true, $response, $message);
232}
233
234/* Unsubscribes to an existing folder */
235function sqimap_unsubscribe ($imap_stream, $mailbox) {
236 global $imap_server_type;
237 $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"",
238 true, $response, $message);
239}
240
241/* Deletes the given folder */
242function sqimap_mailbox_delete ($imap_stream, $mailbox) {
243 global $data_dir, $username;
244 $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
245 true, $response, $message);
246 sqimap_unsubscribe ($imap_stream, $mailbox);
247 do_hook_function("rename_or_delete_folder", $args = array($mailbox, 'delete', ''));
248 removePref($data_dir, $username, "thread_$mailbox");
249}
250
251/* Determines if the user is subscribed to the folder or not */
252function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
253 $boxesall = sqimap_mailbox_list ($imap_stream);
254 foreach ($boxesall as $ref) {
255 if ($ref['unformatted'] == $folder) {
256 return true;
257 }
258 }
259 return false;
260}
261
262/* Renames a mailbox */
263function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
264 if ( $old_name != $new_name ) {
265 global $delimiter, $imap_server_type, $data_dir, $username;
266 if ( substr( $old_name, -1 ) == $delimiter ) {
267 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
268 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
269 $postfix = $delimiter;
270 } else {
271 $postfix = '';
272 }
273 $boxesall = sqimap_mailbox_list($imap_stream);
274 $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' . quoteIMAP($new_name) . '"';
275 $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message);
276 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
277 $oldpref = getPref($data_dir, $username, "thread_".$old_name.$postfix);
278 removePref($data_dir, $username, "thread_".$old_name.$postfix);
279 sqimap_subscribe($imap_stream, $new_name.$postfix);
280 setPref($data_dir, $username, "thread_".$new_name.$postfix, $oldpref);
281 do_hook_function("rename_or_delete_folder",$args = array($old_name, 'rename', $new_name));
282 $l = strlen( $old_name ) + 1;
283 $p = 'unformatted';
284 foreach ( $boxesall as $box ) {
285 if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) {
286 $new_sub = $new_name . $delimiter . substr($box[$p], $l);
287 if ($imap_server_type == 'cyrus') {
288 $cmd = 'RENAME "' . quoteIMAP($box[$p]) . '" "' . quoteIMAP($new_sub) . '"';
289 $data = sqimap_run_command($imap_stream, $cmd, true,
290 $response, $message);
291 }
292 sqimap_unsubscribe($imap_stream, $box[$p]);
293 $oldpref = getPref($data_dir, $username, "thread_".$box[$p]);
294 removePref($data_dir, $username, "thread_".$box[$p]);
295 sqimap_subscribe($imap_stream, $new_sub);
296 setPref($data_dir, $username, "thread_".$new_sub, $oldpref);
297 do_hook_function("rename_or_delete_folder",
298 $args = array($box[$p], 'rename', $new_sub));
299 }
300 }
301 }
302}
303
304/*
305 * Formats a mailbox into 4 parts for the $boxesall array
306 *
307 * The four parts are:
308 *
309 * raw - Raw LIST/LSUB response from the IMAP server
310 * formatted - nicely formatted folder name
311 * unformatted - unformatted, but with delimiter at end removed
312 * unformatted-dm - folder name as it appears in raw response
313 * unformatted-disp - unformatted without $folder_prefix
314 */
315function sqimap_mailbox_parse ($line, $line_lsub) {
316 global $folder_prefix, $delimiter;
317
318 /* Process each folder line */
319 for ($g=0; $g < count($line); $g++) {
320
321 /* Store the raw IMAP reply */
322 if (isset($line[$g])) {
323 $boxesall[$g]["raw"] = $line[$g];
324 }
325 else {
326 $boxesall[$g]["raw"] = '';
327 }
328
329
330 /* Count number of delimiters ($delimiter) in folder name */
331 $mailbox = trim($line_lsub[$g]);
332 $dm_count = substr_count($mailbox, $delimiter);
333 if (substr($mailbox, -1) == $delimiter) {
334 /* If name ends in delimiter, decrement count by one */
335 $dm_count--;
336 }
337
338 /* Format folder name, but only if it's a INBOX.* or has a parent. */
339 $boxesallbyname[$mailbox] = $g;
340 $parentfolder = readMailboxParent($mailbox, $delimiter);
341 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
342 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
343 ( isset($boxesallbyname[$parentfolder]) &&
344 (strlen($parentfolder) > 0) ) ) {
345 $indent = $dm_count - ( substr_count($folder_prefix, $delimiter));
346 if ($indent > 0) {
347 $boxesall[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $indent);
348 }
349 else {
350 $boxesall[$g]['formatted'] = '';
351 }
352 $boxesall[$g]['formatted'] .= imap_utf7_decode_local(
353 readShortMailboxName($mailbox, $delimiter));
354 }
355 else {
356 $boxesall[$g]['formatted'] = imap_utf7_decode_local($mailbox);
357 }
358
359 $boxesall[$g]['unformatted-dm'] = $mailbox;
360 if (substr($mailbox, -1) == $delimiter) {
361 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
362 }
363 $boxesall[$g]['unformatted'] = $mailbox;
364 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
365 $mailbox = substr($mailbox, strlen($folder_prefix));
366 }
367 $boxesall[$g]['unformatted-disp'] = $mailbox;
368 $boxesall[$g]['id'] = $g;
369
370 $boxesall[$g]['flags'] = array();
371 if (isset($line[$g])) {
372 ereg("\(([^)]*)\)",$line[$g],$regs);
373 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
374 if ($flags) {
375 $boxesall[$g]['flags'] = explode(' ', $flags);
376 }
377 }
378 }
379
380 return $boxesall;
381}
382
383/*
384 * Sorting function used to sort mailbox names.
385 * + Original patch from dave_michmerhuizen@yahoo.com
386 * + Allows case insensitivity when sorting folders
387 * + Takes care of the delimiter being sorted to the end, causing
388 * subfolders to be listed in below folders that are prefixed
389 * with their parent folders name.
390 *
391 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
392 * Without special sort function: foobar between foo and foo.bar
393 * With special sort function: foobar AFTER foo and foo.bar :)
394 */
395function user_strcasecmp($a, $b) {
396 global $delimiter;
397
398 /* Calculate the length of some strings. */
399 $a_length = strlen($a);
400 $b_length = strlen($b);
401 $min_length = min($a_length, $b_length);
402 $delimiter_length = strlen($delimiter);
403
404 /* Set the initial result value. */
405 $result = 0;
406
407 /* Check the strings... */
408 for ($c = 0; $c < $min_length; ++$c) {
409 $a_del = substr($a, $c, $delimiter_length);
410 $b_del = substr($b, $c, $delimiter_length);
411
412 if (($a_del == $delimiter) && ($b_del == $delimiter)) {
413 $result = 0;
414 } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
415 $result = -1;
416 } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
417 $result = 1;
418 } else {
419 $result = strcasecmp($a{$c}, $b{$c});
420 }
421
422 if ($result != 0) {
423 break;
424 }
425 }
426
427 /* If one string is a prefix of the other... */
428 if ($result == 0) {
429 if ($a_length < $b_length) {
430 $result = -1;
431 } else if ($a_length > $b_length) {
432 $result = 1;
433 }
434 }
435
436 return $result;
437}
438
439
440/*
441 * Returns sorted mailbox lists in several different ways.
442 * See comment on sqimap_mailbox_parse() for info about the returned array.
443 */
444function sqimap_mailbox_list($imap_stream) {
445 global $boxesnew, $default_folder_prefix;
446
447 if ( !isset( $boxesnew ) ) {
448
449 global $data_dir, $username, $list_special_folders_first,
450 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
451 $move_to_trash, $move_to_sent, $save_as_draft,
452 $delimiter;
453
454 $inbox_in_list = false;
455 $inbox_subscribed = false;
456
457 require_once('../src/load_prefs.php');
458 require_once('../functions/array.php');
459
460 /* LSUB array */
461 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*%\"",
462 true, $response, $message);
463
464 /*
465 * Section about removing the last element was removed
466 * We don't return "* OK" anymore from sqimap_read_data
467 */
468
469 $sorted_lsub_ary = array();
470 for ($i=0;$i < count($lsub_ary); $i++) {
471 /*
472 * Workaround for EIMS
473 * Doesn't work if the mailbox name is multiple lines
474 */
475 if (isset($lsub_ary[$i + 1]) &&
476 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
477 $lsub_ary[$i], $regs)) {
478 $i ++;
479 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
480 }
481 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
482 $sorted_lsub_ary[] = $temp_mailbox_name;
483 if (strtoupper($temp_mailbox_name) == 'INBOX') {
484 $inbox_subscribed = true;
485 }
486 }
487 $new_ary = array();
488 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
489 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
490 $new_ary[] = $sorted_lsub_ary[$i];
491 }
492 }
493 $sorted_lsub_ary = $new_ary;
494 if (isset($sorted_lsub_ary)) {
495 usort($sorted_lsub_ary, 'user_strcasecmp');
496 }
497
498 /* LIST array */
499 $sorted_list_ary = array();
500 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
501 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
502 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
503 }
504 else {
505 $mbx = $sorted_lsub_ary[$i];
506 }
507
508 $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
509 true, $response, $message);
510 /* Another workaround for EIMS */
511 if (isset($read[1]) &&
512 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
513 $read[0], $regs)) {
514 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) . '"' . $regs[2];
515 }
516
517 if (isset($sorted_list_ary[$i])) {
518 $sorted_list_ary[$i] = '';
519 }
520
521 if (isset($read[0])) {
522 $sorted_list_ary[$i] = $read[0];
523 }
524 else {
525 $sorted_list_ary[$i] = '';
526 }
527
528 if (isset($sorted_list_ary[$i]) &&
529 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
530 $inbox_in_list = true;
531 }
532 }
533
534 /*
535 * Just in case they're not subscribed to their inbox,
536 * we'll get it for them anyway
537 */
538 if (!$inbox_subscribed || !$inbox_in_list) {
539 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
540 true, $response, $message);
541 /* Another workaround for EIMS */
542 if (isset($inbox_ary[1]) &&
543 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
544 $inbox_ary[0], $regs)) {
545 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
546 '"' . $regs[2];
547 }
548
549 $sorted_list_ary[] = $inbox_ary[0];
550 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
551 }
552
553 $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
554
555 /* Now, lets sort for special folders */
556 $boxesnew = $used = array();
557
558 /* Find INBOX */
559 foreach ( $boxesall as $k => $box ) {
560 if ( strtolower($box['unformatted']) == 'inbox') {
561 $boxesnew[] = $box;
562 $used[$k] = true;
563 } else {
564 $used[$k] = false;
565 }
566 }
567 /* List special folders and their subfolders, if requested. */
568 if ($list_special_folders_first) {
569 foreach ( $boxesall as $k => $box ) {
570 if ( !$used[$k] && isSpecialMailbox( $box['unformatted'] ) ) {
571 $boxesnew[] = $box;
572 $used[$k] = true;
573 }
574 $spec_sub = str_replace('&nbsp;', '', $box['formatted']);
575
576 /* In case of problems with preg
577 here is a ereg version
578 if (!$used[$k] && ereg("^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$", $box['unformatted']) ) { */
579
580 if (!$used[$k] && preg_match("?^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$?", $box['unformatted']) ) {
581 $boxesnew[] = $box;
582 $used[$k] = true;
583 }
584 }
585
586 }
587 /* Rest of the folders */
588 foreach ( $boxesall as $k => $box ) {
589 if ( !$used[$k] ) {
590 $boxesnew[] = $box;
591 }
592 }
593 }
594 return $boxesnew;
595}
596
597/*
598 * Returns a list of all folders, subscribed or not
599 */
600function sqimap_mailbox_list_all($imap_stream) {
601 global $list_special_folders_first, $folder_prefix, $delimiter;
602
603 require_once('../functions/array.php');
604
605 $ssid = sqimap_session_id();
606 $lsid = strlen( $ssid );
607 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
608 $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message);
609 $g = 0;
610 $phase = 'inbox';
611
612 for ($i = 0; $i < count($read_ary); $i++) {
613 /* Another workaround for EIMS */
614 if (isset($read_ary[$i + 1]) &&
615 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
616 $read_ary[$i], $regs)) {
617 $i ++;
618 $read_ary[$i] = $regs[1] . '"' . addslashes(trim($read_ary[$i])) . '"' . $regs[2];
619 }
620 if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
621
622 /* Store the raw IMAP reply */
623 $boxes[$g]['raw'] = $read_ary[$i];
624
625 /* Count number of delimiters ($delimiter) in folder name */
626 $mailbox = find_mailbox_name($read_ary[$i]);
627 $dm_count = substr_count($mailbox, $delimiter);
628 if (substr($mailbox, -1) == $delimiter) {
629 /* If name ends in delimiter - decrement count by one */
630 $dm_count--;
631 }
632
633 /* Format folder name, but only if it's a INBOX.* or has a parent. */
634 $boxesallbyname[$mailbox] = $g;
635 $parentfolder = readMailboxParent($mailbox, $delimiter);
636 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
637 (ereg('^'.$folder_prefix, $mailbox)) ||
638 ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
639 if ($dm_count) {
640 $boxes[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $dm_count);
641 }
642 else {
643 $boxes[$g]['formatted'] = '';
644 }
645 $boxes[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
646 }
647 else {
648 $boxes[$g]['formatted'] = imap_utf7_decode_local($mailbox);
649 }
650
651 $boxes[$g]['unformatted-dm'] = $mailbox;
652 if (substr($mailbox, -1) == $delimiter) {
653 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
654 }
655 $boxes[$g]['unformatted'] = $mailbox;
656 $boxes[$g]['unformatted-disp'] = ereg_replace('^' . $folder_prefix, '', $mailbox);
657 $boxes[$g]['id'] = $g;
658
659 /* Now lets get the flags for this mailbox */
660 $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
661 true, $response, $message);
662
663 /* Another workaround for EIMS */
664 if (isset($read_mlbx[1]) &&
665 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", $read_mlbx[0], $regs)) {
666 $read_mlbx[0] = $regs[1] . '"' . addslashes(trim($read_mlbx[1])) . '"' . $regs[2];
667 }
668
669 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+1);
670 $flags = substr($flags, 0, strpos($flags, ')'));
671 $flags = str_replace('\\', '', $flags);
672 $flags = trim(strtolower($flags));
673 if ($flags) {
674 $boxes[$g]['flags'] = explode(' ', $flags);
675 } else {
676 $boxes[$g]['flags'] = array();
677 }
678 }
679 $g++;
680 }
681 if(is_array($boxes)) {
682 $boxes = ary_sort ($boxes, 'unformatted', 1);
683 }
684
685 return $boxes;
686}
687
688function sqimap_mailbox_tree($imap_stream) {
689 global $boxesnew, $default_folder_prefix, $unseen_notify, $unseen_type;
690 if ( !isset( $boxesnew ) ) {
691
692 global $data_dir, $username, $list_special_folders_first,
693 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
694 $move_to_trash, $move_to_sent, $save_as_draft,
695 $delimiter;
696
697
698 $inbox_in_list = false;
699 $inbox_subscribed = false;
700
701 require_once('../src/load_prefs.php');
702 require_once('../functions/array.php');
703
704 /* LSUB array */
705 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
706 true, $response, $message);
707
708 /*
709 * Section about removing the last element was removed
710 * We don't return "* OK" anymore from sqimap_read_data
711 */
712 $sorted_lsub_ary = array();
713
714 for ($i=0;$i < count($lsub_ary); $i++) {
715 /*
716 * Workaround for EIMS
717 * Doesn't work if the mailbox name is multiple lines
718 */
719 if (isset($lsub_ary[$i + 1]) &&
720 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
721 $lsub_ary[$i], $regs)) {
722 $i ++;
723 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
724 }
725
726 if (preg_match("/^\*\s+LSUB\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$lsub_ary[$i],$regs)) {
727 $flag = $regs[1];
728 $mbx = trim($regs[3]);
729 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
730 }
731 }
732 array_multisort($sorted_lsub_ary, SORT_ASC, SORT_STRING);
733
734 foreach ($sorted_lsub_ary as $mbx) {
735 if ($mbx['mbx'] == 'INBOX') {
736 $inbox_in_list = true;
737 break;
738 }
739 }
740
741 /*
742 * Just in case they're not subscribed to their inbox,
743 * we'll get it for them anyway
744 */
745 if (!$inbox_in_list) {
746 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
747 true, $response, $message);
748 /* Another workaround for EIMS */
749 if (isset($inbox_ary[1]) &&
750 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
751 $inbox_ary[0], $regs)) {
752 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
753 '"' . $regs[2];
754 }
755 if (preg_match("/^\*\s+LIST\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$inbox_ary[0],$regs)) {
756 $flag = $regs[1];
757 $mbx = trim($regs[3]);
758 if (substr($mbx, -1) == $delimiter) {
759 $mbx = substr($mbx, 0, strlen($mbx) - 1);
760 }
761 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
762 }
763 }
764
765 for ($i=0 ; $i < count($sorted_lsub_ary); $i++) {
766 if (($unseen_notify == 2 && $sorted_lsub_ary[$i]['mbx'] == 'INBOX')
767 || $unseen_notify == 3
768 || ($move_to_trash && ($sorted_lsub_ary[$i]['mbx'] == $trash_folder))) {
769 $sorted_lsub_ary[$i]['unseen'] = sqimap_unseen_messages($imap_stream, $mbx['mbx']);
770 if ($unseen_type == 2 || ($move_to_trash
771 && ($sorted_lsub_ary[$i]['mbx'] == $trash_folder) )) {
772 $sorted_lsub_ary[$i]['nummessages'] = sqimap_get_num_messages($imap_stream, $mbx['mbx']);
773 }
774 if ($unseen_notify != 3 && !($move_to_trash
775 && ($sorted_lsub_ary[$i]['mbx'] == $trash_folder) )) break;
776 }
777 }
778
779 $boxesnew = sqimap_fill_mailbox_tree($sorted_lsub_ary);
780 return $boxesnew;
781 }
782}
783
784
785function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false) {
786 global $data_dir, $username, $list_special_folders_first,
787 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
788 $move_to_trash, $move_to_sent, $save_as_draft,
789 $delimiter;
790
791 $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
792
793 /* create virtual root node */
794 $mailboxes= new mailboxes();
795 $mailboxes->is_root = true;
796 $trail_del = false;
797 if (isset($folder_prefix) && $folder_prefix != '') {
798 $start = substr_count($folder_prefix,$delimiter);
799 if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
800 $trail_del = true;
801 $mailboxes->mailboxname_full = substr($folder_prefix,0, (strlen($folder_prefix)-1));
802 } else {
803 $mailboxes->mailboxname_full = $folder_prefix;
804 $start++;
805 }
806 $mailboxes->mailboxname_sub = $mailboxes->mailboxname_full;
807 } else $start = 0;
808
809 for ($i=0; $i < count($mbx_ary); $i++) {
810 if ($mbx_ary[$i]['mbx'] !='' ) {
811 $mbx = new mailboxes();
812 $mailbox = $mbx_ary[$i]['mbx'];
813 switch ($mailbox) {
814 case 'INBOX':
815 $mbx->is_inbox = true;
816 $mbx->is_special = true;
817 break;
818 case $trash_folder:
819 $mbx->is_trash = true;
820 $mbx->is_special = true;
821 break;
822 case $sent_folder:
823 $mbx->is_sent = true;
824 $mbx->is_special = true;
825 break;
826 case $draft_folder:
827 $mbx->is_draft = true;
828 $mbx->is_special = true;
829 break;
830 }
831
832 if (isset($mbx_ary[$i]['unseen'])) {
833 $mbx->unseen = $mbx_ary[$i]['unseen'];
834 }
835 if (isset($mbx_ary[$i]['nummessages'])) {
836 $mbx->total = $mbx_ary[$i]['nummessages'];
837 }
838
839 $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
840 if ($r_del_pos) {
841 $mbx->mailboxname_sub = substr($mbx_ary[$i]['mbx'],$r_del_pos+1);
842 } else { /* mailbox is root folder */
843 $mbx->mailboxname_sub = $mbx_ary[$i]['mbx'];
844 }
845 $mbx->mailboxname_full = $mbx_ary[$i]['mbx'];
846 $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);
847 }
848 }
849
850 return $mailboxes;
851}
852
853
854?>