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