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