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