<?php
/**
 * @desc Helper functions
 */

function get_configs()
{
    if(!file_exists("system/configurations/cc_env.inc"))
        die("Environment config not found!");

    $configs = explode("\n", file_get_contents("system/configurations/cc_env.inc"));
    foreach($configs AS $config):
        $line = explode("<=>", $config);
        $vals[$line[0]] = (isset($line[1]) ? $line[1] : false);
    endforeach;

    $vals["siteurl"] = (isset($_SERVER["SERVER_NAME"]) ? $_SERVER["SERVER_NAME"] : php_uname("n"));
    $vals["port"] = (in_array($_SERVER["SERVER_PORT"], [80, 443]) ? false : ":{$_SERVER["SERVER_PORT"]}");
    $vals["subdir"] = (empty(explode("/", dirname($_SERVER["SCRIPT_NAME"]))[1]) ? false : dirname($_SERVER["SCRIPT_NAME"]));

    define("env", $vals);

    if(!isset(env["installed"]) && !Stringy\create($_SERVER["REQUEST_URI"])->contains("install"))
        header("location: ./install");

    return get_version();
}

function get_version()
{
    if(!file_exists("system/configurations/cc_ver.inc"))
        die("Version config not found!");

    return define("version", file_get_contents("system/configurations/cc_ver.inc"));
}

function site_url($path = false)
{
    return site_url . "/" . rtrim($path, "/") . "?" . \Volnix\CSRF\CSRF::getQueryString("_token");
}

function response($status, $msg, $data = false)
{
    return die(json_encode([
    	"status" => $status, 
    	"message" => $msg, 
    	"data" => $data
    ]));
}

function set_template($name)
{
    return define("template", $name);
}

function set_language($translations)
{
    $lines = explode("\n", trim($translations));
        
    foreach($lines as $line):
        if(Stringy\create($line)->contains("===")):
            $columns = explode("===", trim($line));
            define("lang_" . trim($columns[0]), trim($columns[1]));
        endif;
    endforeach;
}

function set_blocks($blocks)
{
    foreach($blocks as $key => $value):
        define("block_{$key}", $value);
    endforeach;
}

function set_logged($user)
{
    if(empty($user)):
        $user = [
            "id" => false,
            "admin" => false,
            "hash" => false,
            "email" => false,
            "name" => false
        ];
    endif;

    $user["language"] = (isset($_SESSION["language"]) ? $_SESSION["language"] : (isset($_SESSION["logged"]["language"]) ? $_SESSION["logged"]["language"] : system_default_lang));

    foreach($user as $key => $value):
        define("logged_{$key}", $value);
    endforeach;

    return define("avatar", (file_exists("uploads/avatars/" . $user["hash"] . ".jpg") ? site_url . "/uploads/avatars/" . $user["hash"] . ".jpg?v=" . filemtime("uploads/avatars/" . $user["hash"] . ".jpg") : site_url . "/uploads/avatars/noavatar.png"));
}

function set_system($system)
{
    foreach($system as $key => $value):
        define("system_{$key}", $value);
    endforeach;
}

function _assets($path)
{
    return (Stringy\create($path)->contains(".js") || Stringy\create($path)->contains(".css") ? site_url . "/templates/_assets/{$path}?v=" . md5(version) : site_url . "/templates/_assets/{$path}");
}

function assets($path, $template = template)
{
    return (Stringy\create($path)->contains(".js") || Stringy\create($path)->contains(".css") ? site_url . "/templates/{$template}/assets/{$path}?v=" . md5(filemtime("templates/{$template}/assets/{$path}")) : site_url . "/templates/{$template}/assets/{$path}");
}

function count_months($start, $end)
{
    $month = 1;
    $min = min(strtotime($start), strtotime($end));
    $max = max(strtotime($start), strtotime($end));

    while(($min = strtotime("+1 month", $min)) <= $max):
        $month++;
    endwhile;

    return $month;
}

function truncate($array, $max = 10)
{
    krsort($array);
    return array_slice($array, 0, $max, true);
}
