<?php
    $_name = isset($_POST['name']) ? trim(strval($_POST['name'])) : '';

    if (language() == 'french')
    {
        define('ACTION_COMMUNITY_CREATE_ROOTADMIN', 'Sorry, you can only be the root admin of five communities.');
    }
    else
    {
        define('ACTION_COMMUNITY_CREATE_ROOTADMIN', 'Sorry, you can only be the root admin of five communities.');
    }
    
    $community_permissions = mysqli_query_logged("SELECT * FROM community_permissions WHERE user_id = '" . $GLOBALS['auth']['id'] . "' AND permission = 'administrator'");
    if (mysqli_num_rows($community_permissions) >= 5)
    {
        make_cookie('notice', ACTION_COMMUNITY_CREATE_ROOTADMIN);
        header('Location: ./?s=community_create');
        die;
    }
    
    require_once('include/functions/community_name_valid.php');
    if ($error = community_name_valid($_name))    
    {
        make_cookie('notice', $error);
        header('Location: ./?s=community_create');
        die;
    }    

    mysqli_query_logged("INSERT INTO community SET community_name = " . sq($_name) . ", community_creator = '" . $GLOBALS['auth']['id'] . "', community_created_on = NOW()");
    $mysql_insert_id = mysqli_insert_id($GLOBALS['mysqli']);
    mysqli_query_logged("INSERT INTO community_permissions SET community_id = '" . $mysql_insert_id . "', user_id = '" . $GLOBALS['auth']['id'] . "', permission = 'administrator'");

    header('Location: ./?s=community_modify&i=' . $mysql_insert_id);
?>