diff --git a/extensions/plugi-ext-form/admin/Classes/Finishers/FinishersRepository.php b/extensions/plugi-ext-form/admin/Classes/Finishers/FinishersRepository.php index 0e7644b..5b5e5e0 100644 --- a/extensions/plugi-ext-form/admin/Classes/Finishers/FinishersRepository.php +++ b/extensions/plugi-ext-form/admin/Classes/Finishers/FinishersRepository.php @@ -15,6 +15,19 @@ class FinishersRepository { return $this->config['finishers'][$key] ?? null; } + public function getEditorConfig(): array + { + $editorConfig = []; + foreach ($this->config['finishers'] as $key => $config) { + $editorConfig[$key] = [ + 'label' => $config['label'] ?? $key, + 'settings' => $config['settings'] ?? [], + ]; + } + + return $editorConfig; + } + public function finisherExists (string $key) : bool { return array_key_exists($key, $this->config['finishers']); diff --git a/extensions/plugi-ext-form/admin/Classes/Finishers/SendEmailToAdmin.php b/extensions/plugi-ext-form/admin/Classes/Finishers/SendEmailToAdmin.php index d251607..b750548 100644 --- a/extensions/plugi-ext-form/admin/Classes/Finishers/SendEmailToAdmin.php +++ b/extensions/plugi-ext-form/admin/Classes/Finishers/SendEmailToAdmin.php @@ -8,12 +8,28 @@ class SendEmailToAdmin extends DefaultFinisher public function execute(): bool { $settings = Extensions::getSettings('form'); - $to = $settings['email']['value'] ?? null; + $to = $this->options['recipient'] ?? ''; + if (!is_string($to)) { + return false; + } + if (trim($to) === '') { + $to = $settings['email']['value'] ?? null; + } if (!is_string($to) || filter_var($to, FILTER_VALIDATE_EMAIL) === false) { return false; } - $subject = 'Form submission: '.$this->getFormName(); + $subject = $this->options['subject'] ?? ''; + if (!is_string($subject)) { + return false; + } + if (trim($subject) === '') { + $subject = 'Form submission: '.$this->getFormName(); + } + if (strpbrk($subject, "\r\n") !== false) { + return false; + } + $message = $this->getNiceFields(); $mailer = phpb_instance('mail'); return $mailer !== null && $mailer->send($to, $subject, $message) === true; diff --git a/extensions/plugi-ext-form/admin/Classes/Finishers/SendWebhook.php b/extensions/plugi-ext-form/admin/Classes/Finishers/SendWebhook.php new file mode 100644 index 0000000..96c703c --- /dev/null +++ b/extensions/plugi-ext-form/admin/Classes/Finishers/SendWebhook.php @@ -0,0 +1,108 @@ +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; + } +} \ No newline at end of file diff --git a/extensions/plugi-ext-form/admin/Classes/Finishers/config.php b/extensions/plugi-ext-form/admin/Classes/Finishers/config.php index 47ded79..551d764 100644 --- a/extensions/plugi-ext-form/admin/Classes/Finishers/config.php +++ b/extensions/plugi-ext-form/admin/Classes/Finishers/config.php @@ -4,15 +4,95 @@ return [ 'finishers' => [ 'saveToDatabase' => [ 'class' => \Plugi\Extensions\Form\Finishers\SaveEntryToDatabase::class, - 'label' => 'Save to database' + 'label' => 'Save to database', + 'settings' => [], ], 'sendMailToAdmin' => [ 'class' => \Plugi\Extensions\Form\Finishers\SendEmailToAdmin::class, - 'label' => 'Send email to admin' + 'label' => 'Send email to admin', + 'settings' => [ + [ + 'name' => 'recipient', + 'label' => 'Recipient email', + 'type' => 'email', + 'default' => '', + 'placeholder' => 'Uses the form extension default when blank', + ], + [ + 'name' => 'subject', + 'label' => 'Subject', + 'type' => 'text', + 'default' => '', + 'placeholder' => 'Uses the form name when blank', + ], + ], ], 'sendConfirmationEmailToSubmitter' => [ 'class' => \Plugi\Extensions\Form\Finishers\SendConfirmationEmailToSubmitter::class, - 'label' => 'Send confirmation email to submitter' - ] + 'label' => 'Send confirmation email to submitter', + 'settings' => [ + [ + 'name' => 'recipientField', + 'label' => 'Recipient email field', + 'type' => 'emailField', + 'default' => '', + 'required' => true, + ], + [ + 'name' => 'subject', + 'label' => 'Subject', + 'type' => 'text', + 'default' => 'Thank you for your submission', + 'required' => true, + ], + [ + 'name' => 'message', + 'label' => 'Message', + 'type' => 'textarea', + 'default' => 'Thank you for contacting us. We have received your message.', + 'required' => true, + ], + ], + ], + 'sendWebhook' => [ + 'class' => \Plugi\Extensions\Form\Finishers\SendWebhook::class, + 'label' => 'Send webhook', + 'settings' => [ + [ + 'name' => 'url', + 'label' => 'Webhook URL', + 'type' => 'url', + 'default' => '', + 'required' => true, + 'placeholder' => 'https://example.com/webhook', + 'help' => 'Receives a JSON object with the form ID, form name, and submitted fields.', + ], + [ + 'name' => 'method', + 'label' => 'HTTP method', + 'type' => 'select', + 'default' => 'POST', + 'options' => [ + ['value' => 'POST', 'label' => 'POST'], + ['value' => 'PUT', 'label' => 'PUT'], + ], + ], + [ + 'name' => 'bearerToken', + 'label' => 'Bearer token (optional)', + 'type' => 'password', + 'default' => '', + ], + [ + 'name' => 'timeout', + 'label' => 'Timeout (seconds)', + 'type' => 'number', + 'default' => 10, + 'min' => 1, + 'max' => 30, + 'required' => true, + ], + ], + ], ] ]; diff --git a/extensions/plugi-ext-form/admin/Views/EditForm.php b/extensions/plugi-ext-form/admin/Views/EditForm.php index 2876f90..38964ab 100644 --- a/extensions/plugi-ext-form/admin/Views/EditForm.php +++ b/extensions/plugi-ext-form/admin/Views/EditForm.php @@ -1,8 +1,11 @@ getEditorConfig(); $form = [ 'name' => "New Form", @@ -31,13 +34,13 @@ $isEditing = isset($form['id']) && $form['id'] > 0;