From 14de75762da2bf56d1e12cef0e378a0f2c0b564c Mon Sep 17 00:00:00 2001 From: Ortwin Pinke Date: Fri, 23 Aug 2024 14:21:02 +0200 Subject: [PATCH] fix captcha, add type email, add new attributes, cleanup some code --- php/cl_contactform_output.php | 174 +++++++++++++++++++--------------- 1 file changed, 98 insertions(+), 76 deletions(-) diff --git a/php/cl_contactform_output.php b/php/cl_contactform_output.php index 89491e4..5560765 100644 --- a/php/cl_contactform_output.php +++ b/php/cl_contactform_output.php @@ -1,4 +1,8 @@ '', 'from' => ['name' => '', 'email' => ''], 'message' => '', 'subject' => '']; private array $unraveled = []; private array $form = ['form' => '', 'answer' => '', 'colorError' => '']; - public function __construct(private bool $captchaInstalled) + public function __construct(private bool $captchaInstalled, private cSession $cSession) { } @@ -35,18 +40,22 @@ class w3form $this->unravel($this->suppress('sent')); $this->generateEmailMessage(); - $oMailer = new PHPMailer(); - $oMailer->CharSet = "UTF-8"; - $oMailer->AddAddress($this->email['adresses']); - $oMailer->From = $this->email['from']['email']; - $oMailer->FromName = $this->email['from']['name']; - $oMailer->Subject = $this->email['subject']; - $oMailer->Body = $this->email['message']; + $phpMailer = new PHPMailer(true); + $phpMailer->isMail(); + $phpMailer->isHTML(false); + $phpMailer->CharSet = "UTF-8"; + $phpMailer->AddAddress($this->email['adresses']); + $phpMailer->setFrom($this->email['from']['email'], $this->email['from']['name']); + $phpMailer->Subject = $this->email['subject']; + $phpMailer->Body = $this->email['message']; - if ($oMailer->send()) { - return true; + try { + if ($phpMailer->send()) { + return true; + } + } catch (Exception $e) { + echo $e->errorMessage(); } - return false; } @@ -63,7 +72,7 @@ class w3form private function add2Message($key, $value): void { - if (strlen($key) > 25 or strlen($value) > 54) { + if (strlen($key) > 25 || strlen($value) > 54) { $this->email['message'] .= "$key\n$value\n"; } else { $this->email['message'] .= $key; @@ -74,18 +83,16 @@ class w3form private function generateEmailMessage(): void { - if ($this->unraveled) - foreach ($this->unraveled as $key => $value) { - $this->add2Message($key, $value); - } + foreach ($this->unraveled as $key => $value) { + $this->add2Message($key, $value); + } } - private function suppress(): array + private function suppress(...$suppress): array { $fields = []; - $suppress = func_get_args(); foreach ($_POST as $key => $value) { - if (array_search($key, $suppress) === false) + if (!in_array($key, $suppress)) $fields[$key] = $value; } return $fields; @@ -130,8 +137,9 @@ class w3form { $fields = explode('###', $form); $field = []; + $fieldsCount = count($fields); - for ($i = 1; $i < count($fields); $i = $i + 2) { + for ($i = 1; $i < $fieldsCount; $i += 2) { $attribute = explode(';', trim($fields[$i])); foreach ($attribute as $attribute) { $nameValue = explode(':', trim($attribute)); @@ -148,7 +156,8 @@ class w3form public function formOutput($sent = false): void { - echo '
'; + $form_action = $this->cSession->url('front_content.php?idcat=' . cRegistry::getCategoryId() . '&idart=' . cRegistry::getArticleId()); + echo ''; echo ''; $form = $this->form['form']; $formData = $this->formInterpretation($form); @@ -168,37 +177,57 @@ class w3form { $style = ''; $value = ''; - $parameter = 'name="' . $attribute['name'] . '"'; - if ($sent && !$this->formFieldCorrect($attribute)) - $style = 'style="background-color: ' . $this->form['colorError'] . ';"'; - switch ($attribute['type']) { - case 'select': - case 'password': - case 'text': - if (!empty($attribute['size'])) - $parameter .= ' size="' . $attribute['size'] . '"'; - break; + if (!empty($attribute['id'])) { + $parameter = 'id="' . $attribute['id'] . '"'; + } else { + $parameter = 'id="' . $attribute['name'] . '"'; + } + + $parameter .= ' name="' . $attribute['name'] . '"'; + + if ($sent && !$this->formFieldCorrect($attribute)) { + $style = 'style="background-color: ' . $this->form['colorError'] . ';"'; + } + + if (!empty($attribute['class'])) { + $parameter .= ' class="' . $attribute['class'] . '"'; + } + + if (!empty($attribute['placeholder'])) { + $parameter .= ' placeholder="' . $attribute['placeholder'] . '"'; } switch ($attribute['type']) { case 'textarea': case 'text': - if (!empty($attribute['size'])) + if (!empty($attribute['size'])) { $parameter .= ' size="' . $attribute['size'] . '"'; - if (!empty($attribute['value'])) + } + if (!empty($attribute['value'])) { $value = $attribute['value']; - if (!empty($_POST["{$attribute['name']}"])) + } + if (!empty($_POST["{$attribute['name']}"])) { $value = $_POST["{$attribute['name']}"]; + } break; + case 'select': + case 'password': + if (!empty($attribute['size'])) { + $parameter .= ' size="' . $attribute['size'] . '"'; + } + break; + } + + if (isset($attribute['required']) || isset($attribute['mandantory'])) { + $parameter .= ' required'; } switch ($attribute['type']) { case 'captcha': if ($this->captchaInstalled) { if ($sent) { - $captcha = @$_POST['ct_captcha']; - $capId = @$_POST['captcha_id']; + $captcha = @$_POST['lets_check']; $securimage = new Securimage(); if (!$securimage->check($captcha)) { @@ -209,9 +238,10 @@ class w3form } // show captcha HTML using Securimage::getCaptchaHtml() $options = []; - $options['input_name'] = 'ct_captcha'; // change name of input element for form post input_text + $options['show_audio_button'] = false; + $options['input_name'] = 'lets_check'; // change name of input element for form post input_text $options['input_text'] = mi18n("Zeichen eingeben"); - $options['input_required'] = false; + $options['input_required'] = true; if (!empty($_SESSION['ctform']['captcha_error'])) { // error html to show in captcha output @@ -221,13 +251,13 @@ class w3form echo "
\n"; echo Securimage::getCaptchaHtml($options); echo "\n
\n"; + echo ''; } break; case 'text': - echo ""; - break; + case 'email': case 'password': - echo ""; + echo ''; break; case 'textarea': echo "