Added new scheme to allow multiple plugins to share the onsubmit handler for the...
[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();
60a132f8 747 // if (!$listsubscribed) {
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 }
767 // }
3411d4ec 768 /*
7e235a1a 769 * Just in case they're not subscribed to their inbox,
770 * we'll get it for them anyway
771 */
cef054e4 772 if (!$inbox_subscribed) {
159d2af7 773 $inbox_ary = sqimap_run_command ($imap_stream, 'LIST "" "INBOX"',
3411d4ec 774 true, $response, $message);
159d2af7 775 $sorted_list_ary[] = implode('',compact_mailboxes_response($inbox_ary));
7e235a1a 776 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
777 }
778
779 $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
780
3411d4ec 781 /* Now, lets sort for special folders */
7e235a1a 782 $boxesnew = $used = array();
783
784 /* Find INBOX */
ff245fbd 785 $cnt = count($boxesall);
5c300c60 786 $used = array_pad($used,$cnt,false);
cef054e4 787 for($k = 0; $k < $cnt; ++$k) {
ff245fbd 788 if (strtolower($boxesall[$k]['unformatted']) == 'inbox') {
789 $boxesnew[] = $boxesall[$k];
3411d4ec 790 $used[$k] = true;
5c300c60 791 break;
7e235a1a 792 }
793 }
7e235a1a 794 /* List special folders and their subfolders, if requested. */
3411d4ec 795 if ($list_special_folders_first) {
cef054e4 796 for($k = 0; $k < $cnt; ++$k) {
ff245fbd 797 if (!$used[$k] && isSpecialMailbox($boxesall[$k]['unformatted'])) {
798 $boxesnew[] = $boxesall[$k];
799 $used[$k] = true;
7e235a1a 800 }
5c300c60 801 }
802 }
c3ce607e 803
804 /* Find INBOX's children */
805 for($k = 0; $k < $cnt; ++$k) {
f8a1ed5a 806 if (!$used[$k] && isBoxBelow(strtolower($boxesall[$k]['unformatted']), 'inbox') &&
66c1aad9 807 strtolower($boxesall[$k]['unformatted']) != 'inbox') {
c3ce607e 808 $boxesnew[] = $boxesall[$k];
809 $used[$k] = true;
f8a1ed5a 810 }
c3ce607e 811 }
f8a1ed5a 812
c3ce607e 813 /* Rest of the folders */
ff245fbd 814 for($k = 0; $k < $cnt; $k++) {
815 if (!$used[$k]) {
816 $boxesnew[] = $boxesall[$k];
7e235a1a 817 }
818 }
fe6efa94 819 sqsession_register($boxesnew,'boxesnew');
43b698c7 820 }
3411d4ec 821 return $boxesnew;
43b698c7 822}
823
48af4b64 824/**
3131bc8d 825 * Returns a list of all folders, subscribed or not
826 * @param stream $imap_stream imap connection resource
827 * @return array see sqimap_mailbox_parse()
828 * @since 1.0 or older
90de1755 829 */
1da22cda 830function sqimap_mailbox_list_all($imap_stream) {
3411d4ec 831 global $list_special_folders_first, $folder_prefix, $delimiter;
bac13dd7 832
833 $read_ary = sqimap_run_command($imap_stream,"LIST \"$folder_prefix\" *",true,$response, $message,false);
834 $read_ary = compact_mailboxes_response($read_ary);
835
43b698c7 836 $g = 0;
7d82bceb 837 $fld_pre_length = strlen($folder_prefix);
ff245fbd 838 for ($i = 0, $cnt = count($read_ary); $i < $cnt; $i++) {
780dd344 839 /* Store the raw IMAP reply */
840 $boxes[$g]['raw'] = $read_ary[$i];
90de1755 841
780dd344 842 /* Count number of delimiters ($delimiter) in folder name */
843 $mailbox = find_mailbox_name($read_ary[$i]);
844 $dm_count = substr_count($mailbox, $delimiter);
845 if (substr($mailbox, -1) == $delimiter) {
846 /* If name ends in delimiter - decrement count by one */
847 $dm_count--;
848 }
90de1755 849
780dd344 850 /* Format folder name, but only if it's a INBOX.* or has a parent. */
851 $boxesallbyname[$mailbox] = $g;
852 $parentfolder = readMailboxParent($mailbox, $delimiter);
853 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
854 (ereg('^'.$folder_prefix, $mailbox)) ||
855 ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
856 if ($dm_count) {
857 $boxes[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $dm_count);
90de1755 858 } else {
780dd344 859 $boxes[$g]['formatted'] = '';
12d61439 860 }
780dd344 861 $boxes[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
862 } else {
863 $boxes[$g]['formatted'] = imap_utf7_decode_local($mailbox);
864 }
865
866 $boxes[$g]['unformatted-dm'] = $mailbox;
867 if (substr($mailbox, -1) == $delimiter) {
868 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
869 }
870 $boxes[$g]['unformatted'] = $mailbox;
871 $boxes[$g]['unformatted-disp'] = substr($mailbox,$fld_pre_length);
872
873 $boxes[$g]['id'] = $g;
874
875 /* Now lets get the flags for this mailbox */
876 $read_mlbx = $read_ary[$i];
877 $flags = substr($read_mlbx, strpos($read_mlbx, '(')+1);
878 $flags = substr($flags, 0, strpos($flags, ')'));
879 $flags = str_replace('\\', '', $flags);
880 $flags = trim(strtolower($flags));
881 if ($flags) {
882 $boxes[$g]['flags'] = explode(' ', $flags);
883 } else {
884 $boxes[$g]['flags'] = array();
43b698c7 885 }
886 $g++;
887 }
888 if(is_array($boxes)) {
e429f014 889 sort ($boxes);
43b698c7 890 }
90de1755 891
43b698c7 892 return $boxes;
893}
5bdd7223 894
3131bc8d 895/**
4b2720f5 896 * Fills mailbox object
897 *
898 * Some code fragments are present in 1.3.0 - 1.4.4.
3131bc8d 899 * @param stream $imap_stream imap connection resource
900 * @return object see mailboxes class.
4b2720f5 901 * @since 1.5.0
3131bc8d 902 */
60b5724d 903function sqimap_mailbox_tree($imap_stream) {
4b2720f5 904 global $default_folder_prefix, $data_dir, $username, $list_special_folders_first,
905 $folder_prefix, $delimiter, $trash_folder, $move_to_trash,
906 $imap_server_type, $show_only_subscribed_folders;
60b5724d 907
18ef8f2b 908 // TODO: implement mailbox tree caching. maybe store object in session?
909
4b2720f5 910 $noselect = false;
911 $noinferiors = false;
60b5724d 912
4b2720f5 913 require_once(SM_PATH . 'include/load_prefs.php');
4d5f2b31 914
4b2720f5 915 if ($show_only_subscribed_folders) {
916 $lsub_cmd = 'LSUB';
917 } else {
918 $lsub_cmd = 'LIST';
919 }
60b5724d 920
4b2720f5 921 /* LSUB array */
922 $lsub_ary = sqimap_run_command ($imap_stream, "$lsub_cmd \"$folder_prefix\" \"*\"",
923 true, $response, $message);
924 $lsub_ary = compact_mailboxes_response($lsub_ary);
925
926 /* Check to see if we have an INBOX */
927 $has_inbox = false;
928
929 for ($i = 0, $cnt = count($lsub_ary); $i < $cnt; $i++) {
87aa4014 930 if (preg_match("/^\*\s+$lsub_cmd.*\s\"?INBOX\"?\s*$/i",$lsub_ary[$i])) {
4b2720f5 931 $lsub_ary[$i] = strtoupper($lsub_ary[$i]);
932 // in case of an unsubscribed inbox an imap server can
933 // return the inbox in the lsub results with a \NoSelect
934 // flag.
935 if (!preg_match("/\*\s+$lsub_cmd\s+\(.*\\\\NoSelect.*\).*/i",$lsub_ary[$i])) {
936 $has_inbox = true;
937 } else {
938 // remove the result and request it again with a list
939 // response at a later stage.
940 unset($lsub_ary[$i]);
941 // re-index the array otherwise the addition of the LIST
942 // response will fail in PHP 4.1.2 and probably other older versions
943 $lsub_ary = array_values($lsub_ary);
78bc908d 944 }
4b2720f5 945 break;
78bc908d 946 }
4b2720f5 947 }
78bc908d 948
4b2720f5 949 if ($has_inbox == false) {
950 // do a list request for inbox because we should always show
951 // inbox even if the user isn't subscribed to it.
952 $inbox_ary = sqimap_run_command ($imap_stream, 'LIST "" "INBOX"',
953 true, $response, $message);
954 $inbox_ary = compact_mailboxes_response($inbox_ary);
955 if (count($inbox_ary)) {
956 $lsub_ary[] = $inbox_ary[0];
78bc908d 957 }
4b2720f5 958 }
78bc908d 959
4b2720f5 960 /*
961 * Section about removing the last element was removed
962 * We don't return "* OK" anymore from sqimap_read_data
963 */
bac13dd7 964
4b2720f5 965 $sorted_lsub_ary = array();
966 $cnt = count($lsub_ary);
967 for ($i = 0; $i < $cnt; $i++) {
968 $mbx = find_mailbox_name($lsub_ary[$i]);
969
970 // only do the noselect test if !uw, is checked later. FIX ME see conf.pl setting
971 if ($imap_server_type != "uw") {
972 $noselect = check_is_noselect($lsub_ary[$i]);
973 $noinferiors = check_is_noinferiors($lsub_ary[$i]);
974 }
975 if (substr($mbx, -1) == $delimiter) {
976 $mbx = substr($mbx, 0, strlen($mbx) - 1);
977 }
978 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect, 'noinferiors' => $noinferiors);
979 }
980 // FIX ME this requires a config setting inside conf.pl instead of checking on server type
981 if ($imap_server_type == "uw") {
982 $aQuery = array();
983 $aTag = array();
984 // prepare an array with queries
985 foreach ($sorted_lsub_ary as $aMbx) {
986 $mbx = stripslashes($aMbx['mbx']);
987 sqimap_prepare_pipelined_query('LIST "" ' . sqimap_encode_mailbox_name($mbx), $tag, $aQuery, false);
988 $aTag[$tag] = $mbx;
989 }
60b5724d 990 $sorted_lsub_ary = array();
4b2720f5 991 // execute all the queries at once
992 $aResponse = sqimap_run_pipelined_command ($imap_stream, $aQuery, false, $aServerResponse, $aServerMessage);
993 foreach($aTag as $tag => $mbx) {
994 if ($aServerResponse[$tag] == 'OK') {
995 $sResponse = implode('', $aResponse[$tag]);
996 $noselect = check_is_noselect($sResponse);
997 $noinferiors = check_is_noinferiors($sResponse);
998 $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect, 'noinferiors' => $noinferiors);
a2e66c6d 999 }
60b5724d 1000 }
4b2720f5 1001 $cnt = count($sorted_lsub_ary);
60b5724d 1002 }
4b2720f5 1003 $sorted_lsub_ary = array_values($sorted_lsub_ary);
1004 usort($sorted_lsub_ary, 'mbxSort');
1005 $boxestree = sqimap_fill_mailbox_tree($sorted_lsub_ary,false,$imap_stream);
1006 return $boxestree;
60b5724d 1007}
1008
3131bc8d 1009/**
1010 * Callback function used for sorting mailboxes in sqimap_mailbox_tree
1011 * @param string $a
1012 * @param string $b
1013 * @return integer see php strnatcasecmp()
1014 * @since 1.5.1
1015 */
9d8d1ebd 1016function mbxSort($a, $b) {
1017 return strnatcasecmp($a['mbx'], $b['mbx']);
1018}
1019
3131bc8d 1020/**
4b2720f5 1021 * Fills mailbox object
1022 *
1023 * Some code fragments are present in 1.3.0 - 1.4.4.
3131bc8d 1024 * @param array $mbx_ary
1025 * @param $mbxs
4b2720f5 1026 * @param stream $imap_stream imap connection resource
3131bc8d 1027 * @return object see mailboxes class
4b2720f5 1028 * @since 1.5.0
3131bc8d 1029 */
483f9ef9 1030function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false,$imap_stream) {
60b5724d 1031 global $data_dir, $username, $list_special_folders_first,
1032 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
1033 $move_to_trash, $move_to_sent, $save_as_draft,
43a31298 1034 $delimiter, $imap_server_type;
60b5724d 1035
8d8da447 1036 // $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
ff245fbd 1037
60b5724d 1038 /* create virtual root node */
1039 $mailboxes= new mailboxes();
1040 $mailboxes->is_root = true;
ff245fbd 1041 $trail_del = false;
78bc908d 1042 $start = 0;
1043
587ec647 1044
43a31298 1045 if (isset($folder_prefix) && ($folder_prefix != '')) {
ff245fbd 1046 $start = substr_count($folder_prefix,$delimiter);
1047 if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
ff245fbd 1048 $mailboxes->mailboxname_full = substr($folder_prefix,0, (strlen($folder_prefix)-1));
1049 } else {
1050 $mailboxes->mailboxname_full = $folder_prefix;
1051 $start++;
1052 }
1053 $mailboxes->mailboxname_sub = $mailboxes->mailboxname_full;
1054 } else {
1055 $start = 0;
1056 }
78bc908d 1057
9d8d1ebd 1058 $cnt = count($mbx_ary);
e4c6fe41 1059 for ($i=0; $i < $cnt; $i++) {
ff245fbd 1060 if ($mbx_ary[$i]['mbx'] !='' ) {
1061 $mbx = new mailboxes();
1062 $mailbox = $mbx_ary[$i]['mbx'];
cc82c2b7 1063
4669e892 1064 /*
ae9e2adc 1065 * Set the is_special flag if it concerned a special mailbox.
1066 * Used for displaying the special folders on top in the mailbox
1067 * tree displaying code.
1068 */
8edc9f31 1069 $mbx->is_special |= ($mbx->is_inbox = (strtoupper($mailbox) == 'INBOX'));
1070 $mbx->is_special |= ($mbx->is_trash = isTrashMailbox($mailbox));
1071 $mbx->is_special |= ($mbx->is_sent = isSentMailbox($mailbox));
1072 $mbx->is_special |= ($mbx->is_draft = isDraftMailbox($mailbox));
ae9e2adc 1073
8edc9f31 1074 if (!$mbx->is_special)
fda01075 1075 $mbx->is_special = boolean_hook_function('special_mailbox', $mailbox, 1);
4669e892 1076
ff245fbd 1077 if (isset($mbx_ary[$i]['unseen'])) {
1078 $mbx->unseen = $mbx_ary[$i]['unseen'];
1079 }
1080 if (isset($mbx_ary[$i]['nummessages'])) {
1081 $mbx->total = $mbx_ary[$i]['nummessages'];
1082 }
1083
1084 $mbx->is_noselect = $mbx_ary[$i]['noselect'];
86c2763d 1085 $mbx->is_noinferiors = $mbx_ary[$i]['noinferiors'];
ff245fbd 1086
60b5724d 1087 $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
ff245fbd 1088 if ($r_del_pos) {
587ec647 1089 $mbx->mailboxname_sub = substr($mbx_ary[$i]['mbx'],$r_del_pos+1);
ff245fbd 1090 } else { /* mailbox is root folder */
587ec647 1091 $mbx->mailboxname_sub = $mbx_ary[$i]['mbx'];
ff245fbd 1092 }
1093 $mbx->mailboxname_full = $mbx_ary[$i]['mbx'];
38068e69 1094
9871bdaa 1095 $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);
ff245fbd 1096 }
60b5724d 1097 }
587ec647 1098 sqimap_utf7_decode_mbx_tree($mailboxes);
483f9ef9 1099 sqimap_get_status_mbx_tree($imap_stream,$mailboxes);
60b5724d 1100 return $mailboxes;
1101}
259faa39 1102
3131bc8d 1103/**
1104 * @param object $mbx_tree
1105 * @since 1.5.0
1106 */
587ec647 1107function sqimap_utf7_decode_mbx_tree(&$mbx_tree) {
9e0f10a8 1108
bd27b70b 1109 if (strtoupper($mbx_tree->mailboxname_full) == 'INBOX')
d94c8d61 1110 $mbx_tree->mailboxname_sub = _("INBOX");
1111 else
1112 $mbx_tree->mailboxname_sub = imap_utf7_decode_local($mbx_tree->mailboxname_sub);
587ec647 1113 if ($mbx_tree->mbxs) {
1114 $iCnt = count($mbx_tree->mbxs);
1115 for ($i=0;$i<$iCnt;++$i) {
9e0f10a8 1116 sqimap_utf7_decode_mbx_tree($mbx_tree->mbxs[$i]);
587ec647 1117 }
1118 }
483f9ef9 1119}
1120
3131bc8d 1121/**
1122 * @param object $mbx_tree
1123 * @param array $aMbxs
1124 * @since 1.5.0
1125 */
483f9ef9 1126function sqimap_tree_to_ref_array(&$mbx_tree,&$aMbxs) {
5c300c60 1127 if ($mbx_tree)
483f9ef9 1128 $aMbxs[] =& $mbx_tree;
1129 if ($mbx_tree->mbxs) {
1130 $iCnt = count($mbx_tree->mbxs);
1131 for ($i=0;$i<$iCnt;++$i) {
5c300c60 1132 sqimap_tree_to_ref_array($mbx_tree->mbxs[$i],$aMbxs);
483f9ef9 1133 }
1134 }
4669e892 1135}
483f9ef9 1136
3131bc8d 1137/**
1138 * @param stream $imap_stream imap connection resource
1139 * @param object $mbx_tree
1140 * @since since 1.5.0
1141 */
483f9ef9 1142function sqimap_get_status_mbx_tree($imap_stream,&$mbx_tree) {
1143 global $unseen_notify, $unseen_type, $trash_folder,$move_to_trash;
8d8da447 1144 $aMbxs = $aQuery = array();
483f9ef9 1145 sqimap_tree_to_ref_array($mbx_tree,$aMbxs);
1146 // remove the root node
1147 array_shift($aMbxs);
1148
1149 if($unseen_notify == 3) {
1150 $cnt = count($aMbxs);
1151 for($i=0;$i<$cnt;++$i) {
5c300c60 1152 $oMbx =& $aMbxs[$i];
1153 if (!$oMbx->is_noselect) {
483f9ef9 1154 $mbx = $oMbx->mailboxname_full;
5c300c60 1155 if ($unseen_type == 2 ||
1156 ($move_to_trash && $oMbx->mailboxname_full == $trash_folder)) {
568cb884 1157 $query = 'STATUS ' . sqimap_encode_mailbox_name($mbx) . ' (MESSAGES UNSEEN)';
5c300c60 1158 } else {
568cb884 1159 $query = 'STATUS ' . sqimap_encode_mailbox_name($mbx) . ' (UNSEEN)';
5c300c60 1160 }
483f9ef9 1161 sqimap_prepare_pipelined_query($query,$tag,$aQuery,false);
5c300c60 1162 } else {
1163 $oMbx->unseen = $oMbx->total = false;
1164 $tag = false;
1165 }
1166 $oMbx->tag = $tag;
1167 $aMbxs[$i] =& $oMbx;
483f9ef9 1168 }
1169 // execute all the queries at once
1170 $aResponse = sqimap_run_pipelined_command ($imap_stream, $aQuery, false, $aServerResponse, $aServerMessage);
1171 $cnt = count($aMbxs);
1172 for($i=0;$i<$cnt;++$i) {
5c300c60 1173 $oMbx =& $aMbxs[$i];
1174 $tag = $oMbx->tag;
1175 if ($tag && $aServerResponse[$tag] == 'OK') {
1176 $sResponse = implode('', $aResponse[$tag]);
483f9ef9 1177 if (preg_match('/UNSEEN\s+([0-9]+)/i', $sResponse, $regs)) {
1178 $oMbx->unseen = $regs[1];
1179 }
1180 if (preg_match('/MESSAGES\s+([0-9]+)/i', $sResponse, $regs)) {
1181 $oMbx->total = $regs[1];
5c300c60 1182 }
1183 }
1184 unset($oMbx->tag);
1185 }
483f9ef9 1186 } else if ($unseen_notify == 2) { // INBOX only
1187 $cnt = count($aMbxs);
1188 for($i=0;$i<$cnt;++$i) {
5c300c60 1189 $oMbx =& $aMbxs[$i];
1190 if (strtoupper($oMbx->mailboxname_full) == 'INBOX' ||
1191 ($move_to_trash && $oMbx->mailboxname_full == $trash_folder)) {
4669e892 1192 if ($unseen_type == 2 ||
5c300c60 1193 ($oMbx->mailboxname_full == $trash_folder && $move_to_trash)) {
1194 $aStatus = sqimap_status_messages($imap_stream,$oMbx->mailboxname_full);
1195 $oMbx->unseen = $aStatus['UNSEEN'];
1196 $oMbx->total = $aStatus['MESSAGES'];
1197 } else {
1198 $oMbx->unseen = sqimap_unseen_messages($imap_stream,$oMbx->mailboxname_full);
1199 }
1200 $aMbxs[$i] =& $oMbx;
1201 if (!$move_to_trash && $trash_folder) {
1202 break;
1203 } else {
1204 // trash comes after INBOX
1205 if ($oMbx->mailboxname_full == $trash_folder) {
1206 break;
1207 }
1208 }
1209 }
1210 }
4669e892 1211 }
1212}
587ec647 1213
367fb0ca 1214/**
1215 * Checks if folder is noselect (can't store messages)
1216 *
1217 * Function does not check if folder subscribed.
1218 * @param stream $oImapStream imap connection resource
1219 * @param string $sImapFolder imap folder name
1220 * @param object $oBoxes mailboxes class object.
1221 * @return boolean true, when folder has noselect flag. false in any other case.
1222 * @since 1.5.1
1223 */
1224function sqimap_mailbox_is_noselect($oImapStream,$sImapFolder,&$oBoxes) {
1225 // build mailbox object if it is not available
1226 if (! is_object($oBoxes)) $oBoxes=sqimap_mailbox_list($oImapStream);
1227 foreach($oBoxes as $box) {
1228 if ($box['unformatted']==$sImapFolder) {
1229 return (bool) check_is_noselect($box['raw']);
1230 }
1231 }
1232 return false;
1233}
1234
1235/**
1236 * Checks if folder is noinferiors (can't store other folders)
1237 *
1238 * Function does not check if folder subscribed.
1239 * @param stream $oImapStream imap connection resource
1240 * @param string $sImapFolder imap folder name
1241 * @param object $oBoxes mailboxes class object.
1242 * @return boolean true, when folder has noinferiors flag. false in any other case.
1243 * @since 1.5.1
1244 */
1245function sqimap_mailbox_is_noinferiors($oImapStream,$sImapFolder,&$oBoxes) {
1246 // build mailbox object if it is not available
1247 if (! is_object($oBoxes)) $oBoxes=sqimap_mailbox_list($oImapStream);
1248 foreach($oBoxes as $box) {
1249 if ($box['unformatted']==$sImapFolder) {
1250 return (bool) check_is_noinferiors($box['raw']);
1251 }
1252 }
1253 return false;
1254}
1255
9e0f10a8 1256?>