moved buttons on compose form so they are more accessable to smaller
[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 (ereg ("$mailbox", $mbx[0])) {
25 return true;
26 } else {
27 return false;
28 }
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
150 for ($i=0;$i < count($lsub_ary); $i++) {
151 $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
152 if ($sorted_lsub_ary[$i] == "INBOX")
153 $inbox_subscribed = true;
154 }
155 if (isset($sorted_lsub_ary)) {
156 sort($sorted_lsub_ary);
157 }
158
159 /** LIST array **/
160 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
161 if (substr($sorted_lsub_ary[$i], -1) == $dm)
162 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
163 else
164 $mbx = $sorted_lsub_ary[$i];
165
166 fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n");
167 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
168 $sorted_list_ary[$i] = $read[0];
169 if (find_mailbox_name($sorted_list_ary[$i]) == "INBOX")
170 $inbox_in_list = true;
171 }
172
173 /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/
174 if ($inbox_subscribed == false || $inbox_in_list == false) {
175 fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
176 $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
177
178 $pos = count($sorted_list_ary);
179 $sorted_list_ary[$pos] = $inbox_ary[0];
180
181 $pos = count($sorted_lsub_ary);
182 $sorted_lsub_ary[$pos] = find_mailbox_name($inbox_ary[0]);
183 }
184
185 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm);
186
187 /** Now, lets sort for special folders **/
188 for ($i = 0; $i < count($boxes); $i++) {
189 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
190 $boxesnew[0] = $boxes[$i];
191 $boxes[$i]["used"] = true;
192 $i = count($boxes);
193 }
194 }
195
196 if ($list_special_folders_first == true) {
197 for ($i = count($boxes)-1; $i >= 0 ; $i--) {
198 if (($boxes[$i]["unformatted"] == $trash_folder) && ($move_to_trash)) {
199 $pos = count($boxesnew);
200 $boxesnew[$pos] = $boxes[$i];
201 $boxes[$i]["used"] = true;
202 $trash_found = true;
203 }
204 else if (($boxes[$i]["unformatted"] == $sent_folder) && ($move_to_sent)) {
205 $pos = count($boxesnew);
206 $boxesnew[$pos] = $boxes[$i];
207 $boxes[$i]["used"] = true;
208 $sent_found = true;
209 }
210
211 if (($sent_found && $trash_found) || ($sent_found && !$move_to_trash) || ($trash_found && !$move_to_sent) || (!$move_to_sent && !$move_to_trash))
212 $i = -1;
213 }
214 }
215
216 for ($i = 0; $i < count($boxes); $i++) {
217 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
218 ($boxes[$i]["used"] == false)) {
219 $pos = count($boxesnew);
220 $boxesnew[$pos] = $boxes[$i];
221 $boxes[$i]["used"] = true;
222 }
223 }
224
225 return $boxesnew;
226 }
227
228 /******************************************************************************
229 ** Returns a list of all folders, subscribed or not
230 ******************************************************************************/
231 function sqimap_mailbox_list_all ($imap_stream) {
232 global $list_special_folders_first, $folder_prefix;
233
234 if (!function_exists ("ary_sort"))
235 include ("../functions/array.php");
236
237 $dm = sqimap_get_delimiter ($imap_stream);
238
239 fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
240 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
241 $g = 0;
242 $phase = "inbox";
243 for ($i = 0; $i < count($read_ary); $i++) {
244 if (substr ($read_ary[$i], 0, 4) != "a001") {
245 $boxes[$g]["raw"] = $read_ary[$i];
246
247 $mailbox = find_mailbox_name($read_ary[$i]);
248 $dm_count = countCharInString($mailbox, $dm);
249 if (substr($mailbox, -1) == $dm)
250 $dm_count--;
251
252 for ($j = 0; $j < $dm_count; $j++)
253 $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
254 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
255
256 $boxes[$g]["unformatted-dm"] = $mailbox;
257 if (substr($mailbox, -1) == $dm)
258 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
259 $boxes[$g]["unformatted"] = $mailbox;
260 $boxes[$g]["id"] = $g;
261
262 /** Now lets get the flags for this mailbox **/
263 fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
264 $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
265
266 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
267 $flags = substr($flags, 0, strpos($flags, ")"));
268 $flags = str_replace("\\", "", $flags);
269 $flags = trim(strtolower($flags));
270 if ($flags) {
271 $boxes[$g]["flags"] = explode(" ", $flags);
272 }
273 }
274 $g++;
275 }
276 if ($boxes) {
277 $boxes = ary_sort ($boxes, "unformatted", 1);
278 }
279 return $boxes;
280 }
281
282 ?>