diff --git a/module.php b/module.php index 6367fd2..74e8da2 100755 --- a/module.php +++ b/module.php @@ -9,8 +9,14 @@ return new class extends Lewp\Module const KEY_CSRF_TOKEN = 'csrf_token'; const KEY_CAPTCHA_PHRASE = 'captcha_phrase'; + const KEY_POST_EMAIL = 'email'; + const KEY_POST_NAME = 'name'; + const KEY_POST_MESSAGE = 'message'; + const OPTIONS_REDIRECT_URI = 'location_on_success'; + const OPTIONS_MAIL_FROM = 'mail_from'; + const OPTIONS_MAIL_TO = 'mail_to'; - private function createForm() + private function createForm(bool $group_input_and_labels = true) { $form = $this->createAndSetupElement( 'form', @@ -20,6 +26,7 @@ return new class extends Lewp\Module 'action' => '', ] ); + // LABEL NAME $label_name = $this->createAndSetupElement( 'label', @@ -31,7 +38,6 @@ return new class extends Lewp\Module 'for' => 'name', ] ); - $form->appendChild($label_name); // NAME INPUT $name = $this->createAndSetupElement( 'input', @@ -40,10 +46,11 @@ return new class extends Lewp\Module 'id' => 'name', 'name' => 'name', 'type' => 'text', - 'value' => $_POST['name'] ?? '', + 'value' => $_POST[self::KEY_POST_NAME] ?? '', + 'required' => 'required', + 'placeholder' => ' ', ] ); - $form->appendChild($name); // LABEL Email $label_email = $this->createAndSetupElement( 'label', @@ -55,7 +62,6 @@ return new class extends Lewp\Module 'for' => 'email' ] ); - $form->appendChild($label_email); // EMAIL INPUT $email = $this->createAndSetupElement( 'input', @@ -64,10 +70,11 @@ return new class extends Lewp\Module 'id' => 'email', 'name' => 'email', 'type' => 'email', - 'value' => $_POST['email'] ?? '', + 'value' => $_POST[self::KEY_POST_EMAIL] ?? '', + 'required' => 'required', + 'placeholder' => ' ', ] ); - $form->appendChild($email); // LABEL Message $label_message = $this->createAndSetupElement( 'label', @@ -79,27 +86,28 @@ return new class extends Lewp\Module 'for' => 'message' ] ); - $form->appendChild($label_message); // MESSAGE INPUT $message = $this->createAndSetupElement( 'textarea', - $_POST['message'] ?? '', + $_POST[self::KEY_POST_MESSAGE] ?? '', [ 'id' => 'message', 'name' => 'message', + 'required' => 'required', + 'placeholder' => ' ', ] ); - $form->appendChild($message); // LABEL Captcha $label_captcha = $this->createAndSetupElement( 'label', - '', + $this->loadTextFile(Resolve::arrayToId([ + $this->getLanguage(), + 'captcha' + ])), [ 'for' => self::KEY_CAPTCHA_PHRASE ] ); - $label_captcha->appendChild($this->generateCaptcha()); - $form->appendChild($label_captcha); // CAPTCHA INPUT $captcha = $this->createAndSetupElement( 'input', @@ -108,9 +116,10 @@ return new class extends Lewp\Module 'id' => 'captchainput', 'name' => self::KEY_CAPTCHA_PHRASE, 'type' => 'text', + 'required' => 'required', + 'placeholder' => ' ', ] ); - $form->appendChild($captcha); // SUBMIT BUTTON $submit = $this->createAndSetupElement( 'button', @@ -123,6 +132,48 @@ return new class extends Lewp\Module 'type' => 'submit', ] ); + + // APPEND THEM TO DOCUMENT + if ($group_input_and_labels) { + $div = $this->createAndSetupElement( + 'div', '', ['class' => 'fieldwrapper'] + ); + $div->appendChild($name); + $div->appendChild($label_name); + $form->appendChild($div); + $div = $this->createAndSetupElement( + 'div', '', ['class' => 'fieldwrapper'] + ); + $div->appendChild($email); + $div->appendChild($label_email); + $form->appendChild($div); + $div = $this->createAndSetupElement( + 'div', '', ['class' => 'fieldwrapper'] + ); + $div->appendChild($message); + $div->appendChild($label_message); + $form->appendChild($div); + } else { + $form->appendChild($label_name); + $form->appendChild($name); + $form->appendChild($label_email); + $form->appendChild($email); + $form->appendChild($label_message); + $form->appendChild($message); + } + $form->appendChild($this->generateCaptcha()); + if ($group_input_and_labels) { + $div = $this->createAndSetupElement( + 'div', '', ['class' => 'fieldwrapper'] + ); + $div->appendChild($captcha); + $div->appendChild($label_captcha); + $form->appendChild($div); + } else { + $form->appendChild($label_captcha); + $form->appendChild($captcha); + } + $form->appendChild($submit); // ADD CSRF CHECK $form->appendChild($this->generateCsrfInput()); @@ -202,8 +253,26 @@ return new class extends Lewp\Module ) ? true : false; } + private function sendMail($payload, $options) + { + mail( + $options[self::OPTIONS_MAIL_TO], + '['.$this->getTLD().'] '.$payload[self::KEY_POST_NAME], + $payload[self::KEY_POST_MESSAGE], + 'From: '.$options[self::OPTIONS_MAIL_FROM] + ."\r\n" + .'Reply-To: '.$payload[self::KEY_POST_EMAIL] + ); + } + public function run(array $options = []) : bool { + + // set default options + $options += [ + self::OPTIONS_REDIRECT_URI => "/" + ]; + if (!$this->validateCsrfInput()) { $this->getSession()->getFlashBag()->add( 'error', @@ -226,8 +295,39 @@ return new class extends Lewp\Module ])) ); } - $form = $this->createForm(); - $this->appendChild($form); + + // send form again if captcha or csrf not valid, or form not submitted yet + if ( + !$this->validateCaptcha() + || !$this->validateCsrfInput() + || !$this->formSubmitted() + ) { + $form = $this->createForm(); + $this->appendChild($form); + return true; + } + + // send email + if ( + !is_null($options[self::OPTIONS_MAIL_TO]) + || !is_null($options[self::OPTIONS_MAIL_FROM]) + ) { + $this->sendMail($_POST, $options); + } + // notify user about success + $this->getSession()->getFlashBag()->add( + 'success', + $this->loadTextFile(Resolve::arrayToId([ + $this->getLanguage(), + 'flashmessages', + 'success', + 'thanks-for-message' + ])) + ); + + header('Location: '.$options[self::OPTIONS_REDIRECT_URI]); + exit; // if not present, session gets not saved + return true; } diff --git a/resources/text/de/captcha.txt b/resources/text/de/captcha.txt new file mode 100644 index 0000000..11f5a24 --- /dev/null +++ b/resources/text/de/captcha.txt @@ -0,0 +1 @@ +Captcha diff --git a/resources/text/de/email.txt b/resources/text/de/email.txt index a86ad66..7e5872d 100644 --- a/resources/text/de/email.txt +++ b/resources/text/de/email.txt @@ -1 +1 @@ -Email: +Email diff --git a/resources/text/de/flashmessages/success/thanks-for-message.txt b/resources/text/de/flashmessages/success/thanks-for-message.txt new file mode 100644 index 0000000..3bf1e27 --- /dev/null +++ b/resources/text/de/flashmessages/success/thanks-for-message.txt @@ -0,0 +1 @@ +Vielen Dank für Ihre Nachricht! Wir werden uns schnellstmöglich bei Ihnen melden! diff --git a/resources/text/de/message.txt b/resources/text/de/message.txt index 6a950a1..34e9921 100644 --- a/resources/text/de/message.txt +++ b/resources/text/de/message.txt @@ -1 +1 @@ -Deine Nachricht: +Deine Nachricht diff --git a/resources/text/de/name.txt b/resources/text/de/name.txt index 23346a8..70df60c 100644 --- a/resources/text/de/name.txt +++ b/resources/text/de/name.txt @@ -1 +1 @@ -Name: +Name diff --git a/resources/text/en/captcha.txt b/resources/text/en/captcha.txt new file mode 100644 index 0000000..11f5a24 --- /dev/null +++ b/resources/text/en/captcha.txt @@ -0,0 +1 @@ +Captcha diff --git a/resources/text/en/email.txt b/resources/text/en/email.txt index a86ad66..7e5872d 100644 --- a/resources/text/en/email.txt +++ b/resources/text/en/email.txt @@ -1 +1 @@ -Email: +Email diff --git a/resources/text/en/flashmessages/success/thanks-for-message.txt b/resources/text/en/flashmessages/success/thanks-for-message.txt new file mode 100644 index 0000000..0021db8 --- /dev/null +++ b/resources/text/en/flashmessages/success/thanks-for-message.txt @@ -0,0 +1 @@ +Thanks for your message! We will respond as soon as possible! diff --git a/resources/text/en/message.txt b/resources/text/en/message.txt index dd9fa72..2c7adc9 100644 --- a/resources/text/en/message.txt +++ b/resources/text/en/message.txt @@ -1 +1 @@ -Your message: +Your message diff --git a/resources/text/en/name.txt b/resources/text/en/name.txt index 23346a8..70df60c 100644 --- a/resources/text/en/name.txt +++ b/resources/text/en/name.txt @@ -1 +1 @@ -Name: +Name