5 ** This impliments all functions that manipulate mailboxes
8 /******************************************************************************
10 ******************************************************************************/
11 function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = true) {
12 sqimap_mailbox_select ($imap_stream, $mailbox);
13 fputs ($imap_stream, "a001 EXPUNGE\r\n");
14 $read = sqimap_read_data($imap_stream, "a001", $handle_errors, $response, $message);
18 /******************************************************************************
19 ** Checks whether or not the specified mailbox exists
20 ******************************************************************************/
21 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
22 fputs ($imap_stream, "a001 LIST \"\" \"$mailbox\"\r\n");
23 $mbx = sqimap_read_data($imap_stream, "a001", true, $response, $message);
25 return !!(ereg ("$mailbox", $mbx[0])); // To force into true/false
29 /******************************************************************************
31 ******************************************************************************/
32 function sqimap_mailbox_select ($imap_stream, $mailbox, $hide=true, $recent=false) {
35 fputs ($imap_stream, "a001 SELECT \"$mailbox\"\r\n");
36 $read = sqimap_read_data($imap_stream, "a001", true, $response, $message);
38 for ($i=0; $i<count($read); $i++
) {
39 if (strpos(strtolower($read[$i]), "recent")) {
40 $r = explode(" ", $read[$i]);
46 fputs ($imap_stream, "a001 EXPUNGE\r\n");
47 $tmp = sqimap_read_data($imap_stream, "a001", false, $a, $b);
53 /******************************************************************************
55 ******************************************************************************/
56 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
57 if (strtolower($type) == "noselect") {
58 $dm = sqimap_get_delimiter($imap_stream);
59 $mailbox = $mailbox.$dm;
61 fputs ($imap_stream, "a001 CREATE \"$mailbox\"\r\n");
62 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
64 sqimap_subscribe ($imap_stream, $mailbox);
69 /******************************************************************************
70 ** Subscribes to an existing folder
71 ******************************************************************************/
72 function sqimap_subscribe ($imap_stream, $mailbox) {
73 fputs ($imap_stream, "a001 SUBSCRIBE \"$mailbox\"\r\n");
74 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
80 /******************************************************************************
81 ** Unsubscribes to an existing folder
82 ******************************************************************************/
83 function sqimap_unsubscribe ($imap_stream, $mailbox) {
84 global $imap_server_type;
86 fputs ($imap_stream, "a001 UNSUBSCRIBE \"$mailbox\"\r\n");
87 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
93 /******************************************************************************
94 ** This function simply deletes the given folder
95 ******************************************************************************/
96 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
97 fputs ($imap_stream, "a001 DELETE \"$mailbox\"\r\n");
98 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
99 sqimap_unsubscribe ($imap_stream, $mailbox);
103 /******************************************************************************
104 ** Formats a mailbox into 4 parts for the $boxes array
106 ** The four parts are:
108 ** raw - Raw LIST/LSUB response from the IMAP server
109 ** formatted - nicely formatted folder name
110 ** unformatted - unformatted, but with delimiter at end removed
111 ** unformatted-dm - folder name as it appears in raw response
112 ** unformatted-disp - unformatted without $folder_prefix
114 ******************************************************************************/
115 function sqimap_mailbox_parse ($line, $line_lsub, $dm) {
116 global $folder_prefix;
118 // Process each folder line
119 for ($g=0; $g < count($line); $g++
) {
121 // Store the raw IMAP reply
122 $boxes[$g]["raw"] = $line[$g];
124 // Count number of delimiters ($dm) in folder name
125 $mailbox = trim($line_lsub[$g]);
126 $dm_count = countCharInString($mailbox, $dm);
127 if (substr($mailbox, -1) == $dm)
128 $dm_count--; // If name ends in delimiter - decrement count by one
130 // Format folder name, but only if it's a INBOX.* or have
132 $boxesbyname[$mailbox] = $g;
133 $parentfolder = readMailboxParent($mailbox, $dm);
134 if((eregi("^inbox".quotemeta($dm), $mailbox)) ||
135 (ereg("^".$folder_prefix, $mailbox)) ||
136 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
137 $indent = $dm_count - (countCharInString($folder_prefix, $dm));
139 $boxes[$g]["formatted"] = str_repeat(" ", $indent);
141 $boxes[$g]["formatted"] = '';
142 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
144 $boxes[$g]["formatted"] = $mailbox;
147 $boxes[$g]["unformatted-dm"] = $mailbox;
148 if (substr($mailbox, -1) == $dm)
149 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
150 $boxes[$g]["unformatted"] = $mailbox;
151 $boxes[$g]["unformatted-disp"] = ereg_replace("^" . $folder_prefix, "", $mailbox);
152 $boxes[$g]["id"] = $g;
154 ereg("\(([^)]*)\)",$line[$g],$regs);
155 $flags = trim(strtolower(str_replace("\\", "",$regs[1])));
157 $boxes[$g]["flags"] = explode(" ", $flags);
164 /* patch from dave_michmerhuizen@yahoo.com
165 * allows case insensativity when sorting folders
167 function _icmp ($a, $b) {
168 return strcasecmp($a, $b);
171 /******************************************************************************
172 ** Returns sorted mailbox lists in several different ways.
173 ** See comment on sqimap_mailbox_parse() for info about the returned array.
174 ******************************************************************************/
175 function sqimap_mailbox_list ($imap_stream) {
176 global $load_prefs_php, $prefs_php, $config_php;
177 global $data_dir, $username, $list_special_folders_first;
178 global $trash_folder, $sent_folder;
179 global $move_to_trash, $move_to_sent;
181 $inbox_in_list = false;
182 $inbox_subscribed = false;
184 if (!isset($load_prefs_php)) include "../src/load_prefs.php";
185 else global $folder_prefix;
186 if (!function_exists ("ary_sort")) include "../functions/array.php";
188 $dm = sqimap_get_delimiter ($imap_stream);
191 $inbox_subscribed = false;
192 fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n");
193 $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
195 /** OS: we don't want to parse last element of array, 'cause it is OK command, so we unset it **/
196 unset($lsub_ary[count($lsub_ary)-1]);
198 for ($i=0;$i < count($lsub_ary); $i++
) {
199 $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
200 if ($sorted_lsub_ary[$i] == "INBOX")
201 $inbox_subscribed = true;
204 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
205 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
206 $new_ary[] = $sorted_lsub_ary[$i];
209 $sorted_lsub_ary = $new_ary;
210 if (isset($sorted_lsub_ary)) {
211 usort($sorted_lsub_ary, "_icmp");
212 //sort($sorted_lsub_ary);
216 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
217 if (substr($sorted_lsub_ary[$i], -1) == $dm)
218 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
220 $mbx = $sorted_lsub_ary[$i];
222 fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n");
223 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
224 $sorted_list_ary[$i] = $read[0];
225 if (find_mailbox_name($sorted_list_ary[$i]) == "INBOX")
226 $inbox_in_list = true;
229 /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/
230 if ($inbox_subscribed == false ||
$inbox_in_list == false) {
231 fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
232 $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
234 $pos = count($sorted_list_ary);
235 $sorted_list_ary[$pos] = $inbox_ary[0];
237 $pos = count($sorted_lsub_ary);
238 $sorted_lsub_ary[$pos] = find_mailbox_name($inbox_ary[0]);
241 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm);
244 /** Now, lets sort for special folders **/
249 for ($i = 0; $i < count($boxes); $i++
) {
250 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
251 $boxesnew[] = $boxes[$i];
252 $boxes[$i]["used"] = true;
257 if ($list_special_folders_first == true) {
259 // Then list special folders and their subfolders
260 for ($i = 0 ; $i <= count($boxes) ; $i++
) {
261 if((eregi("^".$trash_folder.'$', $boxes[$i]["unformatted"]) ||
262 eregi("^".$trash_folder.quotemeta($dm), $boxes[$i]["unformatted"]) ) &&
264 $boxesnew[] = $boxes[$i];
265 $boxes[$i]["used"] = true;
267 else if((eregi("^".$sent_folder.'$', $boxes[$i]["unformatted"]) ||
268 eregi("^".$sent_folder.quotemeta($dm), $boxes[$i]["unformatted"]) ) &&
270 $boxesnew[] = $boxes[$i];
271 $boxes[$i]["used"] = true;
275 // Put INBOX.* folders ahead of the rest
276 for ($i = 0; $i <= count($boxes); $i++
) {
277 if (eregi("^inbox\.", $boxes[$i]["unformatted"]) &&
278 ($boxes[$i]["used"] == false)) {
279 $boxesnew[] = $boxes[$i];
280 $boxes[$i]["used"] = true;
286 // Rest of the folders
287 for ($i = 0; $i < count($boxes); $i++
) {
288 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
289 ($boxes[$i]["used"] == false)) {
290 $boxesnew[] = $boxes[$i];
291 $boxes[$i]["used"] = true;
298 /******************************************************************************
299 ** Returns a list of all folders, subscribed or not
300 ******************************************************************************/
301 function sqimap_mailbox_list_all ($imap_stream) {
302 global $list_special_folders_first, $folder_prefix;
304 if (!function_exists ("ary_sort"))
305 include ("../functions/array.php");
307 $dm = sqimap_get_delimiter ($imap_stream);
309 fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
310 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
314 for ($i = 0; $i < count($read_ary); $i++
) {
315 if (substr ($read_ary[$i], 0, 4) != "a001") {
317 // Store the raw IMAP reply
318 $boxes[$g]["raw"] = $read_ary[$i];
320 // Count number of delimiters ($dm) in folder name
321 $mailbox = find_mailbox_name($read_ary[$i]);
322 $dm_count = countCharInString($mailbox, $dm);
323 if (substr($mailbox, -1) == $dm)
324 $dm_count--; // If name ends in delimiter - decrement count by one
326 // Format folder name, but only if it's a INBOX.* or have
328 $boxesbyname[$mailbox] = $g;
329 $parentfolder = readMailboxParent($mailbox, $dm);
330 if((eregi("^inbox".quotemeta($dm), $mailbox)) ||
331 (ereg("^".$folder_prefix, $mailbox)) ||
332 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
334 $boxes[$g]["formatted"] = str_repeat(" ", $dm_count);
336 $boxes[$g]["formatted"] = '';
337 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
339 $boxes[$g]["formatted"] = $mailbox;
342 $boxes[$g]["unformatted-dm"] = $mailbox;
343 if (substr($mailbox, -1) == $dm)
344 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
345 $boxes[$g]["unformatted"] = $mailbox;
346 $boxes[$g]["unformatted-disp"] = ereg_replace("^" . $folder_prefix, "", $mailbox);
347 $boxes[$g]["id"] = $g;
349 /** Now lets get the flags for this mailbox **/
350 fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
351 $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
353 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+
1);
354 $flags = substr($flags, 0, strpos($flags, ")"));
355 $flags = str_replace("\\", "", $flags);
356 $flags = trim(strtolower($flags));
358 $boxes[$g]["flags"] = explode(" ", $flags);
363 if(is_array($boxes)) {
364 $boxes = ary_sort ($boxes, "unformatted", 1);