From 12ec5c1ac3cebf72b3abb2af29c24d1fd9aede4b Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Fri, 29 Oct 2021 12:54:49 +0200 Subject: [PATCH] The request helper can now also fetch JSON-payload attributes --- application/core/EA_Controller.php | 2 +- application/core/EA_Input.php | 76 +++++++++++++++++++++++++++++ application/core/EA_Migration.php | 23 +-------- application/core/EA_Model.php | 23 +-------- application/helpers/http_helper.php | 2 +- 5 files changed, 80 insertions(+), 46 deletions(-) create mode 100644 application/core/EA_Input.php diff --git a/application/core/EA_Controller.php b/application/core/EA_Controller.php index 7f3ad562..036b8bf9 100644 --- a/application/core/EA_Controller.php +++ b/application/core/EA_Controller.php @@ -26,7 +26,7 @@ * @property CI_Encryption $encryption * @property CI_Exceptions $exceptions * @property CI_Hooks $hooks - * @property CI_Input $input + * @property EA_Input $input * @property CI_Lang $lang * @property CI_Loader $load * @property CI_Log $log diff --git a/application/core/EA_Input.php b/application/core/EA_Input.php new file mode 100644 index 00000000..5eb10d25 --- /dev/null +++ b/application/core/EA_Input.php @@ -0,0 +1,76 @@ + + * @copyright Copyright (c) 2013 - 2020, Alex Tselegidis + * @license https://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link https://easyappointments.org + * @since v1.4.0 + * ---------------------------------------------------------------------------- */ + +/** + * Easy!Appointments input. + * + * @property CI_Benchmark $benchmark + * @property CI_Cache $cache + * @property CI_Calendar $calendar + * @property CI_Config $config + * @property CI_DB_forge $dbforge + * @property CI_DB_query_builder $db + * @property CI_DB_utility $dbutil + * @property CI_Email $email + * @property CI_Encrypt $encrypt + * @property CI_Encryption $encryption + * @property CI_Exceptions $exceptions + * @property CI_Hooks $hooks + * @property CI_Input $input + * @property CI_Lang $lang + * @property CI_Loader $load + * @property CI_Log $log + * @property CI_Migration $migration + * @property CI_Output $output + * @property CI_Profiler $profiler + * @property CI_Router $router + * @property CI_Security $security + * @property CI_Session $session + * @property CI_URI $uri + * @property CI_Upload $upload + * + * @property string $raw_input_stream + */ +class EA_Input extends CI_Input { + /** + * Fetch an item from JSON data. + * + * @param string $index Index for item to be fetched from the JSON payload. + * @param bool|false $xss_clean Whether to apply XSS filtering + * + * @return mixed + */ + public function json(string $index, bool $xss_clean = FALSE) + { + /** @var EA_Controller $CI */ + $CI = &get_instance(); + + if ($CI->input->get_request_header('Content-Type') !== 'application/json') + { + throw new RuntimeException('Cannot get JSON attribute from non-JSON content.'); + } + + $input_stream = $CI->input->raw_input_stream; + + if (empty($input_stream)) + { + throw new RuntimeException('Cannot get JSON attribute from an empty input stream.'); + } + + $payload = json_decode($input_stream, TRUE); + + $value = $payload[$index] ?? NULL; + + return $value && $xss_clean ? $CI->security->xss_clean($value) : $value; + } +} diff --git a/application/core/EA_Migration.php b/application/core/EA_Migration.php index 535f4541..bb414988 100644 --- a/application/core/EA_Migration.php +++ b/application/core/EA_Migration.php @@ -26,7 +26,7 @@ * @property CI_Encryption $encryption * @property CI_Exceptions $exceptions * @property CI_Hooks $hooks - * @property CI_Input $input + * @property EA_Input $input * @property CI_Lang $lang * @property CI_Loader $load * @property CI_Log $log @@ -38,27 +38,6 @@ * @property CI_Session $session * @property CI_URI $uri * @property CI_Upload $upload - * - * @property Admins_model $admins_model - * @property Appointments_model $appointments_model - * @property Consents_model $consents_model - * @property Customers_model $customers_model - * @property Providers_model $providers_model - * @property Roles_model $roles_model - * @property Secretaries_model $secretaries_model - * @property Service_categories_model $service_categories_model - * @property Services_model $services_model - * @property Settings_model $settings_model - * @property Users_model $users_model - * - * @property Accounts $accounts - * @property Availability $availability - * @property Google_Sync $google_sync - * @property Ics_file $ics_file - * @property Instance $instance - * @property Notifications $notifications - * @property Synchronization $synchronization - * @property Timezones $timezones */ class EA_Migration extends CI_Migration { // diff --git a/application/core/EA_Model.php b/application/core/EA_Model.php index 386115c2..27a0c735 100644 --- a/application/core/EA_Model.php +++ b/application/core/EA_Model.php @@ -26,7 +26,7 @@ * @property CI_Encryption $encryption * @property CI_Exceptions $exceptions * @property CI_Hooks $hooks - * @property CI_Input $input + * @property EA_Input $input * @property CI_Lang $lang * @property CI_Loader $load * @property CI_Log $log @@ -38,27 +38,6 @@ * @property CI_Session $session * @property CI_URI $uri * @property CI_Upload $upload - * - * @property Admins_model $admins_model - * @property Appointments_model $appointments_model - * @property Consents_model $consents_model - * @property Customers_model $customers_model - * @property Providers_model $providers_model - * @property Roles_model $roles_model - * @property Secretaries_model $secretaries_model - * @property Service_categories_model $service_categories_model - * @property Services_model $services_model - * @property Settings_model $settings_model - * @property Users_model $users_model - * - * @property Accounts $accounts - * @property Availability $availability - * @property Google_Sync $google_sync - * @property Ics_file $ics_file - * @property Instance $instance - * @property Notifications $notifications - * @property Synchronization $synchronization - * @property Timezones $timezones */ class EA_Model extends CI_Model { /** diff --git a/application/helpers/http_helper.php b/application/helpers/http_helper.php index 6a88cbb8..60595967 100644 --- a/application/helpers/http_helper.php +++ b/application/helpers/http_helper.php @@ -37,7 +37,7 @@ if ( ! function_exists('request')) throw new InvalidArgumentException('The $key argument cannot be empty.'); } - return $CI->input->post_get($key) ?? $default; + return $CI->input->post_get($key) ?? $CI->input->json($key) ?? $default; } }