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);
15 sqimap_mailbox_close ($imap_stream);
19 /******************************************************************************
20 ** Checks whether or not the specified mailbox exists
21 ******************************************************************************/
22 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
23 fputs ($imap_stream, "a001 LIST \"\" \"$mailbox\"\r\n");
24 $mbx = sqimap_read_data($imap_stream, "a001", true, $response, $message);
26 return !!(ereg ("$mailbox", $mbx[0])); // To force into true/false
30 /******************************************************************************
31 ** Closes an open mailbox
32 ******************************************************************************/
33 function sqimap_mailbox_close ($imap_stream) {
34 fputs ($imap_stream, "a001 CLOSE\r\n");
35 $tmp = sqimap_read_data($imap_stream, "a001", false, $response, $message);
38 /******************************************************************************
40 ******************************************************************************/
41 function sqimap_mailbox_select ($imap_stream, $mailbox, $hide=true, $recent=false) {
44 fputs ($imap_stream, "a001 SELECT \"$mailbox\"\r\n");
45 $read = sqimap_read_data($imap_stream, "a001", true, $response, $message);
47 for ($i=0; $i<count($read); $i++
) {
48 if (strpos(strtolower($read[$i]), "recent")) {
49 $r = explode(" ", $read[$i]);
55 fputs ($imap_stream, "a001 EXPUNGE\r\n");
56 $tmp = sqimap_read_data($imap_stream, "a001", true, $a, $b);
62 /******************************************************************************
64 ******************************************************************************/
65 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
66 if (strtolower($type) == "noselect") {
67 $dm = sqimap_get_delimiter($imap_stream);
68 $mailbox = $mailbox.$dm;
70 fputs ($imap_stream, "a001 CREATE \"$mailbox\"\r\n");
71 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
73 sqimap_subscribe ($imap_stream, $mailbox);
78 /******************************************************************************
79 ** Subscribes to an existing folder
80 ******************************************************************************/
81 function sqimap_subscribe ($imap_stream, $mailbox) {
82 fputs ($imap_stream, "a001 SUBSCRIBE \"$mailbox\"\r\n");
83 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
89 /******************************************************************************
90 ** Unsubscribes to an existing folder
91 ******************************************************************************/
92 function sqimap_unsubscribe ($imap_stream, $mailbox) {
93 global $imap_server_type;
95 fputs ($imap_stream, "a001 UNSUBSCRIBE \"$mailbox\"\r\n");
96 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
102 /******************************************************************************
103 ** This function simply deletes the given folder
104 ******************************************************************************/
105 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
106 fputs ($imap_stream, "a001 DELETE \"$mailbox\"\r\n");
107 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
108 sqimap_unsubscribe ($imap_stream, $mailbox);
112 /******************************************************************************
113 ** Formats a mailbox into 4 parts for the $boxes array
114 ******************************************************************************/
115 function sqimap_mailbox_parse ($line, $line_lsub, $dm) {
116 global $folder_prefix;
117 for ($g=0; $g < count($line); $g++
) {
118 $boxes[$g]["raw"] = $line[$g];
120 $mailbox = trim($line_lsub[$g]);
121 $dm_count = countCharInString($mailbox, $dm);
122 if (substr($mailbox, -1) == $dm)
125 for ($j = 0; $j < $dm_count - (countCharInString($folder_prefix, $dm)); $j++
)
126 $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
127 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
129 $boxes[$g]["unformatted-dm"] = $mailbox;
130 if (substr($mailbox, -1) == $dm)
131 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
132 $boxes[$g]["unformatted"] = $mailbox;
133 $boxes[$g]["id"] = $g;
135 ereg("\(([^)]*)\)",$line[$g],$regs);
136 $flags = trim(strtolower(str_replace("\\", "",$regs[1])));
138 $boxes[$g]["flags"] = explode(" ", $flags);
144 /******************************************************************************
145 ** Returns sorted mailbox lists in several different ways.
146 ** The array returned looks like this:
147 ******************************************************************************/
148 function sqimap_mailbox_list ($imap_stream) {
149 global $load_prefs_php, $prefs_php, $config_php, $data_dir, $username, $list_special_folders_first;
150 global $trash_folder, $sent_folder;
151 global $move_to_trash, $move_to_sent;
153 $inbox_in_list = false;
154 $inbox_subscribed = false;
156 if (!isset($load_prefs_php)) include "../src/load_prefs.php";
157 else global $folder_prefix;
158 if (!function_exists ("ary_sort")) include "../functions/array.php";
160 $dm = sqimap_get_delimiter ($imap_stream);
163 $inbox_subscribed = false;
164 fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n");
165 $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
167 for ($i=0;$i < count($lsub_ary); $i++
) {
168 $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
169 if ($sorted_lsub_ary[$i] == "INBOX")
170 $inbox_subscribed = true;
173 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
174 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
175 $new_ary[] = $sorted_lsub_ary[$i];
178 $sorted_lsub_ary = $new_ary;
179 if (isset($sorted_lsub_ary)) {
180 sort($sorted_lsub_ary);
184 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
185 if (substr($sorted_lsub_ary[$i], -1) == $dm)
186 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
188 $mbx = $sorted_lsub_ary[$i];
190 fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n");
191 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
192 $sorted_list_ary[$i] = $read[0];
193 if (find_mailbox_name($sorted_list_ary[$i]) == "INBOX")
194 $inbox_in_list = true;
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);
202 $pos = count($sorted_list_ary);
203 $sorted_list_ary[$pos] = $inbox_ary[0];
205 $pos = count($sorted_lsub_ary);
206 $sorted_lsub_ary[$pos] = find_mailbox_name($inbox_ary[0]);
209 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm);
211 /** Now, lets sort for special folders **/
212 for ($i = 0; $i < count($boxes); $i++
) {
213 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
214 $boxesnew[0] = $boxes[$i];
215 $boxes[$i]["used"] = true;
220 if ($list_special_folders_first == true) {
221 for ($i = count($boxes)-1; $i >= 0 ; $i--) {
222 if (($boxes[$i]["unformatted"] == $trash_folder) && ($move_to_trash)) {
223 $pos = count($boxesnew);
224 $boxesnew[$pos] = $boxes[$i];
225 $boxes[$i]["used"] = true;
228 else if (($boxes[$i]["unformatted"] == $sent_folder) && ($move_to_sent)) {
229 $pos = count($boxesnew);
230 $boxesnew[$pos] = $boxes[$i];
231 $boxes[$i]["used"] = true;
235 if (($sent_found && $trash_found) ||
($sent_found && !$move_to_trash) ||
($trash_found && !$move_to_sent) ||
(!$move_to_sent && !$move_to_trash))
240 for ($i = 0; $i < count($boxes); $i++
) {
241 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
242 ($boxes[$i]["used"] == false)) {
243 $pos = count($boxesnew);
244 $boxesnew[$pos] = $boxes[$i];
245 $boxes[$i]["used"] = true;
252 /******************************************************************************
253 ** Returns a list of all folders, subscribed or not
254 ******************************************************************************/
255 function sqimap_mailbox_list_all ($imap_stream) {
256 global $list_special_folders_first, $folder_prefix;
258 if (!function_exists ("ary_sort"))
259 include ("../functions/array.php");
261 $dm = sqimap_get_delimiter ($imap_stream);
263 fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
264 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
267 for ($i = 0; $i < count($read_ary); $i++
) {
268 if (substr ($read_ary[$i], 0, 4) != "a001") {
269 $boxes[$g]["raw"] = $read_ary[$i];
271 $mailbox = find_mailbox_name($read_ary[$i]);
272 $dm_count = countCharInString($mailbox, $dm);
273 if (substr($mailbox, -1) == $dm)
276 for ($j = 0; $j < $dm_count; $j++
)
277 $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
278 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
280 $boxes[$g]["unformatted-dm"] = $mailbox;
281 if (substr($mailbox, -1) == $dm)
282 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
283 $boxes[$g]["unformatted"] = $mailbox;
284 $boxes[$g]["id"] = $g;
286 /** Now lets get the flags for this mailbox **/
287 fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
288 $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
290 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+
1);
291 $flags = substr($flags, 0, strpos($flags, ")"));
292 $flags = str_replace("\\", "", $flags);
293 $flags = trim(strtolower($flags));
295 $boxes[$g]["flags"] = explode(" ", $flags);
301 $boxes = ary_sort ($boxes, "unformatted", 1);