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