dolibarr 19.0.3
interface_95_modWebhook_WebhookTriggers.class.php
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
33require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
34require_once DOL_DOCUMENT_ROOT.'/webhook/class/target.class.php';
35
40{
46 public function __construct($db)
47 {
48 $this->db = $db;
49
50 $this->name = preg_replace('/^Interface/i', '', get_class($this));
51 $this->family = "demo";
52 $this->description = "Webhook triggers.";
53 // 'development', 'experimental', 'dolibarr' or version
54 $this->version = 'development';
55 $this->picto = 'webhook';
56 }
57
63 public function getName()
64 {
65 return $this->name;
66 }
67
73 public function getDesc()
74 {
75 return $this->description;
76 }
77
78
91 public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
92 {
93 if (empty($conf->webhook) || empty($conf->webhook->enabled)) {
94 return 0; // If module is not enabled, we do nothing
95 }
96 require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
97
98 // Or you can execute some code here
99 $nbPosts = 0;
100 $errors = 0;
101 $static_object = new Target($this->db);
102 $target_url = $static_object->fetchAll();
103 foreach ($target_url as $key => $tmpobject) {
104 $actionarray = explode(",", $tmpobject->trigger_codes);
105 if (is_array($actionarray) && in_array($action, $actionarray)) {
106 // Build the answer object
107 $resobject = new stdClass();
108 $resobject->triggercode = $action;
109 $resobject->object = dol_clone($object, 2);
110
111 if (property_exists($resobject->object, 'fields')) {
112 unset($resobject->object->fields);
113 }
114 if (property_exists($resobject->object, 'error')) {
115 unset($resobject->object->error);
116 }
117 if (property_exists($resobject->object, 'errors')) {
118 unset($resobject->object->errors);
119 }
120
121 $jsonstr = json_encode($resobject);
122
123 $headers = array(
124 'Content-Type: application/json'
125 //'Accept: application/json'
126 );
127
128 $response = getURLContent($tmpobject->url, 'POST', $jsonstr, 1, $headers, array('http', 'https'), 2, -1);
129 if (empty($response['curl_error_no']) && $response['http_code'] >= 200 && $response['http_code'] < 300) {
130 $nbPosts++;
131 } else {
132 $errors++;
133 $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'] : "");
134 $this->errors[] = $errormsg;
135 /*if (!empty($response['content'])) {
136 $this->errors[] = dol_trunc($response['content'], 200);
137 }*/
138 dol_syslog($errormsg, LOG_ERR);
139 }
140 }
141 }
142 if (!empty($errors)) {
143 return $errors * -1;
144 }
145 return $nbPosts;
146 }
147}
Class to stock current configuration.
Class that all the triggers must extend.
runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
Function called when a Dolibarrr 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 =...
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).
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:124