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) {
48 fputs ($imap_stream, "a001 SELECT \"$mailbox\"\r\n");
49 $read = sqimap_read_data($imap_stream, "a001", true, $response, $message);
51 for ($i=0; $i<count($read); $i++
) {
52 if (strpos(strtolower($read[$i]), "recent")) {
53 $r = explode(" ", $read[$i]);
59 fputs ($imap_stream, "a001 EXPUNGE\r\n");
60 $tmp = sqimap_read_data($imap_stream, "a001", $a, $b, true);
66 /******************************************************************************
68 ******************************************************************************/
69 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
70 if (strtolower($type) == "noselect") {
71 $dm = sqimap_get_delimiter($imap_stream);
72 $mailbox = $mailbox.$dm;
74 fputs ($imap_stream, "a001 CREATE \"$mailbox\"\r\n");
75 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
77 sqimap_subscribe ($imap_stream, $mailbox);
82 /******************************************************************************
83 ** Subscribes to an existing folder
84 ******************************************************************************/
85 function sqimap_subscribe ($imap_stream, $mailbox) {
86 fputs ($imap_stream, "a001 SUBSCRIBE \"$mailbox\"\r\n");
87 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
93 /******************************************************************************
94 ** Unsubscribes to an existing folder
95 ******************************************************************************/
96 function sqimap_unsubscribe ($imap_stream, $mailbox) {
97 global $imap_server_type;
99 fputs ($imap_stream, "a001 UNSUBSCRIBE \"$mailbox\"\r\n");
100 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
106 /******************************************************************************
107 ** This function simply deletes the given folder
108 ******************************************************************************/
109 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
110 fputs ($imap_stream, "a001 DELETE \"$mailbox\"\r\n");
111 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
112 sqimap_unsubscribe ($imap_stream, $mailbox);
116 /******************************************************************************
117 ** Formats a mailbox into 4 parts for the $boxes array
118 ******************************************************************************/
119 function sqimap_mailbox_parse ($line, $line_lsub, $dm) {
120 global $folder_prefix;
121 for ($g=0; $g < count($line); $g++
) {
122 $boxes[$g]["raw"] = $line[$g];
124 $mailbox = $line_lsub[$g];
125 $dm_count = countCharInString($mailbox, $dm);
126 if (substr($mailbox, -1) == $dm)
129 for ($j = 0; $j < $dm_count - (countCharInString($folder_prefix, $dm)); $j++
)
130 $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
131 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
133 $boxes[$g]["unformatted-dm"] = $mailbox;
134 if (substr($mailbox, -1) == $dm)
135 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
136 $boxes[$g]["unformatted"] = $mailbox;
137 $boxes[$g]["id"] = $g;
139 $flags = substr($line[$g], strpos($line[$g], "(")+
1);
140 $flags = substr($flags, 0, strpos($flags, ")"));
141 $flags = str_replace("\\", "", $flags);
142 $flags = trim(strtolower($flags));
144 $boxes[$g]["flags"] = explode(" ", $flags);
150 /******************************************************************************
151 ** Returns sorted mailbox lists in several different ways.
152 ** The array returned looks like this:
153 ******************************************************************************/
154 function sqimap_mailbox_list ($imap_stream) {
155 global $load_prefs_php, $prefs_php, $config_php, $data_dir, $username, $list_special_folders_first;
156 global $trash_folder, $sent_folder;
157 global $move_to_trash, $move_to_sent;
159 $inbox_in_list = false;
160 $inbox_subscribed = false;
162 if (!isset($load_prefs_php)) include "../src/load_prefs.php";
163 else global $folder_prefix;
164 if (!function_exists ("ary_sort")) include "../functions/array.php";
166 $dm = sqimap_get_delimiter ($imap_stream);
169 $inbox_subscribed = false;
170 fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n");
171 $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
173 for ($i=0;$i < count($lsub_ary); $i++
) {
174 $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
175 if ($sorted_lsub_ary[$i] == "INBOX")
176 $inbox_subscribed = true;
179 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
180 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
181 $new_ary[] = $sorted_lsub_ary[$i];
184 $sorted_lsub_ary = $new_ary;
185 if (isset($sorted_lsub_ary)) {
186 sort($sorted_lsub_ary);
190 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
191 if (substr($sorted_lsub_ary[$i], -1) == $dm)
192 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
194 $mbx = $sorted_lsub_ary[$i];
196 fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n");
197 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
198 $sorted_list_ary[$i] = $read[0];
199 if (find_mailbox_name($sorted_list_ary[$i]) == "INBOX")
200 $inbox_in_list = true;
203 /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/
204 if ($inbox_subscribed == false ||
$inbox_in_list == false) {
205 fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
206 $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
208 $pos = count($sorted_list_ary);
209 $sorted_list_ary[$pos] = $inbox_ary[0];
211 $pos = count($sorted_lsub_ary);
212 $sorted_lsub_ary[$pos] = find_mailbox_name($inbox_ary[0]);
215 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm);
217 /** Now, lets sort for special folders **/
218 for ($i = 0; $i < count($boxes); $i++
) {
219 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
220 $boxesnew[0] = $boxes[$i];
221 $boxes[$i]["used"] = true;
226 if ($list_special_folders_first == true) {
227 for ($i = count($boxes)-1; $i >= 0 ; $i--) {
228 if (($boxes[$i]["unformatted"] == $trash_folder) && ($move_to_trash)) {
229 $pos = count($boxesnew);
230 $boxesnew[$pos] = $boxes[$i];
231 $boxes[$i]["used"] = true;
234 else if (($boxes[$i]["unformatted"] == $sent_folder) && ($move_to_sent)) {
235 $pos = count($boxesnew);
236 $boxesnew[$pos] = $boxes[$i];
237 $boxes[$i]["used"] = true;
241 if (($sent_found && $trash_found) ||
($sent_found && !$move_to_trash) ||
($trash_found && !$move_to_sent) ||
(!$move_to_sent && !$move_to_trash))
246 for ($i = 0; $i < count($boxes); $i++
) {
247 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
248 ($boxes[$i]["used"] == false)) {
249 $pos = count($boxesnew);
250 $boxesnew[$pos] = $boxes[$i];
251 $boxes[$i]["used"] = true;
258 /******************************************************************************
259 ** Returns a list of all folders, subscribed or not
260 ******************************************************************************/
261 function sqimap_mailbox_list_all ($imap_stream) {
262 global $list_special_folders_first, $folder_prefix;
264 if (!function_exists ("ary_sort"))
265 include ("../functions/array.php");
267 $dm = sqimap_get_delimiter ($imap_stream);
269 fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
270 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
273 for ($i = 0; $i < count($read_ary); $i++
) {
274 if (substr ($read_ary[$i], 0, 4) != "a001") {
275 $boxes[$g]["raw"] = $read_ary[$i];
277 $mailbox = find_mailbox_name($read_ary[$i]);
278 $dm_count = countCharInString($mailbox, $dm);
279 if (substr($mailbox, -1) == $dm)
282 for ($j = 0; $j < $dm_count; $j++
)
283 $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
284 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
286 $boxes[$g]["unformatted-dm"] = $mailbox;
287 if (substr($mailbox, -1) == $dm)
288 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
289 $boxes[$g]["unformatted"] = $mailbox;
290 $boxes[$g]["id"] = $g;
292 /** Now lets get the flags for this mailbox **/
293 fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
294 $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
296 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+
1);
297 $flags = substr($flags, 0, strpos($flags, ")"));
298 $flags = str_replace("\\", "", $flags);
299 $flags = trim(strtolower($flags));
301 $boxes[$g]["flags"] = explode(" ", $flags);
307 $boxes = ary_sort ($boxes, "unformatted", 1);