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 global $imap_server_type;
76 /** This is a hack for UW server
77 ** Sometimes a folder will have a / at the end. If that's the case,
78 ** the unsubscribe doesn't work for a box named "mailbox/". We have
79 ** to strip off the / at the end. There may be a better way of doing
80 ** this, but this is the best I've found so far. (lme - April 26, 2000)
82 if ($imap_server_type == "uw") {
83 if (substr($mailbox, -1) == "/") {
84 $mailbox = substr($mailbox, 0, strlen($mailbox)-1);
88 fputs ($imap_stream, "a001 UNSUBSCRIBE \"$mailbox\"\r\n");
89 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
95 /******************************************************************************
96 ** This function simply deletes the given folder
97 ******************************************************************************/
98 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
99 fputs ($imap_stream, "a001 DELETE \"$mailbox\"\r\n");
100 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
101 sqimap_unsubscribe ($imap_stream, $mailbox);
105 /******************************************************************************
106 ** Formats a mailbox into 4 parts for the $boxes array
107 ******************************************************************************/
108 function sqimap_mailbox_parse ($line, $dm) {
109 global $folder_prefix;
110 for ($g=0; $g < count($line); $g++
) {
111 $boxes[$g]["raw"] = $line[$g];
113 $mailbox = find_mailbox_name($line[$g]);
114 $dm_count = countCharInString($mailbox, $dm);
115 if (substr($mailbox, -1) == $dm)
118 for ($j = 0; $j < $dm_count - (countCharInString($folder_prefix, $dm)); $j++
)
119 $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
120 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
122 $boxes[$g]["unformatted-dm"] = $mailbox;
123 if (substr($mailbox, -1) == $dm)
124 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
125 $boxes[$g]["unformatted"] = $mailbox;
126 $boxes[$g]["id"] = $g;
128 $flags = substr($line[$g], strpos($line[$g], "(")+
1);
129 $flags = substr($flags, 0, strpos($flags, ")"));
130 $flags = str_replace("\\", "", $flags);
131 $flags = trim(strtolower($flags));
133 $boxes[$g]["flags"] = explode(" ", $flags);
135 /**** I'm not sure why this was even in here to begin with.. (lme)
136 for ($i=0; $i < count($boxes[$g]["flags"]); $i++) {
137 if ($boxes[$g]["flags"][$i] == "noselect") {
138 $boxes[$g]["unformatted-dm"] = $boxes[$g]["unformatted-dm"].$dm;
139 // echo $boxes[$g]["unformatted-dm"]." - debug<br>";
147 /******************************************************************************
148 ** Returns sorted mailbox lists in several different ways.
149 ** The array returned looks like this:
150 ******************************************************************************/
151 function sqimap_mailbox_list ($imap_stream) {
152 global $load_prefs_php, $prefs_php, $config_php, $data_dir, $username, $list_special_folders_first;
153 global $trash_folder, $sent_folder;
154 global $move_to_trash, $move_to_sent;
156 $inbox_in_list = false;
157 $inbox_subscribed = false;
159 if (!isset($load_prefs_php)) include "../src/load_prefs.php";
160 else global $folder_prefix;
161 if (!function_exists ("ary_sort")) include "../functions/array.php";
163 $dm = sqimap_get_delimiter ($imap_stream);
166 fputs ($imap_stream, "a001 LIST \"\" \"$folder_prefix*\"\r\n");
167 $list_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
169 for ($i=0;$i < count($list_ary); $i++
) {
170 $sorted_list_ary[$i]["name"] = find_mailbox_name($list_ary[$i]);
171 $sorted_list_ary[$i]["raw"] = $list_ary[$i];
172 if ($sorted_list_ary[$i]["name"] == "INBOX")
173 $inbox_in_list = true;
175 if (isset($sorted_list_ary)) {
176 $list_sorted = array_cleave ($sorted_list_ary, "name");
182 $inbox_subscribed = false;
183 fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n");
184 $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
185 for ($i=0;$i < count($lsub_ary); $i++
) {
186 $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
187 if (substr($sorted_lsub_ary[$i], -1) == $dm)
188 $sorted_lsub_ary[$i] = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
189 if ($sorted_lsub_ary[$i] == "INBOX")
190 $inbox_subscribed = true;
192 if (isset($sorted_lsub_ary)) {
193 sort($sorted_lsub_ary);
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);
201 $merged[0] = $inbox_ary[0];
209 if (isset($list_sorted) && isset($sorted_lsub_ary)) {
210 reset ($list_sorted);
211 for (reset($list_sorted); $key = key($list_sorted), isset($key);) {
212 if ($sorted_lsub_ary[$i] == $list_sorted[$key]) {
213 $merged[$k] = $sorted_list_ary[$key]["raw"];
217 } else if ($sorted_lsub_ary[$i] < $list_sorted[$key]) {
225 $boxes = sqimap_mailbox_parse ($merged, $dm);
227 /** Now, lets sort for special folders **/
228 for ($i = 0; $i < count($boxes); $i++
) {
229 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
230 $boxesnew[0] = $boxes[$i];
231 $boxes[$i]["used"] = true;
235 if ($list_special_folders_first == true) {
236 for ($i = 0; $i < count($boxes); $i++
) {
237 if (($boxes[$i]["unformatted"] == $trash_folder) && ($move_to_trash)) {
238 $pos = count($boxesnew);
239 $boxesnew[$pos] = $boxes[$i];
240 $boxes[$i]["used"] = true;
242 else if (($boxes[$i]["unformatted"] == $sent_folder) && ($move_to_sent)) {
243 $pos = count($boxesnew);
244 $boxesnew[$pos] = $boxes[$i];
245 $boxes[$i]["used"] = true;
250 for ($i = 0; $i < count($boxes); $i++
) {
251 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
252 ($boxes[$i]["used"] == false)) {
253 $pos = count($boxesnew);
254 $boxesnew[$pos] = $boxes[$i];
255 $boxes[$i]["used"] = true;
262 /******************************************************************************
263 ** Returns a list of all folders, subscribed or not
264 ******************************************************************************/
265 function sqimap_mailbox_list_all ($imap_stream) {
266 global $list_special_folders_first, $folder_prefix;
268 if (!function_exists ("ary_sort"))
269 include ("../functions/array.php");
271 $dm = sqimap_get_delimiter ($imap_stream);
273 fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
274 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
277 for ($i = 0; $i < count($read_ary); $i++
) {
278 if (substr ($read_ary[$i], 0, 4) != "a001") {
279 $boxes[$g]["raw"] = $read_ary[$i];
281 $mailbox = find_mailbox_name($read_ary[$i]);
282 $dm_count = countCharInString($mailbox, $dm);
283 if (substr($mailbox, -1) == $dm)
286 for ($j = 0; $j < $dm_count; $j++
)
287 $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
288 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
290 $boxes[$g]["unformatted-dm"] = $mailbox;
291 if (substr($mailbox, -1) == $dm)
292 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
293 $boxes[$g]["unformatted"] = $mailbox;
294 $boxes[$g]["id"] = $g;
296 /** Now lets get the flags for this mailbox **/
297 fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
298 $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
300 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+
1);
301 $flags = substr($flags, 0, strpos($flags, ")"));
302 $flags = str_replace("\\", "", $flags);
303 $flags = trim(strtolower($flags));
305 $boxes[$g]["flags"] = explode(" ", $flags);
310 $boxes = ary_sort ($boxes, "unformatted", 1);