updated bug file
[squirrelmail.git] / functions / imap_mailbox.php
CommitLineData
59177427 1<?php
8e9e8afa 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) {
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 fputs ($imap_stream, "a001 UNSUBSCRIBE \"$mailbox\"\r\n");
75 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
76 }
77
78
79
80
81 /******************************************************************************
82 ** This function simply deletes the given folder
83 ******************************************************************************/
84 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
85 fputs ($imap_stream, "a001 DELETE \"$mailbox\"\r\n");
86 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
87 sqimap_unsubscribe ($imap_stream, $mailbox);
88 }
89
90
91 /******************************************************************************
92 ** Formats a mailbox into 4 parts for the $boxes array
93 ******************************************************************************/
94 function sqimap_mailbox_parse ($line, $dm) {
aed206bf 95 global $folder_prefix;
8e9e8afa 96 for ($g=0; $g < count($line); $g++) {
97 $boxes[$g]["raw"] = $line[$g];
98
99 $mailbox = find_mailbox_name($line[$g]);
100 $dm_count = countCharInString($mailbox, $dm);
101 if (substr($mailbox, -1) == $dm)
102 $dm_count--;
103
aed206bf 104 for ($j = 0; $j < $dm_count - (countCharInString($folder_prefix, $dm)); $j++)
8e9e8afa 105 $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
106 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
107
108 $boxes[$g]["unformatted-dm"] = $mailbox;
109 if (substr($mailbox, -1) == $dm)
110 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
111 $boxes[$g]["unformatted"] = $mailbox;
112 $boxes[$g]["id"] = $g;
113
114 $flags = substr($line[$g], strpos($line[$g], "(")+1);
115 $flags = substr($flags, 0, strpos($flags, ")"));
116 $flags = str_replace("\\", "", $flags);
117 $flags = trim(strtolower($flags));
118 if ($flags) {
119 $boxes[$g]["flags"] = explode(" ", $flags);
120 }
cbd55894 121 for ($i=0; $i < count($boxes[$g]["flags"]); $i++) {
122 if ($boxes[$g]["flags"][$i] == "noselect") {
123 $boxes[$g]["unformatted-dm"] = $boxes[$g]["unformatted-dm"].$dm;
124 echo $boxes[$g]["unformatted-dm"]." - debug<br>";
125 }
126 }
8e9e8afa 127 }
128 return $boxes;
129 }
130
131 /******************************************************************************
132 ** Returns sorted mailbox lists in several different ways.
133 ** The array returned looks like this:
134 ******************************************************************************/
135 function sqimap_mailbox_list ($imap_stream) {
aed206bf 136 global $load_prefs_php, $prefs_php, $config_php, $data_dir, $username, $list_special_folders_first;
1e0628fb 137 global $trash_folder, $sent_folder;
138 global $move_to_trash, $move_to_sent;
8e9e8afa 139
1590a668 140 $inbox_in_list = false;
141 $inbox_subscribed = false;
142
8e9e8afa 143 if (!isset($load_prefs_php)) include "../src/load_prefs.php";
144 else global $folder_prefix;
145 if (!function_exists ("ary_sort")) include "../functions/array.php";
146
147 $dm = sqimap_get_delimiter ($imap_stream);
148
149 /** LIST array **/
150 fputs ($imap_stream, "a001 LIST \"\" \"$folder_prefix*\"\r\n");
151 $list_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
aed206bf 152
8e9e8afa 153 for ($i=0;$i < count($list_ary); $i++) {
154 $sorted_list_ary[$i]["name"] = find_mailbox_name($list_ary[$i]);
155 $sorted_list_ary[$i]["raw"] = $list_ary[$i];
1590a668 156 if ($sorted_list_ary[$i]["name"] == "INBOX")
8e9e8afa 157 $inbox_in_list = true;
158 }
159 if (isset($sorted_list_ary)) {
160 $list_sorted = array_cleave ($sorted_list_ary, "name");
161 asort($list_sorted);
162 }
163
8e9e8afa 164
165 /** LSUB array **/
166 $inbox_subscribed = false;
167 fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n");
168 $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
169 for ($i=0;$i < count($lsub_ary); $i++) {
170 $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
171 if (substr($sorted_lsub_ary[$i], -1) == $dm)
172 $sorted_lsub_ary[$i] = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
173 if ($sorted_lsub_ary[$i] == "INBOX")
174 $inbox_subscribed = true;
175 }
176 if (isset($sorted_lsub_ary)) {
177 sort($sorted_lsub_ary);
178 }
179
180
181 /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/
182 if ($inbox_subscribed == false || $inbox_in_list == false) {
183 fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
184 $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
185 $merged[0] = $inbox_ary[0];
186 $k = 1;
187 } else {
188 $k = 0;
189 }
190
191 $i = $j = 0;
192
193 if (isset($list_sorted) && isset($sorted_lsub_ary)) {
194 reset ($list_sorted);
195 for (reset($list_sorted); $key = key($list_sorted), isset($key);) {
196 if ($sorted_lsub_ary[$i] == $list_sorted[$key]) {
197 $merged[$k] = $sorted_list_ary[$key]["raw"];
198 $k++;
199 $i++;
200 next($list_sorted);
201 } else if ($sorted_lsub_ary[$i] < $list_sorted[$key]) {
202 $i++;
203 } else {
204 next($list_sorted);
205 }
206 }
207 }
208
aed206bf 209 $boxes = sqimap_mailbox_parse ($merged, $dm);
210
211 /** Now, lets sort for special folders **/
8e9e8afa 212 for ($i = 0; $i < count($boxes); $i++) {
1e0628fb 213 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
8e9e8afa 214 $boxesnew[0] = $boxes[$i];
215 $boxes[$i]["used"] = true;
216 }
217 }
aed206bf 218
8e9e8afa 219 if ($list_special_folders_first == true) {
220 for ($i = 0; $i < count($boxes); $i++) {
1e0628fb 221 if (($boxes[$i]["unformatted"] == $trash_folder) && ($move_to_trash)) {
222 $pos = count($boxesnew);
223 $boxesnew[$pos] = $boxes[$i];
224 $boxes[$i]["used"] = true;
225 }
226 else if (($boxes[$i]["unformatted"] == $sent_folder) && ($move_to_sent)) {
227 $pos = count($boxesnew);
228 $boxesnew[$pos] = $boxes[$i];
229 $boxes[$i]["used"] = true;
8e9e8afa 230 }
231 }
232 }
aed206bf 233
8e9e8afa 234 for ($i = 0; $i < count($boxes); $i++) {
1e0628fb 235 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
8e9e8afa 236 ($boxes[$i]["used"] == false)) {
237 $pos = count($boxesnew);
238 $boxesnew[$pos] = $boxes[$i];
239 $boxes[$i]["used"] = true;
240 }
241 }
242
243 return $boxesnew;
244 }
8e9e8afa 245
246 /******************************************************************************
247 ** Returns a list of all folders, subscribed or not
248 ******************************************************************************/
249 function sqimap_mailbox_list_all ($imap_stream) {
1e0628fb 250 global $list_special_folders_first, $folder_prefix;
8e9e8afa 251
252 if (!function_exists ("ary_sort"))
253 include ("../functions/array.php");
254
255 $dm = sqimap_get_delimiter ($imap_stream);
256
257 fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
258 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
259 $g = 0;
260 $phase = "inbox";
261 for ($i = 0; $i < count($read_ary); $i++) {
262 if (substr ($read_ary[$i], 0, 4) != "a001") {
263 $boxes[$g]["raw"] = $read_ary[$i];
264
265 $mailbox = find_mailbox_name($read_ary[$i]);
266 $dm_count = countCharInString($mailbox, $dm);
267 if (substr($mailbox, -1) == $dm)
268 $dm_count--;
269
270 for ($j = 0; $j < $dm_count; $j++)
271 $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
272 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
273
274 $boxes[$g]["unformatted-dm"] = $mailbox;
275 if (substr($mailbox, -1) == $dm)
276 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
277 $boxes[$g]["unformatted"] = $mailbox;
278 $boxes[$g]["id"] = $g;
279
280 /** Now lets get the flags for this mailbox **/
281 fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
282 $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
283
284 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
285 $flags = substr($flags, 0, strpos($flags, ")"));
286 $flags = str_replace("\\", "", $flags);
287 $flags = trim(strtolower($flags));
288 if ($flags) {
289 $boxes[$g]["flags"] = explode(" ", $flags);
290 }
291 }
292 $g++;
293 }
294 $boxes = ary_sort ($boxes, "unformatted", 1);
295 return $boxes;
296 }
297
052e0c26 298?>