<?php
/**
 * Nexora VE Networks - Community Links
 * AdminCP module for footer social/community destinations.
 */

if (!defined('access')) die('Direct access not permitted.');

$configKey = 'social_links';
$configFile = __PATH_CONFIGS__ . $configKey . '.json';
$allowedFields = array('discord', 'facebook', 'youtube', 'instagram');

function nexoraCommunityNormalizeUrl($value) {
    $value = trim((string) $value);
    if ($value === '') return '';

    if (!filter_var($value, FILTER_VALIDATE_URL)) {
        throw new Exception('One of the links is not a valid URL. Use a complete URL beginning with https://');
    }

    $scheme = strtolower((string) parse_url($value, PHP_URL_SCHEME));
    if (!in_array($scheme, array('http', 'https'), true)) {
        throw new Exception('Only http:// and https:// links are allowed.');
    }

    return $value;
}

if (isset($_POST['community_links_save'])) {
    try {
        if (!file_exists($configFile)) {
            if (@file_put_contents($configFile, "{}\n", LOCK_EX) === false) {
                throw new Exception('Could not create the social links configuration file.');
            }
        }

        if (!is_writable($configFile)) {
            throw new Exception('The social links configuration file is not writable.');
        }

        $newConfig = array();
        foreach ($allowedFields as $field) {
            $newConfig[$field] = nexoraCommunityNormalizeUrl(isset($_POST[$field]) ? $_POST[$field] : '');
        }

        $json = json_encode($newConfig, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
        if ($json === false || file_put_contents($configFile, $json . "\n", LOCK_EX) === false) {
            throw new Exception('Could not save the social links configuration.');
        }

        message('success', 'Community links updated successfully.');
    } catch (Exception $ex) {
        message('error', $ex->getMessage());
    }
}

$communityLinks = loadConfig($configKey);
if (!is_array($communityLinks)) $communityLinks = array();
foreach ($allowedFields as $field) {
    if (!isset($communityLinks[$field])) $communityLinks[$field] = '';
}
?>
<h1 class="page-header">Community Links</h1>
<p>Manage the social buttons shown in the <strong>Comunidad</strong> section of the Nexora footer. Leave a field empty to hide that button from the website.</p>

<form method="post" action="<?php echo htmlspecialchars(admincp_base('community_links'), ENT_QUOTES, 'UTF-8'); ?>">
    <div class="panel panel-default">
        <div class="panel-heading"><strong>Nexora social destinations</strong></div>
        <div class="panel-body">
            <div class="form-group">
                <label for="community-discord"><i class="fa fa-comments fa-fw"></i> Discord</label>
                <input id="community-discord" type="url" name="discord" class="form-control" value="<?php echo htmlspecialchars($communityLinks['discord'], ENT_QUOTES, 'UTF-8'); ?>" placeholder="https://discord.com/invite/...">
                <span class="help-block">Invite or permanent Discord community URL.</span>
            </div>

            <div class="form-group">
                <label for="community-facebook"><i class="fa fa-facebook fa-fw"></i> Facebook</label>
                <input id="community-facebook" type="url" name="facebook" class="form-control" value="<?php echo htmlspecialchars($communityLinks['facebook'], ENT_QUOTES, 'UTF-8'); ?>" placeholder="https://www.facebook.com/...">
            </div>

            <div class="form-group">
                <label for="community-youtube"><i class="fa fa-youtube-play fa-fw"></i> YouTube</label>
                <input id="community-youtube" type="url" name="youtube" class="form-control" value="<?php echo htmlspecialchars($communityLinks['youtube'], ENT_QUOTES, 'UTF-8'); ?>" placeholder="https://www.youtube.com/@channel">
                <span class="help-block">You can use a channel, handle, playlist or video URL.</span>
            </div>

            <div class="form-group">
                <label for="community-instagram"><i class="fa fa-instagram fa-fw"></i> Instagram</label>
                <input id="community-instagram" type="url" name="instagram" class="form-control" value="<?php echo htmlspecialchars($communityLinks['instagram'], ENT_QUOTES, 'UTF-8'); ?>" placeholder="https://www.instagram.com/...">
            </div>
        </div>
        <div class="panel-footer">
            <button type="submit" name="community_links_save" value="1" class="btn btn-primary">
                <i class="fa fa-save"></i> Save community links
            </button>
        </div>
    </div>
</form>
