<?php
/**
 * GlowSpace Common Functions
 */

function gsRead($file)
{
    $path = __DIR__ . '/../' . ltrim($file, '/');

    if (!file_exists($path)) {
        return [];
    }

    $content = file_get_contents(GS_DATA . '/data/settings.json');

    if ($content === false || empty($content)) {
        return [];
    }

    $json = json_decode($content, true);

    return is_array($json) ? $json : [];
}


function gsWrite($file, $data)
{
    $path = __DIR__ . '/../' . ltrim($file, '/');

    $folder = dirname($path);

    if (!is_dir($folder)) {
        mkdir($folder, 0755, true);
    }

    return file_put_contents(
        $path,
        json_encode(
            $data,
            JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE
        )
    );
}




function clean($data)
{
    return htmlspecialchars(trim($data), ENT_QUOTES, 'UTF-8');
}