6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This impliments all functions that manipulate mailboxes
16 function isBoxBelow( $box2, $box1 ) {
18 global $delimiter, $folder_prefix, $imap_server_type;
20 if ( $imap_server_type == 'uw' ) {
22 $i = strpos( $box1, $delimiter, strlen( $folder_prefix ) );
27 $boxs = $box2 . $delimiter;
28 // Skip next second delimiter
29 $i = strpos( $box1, $delimiter );
30 $i = strpos( $box1, $delimiter, $i +
1 );
38 return( substr( $box1, 0, $i ) == substr( $boxs, 0, $i ) );
43 Defines Special Mail Boxes
45 function isSpecialMailbox( $box ) {
47 global $trash_folder, $sent_folder, $draft_folder,
48 $move_to_trash, $move_to_sent, $save_as_draft;
50 $ret = ( (strtolower($box) == 'inbox') ||
51 ( $move_to_trash && isBoxBelow( $box, $trash_folder ) ) ||
52 ( $move_to_sent && isBoxBelow( $box, $sent_folder )) ||
53 ($save_as_draft && $box == $draft_folder ) );
56 $ret = do_hook_function( 'special_mailbox', $box );
63 /*************************
64 ** Expunges a mailbox **
65 *************************/
66 function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = TRUE) {
67 $read = sqimap_run_command($imap_stream, 'EXPUNGE',
68 $handle_errors, $response, $message);
72 /******************************************************************************
73 ** Checks whether or not the specified mailbox exists
74 ******************************************************************************/
75 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
76 if (! isset($mailbox)) {
79 $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
80 TRUE, $response, $message);
81 return isset($mbx[0]);
84 /******************************************************************************
86 ******************************************************************************/
87 function sqimap_mailbox_select ($imap_stream, $mailbox,
88 $hide=TRUE, $recent=false, $extrainfo=false) {
91 if ( $mailbox == 'None' ) {
95 $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
96 TRUE, $response, $message);
98 for ($i=0; $i<count($read); $i++
) {
99 if (strpos(strtolower($read[$i]), 'recent')) {
100 $r = explode(' ', $read[$i]);
106 $tmp = sqimap_run_command($imap_stream, 'EXPUNGE',
109 if (isset( $extrainfo ) && $extrainfo) {
111 for ($i=0; $i<count($read); $i++
) {
112 if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) {
113 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ;
114 $result['PERMANENTFLAGS'] = $regs[1];
116 else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) {
117 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ;
118 $result['FLAGS'] = $regs[1];
120 else if (preg_match("/(.*)EXISTS/i",$read[$i], $regs)) {
121 $result['EXISTS']=trim($regs[1]);
123 else if (preg_match("/(.*)RECENT/i",$read[$i], $regs)) {
124 $result['RECENT']=trim($regs[1]);
126 else if (preg_match("/\[UNSEEN(.*)\]/i",$read[$i], $regs)) {
127 $result['UNSEEN']=trim($regs[1]);
138 /******************************************************************************
140 ******************************************************************************/
141 function sqimap_mailbox_create ($imap_stream, $mailbox, $type)
144 if (strtolower($type) == 'noselect') {
145 $mailbox = $mailbox.$delimiter;
147 $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
148 TRUE, $response, $message);
150 sqimap_subscribe ($imap_stream, $mailbox);
155 /******************************************************************************
156 ** Subscribes to an existing folder
157 ******************************************************************************/
158 function sqimap_subscribe ($imap_stream, $mailbox)
160 $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"",
161 TRUE, $response, $message);
166 /******************************************************************************
167 ** Unsubscribes to an existing folder
168 ******************************************************************************/
169 function sqimap_unsubscribe ($imap_stream, $mailbox)
171 global $imap_server_type;
173 $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"",
174 TRUE, $response, $message);
179 /******************************************************************************
180 ** This function simply deletes the given folder
181 ******************************************************************************/
182 function sqimap_mailbox_delete ($imap_stream, $mailbox)
184 $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
185 TRUE, $response, $message);
186 sqimap_unsubscribe ($imap_stream, $mailbox);
187 do_hook("rename_or_delete_folder");
190 /***********************************************************************
191 ** Determines if the user is subscribed to the folder or not
192 **********************************************************************/
193 function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
195 $boxesall = sqimap_mailbox_list ($imap_stream);
196 foreach ($boxesall as $ref) {
197 if ($ref['unformatted'] == $folder) {
208 function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
210 if ( $old_name <> $new_name ) {
211 global $delimiter, $imap_server_type;
212 if ( substr( $old_name, -1 ) == $delimiter ) {
213 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
214 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
215 $postfix = $delimiter;
219 $boxesall = sqimap_mailbox_list($imap_stream);
220 $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' . quoteIMAP($new_name) . '"';
221 $data = sqimap_run_command($imap_stream, $cmd,
222 TRUE, $response, $message);
223 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
224 sqimap_subscribe($imap_stream, $new_name.$postfix);
225 do_hook("rename_or_delete_folder");
226 $l = strlen( $old_name ) +
1;
228 foreach ( $boxesall as $box ) {
229 if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) {
230 $new_sub = $new_name . $delimiter . substr($box[$p], $l);
231 if ($imap_server_type == 'cyrus') {
232 $cmd = 'RENAME "' . quoteIMAP($box[$p]) . '" "' . quoteIMAP($new_sub) . '"';
233 $data = sqimap_run_command($imap_stream, $cmd, TRUE,
234 $response, $message);
236 sqimap_unsubscribe($imap_stream, $box[$p]);
237 sqimap_subscribe($imap_stream, $new_sub);
238 do_hook("rename_or_delete_folder");
245 /******************************************************************************
246 ** Formats a mailbox into 4 parts for the $boxesall array
248 ** The four parts are:
250 ** raw - Raw LIST/LSUB response from the IMAP server
251 ** formatted - nicely formatted folder name
252 ** unformatted - unformatted, but with delimiter at end removed
253 ** unformatted-dm - folder name as it appears in raw response
254 ** unformatted-disp - unformatted without $folder_prefix
256 ******************************************************************************/
257 function sqimap_mailbox_parse ($line, $line_lsub)
259 global $folder_prefix, $delimiter;
261 /* Process each folder line */
262 for ($g=0; $g < count($line); $g++
) {
264 /* Store the raw IMAP reply */
265 if (isset($line[$g])) {
266 $boxesall[$g]["raw"] = $line[$g];
269 $boxesall[$g]["raw"] = "";
273 /* Count number of delimiters ($delimiter) in folder name */
274 $mailbox = trim($line_lsub[$g]);
275 $dm_count = substr_count($mailbox, $delimiter);
276 if (substr($mailbox, -1) == $delimiter) {
277 /* If name ends in delimiter - decrement count by one */
281 /* Format folder name, but only if it's a INBOX.* or have */
283 $boxesallbyname[$mailbox] = $g;
284 $parentfolder = readMailboxParent($mailbox, $delimiter);
285 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
286 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
287 ( isset($boxesallbyname[$parentfolder]) &&
288 (strlen($parentfolder) > 0) ) ) {
289 $indent = $dm_count - ( substr_count($folder_prefix, $delimiter));
291 $boxesall[$g]['formatted'] = str_repeat(" ", $indent);
294 $boxesall[$g]['formatted'] = '';
296 $boxesall[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
299 $boxesall[$g]['formatted'] = $mailbox;
302 $boxesall[$g]['unformatted-dm'] = $mailbox;
303 if (substr($mailbox, -1) == $delimiter) {
304 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
306 $boxesall[$g]['unformatted'] = $mailbox;
307 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
308 $mailbox = substr($mailbox, strlen($folder_prefix));
310 $boxesall[$g]['unformatted-disp'] = $mailbox;
311 $boxesall[$g]['id'] = $g;
313 $boxesall[$g]['flags'] = array();
314 if (isset($line[$g])) {
315 ereg("\(([^)]*)\)",$line[$g],$regs);
316 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
318 $boxesall[$g]['flags'] = explode(' ', $flags);
327 * Sorting function used to sort mailbox names.
328 * + Original patch from dave_michmerhuizen@yahoo.com
329 * + Allows case insensitivity when sorting folders
330 * + Takes care of the delimiter being sorted to the end, causing
331 * subfolders to be listed in below folders that are prefixed
332 * with their parent folders name.
333 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
334 * Without special sort function: foobar between foo and foo.bar
335 * With special sort function: foobar AFTER foo and foo.bar :)
337 function user_strcasecmp($a, $b) {
340 /* Calculate the length of some strings. */
341 $a_length = strlen($a);
342 $b_length = strlen($b);
343 $min_length = min($a_length, $b_length);
344 $delimiter_length = strlen($delimiter);
346 /* Set the initial result value. */
349 /* Check the strings... */
350 for ($c = 0; $c < $min_length; ++
$c) {
351 $a_del = substr($a, $c, $delimiter_length);
352 $b_del = substr($b, $c, $delimiter_length);
354 if (($a_del == $delimiter) && ($b_del == $delimiter)) {
356 } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
358 } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
361 $result = strcasecmp($a{$c}, $b{$c});
369 /* If one string is a prefix of the other... */
371 if ($a_length < $b_length) {
373 } else if ($a_length > $b_length) {
382 /******************************************************************************
383 ** Returns sorted mailbox lists in several different ways.
384 ** See comment on sqimap_mailbox_parse() for info about the returned array.
385 ******************************************************************************/
386 function sqimap_mailbox_list($imap_stream) {
390 if ( !isset( $boxesnew ) ) {
392 GLOBAL $data_dir, $username, $list_special_folders_first,
393 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
394 $move_to_trash, $move_to_sent, $save_as_draft,
397 $inbox_in_list = $inbox_subscribed = FALSE;
399 require_once('../src/load_prefs.php');
400 require_once('../functions/array.php');
403 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
404 TRUE, $response, $message);
406 /* Section about removing the last element was removed */
407 /* We don't return "* OK" anymore from sqimap_read_data */
409 $sorted_lsub_ary = array();
410 for ($i=0;$i < count($lsub_ary); $i++
) {
411 /* Workaround for EIMS */
412 /* Doesn't work if the mailbox name is multiple lines */
413 if (isset($lsub_ary[$i +
1]) &&
414 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
415 $lsub_ary[$i], $regs)) {
417 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) .
420 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
421 $sorted_lsub_ary[] = $temp_mailbox_name;
422 if (strtoupper($temp_mailbox_name) == 'INBOX') {
423 $inbox_subscribed = TRUE;
427 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
428 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
429 $new_ary[] = $sorted_lsub_ary[$i];
432 $sorted_lsub_ary = $new_ary;
433 if (isset($sorted_lsub_ary)) {
434 usort($sorted_lsub_ary, 'user_strcasecmp');
438 $sorted_list_ary = array();
439 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
440 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
441 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
444 $mbx = $sorted_lsub_ary[$i];
447 $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
448 TRUE, $response, $message);
449 /* Another workaround for EIMS */
450 if (isset($read[1]) &&
451 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
453 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
457 if (isset($sorted_list_ary[$i])) {
458 $sorted_list_ary[$i] = '';
461 if (isset($read[0])) {
462 $sorted_list_ary[$i] = $read[0];
465 $sorted_list_ary[$i] = '';
468 if (isset($sorted_list_ary[$i]) &&
469 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
470 $inbox_in_list = TRUE;
475 * Just in case they're not subscribed to their inbox,
476 * we'll get it for them anyway
478 if ($inbox_subscribed == false ||
$inbox_in_list == false) {
479 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
480 TRUE, $response, $message);
481 /* Another workaround for EIMS */
482 if (isset($inbox_ary[1]) &&
483 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
484 $inbox_ary[0], $regs)) {
485 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
489 $sorted_list_ary[] = $inbox_ary[0];
490 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
493 $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
495 /** Now, lets sort for special folders **/
496 $boxesnew = $used = array();
499 foreach ( $boxesall as $k => $box ) {
500 if ( strtolower($box['unformatted']) == 'inbox') {
508 /* List special folders and their subfolders, if requested. */
509 if ($list_special_folders_first == TRUE) {
511 foreach ( $boxesall as $k => $box ) {
513 isSpecialMailbox( $box['unformatted'] ) ) {
521 /* Rest of the folders */
522 foreach ( $boxesall as $k => $box ) {
534 * Returns a list of all folders, subscribed or not
536 function sqimap_mailbox_list_all($imap_stream) {
539 GLOBAL $list_special_folders_first, $folder_prefix,
542 require_once('../functions/array.php');
544 $ssid = sqimap_session_id();
545 $lsid = strlen( $ssid );
546 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
547 $read_ary = sqimap_read_data ($imap_stream, $ssid, TRUE, $response, $message);
551 for ($i = 0; $i < count($read_ary); $i++
) {
552 /* Another workaround for EIMS */
553 if (isset($read_ary[$i +
1]) &&
554 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
555 $read_ary[$i], $regs)) {
557 $read_ary[$i] = $regs[1] . '"' .
558 addslashes(trim($read_ary[$i])) .
561 if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
563 /* Store the raw IMAP reply */
564 $boxes[$g]['raw'] = $read_ary[$i];
566 /* Count number of delimiters ($delimiter) in folder name */
567 $mailbox = find_mailbox_name($read_ary[$i]);
568 $dm_count = substr_count($mailbox, $delimiter);
569 if (substr($mailbox, -1) == $delimiter) {
570 /* If name ends in delimiter - decrement count by one */
574 /* Format folder name, but only if it's a INBOX.* or have */
576 $boxesallbyname[$mailbox] = $g;
577 $parentfolder = readMailboxParent($mailbox, $delimiter);
578 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
579 (ereg('^'.$folder_prefix, $mailbox)) ||
580 ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
582 $boxes[$g]['formatted'] = str_repeat(' ', $dm_count);
585 $boxes[$g]['formatted'] = '';
587 $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
590 $boxes[$g]['formatted'] = $mailbox;
593 $boxes[$g]['unformatted-dm'] = $mailbox;
594 if (substr($mailbox, -1) == $delimiter) {
595 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
597 $boxes[$g]['unformatted'] = $mailbox;
598 $boxes[$g]['unformatted-disp'] =
599 ereg_replace('^' . $folder_prefix, '', $mailbox);
600 $boxes[$g]['id'] = $g;
602 /** Now lets get the flags for this mailbox **/
603 $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
604 TRUE, $response, $message);
606 /* Another workaround for EIMS */
607 if (isset($read_mlbx[1]) &&
608 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
609 $read_mlbx[0], $regs)) {
610 $read_mlbx[0] = $regs[1] . '"' .
611 addslashes(trim($read_mlbx[1])) .
615 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+
1);
616 $flags = substr($flags, 0, strpos($flags, ')'));
617 $flags = str_replace('\\', '', $flags);
618 $flags = trim(strtolower($flags));
620 $boxes[$g]['flags'] = explode(' ', $flags);
622 $boxes[$g]['flags'] = array();
627 if(is_array($boxes)) {
628 $boxes = ary_sort ($boxes, 'unformatted', 1);