5d59aafbac04b49e211a73adeb3a29b8f8c40843
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 fputs ($imap_stream, "a001 EXPUNGE\r\n");
19 $read = sqimap_read_data($imap_stream, "a001", $handle_errors, $response, $message);
23 /******************************************************************************
24 ** Checks whether or not the specified mailbox exists
25 ******************************************************************************/
26 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
27 if (! isset($mailbox))
29 fputs ($imap_stream, "a001 LIST \"\" \"$mailbox\"\r\n");
30 $mbx = sqimap_read_data($imap_stream, "a001", true, $response, $message);
31 return isset($mbx[0]);
34 /******************************************************************************
36 ******************************************************************************/
37 function sqimap_mailbox_select ($imap_stream, $mailbox, $hide=true, $recent=false) {
40 fputs ($imap_stream, "a001 SELECT \"$mailbox\"\r\n");
41 $read = sqimap_read_data($imap_stream, "a001", true, $response, $message);
43 for ($i=0; $i<count($read); $i++
) {
44 if (strpos(strtolower($read[$i]), "recent")) {
45 $r = explode(" ", $read[$i]);
51 fputs ($imap_stream, "a001 EXPUNGE\r\n");
52 $tmp = sqimap_read_data($imap_stream, "a001", false, $a, $b);
58 /******************************************************************************
60 ******************************************************************************/
61 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
62 if (strtolower($type) == "noselect") {
63 $dm = sqimap_get_delimiter($imap_stream);
64 $mailbox = $mailbox.$dm;
66 fputs ($imap_stream, "a001 CREATE \"$mailbox\"\r\n");
67 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
69 sqimap_subscribe ($imap_stream, $mailbox);
74 /******************************************************************************
75 ** Subscribes to an existing folder
76 ******************************************************************************/
77 function sqimap_subscribe ($imap_stream, $mailbox) {
78 fputs ($imap_stream, "a001 SUBSCRIBE \"$mailbox\"\r\n");
79 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
85 /******************************************************************************
86 ** Unsubscribes to an existing folder
87 ******************************************************************************/
88 function sqimap_unsubscribe ($imap_stream, $mailbox) {
89 global $imap_server_type;
91 fputs ($imap_stream, "a001 UNSUBSCRIBE \"$mailbox\"\r\n");
92 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
98 /******************************************************************************
99 ** This function simply deletes the given folder
100 ******************************************************************************/
101 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
102 fputs ($imap_stream, "a001 DELETE \"$mailbox\"\r\n");
103 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
104 sqimap_unsubscribe ($imap_stream, $mailbox);
108 /******************************************************************************
109 ** Formats a mailbox into 4 parts for the $boxes array
111 ** The four parts are:
113 ** raw - Raw LIST/LSUB response from the IMAP server
114 ** formatted - nicely formatted folder name
115 ** unformatted - unformatted, but with delimiter at end removed
116 ** unformatted-dm - folder name as it appears in raw response
117 ** unformatted-disp - unformatted without $folder_prefix
119 ******************************************************************************/
120 function sqimap_mailbox_parse ($line, $line_lsub, $dm) {
121 global $folder_prefix;
123 // Process each folder line
124 for ($g=0; $g < count($line); $g++
) {
126 // Store the raw IMAP reply
127 if (isset($line[$g]))
128 $boxes[$g]["raw"] = $line[$g];
130 $boxes[$g]["raw"] = "";
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((strtolower(substr($mailbox, 0, 5)) == "inbox") ||
144 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
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 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix)
161 $mailbox = substr($mailbox, strlen($folder_prefix));
162 $boxes[$g]['unformatted-disp'] = $mailbox;
163 $boxes[$g]['id'] = $g;
165 if (isset($line[$g]))
166 ereg("\(([^)]*)\)",$line[$g],$regs);
167 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
169 $boxes[$g]['flags'] = explode(' ', $flags);
171 $boxes[$g]['flags'] = array();
177 /* Apparently you must call a user function with usort instead
178 * of calling a built-in directly. Stupid.
179 * Patch from dave_michmerhuizen@yahoo.com
180 * Allows case insensitivity when sorting folders
182 function user_strcasecmp($a, $b)
184 return strcasecmp($a, $b);
188 /******************************************************************************
189 ** Returns sorted mailbox lists in several different ways.
190 ** See comment on sqimap_mailbox_parse() for info about the returned array.
191 ******************************************************************************/
192 function sqimap_mailbox_list ($imap_stream) {
193 global $data_dir, $username, $list_special_folders_first;
194 global $trash_folder, $sent_folder;
195 global $move_to_trash, $move_to_sent, $folder_prefix;
197 $inbox_in_list = false;
198 $inbox_subscribed = false;
200 include "../src/load_prefs.php";
201 include "../functions/array.php";
203 $dm = sqimap_get_delimiter ($imap_stream);
206 $inbox_subscribed = false;
207 fputs ($imap_stream, "a001 LSUB \"$folder_prefix\" \"*%\"\r\n");
208 $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
210 /** OS: we don't want to parse last element of array, 'cause it is OK command, so we unset it **/
211 /** LUKE: This introduced errors.. do a check first **/
212 if (substr($lsub_ary[count($lsub_ary)-1], 0, 4) == "* OK") {
213 unset($lsub_ary[count($lsub_ary)-1]);
216 for ($i=0;$i < count($lsub_ary); $i++
) {
217 $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
218 if ($sorted_lsub_ary[$i] == "INBOX")
219 $inbox_subscribed = true;
222 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
223 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
224 $new_ary[] = $sorted_lsub_ary[$i];
227 $sorted_lsub_ary = $new_ary;
228 if (isset($sorted_lsub_ary)) {
229 usort($sorted_lsub_ary, "user_strcasecmp");
230 //sort($sorted_lsub_ary);
234 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
235 if (substr($sorted_lsub_ary[$i], -1) == $dm)
236 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
238 $mbx = $sorted_lsub_ary[$i];
240 fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n");
241 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
242 if (isset($sorted_list_ary[$i]))
243 $sorted_list_ary[$i] = "";
245 $sorted_list_ary[$i] = $read[0];
247 $sorted_list_ary[$i] = "";
248 if (isset($sorted_list_ary[$i]) && find_mailbox_name($sorted_list_ary[$i]) == "INBOX")
249 $inbox_in_list = true;
252 /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/
253 if ($inbox_subscribed == false ||
$inbox_in_list == false) {
254 fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
255 $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
257 $pos = count($sorted_list_ary);
258 $sorted_list_ary[$pos] = $inbox_ary[0];
260 $pos = count($sorted_lsub_ary);
261 $sorted_lsub_ary[$pos] = find_mailbox_name($inbox_ary[0]);
264 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm);
267 /** Now, lets sort for special folders **/
272 for ($i = 0; $i < count($boxes); $i++
) {
273 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
274 $boxesnew[] = $boxes[$i];
280 if ($list_special_folders_first == true) {
282 // Then list special folders and their subfolders
283 for ($i = 0 ; $i < count($boxes) ; $i++
) {
284 if ($move_to_trash &&
285 eregi('^' . quotemeta($trash_folder) . '(' .
286 quotemeta($dm) . '.*)?$', $boxes[$i]["unformatted"])) {
287 $boxesnew[] = $boxes[$i];
290 elseif ($move_to_sent &&
291 eregi('^' . quotemeta($sent_folder) . '(' .
292 quotemeta($dm) . '.*)?$', $boxes[$i]["unformatted"])) {
293 $boxesnew[] = $boxes[$i];
298 // Put INBOX.* folders ahead of the rest
299 for ($i = 0; $i < count($boxes); $i++
) {
300 if (eregi('^inbox\\.', $boxes[$i]["unformatted"]) &&
301 (!isset($used[$i]) ||
$used[$i] == false)) {
302 $boxesnew[] = $boxes[$i];
309 // Rest of the folders
310 for ($i = 0; $i < count($boxes); $i++
) {
311 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
312 (!isset($used[$i]) ||
$used[$i] == false)) {
313 $boxesnew[] = $boxes[$i];
321 /******************************************************************************
322 ** Returns a list of all folders, subscribed or not
323 ******************************************************************************/
324 function sqimap_mailbox_list_all ($imap_stream) {
325 global $list_special_folders_first, $folder_prefix;
327 if (!function_exists ("ary_sort"))
328 include ("../functions/array.php");
330 $dm = sqimap_get_delimiter ($imap_stream);
332 fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
333 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
337 for ($i = 0; $i < count($read_ary); $i++
) {
338 if (substr ($read_ary[$i], 0, 4) != "a001") {
340 // Store the raw IMAP reply
341 $boxes[$g]["raw"] = $read_ary[$i];
343 // Count number of delimiters ($dm) in folder name
344 $mailbox = find_mailbox_name($read_ary[$i]);
345 $dm_count = countCharInString($mailbox, $dm);
346 if (substr($mailbox, -1) == $dm)
347 $dm_count--; // If name ends in delimiter - decrement count by one
349 // Format folder name, but only if it's a INBOX.* or have
351 $boxesbyname[$mailbox] = $g;
352 $parentfolder = readMailboxParent($mailbox, $dm);
353 if((eregi('^inbox'.quotemeta($dm), $mailbox)) ||
354 (ereg('^'.$folder_prefix, $mailbox)) ||
355 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
357 $boxes[$g]["formatted"] = str_repeat(" ", $dm_count);
359 $boxes[$g]["formatted"] = '';
360 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
362 $boxes[$g]["formatted"] = $mailbox;
365 $boxes[$g]["unformatted-dm"] = $mailbox;
366 if (substr($mailbox, -1) == $dm)
367 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
368 $boxes[$g]["unformatted"] = $mailbox;
369 $boxes[$g]["unformatted-disp"] = ereg_replace('^' . $folder_prefix, '', $mailbox);
370 $boxes[$g]["id"] = $g;
372 /** Now lets get the flags for this mailbox **/
373 fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
374 $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
376 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+
1);
377 $flags = substr($flags, 0, strpos($flags, ")"));
378 $flags = str_replace('\\', '', $flags);
379 $flags = trim(strtolower($flags));
381 $boxes[$g]['flags'] = explode(" ", $flags);
385 $boxes[$g]['flags'] = array();
390 if(is_array($boxes)) {
391 $boxes = ary_sort ($boxes, "unformatted", 1);