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