<?php
    $GLOBALS['highlight'] = 'forum';

    //SAFE SHOW

    require_once('include/functions/community_permissions.php');    
    require_once('include/functions/ip_decode.php');

    $_id = isset($_GET['i']) ? intval($_GET['i']) : 0;
    
    $community = mysqli_query_logged("SELECT * FROM community WHERE community_id = " . sq($_id));
    if (!$community_row = mysqli_fetch_assoc($community))
    {
        make_cookie('notice', 'Sorry, but this community does not exist.');
        header('Location: ./?s=community_create');
        die;
    }

    community_permissions($community_row['community_id']);
    
    if (!$GLOBALS['auth']['community']['administration'] && !$GLOBALS['auth']['community']['administration_ban_ip'] && !$GLOBALS['auth']['community']['administration_ban_user'])
    {
        make_cookie('notice', 'Sorry, you don\'t have permission to modify this community.');
        header('Location: ./?s=community_create');
        die;
    }

    include('include/parts/header.php');

    if ($GLOBALS['auth']['community']['administration'])
    {
        echo box_outside_top('Modify Your Community');
        echo box_inside_top();
?>

<form action="?a=community_modify&i=<?php echo $community_row['community_id'] ?>" method="post" class="form">
<b>Community Name</b> (<?php echo COMMUNITY_NAME_MIN ?> to <?php echo COMMUNITY_NAME_MAX ?> characters in length. No spaces.)
<div style="padding: 5px 0px 0px 0px;"></div>
<input type="text" name="name" maxlength="<?php echo COMMUNITY_NAME_MAX ?>" class="input_text" value="<?php echo htmlspecialchars($community_row['community_name']); ?>" style="width: 400px;">
<div style="padding: 5px 0px 0px 0px;"></div>
<input type="submit" value="Modfify Community" class="input_submit">
</form>

<?php
        echo box_inside_bottom();
        echo box_outside_bottom();
    }

    if ($GLOBALS['auth']['community']['administration'])
    {
        echo box_outside_top('Community Moderators');
        $i = 0;
        $community_permissions = mysqli_query_logged("SELECT DISTINCT(user_id), community_id FROM community_permissions WHERE community_id = '" . $community_row['community_id'] . "' AND permission = 'administrator'");
        while ($community_permissions_row = mysqli_fetch_assoc($community_permissions))
        {
            if ($i++)
            {
                echo '<div style="padding: 5px 0px 0px 0px;"></div>';
            }
            echo box_inside_top();
            display_admin_perms($community_permissions_row['community_id'], $community_permissions_row['user_id'], true);
            echo box_inside_bottom();
        }
        $community_permissions = mysqli_query_logged("SELECT DISTINCT(user_id), community_id FROM community_permissions WHERE community_id = '" . $community_row['community_id'] . "' AND permission != 'administrator'");
        while ($community_permissions_row = mysqli_fetch_assoc($community_permissions))
        {
            if ($i++)
            {
                echo '<div style="padding: 5px 0px 0px 0px;"></div>';
            }
            echo box_inside_top();
            display_admin_perms($community_permissions_row['community_id'], $community_permissions_row['user_id'], false);
            echo box_inside_bottom();
        }
        echo '<div style="padding: 5px 0px 0px 0px;"></div>';
        echo box_inside_top();
?>

<form action="?a=community_admin_add&i=<?php echo $community_row['community_id'] ?>" method="post" class="form">        
<b>Add A Moderator</b> (enter username)
<div style="padding: 5px 0px 0px 0px;"></div>
<input type="text" name="admin_name" class="input_text" style="width: 400px;">
<div style="padding: 5px 0px 0px 0px;"></div>
<input type="submit" value="Add Moderator" class="input_submit">                    
</form>

<?php
        echo box_inside_bottom();
        echo box_outside_bottom();
    }
    
    if ($GLOBALS['auth']['community']['administration'])
    {
        echo box_outside_top('Message Board');
        echo box_inside_top();
    
        $i = 0;
        $community_sections = mysqli_query_logged("SELECT * FROM community_sections WHERE community_id = '" . $community_row['community_id'] . "' AND section_deleted = '0' ORDER BY section_order_id");
        if ($section_count = mysqli_num_rows($community_sections))
        {
            while ($community_sections_row = mysqli_fetch_assoc($community_sections))
            {
                $forum_count = mysqli_fetch_assoc(mysqli_query_logged("SELECT COUNT(*) AS num FROM community_forums WHERE section_id = '" . $community_sections_row['section_id'] . "' AND forum_deleted = '0'"));
                if ($i > 0)
                {
                    echo '<div style="padding: 5px 0px 0px 0px;"></div>';
                }
                echo '<div class="' . ($i % 2 ? 'on' : 'off') . '">';                
                echo '<table width="100%" cellpadding="0" cellspacing="0" border="0"><tr>';
                echo '<td><span style="font-size: 14px; font-weight: bold;">';
                echo '<a href="?s=community_section_modify&i=' . $community_sections_row['section_id'] . '">' . htmlspecialchars($community_sections_row['section_name_english']) . '</a>';
                echo '</span> [<a href="?a=community_section_delete&i=' . $community_sections_row['section_id'] . '" onClick="return confirm(\'Are you sure you want to delete this section?\')">remove</a>]';
                echo '<br /> Contains ' . $forum_count['num'] . ' forums';
                echo '</td>';
                if ($i > 0)
                {
                    echo '<td width="75"><a href="?a=community_section_up&i=' . $community_sections_row['section_id'] . '">Move Up</a></td>';
                }
                else
                {
                    echo '<td width="75" nowrap></td>';
                }
                if ($i < ($section_count - 1))
                {
                    echo '<td width="75"><a href="?a=community_section_down&i=' . $community_sections_row['section_id'] . '">Move Down</a></td>';
                }
                else
                {
                    echo '<td width="75" nowrap></td>';
                }
                echo '</tr></table>';
                echo '</div>';
                $i++;
            }
        }
        else
        {
            echo 'You do not have any forum sections created.';
        }
        
        echo box_inside_bottom();
        echo '<div style="padding: 5px 0px 0px 0px;"></div>';
        echo box_inside_top();
?>

<form action="?a=community_section_add&i=<?php echo $community_row['community_id'] ?>" method="post" class="form">        
<div class="patch_fill">
    <b>Add A Section</b> (<?php echo COMMUNITY_SECTION_NAME_MIN ?> to <?php echo COMMUNITY_SECTION_NAME_MAX ?> characters in length.)
    <div style="padding: 5px 0px 0px 0px;"></div>
    <input type="text" name="name" maxlength="<?php echo COMMUNITY_SECTION_NAME_MAX; ?>" class="input_text" style="width: 400px;">
</div>
<div style="padding: 5px 0px 0px 0px;"></div>
<input type="submit" value="Add Section" class="input_submit">                    
</form>

<?php
        echo box_inside_bottom();
        echo box_outside_bottom();
    }
    
    if ($GLOBALS['auth']['community']['administration_ban_user'])
    {
        echo box_outside_top('Banned Users');
        echo box_inside_top();

        $i = 0;
        $banned_community_users_arr = array();
        $community_banned_users = mysqli_query_logged("SELECT * FROM community_banned_users WHERE community_id = '" . $community_row['community_id'] . "' ORDER BY added_on");
        if (mysqli_num_rows($community_banned_users))
        {
            while ($community_banned_users_row = mysqli_fetch_assoc($community_banned_users))
            {
                if ($i++)
                {
                    echo '<div style="padding: 5px 0px 0px 0px;"></div>';
                }
                echo '<div class="' . ($i % 2 == 1 ? 'on' : 'off') . '">';
                echo '<table width="100%" cellpadding="0" cellspacing="0"><tr><td width="200" valign="top">';
                echo '<a href="?s=profile&u=' . get_username($community_banned_users_row['user_id']) . '"><b>' . get_username($community_banned_users_row['user_id']) . '</b></a>';
                echo '</td><td valign="top">';
                $j = 0;
                $stats_ips = mysqli_query_logged("SELECT * FROM stats_ips WHERE user_id = '" . $community_banned_users_row['user_id'] . "' ORDER BY ip");
                while ($stats_ips_row = mysqli_fetch_assoc($stats_ips))
                {
                    if ($j++)
                    {
                        echo ', ';
                    }
                    echo decode_ip($stats_ips_row['ip']);
                }
                echo '</td><td width="80" valign="top" align="right" class="notice_attention">';
                echo '<a href="?a=community_user_unban&i=' . $community_row['community_id'] . '&name=' . get_username($community_banned_users_row['user_id']) . '">Unban</a>';
                echo '</td></tr></table>';
                echo '</div>';
            }
        }
        else
        {
            echo 'There are no banned users.';
        }
    
        echo box_inside_bottom();
        echo '<div style="padding: 5px 0px 0px 0px;"></div>';
        echo box_inside_top();
?>
                    
<form action="?a=community_user_ban&i=<?php echo $community_row['community_id'] ?>" method="post" class="form">    
<div class="patch_fill">
    <b>Ban A User</b>
    <div style="padding: 5px 0px 0px 0px;"></div>
    <input type="text" name="name" class="input_text" style="width: 300px;" />
</div>
<div style="padding: 5px 0px 0px 0px;"></div>
<input type="submit" value="Ban User" class="input_submit">                    
</form>

<?php
        echo box_inside_bottom();
        echo box_outside_bottom();
    }

    if ($GLOBALS['auth']['community']['administration_ban_ip'])
    {
        echo box_outside_top('Banned IPs');
        echo box_inside_top();    
    
        $i = 0;
        $community_banned_ips = mysqli_query_logged("SELECT * FROM community_banned_ips WHERE community_id = '" . $community_row['community_id'] . "' ORDER BY added_on");
        if (mysqli_num_rows($community_banned_ips))
        {
            while ($community_banned_ips_row = mysqli_fetch_assoc($community_banned_ips))
            {
                echo '<div class="' . ($i % 2 ? 'on' : 'off') . '">';
                echo '<table width="100%" cellpadding="0" cellspacing="0"><tr><td width="200" valign="top">';
                echo decode_ip($community_banned_ips_row['ip']);
                echo '</td><td valign="top">';
                $j = 0;
                $stats_ips = mysqli_query_logged("SELECT * FROM stats_ips WHERE ip = '" . $community_banned_ips_row['ip'] . "' AND user_id > 0");
                while ($stats_ips_row = mysqli_fetch_assoc($stats_ips))
                {
                    if ($j++)
                    {
                        echo ', ';
                    }
                    echo get_username($stats_ips_row['user_id']);
                }
                echo '</td><td width="80" valign="top" align="right" class="notice_attention">';
                echo '<a href="?a=community_ip_unban&i=' . $community_row['community_id'] . '&ip=' . decode_ip($community_banned_ips_row['ip']) . '">Unban</a>';
                echo '</td></tr></table>';
                echo '</div>';
            }
        }
        else
        {
            echo 'There are no banned ips.';
        }

        echo box_inside_bottom();
        echo '<div style="padding: 5px 0px 0px 0px;"></div>';
        echo box_inside_top();
?>

<form action="?a=community_ip_ban&i=<?php echo $community_row['community_id'] ?>" method="post" class="form">        
<div class="patch_fill">
    <b>Ban An IP Address</b>
    <div style="padding: 5px 0px 0px 0px;"></div>
    <input type="text" name="ip" class="input_text" style="width: 300px;" />
</div>
<div style="padding: 5px 0px 0px 0px;"></div>
<input type="submit" value="Ban IP" class="input_submit">                    
</form>

<?php
        echo box_inside_bottom();
        echo box_outside_bottom();
    }

    include('include/parts/footer.php');

    function display_admin_perms($community_id, $user_id, $root)
    {
        $username = get_username($user_id);
        $perms = '';
        $perm_arr = array();
        $i = 0;
        if ($root)
        {
            $permissions = mysqli_query_logged("SELECT DISTINCT permission FROM community_permissions");
        }
        else
        {
            $permissions = mysqli_query_logged("SELECT permission FROM community_permissions WHERE community_id = '" . $community_id . "' AND user_id = '" . $user_id . "'");
        }
        while ($permission = mysqli_fetch_array($permissions))
        {
            $perm_arr[$i++] = $permission['permission'];
        }
        $perms = implode(', ', $perm_arr);        
        echo '[' . $user_id . '] <span style="font-size: 14px; font-weight: bold;">';
        if ($root)
        {
            echo $username;
        }
        else
        {
            echo '<a href="?s=community_admin_modify&i=' . $community_id . '&admin_name=' . $username . '">' . $username . '</a>';
        }
        echo '</span> [<a href="?s=profile&u=' . $username . '">info</a>]';
        if ($root)
        {
            echo ' [root admin]';
        }
        else
        {
            echo ' [<a href="?a=community_admin_delete&i=' . $community_id . '&admin_name=' . $username . '" onClick="return confirm(\'Are you sure you want to remove this admin?\')">remove</a>]';
        }
        echo '<div style="padding: 5px 0px 0px 0px;"></div>';
        echo $perms;
    }
?>