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