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