Added numerous changes from embeejay
[squirrelmail.git] / functions / imap_mailbox.php
1 <?php
2 /**
3 ** imap_mailbox.php
4 **
5 ** This impliments all functions that manipulate mailboxes
6 **/
7
8 /******************************************************************************
9 ** Expunges a mailbox
10 ******************************************************************************/
11 function sqimap_mailbox_expunge ($imap_stream, $mailbox) {
12 sqimap_mailbox_select ($imap_stream, $mailbox);
13 fputs ($imap_stream, "a001 EXPUNGE\r\n");
14 $read = sqimap_read_data($imap_stream, "a001", true, $response, $message);
15 sqimap_mailbox_close ($imap_stream);
16 }
17
18
19 /******************************************************************************
20 ** Checks whether or not the specified mailbox exists
21 ******************************************************************************/
22 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
23 fputs ($imap_stream, "a001 LIST \"\" \"$mailbox\"\r\n");
24 $mbx = sqimap_read_data($imap_stream, "a001", true, $response, $message);
25 if ($mailbox) {
26 return !!(ereg ("$mailbox", $mbx[0])); // To force into true/false
27 }
28 }
29
30 /******************************************************************************
31 ** Closes an open mailbox
32 ******************************************************************************/
33 function sqimap_mailbox_close ($imap_stream) {
34 fputs ($imap_stream, "a001 CLOSE\r\n");
35 $tmp = sqimap_read_data($imap_stream, "a001", false, $response, $message);
36 }
37
38 /******************************************************************************
39 ** Selects a mailbox
40 ******************************************************************************/
41 function sqimap_mailbox_select ($imap_stream, $mailbox, $hide=true, $recent=false) {
42 global $auto_expunge;
43
44 fputs ($imap_stream, "a001 SELECT \"$mailbox\"\r\n");
45 $read = sqimap_read_data($imap_stream, "a001", true, $response, $message);
46 if ($recent) {
47 for ($i=0; $i<count($read); $i++) {
48 if (strpos(strtolower($read[$i]), "recent")) {
49 $r = explode(" ", $read[$i]);
50 }
51 }
52 return $r[1];
53 }
54 if ($auto_expunge) {
55 fputs ($imap_stream, "a001 EXPUNGE\r\n");
56 $tmp = sqimap_read_data($imap_stream, "a001", true, $a, $b);
57 }
58 }
59
60
61
62 /******************************************************************************
63 ** Creates a folder
64 ******************************************************************************/
65 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
66 if (strtolower($type) == "noselect") {
67 $dm = sqimap_get_delimiter($imap_stream);
68 $mailbox = $mailbox.$dm;
69 }
70 fputs ($imap_stream, "a001 CREATE \"$mailbox\"\r\n");
71 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
72
73 sqimap_subscribe ($imap_stream, $mailbox);
74 }
75
76
77
78 /******************************************************************************
79 ** Subscribes to an existing folder
80 ******************************************************************************/
81 function sqimap_subscribe ($imap_stream, $mailbox) {
82 fputs ($imap_stream, "a001 SUBSCRIBE \"$mailbox\"\r\n");
83 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
84 }
85
86
87
88
89 /******************************************************************************
90 ** Unsubscribes to an existing folder
91 ******************************************************************************/
92 function sqimap_unsubscribe ($imap_stream, $mailbox) {
93 global $imap_server_type;
94
95 fputs ($imap_stream, "a001 UNSUBSCRIBE \"$mailbox\"\r\n");
96 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
97 }
98
99
100
101
102 /******************************************************************************
103 ** This function simply deletes the given folder
104 ******************************************************************************/
105 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
106 fputs ($imap_stream, "a001 DELETE \"$mailbox\"\r\n");
107 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
108 sqimap_unsubscribe ($imap_stream, $mailbox);
109 }
110
111
112 /******************************************************************************
113 ** Formats a mailbox into 4 parts for the $boxes array
114 ******************************************************************************/
115 function sqimap_mailbox_parse ($line, $line_lsub, $dm) {
116 global $folder_prefix;
117 for ($g=0; $g < count($line); $g++) {
118 $boxes[$g]["raw"] = $line[$g];
119
120 $mailbox = $line_lsub[$g];
121 $dm_count = countCharInString($mailbox, $dm);
122 if (substr($mailbox, -1) == $dm)
123 $dm_count--;
124
125 for ($j = 0; $j < $dm_count - (countCharInString($folder_prefix, $dm)); $j++)
126 $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . "&nbsp;&nbsp;";
127 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
128
129 $boxes[$g]["unformatted-dm"] = $mailbox;
130 if (substr($mailbox, -1) == $dm)
131 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
132 $boxes[$g]["unformatted"] = $mailbox;
133 $boxes[$g]["id"] = $g;
134
135 ereg("\(([^)]*)\)",$line[$g],$regs);
136 $flags = trim(strtolower(str_replace("\\", "",$regs[1])));
137 if ($flags) {
138 $boxes[$g]["flags"] = explode(" ", $flags);
139 }
140 }
141 return $boxes;
142 }
143
144 /******************************************************************************
145 ** Returns sorted mailbox lists in several different ways.
146 ** The array returned looks like this:
147 ******************************************************************************/
148 function sqimap_mailbox_list ($imap_stream) {
149 global $load_prefs_php, $prefs_php, $config_php, $data_dir, $username, $list_special_folders_first;
150 global $trash_folder, $sent_folder, $imap_server_type;
151 global $move_to_trash, $move_to_sent;
152
153 $inbox_in_list = false;
154 $inbox_subscribed = false;
155
156 if (!isset($load_prefs_php)) include "../src/load_prefs.php";
157 else global $folder_prefix;
158 if (!function_exists ("ary_sort")) include "../functions/array.php";
159
160 $dm = sqimap_get_delimiter ($imap_stream);
161
162 /** LSUB array **/
163 $inbox_subscribed = false;
164 if ($imap_server_type == "uw") {
165 fputs ($imap_stream, "a001 LSUB \"~\" \"*\"\r\n");
166 }
167 else {
168 fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n");
169 }
170 $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
171
172 for ($i=0;$i < count($lsub_ary); $i++) {
173 $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
174 if ($sorted_lsub_ary[$i] == "INBOX")
175 $inbox_subscribed = true;
176 }
177 $new_ary = array();
178 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
179 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
180 $new_ary[] = $sorted_lsub_ary[$i];
181 }
182 }
183 $sorted_lsub_ary = $new_ary;
184 if (isset($sorted_lsub_ary)) {
185 sort($sorted_lsub_ary);
186 }
187
188 /** LIST array **/
189 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
190 if (substr($sorted_lsub_ary[$i], -1) == $dm)
191 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
192 else
193 $mbx = $sorted_lsub_ary[$i];
194
195 fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n");
196 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
197 $sorted_list_ary[$i] = $read[0];
198 if (find_mailbox_name($sorted_list_ary[$i]) == "INBOX")
199 $inbox_in_list = true;
200 }
201
202 /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/
203 if ($inbox_subscribed == false || $inbox_in_list == false) {
204 fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
205 $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
206
207 $pos = count($sorted_list_ary);
208 $sorted_list_ary[$pos] = $inbox_ary[0];
209
210 $pos = count($sorted_lsub_ary);
211 $sorted_lsub_ary[$pos] = find_mailbox_name($inbox_ary[0]);
212 }
213
214 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm);
215
216 /** Now, lets sort for special folders **/
217 for ($i = 0; $i < count($boxes); $i++) {
218 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
219 $boxesnew[0] = $boxes[$i];
220 $boxes[$i]["used"] = true;
221 $i = count($boxes);
222 }
223 }
224
225 if ($list_special_folders_first == true) {
226 for ($i = count($boxes)-1; $i >= 0 ; $i--) {
227 if (($boxes[$i]["unformatted"] == $trash_folder) && ($move_to_trash)) {
228 $pos = count($boxesnew);
229 $boxesnew[$pos] = $boxes[$i];
230 $boxes[$i]["used"] = true;
231 $trash_found = true;
232 }
233 else if (($boxes[$i]["unformatted"] == $sent_folder) && ($move_to_sent)) {
234 $pos = count($boxesnew);
235 $boxesnew[$pos] = $boxes[$i];
236 $boxes[$i]["used"] = true;
237 $sent_found = true;
238 }
239
240 if (($sent_found && $trash_found) || ($sent_found && !$move_to_trash) || ($trash_found && !$move_to_sent) || (!$move_to_sent && !$move_to_trash))
241 $i = -1;
242 }
243 }
244
245 for ($i = 0; $i < count($boxes); $i++) {
246 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
247 ($boxes[$i]["used"] == false)) {
248 $pos = count($boxesnew);
249 $boxesnew[$pos] = $boxes[$i];
250 $boxes[$i]["used"] = true;
251 }
252 }
253
254 return $boxesnew;
255 }
256
257 /******************************************************************************
258 ** Returns a list of all folders, subscribed or not
259 ******************************************************************************/
260 function sqimap_mailbox_list_all ($imap_stream) {
261 global $list_special_folders_first, $folder_prefix;
262
263 if (!function_exists ("ary_sort"))
264 include ("../functions/array.php");
265
266 $dm = sqimap_get_delimiter ($imap_stream);
267
268 fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
269 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
270 $g = 0;
271 $phase = "inbox";
272 for ($i = 0; $i < count($read_ary); $i++) {
273 if (substr ($read_ary[$i], 0, 4) != "a001") {
274 $boxes[$g]["raw"] = $read_ary[$i];
275
276 $mailbox = find_mailbox_name($read_ary[$i]);
277 $dm_count = countCharInString($mailbox, $dm);
278 if (substr($mailbox, -1) == $dm)
279 $dm_count--;
280
281 for ($j = 0; $j < $dm_count; $j++)
282 $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
283 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
284
285 $boxes[$g]["unformatted-dm"] = $mailbox;
286 if (substr($mailbox, -1) == $dm)
287 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
288 $boxes[$g]["unformatted"] = $mailbox;
289 $boxes[$g]["id"] = $g;
290
291 /** Now lets get the flags for this mailbox **/
292 fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
293 $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
294
295 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
296 $flags = substr($flags, 0, strpos($flags, ")"));
297 $flags = str_replace("\\", "", $flags);
298 $flags = trim(strtolower($flags));
299 if ($flags) {
300 $boxes[$g]["flags"] = explode(" ", $flags);
301 }
302 }
303 $g++;
304 }
305 if ($boxes) {
306 $boxes = ary_sort ($boxes, "unformatted", 1);
307 }
308 return $boxes;
309 }
310
311 ?>