* Added much better support for the mailto stuff
[squirrelmail.git] / functions / imap_mailbox.php
CommitLineData
59177427 1<?php
8e9e8afa 2 /**
3 ** imap_mailbox.php
4 **
5 ** This impliments all functions that manipulate mailboxes
245a6892 6 **
7 ** $Id$
8e9e8afa 8 **/
9
f435778e 10 if (defined ('imap_mailbox_php'))
11 return;
12 define ('imap_mailbox_php', true);
13
8e9e8afa 14 /******************************************************************************
15 ** Expunges a mailbox
16 ******************************************************************************/
a7f3c40d 17 function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = true) {
8e9e8afa 18 fputs ($imap_stream, "a001 EXPUNGE\r\n");
8cf653ad 19 $read = sqimap_read_data($imap_stream, "a001", $handle_errors, $response, $message);
8e9e8afa 20 }
21
22
23 /******************************************************************************
24 ** Checks whether or not the specified mailbox exists
25 ******************************************************************************/
26 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
146e0c45 27 if (! isset($mailbox))
28 return false;
26511b3e 29 fputs ($imap_stream, "a001 LIST \"\" \"$mailbox\"\r\n");
30 $mbx = sqimap_read_data($imap_stream, "a001", true, $response, $message);
146e0c45 31 return isset($mbx[0]);
8e9e8afa 32 }
33
a2790a61 34 /******************************************************************************
8e9e8afa 35 ** Selects a mailbox
36 ******************************************************************************/
04632dbc 37 function sqimap_mailbox_select ($imap_stream, $mailbox, $hide=true, $recent=false) {
3751dc7f 38 global $auto_expunge;
39
8e9e8afa 40 fputs ($imap_stream, "a001 SELECT \"$mailbox\"\r\n");
2752bb16 41 $read = sqimap_read_data($imap_stream, "a001", true, $response, $message);
04632dbc 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 }
3751dc7f 50 if ($auto_expunge) {
51 fputs ($imap_stream, "a001 EXPUNGE\r\n");
8cf653ad 52 $tmp = sqimap_read_data($imap_stream, "a001", false, $a, $b);
3751dc7f 53 }
8e9e8afa 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) {
2752bb16 89 global $imap_server_type;
cbdc5621 90
8e9e8afa 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
5bdd7223 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
18a148f0 117 ** unformatted-disp - unformatted without $folder_prefix
5bdd7223 118 **
8e9e8afa 119 ******************************************************************************/
16530f8b 120 function sqimap_mailbox_parse ($line, $line_lsub, $dm) {
5bdd7223 121 global $folder_prefix;
122
123 // Process each folder line
8e9e8afa 124 for ($g=0; $g < count($line); $g++) {
5bdd7223 125
2752bb16 126 // Store the raw IMAP reply
a7ea7540 127 if (isset($line[$g]))
128 $boxes[$g]["raw"] = $line[$g];
129 else
130 $boxes[$g]["raw"] = "";
131
5bdd7223 132
2752bb16 133 // Count number of delimiters ($dm) in folder name
6477eb2d 134 $mailbox = trim($line_lsub[$g]);
8e9e8afa 135 $dm_count = countCharInString($mailbox, $dm);
136 if (substr($mailbox, -1) == $dm)
5bdd7223 137 $dm_count--; // If name ends in delimiter - decrement count by one
138
2752bb16 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);
36dfb0c9 143 if((strtolower(substr($mailbox, 0, 5)) == "inbox") ||
144 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
145 (isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
2752bb16 146 $indent = $dm_count - (countCharInString($folder_prefix, $dm));
b1275991 147 if ($indent > 0)
0debfed6 148 $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $indent);
149 else
150 $boxes[$g]["formatted"] = '';
2752bb16 151 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
152 } else {
153 $boxes[$g]["formatted"] = $mailbox;
154 }
8e9e8afa 155
146e0c45 156 $boxes[$g]['unformatted-dm'] = $mailbox;
8e9e8afa 157 if (substr($mailbox, -1) == $dm)
158 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
146e0c45 159 $boxes[$g]['unformatted'] = $mailbox;
20eb2621 160 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix)
631b9da3 161 $mailbox = substr($mailbox, strlen($folder_prefix));
162 $boxes[$g]['unformatted-disp'] = $mailbox;
146e0c45 163 $boxes[$g]['id'] = $g;
8e9e8afa 164
a7ea7540 165 if (isset($line[$g]))
36dfb0c9 166 ereg("\(([^)]*)\)",$line[$g],$regs);
146e0c45 167 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
8e9e8afa 168 if ($flags) {
146e0c45 169 $boxes[$g]['flags'] = explode(' ', $flags);
12d61439 170 } else
171 $boxes[$g]['flags'] = array();
8e9e8afa 172 }
5bdd7223 173
8e9e8afa 174 return $boxes;
175 }
1071ae95 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 }
10359c65 186
6b8a49c9 187
8e9e8afa 188 /******************************************************************************
189 ** Returns sorted mailbox lists in several different ways.
5bdd7223 190 ** See comment on sqimap_mailbox_parse() for info about the returned array.
8e9e8afa 191 ******************************************************************************/
192 function sqimap_mailbox_list ($imap_stream) {
5bdd7223 193 global $data_dir, $username, $list_special_folders_first;
6477eb2d 194 global $trash_folder, $sent_folder;
f435778e 195 global $move_to_trash, $move_to_sent, $folder_prefix;
8e9e8afa 196
1590a668 197 $inbox_in_list = false;
198 $inbox_subscribed = false;
199
f435778e 200 include "../src/load_prefs.php";
201 include "../functions/array.php";
8e9e8afa 202
203 $dm = sqimap_get_delimiter ($imap_stream);
204
8e9e8afa 205 /** LSUB array **/
206 $inbox_subscribed = false;
58fff17f 207 fputs ($imap_stream, "a001 LSUB \"$folder_prefix\" \"*%\"\r\n");
8e9e8afa 208 $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
6477eb2d 209
18a148f0 210 /** OS: we don't want to parse last element of array, 'cause it is OK command, so we unset it **/
211 /** LUKE: This introduced errors.. do a check first **/
212 if (substr($lsub_ary[count($lsub_ary)-1], 0, 4) == "* OK") {
213 unset($lsub_ary[count($lsub_ary)-1]);
214 }
a3db804c 215
8e9e8afa 216 for ($i=0;$i < count($lsub_ary); $i++) {
217 $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
8e9e8afa 218 if ($sorted_lsub_ary[$i] == "INBOX")
219 $inbox_subscribed = true;
220 }
ee62ce13 221 $new_ary = array();
222 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
223 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
224 $new_ary[] = $sorted_lsub_ary[$i];
225 }
226 }
227 $sorted_lsub_ary = $new_ary;
8e9e8afa 228 if (isset($sorted_lsub_ary)) {
1071ae95 229 usort($sorted_lsub_ary, "user_strcasecmp");
10359c65 230 //sort($sorted_lsub_ary);
8e9e8afa 231 }
232
3cb866d7 233 /** LIST array **/
f9b3e5d9 234 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
235 if (substr($sorted_lsub_ary[$i], -1) == $dm)
236 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
237 else
238 $mbx = $sorted_lsub_ary[$i];
239
240 fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n");
241 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
a7ea7540 242 if (isset($sorted_list_ary[$i]))
243 $sorted_list_ary[$i] = "";
244 if (isset($read[0]))
f9b3e5d9 245 $sorted_list_ary[$i] = $read[0];
a7ea7540 246 else
d59bf290 247 $sorted_list_ary[$i] = "";
a7ea7540 248 if (isset($sorted_list_ary[$i]) && find_mailbox_name($sorted_list_ary[$i]) == "INBOX")
3cb866d7 249 $inbox_in_list = true;
f9b3e5d9 250 }
2752bb16 251
8e9e8afa 252 /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/
253 if ($inbox_subscribed == false || $inbox_in_list == false) {
254 fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
255 $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
591000ec 256
257 $pos = count($sorted_list_ary);
258 $sorted_list_ary[$pos] = $inbox_ary[0];
259
260 $pos = count($sorted_lsub_ary);
261 $sorted_lsub_ary[$pos] = find_mailbox_name($inbox_ary[0]);
8e9e8afa 262 }
263
f9b3e5d9 264 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm);
265
5bdd7223 266
f9b3e5d9 267 /** Now, lets sort for special folders **/
5bdd7223 268
269 $boxesnew = Array();
270
271 // Find INBOX
8e9e8afa 272 for ($i = 0; $i < count($boxes); $i++) {
1e0628fb 273 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
5bdd7223 274 $boxesnew[] = $boxes[$i];
283db905 275 $used[$i] = true;
2752bb16 276 $i = count($boxes);
8e9e8afa 277 }
278 }
aed206bf 279
8e9e8afa 280 if ($list_special_folders_first == true) {
5bdd7223 281
2752bb16 282 // Then list special folders and their subfolders
245a6892 283 for ($i = 0 ; $i < count($boxes) ; $i++) {
23ed73eb 284 if ($move_to_trash &&
146e0c45 285 eregi('^' . quotemeta($trash_folder) . '(' .
286 quotemeta($dm) . '.*)?$', $boxes[$i]["unformatted"])) {
5bdd7223 287 $boxesnew[] = $boxes[$i];
283db905 288 $used[$i] = true;
1e0628fb 289 }
23ed73eb 290 elseif ($move_to_sent &&
146e0c45 291 eregi('^' . quotemeta($sent_folder) . '(' .
292 quotemeta($dm) . '.*)?$', $boxes[$i]["unformatted"])) {
5bdd7223 293 $boxesnew[] = $boxes[$i];
283db905 294 $used[$i] = true;
8e9e8afa 295 }
296 }
5bdd7223 297
2752bb16 298 // Put INBOX.* folders ahead of the rest
245a6892 299 for ($i = 0; $i < count($boxes); $i++) {
146e0c45 300 if (eregi('^inbox\\.', $boxes[$i]["unformatted"]) &&
283db905 301 (!isset($used[$i]) || $used[$i] == false)) {
2752bb16 302 $boxesnew[] = $boxes[$i];
283db905 303 $used[$i] = true;
2752bb16 304 }
305 }
5bdd7223 306
8e9e8afa 307 }
c5eb2c03 308
5bdd7223 309 // Rest of the folders
8e9e8afa 310 for ($i = 0; $i < count($boxes); $i++) {
1e0628fb 311 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
283db905 312 (!isset($used[$i]) || $used[$i] == false)) {
5bdd7223 313 $boxesnew[] = $boxes[$i];
283db905 314 $used[$i] = true;
8e9e8afa 315 }
316 }
317
318 return $boxesnew;
319 }
8e9e8afa 320
321 /******************************************************************************
322 ** Returns a list of all folders, subscribed or not
323 ******************************************************************************/
324 function sqimap_mailbox_list_all ($imap_stream) {
1e0628fb 325 global $list_special_folders_first, $folder_prefix;
8e9e8afa 326
327 if (!function_exists ("ary_sort"))
328 include ("../functions/array.php");
329
330 $dm = sqimap_get_delimiter ($imap_stream);
331
332 fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
333 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
334 $g = 0;
335 $phase = "inbox";
5bdd7223 336
8e9e8afa 337 for ($i = 0; $i < count($read_ary); $i++) {
338 if (substr ($read_ary[$i], 0, 4) != "a001") {
5bdd7223 339
2752bb16 340 // Store the raw IMAP reply
8e9e8afa 341 $boxes[$g]["raw"] = $read_ary[$i];
342
2752bb16 343 // Count number of delimiters ($dm) in folder name
8e9e8afa 344 $mailbox = find_mailbox_name($read_ary[$i]);
345 $dm_count = countCharInString($mailbox, $dm);
346 if (substr($mailbox, -1) == $dm)
5bdd7223 347 $dm_count--; // If name ends in delimiter - decrement count by one
2752bb16 348
349 // Format folder name, but only if it's a INBOX.* or have
350 // a parent.
351 $boxesbyname[$mailbox] = $g;
352 $parentfolder = readMailboxParent($mailbox, $dm);
146e0c45 353 if((eregi('^inbox'.quotemeta($dm), $mailbox)) ||
354 (ereg('^'.$folder_prefix, $mailbox)) ||
2752bb16 355 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
0debfed6 356 if ($dm_count)
357 $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $dm_count);
358 else
359 $boxes[$g]["formatted"] = '';
2752bb16 360 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
361 } else {
362 $boxes[$g]["formatted"] = $mailbox;
363 }
8e9e8afa 364
365 $boxes[$g]["unformatted-dm"] = $mailbox;
366 if (substr($mailbox, -1) == $dm)
367 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
368 $boxes[$g]["unformatted"] = $mailbox;
146e0c45 369 $boxes[$g]["unformatted-disp"] = ereg_replace('^' . $folder_prefix, '', $mailbox);
8e9e8afa 370 $boxes[$g]["id"] = $g;
371
372 /** Now lets get the flags for this mailbox **/
373 fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
374 $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
375
376 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
377 $flags = substr($flags, 0, strpos($flags, ")"));
146e0c45 378 $flags = str_replace('\\', '', $flags);
8e9e8afa 379 $flags = trim(strtolower($flags));
380 if ($flags) {
4afa7206 381 $boxes[$g]['flags'] = explode(" ", $flags);
8e9e8afa 382 }
12d61439 383 else
384 {
385 $boxes[$g]['flags'] = array();
386 }
8e9e8afa 387 }
388 $g++;
389 }
5bdd7223 390 if(is_array($boxes)) {
591000ec 391 $boxes = ary_sort ($boxes, "unformatted", 1);
392 }
5bdd7223 393
8e9e8afa 394 return $boxes;
395 }
396
052e0c26 397?>