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