422638ed3193150b8a06adb94ac9cfce9ade72ad
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 if (ereg ("$mailbox", $mbx[0])) {
34 /******************************************************************************
35 ** Closes an open mailbox
36 ******************************************************************************/
37 function sqimap_mailbox_close ($imap_stream) {
38 fputs ($imap_stream, "a001 CLOSE\r\n");
39 $tmp = sqimap_read_data($imap_stream, "a001", false, $response, $message);
42 /******************************************************************************
44 ******************************************************************************/
45 function sqimap_mailbox_select ($imap_stream, $mailbox, $hide=true, $recent=false) {
46 fputs ($imap_stream, "a001 SELECT \"$mailbox\"\r\n");
47 $read = sqimap_read_data($imap_stream, "a001", true, $response, $message);
49 for ($i=0; $i<count($read); $i++
) {
50 if (strpos(strtolower($read[$i]), "recent")) {
51 $r = explode(" ", $read[$i]);
60 /******************************************************************************
62 ******************************************************************************/
63 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
64 if (strtolower($type) == "noselect") {
65 $dm = sqimap_get_delimiter($imap_stream);
66 $mailbox = $mailbox.$dm;
68 fputs ($imap_stream, "a001 CREATE \"$mailbox\"\r\n");
69 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
71 sqimap_subscribe ($imap_stream, $mailbox);
76 /******************************************************************************
77 ** Subscribes to an existing folder
78 ******************************************************************************/
79 function sqimap_subscribe ($imap_stream, $mailbox) {
80 fputs ($imap_stream, "a001 SUBSCRIBE \"$mailbox\"\r\n");
81 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
87 /******************************************************************************
88 ** Unsubscribes to an existing folder
89 ******************************************************************************/
90 function sqimap_unsubscribe ($imap_stream, $mailbox) {
91 global $imap_server_type;
93 fputs ($imap_stream, "a001 UNSUBSCRIBE \"$mailbox\"\r\n");
94 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
100 /******************************************************************************
101 ** This function simply deletes the given folder
102 ******************************************************************************/
103 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
104 fputs ($imap_stream, "a001 DELETE \"$mailbox\"\r\n");
105 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
106 sqimap_unsubscribe ($imap_stream, $mailbox);
110 /******************************************************************************
111 ** Formats a mailbox into 4 parts for the $boxes array
112 ******************************************************************************/
113 function sqimap_mailbox_parse ($line, $line_lsub, $dm) {
114 global $folder_prefix;
115 for ($g=0; $g < count($line); $g++
) {
116 $boxes[$g]["raw"] = $line[$g];
118 $mailbox = $line_lsub[$g];
119 $dm_count = countCharInString($mailbox, $dm);
120 if (substr($mailbox, -1) == $dm)
123 for ($j = 0; $j < $dm_count - (countCharInString($folder_prefix, $dm)); $j++
)
124 $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
125 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
127 $boxes[$g]["unformatted-dm"] = $mailbox;
128 if (substr($mailbox, -1) == $dm)
129 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
130 $boxes[$g]["unformatted"] = $mailbox;
131 $boxes[$g]["id"] = $g;
133 $flags = substr($line[$g], strpos($line[$g], "(")+
1);
134 $flags = substr($flags, 0, strpos($flags, ")"));
135 $flags = str_replace("\\", "", $flags);
136 $flags = trim(strtolower($flags));
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;
172 if (isset($sorted_lsub_ary)) {
173 sort($sorted_lsub_ary);
177 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
178 if (substr($sorted_lsub_ary[$i], -1) == $dm)
179 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
181 $mbx = $sorted_lsub_ary[$i];
183 fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n");
184 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
185 $sorted_list_ary[$i] = $read[0];
186 if (find_mailbox_name($sorted_list_ary[$i]) == "INBOX")
187 $inbox_in_list = true;
190 /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/
191 if ($inbox_subscribed == false ||
$inbox_in_list == false) {
192 fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
193 $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
195 $pos = count($sorted_list_ary);
196 $sorted_list_ary[$pos] = $inbox_ary[0];
198 $pos = count($sorted_lsub_ary);
199 $sorted_lsub_ary[$pos] = find_mailbox_name($inbox_ary[0]);
202 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm);
204 /** Now, lets sort for special folders **/
205 for ($i = 0; $i < count($boxes); $i++
) {
206 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
207 $boxesnew[0] = $boxes[$i];
208 $boxes[$i]["used"] = true;
213 if ($list_special_folders_first == true) {
214 for ($i = count($boxes)-1; $i >= 0 ; $i--) {
215 if (($boxes[$i]["unformatted"] == $trash_folder) && ($move_to_trash)) {
216 $pos = count($boxesnew);
217 $boxesnew[$pos] = $boxes[$i];
218 $boxes[$i]["used"] = true;
221 else if (($boxes[$i]["unformatted"] == $sent_folder) && ($move_to_sent)) {
222 $pos = count($boxesnew);
223 $boxesnew[$pos] = $boxes[$i];
224 $boxes[$i]["used"] = true;
228 if (($sent_found && $trash_found) ||
($sent_found && !$move_to_trash) ||
($trash_found && !$move_to_sent) ||
(!$move_to_sent && !$move_to_trash))
233 for ($i = 0; $i < count($boxes); $i++
) {
234 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
235 ($boxes[$i]["used"] == false)) {
236 $pos = count($boxesnew);
237 $boxesnew[$pos] = $boxes[$i];
238 $boxes[$i]["used"] = true;
245 /******************************************************************************
246 ** Returns a list of all folders, subscribed or not
247 ******************************************************************************/
248 function sqimap_mailbox_list_all ($imap_stream) {
249 global $list_special_folders_first, $folder_prefix;
251 if (!function_exists ("ary_sort"))
252 include ("../functions/array.php");
254 $dm = sqimap_get_delimiter ($imap_stream);
256 fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
257 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
260 for ($i = 0; $i < count($read_ary); $i++
) {
261 if (substr ($read_ary[$i], 0, 4) != "a001") {
262 $boxes[$g]["raw"] = $read_ary[$i];
264 $mailbox = find_mailbox_name($read_ary[$i]);
265 $dm_count = countCharInString($mailbox, $dm);
266 if (substr($mailbox, -1) == $dm)
269 for ($j = 0; $j < $dm_count; $j++
)
270 $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
271 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
273 $boxes[$g]["unformatted-dm"] = $mailbox;
274 if (substr($mailbox, -1) == $dm)
275 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
276 $boxes[$g]["unformatted"] = $mailbox;
277 $boxes[$g]["id"] = $g;
279 /** Now lets get the flags for this mailbox **/
280 fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
281 $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
283 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+
1);
284 $flags = substr($flags, 0, strpos($flags, ")"));
285 $flags = str_replace("\\", "", $flags);
286 $flags = trim(strtolower($flags));
288 $boxes[$g]["flags"] = explode(" ", $flags);
294 $boxes = ary_sort ($boxes, "unformatted", 1);