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