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