Added basic support for message highlighting. Note that this needs quite
[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) {
26511b3e 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;
8e9e8afa 28 }
8e9e8afa 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
8e9e8afa 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 ******************************************************************************/
16530f8b 96 function sqimap_mailbox_parse ($line, $line_lsub, $dm) {
aed206bf 97 global $folder_prefix;
8e9e8afa 98 for ($g=0; $g < count($line); $g++) {
99 $boxes[$g]["raw"] = $line[$g];
100
16530f8b 101 $mailbox = $line_lsub[$g];
8e9e8afa 102 $dm_count = countCharInString($mailbox, $dm);
103 if (substr($mailbox, -1) == $dm)
104 $dm_count--;
105
aed206bf 106 for ($j = 0; $j < $dm_count - (countCharInString($folder_prefix, $dm)); $j++)
8e9e8afa 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 }
8e9e8afa 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) {
aed206bf 132 global $load_prefs_php, $prefs_php, $config_php, $data_dir, $username, $list_special_folders_first;
1e0628fb 133 global $trash_folder, $sent_folder;
134 global $move_to_trash, $move_to_sent;
8e9e8afa 135
1590a668 136 $inbox_in_list = false;
137 $inbox_subscribed = false;
138
8e9e8afa 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
8e9e8afa 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);
480feea7 149
8e9e8afa 150 for ($i=0;$i < count($lsub_ary); $i++) {
151 $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
8e9e8afa 152 if ($sorted_lsub_ary[$i] == "INBOX")
153 $inbox_subscribed = true;
154 }
155 if (isset($sorted_lsub_ary)) {
156 sort($sorted_lsub_ary);
157 }
158
3cb866d7 159 /** LIST array **/
160 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
161 if (substr($sorted_lsub_ary[$i], -1) == $dm)
162 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
163 else
164 $mbx = $sorted_lsub_ary[$i];
165
166 fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n");
167 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
168 $sorted_list_ary[$i] = $read[0];
169 if (find_mailbox_name($sorted_list_ary[$i]) == "INBOX")
170 $inbox_in_list = true;
171 }
172
8e9e8afa 173 /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/
174 if ($inbox_subscribed == false || $inbox_in_list == false) {
175 fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
176 $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
591000ec 177
178 $pos = count($sorted_list_ary);
179 $sorted_list_ary[$pos] = $inbox_ary[0];
180
181 $pos = count($sorted_lsub_ary);
182 $sorted_lsub_ary[$pos] = find_mailbox_name($inbox_ary[0]);
8e9e8afa 183 }
184
16530f8b 185 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm);
aed206bf 186
187 /** Now, lets sort for special folders **/
8e9e8afa 188 for ($i = 0; $i < count($boxes); $i++) {
1e0628fb 189 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
8e9e8afa 190 $boxesnew[0] = $boxes[$i];
191 $boxes[$i]["used"] = true;
c5eb2c03 192 $i = count($boxes);
8e9e8afa 193 }
194 }
aed206bf 195
8e9e8afa 196 if ($list_special_folders_first == true) {
c5eb2c03 197 for ($i = count($boxes)-1; $i >= 0 ; $i--) {
1e0628fb 198 if (($boxes[$i]["unformatted"] == $trash_folder) && ($move_to_trash)) {
199 $pos = count($boxesnew);
200 $boxesnew[$pos] = $boxes[$i];
201 $boxes[$i]["used"] = true;
c5eb2c03 202 $trash_found = true;
1e0628fb 203 }
204 else if (($boxes[$i]["unformatted"] == $sent_folder) && ($move_to_sent)) {
205 $pos = count($boxesnew);
206 $boxesnew[$pos] = $boxes[$i];
207 $boxes[$i]["used"] = true;
c5eb2c03 208 $sent_found = true;
8e9e8afa 209 }
c5eb2c03 210
211 if (($sent_found && $trash_found) || ($sent_found && !$move_to_trash) || ($trash_found && !$move_to_sent) || (!$move_to_sent && !$move_to_trash))
212 $i = -1;
8e9e8afa 213 }
214 }
c5eb2c03 215
8e9e8afa 216 for ($i = 0; $i < count($boxes); $i++) {
1e0628fb 217 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
8e9e8afa 218 ($boxes[$i]["used"] == false)) {
219 $pos = count($boxesnew);
220 $boxesnew[$pos] = $boxes[$i];
221 $boxes[$i]["used"] = true;
222 }
223 }
224
225 return $boxesnew;
226 }
8e9e8afa 227
228 /******************************************************************************
229 ** Returns a list of all folders, subscribed or not
230 ******************************************************************************/
231 function sqimap_mailbox_list_all ($imap_stream) {
1e0628fb 232 global $list_special_folders_first, $folder_prefix;
8e9e8afa 233
234 if (!function_exists ("ary_sort"))
235 include ("../functions/array.php");
236
237 $dm = sqimap_get_delimiter ($imap_stream);
238
239 fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
240 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
241 $g = 0;
242 $phase = "inbox";
243 for ($i = 0; $i < count($read_ary); $i++) {
244 if (substr ($read_ary[$i], 0, 4) != "a001") {
245 $boxes[$g]["raw"] = $read_ary[$i];
246
247 $mailbox = find_mailbox_name($read_ary[$i]);
248 $dm_count = countCharInString($mailbox, $dm);
249 if (substr($mailbox, -1) == $dm)
250 $dm_count--;
251
252 for ($j = 0; $j < $dm_count; $j++)
253 $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
254 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
255
256 $boxes[$g]["unformatted-dm"] = $mailbox;
257 if (substr($mailbox, -1) == $dm)
258 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
259 $boxes[$g]["unformatted"] = $mailbox;
260 $boxes[$g]["id"] = $g;
261
262 /** Now lets get the flags for this mailbox **/
263 fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
264 $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
265
266 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
267 $flags = substr($flags, 0, strpos($flags, ")"));
268 $flags = str_replace("\\", "", $flags);
269 $flags = trim(strtolower($flags));
270 if ($flags) {
271 $boxes[$g]["flags"] = explode(" ", $flags);
272 }
273 }
274 $g++;
275 }
591000ec 276 if ($boxes) {
277 $boxes = ary_sort ($boxes, "unformatted", 1);
278 }
8e9e8afa 279 return $boxes;
280 }
281
052e0c26 282?>