dolibarr  20.0.0-beta
interface_95_modWebhook_WebhookTriggers.class.php
1 <?php
2 /* Copyright (C) 2022 SuperAdmin <test@dolibarr.com>
3  * Copyright (C) 2023 William Mead <william.mead@manchenumerique.fr>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
34 require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
35 require_once DOL_DOCUMENT_ROOT.'/webhook/class/target.class.php';
36 
41 {
47  public function __construct($db)
48  {
49  parent::__construct($db);
50  $this->family = "demo";
51  $this->description = "Webhook triggers.";
52  $this->version = self::VERSIONS['dev'];
53  $this->picto = 'webhook';
54  }
55 
67  public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
68  {
69  if (empty($conf->webhook) || empty($conf->webhook->enabled)) {
70  return 0; // If module is not enabled, we do nothing
71  }
72  require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
73 
74  // Or you can execute some code here
75  $nbPosts = 0;
76  $errors = 0;
77  $static_object = new Target($this->db);
78  $target_url = $static_object->fetchAll();
79  foreach ($target_url as $key => $tmpobject) {
80  $actionarray = explode(",", $tmpobject->trigger_codes);
81  if (is_array($actionarray) && in_array($action, $actionarray)) {
82  // Build the answer object
83  $resobject = new stdClass();
84  $resobject->triggercode = $action;
85  $resobject->object = dol_clone($object, 2);
86 
87  if (property_exists($resobject->object, 'fields')) {
88  unset($resobject->object->fields);
89  }
90  if (property_exists($resobject->object, 'error')) {
91  unset($resobject->object->error);
92  }
93  if (property_exists($resobject->object, 'errors')) {
94  unset($resobject->object->errors);
95  }
96 
97  $jsonstr = json_encode($resobject);
98 
99  $response = getURLContent($tmpobject->url, 'POST', $jsonstr, 1, array('content-type:application/json'), array('http', 'https'), 2, -1);
100  if (empty($response['curl_error_no']) && $response['http_code'] >= 200 && $response['http_code'] < 300) {
101  $nbPosts++;
102  } else {
103  $errors++;
104  $errormsg = "The WebHook for ".$action." failed to get URL ".$tmpobject->url." with httpcode=".(!empty($response['http_code']) ? $response['http_code'] : "")." curl_error_no=".(!empty($response['curl_error_no']) ? $response['curl_error_no'] : "");
105  $this->errors[] = $errormsg;
106  /*if (!empty($response['content'])) {
107  $this->errors[] = dol_trunc($response['content'], 200);
108  }*/
109  dol_syslog($errormsg, LOG_ERR);
110  }
111  }
112  }
113  if (!empty($errors)) {
114  return $errors * -1;
115  }
116  return $nbPosts;
117  }
118 }
if($user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition: card.php:58
Class to stock current configuration.
Definition: conf.class.php:34
Class that all triggers must inherit.
runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
Function called when a Dolibarr business event is done.
Class for Target.
Class to manage translations.
Class to manage Dolibarr users.
Definition: user.class.php:50
print *****$script_file(".$version.") pid cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_clone($object, $native=0)
Create a clone of instance of object (new instance with same value for each properties) With native =...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getURLContent($url, $postorget='GET', $param='', $followlocation=1, $addheaders=array(), $allowedschemes=array('http', 'https'), $localurl=0, $ssl_verifypeer=-1)
Function to get a content from an URL (use proxy if proxy defined).
Definition: geturl.lib.php:42