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);
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", false, $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
115 ** The four parts are:
117 ** raw - Raw LIST/LSUB response from the IMAP server
118 ** formatted - nicely formatted folder name
119 ** unformatted - unformatted, but with delimiter at end removed
120 ** unformatted-dm - folder name as it appears in raw response
121 ** unformatted-disp - unformatted without $folder_prefix
123 ******************************************************************************/
124 function sqimap_mailbox_parse ($line, $line_lsub, $dm) {
125 global $folder_prefix;
127 // Process each folder line
128 for ($g=0; $g < count($line); $g++
) {
130 // Store the raw IMAP reply
131 $boxes[$g]["raw"] = $line[$g];
133 // Count number of delimiters ($dm) in folder name
134 $mailbox = trim($line_lsub[$g]);
135 $dm_count = countCharInString($mailbox, $dm);
136 if (substr($mailbox, -1) == $dm)
137 $dm_count--; // If name ends in delimiter - decrement count by one
139 // Format folder name, but only if it's a INBOX.* or have
141 $boxesbyname[$mailbox] = $g;
142 $parentfolder = readMailboxParent($mailbox, $dm);
143 if((eregi("^inbox".quotemeta($dm), $mailbox)) ||
144 (ereg("^".$folder_prefix, $mailbox)) ||
145 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
146 $indent = $dm_count - (countCharInString($folder_prefix, $dm));
148 $boxes[$g]["formatted"] = str_repeat(" ", $indent);
150 $boxes[$g]["formatted"] = '';
151 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
153 $boxes[$g]["formatted"] = $mailbox;
156 $boxes[$g]["unformatted-dm"] = $mailbox;
157 if (substr($mailbox, -1) == $dm)
158 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
159 $boxes[$g]["unformatted"] = $mailbox;
160 $boxes[$g]["unformatted-disp"] = ereg_replace("^" . $folder_prefix, "", $mailbox);
161 $boxes[$g]["id"] = $g;
163 ereg("\(([^)]*)\)",$line[$g],$regs);
164 $flags = trim(strtolower(str_replace("\\", "",$regs[1])));
166 $boxes[$g]["flags"] = explode(" ", $flags);
173 /* patch from dave_michmerhuizen@yahoo.com
174 * allows case insensativity when sorting folders
176 function _icmp ($a, $b) {
177 return strcasecmp($a, $b);
180 /******************************************************************************
181 ** Returns sorted mailbox lists in several different ways.
182 ** See comment on sqimap_mailbox_parse() for info about the returned array.
183 ******************************************************************************/
184 function sqimap_mailbox_list ($imap_stream) {
185 global $load_prefs_php, $prefs_php, $config_php;
186 global $data_dir, $username, $list_special_folders_first;
187 global $trash_folder, $sent_folder;
188 global $move_to_trash, $move_to_sent;
190 $inbox_in_list = false;
191 $inbox_subscribed = false;
193 if (!isset($load_prefs_php)) include "../src/load_prefs.php";
194 else global $folder_prefix;
195 if (!function_exists ("ary_sort")) include "../functions/array.php";
197 $dm = sqimap_get_delimiter ($imap_stream);
200 $inbox_subscribed = false;
201 fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n");
202 $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
204 for ($i=0;$i < count($lsub_ary); $i++
) {
205 $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
206 if ($sorted_lsub_ary[$i] == "INBOX")
207 $inbox_subscribed = true;
210 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
211 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
212 $new_ary[] = $sorted_lsub_ary[$i];
215 $sorted_lsub_ary = $new_ary;
216 if (isset($sorted_lsub_ary)) {
217 usort($sorted_lsub_ary, "_icmp");
218 //sort($sorted_lsub_ary);
222 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
223 if (substr($sorted_lsub_ary[$i], -1) == $dm)
224 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
226 $mbx = $sorted_lsub_ary[$i];
228 fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n");
229 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
230 $sorted_list_ary[$i] = $read[0];
231 if (find_mailbox_name($sorted_list_ary[$i]) == "INBOX")
232 $inbox_in_list = true;
235 /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/
236 if ($inbox_subscribed == false ||
$inbox_in_list == false) {
237 fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
238 $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
240 $pos = count($sorted_list_ary);
241 $sorted_list_ary[$pos] = $inbox_ary[0];
243 $pos = count($sorted_lsub_ary);
244 $sorted_lsub_ary[$pos] = find_mailbox_name($inbox_ary[0]);
247 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm);
250 /** Now, lets sort for special folders **/
255 for ($i = 0; $i < count($boxes); $i++
) {
256 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
257 $boxesnew[] = $boxes[$i];
258 $boxes[$i]["used"] = true;
263 if ($list_special_folders_first == true) {
265 // Then list special folders and their subfolders
266 for ($i = 0 ; $i <= count($boxes) ; $i++
) {
267 if((eregi("^".$trash_folder.'$', $boxes[$i]["unformatted"]) ||
268 eregi("^".$trash_folder.quotemeta($dm), $boxes[$i]["unformatted"]) ) &&
270 $boxesnew[] = $boxes[$i];
271 $boxes[$i]["used"] = true;
273 else if((eregi("^".$sent_folder.'$', $boxes[$i]["unformatted"]) ||
274 eregi("^".$sent_folder.quotemeta($dm), $boxes[$i]["unformatted"]) ) &&
276 $boxesnew[] = $boxes[$i];
277 $boxes[$i]["used"] = true;
281 // Put INBOX.* folders ahead of the rest
282 for ($i = 0; $i <= count($boxes); $i++
) {
283 if (eregi("^inbox\.", $boxes[$i]["unformatted"]) &&
284 ($boxes[$i]["used"] == false)) {
285 $boxesnew[] = $boxes[$i];
286 $boxes[$i]["used"] = true;
292 // Rest of the folders
293 for ($i = 0; $i < count($boxes); $i++
) {
294 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
295 ($boxes[$i]["used"] == false)) {
296 $boxesnew[] = $boxes[$i];
297 $boxes[$i]["used"] = true;
304 /******************************************************************************
305 ** Returns a list of all folders, subscribed or not
306 ******************************************************************************/
307 function sqimap_mailbox_list_all ($imap_stream) {
308 global $list_special_folders_first, $folder_prefix;
310 if (!function_exists ("ary_sort"))
311 include ("../functions/array.php");
313 $dm = sqimap_get_delimiter ($imap_stream);
315 fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
316 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
320 for ($i = 0; $i < count($read_ary); $i++
) {
321 if (substr ($read_ary[$i], 0, 4) != "a001") {
323 // Store the raw IMAP reply
324 $boxes[$g]["raw"] = $read_ary[$i];
326 // Count number of delimiters ($dm) in folder name
327 $mailbox = find_mailbox_name($read_ary[$i]);
328 $dm_count = countCharInString($mailbox, $dm);
329 if (substr($mailbox, -1) == $dm)
330 $dm_count--; // If name ends in delimiter - decrement count by one
332 // Format folder name, but only if it's a INBOX.* or have
334 $boxesbyname[$mailbox] = $g;
335 $parentfolder = readMailboxParent($mailbox, $dm);
336 if((eregi("^inbox".quotemeta($dm), $mailbox)) ||
337 (ereg("^".$folder_prefix, $mailbox)) ||
338 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
340 $boxes[$g]["formatted"] = str_repeat(" ", $dm_count);
342 $boxes[$g]["formatted"] = '';
343 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
345 $boxes[$g]["formatted"] = $mailbox;
348 $boxes[$g]["unformatted-dm"] = $mailbox;
349 if (substr($mailbox, -1) == $dm)
350 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
351 $boxes[$g]["unformatted"] = $mailbox;
352 $boxes[$g]["unformatted-disp"] = ereg_replace("^" . $folder_prefix, "", $mailbox);
353 $boxes[$g]["id"] = $g;
355 /** Now lets get the flags for this mailbox **/
356 fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
357 $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
359 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+
1);
360 $flags = substr($flags, 0, strpos($flags, ")"));
361 $flags = str_replace("\\", "", $flags);
362 $flags = trim(strtolower($flags));
364 $boxes[$g]["flags"] = explode(" ", $flags);
369 if(is_array($boxes)) {
370 $boxes = ary_sort ($boxes, "unformatted", 1);