Phpdocumentor update - sed is your friend for these kinds of things ;)
[squirrelmail.git] / functions / imap_mailbox.php
1 <?php
2 /**
3 * imap_mailbox.php
4 *
5 * Copyright (c) 1999-2004 The SquirrelMail Project Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
7 *
8 * This impliments all functions that manipulate mailboxes
9 *
10 * @version $Id$
11 * @package squirrelmail
12 * @subpackage imap
13 */
14
15 /** UTF7 support */
16 require_once(SM_PATH . 'functions/imap_utf7_local.php');
17
18 global $boxesnew;
19
20 /**
21 * Mailboxes class
22 *
23 * FIXME. This class should be extracted and placed in a separate file that
24 * can be included before we start the session. That makes caching of the tree
25 * possible. On a refresh mailboxes from left_main.php the only function that
26 * should be called is the sqimap_get_status_mbx_tree. In case of subscribe
27 * / rename / delete / new we have to create methods for adding/changing the
28 * mailbox in the mbx_tree without the need for a refresh.
29 * @package squirrelmail
30 */
31
32 class mailboxes {
33 var $mailboxname_full = '', $mailboxname_sub= '', $is_noselect = false, $is_noinferiors = false,
34 $is_special = false, $is_root = false, $is_inbox = false, $is_sent = false,
35 $is_trash = false, $is_draft = false, $mbxs = array(),
36 $unseen = false, $total = false;
37
38 function addMbx($mbx, $delimiter, $start, $specialfirst) {
39 $ary = explode($delimiter, $mbx->mailboxname_full);
40 $mbx_parent =& $this;
41 for ($i = $start, $c = count($ary)-1; $i < $c; $i++) {
42 $mbx_childs =& $mbx_parent->mbxs;
43 $found = false;
44 if ($mbx_childs) {
45 foreach ($mbx_childs as $key => $parent) {
46 if ($parent->mailboxname_sub == $ary[$i]) {
47 $mbx_parent =& $mbx_parent->mbxs[$key];
48 $found = true;
49 break;
50 }
51 }
52 }
53 if (!$found) {
54 $no_select_mbx = new mailboxes();
55 if (isset($mbx_parent->mailboxname_full) && $mbx_parent->mailboxname_full != '') {
56 $no_select_mbx->mailboxname_full = $mbx_parent->mailboxname_full.$delimiter.$ary[$i];
57 } else {
58 $no_select_mbx->mailboxname_full = $ary[$i];
59 }
60 $no_select_mbx->mailboxname_sub = $ary[$i];
61 $no_select_mbx->is_noselect = true;
62 $mbx_parent->mbxs[] = $no_select_mbx;
63 $i--;
64 }
65 }
66 $mbx_parent->mbxs[] = $mbx;
67 if ($mbx->is_special && $specialfirst) {
68 usort($mbx_parent->mbxs, 'sortSpecialMbx');
69 }
70 }
71 }
72
73 function sortSpecialMbx($a, $b) {
74 if ($a->is_inbox) {
75 $acmp = '0'. $a->mailboxname_full;
76 } else if ($a->is_special) {
77 $acmp = '1'. $a->mailboxname_full;
78 } else {
79 $acmp = '2' . $a->mailboxname_full;
80 }
81 if ($b->is_inbox) {
82 $bcmp = '0'. $b->mailboxname_full;
83 }else if ($b->is_special) {
84 $bcmp = '1' . $b->mailboxname_full;
85 } else {
86 $bcmp = '2' . $b->mailboxname_full;
87 }
88 return strnatcasecmp($acmp, $bcmp);
89 }
90
91 function compact_mailboxes_response($ary)
92 {
93 /*
94 * Workaround for mailboxes returned as literal
95 * FIXME : Doesn't work if the mailbox name is multiple lines
96 * (larger then fgets buffer)
97 */
98 for ($i = 0, $iCnt=count($ary); $i < $iCnt; $i++) {
99 if (isset($ary[$i + 1]) && substr($ary[$i], -3) == "}\r\n") {
100 if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
101 $ary[$i], $regs)) {
102 $ary[$i] = $regs[1] . '"' . addslashes(trim($ary[$i+1])) . '"' . $regs[2];
103 array_splice($ary, $i+1, 2);
104 }
105 }
106 }
107 /* remove duplicates and ensure array is contiguous */
108 return array_values(array_unique($ary));
109 }
110
111 /**
112 * Extract the mailbox name from an untagged LIST (7.2.2) or LSUB (7.2.3) answer
113 * (LIST|LSUB) (<Flags list>) (NIL|"<separator atom>") <mailbox name string>\r\n
114 * mailbox name in quoted string MUST be unquoted and stripslashed (sm API)
115 */
116 function find_mailbox_name($line)
117 {
118 if (preg_match('/^\* (?:LIST|LSUB) \([^\)]*\) (?:NIL|\"[^\"]*\") ([^\r\n]*)[\r\n]*$/i', $line, $regs)) {
119 if (substr($regs[1], 0, 1) == '"')
120 return stripslashes(substr($regs[1], 1, -1));
121 return $regs[1];
122 }
123 return '';
124 }
125
126 /**
127 * @return bool whether this is a Noselect mailbox.
128 */
129 function check_is_noselect ($lsub_line) {
130 return preg_match("/^\* (LSUB|LIST) \([^\)]*\\\\Noselect[^\)]*\)/i", $lsub_line);
131 }
132
133 /**
134 * @return bool whether this is a Noinferiors mailbox.
135 */
136 function check_is_noinferiors ($lsub_line) {
137 return preg_match("/^\* (LSUB|LIST) \([^\)]*\\\\Noinferiors[^\)]*\)/i", $lsub_line);
138 }
139
140 /**
141 * If $haystack is a full mailbox name, and $needle is the mailbox
142 * separator character, returns the second last part of the full
143 * mailbox name (i.e. the mailbox's parent mailbox)
144 */
145 function readMailboxParent($haystack, $needle) {
146 if ($needle == '') {
147 $ret = '';
148 } else {
149 $parts = explode($needle, $haystack);
150 $elem = array_pop($parts);
151 while ($elem == '' && count($parts)) {
152 $elem = array_pop($parts);
153 }
154 $ret = join($needle, $parts);
155 }
156 return( $ret );
157 }
158
159 /**
160 * Check if $subbox is below the specified $parentbox
161 */
162 function isBoxBelow( $subbox, $parentbox ) {
163 global $delimiter;
164 /*
165 * Eliminate the obvious mismatch, where the
166 * subfolder path is shorter than that of the potential parent
167 */
168 if ( strlen($subbox) < strlen($parentbox) ) {
169 return false;
170 }
171 /* check for delimiter */
172 if (!substr($parentbox,-1) == $delimiter) {
173 $parentbox.=$delimiter;
174 }
175 if (substr($subbox,0,strlen($parentbox)) == $parentbox) {
176 return true;
177 } else {
178 return false;
179 }
180 }
181
182 /**
183 * Defines special mailboxes: given a mailbox name, it checks if this is a
184 * "special" one: INBOX, Trash, Sent or Draft.
185 */
186 function isSpecialMailbox( $box ) {
187 $ret = ( (strtolower($box) == 'inbox') ||
188 isTrashMailbox($box) || isSentMailbox($box) || isDraftMailbox($box) );
189
190 if ( !$ret ) {
191 $ret = boolean_hook_function('special_mailbox',$box,1);
192 }
193 return $ret;
194 }
195
196 /**
197 * @return bool whether this is a Trash folder
198 */
199 function isTrashMailbox ($box) {
200 global $trash_folder, $move_to_trash;
201 return $move_to_trash && $trash_folder &&
202 ( $box == $trash_folder || isBoxBelow($box, $trash_folder) );
203 }
204
205 /**
206 * @return bool whether this is a Sent folder
207 */
208 function isSentMailbox($box) {
209 global $sent_folder, $move_to_sent;
210 return $move_to_sent && $sent_folder &&
211 ( $box == $sent_folder || isBoxBelow($box, $sent_folder) );
212 }
213
214 /**
215 * @return bool whether this is a Draft folder
216 */
217 function isDraftMailbox($box) {
218 global $draft_folder, $save_as_draft;
219 return $save_as_draft &&
220 ( $box == $draft_folder || isBoxBelow($box, $draft_folder) );
221 }
222
223 /**
224 * Expunges a mailbox, ie. delete all contents.
225 */
226 function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true, $id='') {
227 if ($id) {
228 if (is_array($id)) {
229 $id = sqimap_message_list_squisher($id);
230 }
231 $id = ' '.$id;
232 $uid = TRUE;
233 } else {
234 $uid = false;
235 }
236 $read = sqimap_run_command($imap_stream, 'EXPUNGE'.$id, $handle_errors,
237 $response, $message, $uid);
238 $cnt = 0;
239
240 if (is_array($read)) {
241 foreach ($read as $r) {
242 if (preg_match('/^\*\s[0-9]+\sEXPUNGE/AUi',$r,$regs)) {
243 $cnt++;
244 }
245 }
246 }
247 return $cnt;
248 }
249
250
251 /**
252 * Expunge specified message, updated $msgs and $msort
253 *
254 * Until Marc and I come up with a better way to maintain
255 * these stupid arrays, we'll use this wrapper function to
256 * remove the message with the matching UID .. the order
257 * won't be changed - the array element for the message
258 * will just be removed.
259 */
260 function sqimap_mailbox_expunge_dmn($message_id, $aMbxResponse, &$server_sort_array)
261 {
262 global $msgs, $msort, $sort, $imapConnection,
263 $mailbox, $auto_expunge,
264 $sort, $allow_server_sort, $thread_sort_messages, $allow_thread_sort,
265 $username, $data_dir;
266 $cnt = 0;
267
268 if (!isset($sort) || $sort === false) {
269 sqgetGlobalVar('sort',$sort,SQ_GET);
270 }
271
272 if ($auto_expunge) {
273 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
274 } else {
275 return $cnt;
276 }
277
278 // Got to grab this out of prefs, since it isn't saved from mailbox_view.php
279 if ($allow_thread_sort && getPref($data_dir, $username, "thread_$mailbox",0)) {
280 $mode = 'thread';
281 } else if ($allow_server_sort) {
282 $mode = 'server_sort';
283 } else {
284 $mode = '';
285 }
286 $error = '';
287 if (is_array($server_sort_array)) {
288 $key = array_search($message_id,$server_sort_array,true);
289 if ($key !== false) {
290 unset($server_sort_array[$key]);
291 $server_sort_array = array_values($server_sort_array);
292 } else {
293 $server_sort_array = get_sorted_msgs_list($imapConnection,$sort,$mode,$error);
294 }
295 } else {
296 $server_sort_array = get_sorted_msgs_list($imapConnection,$sort,$mode,$error);
297 }
298 sqsession_register($server_sort_array,'server_sort_array');
299 return $cnt;
300 }
301
302 /**
303 * Checks whether or not the specified mailbox exists
304 */
305 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
306 if (!isset($mailbox) || empty($mailbox)) {
307 return false;
308 }
309 $mbx = sqimap_run_command($imap_stream, 'LIST "" ' . sqimap_encode_mailbox_name($mailbox),
310 true, $response, $message);
311 return isset($mbx[0]);
312 }
313
314 /**
315 * Selects a mailbox
316 */
317 function sqimap_mailbox_select ($imap_stream, $mailbox) {
318 if ($mailbox == 'None') {
319 return;
320 }
321
322 $read = sqimap_run_command($imap_stream, 'SELECT ' . sqimap_encode_mailbox_name($mailbox),
323 true, $response, $message);
324 $result = array();
325 for ($i = 0, $cnt = count($read); $i < $cnt; $i++) {
326 if (preg_match('/^\*\s+OK\s\[(\w+)\s(\w+)\]/',$read[$i], $regs)) {
327 $result[strtoupper($regs[1])] = $regs[2];
328 } else if (preg_match('/^\*\s([0-9]+)\s(\w+)/',$read[$i], $regs)) {
329 $result[strtoupper($regs[2])] = $regs[1];
330 } else {
331 if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) {
332 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ;
333 $result['PERMANENTFLAGS'] = explode(' ',strtolower($regs[1]));
334 } else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) {
335 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ;
336 $result['FLAGS'] = explode(' ',strtolower($regs[1]));
337 }
338 }
339 }
340 if (!isset($result['PERMANENTFLAGS'])) {
341 $result['PERMANENTFLAGS'] = $result['FLAGS'];
342 }
343 if (preg_match('/^\[(.+)\]/',$message, $regs)) {
344 $result['RIGHTS']=strtoupper($regs[1]);
345 }
346
347 return $result;
348 }
349
350 /**
351 * Creates a folder.
352 */
353 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
354 global $delimiter;
355 if (strtolower($type) == 'noselect') {
356 $mailbox .= $delimiter;
357 }
358
359 $read_ary = sqimap_run_command($imap_stream, 'CREATE ' .
360 sqimap_encode_mailbox_name($mailbox),
361 true, $response, $message);
362 sqimap_subscribe ($imap_stream, $mailbox);
363 }
364
365 /**
366 * Subscribes to an existing folder.
367 */
368 function sqimap_subscribe ($imap_stream, $mailbox,$debug=true) {
369 $read_ary = sqimap_run_command($imap_stream, 'SUBSCRIBE ' .
370 sqimap_encode_mailbox_name($mailbox),
371 $debug, $response, $message);
372 }
373
374 /**
375 * Unsubscribes from an existing folder
376 */
377 function sqimap_unsubscribe ($imap_stream, $mailbox) {
378 $read_ary = sqimap_run_command($imap_stream, 'UNSUBSCRIBE ' .
379 sqimap_encode_mailbox_name($mailbox),
380 false, $response, $message);
381 }
382
383 /**
384 * Deletes the given folder
385 */
386 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
387 global $data_dir, $username;
388 sqimap_unsubscribe ($imap_stream, $mailbox);
389 $read_ary = sqimap_run_command($imap_stream, 'DELETE ' .
390 sqimap_encode_mailbox_name($mailbox),
391 true, $response, $message);
392 if ($response !== 'OK') {
393 // subscribe again
394 sqimap_subscribe ($imap_stream, $mailbox);
395 } else {
396 do_hook_function('rename_or_delete_folder', $args = array($mailbox, 'delete', ''));
397 removePref($data_dir, $username, "thread_$mailbox");
398 }
399 }
400
401 /**
402 * Determines if the user is subscribed to the folder or not
403 */
404 function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
405 $boxesall = sqimap_mailbox_list ($imap_stream);
406 foreach ($boxesall as $ref) {
407 if ($ref['unformatted'] == $folder) {
408 return true;
409 }
410 }
411 return false;
412 }
413
414 /**
415 * Renames a mailbox.
416 */
417 function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
418 if ( $old_name != $new_name ) {
419 global $delimiter, $imap_server_type, $data_dir, $username;
420 if ( substr( $old_name, -1 ) == $delimiter ) {
421 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
422 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
423 $postfix = $delimiter;
424 } else {
425 $postfix = '';
426 }
427
428 $boxesall = sqimap_mailbox_list($imap_stream);
429 $cmd = 'RENAME ' . sqimap_encode_mailbox_name($old_name) .
430 ' ' . sqimap_encode_mailbox_name($new_name);
431 $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message);
432 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
433 $oldpref = getPref($data_dir, $username, 'thread_'.$old_name.$postfix);
434 removePref($data_dir, $username, 'thread_'.$old_name.$postfix);
435 sqimap_subscribe($imap_stream, $new_name.$postfix);
436 setPref($data_dir, $username, 'thread_'.$new_name.$postfix, $oldpref);
437 do_hook_function('rename_or_delete_folder',$args = array($old_name, 'rename', $new_name));
438 $l = strlen( $old_name ) + 1;
439 $p = 'unformatted';
440
441 foreach ($boxesall as $box) {
442 if (substr($box[$p], 0, $l) == $old_name . $delimiter) {
443 $new_sub = $new_name . $delimiter . substr($box[$p], $l);
444 if ($imap_server_type == 'cyrus') {
445 $cmd = 'RENAME "' . $box[$p] . '" "' . $new_sub . '"';
446 $data = sqimap_run_command($imap_stream, $cmd, true,
447 $response, $message);
448 }
449 sqimap_unsubscribe($imap_stream, $box[$p]);
450 $oldpref = getPref($data_dir, $username, 'thread_'.$box[$p]);
451 removePref($data_dir, $username, 'thread_'.$box[$p]);
452 sqimap_subscribe($imap_stream, $new_sub);
453 setPref($data_dir, $username, 'thread_'.$new_sub, $oldpref);
454 do_hook_function('rename_or_delete_folder',
455 $args = array($box[$p], 'rename', $new_sub));
456 }
457 }
458 }
459 }
460
461 /**
462 * Formats a mailbox into parts for the $boxesall array
463 *
464 * The parts are:
465 *
466 * raw - Raw LIST/LSUB response from the IMAP server
467 * formatted - nicely formatted folder name
468 * unformatted - unformatted, but with delimiter at end removed
469 * unformatted-dm - folder name as it appears in raw response
470 * unformatted-disp - unformatted without $folder_prefix
471 */
472 function sqimap_mailbox_parse ($line, $line_lsub) {
473 global $folder_prefix, $delimiter;
474
475 /* Process each folder line */
476 for ($g = 0, $cnt = count($line); $g < $cnt; ++$g) {
477 /* Store the raw IMAP reply */
478 if (isset($line[$g])) {
479 $boxesall[$g]['raw'] = $line[$g];
480 } else {
481 $boxesall[$g]['raw'] = '';
482 }
483
484 /* Count number of delimiters ($delimiter) in folder name */
485 $mailbox = /*trim(*/$line_lsub[$g]/*)*/;
486 $dm_count = substr_count($mailbox, $delimiter);
487 if (substr($mailbox, -1) == $delimiter) {
488 /* If name ends in delimiter, decrement count by one */
489 $dm_count--;
490 }
491
492 /* Format folder name, but only if it's a INBOX.* or has a parent. */
493 $boxesallbyname[$mailbox] = $g;
494 $parentfolder = readMailboxParent($mailbox, $delimiter);
495 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
496 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
497 (isset($boxesallbyname[$parentfolder]) &&
498 (strlen($parentfolder) > 0) ) ) {
499 $indent = $dm_count - (substr_count($folder_prefix, $delimiter));
500 if ($indent > 0) {
501 $boxesall[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $indent);
502 } else {
503 $boxesall[$g]['formatted'] = '';
504 }
505 $boxesall[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
506 } else {
507 $boxesall[$g]['formatted'] = imap_utf7_decode_local($mailbox);
508 }
509
510 $boxesall[$g]['unformatted-dm'] = $mailbox;
511 if (substr($mailbox, -1) == $delimiter) {
512 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
513 }
514 $boxesall[$g]['unformatted'] = $mailbox;
515 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
516 $mailbox = substr($mailbox, strlen($folder_prefix));
517 }
518 $boxesall[$g]['unformatted-disp'] = $mailbox;
519 $boxesall[$g]['id'] = $g;
520
521 $boxesall[$g]['flags'] = array();
522 if (isset($line[$g])) {
523 ereg("\(([^)]*)\)",$line[$g],$regs);
524 // FIXME Flags do contain the \ character. \NoSelect \NoInferiors
525 // and $MDNSent <= last one doesn't have the \
526 // It's better to follow RFC3501 instead of using our own naming.
527 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
528 if ($flags) {
529 $boxesall[$g]['flags'] = explode(' ', $flags);
530 }
531 }
532 }
533 return $boxesall;
534 }
535
536 /**
537 * Returns list of options (to be echoed into select statement
538 * based on available mailboxes and separators
539 * Caller should surround options with <SELECT..> </SELECT> and
540 * any formatting.
541 * $imap_stream - $imapConnection to query for mailboxes
542 * $show_selected - array containing list of mailboxes to pre-select (0 if none)
543 * $folder_skip - array of folders to keep out of option list (compared in lower)
544 * $boxes - list of already fetched boxes (for places like folder panel, where
545 * you know these options will be shown 3 times in a row.. (most often unset).
546 * $flag - flag to check for in mailbox flags, used to filter out mailboxes.
547 * 'noselect' by default to remove unselectable mailboxes.
548 * 'noinferiors' used to filter out folders that can not contain subfolders.
549 * NULL to avoid flag check entirely.
550 * NOTE: noselect and noiferiors are used internally. The IMAP representation is
551 * \NoSelect and \NoInferiors
552 * $use_long_format - override folder display preference and always show full folder name.
553 */
554 function sqimap_mailbox_option_list($imap_stream, $show_selected = 0, $folder_skip = 0, $boxes = 0,
555 $flag = 'noselect', $use_long_format = false ) {
556 global $username, $data_dir;
557 $mbox_options = '';
558 if ( $use_long_format ) {
559 $shorten_box_names = 0;
560 } else {
561 $shorten_box_names = getPref($data_dir, $username, 'mailbox_select_style', SMPREF_OFF);
562 }
563
564 if ($boxes == 0) {
565 $boxes = sqimap_mailbox_list($imap_stream);
566 }
567
568 foreach ($boxes as $boxes_part) {
569 if ($flag == NULL || !in_array($flag, $boxes_part['flags'])) {
570 $box = $boxes_part['unformatted'];
571
572 if ($folder_skip != 0 && in_array($box, $folder_skip) ) {
573 continue;
574 }
575 $lowerbox = strtolower($box);
576 // mailboxes are casesensitive => inbox.sent != inbox.Sent
577 // nevermind, to many dependencies this should be fixed!
578
579 if (strtolower($box) == 'inbox') { // inbox is special and not casesensitive
580 $box2 = _("INBOX");
581 } else {
582 switch ($shorten_box_names)
583 {
584 case 2: /* delimited, style = 2 */
585 $box2 = str_replace('&nbsp;&nbsp;', '.&nbsp;', $boxes_part['formatted']);
586 break;
587 case 1: /* indent, style = 1 */
588 $box2 = $boxes_part['formatted'];
589 break;
590 default: /* default, long names, style = 0 */
591 $box2 = str_replace(' ', '&nbsp;', htmlspecialchars(imap_utf7_decode_local($boxes_part['unformatted-disp'])));
592 break;
593 }
594 }
595 if ($show_selected != 0 && in_array($lowerbox, $show_selected) ) {
596 $mbox_options .= '<OPTION VALUE="' . htmlspecialchars($box) .'" SELECTED>'.$box2.'</OPTION>' . "\n";
597 } else {
598 $mbox_options .= '<OPTION VALUE="' . htmlspecialchars($box) .'">'.$box2.'</OPTION>' . "\n";
599 }
600 }
601 }
602 return $mbox_options;
603 }
604
605 /**
606 * Returns sorted mailbox lists in several different ways.
607 * See comment on sqimap_mailbox_parse() for info about the returned array.
608 */
609
610
611 function sqimap_mailbox_list($imap_stream, $force=false) {
612 global $default_folder_prefix;
613
614 if (!sqgetGlobalVar('boxesnew',$boxesnew,SQ_SESSION) || $force) {
615 global $data_dir, $username, $list_special_folders_first,
616 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
617 $move_to_trash, $move_to_sent, $save_as_draft,
618 $delimiter, $noselect_fix_enable, $imap_server_type;
619 $inbox_in_list = false;
620 $inbox_subscribed = false;
621 $listsubscribed = sqimap_capability($imap_stream,'LIST-SUBSCRIBED');
622
623 require_once(SM_PATH . 'include/load_prefs.php');
624
625
626 if ($listsubscribed) {
627 $lsub = 'LIST (SUBSCRIBED)';
628 } else {
629 $lsub = 'LSUB';
630 }
631
632 if ($noselect_fix_enable) {
633
634 $lsub_args = "$lsub \"$folder_prefix\" \"*%\"";
635 } else {
636 $lsub_args = "$lsub \"$folder_prefix\" \"*\"";
637 }
638 /* LSUB array */
639 $lsub_ary = sqimap_run_command ($imap_stream, $lsub_args,
640 true, $response, $message);
641 $lsub_ary = compact_mailboxes_response($lsub_ary);
642
643 $sorted_lsub_ary = array();
644 for ($i = 0, $cnt = count($lsub_ary);$i < $cnt; $i++) {
645
646 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
647 $sorted_lsub_ary[] = $temp_mailbox_name;
648 if (!$inbox_subscribed && strtoupper($temp_mailbox_name) == 'INBOX') {
649 $inbox_subscribed = true;
650 }
651 }
652
653 /* natural sort mailboxes */
654 if (isset($sorted_lsub_ary)) {
655 usort($sorted_lsub_ary, 'strnatcasecmp');
656 }
657 /*
658 * The LSUB response doesn't provide us information about \Noselect
659 * mail boxes. The LIST response does, that's why we need to do a LIST
660 * call to retrieve the flags for the mailbox
661 * Note: according RFC2060 an imap server may provide \NoSelect flags in the LSUB response.
662 * in other words, we cannot rely on it.
663 */
664 $sorted_list_ary = array();
665 // if (!$listsubscribed) {
666 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
667 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
668 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
669 }
670 else {
671 $mbx = $sorted_lsub_ary[$i];
672 }
673
674 $read = sqimap_run_command ($imap_stream, 'LIST "" ' . sqimap_encode_mailbox_name($mbx),
675 true, $response, $message);
676
677 $read = compact_mailboxes_response($read);
678
679 if (isset($read[0])) {
680 $sorted_list_ary[$i] = $read[0];
681 } else {
682 $sorted_list_ary[$i] = '';
683 }
684 }
685 // }
686 /*
687 * Just in case they're not subscribed to their inbox,
688 * we'll get it for them anyway
689 */
690 if (!$inbox_subscribed) {
691 $inbox_ary = sqimap_run_command ($imap_stream, 'LIST "" "INBOX"',
692 true, $response, $message);
693 $sorted_list_ary[] = implode('',compact_mailboxes_response($inbox_ary));
694 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
695 }
696
697 $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
698
699 /* Now, lets sort for special folders */
700 $boxesnew = $used = array();
701
702 /* Find INBOX */
703 $cnt = count($boxesall);
704 $used = array_pad($used,$cnt,false);
705 for($k = 0; $k < $cnt; ++$k) {
706 if (strtolower($boxesall[$k]['unformatted']) == 'inbox') {
707 $boxesnew[] = $boxesall[$k];
708 $used[$k] = true;
709 break;
710 }
711 }
712 /* List special folders and their subfolders, if requested. */
713 if ($list_special_folders_first) {
714 for($k = 0; $k < $cnt; ++$k) {
715 if (!$used[$k] && isSpecialMailbox($boxesall[$k]['unformatted'])) {
716 $boxesnew[] = $boxesall[$k];
717 $used[$k] = true;
718 }
719 }
720 }
721 /* Rest of the folders */
722 for($k = 0; $k < $cnt; $k++) {
723 if (!$used[$k]) {
724 $boxesnew[] = $boxesall[$k];
725 }
726 }
727 sqsession_register($boxesnew,'boxesnew');
728 }
729 return $boxesnew;
730 }
731
732 /**
733 * Returns a list of all folders, subscribed or not
734 */
735 function sqimap_mailbox_list_all($imap_stream) {
736 global $list_special_folders_first, $folder_prefix, $delimiter;
737
738 $read_ary = sqimap_run_command($imap_stream,"LIST \"$folder_prefix\" *",true,$response, $message,false);
739 $read_ary = compact_mailboxes_response($read_ary);
740
741 $g = 0;
742 $phase = 'inbox';
743 $fld_pre_length = strlen($folder_prefix);
744 for ($i = 0, $cnt = count($read_ary); $i < $cnt; $i++) {
745 /* Store the raw IMAP reply */
746 $boxes[$g]['raw'] = $read_ary[$i];
747
748 /* Count number of delimiters ($delimiter) in folder name */
749 $mailbox = find_mailbox_name($read_ary[$i]);
750 $dm_count = substr_count($mailbox, $delimiter);
751 if (substr($mailbox, -1) == $delimiter) {
752 /* If name ends in delimiter - decrement count by one */
753 $dm_count--;
754 }
755
756 /* Format folder name, but only if it's a INBOX.* or has a parent. */
757 $boxesallbyname[$mailbox] = $g;
758 $parentfolder = readMailboxParent($mailbox, $delimiter);
759 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
760 (ereg('^'.$folder_prefix, $mailbox)) ||
761 ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
762 if ($dm_count) {
763 $boxes[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $dm_count);
764 } else {
765 $boxes[$g]['formatted'] = '';
766 }
767 $boxes[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
768 } else {
769 $boxes[$g]['formatted'] = imap_utf7_decode_local($mailbox);
770 }
771
772 $boxes[$g]['unformatted-dm'] = $mailbox;
773 if (substr($mailbox, -1) == $delimiter) {
774 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
775 }
776 $boxes[$g]['unformatted'] = $mailbox;
777 $boxes[$g]['unformatted-disp'] = substr($mailbox,$fld_pre_length);
778
779 $boxes[$g]['id'] = $g;
780
781 /* Now lets get the flags for this mailbox */
782 $read_mlbx = $read_ary[$i];
783 $flags = substr($read_mlbx, strpos($read_mlbx, '(')+1);
784 $flags = substr($flags, 0, strpos($flags, ')'));
785 $flags = str_replace('\\', '', $flags);
786 $flags = trim(strtolower($flags));
787 if ($flags) {
788 $boxes[$g]['flags'] = explode(' ', $flags);
789 } else {
790 $boxes[$g]['flags'] = array();
791 }
792 $g++;
793 }
794 if(is_array($boxes)) {
795 sort ($boxes);
796 }
797
798 return $boxes;
799 }
800
801 function sqimap_mailbox_tree($imap_stream) {
802 global $boxesnew, $default_folder_prefix, $unseen_notify, $unseen_type;
803 if (true) {
804
805 global $data_dir, $username, $list_special_folders_first,
806 $folder_prefix, $delimiter, $trash_folder, $move_to_trash,
807 $imap_server_type;
808
809
810 $inbox_in_list = false;
811 $inbox_subscribed = false;
812 $noselect = false;
813 $noinferiors = false;
814
815 require_once(SM_PATH . 'include/load_prefs.php');
816
817 /* LSUB array */
818 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
819 true, $response, $message);
820 $lsub_ary = compact_mailboxes_response($lsub_ary);
821
822 /* Check to see if we have an INBOX */
823 $has_inbox = false;
824
825 for ($i = 0, $cnt = count($lsub_ary); $i < $cnt; $i++) {
826 if (preg_match("/^\*\s+LSUB.*\s\"?INBOX\"?[^(\/\.)].*$/i",$lsub_ary[$i])) {
827 $lsub_ary[$i] = strtoupper($lsub_ary[$i]);
828 // in case of an unsubscribed inbox an imap server can
829 // return the inbox in the lsub results with a \NoSelect
830 // flag.
831 if (!preg_match("/\*\s+LSUB\s+\(.*\\\\NoSelect.*\).*/i",$lsub_ary[$i])) {
832 $has_inbox = true;
833 } else {
834 // remove the result and request it again with a list
835 // response at a later stage.
836 unset($lsub_ary[$i]);
837 // re-index the array otherwise the addition of the LIST
838 // response will fail in PHP 4.1.2 and probably other older versions
839 $lsub_ary = array_values($lsub_ary);
840 }
841 break;
842 }
843 }
844
845 if ($has_inbox == false) {
846 // do a list request for inbox because we should always show
847 // inbox even if the user isn't subscribed to it.
848 $inbox_ary = sqimap_run_command ($imap_stream, 'LIST "" INBOX',
849 true, $response, $message);
850 $inbox_ary = compact_mailboxes_response($inbox_ary);
851 if (count($inbox_ary)) {
852 $lsub_ary[] = $inbox_ary[0];
853 }
854 }
855
856 /*
857 * Section about removing the last element was removed
858 * We don't return "* OK" anymore from sqimap_read_data
859 */
860
861 $sorted_lsub_ary = array();
862 $cnt = count($lsub_ary);
863 for ($i = 0; $i < $cnt; $i++) {
864 $mbx = find_mailbox_name($lsub_ary[$i]);
865
866 // only do the noselect test if !uw, is checked later. FIX ME see conf.pl setting
867 if ($imap_server_type != "uw") {
868 $noselect = check_is_noselect($lsub_ary[$i]);
869 $noinferiors = check_is_noinferiors($lsub_ary[$i]);
870 }
871 if (substr($mbx, -1) == $delimiter) {
872 $mbx = substr($mbx, 0, strlen($mbx) - 1);
873 }
874 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect, 'noinferiors' => $noinferiors);
875 }
876 // FIX ME this requires a config setting inside conf.pl instead of checking on server type
877 if ($imap_server_type == "uw") {
878 $aQuery = array();
879 $aTag = array();
880 // prepare an array with queries
881 foreach ($sorted_lsub_ary as $aMbx) {
882 $mbx = stripslashes($aMbx['mbx']);
883 sqimap_prepare_pipelined_query('LIST "" ' . sqimap_encode_mailbox_name($mbx), $tag, $aQuery, false);
884 $aTag[$tag] = $mbx;
885 }
886 $sorted_lsub_ary = array();
887 // execute all the queries at once
888 $aResponse = sqimap_run_pipelined_command ($imap_stream, $aQuery, false, $aServerResponse, $aServerMessage);
889 foreach($aTag as $tag => $mbx) {
890 if ($aServerResponse[$tag] == 'OK') {
891 $sResponse = implode('', $aResponse[$tag]);
892 $noselect = check_is_noselect($sResponse);
893 $noinferiors = check_is_noinferiors($sResponse);
894 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect, 'noinferiors' => $noinferiors);
895 }
896 }
897 $cnt = count($sorted_lsub_ary);
898 }
899 $sorted_lsub_ary = array_values($sorted_lsub_ary);
900 array_multisort($sorted_lsub_ary, SORT_ASC, SORT_REGULAR);
901 $boxesnew = sqimap_fill_mailbox_tree($sorted_lsub_ary,false,$imap_stream);
902 return $boxesnew;
903 }
904 }
905
906 function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false,$imap_stream) {
907 global $data_dir, $username, $list_special_folders_first,
908 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
909 $move_to_trash, $move_to_sent, $save_as_draft,
910 $delimiter, $imap_server_type;
911
912 $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
913
914 /* create virtual root node */
915 $mailboxes= new mailboxes();
916 $mailboxes->is_root = true;
917 $trail_del = false;
918 $start = 0;
919
920
921 if (isset($folder_prefix) && ($folder_prefix != '')) {
922 $start = substr_count($folder_prefix,$delimiter);
923 if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
924 $trail_del = true;
925 $mailboxes->mailboxname_full = substr($folder_prefix,0, (strlen($folder_prefix)-1));
926 } else {
927 $mailboxes->mailboxname_full = $folder_prefix;
928 $start++;
929 }
930 $mailboxes->mailboxname_sub = $mailboxes->mailboxname_full;
931 } else {
932 $start = 0;
933 }
934
935 $cnt = count($mbx_ary);
936 for ($i=0; $i < $cnt; $i++) {
937 if ($mbx_ary[$i]['mbx'] !='' ) {
938 $mbx = new mailboxes();
939 $mailbox = $mbx_ary[$i]['mbx'];
940
941 /*
942 sent subfolders messes up using existing code as subfolders
943 were marked, but the parents were ordered somewhere else in
944 the list, despite having "special folders at top" option set.
945 Need a better method than this.
946 */
947 /*
948 if ($mailbox == 'INBOX') {
949 $mbx->is_special = true;
950 } elseif (stristr($trash_folder , $mailbox)) {
951 $mbx->is_special = true;
952 } elseif (stristr($sent_folder , $mailbox)) {
953 $mbx->is_special = true;
954 } elseif (stristr($draft_folder , $mailbox)) {
955 $mbx->is_special = true;
956 }
957
958 switch ($mailbox) {
959 case 'INBOX':
960 $mbx->is_inbox = true;
961 $mbx->is_special = true;
962 $mbx_ary[$i]['noselect'] = false;
963 break;
964 case $trash_folder:
965 $mbx->is_trash = true;
966 $mbx->is_special = true;
967 break;
968 case $sent_folder:
969 $mbx->is_sent = true;
970 $mbx->is_special = true;
971 break;
972 case $draft_folder:
973 $mbx->is_draft = true;
974 $mbx->is_special = true;
975 break;
976 }
977 */
978 $mbx->is_special |= ($mbx->is_inbox = (strtoupper($mailbox) == 'INBOX'));
979 $mbx->is_special |= ($mbx->is_trash = isTrashMailbox($mailbox));
980 $mbx->is_special |= ($mbx->is_sent = isSentMailbox($mailbox));
981 $mbx->is_special |= ($mbx->is_draft = isDraftMailbox($mailbox));
982 if (!$mbx->is_special)
983 $mbx->is_special = boolean_hook_function('special_mailbox', $mailbox, 1);
984
985 if (isset($mbx_ary[$i]['unseen'])) {
986 $mbx->unseen = $mbx_ary[$i]['unseen'];
987 }
988 if (isset($mbx_ary[$i]['nummessages'])) {
989 $mbx->total = $mbx_ary[$i]['nummessages'];
990 }
991
992 $mbx->is_noselect = $mbx_ary[$i]['noselect'];
993 $mbx->is_noinferiors = $mbx_ary[$i]['noinferiors'];
994
995 $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
996 if ($r_del_pos) {
997 $mbx->mailboxname_sub = substr($mbx_ary[$i]['mbx'],$r_del_pos+1);
998 } else { /* mailbox is root folder */
999 $mbx->mailboxname_sub = $mbx_ary[$i]['mbx'];
1000 }
1001 $mbx->mailboxname_full = $mbx_ary[$i]['mbx'];
1002
1003 $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);
1004 }
1005 }
1006 sqimap_utf7_decode_mbx_tree($mailboxes);
1007 sqimap_get_status_mbx_tree($imap_stream,$mailboxes);
1008 return $mailboxes;
1009 }
1010
1011 function sqimap_utf7_decode_mbx_tree(&$mbx_tree) {
1012 if (strtoupper($mbx_tree->mailboxname_full) == 'INBOX')
1013 $mbx_tree->mailboxname_sub = _("INBOX");
1014 else
1015 $mbx_tree->mailboxname_sub = imap_utf7_decode_local($mbx_tree->mailboxname_sub);
1016 if ($mbx_tree->mbxs) {
1017 $iCnt = count($mbx_tree->mbxs);
1018 for ($i=0;$i<$iCnt;++$i) {
1019 $mbxs_tree->mbxs[$i] = sqimap_utf7_decode_mbx_tree($mbx_tree->mbxs[$i]);
1020 }
1021 }
1022 }
1023
1024
1025 function sqimap_tree_to_ref_array(&$mbx_tree,&$aMbxs) {
1026 if ($mbx_tree)
1027 $aMbxs[] =& $mbx_tree;
1028 if ($mbx_tree->mbxs) {
1029 $iCnt = count($mbx_tree->mbxs);
1030 for ($i=0;$i<$iCnt;++$i) {
1031 sqimap_tree_to_ref_array($mbx_tree->mbxs[$i],$aMbxs);
1032 }
1033 }
1034 }
1035
1036 function sqimap_get_status_mbx_tree($imap_stream,&$mbx_tree) {
1037 global $unseen_notify, $unseen_type, $trash_folder,$move_to_trash;
1038 $aMbxs = $aQuery = $aTag = array();
1039 sqimap_tree_to_ref_array($mbx_tree,$aMbxs);
1040 // remove the root node
1041 array_shift($aMbxs);
1042
1043 if($unseen_notify == 3) {
1044 $cnt = count($aMbxs);
1045 for($i=0;$i<$cnt;++$i) {
1046 $oMbx =& $aMbxs[$i];
1047 if (!$oMbx->is_noselect) {
1048 $mbx = $oMbx->mailboxname_full;
1049 if ($unseen_type == 2 ||
1050 ($move_to_trash && $oMbx->mailboxname_full == $trash_folder)) {
1051 $query = 'STATUS ' . sqimap_encode_mailbox_name($mbx) . ' (MESSAGES UNSEEN)';
1052 } else {
1053 $query = 'STATUS ' . sqimap_encode_mailbox_name($mbx) . ' (UNSEEN)';
1054 }
1055 sqimap_prepare_pipelined_query($query,$tag,$aQuery,false);
1056 } else {
1057 $oMbx->unseen = $oMbx->total = false;
1058 $tag = false;
1059 }
1060 $oMbx->tag = $tag;
1061 $aMbxs[$i] =& $oMbx;
1062 }
1063 // execute all the queries at once
1064 $aResponse = sqimap_run_pipelined_command ($imap_stream, $aQuery, false, $aServerResponse, $aServerMessage);
1065 $cnt = count($aMbxs);
1066 for($i=0;$i<$cnt;++$i) {
1067 $oMbx =& $aMbxs[$i];
1068 $tag = $oMbx->tag;
1069 if ($tag && $aServerResponse[$tag] == 'OK') {
1070 $sResponse = implode('', $aResponse[$tag]);
1071 if (preg_match('/UNSEEN\s+([0-9]+)/i', $sResponse, $regs)) {
1072 $oMbx->unseen = $regs[1];
1073 }
1074 if (preg_match('/MESSAGES\s+([0-9]+)/i', $sResponse, $regs)) {
1075 $oMbx->total = $regs[1];
1076 }
1077 }
1078 unset($oMbx->tag);
1079 }
1080 } else if ($unseen_notify == 2) { // INBOX only
1081 $cnt = count($aMbxs);
1082 for($i=0;$i<$cnt;++$i) {
1083 $oMbx =& $aMbxs[$i];
1084 if (strtoupper($oMbx->mailboxname_full) == 'INBOX' ||
1085 ($move_to_trash && $oMbx->mailboxname_full == $trash_folder)) {
1086 if ($unseen_type == 2 ||
1087 ($oMbx->mailboxname_full == $trash_folder && $move_to_trash)) {
1088 $aStatus = sqimap_status_messages($imap_stream,$oMbx->mailboxname_full);
1089 $oMbx->unseen = $aStatus['UNSEEN'];
1090 $oMbx->total = $aStatus['MESSAGES'];
1091 } else {
1092 $oMbx->unseen = sqimap_unseen_messages($imap_stream,$oMbx->mailboxname_full);
1093 }
1094 $aMbxs[$i] =& $oMbx;
1095 if (!$move_to_trash && $trash_folder) {
1096 break;
1097 } else {
1098 // trash comes after INBOX
1099 if ($oMbx->mailboxname_full == $trash_folder) {
1100 break;
1101 }
1102 }
1103 }
1104 }
1105 }
1106 }
1107
1108 ?>