Compare commits

..

No commits in common. "783e9515706ceb18f390a4c49cec478dd4e4df44" and "d78de57b84f8d9b72c58a47cf2098dd40ed56d1f" have entirely different histories.

2 changed files with 33 additions and 24 deletions

View File

@ -15,7 +15,10 @@ class AjaxController extends BaseController
require_once APPS_PATH . '/Ajax/ajax-actions.php'; require_once APPS_PATH . '/Ajax/ajax-actions.php';
$context['result'] = ""; $context['result'] = "";
$action = $_POST['action'] ?? false; $json = file_get_contents('php://input');
$data = json_decode($json, true);
$action = $data['action'] ?? false;
// If request from other site // If request from other site
if (!in_array($_SERVER['HTTP_HOST'], ALLOWED_HOSTS)) { if (!in_array($_SERVER['HTTP_HOST'], ALLOWED_HOSTS)) {
@ -31,7 +34,7 @@ class AjaxController extends BaseController
$action = "ajax_" . $action; $action = "ajax_" . $action;
try { try {
$context['result'] = $action(); $context['result'] = $action($data['args']);
} catch (\Exception $ex) { } catch (\Exception $ex) {
$context['result'] = get_ajax_error($ex->getMessage()); $context['result'] = get_ajax_error($ex->getMessage());
} }

View File

@ -1,7 +1,5 @@
<?php <?php
use Lycoreco\Apps\Recipes\Models\RecipeModel;
function get_ajax_error($message, $error_code = 500) function get_ajax_error($message, $error_code = 500)
{ {
http_response_code($error_code); http_response_code($error_code);
@ -15,27 +13,35 @@ function get_ajax_error($message, $error_code = 500)
/** /**
* Ajax actions * Ajax actions
*/ */
function ajax_search() { function ajax_search($args) {
$search_query = $_POST['query'] ?? null; $search_query = $args['query'];
if (!isset($search_query)) {
return get_ajax_error("Missing 'query' parameter.", 400);
}
if (!CURRENT_USER) {
return get_ajax_error('You are not authorized', 401);
}
$result = array();
$recipes = RecipeModel::filter( $result = [];
array(), $data = [
array(), [
5, 'id' => 2,
'AND', 'name' => 'Genshin Impact'
0, ],
$search_query [
); 'id' => 3,
'name' => 'Zenless zone zero'
],
[
'id' => 4,
'name' => 'Honkai Star Rail'
],
[
'id' => 5,
'name' => 'Honkai Impact'
],
];
$result['results'] = [];
$result['count'] = count($recipes); foreach ($data as $key => $value) {
$result['result'] = $recipes; if(str_contains($value['name'], $search_query))
$result['results'][] = $value;
}
sleep(3);
return json_encode($result, JSON_PRETTY_PRINT); return json_encode($result, JSON_PRETTY_PRINT);
} }