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