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 filter_check($mailbox, 'delete');
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) {
204 /* fix filters when renaming/deleting folders */
206 function filter_check ($old_folder, $action, $new_folder = 'INBOX') {
207 global $plugins, $data_dir, $username;
209 $count = count($plugins);
210 for ($i=0;$i<$count;$i++
) {
211 if ($plugins[$i] == 'filters') {
212 require_once("../plugins/filters/filters.php");
213 $filters = load_filters();
217 if (empty($filters)) {
220 $filter_count = count($filters);
222 for ($i=0;$i<$filter_count;$i++
) {
223 if ($old_folder == $filters[$i]['folder']) {
224 if ($action == 'rename') {
225 $filters[$i]['folder'] = $new_folder;
226 setPref($data_dir, $username, "filter".$i, $filters[$i]['where'].",".$filters[$i]['what'].",".$new_folder);
228 elseif ($action == 'delete') {
241 function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
243 if ( $old_name <> $new_name ) {
244 global $delimiter, $imap_server_type;
245 if ( substr( $old_name, -1 ) == $delimiter ) {
246 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
247 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
248 $postfix = $delimiter;
252 $boxesall = sqimap_mailbox_list($imap_stream);
253 $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' . quoteIMAP($new_name) . '"';
254 $data = sqimap_run_command($imap_stream, $cmd,
255 TRUE, $response, $message);
256 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
257 sqimap_subscribe($imap_stream, $new_name.$postfix);
258 filter_check($old_name, 'rename', $new_name);
259 $l = strlen( $old_name ) +
1;
261 foreach ( $boxesall as $box ) {
262 if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) {
263 $new_sub = $new_name . $delimiter . substr($box[$p], $l);
264 if ($imap_server_type == 'cyrus') {
265 $cmd = 'RENAME "' . quoteIMAP($box[$p]) . '" "' . quoteIMAP($new_sub) . '"';
266 $data = sqimap_run_command($imap_stream, $cmd, TRUE,
267 $response, $message);
269 sqimap_unsubscribe($imap_stream, $box[$p]);
270 sqimap_subscribe($imap_stream, $new_sub);
271 filter_check($box[$p], 'rename', $new_sub);
278 /******************************************************************************
279 ** Formats a mailbox into 4 parts for the $boxesall array
281 ** The four parts are:
283 ** raw - Raw LIST/LSUB response from the IMAP server
284 ** formatted - nicely formatted folder name
285 ** unformatted - unformatted, but with delimiter at end removed
286 ** unformatted-dm - folder name as it appears in raw response
287 ** unformatted-disp - unformatted without $folder_prefix
289 ******************************************************************************/
290 function sqimap_mailbox_parse ($line, $line_lsub)
292 global $folder_prefix, $delimiter;
294 /* Process each folder line */
295 for ($g=0; $g < count($line); $g++
) {
297 /* Store the raw IMAP reply */
298 if (isset($line[$g])) {
299 $boxesall[$g]["raw"] = $line[$g];
302 $boxesall[$g]["raw"] = "";
306 /* Count number of delimiters ($delimiter) in folder name */
307 $mailbox = trim($line_lsub[$g]);
308 $dm_count = substr_count($mailbox, $delimiter);
309 if (substr($mailbox, -1) == $delimiter) {
310 /* If name ends in delimiter - decrement count by one */
314 /* Format folder name, but only if it's a INBOX.* or have */
316 $boxesallbyname[$mailbox] = $g;
317 $parentfolder = readMailboxParent($mailbox, $delimiter);
318 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
319 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
320 ( isset($boxesallbyname[$parentfolder]) &&
321 (strlen($parentfolder) > 0) ) ) {
322 $indent = $dm_count - ( substr_count($folder_prefix, $delimiter));
324 $boxesall[$g]['formatted'] = str_repeat(" ", $indent);
327 $boxesall[$g]['formatted'] = '';
329 $boxesall[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
332 $boxesall[$g]['formatted'] = $mailbox;
335 $boxesall[$g]['unformatted-dm'] = $mailbox;
336 if (substr($mailbox, -1) == $delimiter) {
337 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
339 $boxesall[$g]['unformatted'] = $mailbox;
340 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
341 $mailbox = substr($mailbox, strlen($folder_prefix));
343 $boxesall[$g]['unformatted-disp'] = $mailbox;
344 $boxesall[$g]['id'] = $g;
346 $boxesall[$g]['flags'] = array();
347 if (isset($line[$g])) {
348 ereg("\(([^)]*)\)",$line[$g],$regs);
349 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
351 $boxesall[$g]['flags'] = explode(' ', $flags);
360 * Sorting function used to sort mailbox names.
361 * + Original patch from dave_michmerhuizen@yahoo.com
362 * + Allows case insensitivity when sorting folders
363 * + Takes care of the delimiter being sorted to the end, causing
364 * subfolders to be listed in below folders that are prefixed
365 * with their parent folders name.
366 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
367 * Without special sort function: foobar between foo and foo.bar
368 * With special sort function: foobar AFTER foo and foo.bar :)
370 function user_strcasecmp($a, $b) {
373 /* Calculate the length of some strings. */
374 $a_length = strlen($a);
375 $b_length = strlen($b);
376 $min_length = min($a_length, $b_length);
377 $delimiter_length = strlen($delimiter);
379 /* Set the initial result value. */
382 /* Check the strings... */
383 for ($c = 0; $c < $min_length; ++
$c) {
384 $a_del = substr($a, $c, $delimiter_length);
385 $b_del = substr($b, $c, $delimiter_length);
387 if (($a_del == $delimiter) && ($b_del == $delimiter)) {
389 } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
391 } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
394 $result = strcasecmp($a{$c}, $b{$c});
402 /* If one string is a prefix of the other... */
404 if ($a_length < $b_length) {
406 } else if ($a_length > $b_length) {
415 /******************************************************************************
416 ** Returns sorted mailbox lists in several different ways.
417 ** See comment on sqimap_mailbox_parse() for info about the returned array.
418 ******************************************************************************/
419 function sqimap_mailbox_list($imap_stream) {
423 if ( !isset( $boxesnew ) ) {
425 GLOBAL $data_dir, $username, $list_special_folders_first,
426 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
427 $move_to_trash, $move_to_sent, $save_as_draft,
430 $inbox_in_list = $inbox_subscribed = FALSE;
432 require_once('../src/load_prefs.php');
433 require_once('../functions/array.php');
436 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
437 TRUE, $response, $message);
439 /* Section about removing the last element was removed */
440 /* We don't return "* OK" anymore from sqimap_read_data */
442 $sorted_lsub_ary = array();
443 for ($i=0;$i < count($lsub_ary); $i++
) {
444 /* Workaround for EIMS */
445 /* Doesn't work if the mailbox name is multiple lines */
446 if (isset($lsub_ary[$i +
1]) &&
447 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
448 $lsub_ary[$i], $regs)) {
450 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) .
453 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
454 $sorted_lsub_ary[] = $temp_mailbox_name;
455 if (strtoupper($temp_mailbox_name) == 'INBOX') {
456 $inbox_subscribed = TRUE;
460 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
461 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
462 $new_ary[] = $sorted_lsub_ary[$i];
465 $sorted_lsub_ary = $new_ary;
466 if (isset($sorted_lsub_ary)) {
467 usort($sorted_lsub_ary, 'user_strcasecmp');
471 $sorted_list_ary = array();
472 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
473 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
474 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
477 $mbx = $sorted_lsub_ary[$i];
480 $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
481 TRUE, $response, $message);
482 /* Another workaround for EIMS */
483 if (isset($read[1]) &&
484 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
486 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
490 if (isset($sorted_list_ary[$i])) {
491 $sorted_list_ary[$i] = '';
494 if (isset($read[0])) {
495 $sorted_list_ary[$i] = $read[0];
498 $sorted_list_ary[$i] = '';
501 if (isset($sorted_list_ary[$i]) &&
502 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
503 $inbox_in_list = TRUE;
508 * Just in case they're not subscribed to their inbox,
509 * we'll get it for them anyway
511 if ($inbox_subscribed == false ||
$inbox_in_list == false) {
512 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
513 TRUE, $response, $message);
514 /* Another workaround for EIMS */
515 if (isset($inbox_ary[1]) &&
516 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
517 $inbox_ary[0], $regs)) {
518 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
522 $sorted_list_ary[] = $inbox_ary[0];
523 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
526 $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
528 /** Now, lets sort for special folders **/
529 $boxesnew = $used = array();
532 foreach ( $boxesall as $k => $box ) {
533 if ( strtolower($box['unformatted']) == 'inbox') {
541 /* List special folders and their subfolders, if requested. */
542 if ($list_special_folders_first == TRUE) {
544 foreach ( $boxesall as $k => $box ) {
546 isSpecialMailbox( $box['unformatted'] ) ) {
554 /* Rest of the folders */
555 foreach ( $boxesall as $k => $box ) {
567 * Returns a list of all folders, subscribed or not
569 function sqimap_mailbox_list_all($imap_stream) {
572 GLOBAL $list_special_folders_first, $folder_prefix,
575 require_once('../functions/array.php');
577 $ssid = sqimap_session_id();
578 $lsid = strlen( $ssid );
579 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
580 $read_ary = sqimap_read_data ($imap_stream, $ssid, TRUE, $response, $message);
584 for ($i = 0; $i < count($read_ary); $i++
) {
585 /* Another workaround for EIMS */
586 if (isset($read_ary[$i +
1]) &&
587 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
588 $read_ary[$i], $regs)) {
590 $read_ary[$i] = $regs[1] . '"' .
591 addslashes(trim($read_ary[$i])) .
594 if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
596 /* Store the raw IMAP reply */
597 $boxes[$g]['raw'] = $read_ary[$i];
599 /* Count number of delimiters ($delimiter) in folder name */
600 $mailbox = find_mailbox_name($read_ary[$i]);
601 $dm_count = substr_count($mailbox, $delimiter);
602 if (substr($mailbox, -1) == $delimiter) {
603 /* If name ends in delimiter - decrement count by one */
607 /* Format folder name, but only if it's a INBOX.* or have */
609 $boxesallbyname[$mailbox] = $g;
610 $parentfolder = readMailboxParent($mailbox, $delimiter);
611 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
612 (ereg('^'.$folder_prefix, $mailbox)) ||
613 ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
615 $boxes[$g]['formatted'] = str_repeat(' ', $dm_count);
618 $boxes[$g]['formatted'] = '';
620 $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
623 $boxes[$g]['formatted'] = $mailbox;
626 $boxes[$g]['unformatted-dm'] = $mailbox;
627 if (substr($mailbox, -1) == $delimiter) {
628 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
630 $boxes[$g]['unformatted'] = $mailbox;
631 $boxes[$g]['unformatted-disp'] =
632 ereg_replace('^' . $folder_prefix, '', $mailbox);
633 $boxes[$g]['id'] = $g;
635 /** Now lets get the flags for this mailbox **/
636 $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
637 TRUE, $response, $message);
639 /* Another workaround for EIMS */
640 if (isset($read_mlbx[1]) &&
641 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
642 $read_mlbx[0], $regs)) {
643 $read_mlbx[0] = $regs[1] . '"' .
644 addslashes(trim($read_mlbx[1])) .
648 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+
1);
649 $flags = substr($flags, 0, strpos($flags, ')'));
650 $flags = str_replace('\\', '', $flags);
651 $flags = trim(strtolower($flags));
653 $boxes[$g]['flags'] = explode(' ', $flags);
655 $boxes[$g]['flags'] = array();
660 if(is_array($boxes)) {
661 $boxes = ary_sort ($boxes, 'unformatted', 1);