<?php
defined("BASEPATH") or exit("No direct script access allowed.");
require_once BASEPATH.'helpers/default_helper.php';
require_once BASEPATH.'database.php';

if (!function_exists('load')) {
	function load($type, $name) {
		global $db;
		if ($type == 'helper') {
			$location = BASEPATH.'helpers/'.$name.'_helper.php';
		} else if ($type == 'middleware') {
			$location = BASEPATH.'middlewares/'.$name.'_middleware.php';
		} else {
			return die("Type [{$type}] not found.");
		}

		return (file_exists($location)) ? require_once($location):die(ucfirst("{$type} [{$name}] not found."));
	}
}

function opt_get($opt_name) {
    global $db;
    
    $opt_name = base64_decode($opt_name);
    
    $query = mysqli_query($db, "SELECT * FROM options WHERE opt_name = '$opt_name'");
    if (mysqli_num_rows($query) === 1) {
        return mysqli_fetch_assoc($query)['opt_value'];
    } else {
        return '-';
    }
}

function opt_update($opt_name, $opt_value) {
    global $db;
    
    $opt_name = base64_decode($opt_name);
    
    $query = mysqli_query($db, "UPDATE options SET opt_value = '$opt_value' WHERE opt_name = '$opt_name'");
}

date_default_timezone_set('Asia/Jakarta');
error_reporting((config('web', 'environment') == 'development') ? E_ALL:0);
ini_set("display_errors", (config('web', 'environment') == 'development') ? 1:0);
if (!isset($_SESSION['csrf_token'])) generate_csrf_token();
if (isset($_SESSION['user'])) { $session = $_SESSION['user']['username']; }
$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'unknown';