Files
plugi/extensions/plugi-ext-form/admin/Classes/Finishers/SendWebhook.php
T
deepDiverPaul d5597b045f add configurable form finishers and webhook delivery
Expose finisher settings through the editor registry and build the action editor dynamically. Add configurable admin email recipients and subjects, and introduce a webhook finisher with validated request options and JSON submission payloads.

Expand submission handler tests to cover editor configuration, email overrides, webhook validation, HTTP responses, and submission outcomes.
2026-11-24 18:51:20 +01:00

108 lines
3.8 KiB
PHP

<?php
namespace Plugi\Extensions\Form\Finishers;
class SendWebhook extends DefaultFinisher
{
public function execute(): bool
{
$url = $this->options['url'] ?? null;
$method = $this->options['method'] ?? 'POST';
$bearerToken = $this->options['bearerToken'] ?? '';
$timeout = filter_var($this->options['timeout'] ?? null, FILTER_VALIDATE_INT, [
'options' => ['min_range' => 1, 'max_range' => 30],
]);
if (!is_string($url) || !$this->isValidUrl($url)
|| !is_string($method) || !in_array($method, ['POST', 'PUT'], true)
|| !is_string($bearerToken) || strlen($bearerToken) > 4096
|| preg_match('/\A[\x21-\x7E]*\z/', $bearerToken) !== 1
|| $timeout === false
) {
return false;
}
$payload = json_encode([
'formId' => $this->formId,
'formName' => $this->getFormName(),
'fields' => $this->getEntryFields(),
], JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE);
if (!is_string($payload)) {
return false;
}
$responseCode = $this->sendRequest($url, $method, $bearerToken !== '' ? $bearerToken : null, $payload, $timeout);
return $responseCode !== null && $responseCode >= 200 && $responseCode < 300;
}
private function isValidUrl(string $url): bool
{
if (strlen($url) > 2048 || filter_var($url, FILTER_VALIDATE_URL) === false
|| preg_match('/[\r\n\x00-\x20\x7F]/', $url) === 1
) {
return false;
}
$parts = parse_url($url);
if (!is_array($parts) || !isset($parts['scheme'], $parts['host'])
|| !in_array(strtolower($parts['scheme']), ['http', 'https'], true)
|| isset($parts['user']) || isset($parts['pass']) || isset($parts['fragment'])
) {
return false;
}
if (isset($parts['port']) && ($parts['port'] < 1 || $parts['port'] > 65535)) {
return false;
}
$host = $parts['host'];
if (filter_var($host, FILTER_VALIDATE_IP) !== false) {
return true;
}
return preg_match('/\A(?=.{1,253}\z)(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?)(?:\.(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?))*\z/', $host) === 1;
}
protected function sendRequest(string $url, string $method, ?string $bearerToken, string $payload, int $timeout): ?int
{
$headers = "Content-Type: application/json\r\nAccept: application/json";
if ($bearerToken !== null) {
$headers .= "\r\nAuthorization: Bearer " . $bearerToken;
}
$host = parse_url($url, PHP_URL_HOST);
$context = stream_context_create([
'http' => [
'method' => $method,
'header' => $headers,
'content' => $payload,
'timeout' => $timeout,
'ignore_errors' => true,
'follow_location' => 0,
'max_redirects' => 0,
'protocol_version' => 1.1,
],
'ssl' => [
'verify_peer' => true,
'verify_peer_name' => true,
'peer_name' => $host,
'SNI_enabled' => true,
],
]);
$stream = @fopen($url, 'r', false, $context);
if (!is_resource($stream)) {
return null;
}
$metadata = stream_get_meta_data($stream);
fclose($stream);
foreach ($metadata['wrapper_data'] ?? [] as $statusLine) {
if (is_string($statusLine) && preg_match('/\AHTTP\/\d(?:\.\d)?\s+(\d{3})(?:\s|\z)/', $statusLine, $matches) === 1) {
$responseCode = (int) $matches[1];
}
}
return $responseCode ?? null;
}
}