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