dolibarr 21.0.0-beta
webhook.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
3 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
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
24if (!defined('NOTOKENRENEWAL')) {
25 define('NOTOKENRENEWAL', '1'); // Disables token renewal
26}
27if (!defined('NOREQUIREHTML')) {
28 define('NOREQUIREHTML', '1');
29}
30if (!defined('NOREQUIREAJAX')) {
31 define('NOREQUIREAJAX', '1');
32}
33if (!defined('NOREQUIRESOC')) {
34 define('NOREQUIRESOC', '1');
35}
36// Do not check anti CSRF attack test
37if (!defined('NOREQUIREMENU')) {
38 define('NOREQUIREMENU', '1');
39}
40// If we need access without being logged.
41if (!empty($_GET['public'])) { // Keep $_GET here. GETPOST() is not yet defined so we use $_GET
42 if (!defined("NOLOGIN")) {
43 define("NOLOGIN", '1');
44 }
45}
46if (!defined('NOIPCHECK')) {
47 define('NOIPCHECK', '1'); // Do not check IP defined into conf $dolibarr_main_restrict_ip
48}
49if (!defined('NOBROWSERNOTIF')) {
50 define('NOBROWSERNOTIF', '1');
51}
52include '../../main.inc.php';
53require_once DOL_DOCUMENT_ROOT.'/webhook/class/target.class.php';
62$action = GETPOST('action', 'aZ09');
63$triggercode = GETPOST('triggercode');
64
65// Security check
66if (empty($user->admin)) {
68}
69
70
71/*
72 * Actions
73 */
74
75// None
76
77
78/*
79 * View
80 */
81
82top_httphead('application/json');
83
84if ($action == "getjsonformtrigger") {
85 $response = '';
86 $objnotfound = 0;
87
88 $json = new stdClass();
89
90 if (!empty($triggercode)) {
91 // Clean triggercode to removes keep only Object trigger name
92 $objecttriggername = array();
93 preg_match('#\‍((.*?)\‍)#', $triggercode, $objecttriggername);
94
95 $json->triggercode = empty($objecttriggername[1]) ? $triggercode : $objecttriggername[1];
96
97 if (!empty($json->triggercode)) {
98 $objtype = explode("_", $json->triggercode)[0];
99 $obj = findobjecttosend($objtype);
100 if (is_object($obj)) {
101 dol_syslog("Ajax webhook: We clean object fetched");
102 $properties = dol_get_object_properties($obj);
103 foreach ($properties as $key => $property) {
104 if (empty($property)) {
105 unset($obj->$key);
106 }
107 }
108 unset($obj->db);
109 unset($obj->fields);
110 unset($obj->table_element);
111 unset($obj->picto);
112 unset($obj->isextrafieldmanaged);
113 unset($obj->ismultientitymanaged);
114
115 $json->object = $obj;
116 } else {
117 $objnotfound++;
118 }
119 } else {
120 $objnotfound++;
121 }
122
123 if ($objnotfound) {
124 dol_syslog("Ajax webhook: Class not found for trigger code ".$json->triggercode);
125 $json->object = new stdClass();
126 $json->object->field1 = 'field1';
127 $json->object->field2 = 'field2';
128 $json->object->field3 = 'field3';
129 }
130 }
131
132 $response = json_encode($json);
133 echo $response;
134}
135
142function findobjecttosend($objecttype)
143{
144 dol_syslog("Ajax webhook: We fetch object of type = ".$objecttype." and we init it as specimen");
145 $obj = fetchObjectByElement(0, dol_strtolower($objecttype));
146 if (is_object($obj)) {
147 '@phan-var-force CommonObject $obj';
148 $obj->initAsSpecimen();
149 } else {
150 return false;
151 }
152 return $obj;
153}
dol_strtolower($string, $encoding="UTF-8")
Convert a string to lower.
dol_get_object_properties($obj, $properties=[])
Get properties for an object - including magic properties when requested.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
fetchObjectByElement($element_id, $element_type, $element_ref='', $useCache=0, $maxCacheByType=10)
Fetch an object from its id and element_type Inclusion of classes is automatic.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.
if( $action=="getjsonformtrigger") findobjecttosend($objecttype)
Find and init a specimen for the given object type.
Definition webhook.php:142