cac503365c1e18ddf1f7acc03a2f2349627b3345
6 * Copyright (c) 1999-2001 The SquirrelMail Development Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This impliments all functions that manipulate mailboxes
14 /*************************
15 ** Expunges a mailbox **
16 *************************/
17 function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = true)
19 fputs ($imap_stream, sqimap_session_id() . " EXPUNGE\r\n");
20 $read = sqimap_read_data($imap_stream, sqimap_session_id(),
21 $handle_errors, $response, $message);
25 /******************************************************************************
26 ** Checks whether or not the specified mailbox exists
27 ******************************************************************************/
28 function sqimap_mailbox_exists ($imap_stream, $mailbox)
30 if (! isset($mailbox)) {
33 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mailbox\"\r\n");
34 $mbx = sqimap_read_data($imap_stream, sqimap_session_id(),
35 true, $response, $message);
36 return isset($mbx[0]);
39 /******************************************************************************
41 ******************************************************************************/
42 function sqimap_mailbox_select ($imap_stream, $mailbox,
43 $hide=true, $recent=false)
47 if ( $mailbox == 'None' ) {
51 fputs ($imap_stream, sqimap_session_id() . " SELECT \"$mailbox\"\r\n");
52 $read = sqimap_read_data($imap_stream, sqimap_session_id(),
53 true, $response, $message);
55 for ($i=0; $i<count($read); $i++
) {
56 if (strpos(strtolower($read[$i]), 'recent')) {
57 $r = explode(' ', $read[$i]);
63 fputs ($imap_stream, sqimap_session_id() . " EXPUNGE\r\n");
64 $tmp = sqimap_read_data($imap_stream, sqimap_session_id(),
71 /******************************************************************************
73 ******************************************************************************/
74 function sqimap_mailbox_create ($imap_stream, $mailbox, $type)
77 if (strtolower($type) == 'noselect') {
78 $mailbox = $mailbox.$delimiter;
80 fputs ($imap_stream, sqimap_session_id() . " CREATE \"$mailbox\"\r\n");
81 $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(),
82 true, $response, $message);
84 sqimap_subscribe ($imap_stream, $mailbox);
89 /******************************************************************************
90 ** Subscribes to an existing folder
91 ******************************************************************************/
92 function sqimap_subscribe ($imap_stream, $mailbox)
94 fputs ($imap_stream, sqimap_session_id() . " SUBSCRIBE \"$mailbox\"\r\n");
95 $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(),
96 true, $response, $message);
101 /******************************************************************************
102 ** Unsubscribes to an existing folder
103 ******************************************************************************/
104 function sqimap_unsubscribe ($imap_stream, $mailbox)
106 global $imap_server_type;
108 fputs ($imap_stream, sqimap_session_id() . " UNSUBSCRIBE \"$mailbox\"\r\n");
109 $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(),
110 true, $response, $message);
115 /******************************************************************************
116 ** This function simply deletes the given folder
117 ******************************************************************************/
118 function sqimap_mailbox_delete ($imap_stream, $mailbox)
120 fputs ($imap_stream, sqimap_session_id() . " DELETE \"$mailbox\"\r\n");
121 $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(),
122 true, $response, $message);
123 sqimap_unsubscribe ($imap_stream, $mailbox);
126 /***********************************************************************
127 ** Determines if the user is subscribed to the folder or not
128 **********************************************************************/
129 function sqimap_mailbox_is_subscribed($imap_stream, $folder)
131 $boxes = sqimap_mailbox_list ($imap_stream);
132 foreach ($boxes as $ref) {
133 if ($ref['unformatted'] == $folder) {
142 /******************************************************************************
143 ** Formats a mailbox into 4 parts for the $boxes array
145 ** The four parts are:
147 ** raw - Raw LIST/LSUB response from the IMAP server
148 ** formatted - nicely formatted folder name
149 ** unformatted - unformatted, but with delimiter at end removed
150 ** unformatted-dm - folder name as it appears in raw response
151 ** unformatted-disp - unformatted without $folder_prefix
153 ******************************************************************************/
154 function sqimap_mailbox_parse ($line, $line_lsub)
156 global $folder_prefix, $delimiter;
158 /* Process each folder line */
159 for ($g=0; $g < count($line); $g++
) {
161 /* Store the raw IMAP reply */
162 if (isset($line[$g])) {
163 $boxes[$g]["raw"] = $line[$g];
166 $boxes[$g]["raw"] = "";
170 /* Count number of delimiters ($delimiter) in folder name */
171 $mailbox = trim($line_lsub[$g]);
172 $dm_count = countCharInString($mailbox, $delimiter);
173 if (substr($mailbox, -1) == $delimiter) {
174 /* If name ends in delimiter - decrement count by one */
178 /* Format folder name, but only if it's a INBOX.* or have */
180 $boxesbyname[$mailbox] = $g;
181 $parentfolder = readMailboxParent($mailbox, $delimiter);
182 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
183 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
184 ( isset($boxesbyname[$parentfolder]) &&
185 (strlen($parentfolder) > 0) ) ) {
186 $indent = $dm_count - (countCharInString($folder_prefix, $delimiter));
188 $boxes[$g]["formatted"] = str_repeat(" ", $indent);
191 $boxes[$g]["formatted"] = '';
193 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $delimiter);
196 $boxes[$g]["formatted"] = $mailbox;
199 $boxes[$g]['unformatted-dm'] = $mailbox;
200 if (substr($mailbox, -1) == $delimiter) {
201 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
203 $boxes[$g]['unformatted'] = $mailbox;
204 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
205 $mailbox = substr($mailbox, strlen($folder_prefix));
207 $boxes[$g]['unformatted-disp'] = $mailbox;
208 $boxes[$g]['id'] = $g;
210 $boxes[$g]['flags'] = array();
211 if (isset($line[$g])) {
212 ereg("\(([^)]*)\)",$line[$g],$regs);
213 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
215 $boxes[$g]['flags'] = explode(' ', $flags);
223 /* Apparently you must call a user function with usort instead
224 * of calling a built-in directly. Stupid.
225 * Patch from dave_michmerhuizen@yahoo.com
226 * Allows case insensitivity when sorting folders
228 function user_strcasecmp($a, $b)
230 return strcasecmp($a, $b);
234 /******************************************************************************
235 ** Returns sorted mailbox lists in several different ways.
236 ** See comment on sqimap_mailbox_parse() for info about the returned array.
237 ******************************************************************************/
238 function sqimap_mailbox_list ($imap_stream)
240 global $data_dir, $username, $list_special_folders_first;
241 global $folder_prefix, $trash_folder, $sent_folder, $draft_folder;
242 global $move_to_trash, $move_to_sent, $save_as_draft;
245 $inbox_in_list = false;
246 $inbox_subscribed = false;
248 require_once('../src/load_prefs.php');
249 require_once('../functions/array.php');
252 fputs ($imap_stream, sqimap_session_id() .
253 " LSUB \"$folder_prefix\" \"*\"\r\n");
254 $lsub_ary = sqimap_read_data ($imap_stream, sqimap_session_id(),
255 true, $response, $message);
257 /* Section about removing the last element was removed */
258 /* We don't return "* OK" anymore from sqimap_read_data */
260 $sorted_lsub_ary = array();
261 for ($i=0;$i < count($lsub_ary); $i++
) {
262 /* Workaround for EIMS */
263 /* Doesn't work if the mailbox name is multiple lines */
264 if (isset($lsub_ary[$i +
1]) &&
265 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
266 $lsub_ary[$i], $regs)) {
268 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) .
271 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
272 $sorted_lsub_ary[] = $temp_mailbox_name;
273 if (strtoupper($temp_mailbox_name) == 'INBOX') {
274 $inbox_subscribed = true;
278 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
279 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
280 $new_ary[] = $sorted_lsub_ary[$i];
283 $sorted_lsub_ary = $new_ary;
284 if (isset($sorted_lsub_ary)) {
285 usort($sorted_lsub_ary, 'user_strcasecmp');
286 /*sort($sorted_lsub_ary); */
290 $sorted_list_ary = array();
291 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
292 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
293 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
296 $mbx = $sorted_lsub_ary[$i];
299 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mbx\"\r\n");
300 $read = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
301 /* Another workaround for EIMS */
302 if (isset($read[1]) &&
303 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
305 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
309 if (isset($sorted_list_ary[$i])) {
310 $sorted_list_ary[$i] = "";
313 if (isset($read[0])) {
314 $sorted_list_ary[$i] = $read[0];
317 $sorted_list_ary[$i] = "";
320 if (isset($sorted_list_ary[$i]) &&
321 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == "INBOX") {
322 $inbox_in_list = true;
327 * Just in case they're not subscribed to their inbox,
328 * we'll get it for them anyway
330 if ($inbox_subscribed == false ||
$inbox_in_list == false) {
331 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"INBOX\"\r\n");
332 $inbox_ary = sqimap_read_data ($imap_stream, sqimap_session_id(),
333 true, $response, $message);
334 /* Another workaround for EIMS */
335 if (isset($inbox_ary[1]) &&
336 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
337 $inbox_ary[0], $regs)) {
338 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
342 $sorted_list_ary[] = $inbox_ary[0];
343 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
346 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
348 /** Now, lets sort for special folders **/
352 for ($i = 0; $i < count($boxes); $i++
) {
353 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
354 $boxesnew[] = $boxes[$i];
360 /* List special folders and their subfolders, if requested. */
361 if ($list_special_folders_first == true) {
362 /* First list the trash folder. */
363 for ($i = 0 ; $i < count($boxes) ; $i++
) {
364 if ($move_to_trash &&
365 eregi('^' . quotemeta($trash_folder) . '(' .
366 quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
367 $boxesnew[] = $boxes[$i];
372 /* Then list the sent folder. */
373 for ($i = 0 ; $i < count($boxes) ; $i++
) {
375 eregi('^' . quotemeta($sent_folder) . '(' .
376 quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
377 $boxesnew[] = $boxes[$i];
382 /* Lastly, list the list the draft folder. */
383 for ($i = 0 ; $i < count($boxes) ; $i++
) {
384 if ($save_as_draft &&
385 eregi('^' . quotemeta($draft_folder) . '(' .
386 quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
387 $boxesnew[] = $boxes[$i];
392 /* Put INBOX.* folders ahead of the rest. */
393 for ($i = 0; $i < count($boxes); $i++
) {
394 if (eregi('^inbox\\.', $boxes[$i]["unformatted"]) &&
395 (!isset($used[$i]) ||
$used[$i] == false)) {
396 $boxesnew[] = $boxes[$i];
402 /* Rest of the folders */
403 for ($i = 0; $i < count($boxes); $i++
) {
404 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
405 (!isset($used[$i]) ||
$used[$i] == false)) {
406 $boxesnew[] = $boxes[$i];
414 /******************************************************************************
415 ** Returns a list of all folders, subscribed or not
416 ******************************************************************************/
417 function sqimap_mailbox_list_all ($imap_stream)
419 global $list_special_folders_first, $folder_prefix;
422 if (!function_exists ("ary_sort")) {
423 include_once('../functions/array.php');
426 $ssid = sqimap_session_id();
427 $lsid = strlen( $ssid );
428 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
429 $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message);
433 for ($i = 0; $i < count($read_ary); $i++
) {
434 /* Another workaround for EIMS */
435 if (isset($read_ary[$i +
1]) &&
436 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
437 $read_ary[$i], $regs)) {
439 $read_ary[$i] = $regs[1] . '"' .
440 addslashes(trim($read_ary[$i])) .
443 if (substr ($read_ary[$i], 0, $lsid) != $ssid ) {
445 /* Store the raw IMAP reply */
446 $boxes[$g]["raw"] = $read_ary[$i];
448 /* Count number of delimiters ($delimiter) in folder name */
449 $mailbox = find_mailbox_name($read_ary[$i]);
450 $dm_count = countCharInString($mailbox, $delimiter);
451 if (substr($mailbox, -1) == $delimiter) {
452 $dm_count--; /* If name ends in delimiter - decrement count by one */
455 /* Format folder name, but only if it's a INBOX.* or have */
457 $boxesbyname[$mailbox] = $g;
458 $parentfolder = readMailboxParent($mailbox, $delimiter);
459 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
460 (ereg('^'.$folder_prefix, $mailbox)) ||
461 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
463 $boxes[$g]["formatted"] = str_repeat(" ", $dm_count);
466 $boxes[$g]["formatted"] = '';
468 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $delimiter);
471 $boxes[$g]["formatted"] = $mailbox;
474 $boxes[$g]["unformatted-dm"] = $mailbox;
475 if (substr($mailbox, -1) == $delimiter) {
476 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
478 $boxes[$g]["unformatted"] = $mailbox;
479 $boxes[$g]["unformatted-disp"] = ereg_replace('^' . $folder_prefix, '', $mailbox);
480 $boxes[$g]["id"] = $g;
482 /** Now lets get the flags for this mailbox **/
483 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mailbox\"\r\n");
484 $read_mlbx = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
486 /* Another workaround for EIMS */
487 if (isset($read_mlbx[1]) &&
488 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
489 $read_mlbx[0], $regs)) {
490 $read_mlbx[0] = $regs[1] . '"' .
491 addslashes(trim($read_mlbx[1])) .
495 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+
1);
496 $flags = substr($flags, 0, strpos($flags, ")"));
497 $flags = str_replace('\\', '', $flags);
498 $flags = trim(strtolower($flags));
500 $boxes[$g]['flags'] = explode(" ", $flags);
503 $boxes[$g]['flags'] = array();
508 if(is_array($boxes)) {
509 $boxes = ary_sort ($boxes, "unformatted", 1);