5 ** This impliments all functions that manipulate mailboxes
10 /******************************************************************************
12 ******************************************************************************/
13 function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = true) {
14 sqimap_mailbox_select ($imap_stream, $mailbox);
15 fputs ($imap_stream, "a001 EXPUNGE\r\n");
16 $read = sqimap_read_data($imap_stream, "a001", $handle_errors, $response, $message);
20 /******************************************************************************
21 ** Checks whether or not the specified mailbox exists
22 ******************************************************************************/
23 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
24 if (! isset($mailbox))
26 fputs ($imap_stream, "a001 LIST \"\" \"$mailbox\"\r\n");
27 $mbx = sqimap_read_data($imap_stream, "a001", true, $response, $message);
28 return isset($mbx[0]);
31 /******************************************************************************
33 ******************************************************************************/
34 function sqimap_mailbox_select ($imap_stream, $mailbox, $hide=true, $recent=false) {
37 fputs ($imap_stream, "a001 SELECT \"$mailbox\"\r\n");
38 $read = sqimap_read_data($imap_stream, "a001", true, $response, $message);
40 for ($i=0; $i<count($read); $i++
) {
41 if (strpos(strtolower($read[$i]), "recent")) {
42 $r = explode(" ", $read[$i]);
48 fputs ($imap_stream, "a001 EXPUNGE\r\n");
49 $tmp = sqimap_read_data($imap_stream, "a001", false, $a, $b);
55 /******************************************************************************
57 ******************************************************************************/
58 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
59 if (strtolower($type) == "noselect") {
60 $dm = sqimap_get_delimiter($imap_stream);
61 $mailbox = $mailbox.$dm;
63 fputs ($imap_stream, "a001 CREATE \"$mailbox\"\r\n");
64 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
66 sqimap_subscribe ($imap_stream, $mailbox);
71 /******************************************************************************
72 ** Subscribes to an existing folder
73 ******************************************************************************/
74 function sqimap_subscribe ($imap_stream, $mailbox) {
75 fputs ($imap_stream, "a001 SUBSCRIBE \"$mailbox\"\r\n");
76 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
82 /******************************************************************************
83 ** Unsubscribes to an existing folder
84 ******************************************************************************/
85 function sqimap_unsubscribe ($imap_stream, $mailbox) {
86 global $imap_server_type;
88 fputs ($imap_stream, "a001 UNSUBSCRIBE \"$mailbox\"\r\n");
89 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
95 /******************************************************************************
96 ** This function simply deletes the given folder
97 ******************************************************************************/
98 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
99 fputs ($imap_stream, "a001 DELETE \"$mailbox\"\r\n");
100 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
101 sqimap_unsubscribe ($imap_stream, $mailbox);
105 /******************************************************************************
106 ** Formats a mailbox into 4 parts for the $boxes array
108 ** The four parts are:
110 ** raw - Raw LIST/LSUB response from the IMAP server
111 ** formatted - nicely formatted folder name
112 ** unformatted - unformatted, but with delimiter at end removed
113 ** unformatted-dm - folder name as it appears in raw response
114 ** unformatted-disp - unformatted without $folder_prefix
116 ******************************************************************************/
117 function sqimap_mailbox_parse ($line, $line_lsub, $dm) {
118 global $folder_prefix;
120 // Process each folder line
121 for ($g=0; $g < count($line); $g++
) {
123 // Store the raw IMAP reply
124 if (isset($line[$g]))
125 $boxes[$g]["raw"] = $line[$g];
127 $boxes[$g]["raw"] = "";
130 // Count number of delimiters ($dm) in folder name
131 $mailbox = trim($line_lsub[$g]);
132 $dm_count = countCharInString($mailbox, $dm);
133 if (substr($mailbox, -1) == $dm)
134 $dm_count--; // If name ends in delimiter - decrement count by one
136 // Format folder name, but only if it's a INBOX.* or have
138 $boxesbyname[$mailbox] = $g;
139 $parentfolder = readMailboxParent($mailbox, $dm);
140 if((eregi("^inbox".quotemeta($dm), $mailbox)) ||
141 (ereg("^".$folder_prefix, $mailbox)) ||
142 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
143 $indent = $dm_count - (countCharInString($folder_prefix, $dm));
145 $boxes[$g]["formatted"] = str_repeat(" ", $indent);
147 $boxes[$g]["formatted"] = '';
148 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
150 $boxes[$g]["formatted"] = $mailbox;
153 $boxes[$g]['unformatted-dm'] = $mailbox;
154 if (substr($mailbox, -1) == $dm)
155 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
156 $boxes[$g]['unformatted'] = $mailbox;
157 $boxes[$g]['unformatted-disp'] = ereg_replace('^' . $folder_prefix, '', $mailbox);
158 $boxes[$g]['id'] = $g;
160 if (isset($line[$g]))
161 ereg("\(([^)]*)\)",$line[$g],$regs);
162 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
164 $boxes[$g]['flags'] = explode(' ', $flags);
167 $boxes[$g]['flags'] = array();
173 /* Apparently you must call a user function with usort instead
174 * of calling a built-in directly. Stupid.
175 * Patch from dave_michmerhuizen@yahoo.com
176 * Allows case insensitivity when sorting folders
178 function user_strcasecmp($a, $b)
180 return strcasecmp($a, $b);
184 /******************************************************************************
185 ** Returns sorted mailbox lists in several different ways.
186 ** See comment on sqimap_mailbox_parse() for info about the returned array.
187 ******************************************************************************/
188 function sqimap_mailbox_list ($imap_stream) {
189 global $load_prefs_php, $prefs_php, $config_php;
190 global $data_dir, $username, $list_special_folders_first;
191 global $trash_folder, $sent_folder;
192 global $move_to_trash, $move_to_sent;
194 $inbox_in_list = false;
195 $inbox_subscribed = false;
197 if (!isset($load_prefs_php)) include "../src/load_prefs.php";
198 else global $folder_prefix;
199 if (!function_exists ("ary_sort")) include "../functions/array.php";
201 $dm = sqimap_get_delimiter ($imap_stream);
204 $inbox_subscribed = false;
205 fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n");
206 $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
208 /** OS: we don't want to parse last element of array, 'cause it is OK command, so we unset it **/
209 /** LUKE: This introduced errors.. do a check first **/
210 if (substr($lsub_ary[count($lsub_ary)-1], 0, 4) == "* OK") {
211 unset($lsub_ary[count($lsub_ary)-1]);
214 for ($i=0;$i < count($lsub_ary); $i++
) {
215 $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
216 if ($sorted_lsub_ary[$i] == "INBOX")
217 $inbox_subscribed = true;
220 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
221 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
222 $new_ary[] = $sorted_lsub_ary[$i];
225 $sorted_lsub_ary = $new_ary;
226 if (isset($sorted_lsub_ary)) {
227 usort($sorted_lsub_ary, "user_strcasecmp");
228 //sort($sorted_lsub_ary);
232 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
233 if (substr($sorted_lsub_ary[$i], -1) == $dm)
234 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
236 $mbx = $sorted_lsub_ary[$i];
238 fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n");
239 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
240 if (isset($sorted_list_ary[$i]))
241 $sorted_list_ary[$i] = "";
243 $sorted_list_ary[$i] = $read[0];
245 $sorget_list_ary[$i] = "";
246 if (isset($sorted_list_ary[$i]) && find_mailbox_name($sorted_list_ary[$i]) == "INBOX")
247 $inbox_in_list = true;
250 /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/
251 if ($inbox_subscribed == false ||
$inbox_in_list == false) {
252 fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
253 $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
255 $pos = count($sorted_list_ary);
256 $sorted_list_ary[$pos] = $inbox_ary[0];
258 $pos = count($sorted_lsub_ary);
259 $sorted_lsub_ary[$pos] = find_mailbox_name($inbox_ary[0]);
262 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm);
265 /** Now, lets sort for special folders **/
270 for ($i = 0; $i < count($boxes); $i++
) {
271 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
272 $boxesnew[] = $boxes[$i];
278 if ($list_special_folders_first == true) {
280 // Then list special folders and their subfolders
281 for ($i = 0 ; $i < count($boxes) ; $i++
) {
282 if ($move_to_trash &&
283 eregi('^' . quotemeta($trash_folder) . '(' .
284 quotemeta($dm) . '.*)?$', $boxes[$i]["unformatted"])) {
285 $boxesnew[] = $boxes[$i];
288 elseif ($move_to_sent &&
289 eregi('^' . quotemeta($sent_folder) . '(' .
290 quotemeta($dm) . '.*)?$', $boxes[$i]["unformatted"])) {
291 $boxesnew[] = $boxes[$i];
296 // Put INBOX.* folders ahead of the rest
297 for ($i = 0; $i < count($boxes); $i++
) {
298 if (eregi('^inbox\\.', $boxes[$i]["unformatted"]) &&
299 (!isset($used[$i]) ||
$used[$i] == false)) {
300 $boxesnew[] = $boxes[$i];
307 // Rest of the folders
308 for ($i = 0; $i < count($boxes); $i++
) {
309 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
310 (!isset($used[$i]) ||
$used[$i] == false)) {
311 $boxesnew[] = $boxes[$i];
319 /******************************************************************************
320 ** Returns a list of all folders, subscribed or not
321 ******************************************************************************/
322 function sqimap_mailbox_list_all ($imap_stream) {
323 global $list_special_folders_first, $folder_prefix;
325 if (!function_exists ("ary_sort"))
326 include ("../functions/array.php");
328 $dm = sqimap_get_delimiter ($imap_stream);
330 fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
331 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
335 for ($i = 0; $i < count($read_ary); $i++
) {
336 if (substr ($read_ary[$i], 0, 4) != "a001") {
338 // Store the raw IMAP reply
339 $boxes[$g]["raw"] = $read_ary[$i];
341 // Count number of delimiters ($dm) in folder name
342 $mailbox = find_mailbox_name($read_ary[$i]);
343 $dm_count = countCharInString($mailbox, $dm);
344 if (substr($mailbox, -1) == $dm)
345 $dm_count--; // If name ends in delimiter - decrement count by one
347 // Format folder name, but only if it's a INBOX.* or have
349 $boxesbyname[$mailbox] = $g;
350 $parentfolder = readMailboxParent($mailbox, $dm);
351 if((eregi('^inbox'.quotemeta($dm), $mailbox)) ||
352 (ereg('^'.$folder_prefix, $mailbox)) ||
353 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
355 $boxes[$g]["formatted"] = str_repeat(" ", $dm_count);
357 $boxes[$g]["formatted"] = '';
358 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
360 $boxes[$g]["formatted"] = $mailbox;
363 $boxes[$g]["unformatted-dm"] = $mailbox;
364 if (substr($mailbox, -1) == $dm)
365 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
366 $boxes[$g]["unformatted"] = $mailbox;
367 $boxes[$g]["unformatted-disp"] = ereg_replace('^' . $folder_prefix, '', $mailbox);
368 $boxes[$g]["id"] = $g;
370 /** Now lets get the flags for this mailbox **/
371 fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
372 $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
374 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+
1);
375 $flags = substr($flags, 0, strpos($flags, ")"));
376 $flags = str_replace('\\', '', $flags);
377 $flags = trim(strtolower($flags));
379 $boxes[$g]['flags'] = explode(" ", $flags);
383 $boxes[$g]['flags'] = array();
388 if(is_array($boxes)) {
389 $boxes = ary_sort ($boxes, "unformatted", 1);