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
17 * If $haystack is a full mailbox name, and $needle is the mailbox
18 * separator character, returns the second last part of the full
19 * mailbox name (i.e. the mailbox's parent mailbox)
21 function readMailboxParent($haystack, $needle) {
26 $parts = explode($needle, $haystack);
27 $elem = array_pop($parts);
28 while ($elem == '' && count($parts)) {
29 $elem = array_pop($parts);
31 $ret = join($needle, $parts);
37 function isBoxBelow( $box2, $box1 ) {
38 global $delimiter, $folder_prefix, $imap_server_type;
40 if ( $imap_server_type == 'uw' ) {
42 $i = strpos( $box1, $delimiter, strlen( $folder_prefix ) );
47 $boxs = $box2 . $delimiter;
48 /* Skip next second delimiter */
49 $i = strpos( $box1, $delimiter );
50 $i = strpos( $box1, $delimiter, $i +
1 );
58 return ( substr( $box1, 0, $i ) == substr( $boxs, 0, $i ) );
61 /* Defines special mailboxes */
62 function isSpecialMailbox( $box ) {
63 global $trash_folder, $sent_folder, $draft_folder,
64 $move_to_trash, $move_to_sent, $save_as_draft;
66 $ret = ( (strtolower($box) == 'inbox') ||
67 ( $move_to_trash && isBoxBelow( $box, $trash_folder ) ) ||
68 ( $move_to_sent && isBoxBelow( $box, $sent_folder )) ||
69 ($save_as_draft && $box == $draft_folder ) );
72 $ret = do_hook_function( 'special_mailbox', $box );
78 /* Expunges a mailbox */
79 function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true) {
80 $read = sqimap_run_command($imap_stream, 'EXPUNGE', $handle_errors,
84 /* Checks whether or not the specified mailbox exists */
85 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
86 if (! isset($mailbox)) {
89 $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
90 true, $response, $message);
91 return isset($mbx[0]);
94 /* Selects a mailbox */
95 function sqimap_mailbox_select ($imap_stream, $mailbox,
96 $hide = true, $recent = false, $extrainfo = false) {
99 if ( $mailbox == 'None' ) {
103 $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
104 true, $response, $message);
106 for ($i=0; $i<count($read); $i++
) {
107 if (strpos(strtolower($read[$i]), 'recent')) {
108 $r = explode(' ', $read[$i]);
114 $tmp = sqimap_run_command($imap_stream, 'EXPUNGE', false, $a, $b);
116 if (isset( $extrainfo ) && $extrainfo) {
118 for ($i=0; $i<count($read); $i++
) {
119 if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) {
120 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ;
121 $result['PERMANENTFLAGS'] = $regs[1];
123 else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) {
124 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ;
125 $result['FLAGS'] = $regs[1];
127 else if (preg_match("/(.*)EXISTS/i",$read[$i], $regs)) {
128 $result['EXISTS']=trim($regs[1]);
130 else if (preg_match("/(.*)RECENT/i",$read[$i], $regs)) {
131 $result['RECENT']=trim($regs[1]);
133 else if (preg_match("/\[UNSEEN(.*)\]/i",$read[$i], $regs)) {
134 $result['UNSEEN']=trim($regs[1]);
143 /* Creates a folder */
144 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
146 if (strtolower($type) == 'noselect') {
147 $mailbox .= $delimiter;
149 $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
150 true, $response, $message);
151 sqimap_subscribe ($imap_stream, $mailbox);
154 /* Subscribes to an existing folder */
155 function sqimap_subscribe ($imap_stream, $mailbox) {
156 $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"",
157 true, $response, $message);
160 /* Unsubscribes to an existing folder */
161 function sqimap_unsubscribe ($imap_stream, $mailbox) {
162 global $imap_server_type;
163 $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"",
164 true, $response, $message);
167 /* Deletes the given folder */
168 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
169 $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
170 true, $response, $message);
171 sqimap_unsubscribe ($imap_stream, $mailbox);
172 do_hook_function("rename_or_delete_folder", $args = array($mailbox, 'delete', ''));
175 /* Determines if the user is subscribed to the folder or not */
176 function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
177 $boxesall = sqimap_mailbox_list ($imap_stream);
178 foreach ($boxesall as $ref) {
179 if ($ref['unformatted'] == $folder) {
186 /* Renames a mailbox */
187 function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
188 if ( $old_name != $new_name ) {
189 global $delimiter, $imap_server_type;
190 if ( substr( $old_name, -1 ) == $delimiter ) {
191 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
192 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
193 $postfix = $delimiter;
197 $boxesall = sqimap_mailbox_list($imap_stream);
198 $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' . quoteIMAP($new_name) . '"';
199 $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message);
200 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
201 sqimap_subscribe($imap_stream, $new_name.$postfix);
202 do_hook_function("rename_or_delete_folder",$args = array($old_name, 'rename', $new_name));
203 $l = strlen( $old_name ) +
1;
205 foreach ( $boxesall as $box ) {
206 if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) {
207 $new_sub = $new_name . $delimiter . substr($box[$p], $l);
208 if ($imap_server_type == 'cyrus') {
209 $cmd = 'RENAME "' . quoteIMAP($box[$p]) . '" "' . quoteIMAP($new_sub) . '"';
210 $data = sqimap_run_command($imap_stream, $cmd, true,
211 $response, $message);
213 sqimap_unsubscribe($imap_stream, $box[$p]);
214 sqimap_subscribe($imap_stream, $new_sub);
215 do_hook_function("rename_or_delete_folder",
216 $args = array($box[$p], 'rename', $new_sub));
223 * Formats a mailbox into 4 parts for the $boxesall array
225 * The four parts are:
227 * raw - Raw LIST/LSUB response from the IMAP server
228 * formatted - nicely formatted folder name
229 * unformatted - unformatted, but with delimiter at end removed
230 * unformatted-dm - folder name as it appears in raw response
231 * unformatted-disp - unformatted without $folder_prefix
233 function sqimap_mailbox_parse ($line, $line_lsub) {
234 global $folder_prefix, $delimiter;
236 /* Process each folder line */
237 for ($g=0; $g < count($line); $g++
) {
239 /* Store the raw IMAP reply */
240 if (isset($line[$g])) {
241 $boxesall[$g]["raw"] = $line[$g];
244 $boxesall[$g]["raw"] = '';
248 /* Count number of delimiters ($delimiter) in folder name */
249 $mailbox = trim($line_lsub[$g]);
250 $dm_count = substr_count($mailbox, $delimiter);
251 if (substr($mailbox, -1) == $delimiter) {
252 /* If name ends in delimiter, decrement count by one */
256 /* Format folder name, but only if it's a INBOX.* or has a parent. */
257 $boxesallbyname[$mailbox] = $g;
258 $parentfolder = readMailboxParent($mailbox, $delimiter);
259 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
260 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
261 ( isset($boxesallbyname[$parentfolder]) &&
262 (strlen($parentfolder) > 0) ) ) {
263 $indent = $dm_count - ( substr_count($folder_prefix, $delimiter));
265 $boxesall[$g]['formatted'] = str_repeat(' ', $indent);
268 $boxesall[$g]['formatted'] = '';
270 $boxesall[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
273 $boxesall[$g]['formatted'] = $mailbox;
276 $boxesall[$g]['unformatted-dm'] = $mailbox;
277 if (substr($mailbox, -1) == $delimiter) {
278 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
280 $boxesall[$g]['unformatted'] = $mailbox;
281 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
282 $mailbox = substr($mailbox, strlen($folder_prefix));
284 $boxesall[$g]['unformatted-disp'] = $mailbox;
285 $boxesall[$g]['id'] = $g;
287 $boxesall[$g]['flags'] = array();
288 if (isset($line[$g])) {
289 ereg("\(([^)]*)\)",$line[$g],$regs);
290 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
292 $boxesall[$g]['flags'] = explode(' ', $flags);
301 * Sorting function used to sort mailbox names.
302 * + Original patch from dave_michmerhuizen@yahoo.com
303 * + Allows case insensitivity when sorting folders
304 * + Takes care of the delimiter being sorted to the end, causing
305 * subfolders to be listed in below folders that are prefixed
306 * with their parent folders name.
308 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
309 * Without special sort function: foobar between foo and foo.bar
310 * With special sort function: foobar AFTER foo and foo.bar :)
312 function user_strcasecmp($a, $b) {
315 /* Calculate the length of some strings. */
316 $a_length = strlen($a);
317 $b_length = strlen($b);
318 $min_length = min($a_length, $b_length);
319 $delimiter_length = strlen($delimiter);
321 /* Set the initial result value. */
324 /* Check the strings... */
325 for ($c = 0; $c < $min_length; ++
$c) {
326 $a_del = substr($a, $c, $delimiter_length);
327 $b_del = substr($b, $c, $delimiter_length);
329 if (($a_del == $delimiter) && ($b_del == $delimiter)) {
331 } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
333 } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
336 $result = strcasecmp($a{$c}, $b{$c});
344 /* If one string is a prefix of the other... */
346 if ($a_length < $b_length) {
348 } else if ($a_length > $b_length) {
358 * Returns sorted mailbox lists in several different ways.
359 * See comment on sqimap_mailbox_parse() for info about the returned array.
361 function sqimap_mailbox_list($imap_stream) {
362 global $boxesnew, $default_folder_prefix;
364 if ( !isset( $boxesnew ) ) {
366 global $data_dir, $username, $list_special_folders_first,
367 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
368 $move_to_trash, $move_to_sent, $save_as_draft,
371 $inbox_in_list = false;
372 $inbox_subscribed = false;
374 require_once('../src/load_prefs.php');
375 require_once('../functions/array.php');
378 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*%\"",
379 true, $response, $message);
382 * Section about removing the last element was removed
383 * We don't return "* OK" anymore from sqimap_read_data
386 $sorted_lsub_ary = array();
387 for ($i=0;$i < count($lsub_ary); $i++
) {
389 * Workaround for EIMS
390 * Doesn't work if the mailbox name is multiple lines
392 if (isset($lsub_ary[$i +
1]) &&
393 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
394 $lsub_ary[$i], $regs)) {
396 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
398 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
399 $sorted_lsub_ary[] = $temp_mailbox_name;
400 if (strtoupper($temp_mailbox_name) == 'INBOX') {
401 $inbox_subscribed = true;
405 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
406 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
407 $new_ary[] = $sorted_lsub_ary[$i];
410 $sorted_lsub_ary = $new_ary;
411 if (isset($sorted_lsub_ary)) {
412 usort($sorted_lsub_ary, 'user_strcasecmp');
416 $sorted_list_ary = array();
417 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
418 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
419 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
422 $mbx = $sorted_lsub_ary[$i];
425 $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
426 true, $response, $message);
427 /* Another workaround for EIMS */
428 if (isset($read[1]) &&
429 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
431 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) . '"' . $regs[2];
434 if (isset($sorted_list_ary[$i])) {
435 $sorted_list_ary[$i] = '';
438 if (isset($read[0])) {
439 $sorted_list_ary[$i] = $read[0];
442 $sorted_list_ary[$i] = '';
445 if (isset($sorted_list_ary[$i]) &&
446 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
447 $inbox_in_list = true;
452 * Just in case they're not subscribed to their inbox,
453 * we'll get it for them anyway
455 if (!$inbox_subscribed ||
!$inbox_in_list) {
456 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
457 true, $response, $message);
458 /* Another workaround for EIMS */
459 if (isset($inbox_ary[1]) &&
460 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
461 $inbox_ary[0], $regs)) {
462 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
466 $sorted_list_ary[] = $inbox_ary[0];
467 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
470 $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
472 /* Now, lets sort for special folders */
473 $boxesnew = $used = array();
476 foreach ( $boxesall as $k => $box ) {
477 if ( strtolower($box['unformatted']) == 'inbox') {
484 /* List special folders and their subfolders, if requested. */
485 if ($list_special_folders_first) {
486 foreach ( $boxesall as $k => $box ) {
487 if ( !$used[$k] && isSpecialMailbox( $box['unformatted'] ) ) {
491 $spec_sub = str_replace(' ', '', $box['formatted']);
493 /* In case of problems with preg
494 here is a ereg version
495 if (!$used[$k] && ereg("^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$", $box['unformatted']) ) { */
497 if (!$used[$k] && preg_match("?^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$?", $box['unformatted']) ) {
504 /* Rest of the folders */
505 foreach ( $boxesall as $k => $box ) {
515 * Returns a list of all folders, subscribed or not
517 function sqimap_mailbox_list_all($imap_stream) {
518 global $list_special_folders_first, $folder_prefix, $delimiter;
520 require_once('../functions/array.php');
522 $ssid = sqimap_session_id();
523 $lsid = strlen( $ssid );
524 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
525 $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message);
529 for ($i = 0; $i < count($read_ary); $i++
) {
530 /* Another workaround for EIMS */
531 if (isset($read_ary[$i +
1]) &&
532 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
533 $read_ary[$i], $regs)) {
535 $read_ary[$i] = $regs[1] . '"' . addslashes(trim($read_ary[$i])) . '"' . $regs[2];
537 if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
539 /* Store the raw IMAP reply */
540 $boxes[$g]['raw'] = $read_ary[$i];
542 /* Count number of delimiters ($delimiter) in folder name */
543 $mailbox = find_mailbox_name($read_ary[$i]);
544 $dm_count = substr_count($mailbox, $delimiter);
545 if (substr($mailbox, -1) == $delimiter) {
546 /* If name ends in delimiter - decrement count by one */
550 /* Format folder name, but only if it's a INBOX.* or has a parent. */
551 $boxesallbyname[$mailbox] = $g;
552 $parentfolder = readMailboxParent($mailbox, $delimiter);
553 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
554 (ereg('^'.$folder_prefix, $mailbox)) ||
555 ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
557 $boxes[$g]['formatted'] = str_repeat(' ', $dm_count);
560 $boxes[$g]['formatted'] = '';
562 $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
565 $boxes[$g]['formatted'] = $mailbox;
568 $boxes[$g]['unformatted-dm'] = $mailbox;
569 if (substr($mailbox, -1) == $delimiter) {
570 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
572 $boxes[$g]['unformatted'] = $mailbox;
573 $boxes[$g]['unformatted-disp'] = ereg_replace('^' . $folder_prefix, '', $mailbox);
574 $boxes[$g]['id'] = $g;
576 /* Now lets get the flags for this mailbox */
577 $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
578 true, $response, $message);
580 /* Another workaround for EIMS */
581 if (isset($read_mlbx[1]) &&
582 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", $read_mlbx[0], $regs)) {
583 $read_mlbx[0] = $regs[1] . '"' . addslashes(trim($read_mlbx[1])) . '"' . $regs[2];
586 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+
1);
587 $flags = substr($flags, 0, strpos($flags, ')'));
588 $flags = str_replace('\\', '', $flags);
589 $flags = trim(strtolower($flags));
591 $boxes[$g]['flags'] = explode(' ', $flags);
593 $boxes[$g]['flags'] = array();
598 if(is_array($boxes)) {
599 $boxes = ary_sort ($boxes, 'unformatted', 1);