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