Webhooks/master_webhook.php hinzugefügt

This commit is contained in:
2026-07-20 20:35:41 +00:00
parent 487183b988
commit 56e147e1c2
+152
View File
@@ -0,0 +1,152 @@
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$config = require_once __DIR__ . '/../config.php';
function logMasterDebug($message) {
file_put_contents('webhook_master_debug.log', date('Y-m-d H:i:s') . " - " . $message . PHP_EOL, FILE_APPEND);
}
logMasterDebug("Master-Webhook aufgerufen.");
$api_key = $config['api_key'];
$webhook_secret = $config['webhook_secret'];
$nextcloud_user = $config['nextcloud']['user'];
$nextcloud_pass = $config['nextcloud']['pass'];
$nextcloud_urls = $config['nextcloud']['urls'];
$signature = '';
if (function_exists('getallheaders')) {
$headers = array_change_key_case(getallheaders(), CASE_LOWER);
$signature = $headers['x-docuseal-signature'] ?? '';
}
if (empty($signature)) {
$signature = $_SERVER['HTTP_X_DOCUSEAL_SIGNATURE'] ?? $_SERVER['x-docuseal-signature'] ?? '';
}
if (empty($signature) || $signature !== $webhook_secret) {
logMasterDebug("FEHLER: Secret passt nicht.");
http_response_code(403);
die('Zugriff verweigert.');
}
$json = file_get_contents('php://input');
$data = json_decode($json, true);
$event = $data['event_type'] ?? 'unbekannt';
$template_id = $data['data']['template']['id'] ?? null;
logMasterDebug("Event: $event | Template-ID: $template_id");
if ($event !== 'submission.completed' && $event !== 'form.submitted') {
logMasterDebug("Event wird ignoriert.");
exit;
}
$pdf_url = $data['data']['documents'][0]['url'] ?? '';
$submission_id = $data['data']['id'] ?? null;
$fields = $data['data']['submitters'][0]['values'] ?? [];
$values = [];
foreach ($fields as $item) {
$values[strtolower($item['field'])] = $item['value'];
}
$time_raw = $data['data']['completed_at'] ?? 'now';
$datum = date('Y-m-d', strtotime($time_raw));
$uhrzeit = date('H-i', strtotime($time_raw));
if (!isset($nextcloud_urls[$template_id])) {
logMasterDebug("Unbekannte oder nicht konfigurierte Template-ID: $template_id. Abbruch.");
exit;
}
$nextcloud_url = $nextcloud_urls[$template_id];
switch ($template_id) {
case 2:
$shop_name = $values['shop_name'] ?? 'unbekannter_stand';
$safe_name = str_replace([' ', 'ä', 'ö', 'ü', 'ß', ';', '/'], ['_', 'ae', 'oe', 'ue', 'ss', '-', '-'], strtolower($shop_name));
$filename = 'haendler_' . $safe_name . '_' . $datum . '_' . $uhrzeit . '.pdf';
break;
case 4:
$vorname = $values['vorname'] ?? '';
$nachname = $values['nachname'] ?? 'helfer';
$name = trim($vorname . '_' . $nachname);
$safe_name = str_replace([' ', 'ä', 'ö', 'ü', 'ß', ';', '/'], ['_', 'ae', 'oe', 'ue', 'ss', '-', '-'], strtolower($name));
$filename = 'helfer_' . $safe_name . '_' . $datum . '_' . $uhrzeit . '.pdf';
break;
case 5:
$act_name = $values['act_name'] ?? 'unbekannter_act';
$safe_name = str_replace([' ', 'ä', 'ö', 'ü', 'ß', ';', '/'], ['_', 'ae', 'oe', 'ue', 'ss', '-', '-'], strtolower($act_name));
$filename = 'showact_' . $safe_name . '_' . $datum . '_' . $uhrzeit . '.pdf';
break;
default:
$name = $values['contact_name'] ?? $values['shop_name'] ?? 'eintrag';
$safe_name = str_replace([' ', 'ä', 'ö', 'ü', 'ß', ';', '/'], ['_', 'ae', 'oe', 'ue', 'ss', '-', '-'], strtolower($name));
$filename = 'formular_' . $template_id . '_' . $safe_name . '_' . $datum . '_' . $uhrzeit . '.pdf';
break;
}
if (!empty($pdf_url)) {
$ch_down = curl_init($pdf_url);
curl_setopt($ch_down, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_down, CURLOPT_FOLLOWLOCATION, true);
$pdf_content = curl_exec($ch_down);
$down_status = curl_getinfo($ch_down, CURLINFO_HTTP_CODE);
curl_close($ch_down);
if ($pdf_content && $down_status == 200) {
$full_target_url = rtrim($nextcloud_url, '/') . '/' . rawurlencode($filename);
$fh = tmpfile();
fwrite($fh, $pdf_content);
fseek($fh, 0);
$ch_up = curl_init($full_target_url);
curl_setopt($ch_up, CURLOPT_USERPWD, $nextcloud_user . ':' . $nextcloud_pass);
curl_setopt($ch_up, CURLOPT_UPLOAD, true);
curl_setopt($ch_up, CURLOPT_INFILE, $fh);
curl_setopt($ch_up, CURLOPT_INFILESIZE, strlen($pdf_content));
curl_setopt($ch_up, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_up, CURLOPT_HTTPHEADER, [
'Expect:',
'Content-Type: application/pdf'
]);
$result = curl_exec($ch_up);
$http_code = curl_getinfo($ch_up, CURLINFO_HTTP_CODE);
curl_close($ch_up);
fclose($fh);
if ($http_code >= 200 && $http_code < 300) {
logMasterDebug("Erfolgreich hochgeladen: $filename in Ordner für Template $template_id");
if ($submission_id) {
$ch_del = curl_init("https://docusign.fuyuzakura.de/api/v1/submissions/" . $submission_id);
curl_setopt($ch_del, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch_del, CURLOPT_HTTPHEADER, [
"X-Auth-Token: " . $api_key,
"Content-Type: application/json"
]);
curl_setopt($ch_del, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch_del);
curl_close($ch_del);
}
echo "Erfolg!";
} else {
logMasterDebug("Nextcloud Upload Fehler. HTTP: $http_code");
http_response_code(500);
}
} else {
logMasterDebug("Fehler beim Herunterladen der PDF von DocuSeal. HTTP-Code: $down_status");
}
} else {
logMasterDebug("Keine PDF-URL im Payload gefunden.");
}
?>