Create $aCriteria to prevent a notice later on if there is nothing in it
[squirrelmail.git] / functions / imap_mailbox.php
CommitLineData
59177427 1<?php
62f7daa5 2
35586184 3/**
4 * imap_mailbox.php
5 *
4b2720f5 6 * This implements all functions that manipulate mailboxes
35586184 7 *
4b4abf93 8 * @copyright &copy; 1999-2005 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
eb19bc67 10 * @version $Id$
d6c32258 11 * @package squirrelmail
eb19bc67 12 * @subpackage imap
35586184 13 */
d6c32258 14
4b2720f5 15/** @ignore */
16if (! defined('SM_PATH')) define('SM_PATH','../');
17
d6c32258 18/** UTF7 support */
334a77f8 19require_once(SM_PATH . 'functions/imap_utf7_local.php');
20
3411d4ec 21global $boxesnew;
1da22cda 22
d6c32258 23/**
24 * Mailboxes class
4669e892 25 *
26 * FIXME. This class should be extracted and placed in a separate file that
d6c32258 27 * can be included before we start the session. That makes caching of the tree
4669e892 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
d6c32258 31 * mailbox in the mbx_tree without the need for a refresh.
4b2720f5 32 *
33 * Some code fragments are present in 1.3.0 - 1.4.4.
d6c32258 34 * @package squirrelmail
dc027257 35 * @subpackage imap
4b2720f5 36 * @since 1.5.0
dc027257 37 */
60b5724d 38class mailboxes {
86c2763d 39 var $mailboxname_full = '', $mailboxname_sub= '', $is_noselect = false, $is_noinferiors = false,
ff245fbd 40 $is_special = false, $is_root = false, $is_inbox = false, $is_sent = false,
4669e892 41 $is_trash = false, $is_draft = false, $mbxs = array(),
ff245fbd 42 $unseen = false, $total = false;
43
44 function addMbx($mbx, $delimiter, $start, $specialfirst) {
45 $ary = explode($delimiter, $mbx->mailboxname_full);
ae7df16e 46 $mbx_parent =& $this;
ff245fbd 47 for ($i = $start, $c = count($ary)-1; $i < $c; $i++) {
ae7df16e 48 $mbx_childs =& $mbx_parent->mbxs;
ff245fbd 49 $found = false;
50 if ($mbx_childs) {
51 foreach ($mbx_childs as $key => $parent) {
52 if ($parent->mailboxname_sub == $ary[$i]) {
ae7df16e 53 $mbx_parent =& $mbx_parent->mbxs[$key];
ff245fbd 54 $found = true;
ae7df16e 55 break;
ff245fbd 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 }
587ec647 66 $no_select_mbx->mailboxname_sub = $ary[$i];
ff245fbd 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 }
60b5724d 77}
78
dc027257 79/**
80 * array callback used for sorting in mailboxes class
81 * @param object $a
82 * @param object $b
3131bc8d 83 * @return integer see php strnatcasecmp()
dc027257 84 * @since 1.3.0
85 */
60b5724d 86function sortSpecialMbx($a, $b) {
87 if ($a->is_inbox) {
ff245fbd 88 $acmp = '0'. $a->mailboxname_full;
60b5724d 89 } else if ($a->is_special) {
ff245fbd 90 $acmp = '1'. $a->mailboxname_full;
60b5724d 91 } else {
ff245fbd 92 $acmp = '2' . $a->mailboxname_full;
93 }
60b5724d 94 if ($b->is_inbox) {
ff245fbd 95 $bcmp = '0'. $b->mailboxname_full;
60b5724d 96 }else if ($b->is_special) {
ff245fbd 97 $bcmp = '1' . $b->mailboxname_full;
60b5724d 98 } else {
ff245fbd 99 $bcmp = '2' . $b->mailboxname_full;
60b5724d 100 }
4669e892 101 return strnatcasecmp($acmp, $bcmp);
ff245fbd 102}
60b5724d 103
dc027257 104/**
105 * @param array $ary
106 * @return array
107 * @since 1.5.0
108 */
109function compact_mailboxes_response($ary) {
86c2763d 110 /*
111 * Workaround for mailboxes returned as literal
4669e892 112 * FIXME : Doesn't work if the mailbox name is multiple lines
86c2763d 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
48af4b64 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)
dc027257 132 *
f8a1ed5a 133 * Originally stored in functions/strings.php. Since 1.2.6 stored in
dc027257 134 * functions/imap_mailbox.php
135 * @param string $line imap LIST/LSUB response line
136 * @return string mailbox name
48af4b64 137 */
dc027257 138function find_mailbox_name($line) {
bac13dd7 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
48af4b64 147/**
dc027257 148 * Detects if mailbox has noselect flag (can't store messages)
ae9e2adc 149 * In versions older than 1.4.5 function checks only LSUB responses
150 * and can produce pcre warnings.
dc027257 151 * @param string $lsub_line mailbox line from untagged LIST or LSUB response
48af4b64 152 * @return bool whether this is a Noselect mailbox.
dc027257 153 * @since 1.3.2
48af4b64 154 */
f73348a3 155function check_is_noselect ($lsub_line) {
3698bd49 156 return preg_match("/^\* (LSUB|LIST) \([^\)]*\\\\Noselect[^\)]*\)/i", $lsub_line);
79e07c7e 157}
158
48af4b64 159/**
dc027257 160 * Detects if mailbox has noinferiors flag (can't store subfolders)
161 * @param string $lsub_line mailbox line from untagged LIST or LSUB response
48af4b64 162 * @return bool whether this is a Noinferiors mailbox.
dc027257 163 * @since 1.5.0
48af4b64 164 */
86c2763d 165function check_is_noinferiors ($lsub_line) {
166 return preg_match("/^\* (LSUB|LIST) \([^\)]*\\\\Noinferiors[^\)]*\)/i", $lsub_line);
167}
168
97b1248c 169/**
d43853c7 170 * Detects mailbox's parent folder
171 *
97b1248c 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)
d43853c7 175 *
f8a1ed5a 176 * Originally stored in functions/strings.php. Since 1.2.6 stored in
d43853c7 177 * functions/imap_mailbox.php
178 * @param string $haystack full mailbox name
179 * @param string $needle delimiter
f8a1ed5a 180 * @return string parent mailbox
97b1248c 181 */
182function readMailboxParent($haystack, $needle) {
97b1248c 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
4669e892 196/**
6a8e7cae 197 * Check if $subbox is below the specified $parentbox
d43853c7 198 * @param string $subbox potential sub folder
199 * @param string $parentbox potential parent
200 * @return boolean
201 * @since 1.2.3
6a8e7cae 202 */
203function isBoxBelow( $subbox, $parentbox ) {
cef054e4 204 global $delimiter;
4669e892 205 /*
206 * Eliminate the obvious mismatch, where the
6a8e7cae 207 * subfolder path is shorter than that of the potential parent
208 */
209 if ( strlen($subbox) < strlen($parentbox) ) {
210 return false;
211 }
cef054e4 212 /* check for delimiter */
c3ce607e 213 if (substr($parentbox,-1) != $delimiter) {
214 $parentbox .= $delimiter;
215 }
216
217 return (substr($subbox,0,strlen($parentbox)) == $parentbox);
1e18bf95 218}
219
48af4b64 220/**
221 * Defines special mailboxes: given a mailbox name, it checks if this is a
222 * "special" one: INBOX, Trash, Sent or Draft.
f8a1ed5a 223 *
d43853c7 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
48af4b64 229 */
1e18bf95 230function isSpecialMailbox( $box ) {
90de1755 231 $ret = ( (strtolower($box) == 'inbox') ||
6a8e7cae 232 isTrashMailbox($box) || isSentMailbox($box) || isDraftMailbox($box) );
90de1755 233
2586d588 234 if ( !$ret ) {
5576644b 235 $ret = boolean_hook_function('special_mailbox',$box,1);
2586d588 236 }
3411d4ec 237 return $ret;
90de1755 238}
239
48af4b64 240/**
d43853c7 241 * Detects if mailbox is a Trash folder or subfolder of Trash
242 * @param string $box mailbox name
48af4b64 243 * @return bool whether this is a Trash folder
d43853c7 244 * @since 1.4.0
48af4b64 245 */
6a8e7cae 246function 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
48af4b64 252/**
d43853c7 253 * Detects if mailbox is a Sent folder or subfolder of Sent
254 * @param string $box mailbox name
48af4b64 255 * @return bool whether this is a Sent folder
d43853c7 256 * @since 1.4.0
48af4b64 257 */
6a8e7cae 258function 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
48af4b64 264/**
d43853c7 265 * Detects if mailbox is a Drafts folder or subfolder of Drafts
266 * @param string $box mailbox name
48af4b64 267 * @return bool whether this is a Draft folder
d43853c7 268 * @since 1.4.0
48af4b64 269 */
6a8e7cae 270function isDraftMailbox($box) {
271 global $draft_folder, $save_as_draft;
272 return $save_as_draft &&
273 ( $box == $draft_folder || isBoxBelow($box, $draft_folder) );
274}
275
48af4b64 276/**
d43853c7 277 * Expunges a mailbox
f8a1ed5a 278 *
d43853c7 279 * WARNING: Select mailbox before calling this function.
f8a1ed5a 280 *
281 * permanently removes all messages that have the \Deleted flag
282 * set from the selected mailbox. See EXPUNGE command chapter in
d43853c7 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
3131bc8d 289 * @since 1.0 or older
48af4b64 290 */
8f6505f6 291function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true, $id='') {
06b5c3ff 292 if ($id) {
ff245fbd 293 if (is_array($id)) {
294 $id = sqimap_message_list_squisher($id);
295 }
296 $id = ' '.$id;
9d8d1ebd 297 $uid = TRUE;
06b5c3ff 298 } else {
ff245fbd 299 $uid = false;
8f6505f6 300 }
06b5c3ff 301 $read = sqimap_run_command($imap_stream, 'EXPUNGE'.$id, $handle_errors,
302 $response, $message, $uid);
63240b90 303 $cnt = 0;
ff245fbd 304
305 if (is_array($read)) {
63240b90 306 foreach ($read as $r) {
ff245fbd 307 if (preg_match('/^\*\s[0-9]+\sEXPUNGE/AUi',$r,$regs)) {
308 $cnt++;
309 }
63240b90 310 }
8f6505f6 311 }
ff245fbd 312 return $cnt;
43b698c7 313}
314
48af4b64 315/**
316 * Checks whether or not the specified mailbox exists
d43853c7 317 * @param stream $imap_stream imap connection resource
318 * @param string $mailbox mailbox name
319 * @return boolean
320 * @since 1.0 or older
48af4b64 321 */
1da22cda 322function sqimap_mailbox_exists ($imap_stream, $mailbox) {
247f700e 323 if (!isset($mailbox) || empty($mailbox)) {
43b698c7 324 return false;
325 }
568cb884 326 $mbx = sqimap_run_command($imap_stream, 'LIST "" ' . sqimap_encode_mailbox_name($mailbox),
3411d4ec 327 true, $response, $message);
43b698c7 328 return isset($mbx[0]);
329}
330
48af4b64 331/**
332 * Selects a mailbox
d43853c7 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
f8a1ed5a 336 * @return array results of select command (on success - permanentflags, flags and rights)
d43853c7 337 * @since 1.0 or older
48af4b64 338 */
e4c6fe41 339function sqimap_mailbox_select ($imap_stream, $mailbox) {
ff245fbd 340 if ($mailbox == 'None') {
43b698c7 341 return;
342 }
f69feefe 343
568cb884 344 $read = sqimap_run_command($imap_stream, 'SELECT ' . sqimap_encode_mailbox_name($mailbox),
3411d4ec 345 true, $response, $message);
e4c6fe41 346 $result = array();
ff245fbd 347 for ($i = 0, $cnt = count($read); $i < $cnt; $i++) {
e4c6fe41 348 if (preg_match('/^\*\s+OK\s\[(\w+)\s(\w+)\]/',$read[$i], $regs)) {
ff245fbd 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])) ;
4669e892 355 $result['PERMANENTFLAGS'] = explode(' ',strtolower($regs[1]));
ff245fbd 356 } else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) {
357 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ;
4669e892 358 $result['FLAGS'] = explode(' ',strtolower($regs[1]));
ff245fbd 359 }
360 }
e4c6fe41 361 }
4669e892 362 if (!isset($result['PERMANENTFLAGS'])) {
363 $result['PERMANENTFLAGS'] = $result['FLAGS'];
364 }
e4c6fe41 365 if (preg_match('/^\[(.+)\]/',$message, $regs)) {
4669e892 366 $result['RIGHTS']=strtoupper($regs[1]);
e4c6fe41 367 }
f69feefe 368
e4c6fe41 369 return $result;
43b698c7 370}
371
48af4b64 372/**
373 * Creates a folder.
d43853c7 374 *
375 * Mailbox is automatically subscribed.
f8a1ed5a 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
d43853c7 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
48af4b64 385 */
3411d4ec 386function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
43b698c7 387 global $delimiter;
388 if (strtolower($type) == 'noselect') {
3411d4ec 389 $mailbox .= $delimiter;
43b698c7 390 }
e429f014 391
48af4b64 392 $read_ary = sqimap_run_command($imap_stream, 'CREATE ' .
393 sqimap_encode_mailbox_name($mailbox),
3411d4ec 394 true, $response, $message);
43b698c7 395 sqimap_subscribe ($imap_stream, $mailbox);
396}
397
48af4b64 398/**
399 * Subscribes to an existing folder.
d43853c7 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
48af4b64 404 */
852abae7 405function sqimap_subscribe ($imap_stream, $mailbox,$debug=true) {
48af4b64 406 $read_ary = sqimap_run_command($imap_stream, 'SUBSCRIBE ' .
407 sqimap_encode_mailbox_name($mailbox),
852abae7 408 $debug, $response, $message);
43b698c7 409}
410
48af4b64 411/**
412 * Unsubscribes from an existing folder
d43853c7 413 * @param stream $imap_stream imap connection resource
414 * @param string $mailbox mailbox name
415 * @since 1.0 or older
48af4b64 416 */
3411d4ec 417function sqimap_unsubscribe ($imap_stream, $mailbox) {
48af4b64 418 $read_ary = sqimap_run_command($imap_stream, 'UNSUBSCRIBE ' .
419 sqimap_encode_mailbox_name($mailbox),
e2e8b92b 420 false, $response, $message);
43b698c7 421}
422
48af4b64 423/**
424 * Deletes the given folder
3131bc8d 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
48af4b64 429 */
3411d4ec 430function sqimap_mailbox_delete ($imap_stream, $mailbox) {
78cc4b12 431 global $data_dir, $username;
e2e8b92b 432 sqimap_unsubscribe ($imap_stream, $mailbox);
c957afd1 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 }
e2e8b92b 447 }
43b698c7 448}
449
48af4b64 450/**
451 * Determines if the user is subscribed to the folder or not
3131bc8d 452 * @param stream $imap_stream imap connection resource
453 * @param string $mailbox mailbox name
454 * @return boolean
455 * @since 1.2.0
48af4b64 456 */
1da22cda 457function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
1da22cda 458 $boxesall = sqimap_mailbox_list ($imap_stream);
459 foreach ($boxesall as $ref) {
43b698c7 460 if ($ref['unformatted'] == $folder) {
3411d4ec 461 return true;
43b698c7 462 }
463 }
464 return false;
465}
466
48af4b64 467/**
468 * Renames a mailbox.
3131bc8d 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
48af4b64 474 */
1c52ba77 475function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
3411d4ec 476 if ( $old_name != $new_name ) {
78cc4b12 477 global $delimiter, $imap_server_type, $data_dir, $username;
1c52ba77 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;
1c52ba77 482 } else {
483 $postfix = '';
1c52ba77 484 }
68f2ce7a 485
5659ad2e 486 $boxesall = sqimap_mailbox_list_all($imap_stream);
48af4b64 487 $cmd = 'RENAME ' . sqimap_encode_mailbox_name($old_name) .
91e0dccc 488 ' ' . sqimap_encode_mailbox_name($new_name);
3411d4ec 489 $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message);
1c52ba77 490 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
5659ad2e 491 $oldpref_thread = getPref($data_dir, $username, 'thread_'.$old_name.$postfix);
492 $oldpref_collapse = getPref($data_dir, $username, 'collapse_folder_'.$old_name.$postfix);
68f2ce7a 493 removePref($data_dir, $username, 'thread_'.$old_name.$postfix);
5659ad2e 494 removePref($data_dir, $username, 'collapse_folder_'.$old_name.$postfix);
1c52ba77 495 sqimap_subscribe($imap_stream, $new_name.$postfix);
5659ad2e 496 setPref($data_dir, $username, 'thread_'.$new_name.$postfix, $oldpref_thread);
497 setPref($data_dir, $username, 'collapse_folder_'.$new_name.$postfix, $oldpref_collapse);
e429f014 498 do_hook_function('rename_or_delete_folder',$args = array($old_name, 'rename', $new_name));
648713af 499 $l = strlen( $old_name ) + 1;
500 $p = 'unformatted';
68f2ce7a 501
ff245fbd 502 foreach ($boxesall as $box) {
503 if (substr($box[$p], 0, $l) == $old_name . $delimiter) {
648713af 504 $new_sub = $new_name . $delimiter . substr($box[$p], $l);
5659ad2e 505 /* With Cyrus IMAPd >= 2.0 rename is recursive, so don't check for errors here */
648713af 506 if ($imap_server_type == 'cyrus') {
68f2ce7a 507 $cmd = 'RENAME "' . $box[$p] . '" "' . $new_sub . '"';
5659ad2e 508 $data = sqimap_run_command($imap_stream, $cmd, false,
648713af 509 $response, $message);
1c52ba77 510 }
5659ad2e 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]);
68f2ce7a 517 removePref($data_dir, $username, 'thread_'.$box[$p]);
5659ad2e 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);
68f2ce7a 524 do_hook_function('rename_or_delete_folder',
3411d4ec 525 $args = array($box[$p], 'rename', $new_sub));
1c52ba77 526 }
527 }
1c52ba77 528 }
1c52ba77 529}
43b698c7 530
48af4b64 531/**
86c2763d 532 * Formats a mailbox into parts for the $boxesall array
3411d4ec 533 *
86c2763d 534 * The parts are:
3131bc8d 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.
3411d4ec 550 */
551function sqimap_mailbox_parse ($line, $line_lsub) {
43b698c7 552 global $folder_prefix, $delimiter;
3411d4ec 553
43b698c7 554 /* Process each folder line */
cef054e4 555 for ($g = 0, $cnt = count($line); $g < $cnt; ++$g) {
43b698c7 556 /* Store the raw IMAP reply */
557 if (isset($line[$g])) {
e429f014 558 $boxesall[$g]['raw'] = $line[$g];
ff245fbd 559 } else {
e429f014 560 $boxesall[$g]['raw'] = '';
43b698c7 561 }
3411d4ec 562
43b698c7 563 /* Count number of delimiters ($delimiter) in folder name */
86c2763d 564 $mailbox = /*trim(*/$line_lsub[$g]/*)*/;
ff245fbd 565 $dm_count = substr_count($mailbox, $delimiter);
43b698c7 566 if (substr($mailbox, -1) == $delimiter) {
3411d4ec 567 /* If name ends in delimiter, decrement count by one */
568 $dm_count--;
43b698c7 569 }
3411d4ec 570
571 /* Format folder name, but only if it's a INBOX.* or has a parent. */
1da22cda 572 $boxesallbyname[$mailbox] = $g;
43b698c7 573 $parentfolder = readMailboxParent($mailbox, $delimiter);
574 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
575 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
ff245fbd 576 (isset($boxesallbyname[$parentfolder]) &&
577 (strlen($parentfolder) > 0) ) ) {
578 $indent = $dm_count - (substr_count($folder_prefix, $delimiter));
43b698c7 579 if ($indent > 0) {
ff245fbd 580 $boxesall[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $indent);
581 } else {
1da22cda 582 $boxesall[$g]['formatted'] = '';
43b698c7 583 }
447b2166 584 $boxesall[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
ff245fbd 585 } else {
447b2166 586 $boxesall[$g]['formatted'] = imap_utf7_decode_local($mailbox);
43b698c7 587 }
90de1755 588
1da22cda 589 $boxesall[$g]['unformatted-dm'] = $mailbox;
43b698c7 590 if (substr($mailbox, -1) == $delimiter) {
8e9e8afa 591 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
43b698c7 592 }
1da22cda 593 $boxesall[$g]['unformatted'] = $mailbox;
43b698c7 594 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
631b9da3 595 $mailbox = substr($mailbox, strlen($folder_prefix));
43b698c7 596 }
1da22cda 597 $boxesall[$g]['unformatted-disp'] = $mailbox;
598 $boxesall[$g]['id'] = $g;
90de1755 599
1da22cda 600 $boxesall[$g]['flags'] = array();
43b698c7 601 if (isset($line[$g])) {
36dfb0c9 602 ereg("\(([^)]*)\)",$line[$g],$regs);
4669e892 603 // FIXME Flags do contain the \ character. \NoSelect \NoInferiors
5c300c60 604 // and $MDNSent <= last one doesn't have the \
605 // It's better to follow RFC3501 instead of using our own naming.
1a7e1e97 606 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
43b698c7 607 if ($flags) {
1da22cda 608 $boxesall[$g]['flags'] = explode(' ', $flags);
43b698c7 609 }
610 }
611 }
1da22cda 612 return $boxesall;
43b698c7 613}
614
48af4b64 615/**
be2d5495 616 * Returns list of options (to be echoed into select statement
617 * based on available mailboxes and separators
6fd95361 618 * Caller should surround options with <select ...> </select> and
be2d5495 619 * any formatting.
3131bc8d 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
be2d5495 624 * you know these options will be shown 3 times in a row.. (most often unset).
3131bc8d 625 * @param string $flag (since 1.4.1) flag to check for in mailbox flags, used to filter out mailboxes.
59a8e3e8 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.
d0928dd5 629 * NOTE: noselect and noiferiors are used internally. The IMAP representation is
630 * \NoSelect and \NoInferiors
3131bc8d 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
be2d5495 634 */
4669e892 635function sqimap_mailbox_option_list($imap_stream, $show_selected = 0, $folder_skip = 0, $boxes = 0,
59a8e3e8 636 $flag = 'noselect', $use_long_format = false ) {
be2d5495 637 global $username, $data_dir;
638 $mbox_options = '';
45f836eb 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 }
ff245fbd 644
645 if ($boxes == 0) {
be2d5495 646 $boxes = sqimap_mailbox_list($imap_stream);
ff245fbd 647 }
59a8e3e8 648
be2d5495 649 foreach ($boxes as $boxes_part) {
4df5d823 650 if ($flag == NULL || (is_array($boxes_part['flags'])
651 && !in_array($flag, $boxes_part['flags']))) {
be2d5495 652 $box = $boxes_part['unformatted'];
be2d5495 653
d0928dd5 654 if ($folder_skip != 0 && in_array($box, $folder_skip) ) {
be2d5495 655 continue;
656 }
4669e892 657 $lowerbox = strtolower($box);
5c300c60 658 // mailboxes are casesensitive => inbox.sent != inbox.Sent
659 // nevermind, to many dependencies this should be fixed!
4669e892 660
d0928dd5 661 if (strtolower($box) == 'inbox') { // inbox is special and not casesensitive
be2d5495 662 $box2 = _("INBOX");
4669e892 663 } else {
5c300c60 664 switch ($shorten_box_names)
665 {
666 case 2: /* delimited, style = 2 */
c77a0487 667 $box2 = str_replace('&amp;nbsp;&amp;nbsp;', '.&nbsp;', htmlspecialchars($boxes_part['formatted']));
5c300c60 668 break;
d0928dd5 669 case 1: /* indent, style = 1 */
c77a0487 670 $box2 = str_replace('&amp;nbsp;&amp;nbsp;', '&nbsp;&nbsp;', htmlspecialchars($boxes_part['formatted']));
5c300c60 671 break;
d0928dd5 672 default: /* default, long names, style = 0 */
6c4f0fa1 673 $box2 = str_replace(' ', '&nbsp;', htmlspecialchars(imap_utf7_decode_local($boxes_part['unformatted-disp'])));
5c300c60 674 break;
675 }
be2d5495 676 }
677 if ($show_selected != 0 && in_array($lowerbox, $show_selected) ) {
ff0969a0 678 $mbox_options .= '<option value="' . htmlspecialchars($box) .'" selected="selected">'.$box2.'</option>' . "\n";
be2d5495 679 } else {
ff0969a0 680 $mbox_options .= '<option value="' . htmlspecialchars($box) .'">'.$box2.'</option>' . "\n";
be2d5495 681 }
682 }
683 }
684 return $mbox_options;
685}
43b698c7 686
48af4b64 687/**
4669e892 688 * Returns sorted mailbox lists in several different ways.
3411d4ec 689 * See comment on sqimap_mailbox_parse() for info about the returned array.
3e9441a2 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
3131bc8d 693 * @since 1.0 or older
3411d4ec 694 */
fe6efa94 695function sqimap_mailbox_list($imap_stream, $force=false) {
fe6efa94 696 if (!sqgetGlobalVar('boxesnew',$boxesnew,SQ_SESSION) || $force) {
3411d4ec 697 global $data_dir, $username, $list_special_folders_first,
7e235a1a 698 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
699 $move_to_trash, $move_to_sent, $save_as_draft,
4d5f2b31 700 $delimiter, $noselect_fix_enable, $imap_server_type,
701 $show_only_subscribed_folders;
3411d4ec 702 $inbox_subscribed = false;
60a132f8 703 $listsubscribed = sqimap_capability($imap_stream,'LIST-SUBSCRIBED');
7e235a1a 704
08185f2a 705 require_once(SM_PATH . 'include/load_prefs.php');
7e235a1a 706
4d5f2b31 707 if (!$show_only_subscribed_folders) {
708 $lsub = 'LIST';
709 } elseif ($listsubscribed) {
60a132f8 710 $lsub = 'LIST (SUBSCRIBED)';
711 } else {
712 $lsub = 'LSUB';
4669e892 713 }
714
ff245fbd 715 if ($noselect_fix_enable) {
60a132f8 716 $lsub_args = "$lsub \"$folder_prefix\" \"*%\"";
ff245fbd 717 } else {
60a132f8 718 $lsub_args = "$lsub \"$folder_prefix\" \"*\"";
ff245fbd 719 }
3411d4ec 720 /* LSUB array */
ca85aabe 721 $lsub_ary = sqimap_run_command ($imap_stream, $lsub_args,
3411d4ec 722 true, $response, $message);
4669e892 723 $lsub_ary = compact_mailboxes_response($lsub_ary);
7e235a1a 724
7e235a1a 725 $sorted_lsub_ary = array();
ff245fbd 726 for ($i = 0, $cnt = count($lsub_ary);$i < $cnt; $i++) {
159d2af7 727
7e235a1a 728 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
729 $sorted_lsub_ary[] = $temp_mailbox_name;
cef054e4 730 if (!$inbox_subscribed && strtoupper($temp_mailbox_name) == 'INBOX') {
3411d4ec 731 $inbox_subscribed = true;
7e235a1a 732 }
733 }
cef054e4 734
5c300c60 735 /* natural sort mailboxes */
7e235a1a 736 if (isset($sorted_lsub_ary)) {
159d2af7 737 usort($sorted_lsub_ary, 'strnatcasecmp');
7e235a1a 738 }
5c300c60 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
cef054e4 743 * Note: according RFC2060 an imap server may provide \NoSelect flags in the LSUB response.
744 * in other words, we cannot rely on it.
fe6efa94 745 */
cef054e4 746 $sorted_list_ary = array();
146b46ac 747 if (!$listsubscribed && $show_only_subscribed_folders) {
60a132f8 748 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
7e235a1a 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 }
fe6efa94 755
159d2af7 756 $read = sqimap_run_command ($imap_stream, 'LIST "" ' . sqimap_encode_mailbox_name($mbx),
3411d4ec 757 true, $response, $message);
4669e892 758
159d2af7 759 $read = compact_mailboxes_response($read);
4669e892 760
7e235a1a 761 if (isset($read[0])) {
762 $sorted_list_ary[$i] = $read[0];
cef054e4 763 } else {
7e235a1a 764 $sorted_list_ary[$i] = '';
765 }
60a132f8 766 }
146b46ac 767 } else {
768 $sorted_list_ary = $sorted_lsub_ary;
769 }
3411d4ec 770 /*
7e235a1a 771 * Just in case they're not subscribed to their inbox,
772 * we'll get it for them anyway
773 */
cef054e4 774 if (!$inbox_subscribed) {
159d2af7 775 $inbox_ary = sqimap_run_command ($imap_stream, 'LIST "" "INBOX"',
3411d4ec 776 true, $response, $message);
159d2af7 777 $sorted_list_ary[] = implode('',compact_mailboxes_response($inbox_ary));
7e235a1a 778 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
779 }
780
781 $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
782
3411d4ec 783 /* Now, lets sort for special folders */
7e235a1a 784 $boxesnew = $used = array();
785
786 /* Find INBOX */
ff245fbd 787 $cnt = count($boxesall);
5c300c60 788 $used = array_pad($used,$cnt,false);
cef054e4 789 for($k = 0; $k < $cnt; ++$k) {
ff245fbd 790 if (strtolower($boxesall[$k]['unformatted']) == 'inbox') {
791 $boxesnew[] = $boxesall[$k];
3411d4ec 792 $used[$k] = true;
5c300c60 793 break;
7e235a1a 794 }
795 }
7e235a1a 796 /* List special folders and their subfolders, if requested. */
3411d4ec 797 if ($list_special_folders_first) {
cef054e4 798 for($k = 0; $k < $cnt; ++$k) {
ff245fbd 799 if (!$used[$k] && isSpecialMailbox($boxesall[$k]['unformatted'])) {
800 $boxesnew[] = $boxesall[$k];
801 $used[$k] = true;
7e235a1a 802 }
5c300c60 803 }
804 }
c3ce607e 805
806 /* Find INBOX's children */
807 for($k = 0; $k < $cnt; ++$k) {
f8a1ed5a 808 if (!$used[$k] && isBoxBelow(strtolower($boxesall[$k]['unformatted']), 'inbox') &&
66c1aad9 809 strtolower($boxesall[$k]['unformatted']) != 'inbox') {
c3ce607e 810 $boxesnew[] = $boxesall[$k];
811 $used[$k] = true;
f8a1ed5a 812 }
c3ce607e 813 }
f8a1ed5a 814
c3ce607e 815 /* Rest of the folders */
ff245fbd 816 for($k = 0; $k < $cnt; $k++) {
817 if (!$used[$k]) {
818 $boxesnew[] = $boxesall[$k];
7e235a1a 819 }
820 }
fe6efa94 821 sqsession_register($boxesnew,'boxesnew');
43b698c7 822 }
3411d4ec 823 return $boxesnew;
43b698c7 824}
825
48af4b64 826/**
3131bc8d 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
90de1755 831 */
1da22cda 832function sqimap_mailbox_list_all($imap_stream) {
3411d4ec 833 global $list_special_folders_first, $folder_prefix, $delimiter;
bac13dd7 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
43b698c7 838 $g = 0;
7d82bceb 839 $fld_pre_length = strlen($folder_prefix);
ff245fbd 840 for ($i = 0, $cnt = count($read_ary); $i < $cnt; $i++) {
780dd344 841 /* Store the raw IMAP reply */
842 $boxes[$g]['raw'] = $read_ary[$i];
90de1755 843
780dd344 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 }
90de1755 851
780dd344 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);
90de1755 860 } else {
780dd344 861 $boxes[$g]['formatted'] = '';
12d61439 862 }
780dd344 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();
43b698c7 887 }
888 $g++;
889 }
890 if(is_array($boxes)) {
e429f014 891 sort ($boxes);
43b698c7 892 }
90de1755 893
43b698c7 894 return $boxes;
895}
5bdd7223 896
3131bc8d 897/**
4b2720f5 898 * Fills mailbox object
899 *
900 * Some code fragments are present in 1.3.0 - 1.4.4.
3131bc8d 901 * @param stream $imap_stream imap connection resource
902 * @return object see mailboxes class.
4b2720f5 903 * @since 1.5.0
3131bc8d 904 */
60b5724d 905function sqimap_mailbox_tree($imap_stream) {
4b2720f5 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;
60b5724d 909
18ef8f2b 910 // TODO: implement mailbox tree caching. maybe store object in session?
911
4b2720f5 912 $noselect = false;
913 $noinferiors = false;
60b5724d 914
4b2720f5 915 require_once(SM_PATH . 'include/load_prefs.php');
4d5f2b31 916
4b2720f5 917 if ($show_only_subscribed_folders) {
918 $lsub_cmd = 'LSUB';
919 } else {
920 $lsub_cmd = 'LIST';
921 }
60b5724d 922
4b2720f5 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++) {
87aa4014 932 if (preg_match("/^\*\s+$lsub_cmd.*\s\"?INBOX\"?\s*$/i",$lsub_ary[$i])) {
4b2720f5 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);
78bc908d 946 }
4b2720f5 947 break;
78bc908d 948 }
4b2720f5 949 }
78bc908d 950
4b2720f5 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];
78bc908d 959 }
4b2720f5 960 }
78bc908d 961
4b2720f5 962 /*
963 * Section about removing the last element was removed
964 * We don't return "* OK" anymore from sqimap_read_data
965 */
bac13dd7 966
4b2720f5 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 }
60b5724d 992 $sorted_lsub_ary = array();
4b2720f5 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);
a2e66c6d 1001 }
60b5724d 1002 }
4b2720f5 1003 $cnt = count($sorted_lsub_ary);
60b5724d 1004 }
4b2720f5 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;
60b5724d 1009}
1010
3131bc8d 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 */
9d8d1ebd 1018function mbxSort($a, $b) {
1019 return strnatcasecmp($a['mbx'], $b['mbx']);
1020}
1021
3131bc8d 1022/**
4b2720f5 1023 * Fills mailbox object
1024 *
1025 * Some code fragments are present in 1.3.0 - 1.4.4.
3131bc8d 1026 * @param array $mbx_ary
1027 * @param $mbxs
4b2720f5 1028 * @param stream $imap_stream imap connection resource
3131bc8d 1029 * @return object see mailboxes class
4b2720f5 1030 * @since 1.5.0
3131bc8d 1031 */
483f9ef9 1032function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false,$imap_stream) {
60b5724d 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,
43a31298 1036 $delimiter, $imap_server_type;
60b5724d 1037
8d8da447 1038 // $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
ff245fbd 1039
60b5724d 1040 /* create virtual root node */
1041 $mailboxes= new mailboxes();
1042 $mailboxes->is_root = true;
ff245fbd 1043 $trail_del = false;
78bc908d 1044 $start = 0;
1045
587ec647 1046
43a31298 1047 if (isset($folder_prefix) && ($folder_prefix != '')) {
ff245fbd 1048 $start = substr_count($folder_prefix,$delimiter);
1049 if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
ff245fbd 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 }
78bc908d 1059
9d8d1ebd 1060 $cnt = count($mbx_ary);
e4c6fe41 1061 for ($i=0; $i < $cnt; $i++) {
ff245fbd 1062 if ($mbx_ary[$i]['mbx'] !='' ) {
1063 $mbx = new mailboxes();
1064 $mailbox = $mbx_ary[$i]['mbx'];
cc82c2b7 1065
4669e892 1066 /*
ae9e2adc 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 */
8edc9f31 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));
ae9e2adc 1075
8edc9f31 1076 if (!$mbx->is_special)
fda01075 1077 $mbx->is_special = boolean_hook_function('special_mailbox', $mailbox, 1);
4669e892 1078
ff245fbd 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'];
86c2763d 1087 $mbx->is_noinferiors = $mbx_ary[$i]['noinferiors'];
ff245fbd 1088
60b5724d 1089 $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
ff245fbd 1090 if ($r_del_pos) {
587ec647 1091 $mbx->mailboxname_sub = substr($mbx_ary[$i]['mbx'],$r_del_pos+1);
ff245fbd 1092 } else { /* mailbox is root folder */
587ec647 1093 $mbx->mailboxname_sub = $mbx_ary[$i]['mbx'];
ff245fbd 1094 }
1095 $mbx->mailboxname_full = $mbx_ary[$i]['mbx'];
38068e69 1096
9871bdaa 1097 $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);
ff245fbd 1098 }
60b5724d 1099 }
587ec647 1100 sqimap_utf7_decode_mbx_tree($mailboxes);
483f9ef9 1101 sqimap_get_status_mbx_tree($imap_stream,$mailboxes);
60b5724d 1102 return $mailboxes;
1103}
259faa39 1104
3131bc8d 1105/**
1106 * @param object $mbx_tree
1107 * @since 1.5.0
1108 */
587ec647 1109function sqimap_utf7_decode_mbx_tree(&$mbx_tree) {
9e0f10a8 1110
bd27b70b 1111 if (strtoupper($mbx_tree->mailboxname_full) == 'INBOX')
d94c8d61 1112 $mbx_tree->mailboxname_sub = _("INBOX");
1113 else
1114 $mbx_tree->mailboxname_sub = imap_utf7_decode_local($mbx_tree->mailboxname_sub);
587ec647 1115 if ($mbx_tree->mbxs) {
1116 $iCnt = count($mbx_tree->mbxs);
1117 for ($i=0;$i<$iCnt;++$i) {
9e0f10a8 1118 sqimap_utf7_decode_mbx_tree($mbx_tree->mbxs[$i]);
587ec647 1119 }
1120 }
483f9ef9 1121}
1122
3131bc8d 1123/**
1124 * @param object $mbx_tree
1125 * @param array $aMbxs
1126 * @since 1.5.0
1127 */
483f9ef9 1128function sqimap_tree_to_ref_array(&$mbx_tree,&$aMbxs) {
5c300c60 1129 if ($mbx_tree)
483f9ef9 1130 $aMbxs[] =& $mbx_tree;
1131 if ($mbx_tree->mbxs) {
1132 $iCnt = count($mbx_tree->mbxs);
1133 for ($i=0;$i<$iCnt;++$i) {
5c300c60 1134 sqimap_tree_to_ref_array($mbx_tree->mbxs[$i],$aMbxs);
483f9ef9 1135 }
1136 }
4669e892 1137}
483f9ef9 1138
3131bc8d 1139/**
1140 * @param stream $imap_stream imap connection resource
1141 * @param object $mbx_tree
1142 * @since since 1.5.0
1143 */
483f9ef9 1144function sqimap_get_status_mbx_tree($imap_stream,&$mbx_tree) {
1145 global $unseen_notify, $unseen_type, $trash_folder,$move_to_trash;
8d8da447 1146 $aMbxs = $aQuery = array();
483f9ef9 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) {
5c300c60 1154 $oMbx =& $aMbxs[$i];
1155 if (!$oMbx->is_noselect) {
483f9ef9 1156 $mbx = $oMbx->mailboxname_full;
5c300c60 1157 if ($unseen_type == 2 ||
1158 ($move_to_trash && $oMbx->mailboxname_full == $trash_folder)) {
568cb884 1159 $query = 'STATUS ' . sqimap_encode_mailbox_name($mbx) . ' (MESSAGES UNSEEN)';
5c300c60 1160 } else {
568cb884 1161 $query = 'STATUS ' . sqimap_encode_mailbox_name($mbx) . ' (UNSEEN)';
5c300c60 1162 }
483f9ef9 1163 sqimap_prepare_pipelined_query($query,$tag,$aQuery,false);
5c300c60 1164 } else {
1165 $oMbx->unseen = $oMbx->total = false;
1166 $tag = false;
1167 }
1168 $oMbx->tag = $tag;
1169 $aMbxs[$i] =& $oMbx;
483f9ef9 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) {
5c300c60 1175 $oMbx =& $aMbxs[$i];
1176 $tag = $oMbx->tag;
1177 if ($tag && $aServerResponse[$tag] == 'OK') {
1178 $sResponse = implode('', $aResponse[$tag]);
483f9ef9 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];
5c300c60 1184 }
1185 }
1186 unset($oMbx->tag);
1187 }
483f9ef9 1188 } else if ($unseen_notify == 2) { // INBOX only
1189 $cnt = count($aMbxs);
1190 for($i=0;$i<$cnt;++$i) {
5c300c60 1191 $oMbx =& $aMbxs[$i];
1192 if (strtoupper($oMbx->mailboxname_full) == 'INBOX' ||
1193 ($move_to_trash && $oMbx->mailboxname_full == $trash_folder)) {
4669e892 1194 if ($unseen_type == 2 ||
5c300c60 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 }
4669e892 1213 }
1214}
587ec647 1215
367fb0ca 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 */
1226function 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 */
1247function 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
9e0f10a8 1258?>