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 * 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';
54
55$action = GETPOST('action', 'aZ09');
56$triggercode = GETPOST('triggercode');
57
58// Security check
59if (empty($user->admin)) {
61}
62
63
64/*
65 * Actions
66 */
67
68// None
69
70
71/*
72 * View
73 */
74
75top_httphead('application/json');
76
77if ($action == "getjsonformtrigger") {
78 $response = '';
79 $objnotfound = 0;
80
81 $json = new stdClass();
82
83 if (!empty($triggercode)) {
84 // Clean triggercode to removes keep only Object trigger name
85 $objecttriggername = array();
86 preg_match('#\‍((.*?)\‍)#', $triggercode, $objecttriggername);
87
88 $json->triggercode = empty($objecttriggername[1]) ? $triggercode : $objecttriggername[1];
89
90 if (!empty($json->triggercode)) {
91 $objtype = explode("_", $json->triggercode)[0];
92 $obj = findobjecttosend($objtype);
93 if (is_object($obj)) {
94 dol_syslog("Ajax webhook: We clean object fetched");
95 $properties = dol_get_object_properties($obj);
96 foreach ($properties as $key => $property) {
97 if (empty($property)) {
98 unset($obj->$key);
99 }
100 }
101 unset($obj->db);
102 unset($obj->fields);
103 unset($obj->table_element);
104 unset($obj->picto);
105 unset($obj->isextrafieldmanaged);
106 unset($obj->ismultientitymanaged);
107
108 $json->object = $obj;
109 } else {
110 $objnotfound++;
111 }
112 } else {
113 $objnotfound++;
114 }
115
116 if ($objnotfound) {
117 dol_syslog("Ajax webhook: Class not found for trigger code ".$json->triggercode);
118 $json->object = new stdClass();
119 $json->object->field1 = 'field1';
120 $json->object->field2 = 'field2';
121 $json->object->field3 = 'field3';
122 }
123 }
124
125 $response = json_encode($json);
126 echo $response;
127}
128
135function findobjecttosend($objecttype)
136{
137 dol_syslog("Ajax webhook: We fetch object of type = ".$objecttype." and we init it as specimen");
138 $obj = fetchObjectByElement(0, dol_strtolower($objecttype));
139 if (is_object($obj)) {
140 '@phan-var-force CommonObject $obj';
141 $obj->initAsSpecimen();
142 } else {
143 return false;
144 }
145 return $obj;
146}
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:135