<?php
    $_id = isset($_GET['i']) ? intval($_GET['i']) : 0;
    $_admin_name = isset($_POST['admin_name']) ? trim(strval($_POST['admin_name'])) : '';
    
    $community = mysqli_query_logged("SELECT * FROM community_sections, community WHERE community_sections.section_id = " . $_id . " AND community_sections.section_deleted = '0' AND community_sections.community_id = community.community_id");
    if (!$community_row = mysqli_fetch_assoc($community))
    {
        make_cookie('notice', 'Sorry, this section does not exist.');
        header('Location: ./?s=community_create');
        die;
    }
    
    require_once('include/functions/community_permissions.php');
    community_permissions($community_row['community_id']);
    
    if (!$GLOBALS['auth']['community']['administration'])
    {
        make_cookie('notice', 'Sorry, you don\'t have permission to modify this forum.');
        header('Location: ./?s=community_create');
        die;
    }
    
    if (!$user_id = get_user_id($_admin_name))    
    {
        make_cookie('notice', 'That user does not exist.');
        header('Location: ./?s=community_section_modify&i=' . $community_row['section_id']);
        die;        
    }
    
    if ($user_id == $GLOBALS['auth']['id'])
    {
        make_cookie('notice', 'Sorry, but you can not edit your own permissions.');
        header('Location: ./?s=community_section_modify&i=' . $community_row['section_id']);
        die;
    }
    
    mysqli_query_logged("INSERT INTO community_sections_permissions SET section_id = '" . $community_row['section_id'] . "', user_id = '" . $user_id . "', permission = 'admin'");
    
    header('Location: ./?s=community_section_modify&i=' . $community_row['section_id']);
    die;
?>