dolibarr 21.0.0-alpha
webhook.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
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
23if (!defined('NOTOKENRENEWAL')) {
24 define('NOTOKENRENEWAL', '1'); // Disables token renewal
25}
26if (!defined('NOREQUIREHTML')) {
27 define('NOREQUIREHTML', '1');
28}
29if (!defined('NOREQUIREAJAX')) {
30 define('NOREQUIREAJAX', '1');
31}
32if (!defined('NOREQUIRESOC')) {
33 define('NOREQUIRESOC', '1');
34}
35// Do not check anti CSRF attack test
36if (!defined('NOREQUIREMENU')) {
37 define('NOREQUIREMENU', '1');
38}
39// If we need access without being logged.
40if (!empty($_GET['public'])) { // Keep $_GET here. GETPOST() is not yet defined so we use $_GET
41 if (!defined("NOLOGIN")) {
42 define("NOLOGIN", '1');
43 }
44}
45if (!defined('NOIPCHECK')) {
46 define('NOIPCHECK', '1'); // Do not check IP defined into conf $dolibarr_main_restrict_ip
47}
48if (!defined('NOBROWSERNOTIF')) {
49 define('NOBROWSERNOTIF', '1');
50}
51include '../../main.inc.php';
52require_once DOL_DOCUMENT_ROOT.'/webhook/class/target.class.php';
53
54$action = GETPOST('action', 'aZ09');
55$triggercode = GETPOST('triggercode');
56
57// Security check
58if (empty($user->admin)) {
60}
61
62
63/*
64 * Actions
65 */
66
67// None
68
69
70/*
71 * View
72 */
73
74top_httphead('application/json');
75
76if ($action == "getjsonformtrigger") {
77 $response = '';
78 $objnotfound = 0;
79
80 $json = new stdClass();
81
82 if (!empty($triggercode)) {
83 // Clean triggercode to removes keep only Object trigger name
84 $objecttriggername = array();
85 preg_match('#\‍((.*?)\‍)#', $triggercode, $objecttriggername);
86
87 $json->triggercode = empty($objecttriggername[1]) ? $triggercode : $objecttriggername[1];
88
89 if (!empty($json->triggercode)) {
90 $objtype = explode("_", $json->triggercode)[0];
91 $obj = findobjecttosend($objtype);
92 if (is_object($obj)) {
93 dol_syslog("Ajax webhook: We clean object fetched");
94 $properties = dol_get_object_properties($obj);
95 foreach ($properties as $key => $property) {
96 if (empty($property)) {
97 unset($obj->$key);
98 }
99 }
100 unset($obj->db);
101 unset($obj->fields);
102 unset($obj->table_element);
103 unset($obj->picto);
104 unset($obj->isextrafieldmanaged);
105 unset($obj->ismultientitymanaged);
106
107 $json->object = $obj;
108 } else {
109 $objnotfound ++;
110 }
111 } else {
112 $objnotfound ++;
113 }
114
115 if ($objnotfound) {
116 dol_syslog("Ajax webhook: Class not found for trigger code ".$json->triggercode);
117 $json->object = new stdClass();
118 $json->object->field1 = 'field1';
119 $json->object->field2 = 'field2';
120 $json->object->field3 = 'field3';
121 }
122 }
123
124 $response = json_encode($json);
125 echo $response;
126}
127
134function findobjecttosend($objecttype)
135{
136 dol_syslog("Ajax webhook: We fetch object of type = ".$objecttype." and we init it as specimen");
137 $obj = fetchObjectByElement(0, dol_strtolower($objecttype));
138 if (is_object($obj)) {
139 $obj->initAsSpecimen();
140 } else {
141 return false;
142 }
143 return $obj;
144}
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:134