NEW Test send triggercode to url (#29675)

* New test send triggercode to url

* Update webhook.php

---------

Co-authored-by: Hystepik <lmarcouiller@nltechno.com>
Co-authored-by: Laurent Destailleur <eldy@destailleur.fr>
This commit is contained in:
Lucas Marcouiller 2024-05-15 16:27:46 +02:00 committed by GitHub
parent 62e17797de
commit 97bd47a589
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 232 additions and 1 deletions

View file

@ -2484,3 +2484,6 @@ ZapierForDolibarrSetup=Setup of Zapier for Dolibarr
ZapierDescription=Interface with Zapier
ZapierAbout=About the module Zapier
ZapierSetupPage=There is no need for a setup on Dolibarr side to use Zapier. However, you must generate and publish a package on zapier to be able to use Zapier with Dolibarr. See documentation on <a href="https://wiki.dolibarr.org/index.php/Module_Zapier">this wiki page</a>.
TestWebhookTarget=Test triggerable event
DataToSendTrigger=Data sent to Url
SendToUrl=Send to Url

View file

@ -0,0 +1,120 @@
<?php
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* \file /htdocs/webhook/ajax/webhook.php
* \brief File to make Ajax action on webhook
*/
if (!defined('NOTOKENRENEWAL')) {
define('NOTOKENRENEWAL', '1'); // Disables token renewal
}
if (!defined('NOREQUIREHTML')) {
define('NOREQUIREHTML', '1');
}
if (!defined('NOREQUIREAJAX')) {
define('NOREQUIREAJAX', '1');
}
if (!defined('NOREQUIRESOC')) {
define('NOREQUIRESOC', '1');
}
// Do not check anti CSRF attack test
if (!defined('NOREQUIREMENU')) {
define('NOREQUIREMENU', '1');
}
// If we need access without being logged.
if (!empty($_GET['public'])) { // Keep $_GET here. GETPOST() is not yet defined so we use $_GET
if (!defined("NOLOGIN")) {
define("NOLOGIN", '1');
}
}
if (!defined('NOIPCHECK')) {
define('NOIPCHECK', '1'); // Do not check IP defined into conf $dolibarr_main_restrict_ip
}
if (!defined('NOBROWSERNOTIF')) {
define('NOBROWSERNOTIF', '1');
}
include '../../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/webhook/class/target.class.php';
$action = GETPOST('action', 'aZ09');
$triggercode = GETPOST('triggercode');
// Security check
if (empty($user->admin)) {
access_forbidden();
}
/*
* Actions
*/
// None
/*
* View
*/
top_httphead('application/json');
if ($action == "getjsonformtrigger") {
$response = '';
$objnotfound = 0;
$json = new stdClass();
if (!empty($triggercode)) {
// Clean triggercode to removes keep only Object trigger name
$objecttriggername = array();
preg_match('#\((.*?)\)#', $triggercode, $objecttriggername);
$json->triggercode = !empty($objecttriggername[1]) ? $objecttriggername[1] : $triggercode;
if ($objecttriggername[1]) {
$objtype = explode("_", $objecttriggername[1])[0];
$obj = findobjecttosend($objtype);
if (is_object($obj)) {
//TODO: Case if obj is an object
} else {
$objnotfound ++;
}
} else {
$objnotfound ++;
}
if ($objnotfound) {
$json->object = new Target($db);
$json->object->initAsSpecimen();
unset($json->object->db);
unset($json->object->fields);
unset($json->object->error);
unset($json->object->errors);
}
}
$response = json_encode($json);
echo $response;
}
/**
* Find and init a specimen for the given object type
*
* @param string $objecttype Object type to init as a specimen
* @return object|false
*/
function findobjecttosend($objecttype)
{
// TODO: Find right object from objecttype and initAsSpecimen
return false;
}

View file

@ -29,11 +29,12 @@ require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php';
require_once DOL_DOCUMENT_ROOT.'/webhook/class/target.class.php';
require_once DOL_DOCUMENT_ROOT.'/webhook/lib/webhook_target.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
global $conf, $db, $hookmanager, $langs, $user;
// Load translation files required by the page
$langs->loadLangs(array('other'));
$langs->loadLangs(array('other','admin'));
// Get parameters
$id = GETPOSTINT('id');
@ -155,6 +156,27 @@ if (empty($reshook)) {
$object->setProject(GETPOSTINT('projectid'));
}
if ($action == 'testsendtourl' && $permissiontoadd) {
$triggercode = GETPOST("triggercode");
$url = GETPOST("url");
$jsondata = GETPOST("jsondata");
if (empty($url)) {
$error++;
setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Url")), null, 'errors');
}
if (empty($jsondata)) {
$error++;
setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("DataToSendTrigger")), null, 'errors');
}
$response = getURLContent($url, 'POST', $jsondata, 1, array('content-type:application/json'), array('http', 'https'), 0, -1);
if (empty($response['curl_error_no']) && $response['http_code'] >= 200 && $response['http_code'] < 300) {
setEventMessages($langs->trans("Success"), null);
} else {
$errormsg = "The WebHook for triggercode: ".$triggercode." failed to get URL ".$url." with httpcode=".(!empty($response['http_code']) ? $response['http_code'] : "")." curl_error_no=".(!empty($response['curl_error_no']) ? $response['curl_error_no'] : "");
setEventMessages($langs->trans($errormsg), null, 'errors');
}
}
// Actions to send emails
$triggersendname = 'WEBHOOK_TARGET_SENTBYMAIL';
$autocopy = 'MAIN_MAIL_AUTOCOPY_TARGET_TO';
@ -495,6 +517,8 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
// Clone
print dolGetButtonAction($langs->trans('ToClone'), '', 'default', $_SERVER['PHP_SELF'].'?id='.$object->id.'&action=clone&token='.newToken(), '', $permissiontoadd);
// Webhook send test
print dolGetButtonAction($langs->trans('TestWebhookTarget'), '', 'default', $_SERVER['PHP_SELF'].'?id='.$object->id.'&action=test&token='.newToken(), '', $permissiontoadd);
/*
if ($permissiontoadd) {
if ($object->status == $object::STATUS_ENABLED) {
@ -519,6 +543,90 @@ if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'crea
}
}
if ($action == "test") {
print '<div id="formtesttarget" name="formtesttarget"></div>';
print load_fiche_titre($langs->trans("TestWebhookTarget"));
print dol_get_fiche_head(array(), '', '', -1);
print "\n".'<!-- Begin form test target --><div id="targettestform"></div>'."\n";
print '<form method="POST" name="testtargetform" id="testtargetform" enctype="multipart/form-data" action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'">';
print '<input type="hidden" name="token" value="'.newToken().'">';
print '<input type="hidden" name="action" value="testsendtourl">';
print '<table class="tableforemailform boxtablenotop centpercent">';
print '<tr><td class="titlefieldcreate fieldrequired minwidth200">';
print $langs->trans("TriggerCodes");
print '</td><td class="valuefieldcreate">';
$arraytriggercodes = explode(",", $object->trigger_codes);
$idtriggercode = '';
if (in_array(GETPOST("triggercodes"), $arraytriggercodes)) {
$idtriggercode = array_search(GETPOST("triggercodes"), $arraytriggercodes);
}
print $form->selectarray("triggercode", $arraytriggercodes, $idtriggercode, 0, 0, 1);
print '</td></tr>';
print '<tr><td class="titlefieldcreate fieldrequired minwidth200">';
print $langs->trans("Url");
print '</td><td class="valuefieldcreate">';
print '<input class="flat minwidth400" name="url" value="'.(GETPOSTISSET("url") ? GETPOST("url") : $object->url).'" />';
print '</td></tr>';
print '<tr><td class="titlefieldcreate fieldrequired minwidth200">';
print $langs->trans("DataToSendTrigger");
print '</td><td>';
print '<textarea id="jsondatasendtarget" class="flat minwidth100" style="margin-top: 5px; width: 95%" rows="8" name="jsondata">';
if (!$conf->use_javascript_ajax) {
$json = new stdClass();
$json->triggercode = "TEST_TRIGGER_CODE";
$json->object = new Target($db);
$json->object->initAsSpecimen();
unset($json->object->db);
unset($json->object->fields);
unset($json->object->error);
unset($json->object->errors);
$datatosend = json_encode($json);
print $datatosend;
}
print '</textarea>';
print '</td></tr>';
print '</table>';
print '<div class="center">';
print $form->buttonsSaveCancel("SendToUrl");
print '</div>';
print '</form>';
if ($conf->use_javascript_ajax) {
print '<script>
$("#triggercode").change(function(){
triggercode = $(this).val();
getDatatToSendTriggerCode(triggercode);
});
function getDatatToSendTriggerCode(triggercode){
$.ajax({
method: \'GET\',
url: \''.DOL_URL_ROOT.'/webhook/ajax/webhook.php\',
data: { action: "getjsonformtrigger", triggercode: triggercode },
success: function(response) {
obj = JSON.stringify(response);
$("#jsondatasendtarget").val(obj);
}
})
};
$(document).ready(function () {
triggercode = $("#triggercode").val();
getDatatToSendTriggerCode(triggercode);
});
';
print '</script>';
}
print "\n".'<!-- END form test target -->';
}
// End of page
llxFooter();
$db->close();