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