dolibarr 20.0.0
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
34require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
35require_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 ($tmpobject->status == Target::STATUS_VALIDATED && 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 $headers = array(
100 'Content-Type: application/json'
101 //'Accept: application/json'
102 );
103
104 $method = 'POST';
105 if (getDolGlobalString('WEBHOOK_POST_SEND_DATA_IN_BODY')) {
106 $method = 'POSTALREADYFORMATED';
107 }
108 $response = getURLContent($tmpobject->url, $method, $jsonstr, 1, $headers, array('http', 'https'), 2, -1);
109
110 if (empty($response['curl_error_no']) && $response['http_code'] >= 200 && $response['http_code'] < 300) {
111 $nbPosts++;
112 } else {
113 $errors++;
114 $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'] : "");
115 $this->errors[] = $errormsg;
116 /*if (!empty($response['content'])) {
117 $this->errors[] = dol_trunc($response['content'], 200);
118 }*/
119 dol_syslog($errormsg, LOG_ERR);
120 }
121 }
122 }
123 if (!empty($errors)) {
124 return $errors * -1;
125 }
126 return $nbPosts;
127 }
128}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Class to stock current configuration.
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.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after 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 =...
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
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).