b9f461c97630be95c73225e9697b68983f07409d
[squirrelmail.git] / functions / imap_mailbox.php
1 <?
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 /******************************************************************************
93 ** Returns sorted mailbox lists in several different ways.
94 ** The array returned looks like this:
95 ******************************************************************************/
96 function sqimap_mailbox_list ($imap_stream) {
97 global $load_prefs_php, $prefs_php, $config_php, $data_dir, $username;
98 if (!isset($load_prefs_php))
99 include "../src/load_prefs.php";
100 else
101 global $folder_prefix;
102 global $special_folders, $list_special_folders_first, $default_folder_prefix;
103
104 if (!function_exists ("ary_sort"))
105 include ("../functions/array.php");
106
107 $dm = sqimap_get_delimiter ($imap_stream);
108
109 fputs ($imap_stream, "a001 LIST \"\" INBOX\r\n");
110 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
111 $g = 0;
112 $phase = "inbox";
113 for ($i = 0; $i < count($read_ary); $i++) {
114 if (substr ($read_ary[$i], 0, 4) != "a001") {
115 $boxes[$g]["raw"] = $read_ary[$i];
116
117 $mailbox = find_mailbox_name($read_ary[$i]);
118 $dm_count = countCharInString($mailbox, $dm);
119 if (substr($mailbox, -1) == $dm)
120 $dm_count--;
121
122 for ($j = 0; $j < $dm_count; $j++)
123 $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
124 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
125
126 $boxes[$g]["unformatted-dm"] = $mailbox;
127 if (substr($mailbox, -1) == $dm)
128 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
129 $boxes[$g]["unformatted"] = $mailbox;
130 $boxes[$g]["id"] = $g;
131
132 /** Now lets get the flags for this mailbox **/
133 fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
134 $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
135
136 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
137 $flags = substr($flags, 0, strpos($flags, ")"));
138 $flags = str_replace("\\", "", $flags);
139 $flags = trim(strtolower($flags));
140 if ($flags) {
141 $boxes[$g]["flags"] = explode(" ", $flags);
142 }
143 }
144 $g++;
145
146 if (!$read_ary[$i+1]) {
147 if ($phase == "inbox") {
148 if ($folder_prefix && (substr($folder_prefix, -1) != $dm))
149 $folder_prefix = $folder_prefix . $dm;
150
151 fputs ($imap_stream, "a001 LSUB \"$folder_prefix\" *\r\n");
152 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
153 $phase = "lsub";
154 $i--;
155 }
156 }
157 }
158
159 $original = $boxes;
160
161 /** Get the folders into lower case so sorting is not case sensative */
162 for ($i = 0; $i < count($original); $i++) {
163 $boxes[$i]["unformatted"] = strtolower($boxes[$i]["unformatted"]);
164 }
165
166 /** Sort them **/
167 $boxes = ary_sort($boxes, "unformatted", 1);
168
169 /** Get them back from the original array, still sorted by the id **/
170 for ($i = 0; $i < count($boxes); $i++) {
171 for ($j = 0; $j < count($original); $j++) {
172 if ($boxes[$i]["id"] == $original[$j]["id"]) {
173 $boxes[$i] = $original[$j];
174 }
175 }
176 }
177
178
179 for ($i = 0; $i < count($boxes); $i++) {
180 if ($boxes[$i]["unformatted"] == $special_folders[0]) {
181 $boxesnew[0] = $boxes[$i];
182 $boxes[$i]["used"] = true;
183 }
184 }
185
186 if ($list_special_folders_first == true) {
187 for ($i = 0; $i < count($boxes); $i++) {
188 for ($j = 1; $j < count($special_folders); $j++) {
189 if (substr($boxes[$i]["unformatted"], 0, strlen($special_folders[$j])) == $special_folders[$j]) {
190 $pos = count($boxesnew);
191 $boxesnew[$pos] = $boxes[$i];
192 $boxes[$i]["used"] = true;
193 }
194 }
195 }
196 }
197
198 for ($i = 0; $i < count($boxes); $i++) {
199 if (($boxes[$i]["unformatted"] != $special_folders[0]) &&
200 ($boxes[$i]["used"] == false)) {
201 $pos = count($boxesnew);
202 $boxesnew[$pos] = $boxes[$i];
203 $boxes[$i]["used"] = true;
204 }
205 }
206
207 return $boxesnew;
208 }
209
210
211 /******************************************************************************
212 ** Returns a list of all folders, subscribed or not
213 ******************************************************************************/
214 function sqimap_mailbox_list_all ($imap_stream) {
215 global $special_folders, $list_special_folders_first;
216
217 if (!function_exists ("ary_sort"))
218 include ("../functions/array.php");
219
220 $dm = sqimap_get_delimiter ($imap_stream);
221
222 fputs ($imap_stream, "a001 LIST \"INBOX\" *\r\n");
223 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
224 $g = 0;
225 $phase = "inbox";
226 for ($i = 0; $i < count($read_ary); $i++) {
227 if (substr ($read_ary[$i], 0, 4) != "a001") {
228 $boxes[$g]["raw"] = $read_ary[$i];
229
230 $mailbox = find_mailbox_name($read_ary[$i]);
231 $dm_count = countCharInString($mailbox, $dm);
232 if (substr($mailbox, -1) == $dm)
233 $dm_count--;
234
235 for ($j = 0; $j < $dm_count; $j++)
236 $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
237 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
238
239 $boxes[$g]["unformatted-dm"] = $mailbox;
240 if (substr($mailbox, -1) == $dm)
241 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
242 $boxes[$g]["unformatted"] = $mailbox;
243 $boxes[$g]["id"] = $g;
244
245 /** Now lets get the flags for this mailbox **/
246 fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
247 $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
248
249 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
250 $flags = substr($flags, 0, strpos($flags, ")"));
251 $flags = str_replace("\\", "", $flags);
252 $flags = trim(strtolower($flags));
253 if ($flags) {
254 $boxes[$g]["flags"] = explode(" ", $flags);
255 }
256 }
257 $g++;
258 }
259 $boxes = ary_sort ($boxes, "unformatted", 1);
260 return $boxes;
261 }
262
263 ?>