function handle_mark_actions($user_id, $mark_action, $msg_ids, $cur_folder_id)
{
    global $_CLASS;
    if (empty($msg_ids)) {
        return;
    }
    switch ($mark_action) {
        case 'mark_important':
            $mark_list = array();
            $sql = 'SELECT msg_id, marked FROM ' . FORUMS_PRIVMSGS_TO_TABLE . "\n\t\t\t\tWHERE folder_id = {$cur_folder_id}\n\t\t\t\t\tAND user_id = {$user_id}\n\t\t\t\t\tAND msg_id IN (" . implode(', ', $msg_ids) . ')';
            $result = $_CLASS['core_db']->query($sql);
            while ($row = $_CLASS['core_db']->fetch_row_assoc($result)) {
                $row['marked'] = $row['marked'] ? 0 : 1;
                $mark_list[$row['marked']][] = $row['msg_id'];
            }
            $_CLASS['core_db']->free_result($result);
            if (empty($mark_list)) {
                break;
            }
            $_CLASS['core_db']->transaction();
            foreach ($mark_list as $mark => $ids) {
                $sql = 'UPDATE ' . FORUMS_PRIVMSGS_TO_TABLE . "\n\t\t\t\t\tSET marked = {$mark}\n\t\t\t\t\tWHERE msg_id IN (" . implode(', ', $ids) . ')';
                $_CLASS['core_db']->query($sql);
            }
            $_CLASS['core_db']->transaction('commit');
            break;
        case 'delete_marked':
            $hidden_fields = array('marked_msg_id' => $msg_ids, 'cur_folder_id' => $cur_folder_id, 'mark_option' => 'delete_marked', 'submit_mark' => true);
            if (display_confirmation($_CLASS['core_user']->get_lang('DELETE_MARKED_PM'), generate_hidden_fields($hidden_fields))) {
                $_CLASS['core_db']->transaction();
                delete_pm($user_id, $msg_ids, $cur_folder_id);
                $_CLASS['core_db']->transaction('commit');
                $success_msg = count($msg_ids) === 1 ? 'MESSAGE_DELETED' : 'MESSAGES_DELETED';
                $redirect = generate_link('Control_Panel&i=pm&folder=' . $cur_folder_id);
                $_CLASS['core_display']->meta_refresh(3, $redirect);
                trigger_error($_CLASS['core_user']->lang[$success_msg] . '<br /><br />' . sprintf($_CLASS['core_user']->lang['RETURN_FOLDER'], '<a href="' . $redirect . '">', '</a>'));
            }
            break;
            /*
            case 'export_as_xml':
            case 'export_as_csv':
            case 'export_as_txt':
            	$export_as = str_replace('export_as_', '', $mark_action);
            break;
            */
        /*
        case 'export_as_xml':
        case 'export_as_csv':
        case 'export_as_txt':
        	$export_as = str_replace('export_as_', '', $mark_action);
        break;
        */
        default:
            return false;
            break;
    }
    return true;
}
function message_options($id, $mode, $global_privmsgs_rules, $global_rule_conditions)
{
    global $_CLASS, $config;
    $redirect_url = generate_link('Control_Panel&i=pm&mode=options');
    $_CLASS['core_template']->assign_array(array('ERROR_MESSAGE' => false, 'S_RULE_DEFINED' => false, 'S_COND_DEFINED' => false, 'NONE_CONDITION' => false, 'S_ACTION_DEFINED' => false, 'NOTIFICATION_MESSAGE' => false, 'rule' => false));
    // Change "full folder" setting - what to do if folder is full
    if (isset($_POST['fullfolder'])) {
        $full_action = request_var('full_action', 0);
        $set_folder_id = 0;
        switch ($full_action) {
            case 1:
                $set_folder_id = FULL_FOLDER_DELETE;
                break;
            case 2:
                $set_folder_id = request_var('full_move_to', PRIVMSGS_INBOX);
                break;
            case 3:
                $set_folder_id = FULL_FOLDER_HOLD;
                break;
            default:
                $full_action = 0;
                break;
        }
        if ($full_action) {
            $sql = 'UPDATE ' . USERS_TABLE . '
				SET user_full_folder = ' . $set_folder_id . '
				WHERE user_id = ' . $_CLASS['core_user']->data['user_id'];
            $_CLASS['core_db']->query($sql);
            $_CLASS['core_user']->data['user_full_folder'] = $set_folder_id;
            $message = $_CLASS['core_user']->lang['FULL_FOLDER_OPTION_CHANGED'] . '<br /><br />' . sprintf($_CLASS['core_user']->lang['RETURN_UCP'], '<a href="' . $redirect_url . '">', '</a>');
            $_CLASS['core_display']->meta_refresh(3, $redirect_url);
            trigger_error($message);
        }
    }
    // Add Folder
    if (isset($_POST['addfolder'])) {
        $folder_name = request_var('foldername', '');
        if ($folder_name) {
            $sql = 'SELECT folder_name 
				FROM ' . FORUMS_PRIVMSGS_FOLDER_TABLE . "\n\t\t\t\tWHERE folder_name = '" . $_CLASS['core_db']->escape($folder_name) . "'\n\t\t\t\t\tAND user_id = " . $_CLASS['core_user']->data['user_id'];
            $result = $_CLASS['core_db']->query_limit($sql, 1);
            $row = $_CLASS['core_db']->fetch_row_assoc($result);
            $_CLASS['core_db']->free_result($result);
            if ($row) {
                trigger_error(sprintf($_CLASS['core_user']->lang['FOLDER_NAME_EXIST'], $folder_name));
            }
            $sql = 'SELECT COUNT(*) as num_folder
				FROM ' . FORUMS_PRIVMSGS_FOLDER_TABLE . '
					WHERE user_id = ' . $_CLASS['core_user']->data['user_id'];
            $result = $_CLASS['core_db']->query($sql);
            list($count) = $_CLASS['core_db']->fetch_row_num($result);
            $_CLASS['core_db']->free_result($result);
            if ($count >= $config['pm_max_boxes']) {
                trigger_error('MAX_FOLDER_REACHED');
            }
            $sql_array = array('user_id' => (int) $_CLASS['core_user']->data['user_id'], 'folder_name' => $folder_name, 'pm_count' => 0);
            $_CLASS['core_db']->query('INSERT INTO ' . FORUMS_PRIVMSGS_FOLDER_TABLE . ' ' . $_CLASS['core_db']->sql_build_array('INSERT', $sql_array));
            $message = $_CLASS['core_user']->lang['FOLDER_ADDED'] . '<br /><br />' . sprintf($_CLASS['core_user']->lang['RETURN_UCP'], '<a href="' . $redirect_url . '">', '</a>');
            $_CLASS['core_display']->meta_refresh(3, $redirect_url);
            trigger_error($message);
        }
    }
    // Rename folder
    if (isset($_POST['rename_folder'])) {
        $new_folder_name = request_var('new_folder_name', '');
        $rename_folder_id = request_var('rename_folder_id', 0);
        if (!$new_folder_name) {
            trigger_error('NO_NEW_FOLDER_NAME');
        }
        // Select custom folder
        $sql = 'SELECT folder_name, pm_count
			FROM ' . FORUMS_PRIVMSGS_FOLDER_TABLE . '
			WHERE user_id = ' . $_CLASS['core_user']->data['user_id'] . "\n\t\t\t\tAND folder_id = {$rename_folder_id}";
        $result = $_CLASS['core_db']->query_limit($sql, 1);
        $folder_row = $_CLASS['core_db']->fetch_row_assoc($result);
        $_CLASS['core_db']->free_result($result);
        if (!$folder_row) {
            trigger_error('CANNOT_RENAME_FOLDER');
        }
        $sql = 'SELECT folder_name 
			FROM ' . FORUMS_PRIVMSGS_FOLDER_TABLE . "\n\t\t\tWHERE folder_name = '" . $_CLASS['core_db']->escape($new_folder_name) . "'\n\t\t\t\tAND user_id = " . $_CLASS['core_user']->data['user_id'];
        $result = $_CLASS['core_db']->query_limit($sql, 1);
        $row = $_CLASS['core_db']->fetch_row_assoc($result);
        $_CLASS['core_db']->free_result($result);
        if ($row) {
            trigger_error(sprintf($_CLASS['core_user']->lang['FOLDER_NAME_EXIST'], $new_folder_name));
        }
        $sql = 'UPDATE ' . FORUMS_PRIVMSGS_FOLDER_TABLE . " \n\t\t\tSET folder_name = '" . $_CLASS['core_db']->escape($new_folder_name) . "'\n\t\t\tWHERE folder_id = {$rename_folder_id}\n\t\t\t\tAND user_id = " . $_CLASS['core_user']->data['user_id'];
        $_CLASS['core_db']->query($sql);
        $message = $_CLASS['core_user']->lang['FOLDER_RENAMED'] . '<br /><br />' . sprintf($_CLASS['core_user']->lang['RETURN_UCP'], '<a href="' . $redirect_url . '">', '</a>');
        $_CLASS['core_display']->meta_refresh(3, $redirect_url);
        trigger_error($message);
    }
    // Remove Folder
    if (isset($_POST['remove_folder'])) {
        $remove_folder_id = request_var('remove_folder_id', 0);
        // Default to "move all messages to inbox"
        $remove_action = request_var('remove_action', 1);
        $move_to = request_var('move_to', PRIVMSGS_INBOX);
        // Move to same folder?
        if ($remove_action == 1 && $remove_folder_id === $move_to) {
            trigger_error('CANNOT_MOVE_TO_SAME_FOLDER');
        }
        // Select custom folder
        $sql = 'SELECT folder_name, pm_count
			FROM ' . FORUMS_PRIVMSGS_FOLDER_TABLE . '
			WHERE user_id = ' . $_CLASS['core_user']->data['user_id'] . "\n\t\t\t\tAND folder_id = {$remove_folder_id}";
        $result = $_CLASS['core_db']->query($sql);
        $folder_row = $_CLASS['core_db']->fetch_row_assoc($result);
        $_CLASS['core_db']->free_result($result);
        if (!$folder_row) {
            trigger_error('CANNOT_REMOVE_FOLDER');
        }
        $hidden_fields = array('remove_folder_id' => $remove_folder_id, 'remove_folder' => 1, 'remove_action' => $remove_action, 'move_to' => $move_to);
        // Do we need to confirm?
        if (display_confirmation($_CLASS['core_user']->get_lang('REMOVE_FOLDER'), generate_hidden_fields($hidden_fields))) {
            // Gather message ids
            $sql = 'SELECT msg_id 
				FROM ' . FORUMS_PRIVMSGS_TO_TABLE . '
				WHERE user_id = ' . $_CLASS['core_user']->data['user_id'] . "\n\t\t\t\t\tAND folder_id = {$remove_folder_id}";
            $result = $_CLASS['core_db']->query($sql);
            $msg_ids = array();
            while ($row = $_CLASS['core_db']->fetch_row_assoc($result)) {
                $msg_ids[] = (int) $row['msg_id'];
            }
            $_CLASS['core_db']->free_result($result);
            // First of all, copy all messages to another folder... or delete all messages
            switch ($remove_action) {
                // Move Messages
                case 1:
                    $message_limit = $_CLASS['core_user']->data['user_message_limit'] ? $_CLASS['core_user']->data['user_message_limit'] : $config['pm_max_msgs'];
                    $num_moved = move_pm($_CLASS['core_user']->data['user_id'], $message_limit, $msg_ids, $move_to, $remove_folder_id);
                    // Something went wrong, only partially moved?
                    if ($num_moved != $folder_row['pm_count']) {
                        trigger_error(sprintf($_CLASS['core_user']->lang['MOVE_PM_ERROR'], $num_moved, $folder_row['pm_count']));
                    }
                    break;
                    // Remove Messages
                // Remove Messages
                case 2:
                    delete_pm($_CLASS['core_user']->data['user_id'], $msg_ids, $remove_folder_id);
                    break;
            }
            // Remove folder
            $sql = 'DELETE FROM ' . FORUMS_PRIVMSGS_FOLDER_TABLE . '
				WHERE user_id = ' . $_CLASS['core_user']->data['user_id'] . "\n\t\t\t\t\tAND folder_id = {$remove_folder_id}";
            $_CLASS['core_db']->query($sql);
            // Check full folder option. If the removed folder has been specified as destination switch back to inbox
            if ($_CLASS['core_user']->data['user_full_folder'] == $remove_folder_id) {
                $sql = 'UPDATE ' . USERS_TABLE . '
					SET user_full_folder = ' . PRIVMSGS_INBOX . '
					WHERE user_id = ' . $_CLASS['core_user']->data['user_id'];
                $_CLASS['core_db']->query($sql);
                $_CLASS['core_user']->data['user_full_folder'] = PRIVMSGS_INBOX;
            }
            $meta_info = generate_link('Control_Panel&amp;i=pm&amp;mode=' . $mode);
            $message = $_CLASS['core_user']->lang['FOLDER_REMOVED'];
            $_CLASS['core_display']->meta_refresh(3, $meta_info);
            $message .= '<br /><br />' . sprintf($_CLASS['core_user']->lang['RETURN_UCP'], '<a href="' . $meta_info . '">', '</a>');
            trigger_error($message);
        }
    }
    // Add Rule
    if (isset($_POST['add_rule'])) {
        $check_option = request_var('check_option', 0);
        $rule_option = request_var('rule_option', 0);
        $cond_option = request_var('cond_option', '');
        $action_option = explode('|', request_var('action_option', ''));
        $rule_string = $cond_option != 'none' ? request_var('rule_string', '') : '';
        $rule_user_id = $cond_option != 'none' ? request_var('rule_user_id', 0) : 0;
        $rule_group_id = $cond_option != 'none' ? request_var('rule_group_id', 0) : 0;
        $action = (int) $action_option[0];
        $folder_id = (int) $action_option[1];
        if (!$action || !$check_option || !$rule_option || !$cond_option || $cond_option != 'none' && !$rule_string) {
            trigger_error('RULE_NOT_DEFINED');
        }
        if ($cond_option == 'user' && !$rule_user_id || $cond_option == 'group' && !$rule_group_id) {
            trigger_error('RULE_NOT_DEFINED');
        }
        $rule_ary = array('user_id' => $_CLASS['core_user']->data['user_id'], 'rule_check' => $check_option, 'rule_connection' => $rule_option, 'rule_string' => $rule_string, 'rule_user_id' => $rule_user_id, 'rule_group_id' => $rule_group_id, 'rule_action' => $action, 'rule_folder_id' => $folder_id);
        $sql = 'SELECT rule_id 
			FROM ' . FORUMS_PRIVMSGS_RULES_TABLE . '
			WHERE ' . $_CLASS['core_db']->sql_build_array('SELECT', $rule_ary);
        $result = $_CLASS['core_db']->query($sql, 1);
        $row = $_CLASS['core_db']->fetch_row_assoc($result);
        $_CLASS['core_db']->free_result($result);
        if ($row) {
            trigger_error('RULE_ALREADY_DEFINED');
        }
        $sql = 'INSERT INTO ' . FORUMS_PRIVMSGS_RULES_TABLE . ' ' . $_CLASS['core_db']->sql_build_array('INSERT', $rule_ary);
        $_CLASS['core_db']->query($sql);
        $message = $_CLASS['core_user']->lang['RULE_ADDED'] . '<br /><br />' . sprintf($_CLASS['core_user']->lang['RETURN_UCP'], '<a href="' . $redirect_url . '">', '</a>');
        $_CLASS['core_display']->meta_refresh(3, $redirect_url);
        trigger_error($message);
    }
    // Remove Rule
    if (isset($_POST['delete_rule']) && !isset($_POST['cancel'])) {
        $delete_id = array_map('intval', array_keys($_POST['delete_rule']));
        $delete_id = (int) $delete_id[0];
        if (!$delete_id) {
            redirect(generate_link('Control_Panel&amp;i=pm&amp;mode=' . $mode));
        }
        $s_hidden_fields = '<input type="hidden" name="delete_rule[' . $delete_id . ']" value="1" />';
        // Do we need to confirm ?
        if (confirm_box(true)) {
            $sql = 'DELETE FROM ' . FORUMS_PRIVMSGS_RULES_TABLE . '
				WHERE user_id = ' . $_CLASS['core_user']->data['user_id'] . "\n\t\t\t\t\tAND rule_id = {$delete_id}";
            $_CLASS['core_db']->query($sql);
            $meta_info = generate_link("Control_Panel{$SID}&amp;i=pm&amp;mode={$mode}");
            $message = $_CLASS['core_user']->lang['RULE_DELETED'];
            $_CLASS['core_display']->meta_refresh(3, $meta_info);
            $message .= '<br /><br />' . sprintf($_CLASS['core_user']->lang['RETURN_UCP'], '<a href="' . $meta_info . '">', '</a>');
            trigger_error($message);
        } else {
            confirm_box(false, 'DELETE_RULE', $s_hidden_fields);
        }
    }
    $folder = array();
    $message_limit = $_CLASS['core_user']->data['user_message_limit'] ? $config['pm_max_msgs'] : $_CLASS['core_user']->data['user_message_limit'];
    $sql = 'SELECT COUNT(*) as num_messages
		FROM ' . FORUMS_PRIVMSGS_TO_TABLE . '
		WHERE user_id = ' . $_CLASS['core_user']->data['user_id'] . '
			AND folder_id = ' . PRIVMSGS_INBOX;
    $result = $_CLASS['core_db']->query($sql);
    list($num_messages) = $_CLASS['core_db']->fetch_row_num($result);
    $_CLASS['core_db']->free_result($result);
    $folder[PRIVMSGS_INBOX] = array('folder_name' => $_CLASS['core_user']->get_lang('PM_INBOX'), 'message_status' => sprintf($_CLASS['core_user']->lang['FOLDER_MESSAGE_STATUS'], $num_messages, $message_limit));
    $sql = 'SELECT folder_id, folder_name, pm_count 
		FROM ' . FORUMS_PRIVMSGS_FOLDER_TABLE . '
			WHERE user_id = ' . $_CLASS['core_user']->data['user_id'];
    $result = $_CLASS['core_db']->query($sql);
    $num_user_folder = 0;
    while ($row = $_CLASS['core_db']->fetch_row_assoc($result)) {
        $num_user_folder++;
        $folder[$row['folder_id']] = array('folder_name' => $row['folder_name'], 'message_status' => sprintf($_CLASS['core_user']->lang['FOLDER_MESSAGE_STATUS'], $row['pm_count'], $message_limit));
    }
    $_CLASS['core_db']->free_result($result);
    $s_full_folder_options = $s_to_folder_options = $s_folder_options = '';
    // temp
    $_CLASS['core_user']->data['user_full_folder'] = FULL_FOLDER_NONE;
    if ($_CLASS['core_user']->data['user_full_folder'] == FULL_FOLDER_NONE) {
        // -3 here to let the correct folder id be selected
        $to_folder_id = $config['full_folder_action'] - 3;
    } else {
        $to_folder_id = $_CLASS['core_user']->data['user_full_folder'];
    }
    foreach ($folder as $folder_id => $folder_ary) {
        $s_full_folder_options .= '<option value="' . $folder_id . '"' . ($_CLASS['core_user']->data['user_full_folder'] == $folder_id ? ' selected="selected"' : '') . '>' . $folder_ary['folder_name'] . ' (' . $folder_ary['message_status'] . ')</option>';
        //$s_to_folder_options .= '<option value="' . $folder_id . '"' . (($to_folder_id == $folder_id) ? ' selected="selected"' : '') . '>' . $folder_ary['folder_name'] . ' (' . $folder_ary['message_status'] . ')</option>';
        $s_to_folder_options .= '<option value="' . $folder_id . '">' . $folder_ary['folder_name'] . ' (' . $folder_ary['message_status'] . ')</option>';
        if ($folder_id != PRIVMSGS_INBOX) {
            $s_folder_options .= '<option value="' . $folder_id . '">' . $folder_ary['folder_name'] . ' (' . $folder_ary['message_status'] . ')</option>';
        }
    }
    $s_delete_checked = $_CLASS['core_user']->data['user_full_folder'] == FULL_FOLDER_DELETE ? ' checked="checked"' : '';
    $s_hold_checked = $_CLASS['core_user']->data['user_full_folder'] == FULL_FOLDER_HOLD ? ' checked="checked"' : '';
    $s_move_checked = $_CLASS['core_user']->data['user_full_folder'] >= 0 ? ' checked="checked"' : '';
    if ($_CLASS['core_user']->data['user_full_folder'] == FULL_FOLDER_NONE) {
        switch ($config['full_folder_action']) {
            case 1:
                $s_delete_checked = ' checked="checked"';
                break;
            case 2:
                $s_hold_checked = ' checked="checked"';
                break;
        }
    }
    $_CLASS['core_template']->assign_array(array('S_FULL_FOLDER_OPTIONS' => $s_full_folder_options, 'S_TO_FOLDER_OPTIONS' => $s_to_folder_options, 'S_FOLDER_OPTIONS' => $s_folder_options, 'S_DELETE_CHECKED' => $s_delete_checked, 'S_HOLD_CHECKED' => $s_hold_checked, 'S_MOVE_CHECKED' => $s_move_checked, 'S_MAX_FOLDER_REACHED' => $num_user_folder >= $config['pm_max_boxes'] ? true : false, 'DEFAULT_ACTION' => $config['full_folder_action'] == 1 ? $_CLASS['core_user']->lang['DELETE_OLDEST_MESSAGES'] : $_CLASS['core_user']->lang['HOLD_NEW_MESSAGES'], 'U_FIND_USERNAME' => generate_link('Members_List&amp;mode=searchuser&amp;form=ucp&amp;field=rule_string')));
    $rule_lang = $action_lang = $check_lang = array();
    // Build all three language arrays
    preg_replace('#^((RULE|ACTION|CHECK)_([A-Z0-9_]+))$#e', "\${strtolower('\\2') . '_lang'}[constant('\\1')] = \$_CLASS['core_user']->lang['PM_\\2']['\\3']", array_keys(get_defined_constants()));
    /*
    	Rule Ordering:
    		-> CHECK_* -> RULE_* [IN $global_privmsgs_rules:CHECK_*] -> [IF $rule_conditions[RULE_*] [|text|bool|user|group|own_group]] -> ACTION_*
    */
    $check_option = request_var('check_option', 0);
    $rule_option = request_var('rule_option', 0);
    $cond_option = request_var('cond_option', '');
    $action_option = request_var('action_option', '');
    $back = isset($_REQUEST['back']) ? request_var('back', '') : array();
    if (!empty($back)) {
        if ($action_option) {
            $action_option = '';
        } elseif ($cond_option) {
            $cond_option = '';
        } elseif ($rule_option) {
            $rule_option = 0;
        } elseif ($check_option) {
            $check_option = 0;
        }
    }
    if (isset($back['action']) && $cond_option == 'none') {
        $back['cond'] = true;
    }
    // Check
    define_check_option($check_option && !isset($back['rule']) ? true : false, $check_option, $check_lang);
    if ($check_option && !isset($back['rule'])) {
        define_rule_option($rule_option && !isset($back['cond']) ? true : false, $rule_option, $rule_lang, $global_privmsgs_rules[$check_option]);
    }
    if ($rule_option && !isset($back['cond'])) {
        if (!isset($global_rule_conditions[$rule_option])) {
            $cond_option = 'none';
            $_CLASS['core_template']->assign('NONE_CONDITION', true);
        } else {
            define_cond_option($cond_option && !isset($back['action']) ? true : false, $cond_option, $rule_option, $global_rule_conditions);
        }
    }
    if ($cond_option && !isset($back['action'])) {
        define_action_option(false, $action_option, $action_lang, $folder);
    }
    show_defined_rules($_CLASS['core_user']->data['user_id'], $check_lang, $rule_lang, $action_lang, $folder);
}
function display_confirmation($message = '', $hidden = '', $template = false, $image = false)
{
    global $_CLASS;
    // Add user entered confirmation code as a choose, maybe ...
    if (isset($_POST['cancel'])) {
        return false;
    }
    if (isset($_POST['confirm'])) {
        $code = $_CLASS['core_user']->session_data_get('confirmation_code');
        $confirm_code = get_variable('confirm_code', 'POST', false);
        if ($code && $confirm_code && $code === $confirm_code) {
            return true;
        }
        return false;
    }
    $confirmation_code = generate_string(6);
    if (is_array($hidden)) {
        $hidden = generate_hidden_fields($hidden);
    }
    if ($image) {
        $confirm_image = '<img src="' . generate_link('system&amp;mode=confirmation_image') . '" alt="" title="" />';
    } else {
        $confirm_image = false;
        $hidden .= '<input type="hidden" name="confirm_code" value="' . $confirmation_code . '" />';
    }
    $_CLASS['core_user']->session_data_set('confirmation_code', $confirmation_code);
    $_CLASS['core_template']->assign_array(array('MESSAGE' => $message ? $message : 'Are you sure you want to perform this action ?', 'CONFIRM_ACTION' => $_CLASS['core_user']->url ? generate_link($_CLASS['core_user']->url) : '', 'CONFIRM_IMAGE' => $confirm_image, 'HIDDEN_FIELDS' => $hidden));
    $_CLASS['core_template']->display($template ? $template : 'confirmation.html');
    script_close(false);
}
if ($submit || $action === 'remove') {
    switch ($action) {
        case 'remove':
            if (!$role_id) {
                trigger_error($_CLASS['core_user']->lang['NO_ROLE_SELECTED'] . adm_back_link(generate_link($u_action, array('admin' => true))), E_USER_WARNING);
            }
            $sql = 'SELECT *
				FROM ' . FORUMS_ACL_ROLES_TABLE . '
				WHERE role_id = ' . $role_id;
            $result = $_CLASS['core_db']->query($sql);
            $role_row = $_CLASS['core_db']->fetch_row_assoc($result);
            $_CLASS['core_db']->free_result($result);
            if (!$role_row) {
                trigger_error($_CLASS['core_user']->lang['NO_ROLE_SELECTED'] . adm_back_link(generate_link($u_action, array('admin' => true))), E_USER_WARNING);
            }
            $hidden_fields = generate_hidden_fields(array('i' => $id, 'mode' => $mode, 'role_id' => $role_id, 'action' => $action));
            if (display_confirmation('DELETE_ROLE', $hidden_fields)) {
                remove_role($role_id, $permission_type);
                add_log('admin', 'LOG_' . strtoupper($permission_type) . 'ROLE_REMOVED', $role_row['role_name']);
                trigger_error($_CLASS['core_user']->lang['ROLE_DELETED'] . adm_back_link(generate_link($u_action, array('admin' => true))));
            }
            break;
        case 'edit':
            if (!$role_id) {
                trigger_error($_CLASS['core_user']->lang['NO_ROLE_SELECTED'] . adm_back_link(generate_link($u_action, array('admin' => true))), E_USER_WARNING);
            }
            // Get role we edit
            $sql = 'SELECT *
				FROM ' . FORUMS_ACL_ROLES_TABLE . '
				WHERE role_id = ' . $role_id;
            $result = $_CLASS['core_db']->query($sql);
            }
        }
        if (!isset($bday_day)) {
            if ($_CLASS['core_user']->data['user_birthday']) {
                list($bday_day, $bday_month, $bday_year) = explode('-', $_CLASS['core_user']->data['user_birthday']);
            } else {
                $bday_day = $bday_month = $bday_year = '';
            }
        }
        $s_birthday_day_options = '<option value="0"' . (!$bday_day ? ' selected="selected"' : '') . '>--</option>';
        for ($i = 1; $i < 32; $i++) {
            $selected = $i == $bday_day ? ' selected="selected"' : '';
            $s_birthday_day_options .= "<option value=\"{$i}\"{$selected}>{$i}</option>";
        }
        $s_birthday_month_options = '<option value="0"' . (!$bday_month ? ' selected="selected"' : '') . '>--</option>';
        for ($i = 1; $i < 13; $i++) {
            $selected = $i == $bday_month ? ' selected="selected"' : '';
            $s_birthday_month_options .= "<option value=\"{$i}\"{$selected}>{$i}</option>";
        }
        $s_birthday_year_options = '';
        $s_birthday_year_options = '<option value="0"' . (!$bday_year ? ' selected="selected"' : '') . '>--</option>';
        $i = $this_year - 100;
        for ($i; $i < $this_year; $i++) {
            $selected = $i == $bday_year ? ' selected="selected"' : '';
            $s_birthday_year_options .= "<option value=\"{$i}\"{$selected}>{$i}</option>";
        }
        $_CLASS['core_template']->assign_array(array('ERROR' => empty($error) ? '' : implode('<br />', $error), 'ICQ' => isset($icq) ? $icq : $_CLASS['core_user']->data['user_icq'], 'YIM' => isset($yim) ? $yim : $_CLASS['core_user']->data['user_yim'], 'AIM' => isset($aim) ? $aim : $_CLASS['core_user']->data['user_aim'], 'MSN' => isset($msn) ? $msn : $_CLASS['core_user']->data['user_msnm'], 'JABBER' => isset($jabber) ? $jabber : $_CLASS['core_user']->data['user_jabber'], 'WEBSITE' => isset($website) ? $website : $_CLASS['core_user']->data['user_website'], 'LOCATION' => isset($location) ? $location : $_CLASS['core_user']->data['user_from'], 'OCCUPATION' => isset($occupation) ? $occupation : $_CLASS['core_user']->data['user_occ'], 'INTERESTS' => isset($interests) ? $interests : $_CLASS['core_user']->data['user_interests'], 'S_BIRTHDAY_DAY_OPTIONS' => $s_birthday_day_options, 'S_BIRTHDAY_MONTH_OPTIONS' => $s_birthday_month_options, 'S_BIRTHDAY_YEAR_OPTIONS' => $s_birthday_year_options));
        break;
}
$_CLASS['core_template']->assign_array(array('L_TITLE' => $_CLASS['core_user']->lang['UCP_PROFILE_' . strtoupper($this->mode)], 'S_HIDDEN_FIELDS' => generate_hidden_fields($hidden_fields), 'S_UCP_ACTION' => generate_link($this->link)));
$_CLASS['core_display']->display($_CLASS['core_user']->lang['UCP_PROFILE'], 'modules/control_panel/ucp_profile_' . $this->mode . '.html');
    function ucp_main($id, $mode)
    {
        global $config, $_CLASS, $site_file_root, $_CORE_CONFIG;
        $_CLASS['core_template']->assign_array(array('ERROR' => false, 'topicrow' => false, 'WARNINGS' => false, 'draftrow' => false));
        $_CLASS['core_user']->user_setup();
        switch ($mode) {
            case 'front':
                $_CLASS['core_user']->add_lang(false, 'Members_List');
                if ($config['load_db_lastread'] || $config['load_db_track']) {
                    if ($config['load_db_lastread']) {
                        $sql = 'SELECT mark_time 
							FROM ' . FORUMS_TRACK_TABLE . ' 
							WHERE forum_id = 0
								AND user_id = ' . $_CLASS['core_user']->data['user_id'];
                        $result = $_CLASS['core_db']->query($sql);
                        $track_data = $_CLASS['core_db']->fetch_row_assoc($result);
                        $_CLASS['core_db']->free_result($result);
                    }
                    $sql_from = FORUMS_TOPICS_TABLE . ' t LEFT JOIN ' . FORUMS_TRACK_TABLE . ' tt ON (tt.topic_id = t.topic_id AND tt.user_id = ' . $_CLASS['core_user']->data['user_id'] . ')';
                    $sql_select = ', tt.mark_time';
                } else {
                    $sql_from = TOPICS_TABLE . ' t ';
                    $sql_select = '';
                }
                // Has to be in while loop if we not only check forum id 0
                if ($config['load_db_lastread']) {
                    $forum_check = $track_data['mark_time'];
                } else {
                    $tracking_topics = isset($_COOKIE[$_CORE_CONFIG['server']['cookie_name'] . '_track']) ? unserialize(stripslashes($_COOKIE[$_CORE_CONFIG['server']['cookie_name'] . '_track'])) : array();
                    $forum_check = isset($tracking_topics[0][0]) ? base_convert($tracking_topics[0][0], 36, 10) + $config['board_startdate'] : 0;
                }
                $topic_type = $_CLASS['core_user']->lang['VIEW_TOPIC_ANNOUNCEMENT'];
                $folder = 'folder_announce';
                $folder_new = $folder . '_new';
                $sql = "SELECT t.* {$sql_select} \n\t\t\t\t\tFROM {$sql_from}\n\t\t\t\t\tWHERE t.forum_id = 0\n\t\t\t\t\t\tAND t.topic_type = " . POST_GLOBAL . '
					ORDER BY t.topic_last_post_time DESC';
                $result = $_CLASS['core_db']->query($sql);
                while ($row = $_CLASS['core_db']->fetch_row_assoc($result)) {
                    $forum_id = $row['forum_id'];
                    $topic_id = $row['topic_id'];
                    if ($row['topic_status'] == ITEM_LOCKED) {
                        $topic_type = $_CLASS['core_user']->lang['VIEW_TOPIC_LOCKED'];
                        $folder = 'folder_locked';
                        $folder_new = 'folder_locked_new';
                    }
                    $unread_topic = true;
                    if ($config['load_db_lastread']) {
                        $topic_check = $row['mark_time'];
                    } else {
                        $topic_id36 = base_convert($topic_id, 10, 36);
                        $topic_check = isset($tracking_topics[0][$topic_id36]) ? base_convert($tracking_topics[0][$topic_id36], 36, 10) + $config['board_startdate'] : 0;
                    }
                    if ($topic_check >= $row['topic_last_post_time'] || $forum_check >= $row['topic_last_post_time']) {
                        $unread_topic = false;
                    }
                    $newest_post_img = $unread_topic ? '<a href="' . generate_link("Forums&amp;file=viewtopic&amp;t={$topic_id}&amp;view=unread#unread") . '">' . $_CLASS['core_user']->img('icon_post_newest', 'VIEW_NEWEST_POST') . '</a> ' : '';
                    $folder_img = $unread_topic ? $folder_new : $folder;
                    $folder_alt = $unread_topic ? 'NEW_POSTS' : ($row['topic_status'] == ITEM_LOCKED ? 'TOPIC_LOCKED' : 'NO_NEW_POSTS');
                    // Posted image?
                    $view_topic_url = generate_link("Forums&amp;file=viewtopic&amp;&amp;t={$topic_id}");
                    $last_post_img = '<a href="' . generate_link("Forums&amp;file=viewtopic&amp;t={$topic_id}&amp;p=" . $row['topic_last_post_id'] . '#' . $row['topic_last_post_id']) . '">' . $_CLASS['core_user']->img('icon_post_latest', 'VIEW_LATEST_POST') . '</a>';
                    $_CLASS['core_template']->assign_vars_array('topicrow', array('FORUM_ID' => $forum_id, 'TOPIC_ID' => $topic_id, 'GOTO_PAGE' => '', 'LAST_POST_TIME' => $_CLASS['core_user']->format_date($row['topic_last_post_time']), 'LAST_POST_AUTHOR' => $row['topic_last_poster_name'] ? $row['topic_last_poster_name'] : $_CLASS['core_user']->lang['GUEST'], 'TOPIC_TITLE' => censor_text($row['topic_title']), 'TOPIC_TYPE' => $topic_type, 'LAST_POST_IMG' => $last_post_img, 'NEWEST_POST_IMG' => $newest_post_img, 'TOPIC_FOLDER_IMG' => $_CLASS['core_user']->img($folder_img, $folder_alt), 'ATTACH_ICON_IMG' => $_CLASS['auth']->acl_gets('f_download', 'u_download', $forum_id) && $row['topic_attachment'] ? $_CLASS['core_user']->img('icon_attach', '') : '', 'U_LAST_POST_AUTHOR' => $row['topic_last_poster_id'] != ANONYMOUS ? generate_link('Members_List&amp;mode=viewprofile&amp;u=' . $row['topic_last_poster_id']) : false, 'U_VIEW_TOPIC' => $view_topic_url));
                }
                $_CLASS['core_db']->free_result($result);
                $num_real_posts = $_CLASS['core_user']->data['user_posts'];
                $active_f_row = $active_t_row = array();
                // Do the relevant calculations
                $memberdays = max(1, round(($_CLASS['core_user']->time - $_CLASS['core_user']->data['user_reg_date']) / 86400));
                $posts_per_day = $_CLASS['core_user']->data['user_posts'] / $memberdays;
                $percentage = $config['num_posts'] ? min(100, $num_real_posts / $config['num_posts'] * 100) : 0;
                $active_f_name = $active_f_id = $active_f_count = $active_f_pct = '';
                if (!empty($active_f_row['num_posts'])) {
                    $active_f_name = $active_f_row['forum_name'];
                    $active_f_id = $active_f_row['forum_id'];
                    $active_f_count = $active_f_row['num_posts'];
                    $active_f_pct = $_CLASS['core_user']->data['user_posts'] ? $active_f_count / $_CLASS['core_user']->data['user_posts'] * 100 : 0;
                }
                unset($active_f_row);
                $active_t_name = $active_t_id = $active_t_count = $active_t_pct = '';
                if (!empty($active_t_row['num_posts'])) {
                    $active_t_name = $active_t_row['topic_title'];
                    $active_t_id = $active_t_row['topic_id'];
                    $active_t_count = $active_t_row['num_posts'];
                    $active_t_pct = $_CLASS['core_user']->data['user_posts'] ? $active_t_count / $_CLASS['core_user']->data['user_posts'] * 100 : 0;
                }
                unset($active_t_row);
                $_CLASS['core_template']->assign_array(array('USER_COLOR' => !empty($_CLASS['core_user']->data['user_colour']) ? $_CLASS['core_user']->data['user_colour'] : '', 'JOINED' => $_CLASS['core_user']->format_date($_CLASS['core_user']->data['user_reg_date']), 'VISITED' => empty($_CLASS['core_user']->data['user_lastvisit']) ? ' - ' : $_CLASS['core_user']->format_date($_CLASS['core_user']->data['user_lastvisit']), 'POSTS' => $_CLASS['core_user']->data['user_posts'] ? $_CLASS['core_user']->data['user_posts'] : 0, 'POSTS_DAY' => sprintf($_CLASS['core_user']->lang['POST_DAY'], $posts_per_day), 'POSTS_PCT' => sprintf($_CLASS['core_user']->lang['POST_PCT'], $percentage), 'ACTIVE_FORUM' => $active_f_name, 'ACTIVE_FORUM_POSTS' => $active_f_count == 1 ? sprintf($_CLASS['core_user']->lang['USER_POST'], 1) : sprintf($_CLASS['core_user']->lang['USER_POSTS'], $active_f_count), 'ACTIVE_FORUM_PCT' => sprintf($_CLASS['core_user']->lang['POST_PCT'], $active_f_pct), 'ACTIVE_TOPIC' => $active_t_name, 'ACTIVE_TOPIC_POSTS' => $active_t_count == 1 ? sprintf($_CLASS['core_user']->lang['USER_POST'], 1) : sprintf($_CLASS['core_user']->lang['USER_POSTS'], $active_t_count), 'ACTIVE_TOPIC_PCT' => sprintf($_CLASS['core_user']->lang['POST_PCT'], $active_t_pct), 'OCCUPATION' => !empty($row['user_occ']) ? $row['user_occ'] : '', 'INTERESTS' => !empty($row['user_interests']) ? $row['user_interests'] : '', 'U_SEARCH_USER' => $_CLASS['auth']->acl_get('u_search') ? generate_link('Forums&amp;file=search&amp;search_author=' . urlencode($_CLASS['core_user']->data['username']) . "&amp;show_results=posts") : '', 'U_ACTIVE_FORUM' => generate_link('Forums&amp;file=viewforum&amp;f=' . $active_f_id), 'U_LAST_POST_AUTHOR' => $row['topic_last_poster_id'] != ANONYMOUS ? generate_link('Members_List&amp;mode=viewprofile&amp;u=' . $row['topic_last_poster_id']) : false, 'U_ACTIVE_TOPIC' => generate_link('Forums&amp;file=viewtopic&amp;t=' . $active_t_id)));
                break;
            case 'subscribed':
                require $site_file_root . 'includes/forums/functions_display.php';
                $unwatch = isset($_POST['unwatch']);
                if ($unwatch) {
                    $forums = array_unique(get_variable('f', 'POST', array(), 'array:int'));
                    $topics = array_unique(get_variable('t', 'POST', array(), 'array:int'));
                    if (!empty($forums) || !empty($topics)) {
                        $l_unwatch = '';
                        if (!empty($forums)) {
                            $sql = 'DELETE FROM ' . FORUMS_WATCH_TABLE . '
								WHERE forum_id IN (' . implode(', ', $forums) . ') AND topic_id = 0
									AND user_id = ' . $_CLASS['core_user']->data['user_id'];
                            $_CLASS['core_db']->query($sql);
                            $l_unwatch .= '_FORUMS';
                        }
                        if (!empty($topics)) {
                            $sql = 'DELETE FROM ' . FORUMS_WATCH_TABLE . '
								WHERE topic_id IN (' . implode(', ', $topics) . ')
									AND user_id = ' . $_CLASS['core_user']->data['user_id'];
                            $_CLASS['core_db']->query($sql);
                            $l_unwatch .= '_TOPICS';
                        }
                        $message = $_CLASS['core_user']->lang['UNWATCHED' . $l_unwatch] . '<br /><br />' . sprintf($_CLASS['core_user']->lang['RETURN_UCP'], '<a href="' . generate_link("Control_Panel&amp;i={$id}&amp;mode=subscribed") . '">', '</a>');
                        $_CLASS['core_display']->meta_refresh(3, generate_link("Control_Panel&amp;i={$id}&amp;mode=subscribed"));
                        trigger_error($message);
                    }
                }
                if ($config['load_db_lastread']) {
                    $sql_from = FORUMS_FORUMS_TABLE . ' f  LEFT JOIN ' . FORUMS_TRACK_TABLE . ' ft ON (ft.user_id = ' . $_CLASS['core_user']->data['user_id'] . ' 
									AND ft.forum_id = f.forum_id AND ft.topic_id = 0)';
                    $lastread_select = ', ft.mark_time ';
                } else {
                    $sql_from = FORUMS_FORUMS_TABLE . ' f ';
                    $lastread_select = '';
                    $tracking = @unserialize(get_variable($_CORE_CONFIG['server']['cookie_name'] . '_track', 'COOKIE'));
                    if (!is_array($tracking)) {
                        $tracking = array();
                    }
                }
                $sql = "SELECT f.*{$lastread_select} \n\t\t\t\t\tFROM {$sql_from}, " . FORUMS_WATCH_TABLE . ' fw
					WHERE fw.user_id = ' . $_CLASS['core_user']->data['user_id'] . ' 
						 AND fw.topic_id = 0 AND f.forum_id = fw.forum_id
					ORDER BY left_id';
                $result = $_CLASS['core_db']->query($sql);
                //$topics_count = $_CLASS['core_db']->num_rows($result);
                while ($row = $_CLASS['core_db']->fetch_row_assoc($result)) {
                    $forum_id = (int) $row['forum_id'];
                    $unread_forum = false;
                    if ($config['load_db_lastread']) {
                        $mark_time_forum = $row['mark_time'];
                    } else {
                        $forum_id36 = base_convert($forum_id, 10, 36);
                        $mark_time_forum = isset($tracking[$forum_id36][0]) ? (int) base_convert($tracking[$forum_id36][0], 36, 10) : 0;
                    }
                    if ($mark_time_forum < $row['forum_last_post_time']) {
                        $unread_forum = true;
                    }
                    // Which folder should we display?
                    if ($row['forum_status'] == ITEM_LOCKED) {
                        $folder_image = $unread_forum ? 'folder_locked_new' : 'folder_locked';
                        $folder_alt = 'FORUM_LOCKED';
                    } else {
                        $folder_image = $unread_forum ? 'folder_new' : 'folder';
                        $folder_alt = $unread_forum ? 'NEW_POSTS' : 'NO_NEW_POSTS';
                    }
                    // Create last post link information, if appropriate
                    if ($row['forum_last_post_id']) {
                        $last_post_time = $_CLASS['core_user']->format_date($row['forum_last_post_time']);
                        $last_poster = $row['forum_last_poster_name'] != '' ? $row['forum_last_poster_name'] : $_CLASS['core_user']->lang['GUEST'];
                        $last_poster_url = $row['forum_last_poster_id'] == ANONYMOUS ? '' : generate_link('Members_List&amp;mode=viewprofile&amp;u=' . $row['forum_last_poster_id']);
                        $last_post_url = generate_link("Forums&amp;file=viewtopic&amp;f={$forum_id}&amp;p=" . $row['forum_last_post_id'] . '#' . $row['forum_last_post_id']);
                    } else {
                        $last_post_time = $last_poster = $last_poster_url = $last_post_url = '';
                    }
                    $_CLASS['core_template']->assign_vars_array('forumrow', array('FORUM_ID' => $forum_id, 'FORUM_FOLDER_IMG' => $_CLASS['core_user']->img($folder_image, $folder_alt), 'FORUM_NAME' => $row['forum_name'], 'LAST_POST_IMG' => $_CLASS['core_user']->img('icon_post_latest', 'VIEW_LATEST_POST'), 'LAST_POST_TIME' => $last_post_time, 'LAST_POST_AUTHOR' => $last_poster, 'U_LAST_POST_AUTHOR' => $last_poster_url, 'U_LAST_POST' => $last_post_url, 'U_VIEWFORUM' => generate_link('Forums&amp;file=viewforum&amp;f=' . $row['forum_id'])));
                }
                $_CLASS['core_db']->free_result($result);
                // Subscribed Topics
                $start = get_variable('start', 'REQUEST', 0, 'int');
                if ($config['load_db_lastread']) {
                    $sql_from = FORUMS_TOPICS_TABLE . ' t LEFT JOIN ' . FORUMS_TRACK_TABLE . ' tt ON (tt.topic_id = t.topic_id AND tt.user_id = ' . $_CLASS['core_user']->data['user_id'] . ')';
                    $sql_t_select = ', tt.mark_time';
                } else {
                    $sql_from = FORUMS_TOPICS_TABLE . ' t';
                    $sql_t_select = '';
                }
                $sql = "SELECT t.* {$sql_t_select} \n\t\t\t\t\tFROM " . FORUMS_WATCH_TABLE . " tw, {$sql_from} \n\t\t\t\t\tWHERE tw.user_id = " . $_CLASS['core_user']->data['user_id'] . '
						AND t.topic_id = tw.topic_id 
					ORDER BY t.topic_last_post_time DESC';
                $result = $_CLASS['core_db']->query_limit($sql, $config['topics_per_page'], $start);
                $topics_count = $_CLASS['core_db']->num_rows($result);
                if ($topics_count) {
                    $pagination = generate_pagination("Control_Panel&amp;i={$id}&amp;mode={$mode}", $topics_count, $config['topics_per_page'], $start);
                    $_CLASS['core_template']->assign_array(array('PAGINATION' => $pagination['formated'], 'PAGINATION_ARRAY' => $pagination['array'], 'PAGE_NUMBER' => on_page($topics_count, $config['topics_per_page'], $start), 'TOTAL_TOPICS' => $topics_count === 1 ? $_CLASS['core_user']->lang['VIEW_FORUM_TOPIC'] : sprintf($_CLASS['core_user']->lang['VIEW_FORUM_TOPICS'], $topics_count)));
                } else {
                    $_CLASS['core_template']->assign('TOTAL_TOPICS', false);
                }
                while ($row = $_CLASS['core_db']->fetch_row_assoc($result)) {
                    $topic_id = $row['topic_id'];
                    $forum_id = $row['forum_id'];
                    if (!$config['load_db_lastread']) {
                        $topic_id36 = base_convert($topic_id, 10, 36);
                        $forum_id36 = $row['topic_type'] == POST_GLOBAL ? 0 : base_convert($forum_id, 10, 36);
                        $mark_time_topic = isset($tracking[$forum_id36][$topic_id36]) ? (int) base_convert($tracking[$forum_id36][$topic_id36], 36, 10) : 0;
                        $mark_time_forum = isset($tracking[$forum_id36][0]) ? (int) base_convert($tracking[$forum_id36][0], 36, 10) : 0;
                        $row['mark_time'] = max($mark_time_topic, $mark_time_forum);
                    }
                    // Replies
                    $replies = $_CLASS['auth']->acl_get('m_approve', $forum_id) ? $row['topic_replies_real'] : $row['topic_replies'];
                    if ($row['topic_status'] == ITEM_MOVED) {
                        $topic_id = $row['topic_moved_id'];
                    }
                    // Get folder img, topic status/type related informations
                    $folder_img = $folder_alt = $topic_type = '';
                    topic_status($row, $replies, $row['mark_time'], $unread_topic, $folder_img, $folder_alt, $topic_type);
                    $newest_post_img = $unread_topic ? '<a href="' . generate_link("Forums&amp;file=viewtopic&amp;f={$forum_id}&amp;t={$topic_id}&amp;view=unread#unread") . '">' . $_CLASS['core_user']->img('icon_post_newest', 'VIEW_NEWEST_POST') . '</a> ' : '';
                    $view_topic_url = 'Forums&amp;file=viewtopic&amp;t=' . $topic_id;
                    $pagination = generate_pagination($view_topic_url, $replies, $config['topics_per_page'], 0);
                    $_CLASS['core_template']->assign_vars_array('topicrow', array('FORUM_ID' => $forum_id, 'TOPIC_ID' => $topic_id, 'TOPIC_AUTHOR' => $row['topic_poster'] == ANONYMOUS ? $row['topic_first_poster_name'] ? $row['topic_first_poster_name'] : $_CLASS['core_user']->get_lang('GUEST') : $row['topic_first_poster_name'], 'LINK_AUTHOR' => $row['topic_poster'] == ANONYMOUS ? '' : generate_link('Members_List&amp;mode=viewprofile&amp;u=' . $row['topic_poster']), 'FIRST_POST_TIME' => $_CLASS['core_user']->format_date($row['topic_time']), 'LAST_POST_TIME' => $_CLASS['core_user']->format_date($row['topic_last_post_time']), 'LAST_VIEW_TIME' => $_CLASS['core_user']->format_date($row['topic_last_view_time']), 'LAST_POST_AUTHOR' => $row['topic_last_poster_name'] ? $row['topic_last_poster_name'] : $_CLASS['core_user']->lang['GUEST'], 'PAGINATION' => $pagination['formated'], 'PAGINATION_ARRAY' => $pagination['array'], 'REPLIES' => $replies, 'VIEWS' => $row['topic_views'], 'TOPIC_TITLE' => censor_text($row['topic_title']), 'TOPIC_TYPE' => $topic_type, 'LAST_POST_IMG' => $_CLASS['core_user']->img('icon_post_latest', 'VIEW_LATEST_POST'), 'NEWEST_POST_IMG' => $newest_post_img, 'TOPIC_FOLDER_IMG' => $_CLASS['core_user']->img($folder_img, $folder_alt), 'TOPIC_ICON_IMG' => empty($icons[$row['icon_id']]) ? '' : '<img src="' . $config['icons_path'] . '/' . $icons[$row['icon_id']]['img'] . '" width="' . $icons[$row['icon_id']]['width'] . '" height="' . $icons[$row['icon_id']]['height'] . '" alt="" title="" />', 'ATTACH_ICON_IMG' => $_CLASS['auth']->acl_gets('f_download', 'u_download', $forum_id) && $row['topic_attachment'] ? $_CLASS['core_user']->img('icon_attach', sprintf($_CLASS['core_user']->lang['TOTAL_ATTACHMENTS'], $row['topic_attachment'])) : '', 'S_TOPIC_TYPE' => $row['topic_type'], 'S_UNREAD_TOPIC' => $unread_topic, 'U_LAST_POST' => generate_link($view_topic_url . '&amp;p=' . $row['topic_last_post_id'] . '#' . $row['topic_last_post_id']), 'U_LAST_POST_AUTHOR' => $row['topic_last_poster_id'] && $row['topic_last_poster_id'] != ANONYMOUS ? generate_link('Members_List&amp;mode=viewprofile&amp;u=' . $row['topic_last_poster_id']) : '', 'U_VIEW_TOPIC' => generate_link($view_topic_url)));
                }
                $_CLASS['core_db']->free_result($result);
                break;
            case 'bookmarks':
                require $site_file_root . 'includes/forums/functions_display.php';
                $move_up = request_var('move_up', 0);
                $move_down = request_var('move_down', 0);
                $sql = 'SELECT MAX(order_id) as max_order_id FROM ' . FORUMS_BOOKMARKS_TABLE . '
					WHERE user_id = ' . $_CLASS['core_user']->data['user_id'];
                $result = $_CLASS['core_db']->query($sql);
                list($max_order_id) = $_CLASS['core_db']->fetch_row_num($result);
                $_CLASS['core_db']->free_result($result);
                if ($move_up || $move_down) {
                    if ($move_up && $move_up != 1 || $move_down && $move_down != $max_order_id) {
                        $order = $move_up ? $move_up : $move_down;
                        $order_total = $order * 2 + ($move_up ? -1 : 1);
                        $sql = 'UPDATE ' . FORUMS_BOOKMARKS_TABLE . "\n\t\t\t\t\t\t\tSET order_id = {$order_total} - order_id\n\t\t\t\t\t\t\tWHERE order_id IN ({$order}, " . ($move_up ? $order - 1 : $order + 1) . ')
								AND user_id = ' . $_CLASS['core_user']->data['user_id'];
                        $_CLASS['core_db']->query($sql);
                    }
                }
                if (isset($_POST['unbookmark'])) {
                    $topics = array_unique(get_variable('t', 'POST', array(), 'array:int'));
                    if (empty($topics)) {
                        trigger_error('NO_BOOKMARKS_SELECTED');
                    }
                    $hidden_fields = array('unbookmark' => 1, 't' => $topics);
                    if (display_confirmation($_CLASS['core_user']->get_lang('REMOVE_SELECTED_BOOKMARKS'), generate_hidden_fields($hidden_fields))) {
                        $sql = 'DELETE FROM ' . FORUMS_BOOKMARKS_TABLE . '
							WHERE user_id = ' . $_CLASS['core_user']->data['user_id'] . '
								AND topic_id IN (' . implode(', ', $topics) . ')';
                        $_CLASS['core_db']->query($sql);
                        $sql = 'SELECT topic_id FROM ' . FORUMS_BOOKMARKS_TABLE . '
							WHERE user_id = ' . $_CLASS['core_user']->data['user_id'] . '
							ORDER BY order_id ASC';
                        $result = $_CLASS['core_db']->query($sql);
                        $i = 1;
                        while ($row = $_CLASS['core_db']->fetch_row_assoc($result)) {
                            $_CLASS['core_db']->query('UPDATE ' . FORUMS_BOOKMARKS_TABLE . "\n\t\t\t\t\t\t\t\tSET order_id = '{$i}'\n\t\t\t\t\t\t\t\tWHERE topic_id = '{$row['topic_id']}'\n\t\t\t\t\t\t\t\t\tAND user_id = '{$_CLASS['core_user']->data['user_id']}'");
                            $i++;
                        }
                        $_CLASS['core_db']->free_result($result);
                        $url = generate_link('Control_Panel&amp;i=main&amp;mode=bookmarks');
                        $_CLASS['core_display']->meta_refresh(3, $url);
                        $message = $_CLASS['core_user']->lang['BOOKMARKS_REMOVED'] . '<br /><br />' . sprintf($_CLASS['core_user']->lang['RETURN_UCP'], '<a href="' . $url . '">', '</a>');
                        trigger_error($message);
                    }
                }
                // We grab deleted topics here too...
                // NOTE: At the moment bookmarks are not removed with topics, might be useful later (not really sure how though. :D)
                // But since bookmarks are sensible to the user, they should not be deleted without notice.
                $sql = 'SELECT b.order_id, b.topic_id as b_topic_id, t.*, f.forum_name
					FROM ' . FORUMS_BOOKMARKS_TABLE . ' b
						LEFT JOIN ' . FORUMS_TOPICS_TABLE . ' t ON b.topic_id = t.topic_id
						LEFT JOIN ' . FORUMS_FORUMS_TABLE . ' f ON t.forum_id = f.forum_id
					WHERE b.user_id = ' . $_CLASS['core_user']->data['user_id'] . '
					ORDER BY b.order_id ASC';
                $result = $_CLASS['core_db']->query($sql);
                if (!($row = $_CLASS['core_db']->fetch_row_assoc($result))) {
                    $_CLASS['core_db']->free_result($result);
                    $_CLASS['core_template']->assign_array(array('S_BOOKMARKS' => false, 'S_BOOKMARKS_DISABLED' => false));
                    break;
                }
                $bookmarks = true;
                do {
                    $forum_id = $row['forum_id'];
                    $topic_id = $row['b_topic_id'];
                    $bookmarks = true;
                    $replies = $_CLASS['auth']->acl_get('m_approve', $forum_id) ? $row['topic_replies_real'] : $row['topic_replies'];
                    // Get folder img, topic status/type related informations
                    $folder_img = $folder_alt = $topic_type = '';
                    topic_status($row, $replies, $_CLASS['core_user']->time, $unread_topic, $folder_img, $folder_alt, $topic_type);
                    $view_topic_url = "Forums&amp;file=viewtopic&amp;t={$topic_id}";
                    //					$last_post_img = '<a href="'.generate_link("Forums&amp;file=viewtopic&amp;f=$forum_id&amp;p=" . $row['topic_last_post_id'] . '#' . $row['topic_last_post_id']) . '">' . $_CLASS['core_user']->img('icon_post_latest', 'VIEW_LATEST_POST') . '</a>';
                    $pagination = generate_pagination('Forums&amp;file=viewtopic&amp;t=' . $topic_id, $replies, $config['posts_per_page'], 0);
                    $_CLASS['core_template']->assign_vars_array('forummarks', array('FORUM_ID' => $forum_id, 'TOPIC_ID' => $topic_id, 'S_DELETED_TOPIC' => !$row['topic_id'] ? true : false, 'TOPIC_TITLE' => censor_text($row['topic_title']), 'TOPIC_TYPE' => $topic_type, 'FORUM_NAME' => $row['forum_name'], 'TOPIC_AUTHOR' => $row['topic_poster'] == ANONYMOUS ? $row['topic_first_poster_name'] ? $row['topic_first_poster_name'] : $_CLASS['core_user']->get_lang('GUEST') : $row['topic_first_poster_name'], 'LINK_AUTHOR' => $row['topic_poster'] == ANONYMOUS ? '' : generate_link('Members_List&amp;mode=viewprofile&amp;u=' . $row['topic_poster']), 'FIRST_POST_TIME' => $_CLASS['core_user']->format_date($row['topic_time']), 'LAST_POST_TIME' => $_CLASS['core_user']->format_date($row['topic_last_post_time']), 'LAST_VIEW_TIME' => $_CLASS['core_user']->format_date($row['topic_last_view_time']), 'LAST_POST_AUTHOR' => $row['topic_last_poster_name'] != '' ? $row['topic_last_poster_name'] : $_CLASS['core_user']->lang['GUEST'], 'LAST_POST_IMG' => $_CLASS['core_user']->img('icon_post_latest', 'VIEW_LATEST_POST'), 'PAGINATION' => $pagination['formated'], 'PAGINATION_ARRAY' => $pagination['array'], 'POSTED_AT' => $_CLASS['core_user']->format_date($row['topic_time']), 'TOPIC_FOLDER_IMG' => $_CLASS['core_user']->img($folder_img, $folder_alt), 'ATTACH_ICON_IMG' => $_CLASS['auth']->acl_gets('f_download', 'u_download', $forum_id) && $row['topic_attachment'] ? $_CLASS['core_user']->img('icon_attach', '') : '', 'U_VIEW_TOPIC' => generate_link($view_topic_url), 'U_VIEW_FORUM' => generate_link('Forums&amp;file=viewforum&amp;f=' . $forum_id), 'U_LAST_POST' => generate_link($view_topic_url . '&amp;p=' . $row['topic_last_post_id'] . '#' . $row['topic_last_post_id']), 'U_LAST_POST_AUTHOR' => $row['topic_last_poster_id'] != ANONYMOUS ? generate_link('Members_List&amp;mode=viewprofile&amp;u=' . $row['topic_last_poster_id']) : '', 'U_MOVE_UP' => $row['order_id'] != 1 ? generate_link("Control_Panel&amp;i=main&amp;mode=bookmarks&amp;move_up={$row['order_id']}") : '', 'U_MOVE_DOWN' => $row['order_id'] != $max_order_id ? generate_link("Control_Panel&amp;i=main&amp;mode=bookmarks&amp;move_down={$row['order_id']}") : ''));
                } while ($row = $_CLASS['core_db']->fetch_row_assoc($result));
                $_CLASS['core_db']->free_result($result);
                $_CLASS['core_template']->assign_array(array('S_BOOKMARKS' => $bookmarks, 'S_BOOKMARKS_DISABLED' => false));
                break;
            case 'drafts':
                global $ucp;
                $pm_drafts = $ucp->name == 'pm' ? true : false;
                $_CLASS['core_user']->add_lang('posting', 'Forums');
                $edit = isset($_REQUEST['edit']) ? true : false;
                $submit = isset($_POST['submit']) ? true : false;
                $draft_id = $edit ? intval($_REQUEST['edit']) : 0;
                $delete = isset($_POST['delete']) ? true : false;
                $s_hidden_fields = $edit ? '<input type="hidden" name="edit" value="' . $draft_id . '" />' : '';
                $draft_subject = $draft_message = '';
                if ($delete) {
                    $drafts = isset($_POST['d']) ? implode(', ', array_map('intval', array_keys($_POST['d']))) : '';
                    if ($drafts) {
                        $sql = 'DELETE FROM ' . FORUMS_DRAFTS_TABLE . "\n\t\t\t\t\t\t\tWHERE draft_id IN ({$drafts}) \n\t\t\t\t\t\t\t\tAND user_id = " . $_CLASS['core_user']->data['user_id'];
                        $_CLASS['core_db']->query($sql);
                        $message = $_CLASS['core_user']->lang['DRAFTS_DELETED'] . '<br /><br />' . sprintf($_CLASS['core_user']->lang['RETURN_UCP'], '<a href="' . generate_link("Control_Panel&amp;i={$id}&amp;mode={$mode}") . '">', '</a>');
                        $_CLASS['core_display']->meta_refresh(3, generate_link("Control_Panel&amp;i={$id}&amp;mode={$mode}"));
                        trigger_error($message);
                    }
                }
                if ($submit && $edit) {
                    $draft_subject = preg_replace('#&amp;(\\#[0-9]+;)#', '&\\1', request_var('subject', ''));
                    $draft_message = isset($_POST['message']) ? htmlspecialchars(trim(str_replace(array('\\\'', '\\"', '\\0', '\\\\'), array('\'', '"', '\\0', '\\'), $_POST['message']))) : '';
                    $draft_message = preg_replace('#&amp;(\\#[0-9]+;)#', '&\\1', $draft_message);
                    if ($draft_message && $draft_subject) {
                        $draft_row = array('draft_subject' => $draft_subject, 'draft_message' => $draft_message);
                        $sql = 'UPDATE ' . FORUMS_DRAFTS_TABLE . ' 
							SET ' . $_CLASS['core_db']->sql_build_array('UPDATE', $draft_row) . " \n\t\t\t\t\t\t\tWHERE draft_id = {$draft_id}\n\t\t\t\t\t\t\t\tAND user_id = " . $_CLASS['core_user']->data['user_id'];
                        $_CLASS['core_db']->query($sql);
                        $message = $_CLASS['core_user']->lang['DRAFT_UPDATED'] . '<br /><br />' . sprintf($_CLASS['core_user']->lang['RETURN_UCP'], '<a href="' . generate_link("Control_Panel&amp;i={$id}&amp;mode={$mode}") . '">', '</a>');
                        $_CLASS['core_display']->meta_refresh(3, generate_link("Control_Panel&amp;i={$id}&amp;mode={$mode}"));
                        trigger_error($message);
                    } else {
                        $_CLASS['core_template']->assign('ERROR', $draft_message == '' ? $_CLASS['core_user']->lang['EMPTY_DRAFT'] : ($draft_subject == '' ? $_CLASS['core_user']->lang['EMPTY_DRAFT_TITLE'] : ''));
                    }
                }
                if (!$pm_drafts) {
                    $sql = 'SELECT d.*, f.forum_name
						FROM ' . FORUMS_DRAFTS_TABLE . ' d, ' . FORUMS_FORUMS_TABLE . ' f
						WHERE d.user_id = ' . $_CLASS['core_user']->data['user_id'] . ' ' . ($edit ? "AND d.draft_id = {$draft_id}" : '') . '
							AND f.forum_id = d.forum_id
							ORDER BY d.save_time DESC';
                } else {
                    $sql = 'SELECT * FROM ' . FORUMS_DRAFTS_TABLE . '
						WHERE user_id = ' . $_CLASS['core_user']->data['user_id'] . ' ' . ($edit ? "AND draft_id = {$draft_id}" : '') . '
							AND forum_id = 0 
							AND topic_id = 0
							ORDER BY save_time DESC';
                }
                $result = $_CLASS['core_db']->query($sql);
                $draftrows = $topic_ids = array();
                while ($row = $_CLASS['core_db']->fetch_row_assoc($result)) {
                    if ($row['topic_id']) {
                        $topic_ids[] = (int) $row['topic_id'];
                    }
                    $draftrows[] = $row;
                }
                $_CLASS['core_db']->free_result($result);
                if (sizeof($topic_ids)) {
                    $sql = 'SELECT topic_id, forum_id, topic_title
						FROM ' . FORUMS_TOPICS_TABLE . '
						WHERE topic_id IN (' . implode(',', array_unique($topic_ids)) . ')';
                    $result = $_CLASS['core_db']->query($sql);
                    while ($row = $_CLASS['core_db']->fetch_row_assoc($result)) {
                        $topic_rows[$row['topic_id']] = $row;
                    }
                    $_CLASS['core_db']->free_result($result);
                }
                unset($topic_ids);
                $_CLASS['core_template']->assign('S_EDIT_DRAFT', $edit);
                foreach ($draftrows as $draft) {
                    $link_topic = $link_forum = $link_pm = false;
                    $insert_url = $view_url = $title = '';
                    if ($pm_drafts) {
                        $link_pm = true;
                        $insert_url = generate_link("Control_Panel&amp;i={$id}&amp;mode=compose&amp;d=" . $draft['draft_id']);
                    } else {
                        if (isset($topic_rows[$draft['topic_id']]) && $_CLASS['auth']->acl_get('f_read', $topic_rows[$draft['topic_id']]['forum_id'])) {
                            $link_topic = true;
                            $view_url = generate_link('Forums&amp;file=viewtopic&amp;f=' . $topic_rows[$draft['topic_id']]['forum_id'] . "&amp;t=" . $draft['topic_id']);
                            $title = $topic_rows[$draft['topic_id']]['topic_title'];
                            $insert_url = generate_link('Forums&amp;file=posting&amp;f=' . $topic_rows[$draft['topic_id']]['forum_id'] . '&amp;t=' . $draft['topic_id'] . '&amp;mode=reply&amp;d=' . $draft['draft_id']);
                        } else {
                            if ($_CLASS['auth']->acl_get('f_read', $draft['forum_id'])) {
                                $link_forum = true;
                                $view_url = generate_link('Forums&amp;file=viewforum&amp;f=' . $draft['forum_id']);
                                $title = $draft['forum_name'];
                                $insert_url = generate_link('Forums&amp;file=posting&amp;f=' . $draft['forum_id'] . '&amp;mode=post&amp;d=' . $draft['draft_id']);
                            }
                        }
                    }
                    $template_row = array('DATE' => $_CLASS['core_user']->format_date($draft['save_time']), 'DRAFT_MESSAGE' => $submit ? $draft_message : $draft['draft_message'], 'DRAFT_SUBJECT' => $submit ? $draft_subject : $draft['draft_subject'], 'TITLE' => $title, 'DRAFT_ID' => $draft['draft_id'], 'FORUM_ID' => $draft['forum_id'], 'TOPIC_ID' => $draft['topic_id'], 'U_VIEW' => $view_url, 'U_VIEW_EDIT' => generate_link("Control_Panel&amp;i={$id}&amp;mode={$mode}&amp;edit=" . $draft['draft_id']), 'U_INSERT' => $insert_url, 'S_LINK_TOPIC' => $link_topic, 'S_LINK_FORUM' => $link_forum, 'S_LINK_PM' => $link_pm, 'S_HIDDEN_FIELDS' => $s_hidden_fields);
                    $edit ? $_CLASS['core_template']->assign_array($template_row) : $_CLASS['core_template']->assign_vars_array('draftrow', $template_row);
                }
                break;
        }
        $_CLASS['core_template']->assign_array(array('L_TITLE' => $_CLASS['core_user']->lang['UCP_MAIN_' . strtoupper($mode)], 'S_DISPLAY_MARK_ALL' => $mode == 'watched' || $mode == 'drafts' && !isset($_GET['edit']) ? true : false, 'S_HIDDEN_FIELDS' => isset($s_hidden_fields) ? $s_hidden_fields : '', 'S_DISPLAY_FORM' => true, 'S_UCP_ACTION' => generate_link("Control_Panel&amp;i={$id}&amp;mode={$mode}")));
        $this->display($_CLASS['core_user']->lang['UCP_MAIN'], 'ucp_main_' . $mode . '.html');
    }
                $error[] = 'USER_NOT_FOUND_OR_INACTIVE';
            }
            $_CLASS['core_db']->free_result($result);
        }
    } elseif ($remove_users = get_variable('usernames', 'POST', false, 'array:int')) {
        $sql = 'DELETE FROM ' . ZEBRA_TABLE . ' 
			WHERE user_id = ' . $_CLASS['core_user']->data['user_id'] . ' 
				AND zebra_id IN (' . implode(', ', array_unique($remove_users)) . ')';
        $_CLASS['core_db']->query($sql);
    }
    if (empty($error)) {
        $_CLASS['core_display']->meta_refresh(3, generate_link($this->link));
        $message = $_CLASS['core_user']->lang[strtoupper($this->mode) . '_UPDATED'] . '<br /><br />' . sprintf($_CLASS['core_user']->lang['RETURN_UCP'], '<a href="' . generate_link($this->link) . '">', '</a>');
        trigger_error($message);
    } else {
        $_CLASS['core_template']->assign('ERROR', implode('<br />', $error));
    }
}
$sql_and = $this->mode === 'friends' ? 'z.friend = 1' : 'z.foe = 1';
$sql = 'SELECT u.user_id, u.username 
	FROM ' . ZEBRA_TABLE . ' z, ' . CORE_USERS_TABLE . ' u 
	WHERE z.user_id = ' . $_CLASS['core_user']->data['user_id'] . "\n\t\tAND {$sql_and} \n\t\tAND u.user_id = z.zebra_id";
$result = $_CLASS['core_db']->query($sql);
$username_options = '';
while ($row = $_CLASS['core_db']->fetch_row_assoc($result)) {
    $username_options .= '<option value="' . $row['user_id'] . '">' . $row['username'] . '</option>';
}
$_CLASS['core_db']->free_result($result);
$_CLASS['core_template']->assign_array(array('L_TITLE' => $_CLASS['core_user']->lang['UCP_ZEBRA_' . strtoupper($this->mode)], 'U_SEARCH_USER' => generate_link('members_list&amp;mode=searchuser&amp;form=ucp&amp;field=add'), 'S_USERNAME_OPTIONS' => $username_options, 'S_HIDDEN_FIELDS' => generate_hidden_fields($hidden_fields), 'S_UCP_ACTION' => generate_link($this->link)));
unset($username_options);
$_CLASS['core_display']->display($_CLASS['core_user']->get_lang('UCP_ZEBRA'), 'modules/control_panel/ucp_zebra_' . $this->mode . '.html');
        if ($move_up || $move_down) {
            if ($move_up && $move_up != 1 || $move_down && $move_down != $max_order_id) {
                $order = $move_up ? $move_up : $move_down;
                $order_total = $order * 2 + ($move_up ? -1 : 1);
                $sql = 'UPDATE ' . FORUMS_BOOKMARKS_TABLE . "\n\t\t\t\t\tSET order_id = {$order_total} - order_id\n\t\t\t\t\tWHERE order_id IN ({$order}, " . ($move_up ? $order - 1 : $order + 1) . ')
						AND user_id = ' . $_CLASS['core_user']->data['user_id'];
                $_CLASS['core_db']->query($sql);
            }
        }
        if (isset($_POST['unbookmark'])) {
            $topics = array_unique(get_variable('t', 'POST', array(), 'array:int'));
            if (empty($topics)) {
                trigger_error('NO_BOOKMARKS_SELECTED');
            }
            $hidden_fields = array('unbookmark' => 1, 't' => $topics);
            if (display_confirmation($_CLASS['core_user']->get_lang('REMOVE_SELECTED_BOOKMARKS'), generate_hidden_fields($hidden_fields))) {
                $sql = 'DELETE FROM ' . FORUMS_BOOKMARKS_TABLE . '
					WHERE user_id = ' . $_CLASS['core_user']->data['user_id'] . '
						AND topic_id IN (' . implode(', ', $topics) . ')';
                $_CLASS['core_db']->query($sql);
                $sql = 'SELECT topic_id FROM ' . FORUMS_BOOKMARKS_TABLE . '
					WHERE user_id = ' . $_CLASS['core_user']->data['user_id'] . '
					ORDER BY order_id ASC';
                $result = $_CLASS['core_db']->query($sql);
                $i = 1;
                while ($row = $_CLASS['core_db']->fetch_row_assoc($result)) {
                    $_CLASS['core_db']->query('UPDATE ' . FORUMS_BOOKMARKS_TABLE . "\n\t\t\t\t\t\tSET order_id = '{$i}'\n\t\t\t\t\t\tWHERE topic_id = '{$row['topic_id']}'\n\t\t\t\t\t\t\tAND user_id = '{$_CLASS['core_user']->data['user_id']}'");
                    $i++;
                }
                $_CLASS['core_db']->free_result($result);
                $url = generate_link('control_panel&amp;i=main&amp;mode=bookmarks');
function mcp_fork_topic($topic_ids)
{
    global $_CLASS, $config;
    if (!check_ids($topic_ids, FORUMS_TOPICS_TABLE, 'topic_id', 'm_')) {
        return;
    }
    $redirect = get_variable('redirect', 'POST', $_CLASS['core_user']->data['session_url']);
    $to_forum_id = get_variable('to_forum_id', 'POST', 0, 'int');
    $additional_msg = $success_msg = '';
    if ($to_forum_id) {
        $forum_data = get_forum_data($to_forum_id, 'm_');
        if (empty($forum_data[$to_forum_id])) {
            $additional_msg = $_CLASS['core_user']->lang['FORUM_NOT_EXIST'];
        } else {
            $forum_data = $forum_data[$to_forum_id];
            if ($forum_data['forum_type'] != FORUM_POST) {
                $additional_msg = $_CLASS['core_user']->lang['FORUM_NOT_POSTABLE'];
            } elseif (!$_CLASS['auth']->acl_get('f_post', $to_forum_id)) {
                $additional_msg = $_CLASS['core_user']->lang['USER_CANNOT_POST'];
            }
        }
    }
    if (!$to_forum_id || $additional_msg) {
        unset($_POST['confirm']);
    }
    $hidden_fields = generate_hidden_fields(array('topic_id_list' => $topic_ids, 'mode' => 'fork', 'redirect' => $redirect));
    $_CLASS['core_template']->assign_array(array('S_FORUM_SELECT' => make_forum_select($to_forum_id, false, false, true, true), 'S_CAN_LEAVE_SHADOW' => false, 'ADDITIONAL_MSG' => $additional_msg));
    $message = $_CLASS['core_user']->get_lang('FORK_TOPIC' . (count($topic_ids) === 1 ? '' : 'S'));
    page_header();
    if (display_confirmation($message, $hidden_fields, 'modules/Forums/mcp_move.html')) {
        $topic_data = get_topic_data($topic_ids);
        $total_posts = 0;
        $new_topic_id_list = $new_topic_forum_name_list = $insert_array = array();
        $_CLASS['core_db']->transaction();
        foreach ($topic_data as $topic_id => $topic_row) {
            // just change $row values for forum_id, topic_reported;
            // get_topic_data gets some unneeded stuff, remove it so we can just use $row
            $sql_ary = array('forum_id' => (int) $to_forum_id, 'icon_id' => (int) $topic_row['icon_id'], 'topic_attachment' => (int) $topic_row['topic_attachment'], 'topic_approved' => 1, 'topic_reported' => 0, 'topic_title' => (string) $topic_row['topic_title'], 'topic_poster' => (int) $topic_row['topic_poster'], 'topic_time' => (int) $topic_row['topic_time'], 'topic_replies' => (int) $topic_row['topic_replies_real'], 'topic_replies_real' => (int) $topic_row['topic_replies_real'], 'topic_status' => (int) $topic_row['topic_status'], 'topic_type' => (int) $topic_row['topic_type'], 'topic_first_poster_name' => (string) $topic_row['topic_first_poster_name'], 'topic_last_poster_id' => (int) $topic_row['topic_last_poster_id'], 'topic_last_poster_name' => (string) $topic_row['topic_last_poster_name'], 'topic_last_post_time' => (int) $topic_row['topic_last_post_time'], 'topic_last_view_time' => (int) $topic_row['topic_last_view_time'], 'topic_bumped' => (int) $topic_row['topic_bumped'], 'topic_bumper' => (int) $topic_row['topic_bumper'], 'topic_views' => 0, 'poll_title' => (string) $topic_row['poll_title'], 'poll_start' => (int) $topic_row['poll_start'], 'poll_length' => (int) $topic_row['poll_length']);
            $_CLASS['core_db']->sql_query_build('INSERT', $sql_ary, FORUMS_TOPICS_TABLE);
            unset($sql_ary);
            $new_topic_id = $_CLASS['core_db']->insert_id(FORUMS_TOPICS_TABLE, 'topic_id');
            $new_topic_id_list[$topic_id] = $new_topic_id;
            $new_topic_forum_name_list[$topic_id] = $topic_row['forum_name'];
            if ($topic_row['poll_start']) {
                $poll_rows = array();
                $sql = 'SELECT * 
					FROM ' . FORUMS_POLL_OPTIONS_TABLE . " \n\t\t\t\t\tWHERE topic_id = {$topic_id}";
                $result = $_CLASS['core_db']->query($sql);
                while ($row = $_CLASS['core_db']->fetch_row_assoc($result)) {
                    $insert_array[FORUMS_POLL_OPTIONS_TABLE][] = array('poll_option_id' => (int) $row['poll_option_id'], 'topic_id' => (int) $new_topic_id, 'poll_option_text' => (string) $row['poll_option_text'], 'poll_option_total' => 0);
                }
                $_CLASS['core_db']->free_result($result);
            }
            unset($topic_data[$topic_id]);
            $sql = 'SELECT *
				FROM ' . FORUMS_POSTS_TABLE . "\n\t\t\t\tWHERE topic_id = {$topic_id}\n\t\t\t\tORDER BY post_id ASC";
            $result = $_CLASS['core_db']->query($sql);
            while ($row = $_CLASS['core_db']->fetch_row_assoc($result)) {
                $total_posts++;
                $insert_array[FORUMS_POSTS_TABLE][] = array('topic_id' => (int) $new_topic_id, 'forum_id' => (int) $to_forum_id, 'poster_id' => (int) $row['poster_id'], 'icon_id' => (int) $row['icon_id'], 'poster_ip' => (string) $row['poster_ip'], 'post_time' => (int) $row['post_time'], 'post_approved' => 1, 'post_reported' => 0, 'enable_bbcode' => (int) $row['enable_bbcode'], 'enable_html' => (int) $row['enable_html'], 'enable_smilies' => (int) $row['enable_smilies'], 'enable_magic_url' => (int) $row['enable_magic_url'], 'enable_sig' => (int) $row['enable_sig'], 'post_username' => (string) $row['post_username'], 'post_subject' => (string) $row['post_subject'], 'post_text' => (string) $row['post_text'], 'post_edit_reason' => (string) $row['post_edit_reason'], 'post_edit_user' => (int) $row['post_edit_user'], 'post_checksum' => (string) $row['post_checksum'], 'post_attachment' => (int) $row['post_attachment'], 'bbcode_bitfield' => (int) $row['bbcode_bitfield'], 'bbcode_uid' => (string) $row['bbcode_uid'], 'post_edit_time' => (int) $row['post_edit_time'], 'post_edit_count' => (int) $row['post_edit_count'], 'post_edit_locked' => (int) $row['post_edit_locked']);
                // Copy Attachments
                if ($row['post_attachment']) {
                    $_CLASS['core_db']->query('INSERT INTO ' . FORUMS_POSTS_TABLE . ' ' . $_CLASS['core_db']->sql_build_array('INSERT', array_pop($insert_array[FORUMS_POSTS_TABLE])));
                    $new_post_id = $_CLASS['core_db']->insert_id(FORUMS_POSTS_TABLE, 'post_id');
                    $sql = 'SELECT * FROM ' . FORUMS_ATTACHMENTS_TABLE . "\n\t\t\t\t\t\tWHERE post_msg_id = {$row['post_id']}\n\t\t\t\t\t\t\tAND topic_id = {$topic_id}\n\t\t\t\t\t\t\tAND in_message = 0";
                    $result = $_CLASS['core_db']->query($sql);
                    while ($attach_row = $_CLASS['core_db']->fetch_row_assoc($result)) {
                        $insert_array[FORUMS_ATTACHMENTS_TABLE][] = array('post_msg_id' => (int) $new_post_id, 'topic_id' => (int) $new_topic_id, 'in_message' => 0, 'poster_id' => (int) $attach_row['poster_id'], 'physical_filename' => (string) basename($attach_row['physical_filename']), 'real_filename' => (string) basename($attach_row['real_filename']), 'download_count' => (int) $attach_row['download_count'], 'attach_comment' => (string) $attach_row['attach_comment'], 'extension' => (string) $attach_row['extension'], 'mimetype' => (string) $attach_row['mimetype'], 'filesize' => (int) $attach_row['filesize'], 'filetime' => (int) $attach_row['filetime'], 'thumbnail' => (int) $attach_row['thumbnail']);
                    }
                    $_CLASS['core_db']->free_result($result);
                }
            }
            $_CLASS['core_db']->free_result($result);
        }
        unset($topic_data);
        $_CLASS['core_db']->transaction('commit');
        if (!empty($new_topic_id_list)) {
            if (!empty($insert_array[FORUMS_POLL_OPTIONS_TABLE])) {
                $_CLASS['core_db']->sql_query_build('MULTI_INSERT', $insert_array[FORUMS_POLL_OPTIONS_TABLE], FORUMS_POLL_OPTIONS_TABLE);
            }
            if (!empty($insert_array[FORUMS_POSTS_TABLE])) {
                $_CLASS['core_db']->sql_query_build('MULTI_INSERT', $insert_array[FORUMS_POSTS_TABLE], FORUMS_POSTS_TABLE);
            }
            if (!empty($insert_array[FORUMS_ATTACHMENTS_TABLE])) {
                $_CLASS['core_db']->sql_query_build('MULTI_INSERT', $insert_array[FORUMS_ATTACHMENTS_TABLE], FORUMS_ATTACHMENTS_TABLE);
            }
            unset($insert_array);
            // Sync new topics, parent forums and board stats
            sync('topic', 'topic_id', $new_topic_id_list, true);
            sync('forum', 'forum_id', $to_forum_id, true);
            set_config('num_topics', $config['num_topics'] + count($new_topic_id_list));
            set_config('num_posts', $config['num_posts'] + $total_posts);
            foreach ($new_topic_id_list as $topic_id => $new_topic_id) {
                add_log('mod', $to_forum_id, $new_topic_id, 'LOG_FORK', $new_topic_forum_name_list[$topic_id]['forum_name']);
            }
            $success_msg = count($topic_ids) === 1 ? 'TOPIC_FORKED_SUCCESS' : 'TOPICS_FORKED_SUCCESS';
        }
    }
    $redirect = generate_link($redirect);
    if (!$success_msg) {
        redirect($redirect);
    } else {
        $_CLASS['core_display']->meta_refresh(3, generate_link('forums&amp;file=viewforum&amp;f=' . $to_forum_id));
        $return_link = sprintf($_CLASS['core_user']->lang['RETURN_NEW_FORUM'], '<a href="' . $redirect . '">', '</a>');
        trigger_error($_CLASS['core_user']->lang[$success_msg] . '<br /><br />' . $return_link);
    }
}
// LICENCE   : GPL vs2.0 [ see /docs/COPYING ]
//
// -------------------------------------------------------------
//
// * Use this for ACP integration - changeable user id
//
global $_CLASS, $config, $site_file_root;
$start = get_variable('start', 'REQUEST', 0, 'int');
$delete = isset($_POST['delete']);
$confirm = isset($_POST['confirm']);
// change this
$delete_ids = array_unique(get_variable('attachment', 'POST', array(), 'array:int'));
if (!empty($delete_ids)) {
    $hidden_fields['delete'] = 1;
    $hidden_fields['attachment'] = $delete_ids;
    if (display_confirmation($_CLASS['core_user']->get_lang(count($delete_ids) == 1 ? 'DELETE_ATTACHMENT' : 'DELETE_ATTACHMENTS'), generate_hidden_fields($hidden_fields))) {
        require_once $site_file_root . 'includes/forums/functions_admin.php';
        require_once $site_file_root . 'includes/forums/functions.php';
        $_CLASS['core_db']->transaction();
        delete_attachments('attach', $delete_ids);
        $_CLASS['core_db']->transaction('commit');
        $return_link = generate_link($this->link_parent);
        $_CLASS['core_display']->meta_refresh(3, $return_link);
        $message = (count($delete_ids) === 1 ? $_CLASS['core_user']->lang['ATTACHMENT_DELETED'] : $_CLASS['core_user']->lang['ATTACHMENTS_DELETED']) . '<br /><br />' . sprintf($_CLASS['core_user']->lang['RETURN_UCP'], '<a href="' . $return_link . '">', '</a>');
        trigger_error($message);
    }
}
$sort_key = get_variable('sk', 'REQUEST', 'a');
$sort_dir = get_variable('sd', 'REQUEST', 'a');
// Select box eventually
$sort_key_text = array('a' => $_CLASS['core_user']->lang['SORT_FILENAME'], 'b' => $_CLASS['core_user']->lang['SORT_COMMENT'], 'c' => $_CLASS['core_user']->lang['SORT_EXTENSION'], 'd' => $_CLASS['core_user']->lang['SORT_SIZE'], 'e' => $_CLASS['core_user']->lang['SORT_DOWNLOADS'], 'f' => $_CLASS['core_user']->lang['SORT_POST_TIME'], 'g' => $_CLASS['core_user']->lang['SORT_TOPIC_TITLE']);