2022-06-19 20:05:45 +03:00
|
|
|
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Open Source Web Scheduler
|
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
|
|
|
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
|
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link http://easyappointments.org
|
|
|
|
* @since v1.4.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Webhooks client library.
|
|
|
|
*
|
|
|
|
* Handles the webhook HTTP related functionality.
|
|
|
|
*
|
|
|
|
* @package Libraries
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
class Webhooks_client
|
|
|
|
{
|
2022-06-19 20:05:45 +03:00
|
|
|
/**
|
2023-03-13 11:06:18 +03:00
|
|
|
* @var EA_Controller|CI_Controller
|
2022-06-19 20:05:45 +03:00
|
|
|
*/
|
2023-03-13 11:06:18 +03:00
|
|
|
protected EA_Controller|CI_Controller $CI;
|
2022-06-19 20:05:45 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Webhook client constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
$this->CI = &get_instance();
|
2022-06-19 20:05:45 +03:00
|
|
|
|
|
|
|
$this->CI->load->model('providers_model');
|
|
|
|
$this->CI->load->model('secretaries_model');
|
|
|
|
$this->CI->load->model('secretaries_model');
|
|
|
|
$this->CI->load->model('admins_model');
|
|
|
|
$this->CI->load->model('appointments_model');
|
|
|
|
$this->CI->load->model('settings_model');
|
|
|
|
$this->CI->load->model('webhooks_model');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trigger the registered webhooks for the provided action.
|
|
|
|
*
|
|
|
|
* @param string $action Webhook action.
|
|
|
|
* @param array $payload Payload data.
|
|
|
|
*
|
|
|
|
* @return void|null
|
|
|
|
*/
|
|
|
|
public function trigger(string $action, array $payload)
|
|
|
|
{
|
|
|
|
$webhooks = $this->CI->webhooks_model->get();
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($webhooks as $webhook) {
|
|
|
|
if (str_contains($webhook['actions'], $action)) {
|
2022-06-19 20:05:45 +03:00
|
|
|
$this->call($webhook, $action, $payload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call the provided webhook.
|
|
|
|
*
|
|
|
|
* @param array $webhook
|
|
|
|
* @param string $action
|
|
|
|
* @param array $payload
|
|
|
|
*/
|
2023-03-13 11:06:18 +03:00
|
|
|
private function call(array $webhook, string $action, array $payload): void
|
2022-06-19 20:05:45 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
2022-06-19 20:05:45 +03:00
|
|
|
$client = new Client();
|
|
|
|
|
|
|
|
$client->post($webhook['url'], [
|
|
|
|
'verify' => $webhook['is_ssl_verified'],
|
|
|
|
'json' => [
|
|
|
|
'action' => $action,
|
2023-12-22 13:35:41 +03:00
|
|
|
'payload' => $payload,
|
|
|
|
],
|
2022-06-19 20:05:45 +03:00
|
|
|
]);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
|
|
|
log_message(
|
|
|
|
'error',
|
|
|
|
'Webhooks Client - The webhook (' .
|
|
|
|
($webhook['id'] ?? null) .
|
|
|
|
') request received an unexpected exception: ' .
|
2023-12-22 13:35:41 +03:00
|
|
|
$e->getMessage(),
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
2022-06-20 12:09:56 +03:00
|
|
|
log_message('error', $e->getTraceAsString());
|
2022-06-19 20:05:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|