fixed bugs in next/previous
[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 ******************************************************************************/
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 ** Formats a mailbox into 4 parts for the $boxes array
93 ******************************************************************************/
94 function sqimap_mailbox_parse ($line, $dm) {
aed206bf 95 global $folder_prefix;
8e9e8afa 96 for ($g=0; $g < count($line); $g++) {
97 $boxes[$g]["raw"] = $line[$g];
98
99 $mailbox = find_mailbox_name($line[$g]);
100 $dm_count = countCharInString($mailbox, $dm);
101 if (substr($mailbox, -1) == $dm)
102 $dm_count--;
103
aed206bf 104 for ($j = 0; $j < $dm_count - (countCharInString($folder_prefix, $dm)); $j++)
8e9e8afa 105 $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
106 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
107
108 $boxes[$g]["unformatted-dm"] = $mailbox;
109 if (substr($mailbox, -1) == $dm)
110 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
111 $boxes[$g]["unformatted"] = $mailbox;
112 $boxes[$g]["id"] = $g;
113
114 $flags = substr($line[$g], strpos($line[$g], "(")+1);
115 $flags = substr($flags, 0, strpos($flags, ")"));
116 $flags = str_replace("\\", "", $flags);
117 $flags = trim(strtolower($flags));
118 if ($flags) {
119 $boxes[$g]["flags"] = explode(" ", $flags);
120 }
121
122 }
123 return $boxes;
124 }
125
126 /******************************************************************************
127 ** Returns sorted mailbox lists in several different ways.
128 ** The array returned looks like this:
129 ******************************************************************************/
130 function sqimap_mailbox_list ($imap_stream) {
aed206bf 131 global $load_prefs_php, $prefs_php, $config_php, $data_dir, $username, $list_special_folders_first;
132 global $special_folders;
8e9e8afa 133
1590a668 134 $inbox_in_list = false;
135 $inbox_subscribed = false;
136
8e9e8afa 137 if (!isset($load_prefs_php)) include "../src/load_prefs.php";
138 else global $folder_prefix;
139 if (!function_exists ("ary_sort")) include "../functions/array.php";
140
141 $dm = sqimap_get_delimiter ($imap_stream);
142
143 /** LIST array **/
144 fputs ($imap_stream, "a001 LIST \"\" \"$folder_prefix*\"\r\n");
145 $list_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
aed206bf 146
8e9e8afa 147 for ($i=0;$i < count($list_ary); $i++) {
148 $sorted_list_ary[$i]["name"] = find_mailbox_name($list_ary[$i]);
149 $sorted_list_ary[$i]["raw"] = $list_ary[$i];
1590a668 150 if ($sorted_list_ary[$i]["name"] == "INBOX")
8e9e8afa 151 $inbox_in_list = true;
152 }
153 if (isset($sorted_list_ary)) {
154 $list_sorted = array_cleave ($sorted_list_ary, "name");
155 asort($list_sorted);
156 }
157
8e9e8afa 158
159 /** LSUB array **/
160 $inbox_subscribed = false;
161 fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n");
162 $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
163 for ($i=0;$i < count($lsub_ary); $i++) {
164 $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
165 if (substr($sorted_lsub_ary[$i], -1) == $dm)
166 $sorted_lsub_ary[$i] = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
167 if ($sorted_lsub_ary[$i] == "INBOX")
168 $inbox_subscribed = true;
169 }
170 if (isset($sorted_lsub_ary)) {
171 sort($sorted_lsub_ary);
172 }
173
174
175 /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/
176 if ($inbox_subscribed == false || $inbox_in_list == false) {
177 fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
178 $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
179 $merged[0] = $inbox_ary[0];
180 $k = 1;
181 } else {
182 $k = 0;
183 }
184
185 $i = $j = 0;
186
187 if (isset($list_sorted) && isset($sorted_lsub_ary)) {
188 reset ($list_sorted);
189 for (reset($list_sorted); $key = key($list_sorted), isset($key);) {
190 if ($sorted_lsub_ary[$i] == $list_sorted[$key]) {
191 $merged[$k] = $sorted_list_ary[$key]["raw"];
192 $k++;
193 $i++;
194 next($list_sorted);
195 } else if ($sorted_lsub_ary[$i] < $list_sorted[$key]) {
196 $i++;
197 } else {
198 next($list_sorted);
199 }
200 }
201 }
202
aed206bf 203 $boxes = sqimap_mailbox_parse ($merged, $dm);
204
205 /** Now, lets sort for special folders **/
8e9e8afa 206 for ($i = 0; $i < count($boxes); $i++) {
207 if ($boxes[$i]["unformatted"] == $special_folders[0]) {
208 $boxesnew[0] = $boxes[$i];
209 $boxes[$i]["used"] = true;
210 }
211 }
aed206bf 212
8e9e8afa 213 if ($list_special_folders_first == true) {
214 for ($i = 0; $i < count($boxes); $i++) {
215 for ($j = 1; $j < count($special_folders); $j++) {
aed206bf 216 if (substr($boxes[$i]["unformatted"], strlen($folder_prefix), strlen($special_folders[$j])) == $special_folders[$j]) {
8e9e8afa 217 $pos = count($boxesnew);
218 $boxesnew[$pos] = $boxes[$i];
219 $boxes[$i]["used"] = true;
220 }
221 }
222 }
223 }
aed206bf 224
8e9e8afa 225 for ($i = 0; $i < count($boxes); $i++) {
226 if (($boxes[$i]["unformatted"] != $special_folders[0]) &&
227 ($boxes[$i]["used"] == false)) {
228 $pos = count($boxesnew);
229 $boxesnew[$pos] = $boxes[$i];
230 $boxes[$i]["used"] = true;
231 }
232 }
233
234 return $boxesnew;
235 }
8e9e8afa 236
237 /******************************************************************************
238 ** Returns a list of all folders, subscribed or not
239 ******************************************************************************/
240 function sqimap_mailbox_list_all ($imap_stream) {
241 global $special_folders, $list_special_folders_first, $folder_prefix;
242
243 if (!function_exists ("ary_sort"))
244 include ("../functions/array.php");
245
246 $dm = sqimap_get_delimiter ($imap_stream);
247
248 fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
249 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
250 $g = 0;
251 $phase = "inbox";
252 for ($i = 0; $i < count($read_ary); $i++) {
253 if (substr ($read_ary[$i], 0, 4) != "a001") {
254 $boxes[$g]["raw"] = $read_ary[$i];
255
256 $mailbox = find_mailbox_name($read_ary[$i]);
257 $dm_count = countCharInString($mailbox, $dm);
258 if (substr($mailbox, -1) == $dm)
259 $dm_count--;
260
261 for ($j = 0; $j < $dm_count; $j++)
262 $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
263 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
264
265 $boxes[$g]["unformatted-dm"] = $mailbox;
266 if (substr($mailbox, -1) == $dm)
267 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
268 $boxes[$g]["unformatted"] = $mailbox;
269 $boxes[$g]["id"] = $g;
270
271 /** Now lets get the flags for this mailbox **/
272 fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
273 $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
274
275 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
276 $flags = substr($flags, 0, strpos($flags, ")"));
277 $flags = str_replace("\\", "", $flags);
278 $flags = trim(strtolower($flags));
279 if ($flags) {
280 $boxes[$g]["flags"] = explode(" ", $flags);
281 }
282 }
283 $g++;
284 }
285 $boxes = ary_sort ($boxes, "unformatted", 1);
286 return $boxes;
287 }
288
052e0c26 289?>