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