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