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