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