5 ** This impliments all functions that manipulate mailboxes
8 /******************************************************************************
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);
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);
24 for ($i = 0; $i < count ($boxes); $i++
) {
25 if ($boxes[$i]["unformatted"] == $mailbox)
33 /******************************************************************************
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);
43 /******************************************************************************
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;
51 fputs ($imap_stream, "a001 CREATE \"$mailbox\"\r\n");
52 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
54 sqimap_subscribe ($imap_stream, $mailbox);
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);
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);
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);
91 /******************************************************************************
92 ** Formats a mailbox into 4 parts for the $boxes array
93 ******************************************************************************/
94 function sqimap_mailbox_parse ($line, $dm) {
95 for ($g=0; $g < count($line); $g++
) {
96 $boxes[$g]["raw"] = $line[$g];
98 $mailbox = find_mailbox_name($line[$g]);
99 $dm_count = countCharInString($mailbox, $dm);
100 if (substr($mailbox, -1) == $dm)
103 for ($j = 0; $j < $dm_count; $j++
)
104 $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
105 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
107 $boxes[$g]["unformatted-dm"] = $mailbox;
108 if (substr($mailbox, -1) == $dm)
109 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
110 $boxes[$g]["unformatted"] = $mailbox;
111 $boxes[$g]["id"] = $g;
113 $flags = substr($line[$g], strpos($line[$g], "(")+
1);
114 $flags = substr($flags, 0, strpos($flags, ")"));
115 $flags = str_replace("\\", "", $flags);
116 $flags = trim(strtolower($flags));
118 $boxes[$g]["flags"] = explode(" ", $flags);
124 /******************************************************************************
125 ** Returns sorted mailbox lists in several different ways.
126 ** The array returned looks like this:
127 ******************************************************************************/
128 function sqimap_mailbox_list ($imap_stream) {
129 global $load_prefs_php, $prefs_php, $config_php, $data_dir, $username;
131 if (!isset($load_prefs_php)) include "../src/load_prefs.php";
132 else global $folder_prefix;
133 if (!function_exists ("ary_sort")) include "../functions/array.php";
135 $dm = sqimap_get_delimiter ($imap_stream);
138 fputs ($imap_stream, "a001 LIST \"\" \"$folder_prefix*\"\r\n");
139 $list_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
140 for ($i=0;$i < count($list_ary); $i++
) {
141 $sorted_list_ary[$i]["name"] = find_mailbox_name($list_ary[$i]);
142 $sorted_list_ary[$i]["raw"] = $list_ary[$i];
144 $list_sorted = array_cleave ($sorted_list_ary, "name");
148 fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n");
149 $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
150 for ($i=0;$i < count($lsub_ary); $i++
) {
151 $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
152 if (substr($sorted_lsub_ary[$i], -1) == $dm)
153 $sorted_lsub_ary[$i] = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
155 sort($sorted_lsub_ary);
158 /** Now we'll get a list of subscribed folders WITH flags **/
159 fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
160 $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
161 $merged[0] = $inbox_ary[0];
166 reset ($list_sorted);
167 for (reset($list_sorted); $key = key($list_sorted), isset($key);) {
168 if ($sorted_lsub_ary[$i] == $list_sorted[$key]) {
169 $merged[$k] = $sorted_list_ary[$key]["raw"];
173 } else if ($sorted_lsub_ary[$i] < $list_sorted[$key]) {
180 return sqimap_mailbox_parse ($merged, $dm);
186 for ($i = 0; $i < count($original); $i++) {
187 $boxes[$i]["unformatted"] = strtolower($boxes[$i]["unformatted"]);
190 $boxes = ary_sort($boxes, "unformatted", 1);
192 for ($i = 0; $i < count($boxes); $i++) {
193 for ($j = 0; $j < count($original); $j++) {
194 if ($boxes[$i]["id"] == $original[$j]["id"]) {
195 $boxes[$i] = $original[$j];
201 for ($i = 0; $i < count($boxes); $i++) {
202 if ($boxes[$i]["unformatted"] == $special_folders[0]) {
203 $boxesnew[0] = $boxes[$i];
204 $boxes[$i]["used"] = true;
208 if ($list_special_folders_first == true) {
209 for ($i = 0; $i < count($boxes); $i++) {
210 for ($j = 1; $j < count($special_folders); $j++) {
211 if (substr($boxes[$i]["unformatted"], 0, strlen($special_folders[$j])) == $special_folders[$j]) {
212 $pos = count($boxesnew);
213 $boxesnew[$pos] = $boxes[$i];
214 $boxes[$i]["used"] = true;
220 for ($i = 0; $i < count($boxes); $i++) {
221 if (($boxes[$i]["unformatted"] != $special_folders[0]) &&
222 ($boxes[$i]["used"] == false)) {
223 $pos = count($boxesnew);
224 $boxesnew[$pos] = $boxes[$i];
225 $boxes[$i]["used"] = true;
233 /******************************************************************************
234 ** Returns a list of all folders, subscribed or not
235 ******************************************************************************/
236 function sqimap_mailbox_list_all ($imap_stream) {
237 global $special_folders, $list_special_folders_first, $folder_prefix;
239 if (!function_exists ("ary_sort"))
240 include ("../functions/array.php");
242 $dm = sqimap_get_delimiter ($imap_stream);
244 fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
245 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
248 for ($i = 0; $i < count($read_ary); $i++
) {
249 if (substr ($read_ary[$i], 0, 4) != "a001") {
250 $boxes[$g]["raw"] = $read_ary[$i];
252 $mailbox = find_mailbox_name($read_ary[$i]);
253 $dm_count = countCharInString($mailbox, $dm);
254 if (substr($mailbox, -1) == $dm)
257 for ($j = 0; $j < $dm_count; $j++
)
258 $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
259 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
261 $boxes[$g]["unformatted-dm"] = $mailbox;
262 if (substr($mailbox, -1) == $dm)
263 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
264 $boxes[$g]["unformatted"] = $mailbox;
265 $boxes[$g]["id"] = $g;
267 /** Now lets get the flags for this mailbox **/
268 fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
269 $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
271 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+
1);
272 $flags = substr($flags, 0, strpos($flags, ")"));
273 $flags = str_replace("\\", "", $flags);
274 $flags = trim(strtolower($flags));
276 $boxes[$g]["flags"] = explode(" ", $flags);
281 $boxes = ary_sort ($boxes, "unformatted", 1);