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
15 Defines Special Mail Boxes
18 function isSpecialMailbox( $box ) {
20 global $trash_folder, $sent_folder, $draft_folder,
21 $move_to_trash, $move_to_sent, $save_as_draft,
22 $delimiter, $folder_prefix, $imap_server_type;
24 if ( $move_to_sent ) {
25 if ( $imap_server_type == 'uw' ) {
26 $i = strpos( $sent_folder, $delimiter, strlen( $folder_prefix ) );
31 // Skip next second delimiter
32 $i = strpos( $sent_folder, $delimiter );
33 $i = strpos( $sent_folder, $delimiter, $i+
1 );
40 $ret = ( (strtolower($box) == 'inbox') ||
41 ($box == $trash_folder &&
43 ( substr( $sent_folder, 0, $i ) == substr( $box, 0, $i ) &&
45 ($box == $draft_folder &&
52 /*************************
53 ** Expunges a mailbox **
54 *************************/
55 function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = TRUE)
57 $read = sqimap_run_command($imap_stream, 'EXPUNGE',
58 $handle_errors, $response, $message);
62 /******************************************************************************
63 ** Checks whether or not the specified mailbox exists
64 ******************************************************************************/
65 function sqimap_mailbox_exists ($imap_stream, $mailbox)
67 if (! isset($mailbox)) {
70 $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
71 TRUE, $response, $message);
72 return isset($mbx[0]);
75 /******************************************************************************
77 ******************************************************************************/
78 function sqimap_mailbox_select ($imap_stream, $mailbox,
79 $hide=TRUE, $recent=false)
83 if ( $mailbox == 'None' ) {
87 $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
88 TRUE, $response, $message);
90 for ($i=0; $i<count($read); $i++
) {
91 if (strpos(strtolower($read[$i]), 'recent')) {
92 $r = explode(' ', $read[$i]);
98 $tmp = sqimap_run_command($imap_stream, 'EXPUNGE',
105 /******************************************************************************
107 ******************************************************************************/
108 function sqimap_mailbox_create ($imap_stream, $mailbox, $type)
111 if (strtolower($type) == 'noselect') {
112 $mailbox = $mailbox.$delimiter;
114 $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
115 TRUE, $response, $message);
117 sqimap_subscribe ($imap_stream, $mailbox);
122 /******************************************************************************
123 ** Subscribes to an existing folder
124 ******************************************************************************/
125 function sqimap_subscribe ($imap_stream, $mailbox)
127 $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"",
128 TRUE, $response, $message);
133 /******************************************************************************
134 ** Unsubscribes to an existing folder
135 ******************************************************************************/
136 function sqimap_unsubscribe ($imap_stream, $mailbox)
138 global $imap_server_type;
140 $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"",
141 TRUE, $response, $message);
146 /******************************************************************************
147 ** This function simply deletes the given folder
148 ******************************************************************************/
149 function sqimap_mailbox_delete ($imap_stream, $mailbox)
151 $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
152 TRUE, $response, $message);
153 sqimap_unsubscribe ($imap_stream, $mailbox);
156 /***********************************************************************
157 ** Determines if the user is subscribed to the folder or not
158 **********************************************************************/
159 function sqimap_mailbox_is_subscribed($imap_stream, $folder)
161 $boxes = sqimap_mailbox_list ($imap_stream);
162 foreach ($boxes as $ref) {
163 if ($ref['unformatted'] == $folder) {
173 function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
175 if ( $old_name <> $new_name ) {
179 if ( substr( $old_name, -1 ) == $delimiter ) {
180 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
181 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
182 $postfix = $delimiter;
183 $boxes = sqimap_mailbox_list($imap_stream);
189 $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' . quoteIMAP($new_name) . '"';
190 $data = sqimap_run_command($imap_stream, $cmd,
191 TRUE, $response, $message);
192 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
193 sqimap_subscribe($imap_stream, $new_name.$postfix);
196 // Sub-unsub subfolders
197 $l = strlen( $old_name ) +
1;
199 foreach ( $boxes as $box ) {
200 if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) {
201 sqimap_unsubscribe($imap_stream, $box[$p]);
202 sqimap_subscribe($imap_stream,
203 $new_name . $delimiter . substr( $box[$p], $l ) );
212 /******************************************************************************
213 ** Formats a mailbox into 4 parts for the $boxes array
215 ** The four parts are:
217 ** raw - Raw LIST/LSUB response from the IMAP server
218 ** formatted - nicely formatted folder name
219 ** unformatted - unformatted, but with delimiter at end removed
220 ** unformatted-dm - folder name as it appears in raw response
221 ** unformatted-disp - unformatted without $folder_prefix
223 ******************************************************************************/
224 function sqimap_mailbox_parse ($line, $line_lsub)
226 global $folder_prefix, $delimiter;
228 /* Process each folder line */
229 for ($g=0; $g < count($line); $g++
) {
231 /* Store the raw IMAP reply */
232 if (isset($line[$g])) {
233 $boxes[$g]["raw"] = $line[$g];
236 $boxes[$g]["raw"] = "";
240 /* Count number of delimiters ($delimiter) in folder name */
241 $mailbox = trim($line_lsub[$g]);
242 $dm_count = substr_count($mailbox, $delimiter);
243 if (substr($mailbox, -1) == $delimiter) {
244 /* If name ends in delimiter - decrement count by one */
248 /* Format folder name, but only if it's a INBOX.* or have */
250 $boxesbyname[$mailbox] = $g;
251 $parentfolder = readMailboxParent($mailbox, $delimiter);
252 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
253 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
254 ( isset($boxesbyname[$parentfolder]) &&
255 (strlen($parentfolder) > 0) ) ) {
256 $indent = $dm_count - ( substr_count($folder_prefix, $delimiter));
258 $boxes[$g]['formatted'] = str_repeat(" ", $indent);
261 $boxes[$g]['formatted'] = '';
263 $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
266 $boxes[$g]['formatted'] = $mailbox;
269 $boxes[$g]['unformatted-dm'] = $mailbox;
270 if (substr($mailbox, -1) == $delimiter) {
271 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
273 $boxes[$g]['unformatted'] = $mailbox;
274 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
275 $mailbox = substr($mailbox, strlen($folder_prefix));
277 $boxes[$g]['unformatted-disp'] = $mailbox;
278 $boxes[$g]['id'] = $g;
280 $boxes[$g]['flags'] = array();
281 if (isset($line[$g])) {
282 ereg("\(([^)]*)\)",$line[$g],$regs);
283 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
285 $boxes[$g]['flags'] = explode(' ', $flags);
294 * Sorting function used to sort mailbox names.
295 * + Original patch from dave_michmerhuizen@yahoo.com
296 * + Allows case insensitivity when sorting folders
297 * + Takes care of the delimiter being sorted to the end, causing
298 * subfolders to be listed in below folders that are prefixed
299 * with their parent folders name.
300 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
301 * Without special sort function: foobar between foo and foo.bar
302 * With special sort function: foobar AFTER foo and foo.bar :)
304 function user_strcasecmp($a, $b) {
307 /* Calculate the length of some strings. */
308 $a_length = strlen($a);
309 $b_length = strlen($b);
310 $min_length = min($a_length, $b_length);
311 $delimiter_length = strlen($delimiter);
313 /* Set the initial result value. */
316 /* Check the strings... */
317 for ($c = 0; $c < $min_length; ++
$c) {
318 $a_del = substr($a, $c, $delimiter_length);
319 $b_del = substr($b, $c, $delimiter_length);
321 if (($a_del == $delimiter) && ($b_del == $delimiter)) {
323 } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
325 } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
328 $result = strcasecmp($a{$c}, $b{$c});
336 /* If one string is a prefix of the other... */
338 if ($a_length < $b_length) {
340 } else if ($a_length > $b_length) {
349 /******************************************************************************
350 ** Returns sorted mailbox lists in several different ways.
351 ** See comment on sqimap_mailbox_parse() for info about the returned array.
352 ******************************************************************************/
353 function sqimap_mailbox_list ($imap_stream) {
354 global $data_dir, $username, $list_special_folders_first,
355 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
356 $move_to_trash, $move_to_sent, $save_as_draft,
359 $inbox_in_list = false;
360 $inbox_subscribed = false;
362 require_once('../src/load_prefs.php');
363 require_once('../functions/array.php');
366 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
367 TRUE, $response, $message);
369 /* Section about removing the last element was removed */
370 /* We don't return "* OK" anymore from sqimap_read_data */
372 $sorted_lsub_ary = array();
373 for ($i=0;$i < count($lsub_ary); $i++
) {
374 /* Workaround for EIMS */
375 /* Doesn't work if the mailbox name is multiple lines */
376 if (isset($lsub_ary[$i +
1]) &&
377 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
378 $lsub_ary[$i], $regs)) {
380 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) .
383 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
384 $sorted_lsub_ary[] = $temp_mailbox_name;
385 if (strtoupper($temp_mailbox_name) == 'INBOX') {
386 $inbox_subscribed = TRUE;
390 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
391 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
392 $new_ary[] = $sorted_lsub_ary[$i];
395 $sorted_lsub_ary = $new_ary;
396 if (isset($sorted_lsub_ary)) {
397 usort($sorted_lsub_ary, 'user_strcasecmp');
401 $sorted_list_ary = array();
402 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
403 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
404 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
407 $mbx = $sorted_lsub_ary[$i];
410 $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
411 TRUE, $response, $message);
412 /* Another workaround for EIMS */
413 if (isset($read[1]) &&
414 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
416 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
420 if (isset($sorted_list_ary[$i])) {
421 $sorted_list_ary[$i] = '';
424 if (isset($read[0])) {
425 $sorted_list_ary[$i] = $read[0];
428 $sorted_list_ary[$i] = '';
431 if (isset($sorted_list_ary[$i]) &&
432 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
433 $inbox_in_list = TRUE;
438 * Just in case they're not subscribed to their inbox,
439 * we'll get it for them anyway
441 if ($inbox_subscribed == false ||
$inbox_in_list == false) {
442 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
443 TRUE, $response, $message);
444 /* Another workaround for EIMS */
445 if (isset($inbox_ary[1]) &&
446 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
447 $inbox_ary[0], $regs)) {
448 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
452 $sorted_list_ary[] = $inbox_ary[0];
453 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
456 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
458 /** Now, lets sort for special folders **/
459 $boxesnew = $used = array();
462 foreach ( $boxes as $k => $box ) {
463 if ( strtolower($box['unformatted']) == 'inbox') {
471 /* List special folders and their subfolders, if requested. */
472 if ($list_special_folders_first == TRUE) {
474 foreach ( $boxes as $k => $box ) {
476 isSpecialMailbox( $box['unformatted'] ) ) {
484 /* Rest of the folders */
485 foreach ( $boxes as $k => $box ) {
495 * Returns a list of all folders, subscribed or not
497 function sqimap_mailbox_list_all ($imap_stream)
499 global $list_special_folders_first, $folder_prefix;
502 if (!function_exists('ary_sort')) {
503 include_once('../functions/array.php');
506 $ssid = sqimap_session_id();
507 $lsid = strlen( $ssid );
508 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
509 $read_ary = sqimap_read_data ($imap_stream, $ssid, TRUE, $response, $message);
513 for ($i = 0; $i < count($read_ary); $i++
) {
514 /* Another workaround for EIMS */
515 if (isset($read_ary[$i +
1]) &&
516 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
517 $read_ary[$i], $regs)) {
519 $read_ary[$i] = $regs[1] . '"' .
520 addslashes(trim($read_ary[$i])) .
523 if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
525 /* Store the raw IMAP reply */
526 $boxes[$g]["raw"] = $read_ary[$i];
528 /* Count number of delimiters ($delimiter) in folder name */
529 $mailbox = find_mailbox_name($read_ary[$i]);
530 $dm_count = substr_count($mailbox, $delimiter);
531 if (substr($mailbox, -1) == $delimiter) {
532 /* If name ends in delimiter - decrement count by one */
536 /* Format folder name, but only if it's a INBOX.* or have */
538 $boxesbyname[$mailbox] = $g;
539 $parentfolder = readMailboxParent($mailbox, $delimiter);
540 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
541 (ereg('^'.$folder_prefix, $mailbox)) ||
542 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
544 $boxes[$g]['formatted'] = str_repeat(' ', $dm_count);
547 $boxes[$g]['formatted'] = '';
549 $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
552 $boxes[$g]['formatted'] = $mailbox;
555 $boxes[$g]["unformatted-dm"] = $mailbox;
556 if (substr($mailbox, -1) == $delimiter) {
557 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
559 $boxes[$g]['unformatted'] = $mailbox;
560 $boxes[$g]['unformatted-disp'] =
561 ereg_replace('^' . $folder_prefix, '', $mailbox);
562 $boxes[$g]['id'] = $g;
564 /** Now lets get the flags for this mailbox **/
565 $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
566 TRUE, $response, $message);
568 /* Another workaround for EIMS */
569 if (isset($read_mlbx[1]) &&
570 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
571 $read_mlbx[0], $regs)) {
572 $read_mlbx[0] = $regs[1] . '"' .
573 addslashes(trim($read_mlbx[1])) .
577 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+
1);
578 $flags = substr($flags, 0, strpos($flags, ')'));
579 $flags = str_replace('\\', '', $flags);
580 $flags = trim(strtolower($flags));
582 $boxes[$g]['flags'] = explode(' ', $flags);
584 $boxes[$g]['flags'] = array();
589 if(is_array($boxes)) {
590 $boxes = ary_sort ($boxes, 'unformatted', 1);