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