Fixed a bug where users that did not log in before would get tons of
[squirrelmail.git] / functions / imap_mailbox.php
1 <?php
2 /**
3 ** imap_mailbox.php
4 **
5 ** This impliments all functions that manipulate mailboxes
6 **/
7
8 /******************************************************************************
9 ** Expunges a mailbox
10 ******************************************************************************/
11 function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = true) {
12 sqimap_mailbox_select ($imap_stream, $mailbox);
13 fputs ($imap_stream, "a001 EXPUNGE\r\n");
14 $read = sqimap_read_data($imap_stream, "a001", $handle_errors, $response, $message);
15 }
16
17
18 /******************************************************************************
19 ** Checks whether or not the specified mailbox exists
20 ******************************************************************************/
21 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
22 fputs ($imap_stream, "a001 LIST \"\" \"$mailbox\"\r\n");
23 $mbx = sqimap_read_data($imap_stream, "a001", true, $response, $message);
24 if ($mailbox) {
25 return !!(ereg ("$mailbox", $mbx[0])); // To force into true/false
26 }
27 }
28
29 /******************************************************************************
30 ** Selects a mailbox
31 ******************************************************************************/
32 function sqimap_mailbox_select ($imap_stream, $mailbox, $hide=true, $recent=false) {
33 global $auto_expunge;
34
35 fputs ($imap_stream, "a001 SELECT \"$mailbox\"\r\n");
36 $read = sqimap_read_data($imap_stream, "a001", true, $response, $message);
37 if ($recent) {
38 for ($i=0; $i<count($read); $i++) {
39 if (strpos(strtolower($read[$i]), "recent")) {
40 $r = explode(" ", $read[$i]);
41 }
42 }
43 return $r[1];
44 }
45 if ($auto_expunge) {
46 fputs ($imap_stream, "a001 EXPUNGE\r\n");
47 $tmp = sqimap_read_data($imap_stream, "a001", false, $a, $b);
48 }
49 }
50
51
52
53 /******************************************************************************
54 ** Creates a folder
55 ******************************************************************************/
56 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
57 if (strtolower($type) == "noselect") {
58 $dm = sqimap_get_delimiter($imap_stream);
59 $mailbox = $mailbox.$dm;
60 }
61 fputs ($imap_stream, "a001 CREATE \"$mailbox\"\r\n");
62 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
63
64 sqimap_subscribe ($imap_stream, $mailbox);
65 }
66
67
68
69 /******************************************************************************
70 ** Subscribes to an existing folder
71 ******************************************************************************/
72 function sqimap_subscribe ($imap_stream, $mailbox) {
73 fputs ($imap_stream, "a001 SUBSCRIBE \"$mailbox\"\r\n");
74 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
75 }
76
77
78
79
80 /******************************************************************************
81 ** Unsubscribes to an existing folder
82 ******************************************************************************/
83 function sqimap_unsubscribe ($imap_stream, $mailbox) {
84 global $imap_server_type;
85
86 fputs ($imap_stream, "a001 UNSUBSCRIBE \"$mailbox\"\r\n");
87 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
88 }
89
90
91
92
93 /******************************************************************************
94 ** This function simply deletes the given folder
95 ******************************************************************************/
96 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
97 fputs ($imap_stream, "a001 DELETE \"$mailbox\"\r\n");
98 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
99 sqimap_unsubscribe ($imap_stream, $mailbox);
100 }
101
102
103 /******************************************************************************
104 ** Formats a mailbox into 4 parts for the $boxes array
105 **
106 ** The four parts are:
107 **
108 ** raw - Raw LIST/LSUB response from the IMAP server
109 ** formatted - nicely formatted folder name
110 ** unformatted - unformatted, but with delimiter at end removed
111 ** unformatted-dm - folder name as it appears in raw response
112 ** unformatted-disp - unformatted without $folder_prefix
113 **
114 ******************************************************************************/
115 function sqimap_mailbox_parse ($line, $line_lsub, $dm) {
116 global $folder_prefix;
117
118 // Process each folder line
119 for ($g=0; $g < count($line); $g++) {
120
121 // Store the raw IMAP reply
122 $boxes[$g]["raw"] = $line[$g];
123
124 // Count number of delimiters ($dm) in folder name
125 $mailbox = trim($line_lsub[$g]);
126 $dm_count = countCharInString($mailbox, $dm);
127 if (substr($mailbox, -1) == $dm)
128 $dm_count--; // If name ends in delimiter - decrement count by one
129
130 // Format folder name, but only if it's a INBOX.* or have
131 // a parent.
132 $boxesbyname[$mailbox] = $g;
133 $parentfolder = readMailboxParent($mailbox, $dm);
134 if((eregi("^inbox".quotemeta($dm), $mailbox)) ||
135 (ereg("^".$folder_prefix, $mailbox)) ||
136 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
137 $indent = $dm_count - (countCharInString($folder_prefix, $dm));
138 if ($indent)
139 $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $indent);
140 else
141 $boxes[$g]["formatted"] = '';
142 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
143 } else {
144 $boxes[$g]["formatted"] = $mailbox;
145 }
146
147 $boxes[$g]["unformatted-dm"] = $mailbox;
148 if (substr($mailbox, -1) == $dm)
149 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
150 $boxes[$g]["unformatted"] = $mailbox;
151 $boxes[$g]["unformatted-disp"] = ereg_replace("^" . $folder_prefix, "", $mailbox);
152 $boxes[$g]["id"] = $g;
153
154 ereg("\(([^)]*)\)",$line[$g],$regs);
155 $flags = trim(strtolower(str_replace("\\", "",$regs[1])));
156 if ($flags) {
157 $boxes[$g]["flags"] = explode(" ", $flags);
158 }
159 }
160
161 return $boxes;
162 }
163
164 /* patch from dave_michmerhuizen@yahoo.com
165 * allows case insensativity when sorting folders
166 */
167 function _icmp ($a, $b) {
168 return strcasecmp($a, $b);
169 }
170
171 /******************************************************************************
172 ** Returns sorted mailbox lists in several different ways.
173 ** See comment on sqimap_mailbox_parse() for info about the returned array.
174 ******************************************************************************/
175 function sqimap_mailbox_list ($imap_stream) {
176 global $load_prefs_php, $prefs_php, $config_php;
177 global $data_dir, $username, $list_special_folders_first;
178 global $trash_folder, $sent_folder;
179 global $move_to_trash, $move_to_sent;
180
181 $inbox_in_list = false;
182 $inbox_subscribed = false;
183
184 if (!isset($load_prefs_php)) include "../src/load_prefs.php";
185 else global $folder_prefix;
186 if (!function_exists ("ary_sort")) include "../functions/array.php";
187
188 $dm = sqimap_get_delimiter ($imap_stream);
189
190 /** LSUB array **/
191 $inbox_subscribed = false;
192 fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n");
193 $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
194
195 /** OS: we don't want to parse last element of array, 'cause it is OK command, so we unset it **/
196 unset($lsub_ary[count($lsub_ary)-1]);
197
198 for ($i=0;$i < count($lsub_ary); $i++) {
199 $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
200 if ($sorted_lsub_ary[$i] == "INBOX")
201 $inbox_subscribed = true;
202 }
203 $new_ary = array();
204 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
205 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
206 $new_ary[] = $sorted_lsub_ary[$i];
207 }
208 }
209 $sorted_lsub_ary = $new_ary;
210 if (isset($sorted_lsub_ary)) {
211 usort($sorted_lsub_ary, "_icmp");
212 //sort($sorted_lsub_ary);
213 }
214
215 /** LIST array **/
216 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
217 if (substr($sorted_lsub_ary[$i], -1) == $dm)
218 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
219 else
220 $mbx = $sorted_lsub_ary[$i];
221
222 fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n");
223 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
224 $sorted_list_ary[$i] = $read[0];
225 if (find_mailbox_name($sorted_list_ary[$i]) == "INBOX")
226 $inbox_in_list = true;
227 }
228
229 /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/
230 if ($inbox_subscribed == false || $inbox_in_list == false) {
231 fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
232 $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
233
234 $pos = count($sorted_list_ary);
235 $sorted_list_ary[$pos] = $inbox_ary[0];
236
237 $pos = count($sorted_lsub_ary);
238 $sorted_lsub_ary[$pos] = find_mailbox_name($inbox_ary[0]);
239 }
240
241 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm);
242
243
244 /** Now, lets sort for special folders **/
245
246 $boxesnew = Array();
247
248 // Find INBOX
249 for ($i = 0; $i < count($boxes); $i++) {
250 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
251 $boxesnew[] = $boxes[$i];
252 $boxes[$i]["used"] = true;
253 $i = count($boxes);
254 }
255 }
256
257 if ($list_special_folders_first == true) {
258
259 // Then list special folders and their subfolders
260 for ($i = 0 ; $i <= count($boxes) ; $i++) {
261 if((eregi("^".quotemeta($trash_folder).'$', $boxes[$i]["unformatted"]) ||
262 eregi("^".quotemeta($trash_folder).quotemeta($dm), $boxes[$i]["unformatted"]) ) &&
263 ($move_to_trash)) {
264 $boxesnew[] = $boxes[$i];
265 $boxes[$i]["used"] = true;
266 }
267 else if((eregi("^".quotemeta($sent_folder).'$', $boxes[$i]["unformatted"]) ||
268 eregi("^".quotemeta($sent_folder).quotemeta($dm), $boxes[$i]["unformatted"]) ) &&
269 ($move_to_sent)) {
270 $boxesnew[] = $boxes[$i];
271 $boxes[$i]["used"] = true;
272 }
273 }
274
275 // Put INBOX.* folders ahead of the rest
276 for ($i = 0; $i <= count($boxes); $i++) {
277 if (eregi("^inbox\.", $boxes[$i]["unformatted"]) &&
278 ($boxes[$i]["used"] == false)) {
279 $boxesnew[] = $boxes[$i];
280 $boxes[$i]["used"] = true;
281 }
282 }
283
284 }
285
286 // Rest of the folders
287 for ($i = 0; $i < count($boxes); $i++) {
288 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
289 ($boxes[$i]["used"] == false)) {
290 $boxesnew[] = $boxes[$i];
291 $boxes[$i]["used"] = true;
292 }
293 }
294
295 return $boxesnew;
296 }
297
298 /******************************************************************************
299 ** Returns a list of all folders, subscribed or not
300 ******************************************************************************/
301 function sqimap_mailbox_list_all ($imap_stream) {
302 global $list_special_folders_first, $folder_prefix;
303
304 if (!function_exists ("ary_sort"))
305 include ("../functions/array.php");
306
307 $dm = sqimap_get_delimiter ($imap_stream);
308
309 fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
310 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
311 $g = 0;
312 $phase = "inbox";
313
314 for ($i = 0; $i < count($read_ary); $i++) {
315 if (substr ($read_ary[$i], 0, 4) != "a001") {
316
317 // Store the raw IMAP reply
318 $boxes[$g]["raw"] = $read_ary[$i];
319
320 // Count number of delimiters ($dm) in folder name
321 $mailbox = find_mailbox_name($read_ary[$i]);
322 $dm_count = countCharInString($mailbox, $dm);
323 if (substr($mailbox, -1) == $dm)
324 $dm_count--; // If name ends in delimiter - decrement count by one
325
326 // Format folder name, but only if it's a INBOX.* or have
327 // a parent.
328 $boxesbyname[$mailbox] = $g;
329 $parentfolder = readMailboxParent($mailbox, $dm);
330 if((eregi("^inbox".quotemeta($dm), $mailbox)) ||
331 (ereg("^".$folder_prefix, $mailbox)) ||
332 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
333 if ($dm_count)
334 $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $dm_count);
335 else
336 $boxes[$g]["formatted"] = '';
337 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
338 } else {
339 $boxes[$g]["formatted"] = $mailbox;
340 }
341
342 $boxes[$g]["unformatted-dm"] = $mailbox;
343 if (substr($mailbox, -1) == $dm)
344 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
345 $boxes[$g]["unformatted"] = $mailbox;
346 $boxes[$g]["unformatted-disp"] = ereg_replace("^" . $folder_prefix, "", $mailbox);
347 $boxes[$g]["id"] = $g;
348
349 /** Now lets get the flags for this mailbox **/
350 fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
351 $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
352
353 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
354 $flags = substr($flags, 0, strpos($flags, ")"));
355 $flags = str_replace("\\", "", $flags);
356 $flags = trim(strtolower($flags));
357 if ($flags) {
358 $boxes[$g]["flags"] = explode(" ", $flags);
359 }
360 }
361 $g++;
362 }
363 if(is_array($boxes)) {
364 $boxes = ary_sort ($boxes, "unformatted", 1);
365 }
366
367 return $boxes;
368 }
369
370 ?>