fixed double folder problem
[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);
a2790a61 15 sqimap_mailbox_close ($imap_stream);
8e9e8afa 16 }
17
18
19 /******************************************************************************
20 ** Checks whether or not the specified mailbox exists
21 ******************************************************************************/
22 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
26511b3e 23 fputs ($imap_stream, "a001 LIST \"\" \"$mailbox\"\r\n");
24 $mbx = sqimap_read_data($imap_stream, "a001", true, $response, $message);
44f642f5 25 if ($mailbox) {
26 if (ereg ("$mailbox", $mbx[0])) {
27 return true;
28 } else {
29 return false;
30 }
8e9e8afa 31 }
8e9e8afa 32 }
33
a2790a61 34 /******************************************************************************
35 ** Closes an open mailbox
36 ******************************************************************************/
37 function sqimap_mailbox_close ($imap_stream) {
38 fputs ($imap_stream, "a001 CLOSE\r\n");
c95df380 39 $tmp = sqimap_read_data($imap_stream, "a001", false, $response, $message);
a2790a61 40 }
8e9e8afa 41
8e9e8afa 42 /******************************************************************************
43 ** Selects a mailbox
44 ******************************************************************************/
04632dbc 45 function sqimap_mailbox_select ($imap_stream, $mailbox, $hide=true, $recent=false) {
8e9e8afa 46 fputs ($imap_stream, "a001 SELECT \"$mailbox\"\r\n");
c5eb2c03 47 $read = sqimap_read_data($imap_stream, "a001", true, $response, $message);
04632dbc 48 if ($recent) {
49 for ($i=0; $i<count($read); $i++) {
50 if (strpos(strtolower($read[$i]), "recent")) {
51 $r = explode(" ", $read[$i]);
52 }
53 }
54 return $r[1];
55 }
8e9e8afa 56 }
57
58
59
60 /******************************************************************************
61 ** Creates a folder
62 ******************************************************************************/
63 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
64 if (strtolower($type) == "noselect") {
65 $dm = sqimap_get_delimiter($imap_stream);
66 $mailbox = $mailbox.$dm;
67 }
68 fputs ($imap_stream, "a001 CREATE \"$mailbox\"\r\n");
69 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
70
71 sqimap_subscribe ($imap_stream, $mailbox);
72 }
73
74
75
76 /******************************************************************************
77 ** Subscribes to an existing folder
78 ******************************************************************************/
79 function sqimap_subscribe ($imap_stream, $mailbox) {
80 fputs ($imap_stream, "a001 SUBSCRIBE \"$mailbox\"\r\n");
81 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
82 }
83
84
85
86
87 /******************************************************************************
88 ** Unsubscribes to an existing folder
89 ******************************************************************************/
90 function sqimap_unsubscribe ($imap_stream, $mailbox) {
cbdc5621 91 global $imap_server_type;
92
8e9e8afa 93 fputs ($imap_stream, "a001 UNSUBSCRIBE \"$mailbox\"\r\n");
94 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
95 }
96
97
98
99
100 /******************************************************************************
101 ** This function simply deletes the given folder
102 ******************************************************************************/
103 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
104 fputs ($imap_stream, "a001 DELETE \"$mailbox\"\r\n");
105 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
106 sqimap_unsubscribe ($imap_stream, $mailbox);
107 }
108
109
110 /******************************************************************************
111 ** Formats a mailbox into 4 parts for the $boxes array
112 ******************************************************************************/
16530f8b 113 function sqimap_mailbox_parse ($line, $line_lsub, $dm) {
aed206bf 114 global $folder_prefix;
8e9e8afa 115 for ($g=0; $g < count($line); $g++) {
116 $boxes[$g]["raw"] = $line[$g];
117
16530f8b 118 $mailbox = $line_lsub[$g];
8e9e8afa 119 $dm_count = countCharInString($mailbox, $dm);
120 if (substr($mailbox, -1) == $dm)
121 $dm_count--;
122
aed206bf 123 for ($j = 0; $j < $dm_count - (countCharInString($folder_prefix, $dm)); $j++)
390372b4 124 $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . "&nbsp;&nbsp;";
8e9e8afa 125 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
126
127 $boxes[$g]["unformatted-dm"] = $mailbox;
128 if (substr($mailbox, -1) == $dm)
129 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
130 $boxes[$g]["unformatted"] = $mailbox;
131 $boxes[$g]["id"] = $g;
132
133 $flags = substr($line[$g], strpos($line[$g], "(")+1);
134 $flags = substr($flags, 0, strpos($flags, ")"));
135 $flags = str_replace("\\", "", $flags);
136 $flags = trim(strtolower($flags));
137 if ($flags) {
138 $boxes[$g]["flags"] = explode(" ", $flags);
139 }
8e9e8afa 140 }
141 return $boxes;
142 }
143
144 /******************************************************************************
145 ** Returns sorted mailbox lists in several different ways.
146 ** The array returned looks like this:
147 ******************************************************************************/
148 function sqimap_mailbox_list ($imap_stream) {
aed206bf 149 global $load_prefs_php, $prefs_php, $config_php, $data_dir, $username, $list_special_folders_first;
1e0628fb 150 global $trash_folder, $sent_folder;
151 global $move_to_trash, $move_to_sent;
8e9e8afa 152
1590a668 153 $inbox_in_list = false;
154 $inbox_subscribed = false;
155
8e9e8afa 156 if (!isset($load_prefs_php)) include "../src/load_prefs.php";
157 else global $folder_prefix;
158 if (!function_exists ("ary_sort")) include "../functions/array.php";
159
160 $dm = sqimap_get_delimiter ($imap_stream);
161
8e9e8afa 162 /** LSUB array **/
163 $inbox_subscribed = false;
164 fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n");
165 $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
480feea7 166
8e9e8afa 167 for ($i=0;$i < count($lsub_ary); $i++) {
168 $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
8e9e8afa 169 if ($sorted_lsub_ary[$i] == "INBOX")
170 $inbox_subscribed = true;
171 }
ee62ce13 172 $new_ary = array();
173 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
174 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
175 $new_ary[] = $sorted_lsub_ary[$i];
176 }
177 }
178 $sorted_lsub_ary = $new_ary;
8e9e8afa 179 if (isset($sorted_lsub_ary)) {
180 sort($sorted_lsub_ary);
181 }
182
3cb866d7 183 /** LIST array **/
184 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
185 if (substr($sorted_lsub_ary[$i], -1) == $dm)
186 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
187 else
188 $mbx = $sorted_lsub_ary[$i];
189
190 fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n");
191 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
192 $sorted_list_ary[$i] = $read[0];
193 if (find_mailbox_name($sorted_list_ary[$i]) == "INBOX")
194 $inbox_in_list = true;
195 }
196
8e9e8afa 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);
591000ec 201
202 $pos = count($sorted_list_ary);
203 $sorted_list_ary[$pos] = $inbox_ary[0];
204
205 $pos = count($sorted_lsub_ary);
206 $sorted_lsub_ary[$pos] = find_mailbox_name($inbox_ary[0]);
8e9e8afa 207 }
208
16530f8b 209 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm);
aed206bf 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;
c5eb2c03 216 $i = count($boxes);
8e9e8afa 217 }
218 }
aed206bf 219
8e9e8afa 220 if ($list_special_folders_first == true) {
c5eb2c03 221 for ($i = count($boxes)-1; $i >= 0 ; $i--) {
1e0628fb 222 if (($boxes[$i]["unformatted"] == $trash_folder) && ($move_to_trash)) {
223 $pos = count($boxesnew);
224 $boxesnew[$pos] = $boxes[$i];
225 $boxes[$i]["used"] = true;
c5eb2c03 226 $trash_found = true;
1e0628fb 227 }
228 else if (($boxes[$i]["unformatted"] == $sent_folder) && ($move_to_sent)) {
229 $pos = count($boxesnew);
230 $boxesnew[$pos] = $boxes[$i];
231 $boxes[$i]["used"] = true;
c5eb2c03 232 $sent_found = true;
8e9e8afa 233 }
c5eb2c03 234
235 if (($sent_found && $trash_found) || ($sent_found && !$move_to_trash) || ($trash_found && !$move_to_sent) || (!$move_to_sent && !$move_to_trash))
236 $i = -1;
8e9e8afa 237 }
238 }
c5eb2c03 239
8e9e8afa 240 for ($i = 0; $i < count($boxes); $i++) {
1e0628fb 241 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
8e9e8afa 242 ($boxes[$i]["used"] == false)) {
243 $pos = count($boxesnew);
244 $boxesnew[$pos] = $boxes[$i];
245 $boxes[$i]["used"] = true;
246 }
247 }
248
249 return $boxesnew;
250 }
8e9e8afa 251
252 /******************************************************************************
253 ** Returns a list of all folders, subscribed or not
254 ******************************************************************************/
255 function sqimap_mailbox_list_all ($imap_stream) {
1e0628fb 256 global $list_special_folders_first, $folder_prefix;
8e9e8afa 257
258 if (!function_exists ("ary_sort"))
259 include ("../functions/array.php");
260
261 $dm = sqimap_get_delimiter ($imap_stream);
262
263 fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
264 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
265 $g = 0;
266 $phase = "inbox";
267 for ($i = 0; $i < count($read_ary); $i++) {
268 if (substr ($read_ary[$i], 0, 4) != "a001") {
269 $boxes[$g]["raw"] = $read_ary[$i];
270
271 $mailbox = find_mailbox_name($read_ary[$i]);
272 $dm_count = countCharInString($mailbox, $dm);
273 if (substr($mailbox, -1) == $dm)
274 $dm_count--;
275
276 for ($j = 0; $j < $dm_count; $j++)
277 $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
278 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
279
280 $boxes[$g]["unformatted-dm"] = $mailbox;
281 if (substr($mailbox, -1) == $dm)
282 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
283 $boxes[$g]["unformatted"] = $mailbox;
284 $boxes[$g]["id"] = $g;
285
286 /** Now lets get the flags for this mailbox **/
287 fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
288 $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
289
290 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
291 $flags = substr($flags, 0, strpos($flags, ")"));
292 $flags = str_replace("\\", "", $flags);
293 $flags = trim(strtolower($flags));
294 if ($flags) {
295 $boxes[$g]["flags"] = explode(" ", $flags);
296 }
297 }
298 $g++;
299 }
591000ec 300 if ($boxes) {
301 $boxes = ary_sort ($boxes, "unformatted", 1);
302 }
8e9e8afa 303 return $boxes;
304 }
305
052e0c26 306?>