5 ** This impliments all functions that manipulate mailboxes
10 if (defined ('imap_mailbox_php'))
12 define ('imap_mailbox_php', true);
14 /******************************************************************************
16 ******************************************************************************/
17 function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = true) {
18 sqimap_mailbox_select ($imap_stream, $mailbox);
19 fputs ($imap_stream, "a001 EXPUNGE\r\n");
20 $read = sqimap_read_data($imap_stream, "a001", $handle_errors, $response, $message);
24 /******************************************************************************
25 ** Checks whether or not the specified mailbox exists
26 ******************************************************************************/
27 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
28 if (! isset($mailbox))
30 fputs ($imap_stream, "a001 LIST \"\" \"$mailbox\"\r\n");
31 $mbx = sqimap_read_data($imap_stream, "a001", true, $response, $message);
32 return isset($mbx[0]);
35 /******************************************************************************
37 ******************************************************************************/
38 function sqimap_mailbox_select ($imap_stream, $mailbox, $hide=true, $recent=false) {
41 fputs ($imap_stream, "a001 SELECT \"$mailbox\"\r\n");
42 $read = sqimap_read_data($imap_stream, "a001", true, $response, $message);
44 for ($i=0; $i<count($read); $i++
) {
45 if (strpos(strtolower($read[$i]), "recent")) {
46 $r = explode(" ", $read[$i]);
52 fputs ($imap_stream, "a001 EXPUNGE\r\n");
53 $tmp = sqimap_read_data($imap_stream, "a001", false, $a, $b);
59 /******************************************************************************
61 ******************************************************************************/
62 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
63 if (strtolower($type) == "noselect") {
64 $dm = sqimap_get_delimiter($imap_stream);
65 $mailbox = $mailbox.$dm;
67 fputs ($imap_stream, "a001 CREATE \"$mailbox\"\r\n");
68 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
70 sqimap_subscribe ($imap_stream, $mailbox);
75 /******************************************************************************
76 ** Subscribes to an existing folder
77 ******************************************************************************/
78 function sqimap_subscribe ($imap_stream, $mailbox) {
79 fputs ($imap_stream, "a001 SUBSCRIBE \"$mailbox\"\r\n");
80 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
86 /******************************************************************************
87 ** Unsubscribes to an existing folder
88 ******************************************************************************/
89 function sqimap_unsubscribe ($imap_stream, $mailbox) {
90 global $imap_server_type;
92 fputs ($imap_stream, "a001 UNSUBSCRIBE \"$mailbox\"\r\n");
93 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
99 /******************************************************************************
100 ** This function simply deletes the given folder
101 ******************************************************************************/
102 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
103 fputs ($imap_stream, "a001 DELETE \"$mailbox\"\r\n");
104 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
105 sqimap_unsubscribe ($imap_stream, $mailbox);
109 /******************************************************************************
110 ** Formats a mailbox into 4 parts for the $boxes array
112 ** The four parts are:
114 ** raw - Raw LIST/LSUB response from the IMAP server
115 ** formatted - nicely formatted folder name
116 ** unformatted - unformatted, but with delimiter at end removed
117 ** unformatted-dm - folder name as it appears in raw response
118 ** unformatted-disp - unformatted without $folder_prefix
120 ******************************************************************************/
121 function sqimap_mailbox_parse ($line, $line_lsub, $dm) {
122 global $folder_prefix;
124 // Process each folder line
125 for ($g=0; $g < count($line); $g++
) {
127 // Store the raw IMAP reply
128 if (isset($line[$g]))
129 $boxes[$g]["raw"] = $line[$g];
131 $boxes[$g]["raw"] = "";
134 // Count number of delimiters ($dm) in folder name
135 $mailbox = trim($line_lsub[$g]);
136 $dm_count = countCharInString($mailbox, $dm);
137 if (substr($mailbox, -1) == $dm)
138 $dm_count--; // If name ends in delimiter - decrement count by one
140 // Format folder name, but only if it's a INBOX.* or have
142 $boxesbyname[$mailbox] = $g;
143 $parentfolder = readMailboxParent($mailbox, $dm);
144 if((strtolower(substr($mailbox, 0, 5)) == "inbox") ||
145 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
146 (isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
147 $indent = $dm_count - (countCharInString($folder_prefix, $dm));
149 $boxes[$g]["formatted"] = str_repeat(" ", $indent);
151 $boxes[$g]["formatted"] = '';
152 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
154 $boxes[$g]["formatted"] = $mailbox;
157 $boxes[$g]['unformatted-dm'] = $mailbox;
158 if (substr($mailbox, -1) == $dm)
159 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
160 $boxes[$g]['unformatted'] = $mailbox;
161 //$boxes[$g]['unformatted-disp'] = ereg_replace('^' . $folder_prefix, '', $mailbox);
162 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
163 $boxes[$g]['unformatted-disp'] = substr($mailbox, strlen($folder_prefix));
164 } else if (strtolower($mailbox) == "inbox") {
165 $boxes[$g]['unformatted-disp'] = $mailbox;
167 $boxes[$g]['id'] = $g;
169 if (isset($line[$g]))
170 ereg("\(([^)]*)\)",$line[$g],$regs);
171 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
173 $boxes[$g]['flags'] = explode(' ', $flags);
176 $boxes[$g]['flags'] = array();
182 /* Apparently you must call a user function with usort instead
183 * of calling a built-in directly. Stupid.
184 * Patch from dave_michmerhuizen@yahoo.com
185 * Allows case insensitivity when sorting folders
187 function user_strcasecmp($a, $b)
189 return strcasecmp($a, $b);
193 /******************************************************************************
194 ** Returns sorted mailbox lists in several different ways.
195 ** See comment on sqimap_mailbox_parse() for info about the returned array.
196 ******************************************************************************/
197 function sqimap_mailbox_list ($imap_stream) {
198 global $data_dir, $username, $list_special_folders_first;
199 global $trash_folder, $sent_folder;
200 global $move_to_trash, $move_to_sent, $folder_prefix;
202 $inbox_in_list = false;
203 $inbox_subscribed = false;
205 include "../src/load_prefs.php";
206 include "../functions/array.php";
208 $dm = sqimap_get_delimiter ($imap_stream);
211 $inbox_subscribed = false;
212 fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n");
213 $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
215 /** OS: we don't want to parse last element of array, 'cause it is OK command, so we unset it **/
216 /** LUKE: This introduced errors.. do a check first **/
217 if (substr($lsub_ary[count($lsub_ary)-1], 0, 4) == "* OK") {
218 unset($lsub_ary[count($lsub_ary)-1]);
221 for ($i=0;$i < count($lsub_ary); $i++
) {
222 $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
223 if ($sorted_lsub_ary[$i] == "INBOX")
224 $inbox_subscribed = true;
227 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
228 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
229 $new_ary[] = $sorted_lsub_ary[$i];
232 $sorted_lsub_ary = $new_ary;
233 if (isset($sorted_lsub_ary)) {
234 usort($sorted_lsub_ary, "user_strcasecmp");
235 //sort($sorted_lsub_ary);
239 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
240 if (substr($sorted_lsub_ary[$i], -1) == $dm)
241 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
243 $mbx = $sorted_lsub_ary[$i];
245 fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n");
246 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
247 if (isset($sorted_list_ary[$i]))
248 $sorted_list_ary[$i] = "";
250 $sorted_list_ary[$i] = $read[0];
252 $sorted_list_ary[$i] = "";
253 if (isset($sorted_list_ary[$i]) && find_mailbox_name($sorted_list_ary[$i]) == "INBOX")
254 $inbox_in_list = true;
257 /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/
258 if ($inbox_subscribed == false ||
$inbox_in_list == false) {
259 fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
260 $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
262 $pos = count($sorted_list_ary);
263 $sorted_list_ary[$pos] = $inbox_ary[0];
265 $pos = count($sorted_lsub_ary);
266 $sorted_lsub_ary[$pos] = find_mailbox_name($inbox_ary[0]);
269 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm);
272 /** Now, lets sort for special folders **/
277 for ($i = 0; $i < count($boxes); $i++
) {
278 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
279 $boxesnew[] = $boxes[$i];
285 if ($list_special_folders_first == true) {
287 // Then list special folders and their subfolders
288 for ($i = 0 ; $i < count($boxes) ; $i++
) {
289 if ($move_to_trash &&
290 eregi('^' . quotemeta($trash_folder) . '(' .
291 quotemeta($dm) . '.*)?$', $boxes[$i]["unformatted"])) {
292 $boxesnew[] = $boxes[$i];
295 elseif ($move_to_sent &&
296 eregi('^' . quotemeta($sent_folder) . '(' .
297 quotemeta($dm) . '.*)?$', $boxes[$i]["unformatted"])) {
298 $boxesnew[] = $boxes[$i];
303 // Put INBOX.* folders ahead of the rest
304 for ($i = 0; $i < count($boxes); $i++
) {
305 if (eregi('^inbox\\.', $boxes[$i]["unformatted"]) &&
306 (!isset($used[$i]) ||
$used[$i] == false)) {
307 $boxesnew[] = $boxes[$i];
314 // Rest of the folders
315 for ($i = 0; $i < count($boxes); $i++
) {
316 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
317 (!isset($used[$i]) ||
$used[$i] == false)) {
318 $boxesnew[] = $boxes[$i];
326 /******************************************************************************
327 ** Returns a list of all folders, subscribed or not
328 ******************************************************************************/
329 function sqimap_mailbox_list_all ($imap_stream) {
330 global $list_special_folders_first, $folder_prefix;
332 if (!function_exists ("ary_sort"))
333 include ("../functions/array.php");
335 $dm = sqimap_get_delimiter ($imap_stream);
337 fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
338 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
342 for ($i = 0; $i < count($read_ary); $i++
) {
343 if (substr ($read_ary[$i], 0, 4) != "a001") {
345 // Store the raw IMAP reply
346 $boxes[$g]["raw"] = $read_ary[$i];
348 // Count number of delimiters ($dm) in folder name
349 $mailbox = find_mailbox_name($read_ary[$i]);
350 $dm_count = countCharInString($mailbox, $dm);
351 if (substr($mailbox, -1) == $dm)
352 $dm_count--; // If name ends in delimiter - decrement count by one
354 // Format folder name, but only if it's a INBOX.* or have
356 $boxesbyname[$mailbox] = $g;
357 $parentfolder = readMailboxParent($mailbox, $dm);
358 if((eregi('^inbox'.quotemeta($dm), $mailbox)) ||
359 (ereg('^'.$folder_prefix, $mailbox)) ||
360 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
362 $boxes[$g]["formatted"] = str_repeat(" ", $dm_count);
364 $boxes[$g]["formatted"] = '';
365 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
367 $boxes[$g]["formatted"] = $mailbox;
370 $boxes[$g]["unformatted-dm"] = $mailbox;
371 if (substr($mailbox, -1) == $dm)
372 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
373 $boxes[$g]["unformatted"] = $mailbox;
374 $boxes[$g]["unformatted-disp"] = ereg_replace('^' . $folder_prefix, '', $mailbox);
375 $boxes[$g]["id"] = $g;
377 /** Now lets get the flags for this mailbox **/
378 fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
379 $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
381 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+
1);
382 $flags = substr($flags, 0, strpos($flags, ")"));
383 $flags = str_replace('\\', '', $flags);
384 $flags = trim(strtolower($flags));
386 $boxes[$g]['flags'] = explode(" ", $flags);
390 $boxes[$g]['flags'] = array();
395 if(is_array($boxes)) {
396 $boxes = ary_sort ($boxes, "unformatted", 1);