51 lines
1.4 KiB
PHP
51 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace Lycoreco\Apps\Ajax\Controllers;
|
|
|
|
use Lycoreco\Includes\BaseController;
|
|
use Lycoreco\Includes\Model\ValidationError;
|
|
|
|
class AjaxController extends BaseController
|
|
{
|
|
protected $template_name = APPS_PATH . '/Ajax/Templates/ajax-result.php';
|
|
|
|
protected function restrict() {}
|
|
|
|
public function get_context_data()
|
|
{
|
|
require_once APPS_PATH . '/Ajax/ajax-actions.php';
|
|
$context['result'] = "";
|
|
|
|
$action = $_POST['action'] ?? false;
|
|
|
|
// If request from other site
|
|
if (!in_array($_SERVER['HTTP_HOST'], ALLOWED_HOSTS)) {
|
|
$context['result'] = get_ajax_error("403 Forbidden", 403);
|
|
return $context;
|
|
}
|
|
|
|
// if don't receive action method
|
|
if (empty($action)) {
|
|
$context['result'] = get_ajax_error("The action field indicating the function is not specified");
|
|
return $context;
|
|
}
|
|
$action = "ajax_" . $action;
|
|
|
|
try {
|
|
$context['result'] = $action();
|
|
}
|
|
catch (ValidationError $ex) {
|
|
$context['result'] = get_ajax_error($ex->getMessage(), 400);
|
|
return $context;
|
|
}
|
|
catch (\Exception $ex) {
|
|
http_response_code(500);
|
|
$context['result'] = get_ajax_error($ex->getMessage());
|
|
return $context;
|
|
}
|
|
|
|
|
|
return $context;
|
|
}
|
|
}
|