dolibarr  17.0.4
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 
33 require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
34 require_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  $response = getURLContent($tmpobject->url, 'POST', $jsonstr, 1, array(), array('http', 'https'), 0, -1);
124  if (empty($response['curl_error_no']) && $response['http_code'] >= 200 && $response['http_code'] < 300) {
125  $nbPosts ++;
126  } else {
127  $errors ++;
128  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);
129  }
130  }
131  }
132  if (!empty($errors)) {
133  return $errors * -1;
134  }
135  return $nbPosts;
136  }
137 }
Class to stock current configuration.
Definition: conf.class.php:34
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.
Definition: user.class.php:47
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:41
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:122
$conf db
API class for accounts.
Definition: inc.php:41