Newer
Older
vmk-demo-bot / adminback / api.php
<?php

include "include/kernel.php";

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST, GET, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type");
header('Content-Type: text/plain');

function api_handler($action, $params) {
	$action_path = explode(".", $action);
	if(count($action_path) < 2) {
		return throw new Exception("Action not found");
	}

	$action_file = __DIR__ . "/actions/{$action_path[0]}.php";
	$action_func = $action_path[0] . "_" . $action_path[1] . "_action";

	if(!file_exists($action_file)) {
		return throw new Exception("`{$action_file}` not found");
	}

	include $action_file;
	if(!function_exists($action_func)) {
		return throw new Exception("Action handler `{$action_func}` not found");
	}

	return $action_func($params);
}

$data = isset($_GET["action"]) ? ["action" => $_GET["action"], "params" => $_GET] : json_decode(file_get_contents('php://input'), true);

if(!$data) {
	die();
}

if(!isset($data["action"])) {
	die();
}

api_handler($data["action"], $data["params"]);