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