<?php function response_json(Array $resp = []) { @header('Content-Type: application/json'); return json_encode($resp); } function app() { global $app; return $app; }; function dd($var) { die(var_dump($var)); } function gen_token(int $user_id) :String { return hash_hmac('sha256', bin2hex(random_bytes(16)), $user_id); } function sanitize_input(String $var) :String { return htmlspecialchars(strip_tags(trim($var)), ENT_QUOTES, 'UTF-8'); } function get_formatted_timestamp() :String { return date("Y-m-d H:i:s"); } function error_response(String $class, String $errtext) :Array { return response(false, [ "error" => [ "class" => ucfirst($class), "text" => $errtext ] ]); } function success_response(Array $data = []) :Array { return response(true, [ "data" => $data ]); } function response(bool $status = true, Array $body = []) :Array { return [ "status" => $status, "response" => $body ]; } function force_error_response(String $class, String $errtext) { die(response_json(error_response($class, $errtext))); } function get_current_session() { $token = sanitize_input($_GET["token"]); $session = new \App\Entity\Session(); if(!$session -> init_by_token($token)) { force_error_response("Auth", "Invalid token"); } return $session; } function get_expected_vars(Array $vars_scope, Array $vars_list) :Array { $result = []; foreach($vars_list as $var_name) { if(!isset($vars_scope[$var_name])) { $expected_vars = implode(", ", $vars_list); force_error_response("Router", "Controller expects ({$expected_vars})"); } $result[] = $vars_scope[$var_name]; } return $result; }