BxDolAiTriggerWebhook.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. class BxDolAiTriggerWebhook extends BxDolAiTrigger
  10. {
  11. protected $_sType = 'webhook';
  12. protected function getSampleField()
  13. {
  14. return 'webhook_sample';
  15. }
  16. public function handle($mixed = null)
  17. {
  18. if (!BxDolAi::getInstance()) {
  19. header('Content-Type: application/json');
  20. header('HTTP/1.0 403 Forbidden');
  21. BxDolLanguages::getInstance();
  22. echo json_encode(['status' => 403, 'error' => _t("_Access denied")]);
  23. exit;
  24. }
  25. header('Content-Type: application/json');
  26. $aHeaders = function_exists('getallheaders') ? getallheaders() : false;
  27. if ($aHeaders) {
  28. $sAuthHeader = isset($aHeaders['Authorization']) ? $aHeaders['Authorization'] : (isset($aHeaders['authorization']) ? $aHeaders['authorization'] : false);
  29. } else {
  30. $sAuthHeader = isset($_SERVER['HTTP_AUTHORIZATION']) ? $_SERVER['HTTP_AUTHORIZATION'] : false;
  31. }
  32. $sKey = str_replace('Bearer ', '', $sAuthHeader);
  33. $aAgent = $sKey ? $this->getAgentByKey($sKey) : false;
  34. if (!$aAgent) {
  35. header('HTTP/1.0 403 Forbidden');
  36. BxDolLanguages::getInstance();
  37. echo json_encode(['status' => 403, 'error' => _t("_Access denied")]);
  38. exit;
  39. }
  40. $aParams = is_array($mixed) ? $mixed : $_REQUEST;
  41. $aParams['trigger'] = $this->getType();
  42. if (!empty($aAgent['async'])) {
  43. $this->enqueue($aAgent, $aParams);
  44. echo json_encode(['result' => 'scheduled']);
  45. return;
  46. }
  47. echo json_encode($this->call($aAgent, $aParams));
  48. }
  49. public function getAgentByKey($sKey)
  50. {
  51. $oDb = new BxDolAiQuery();
  52. return $oDb->getAgentByTriggerWebhookKey($sKey);
  53. }
  54. }
  55. /** @} */