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