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