<?php

/**
 * folder_manip.php
 *
 * Functions for IMAP folder manipulation:
 * (un)subscribe, create, rename, delete.
 *
 * @author Thijs Kinkhorst <kink at squirrelmail.org>
 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 * @version $Id: folder_manip.php,v 1.7 2006/01/23 18:39:32 tokul Exp $
 * @package squirrelmail
 * @see folders.php
 */


/**
 * Helper function for the functions below; checks if the user entered
 * folder name is valid according to the IMAP standard. If not, it
 * bails out with an error and cleanly terminates the IMAP connection.
 */
function folders_checkname($imapConnection, $folder_name, $delimiter)
{
    if (substr_count($folder_name, '"') || substr_count($folder_name, "\\") ||
        substr_count($folder_name, $delimiter) || ($folder_name == '')) {

        global $color;
        error_box(_("Illegal folder name.") . "<br />\n" .
                sprintf(_("The name may not contain any of the following: %s"), '<tt>" \\ '.$delimiter.'</tt>')
                . "<br />\n" .
                _("Please select a different name.").
                '<br /><a href="folders.php">'.
                _("Click here to go back") . '</a>.', $color);

        sqimap_logout($imapConnection);
        exit;
    }
}

/**
 * Called from folders.php to create a new folder.
 */
function folders_create ($imapConnection, $delimiter, $folder_name, $subfolder, $contain_subs)
{
    folders_checkname($imapConnection, $folder_name, $delimiter);

    global $folder_prefix;

    $folder_name = imap_utf7_encode_local($folder_name);

    if ( ! empty($contain_subs) ) {
        $folder_name = $folder_name . $delimiter;
    }

    if ($folder_prefix && (substr($folder_prefix, -1) != $delimiter)) {
        $folder_prefix = $folder_prefix . $delimiter;
    }
    if ($folder_prefix && (substr($subfolder, 0, strlen($folder_prefix)) != $folder_prefix)) {
        $subfolder_orig = $subfolder;
        $subfolder = $folder_prefix . $subfolder;
    } else {
        $subfolder_orig = $subfolder;
    }

    if (trim($subfolder_orig) == '') {
        sqimap_mailbox_create ($imapConnection, $folder_prefix.$folder_name, '');
    } else {
        sqimap_mailbox_create ($imapConnection, $subfolder.$delimiter.$folder_name, '');
    }

    return;
}

/**
 * Called from folders.php, given a folder name, ask the user what this
 * folder should be renamed to.
 */
function folders_rename_getname ($imapConnection, $delimiter, $old) {
    global $color,$default_folder_prefix;

    if ( $old == '' ) {
        plain_error_message(_("You have not selected a folder to rename. Please do so.").
            '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
        sqimap_logout($imapConnection);
        exit;
    }

    if (substr($old, strlen($old) - strlen($delimiter)) == $delimiter) {
        $isfolder = TRUE;
        $old = substr($old, 0, strlen($old) - 1);
    } else {
        $isfolder = FALSE;
    }

    $old = imap_utf7_decode_local($old);

    if (strpos($old, $delimiter)) {
        $old_name = substr($old, strrpos($old, $delimiter)+1, strlen($old));
        // hide default prefix (INBOX., mail/ or other)
        $quoted_prefix=preg_quote($default_folder_prefix,'/');
        $prefix_length=(preg_match("/^$quoted_prefix/",$old) ? strlen($default_folder_prefix) : 0);
        if ($prefix_length>strrpos($old, $delimiter)) {
            $old_parent = '';
        } else {
            $old_parent = substr($old, $prefix_length, (strrpos($old, $delimiter)-$prefix_length))
                . ' ' . $delimiter;
        }
    } else {
        $old_name = $old;
        $old_parent = '';
    }

    echo '<br />' .
    html_tag( 'table', '', 'center', '', 'width="95%" border="0"' ) .
        html_tag( 'tr',
            html_tag( 'td', '<b>' . _("Rename a folder") . '</b>', 'center', $color[0] )
        ) .
        html_tag( 'tr' ) .
            html_tag( 'td', '', 'center', $color[4] ) .
            addForm('folders.php').
            addHidden('smaction', 'rename').
             _("New name:").
             '<br /><b>' . htmlspecialchars($old_parent) . '</b>' .
             addInput('new_name', $old_name, 25) . '<br /><br />' . "\n";
             if ( $isfolder ) {
                 echo addHidden('isfolder', 'true');
             }
             echo addHidden('orig', $old).
             addHidden('old_name', $old_name).
             '<input type="submit" value="'._("Rename")."\" />\n".
             '<input type="submit" name="cancelbutton" value="'._("Cancel")."\" />\n".
             '</form><br /></td></tr></table>';
    echo "\n</td></tr></table>\n</td></tr></table>\n\n</body></html>";

    sqimap_logout($imapConnection);
    exit;
}

/**
 * Given an old and new folder name, renames the folder.
 */
function folders_rename_do($imapConnection, $delimiter, $orig, $old_name, $new_name)
{
    folders_checkname($imapConnection, $new_name, $delimiter);

    $orig = imap_utf7_encode_local($orig);
    $old_name = imap_utf7_encode_local($old_name);
    $new_name = imap_utf7_encode_local($new_name);

    if ($old_name != $new_name) {

        if (strpos($orig, $delimiter)) {
            $old_dir = substr($orig, 0, strrpos($orig, $delimiter));
        } else {
            $old_dir = '';
        }

        if ($old_dir != '') {
            $newone = $old_dir . $delimiter . $new_name;
        } else {
            $newone = $new_name;
        }

        // Renaming a folder doesn't rename the folder but leaves you unsubscribed
        //    at least on Cyrus IMAP servers.
        if (isset($isfolder)) {
            $newone = $newone.$delimiter;
            $orig = $orig.$delimiter;
        }
        sqimap_mailbox_rename( $imapConnection, $orig, $newone );

    }

    return;
}

/**
 * Presents a confirmation dialog to the user asking whether they're
 * sure they want to delete this folder.
 */
function folders_delete_ask ($imapConnection, $folder_name)
{
    global $color,$default_folder_prefix;

    if ($folder_name == '') {
        plain_error_message(_("You have not selected a folder to delete. Please do so.").
            '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
        exit;
    }

    // hide default folder prefix (INBOX., mail/ or other)
    $visible_folder_name = imap_utf7_decode_local($folder_name);
    $quoted_prefix = preg_quote($default_folder_prefix,'/');
    $prefix_length = (preg_match("/^$quoted_prefix/",$visible_folder_name) ? strlen($default_folder_prefix) : 0);
    $visible_folder_name = substr($visible_folder_name,$prefix_length);

    echo '<br />' .
        html_tag( 'table', '', 'center', '', 'width="95%" border="0"' ) .
        html_tag( 'tr',
            html_tag( 'td', '<b>' . _("Delete Folder") . '</b>', 'center', $color[0] )
        ) .
        html_tag( 'tr' ) .
        html_tag( 'td', '', 'center', $color[4] ) .
        sprintf(_("Are you sure you want to delete %s?"),
            str_replace(array(' ','<','>'),array('&nbsp;','&lt;','&gt;'),$visible_folder_name)).
        addForm('folders.php', 'post')."<p>\n".
        addHidden('smaction', 'delete').
        addHidden('folder_name', $folder_name).
        addSubmit(_("Yes"), 'confirmed').
        addSubmit(_("No"), 'cancelbutton').
        '</p></form><br /></td></tr></table>';

    echo "\n</td></tr></table>\n</td></tr></table>\n\n</body></html>";

    sqimap_logout($imapConnection);
    exit;
}

/**
 * Given a folder, moves it to trash (and all subfolders of it too).
 */
function folders_delete_do ($imapConnection, $delimiter, $folder_name)
{
    require_once(SM_PATH . 'functions/tree.php');

    $boxes = sqimap_mailbox_list ($imapConnection);

    global $delete_folder, $imap_server_type, $trash_folder, $move_to_trash;

    if (substr($folder_name, -1) == $delimiter) {
        $folder_name_no_dm = substr($folder_name, 0, strlen($folder_name) - 1);
    } else {
        $folder_name_no_dm = $folder_name;
    }

    /** lets see if we CAN move folders to the trash.. otherwise,
        ** just delete them **/

    /* Courier IMAP doesn't like subfolders of Trash
     * If global options say we can't move it into Trash
     * If it's already a subfolder of trash, we'll have to delete it */
    if (strtolower($imap_server_type) == 'courier' ||
       (isset($delete_folder) && $delete_folder) ||
        eregi('^'.$trash_folder.'.+', $folder_name) )
    {
        $can_move_to_trash = FALSE;
    }
    /* Otherwise, check if trash folder exits and support sub-folders */
    else {
        foreach($boxes as $box) {
            if ($box['unformatted'] == $trash_folder) {
                $can_move_to_trash = !in_array('noinferiors', $box['flags']);
            }
        }
    }

    /** First create the top node in the tree **/
    foreach($boxes as $box) {
        if (($box['unformatted-dm'] == $folder_name) && (strlen($box['unformatted-dm']) == strlen($folder_name))) {
            $foldersTree[0]['value'] = $folder_name;
            $foldersTree[0]['doIHaveChildren'] = false;
            continue;
        }
    }

    /* Now create the nodes for subfolders of the parent folder
       You can tell that it is a subfolder by tacking the mailbox delimiter
       on the end of the $folder_name string, and compare to that.  */
    foreach($boxes as $box) {
        if (substr($box['unformatted'], 0, strlen($folder_name_no_dm . $delimiter)) == ($folder_name_no_dm . $delimiter)) {
            addChildNodeToTree($box['unformatted'], $box['unformatted-dm'], $foldersTree);
        }
    }

    /** Lets start removing the folders and messages **/
    if (($move_to_trash == true) && ($can_move_to_trash == true)) { /** if they wish to move messages to the trash **/
        walkTreeInPostOrderCreatingFoldersUnderTrash(0, $imapConnection, $foldersTree, $folder_name);
        walkTreeInPreOrderDeleteFolders(0, $imapConnection, $foldersTree);
    } else { /** if they do NOT wish to move messages to the trash (or cannot)**/
        walkTreeInPreOrderDeleteFolders(0, $imapConnection, $foldersTree);
    }

    return;
}

/**
 * Given an array of folder_names, subscribes to each of them.
 */
function folders_subscribe($imapConnection, $folder_names)
{
    global $color;

    if (count($folder_names) == 0 || $folder_names[0] == '') {
        plain_error_message(_("You have not selected a folder to subscribe. Please do so.").
            '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
        sqimap_logout($imapConnection);
        exit;
    }

    global $no_list_for_subscribe, $imap_server_type;

    if($no_list_for_subscribe && $imap_server_type == 'cyrus') {
        /* Cyrus, atleast, does not typically allow subscription to
         * nonexistent folders (this is an optional part of IMAP),
         * lets catch it here and report back cleanly. */
        if(!sqimap_mailbox_exists($imapConnection, $folder_names[0])) {
            plain_error_message(_("Subscription Unsuccessful - Folder does not exist.").
                '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
            sqimap_logout($imapConnection);
            exit;

        }
    }
    foreach ( $folder_names as $folder_name ) {
        sqimap_subscribe ($imapConnection, $folder_name);
    }

    return;
}

/**
 * Given a list of folder names, unsubscribes from each of them.
 */
function folders_unsubscribe($imapConnection, $folder_names)
{
    global $color;

    if (count($folder_names) == 0 || $folder_names[0] == '') {
        plain_error_message(_("You have not selected a folder to unsubscribe. Please do so.").
            '<br /><a href="../src/folders.php">'._("Click here to go back").'</a>.', $color);
        sqimap_logout($imapConnection);
        exit;
    }

    foreach ( $folder_names as $folder_name ) {
        sqimap_unsubscribe ($imapConnection, $folder_name);
    }

    return;
}


?>