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