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