interface_95_modWebhook_WebhookTriggers.class.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /* Copyright (C) 2022 SuperAdmin <test@dolibarr.com>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file core/triggers/interface_99_modWebhook_WebhookTriggers.class.php
  19. * \ingroup webhook
  20. * \brief Example trigger.
  21. *
  22. * Put detailed description here.
  23. *
  24. * \remarks You can create other triggers by copying this one.
  25. * - File name should be either:
  26. * - interface_99_modWebhook_MyTrigger.class.php
  27. * - interface_99_all_MyTrigger.class.php
  28. * - The file must stay in core/triggers
  29. * - The class name must be InterfaceMytrigger
  30. */
  31. require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/webhook/class/target.class.php';
  33. /**
  34. * Class of triggers for Webhook module
  35. */
  36. class InterfaceWebhookTriggers extends DolibarrTriggers
  37. {
  38. /**
  39. * Constructor
  40. *
  41. * @param DoliDB $db Database handler
  42. */
  43. public function __construct($db)
  44. {
  45. $this->db = $db;
  46. $this->name = preg_replace('/^Interface/i', '', get_class($this));
  47. $this->family = "demo";
  48. $this->description = "Webhook triggers.";
  49. // 'development', 'experimental', 'dolibarr' or version
  50. $this->version = 'development';
  51. $this->picto = 'webhook';
  52. }
  53. /**
  54. * Trigger name
  55. *
  56. * @return string Name of trigger file
  57. */
  58. public function getName()
  59. {
  60. return $this->name;
  61. }
  62. /**
  63. * Trigger description
  64. *
  65. * @return string Description of trigger file
  66. */
  67. public function getDesc()
  68. {
  69. return $this->description;
  70. }
  71. /**
  72. * Function called when a Dolibarrr business event is done.
  73. * All functions "runTrigger" are triggered if file
  74. * is inside directory core/triggers
  75. *
  76. * @param string $action Event action code
  77. * @param CommonObject $object Object
  78. * @param User $user Object user
  79. * @param Translate $langs Object langs
  80. * @param Conf $conf Object conf
  81. * @return int <0 if KO, 0 if no triggered ran, >0 if OK
  82. */
  83. public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
  84. {
  85. if (empty($conf->webhook) || empty($conf->webhook->enabled)) {
  86. return 0; // If module is not enabled, we do nothing
  87. }
  88. require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
  89. // Or you can execute some code here
  90. $nbPosts = 0;
  91. $errors = 0;
  92. $static_object = new Target($this->db);
  93. $target_url = $static_object->fetchAll();
  94. foreach ($target_url as $key => $tmpobject) {
  95. $actionarray = explode(",", $tmpobject->trigger_codes);
  96. if (is_array($actionarray) && in_array($action, $actionarray)) {
  97. // Build the answer object
  98. $resobject = new stdClass();
  99. $resobject->triggercode = $action;
  100. $resobject->object = dol_clone($object, 2);
  101. if (property_exists($resobject->object, 'fields')) {
  102. unset($resobject->object->fields);
  103. }
  104. if (property_exists($resobject->object, 'error')) {
  105. unset($resobject->object->error);
  106. }
  107. if (property_exists($resobject->object, 'errors')) {
  108. unset($resobject->object->errors);
  109. }
  110. $jsonstr = json_encode($resobject);
  111. $response = getURLContent($tmpobject->url, 'POST', $jsonstr, 1, array(), array('http', 'https'), 0, -1);
  112. if (empty($response['curl_error_no']) && $response['http_code'] >= 200 && $response['http_code'] < 300) {
  113. $nbPosts ++;
  114. } else {
  115. $errors ++;
  116. dol_syslog("Failed to get url with httpcode=".(!empty($response['http_code']) ? $response['http_code'] : "")." curl_error_no=".(!empty($response['curl_error_no']) ? $response['curl_error_no'] : ""), LOG_DEBUG);
  117. }
  118. }
  119. }
  120. if (!empty($errors)) {
  121. return $errors * -1;
  122. }
  123. return $nbPosts;
  124. }
  125. }