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