44e14a91dca65f3f059282464b1c757646f83299
[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 if ($mbx_childs) {
30 foreach ($mbx_childs as $key => $parent) {
31 if ($parent->mailboxname_sub == $ary[$i]) {
32 $mbx_parent = &$mbx_parent->mbxs[$key];
33 $found = true;
34 }
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
59 function 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
79 function 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 */
92 function 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
108 function 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 */
133 function 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 */
150 function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true, $id='') {
151 global $uid_support;
152 if (isset($id)) {
153 if (is_array($id)) {
154 $id = sqimap_message_list_squisher($id);
155 }
156 $id = ' '.$id;
157 }
158 $read = sqimap_run_command($imap_stream, 'EXPUNGE$id', $handle_errors,
159 $response, $message, $uid_support);
160 $cnt = 0;
161 foreach ($read as $r) {
162 if (pregmatch('/^\*\s[0-9]+\sEXPUNGE/AUi',$r,$regs)) {
163 $cnt++;
164 }
165 }
166 return $cnt;
167 }
168
169 /* Checks whether or not the specified mailbox exists */
170 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
171 if (! isset($mailbox)) {
172 return false;
173 }
174 $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
175 true, $response, $message);
176 return isset($mbx[0]);
177 }
178
179 /* Selects a mailbox */
180 function sqimap_mailbox_select ($imap_stream, $mailbox) {
181 global $auto_expunge;
182
183 if ( $mailbox == 'None' ) {
184 return;
185 }
186
187 $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
188 true, $response, $message);
189 $result = array();
190 for ($i=0; $i<count($read); $i++) {
191 if (preg_match('/^\*\s+OK\s\[(\w+)\s(\w+)\]/',$read[$i], $regs)) {
192 $result[strtoupper($regs[1])] = $regs[2];
193 } else if (preg_match('/^\*\s([0-9]+)\s(\w+)/',$read[$i], $regs)) {
194 $result[strtoupper($regs[2])] = $regs[1];
195 } else {
196 if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) {
197 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ;
198 $result['PERMANENTFLAGS'] = $regs[1];
199 }
200 else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) {
201 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ;
202 $result['FLAGS'] = $regs[1];
203 }
204 }
205 }
206 if (preg_match('/^\[(.+)\]/',$message, $regs)) {
207 $result['RIGHTS']=$regs[1];
208 }
209
210 if ($auto_expunge) {
211 $tmp = sqimap_run_command($imap_stream, 'EXPUNGE', false, $a, $b);
212 }
213 return $result;
214 }
215
216 /* Creates a folder */
217 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
218 global $delimiter;
219 if (strtolower($type) == 'noselect') {
220 $mailbox .= $delimiter;
221 }
222 $mailbox = imap_utf7_encode_local($mailbox);
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 */
229 function 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 */
235 function 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 */
242 function 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 */
252 function 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 */
263 function 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 */
315 function 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(readShortMailboxName($mailbox, $delimiter));
353 }
354 else {
355 $boxesall[$g]['formatted'] = imap_utf7_decode_local($mailbox);
356 }
357
358 $boxesall[$g]['unformatted-dm'] = $mailbox;
359 if (substr($mailbox, -1) == $delimiter) {
360 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
361 }
362 $boxesall[$g]['unformatted'] = $mailbox;
363 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
364 $mailbox = substr($mailbox, strlen($folder_prefix));
365 }
366 $boxesall[$g]['unformatted-disp'] = $mailbox;
367 $boxesall[$g]['id'] = $g;
368
369 $boxesall[$g]['flags'] = array();
370 if (isset($line[$g])) {
371 ereg("\(([^)]*)\)",$line[$g],$regs);
372 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
373 if ($flags) {
374 $boxesall[$g]['flags'] = explode(' ', $flags);
375 }
376 }
377 }
378
379 return $boxesall;
380 }
381
382 /*
383 * Sorting function used to sort mailbox names.
384 * + Original patch from dave_michmerhuizen@yahoo.com
385 * + Allows case insensitivity when sorting folders
386 * + Takes care of the delimiter being sorted to the end, causing
387 * subfolders to be listed in below folders that are prefixed
388 * with their parent folders name.
389 *
390 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
391 * Without special sort function: foobar between foo and foo.bar
392 * With special sort function: foobar AFTER foo and foo.bar :)
393 */
394 function user_strcasecmp($a, $b) {
395 global $delimiter;
396
397 /* Calculate the length of some strings. */
398 $a_length = strlen($a);
399 $b_length = strlen($b);
400 $min_length = min($a_length, $b_length);
401 $delimiter_length = strlen($delimiter);
402
403 /* Set the initial result value. */
404 $result = 0;
405
406 /* Check the strings... */
407 for ($c = 0; $c < $min_length; ++$c) {
408 $a_del = substr($a, $c, $delimiter_length);
409 $b_del = substr($b, $c, $delimiter_length);
410
411 if (($a_del == $delimiter) && ($b_del == $delimiter)) {
412 $result = 0;
413 } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
414 $result = -1;
415 } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
416 $result = 1;
417 } else {
418 $result = strcasecmp($a{$c}, $b{$c});
419 }
420
421 if ($result != 0) {
422 break;
423 }
424 }
425
426 /* If one string is a prefix of the other... */
427 if ($result == 0) {
428 if ($a_length < $b_length) {
429 $result = -1;
430 } else if ($a_length > $b_length) {
431 $result = 1;
432 }
433 }
434
435 return $result;
436 }
437
438
439 /*
440 * Returns sorted mailbox lists in several different ways.
441 * See comment on sqimap_mailbox_parse() for info about the returned array.
442 */
443 function sqimap_mailbox_list($imap_stream) {
444 global $default_folder_prefix;
445
446 if ( !isset( $boxesnew ) ) {
447
448 global $data_dir, $username, $list_special_folders_first,
449 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
450 $move_to_trash, $move_to_sent, $save_as_draft,
451 $delimiter, $noselect_fix_enable;
452
453 $inbox_in_list = false;
454 $inbox_subscribed = false;
455
456 require_once('../src/load_prefs.php');
457 require_once('../functions/array.php');
458
459 if ($noselect_fix_enable) {
460 $lsub_args = "LSUB \"$folder_prefix\" \"*%\"";
461 }
462 else {
463 $lsub_args = "LSUB \"$folder_prefix\" \"*\"";
464 }
465 /* LSUB array */
466 $lsub_ary = sqimap_run_command ($imap_stream, $lsub_args,
467 true, $response, $message);
468
469
470 /*
471 * Section about removing the last element was removed
472 * We don't return "* OK" anymore from sqimap_read_data
473 */
474
475 $sorted_lsub_ary = array();
476 for ($i=0;$i < count($lsub_ary); $i++) {
477 /*
478 * Workaround for EIMS
479 * Doesn't work if the mailbox name is multiple lines
480 */
481 if (isset($lsub_ary[$i + 1]) &&
482 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
483 $lsub_ary[$i], $regs)) {
484 $i ++;
485 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
486 }
487 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
488 $sorted_lsub_ary[] = $temp_mailbox_name;
489 if (strtoupper($temp_mailbox_name) == 'INBOX') {
490 $inbox_subscribed = true;
491 }
492 }
493 $new_ary = array();
494 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
495 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
496 $new_ary[] = $sorted_lsub_ary[$i];
497 }
498 }
499 $sorted_lsub_ary = $new_ary;
500 if (isset($sorted_lsub_ary)) {
501 usort($sorted_lsub_ary, 'user_strcasecmp');
502 }
503
504 /* LIST array */
505 $sorted_list_ary = array();
506 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
507 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
508 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
509 }
510 else {
511 $mbx = $sorted_lsub_ary[$i];
512 }
513
514 $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
515 true, $response, $message);
516 /* Another workaround for EIMS */
517 if (isset($read[1]) &&
518 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
519 $read[0], $regs)) {
520 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) . '"' . $regs[2];
521 }
522
523 if (isset($sorted_list_ary[$i])) {
524 $sorted_list_ary[$i] = '';
525 }
526
527 if (isset($read[0])) {
528 $sorted_list_ary[$i] = $read[0];
529 }
530 else {
531 $sorted_list_ary[$i] = '';
532 }
533
534 if (isset($sorted_list_ary[$i]) &&
535 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
536 $inbox_in_list = true;
537 }
538 }
539
540 /*
541 * Just in case they're not subscribed to their inbox,
542 * we'll get it for them anyway
543 */
544 if (!$inbox_subscribed || !$inbox_in_list) {
545 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
546 true, $response, $message);
547 /* Another workaround for EIMS */
548 if (isset($inbox_ary[1]) &&
549 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
550 $inbox_ary[0], $regs)) {
551 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
552 '"' . $regs[2];
553 }
554
555 $sorted_list_ary[] = $inbox_ary[0];
556 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
557 }
558
559 $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
560
561 /* Now, lets sort for special folders */
562 $boxesnew = $used = array();
563
564 /* Find INBOX */
565 foreach ( $boxesall as $k => $box ) {
566 if ( strtolower($box['unformatted']) == 'inbox') {
567 $boxesnew[] = $box;
568 $used[$k] = true;
569 } else {
570 $used[$k] = false;
571 }
572 }
573 /* List special folders and their subfolders, if requested. */
574 if ($list_special_folders_first) {
575 foreach ( $boxesall as $k => $box ) {
576 if ( !$used[$k] && isSpecialMailbox( $box['unformatted'] ) ) {
577 $boxesnew[] = $box;
578 $used[$k] = true;
579 }
580 $spec_sub = str_replace('&nbsp;', '', $box['formatted']);
581 $spec_sub = str_replace('*', '', $spec_sub);
582 $spec_sub = str_replace('?', '\?', $spec_sub);
583
584 /* In case of problems with preg
585 here is a ereg version
586 if (!$used[$k] && ereg("^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$", $box['unformatted']) ) { */
587
588 if (!$used[$k] && preg_match("?^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$?", $box['unformatted']) ) {
589 $boxesnew[] = $box;
590 $used[$k] = true;
591 }
592 }
593
594 }
595 /* Rest of the folders */
596 foreach ( $boxesall as $k => $box ) {
597 if ( !$used[$k] ) {
598 $boxesnew[] = $box;
599 }
600 }
601 }
602 return $boxesnew;
603 }
604
605 /*
606 * Returns a list of all folders, subscribed or not
607 */
608 function sqimap_mailbox_list_all($imap_stream) {
609 global $list_special_folders_first, $folder_prefix, $delimiter;
610
611 require_once('../functions/array.php');
612
613 $ssid = sqimap_session_id();
614 $lsid = strlen( $ssid );
615 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
616 $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message);
617 $g = 0;
618 $phase = 'inbox';
619
620 for ($i = 0; $i < count($read_ary); $i++) {
621 /* Another workaround for EIMS */
622 if (isset($read_ary[$i + 1]) &&
623 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
624 $read_ary[$i], $regs)) {
625 $i ++;
626 $read_ary[$i] = $regs[1] . '"' . addslashes(trim($read_ary[$i])) . '"' . $regs[2];
627 }
628 if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
629
630 /* Store the raw IMAP reply */
631 $boxes[$g]['raw'] = $read_ary[$i];
632
633 /* Count number of delimiters ($delimiter) in folder name */
634 $mailbox = find_mailbox_name($read_ary[$i]);
635 $dm_count = substr_count($mailbox, $delimiter);
636 if (substr($mailbox, -1) == $delimiter) {
637 /* If name ends in delimiter - decrement count by one */
638 $dm_count--;
639 }
640
641 /* Format folder name, but only if it's a INBOX.* or has a parent. */
642 $boxesallbyname[$mailbox] = $g;
643 $parentfolder = readMailboxParent($mailbox, $delimiter);
644 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
645 (ereg('^'.$folder_prefix, $mailbox)) ||
646 ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
647 if ($dm_count) {
648 $boxes[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $dm_count);
649 }
650 else {
651 $boxes[$g]['formatted'] = '';
652 }
653 $boxes[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
654 }
655 else {
656 $boxes[$g]['formatted'] = imap_utf7_decode_local($mailbox);
657 }
658
659 $boxes[$g]['unformatted-dm'] = $mailbox;
660 if (substr($mailbox, -1) == $delimiter) {
661 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
662 }
663 $boxes[$g]['unformatted'] = $mailbox;
664 $boxes[$g]['unformatted-disp'] = ereg_replace('^' . $folder_prefix, '', $mailbox);
665 $boxes[$g]['id'] = $g;
666
667 /* Now lets get the flags for this mailbox */
668 $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
669 true, $response, $message);
670
671 /* Another workaround for EIMS */
672 if (isset($read_mlbx[1]) &&
673 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", $read_mlbx[0], $regs)) {
674 $read_mlbx[0] = $regs[1] . '"' . addslashes(trim($read_mlbx[1])) . '"' . $regs[2];
675 }
676
677 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+1);
678 $flags = substr($flags, 0, strpos($flags, ')'));
679 $flags = str_replace('\\', '', $flags);
680 $flags = trim(strtolower($flags));
681 if ($flags) {
682 $boxes[$g]['flags'] = explode(' ', $flags);
683 } else {
684 $boxes[$g]['flags'] = array();
685 }
686 }
687 $g++;
688 }
689 if(is_array($boxes)) {
690 $boxes = ary_sort ($boxes, 'unformatted', 1);
691 }
692
693 return $boxes;
694 }
695
696 function sqimap_mailbox_tree($imap_stream) {
697 global $boxesnew, $default_folder_prefix, $unseen_notify, $unseen_type;
698 if ( !isset( $boxesnew ) ) {
699
700 global $data_dir, $username, $list_special_folders_first,
701 $folder_prefix, $delimiter, $trash_folder, $move_to_trash;
702
703
704 $inbox_in_list = false;
705 $inbox_subscribed = false;
706
707 require_once('../src/load_prefs.php');
708 require_once('../functions/array.php');
709
710 /* LSUB array */
711 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
712 true, $response, $message);
713
714 /*
715 * Section about removing the last element was removed
716 * We don't return "* OK" anymore from sqimap_read_data
717 */
718 $sorted_lsub_ary = array();
719 $cnt = count($lsub_ary);
720 for ($i=0;$i < $cnt; $i++) {
721 /*
722 * Workaround for EIMS
723 * Doesn't work if the mailbox name is multiple lines
724 */
725 if (isset($lsub_ary[$i + 1]) &&
726 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
727 $lsub_ary[$i], $regs)) {
728 $i ++;
729 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
730 }
731
732 // if (preg_match("/^\*\s+LSUB\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$lsub_ary[$i],$regs)) {
733 // $flag = $regs[1];
734 // $mbx = trim($regs[3]);
735 // $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
736 // }
737 $mbx = find_mailbox_name($lsub_ary[$i]);
738 if (substr($mbx, -1) == $delimiter) {
739 $mbx = substr($mbx, 0, strlen($mbx) - 1);
740 }
741 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => '');
742 }
743 array_multisort($sorted_lsub_ary, SORT_ASC, SORT_REGULAR);
744
745 foreach ($sorted_lsub_ary as $mbx) {
746 if ($mbx['mbx'] == 'INBOX') {
747 $inbox_in_list = true;
748 break;
749 }
750 }
751
752 /*
753 * Just in case they're not subscribed to their inbox,
754 * we'll get it for them anyway
755 */
756 if (!$inbox_in_list) {
757 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
758 true, $response, $message);
759 /* Another workaround for EIMS */
760 if (isset($inbox_ary[1]) &&
761 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
762 $inbox_ary[0], $regs)) {
763 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
764 '"' . $regs[2];
765 }
766 $mbx = find_mailbox_name($inbox_ary[0]);
767 if (substr($mbx, -1) == $delimiter) {
768 $mbx = substr($mbx, 0, strlen($mbx) - 1);
769 }
770 if ( $mbx == 'INBOX') {
771 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => '');
772 sqimap_subscribe($imap_stream, 'INBOX');
773 }
774
775 // if (preg_match("/^\*\s+LIST\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$inbox_ary[0],$regs)) {
776 // $flag = $regs[1];
777 // $mbx = trim($regs[3]);
778 // if (substr($mbx, -1) == $delimiter) {
779 // $mbx = substr($mbx, 0, strlen($mbx) - 1);
780 // }
781 // $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag);
782 // }
783 }
784 $cnt = count($sorted_lsub_ary);
785 for ($i=0 ; $i < $cnt; $i++) {
786 $mbx = $sorted_lsub_ary[$i]['mbx'];
787 if (($unseen_notify == 2 && $mbx == 'INBOX')
788 || $unseen_notify == 3
789 || ($move_to_trash && ($mbx == $trash_folder))) {
790 $sorted_lsub_ary[$i]['unseen'] = sqimap_unseen_messages($imap_stream, $mbx);
791 if ($unseen_type == 2 || ($move_to_trash
792 && ($mbx == $trash_folder) )) {
793 $sorted_lsub_ary[$i]['nummessages'] = sqimap_get_num_messages($imap_stream, $mbx);
794 }
795 if ($mbx == $trash_folder) {
796 $sorted_lsub_ary[$i]['nummessages'] = sqimap_get_num_messages($imap_stream, $mbx);
797 }
798 }
799 }
800 $boxesnew = sqimap_fill_mailbox_tree($sorted_lsub_ary);
801 return $boxesnew;
802 }
803 }
804
805
806 function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false) {
807 global $data_dir, $username, $list_special_folders_first,
808 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
809 $move_to_trash, $move_to_sent, $save_as_draft,
810 $delimiter;
811
812 $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
813
814 /* create virtual root node */
815 $mailboxes= new mailboxes();
816 $mailboxes->is_root = true;
817 $trail_del = false;
818 if (isset($folder_prefix) && $folder_prefix != '') {
819 $start = substr_count($folder_prefix,$delimiter);
820 if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
821 $trail_del = true;
822 $mailboxes->mailboxname_full = substr($folder_prefix,0, (strlen($folder_prefix)-1));
823 } else {
824 $mailboxes->mailboxname_full = $folder_prefix;
825 $start++;
826 }
827 $mailboxes->mailboxname_sub = $mailboxes->mailboxname_full;
828 } else $start = 0;
829 $cnt = count($mbx_ary);
830 for ($i=0; $i < $cnt; $i++) {
831 if ($mbx_ary[$i]['mbx'] !='' ) {
832 $mbx = new mailboxes();
833 $mailbox = $mbx_ary[$i]['mbx'];
834 switch ($mailbox) {
835 case 'INBOX':
836 $mbx->is_inbox = true;
837 $mbx->is_special = true;
838 break;
839 case $trash_folder:
840 $mbx->is_trash = true;
841 $mbx->is_special = true;
842 break;
843 case $sent_folder:
844 $mbx->is_sent = true;
845 $mbx->is_special = true;
846 break;
847 case $draft_folder:
848 $mbx->is_draft = true;
849 $mbx->is_special = true;
850 break;
851 }
852
853 if (isset($mbx_ary[$i]['unseen'])) {
854 $mbx->unseen = $mbx_ary[$i]['unseen'];
855 }
856 if (isset($mbx_ary[$i]['nummessages'])) {
857 $mbx->total = $mbx_ary[$i]['nummessages'];
858 }
859
860 $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
861 if ($r_del_pos) {
862 $mbx->mailboxname_sub = substr($mbx_ary[$i]['mbx'],$r_del_pos+1);
863 } else { /* mailbox is root folder */
864 $mbx->mailboxname_sub = $mbx_ary[$i]['mbx'];
865 }
866 $mbx->mailboxname_full = $mbx_ary[$i]['mbx'];
867 $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);
868 }
869 }
870
871 return $mailboxes;
872 }
873
874
875 ?>