2021-10-23 20:08:08 +03:00
|
|
|
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
2022-01-18 15:05:42 +03:00
|
|
|
* Easy!Appointments - Online Appointment Scheduler
|
2021-10-23 20:08:08 +03:00
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2021-12-18 19:43:45 +03:00
|
|
|
* @copyright Copyright (c) Alex Tselegidis
|
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link https://easyappointments.org
|
2021-10-23 20:08:08 +03:00
|
|
|
* @since v1.5.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!function_exists('request')) {
|
2021-10-23 20:08:08 +03:00
|
|
|
/**
|
|
|
|
* Gets the value of a request variable.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* $first_name = request('first_name', 'John');
|
|
|
|
*
|
|
|
|
* @param string|null $key Request variable key.
|
2021-11-03 10:21:53 +03:00
|
|
|
* @param mixed|null $default Default value in case the requested variable has no value.
|
2021-10-23 20:08:08 +03:00
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
2024-12-19 21:13:51 +03:00
|
|
|
function request(?string $key = null, $default = null): mixed
|
2021-10-23 20:08:08 +03:00
|
|
|
{
|
|
|
|
/** @var EA_Controller $CI */
|
|
|
|
$CI = &get_instance();
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($key)) {
|
2021-11-03 10:21:53 +03:00
|
|
|
$payload = $CI->input->post_get($key);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($payload)) {
|
2021-11-03 10:21:53 +03:00
|
|
|
$payload = $CI->input->json($key);
|
|
|
|
}
|
2023-11-29 12:24:09 +03:00
|
|
|
|
2021-11-03 10:21:53 +03:00
|
|
|
return $payload;
|
2021-10-23 20:08:08 +03:00
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
return $CI->input->post_get($key) ?? ($CI->input->json($key) ?? $default);
|
2021-10-23 20:08:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!function_exists('response')) {
|
2021-10-23 20:08:08 +03:00
|
|
|
/**
|
|
|
|
* Return a new response from the application.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* response('This is the response content', 200, []);
|
|
|
|
*
|
|
|
|
* @param string $content
|
|
|
|
* @param int $status
|
|
|
|
* @param array $headers
|
|
|
|
*/
|
2024-06-20 15:20:10 +03:00
|
|
|
function response(string $content = '', int $status = 200, array $headers = []): void
|
2021-10-23 20:08:08 +03:00
|
|
|
{
|
|
|
|
/** @var EA_Controller $CI */
|
|
|
|
$CI = &get_instance();
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($headers as $header) {
|
2021-11-05 10:31:37 +03:00
|
|
|
$CI->output->set_header($header);
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$CI->output->set_status_header($status)->set_output($content);
|
2021-10-23 20:08:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!function_exists('response')) {
|
2021-10-23 20:08:08 +03:00
|
|
|
/**
|
|
|
|
* Return a new response from the application.
|
|
|
|
*
|
|
|
|
* @param string $content
|
|
|
|
* @param int $status
|
|
|
|
* @param array $headers
|
|
|
|
*/
|
2024-06-20 15:20:10 +03:00
|
|
|
function response(string $content = '', int $status = 200, array $headers = []): void
|
2021-10-23 20:08:08 +03:00
|
|
|
{
|
|
|
|
/** @var EA_Controller $CI */
|
|
|
|
$CI = &get_instance();
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($headers as $header) {
|
2021-10-23 20:08:08 +03:00
|
|
|
$CI->output->set_header($header);
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$CI->output->set_status_header($status)->set_output($content);
|
2021-10-23 20:08:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!function_exists('json_response')) {
|
2021-10-23 20:08:08 +03:00
|
|
|
/**
|
|
|
|
* Return a new response from the application.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* json_response([
|
|
|
|
* 'message' => 'This is a JSON property.'
|
|
|
|
* ]);
|
|
|
|
*
|
|
|
|
* @param array $content
|
|
|
|
* @param int $status
|
|
|
|
* @param array $headers
|
|
|
|
*/
|
2024-06-20 15:20:10 +03:00
|
|
|
function json_response(array $content = [], int $status = 200, array $headers = []): void
|
2021-10-23 20:08:08 +03:00
|
|
|
{
|
|
|
|
/** @var EA_Controller $CI */
|
|
|
|
$CI = &get_instance();
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($headers as $header) {
|
2021-10-23 20:08:08 +03:00
|
|
|
$CI->output->set_header($header);
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$CI->output
|
2021-10-23 20:08:08 +03:00
|
|
|
->set_status_header($status)
|
|
|
|
->set_content_type('application/json')
|
2021-10-27 12:44:45 +03:00
|
|
|
->set_output(json_encode($content));
|
2021-10-23 20:08:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!function_exists('json_exception')) {
|
2021-10-23 20:08:08 +03:00
|
|
|
/**
|
|
|
|
* Return a new json exception from the application.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* json_exception($exception); // Add this in a catch block to return the exception information.
|
|
|
|
*
|
2022-06-20 12:31:52 +03:00
|
|
|
* @param Throwable $e
|
2021-10-23 20:08:08 +03:00
|
|
|
*/
|
2024-06-20 15:20:10 +03:00
|
|
|
function json_exception(Throwable $e): void
|
2021-10-23 20:08:08 +03:00
|
|
|
{
|
2024-06-20 15:20:10 +03:00
|
|
|
$response = [
|
|
|
|
'success' => false,
|
|
|
|
'message' => $e->getMessage(),
|
|
|
|
'trace' => config('debug') ? $e->getTrace() : [],
|
|
|
|
];
|
|
|
|
|
|
|
|
log_message('error', 'JSON exception: ' . json_encode($response));
|
|
|
|
|
|
|
|
json_response($response, 500);
|
2021-10-23 20:08:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!function_exists('abort')) {
|
2021-10-23 20:08:08 +03:00
|
|
|
/**
|
|
|
|
* Throw an HttpException with the given data.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* if ($error) abort(500);
|
|
|
|
*
|
|
|
|
* @param int $code
|
|
|
|
* @param string $message
|
|
|
|
* @param array $headers
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2024-06-20 15:20:10 +03:00
|
|
|
function abort(int $code, string $message = '', array $headers = []): void
|
2021-10-23 20:08:08 +03:00
|
|
|
{
|
|
|
|
/** @var EA_Controller $CI */
|
|
|
|
$CI = &get_instance();
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($headers as $header) {
|
2021-10-23 20:08:08 +03:00
|
|
|
$CI->output->set_header($header);
|
|
|
|
}
|
|
|
|
|
|
|
|
show_error($message, $code);
|
|
|
|
}
|
|
|
|
}
|