diff --git a/htdocs/langs/en_US/admin.lang b/htdocs/langs/en_US/admin.lang index 66c830a9045..17cffe3cc60 100644 --- a/htdocs/langs/en_US/admin.lang +++ b/htdocs/langs/en_US/admin.lang @@ -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 this wiki page. +TestWebhookTarget=Test triggerable event +DataToSendTrigger=Data sent to Url +SendToUrl=Send to Url diff --git a/htdocs/webhook/ajax/webhook.php b/htdocs/webhook/ajax/webhook.php new file mode 100644 index 00000000000..2945f4c6abd --- /dev/null +++ b/htdocs/webhook/ajax/webhook.php @@ -0,0 +1,120 @@ +. + */ + +/** + * \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; +} diff --git a/htdocs/webhook/target_card.php b/htdocs/webhook/target_card.php index 0a8d43f85a6..6c499d2e7b9 100644 --- a/htdocs/webhook/target_card.php +++ b/htdocs/webhook/target_card.php @@ -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 '
'; + print load_fiche_titre($langs->trans("TestWebhookTarget")); + print dol_get_fiche_head(array(), '', '', -1); + + print "\n".''."\n"; + print ''; + + if ($conf->use_javascript_ajax) { + print ''; + } + + print "\n".''; +} + // End of page llxFooter(); $db->close();