fix captcha, add type email, add new attributes, cleanup some code

Dieser Commit ist enthalten in:
Ortwin Pinke 2024-08-23 14:21:02 +02:00
Ursprung 0be23bcd33
Commit 14de75762d

Datei anzeigen

@ -1,4 +1,8 @@
<?php <?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
global $sess;
/** /**
* Module cl-contactform output * Module cl-contactform output
* *
@ -21,11 +25,12 @@ if (is_readable($sFrontEndPath)) {
class w3form class w3form
{ {
public $emailAdresses;
private array $email = ['adresses' => '', 'from' => ['name' => '', 'email' => ''], 'message' => '', 'subject' => '']; private array $email = ['adresses' => '', 'from' => ['name' => '', 'email' => ''], 'message' => '', 'subject' => ''];
private array $unraveled = []; private array $unraveled = [];
private array $form = ['form' => '', 'answer' => '', 'colorError' => '']; 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->unravel($this->suppress('sent'));
$this->generateEmailMessage(); $this->generateEmailMessage();
$oMailer = new PHPMailer(); $phpMailer = new PHPMailer(true);
$oMailer->CharSet = "UTF-8"; $phpMailer->isMail();
$oMailer->AddAddress($this->email['adresses']); $phpMailer->isHTML(false);
$oMailer->From = $this->email['from']['email']; $phpMailer->CharSet = "UTF-8";
$oMailer->FromName = $this->email['from']['name']; $phpMailer->AddAddress($this->email['adresses']);
$oMailer->Subject = $this->email['subject']; $phpMailer->setFrom($this->email['from']['email'], $this->email['from']['name']);
$oMailer->Body = $this->email['message']; $phpMailer->Subject = $this->email['subject'];
$phpMailer->Body = $this->email['message'];
if ($oMailer->send()) { try {
return true; if ($phpMailer->send()) {
return true;
}
} catch (Exception $e) {
echo $e->errorMessage();
} }
return false; return false;
} }
@ -63,7 +72,7 @@ class w3form
private function add2Message($key, $value): void 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"; $this->email['message'] .= "$key\n$value\n";
} else { } else {
$this->email['message'] .= $key; $this->email['message'] .= $key;
@ -74,18 +83,16 @@ class w3form
private function generateEmailMessage(): void private function generateEmailMessage(): void
{ {
if ($this->unraveled) foreach ($this->unraveled as $key => $value) {
foreach ($this->unraveled as $key => $value) { $this->add2Message($key, $value);
$this->add2Message($key, $value); }
}
} }
private function suppress(): array private function suppress(...$suppress): array
{ {
$fields = []; $fields = [];
$suppress = func_get_args();
foreach ($_POST as $key => $value) { foreach ($_POST as $key => $value) {
if (array_search($key, $suppress) === false) if (!in_array($key, $suppress))
$fields[$key] = $value; $fields[$key] = $value;
} }
return $fields; return $fields;
@ -130,8 +137,9 @@ class w3form
{ {
$fields = explode('###', $form); $fields = explode('###', $form);
$field = []; $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])); $attribute = explode(';', trim($fields[$i]));
foreach ($attribute as $attribute) { foreach ($attribute as $attribute) {
$nameValue = explode(':', trim($attribute)); $nameValue = explode(':', trim($attribute));
@ -148,7 +156,8 @@ class w3form
public function formOutput($sent = false): void public function formOutput($sent = false): void
{ {
echo '<form action="" method="post" class="form-horizontal">'; $form_action = $this->cSession->url('front_content.php?idcat=' . cRegistry::getCategoryId() . '&idart=' . cRegistry::getArticleId());
echo '<form action="' . $form_action . '" method="post" class="form-horizontal">';
echo '<input type="hidden" name="sent" value="true" />'; echo '<input type="hidden" name="sent" value="true" />';
$form = $this->form['form']; $form = $this->form['form'];
$formData = $this->formInterpretation($form); $formData = $this->formInterpretation($form);
@ -168,37 +177,57 @@ class w3form
{ {
$style = ''; $style = '';
$value = ''; $value = '';
$parameter = 'name="' . $attribute['name'] . '"';
if ($sent && !$this->formFieldCorrect($attribute))
$style = 'style="background-color: ' . $this->form['colorError'] . ';"';
switch ($attribute['type']) { if (!empty($attribute['id'])) {
case 'select': $parameter = 'id="' . $attribute['id'] . '"';
case 'password': } else {
case 'text': $parameter = 'id="' . $attribute['name'] . '"';
if (!empty($attribute['size'])) }
$parameter .= ' size="' . $attribute['size'] . '"';
break; $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']) { switch ($attribute['type']) {
case 'textarea': case 'textarea':
case 'text': case 'text':
if (!empty($attribute['size'])) if (!empty($attribute['size'])) {
$parameter .= ' size="' . $attribute['size'] . '"'; $parameter .= ' size="' . $attribute['size'] . '"';
if (!empty($attribute['value'])) }
if (!empty($attribute['value'])) {
$value = $attribute['value']; $value = $attribute['value'];
if (!empty($_POST["{$attribute['name']}"])) }
if (!empty($_POST["{$attribute['name']}"])) {
$value = $_POST["{$attribute['name']}"]; $value = $_POST["{$attribute['name']}"];
}
break; 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']) { switch ($attribute['type']) {
case 'captcha': case 'captcha':
if ($this->captchaInstalled) { if ($this->captchaInstalled) {
if ($sent) { if ($sent) {
$captcha = @$_POST['ct_captcha']; $captcha = @$_POST['lets_check'];
$capId = @$_POST['captcha_id'];
$securimage = new Securimage(); $securimage = new Securimage();
if (!$securimage->check($captcha)) { if (!$securimage->check($captcha)) {
@ -209,9 +238,10 @@ class w3form
} }
// show captcha HTML using Securimage::getCaptchaHtml() // show captcha HTML using Securimage::getCaptchaHtml()
$options = []; $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_text'] = mi18n("Zeichen eingeben");
$options['input_required'] = false; $options['input_required'] = true;
if (!empty($_SESSION['ctform']['captcha_error'])) { if (!empty($_SESSION['ctform']['captcha_error'])) {
// error html to show in captcha output // error html to show in captcha output
@ -221,13 +251,13 @@ class w3form
echo "<div id='captcha_container_1'>\n"; echo "<div id='captcha_container_1'>\n";
echo Securimage::getCaptchaHtml($options); echo Securimage::getCaptchaHtml($options);
echo "\n</div>\n"; echo "\n</div>\n";
echo '<script src="securimage/securimage.js"></script>';
} }
break; break;
case 'text': case 'text':
echo "<input type=\"text\" $parameter value=\"$value\" $style />"; case 'email':
break;
case 'password': case 'password':
echo "<input type=\"password\" $parameter value=\"$value\" $style />"; echo '<input type="'.$attribute['type'].'" '.$parameter.' value="'.$value.'" '.$style.' />';
break; break;
case 'textarea': case 'textarea':
echo "<textarea name=\"{$attribute['name']}\" cols=\""; echo "<textarea name=\"{$attribute['name']}\" cols=\"";
@ -238,27 +268,22 @@ class w3form
break; break;
case 'select': case 'select':
echo "<select $parameter $style>"; echo "<select $parameter $style>";
for ($i = 0; $i < (is_countable($attribute['option']) ? count($attribute['option']) : 0); $i++) { $itemsCount = is_countable($attribute['option']) ? count($attribute['option']) : 0;
for ($i = 0; $i < (is_countable($attribute['option']) ? $itemsCount : 0); $i++) {
if (!empty($attribute['optionvalue'][$i])) { if (!empty($attribute['optionvalue'][$i])) {
if (!empty($_POST["{$attribute['name']}"]) && $_POST["{$attribute['name']}"] == $attribute['optionvalue'][$i]) { if (!empty($_POST["{$attribute['name']}"]) && $_POST["{$attribute['name']}"] == $attribute['optionvalue'][$i]) {
echo "<option value=\"{$attribute['optionvalue'][$i]}\" selected=\"selected\">{$attribute['option'][$i]}</option>\n"; echo "<option value=\"{$attribute['optionvalue'][$i]}\" selected=\"selected\">{$attribute['option'][$i]}</option>\n";
} elseif (empty($_POST["{$attribute['name']}"]) && !empty($attribute['optionvalue'][$i]) && $attribute['optionvalue'][$i] == $attribute['value']) {
echo "<option value=\"{$attribute['optionvalue'][$i]}\" selected=\"selected\">{$attribute['option'][$i]}</option>\n";
} else { } else {
if (empty($_POST["{$attribute['name']}"]) && !empty($attribute['optionvalue'][$i]) && $attribute['optionvalue'][$i] == $attribute['value']) { echo "<option value=\"{$attribute['optionvalue'][$i]}\">{$attribute['option'][$i]}</option>\n";
echo "<option value=\"{$attribute['optionvalue'][$i]}\" selected=\"selected\">{$attribute['option'][$i]}</option>\n";
} else {
echo "<option value=\"{$attribute['optionvalue'][$i]}\">{$attribute['option'][$i]}</option>\n";
}
} }
} elseif (!empty($_POST["{$attribute['name']}"]) && $_POST["{$attribute['name']}"] == $attribute['option'][$i]) {
echo "<option selected=\"selected\">{$attribute['option'][$i]}</option>\n";
} elseif (empty($_POST["{$attribute['name']}"]) && $attribute['option'][$i] == $attribute['value']) {
echo "<option selected=\"selected\">{$attribute['option'][$i]}</option>\n";
} else { } else {
if (!empty($_POST["{$attribute['name']}"]) && $_POST["{$attribute['name']}"] == $attribute['option'][$i]) { echo "<option>{$attribute['option'][$i]}</option>\n";
echo "<option selected=\"selected\">{$attribute['option'][$i]}</option>\n";
} else {
if (empty($_POST["{$attribute['name']}"]) && $attribute['option'][$i] == $attribute['value']) {
echo "<option selected=\"selected\">{$attribute['option'][$i]}</option>\n";
} else {
echo "<option>{$attribute['option'][$i]}</option>\n";
}
}
} }
} }
echo "</select>"; echo "</select>";
@ -271,12 +296,10 @@ class w3form
} else { } else {
echo "<label $style><input type=\"checkbox\" $parameter value=\"{$attribute['value']}\"/></label>"; echo "<label $style><input type=\"checkbox\" $parameter value=\"{$attribute['value']}\"/></label>";
} }
} elseif (!empty($attribute['selected']) && $attribute['selected'] == 'true') {
echo "<input type=\"checkbox\" $parameter value=\"{$attribute['value']}\" checked=\"checked\"/>";
} else { } else {
if (!empty($attribute['selected']) && $attribute['selected'] == 'true') { echo "<input type=\"checkbox\" $parameter value=\"{$attribute['value']}\"/>";
echo "<input type=\"checkbox\" $parameter value=\"{$attribute['value']}\" checked=\"checked\"/>";
} else {
echo "<input type=\"checkbox\" $parameter value=\"{$attribute['value']}\"/>";
}
} }
break; break;
case 'radio': case 'radio':
@ -286,12 +309,10 @@ class w3form
} else { } else {
echo "<input type=\"radio\" $parameter value=\"{$attribute['value']}\" />"; echo "<input type=\"radio\" $parameter value=\"{$attribute['value']}\" />";
} }
} elseif (!empty($attribute['selected']) && $attribute['selected'] == 'true') {
echo "<input type=\"radio\" $parameter value=\"{$attribute['value']}\" checked=\"checked\"/>";
} else { } else {
if (!empty($attribute['selected']) && $attribute['selected'] == 'true') { echo "<input type=\"radio\" $parameter value=\"{$attribute['value']}\"/>";
echo "<input type=\"radio\" $parameter value=\"{$attribute['value']}\" checked=\"checked\"/>";
} else {
echo "<input type=\"radio\" $parameter value=\"{$attribute['value']}\"/>";
}
} }
break; break;
} }
@ -305,7 +326,7 @@ class w3form
switch ($field['type']) { switch ($field['type']) {
case 'captcha': case 'captcha':
if ($this->captchaInstalled) { if ($this->captchaInstalled) {
$captcha = @$_POST['ct_captcha']; $captcha = @$_POST['lets_check'];
$capId = @$_POST['captcha_id']; $capId = @$_POST['captcha_id'];
$securimage = new Securimage(); $securimage = new Securimage();
if (!$securimage->check($captcha, $capId, true)) { if (!$securimage->check($captcha, $capId, true)) {
@ -326,7 +347,13 @@ class w3form
private function success(): void private function success(): void
{ {
if ($this->sendEmail()) { if ($this->sendEmail()) {
echo $this->form['answer']; $answer = trim($this->form['answer']);
if (mb_strlen($answer) > 0) {
echo $this->form['answer'];
} else {
echo '<span style="color:green;">' . mi18n("Form has been successfully send.") . '</span>';
}
} else { } else {
echo '<span style="color:red;">' . mi18n("Es ist ein Fehler aufgetreten!<br>Bitte versuchen Sie es später noch einmal.") . '</span>'; echo '<span style="color:red;">' . mi18n("Es ist ein Fehler aufgetreten!<br>Bitte versuchen Sie es später noch einmal.") . '</span>';
} }
@ -338,10 +365,8 @@ class w3form
$tmp_name = rtrim($field['name'], '[0]'); $tmp_name = rtrim($field['name'], '[0]');
preg_match('/\[(\d*)\]/', $field['name'], $matches); preg_match('/\[(\d*)\]/', $field['name'], $matches);
$bEmptyPost = false; $bEmptyPost = false;
$bIsPostArray = false;
$sPostFieldValue = $_POST[$tmp_name]; $sPostFieldValue = $_POST[$tmp_name];
if (is_array($_POST[$tmp_name])) { if (is_array($_POST[$tmp_name])) {
$bIsPostArray == true;
$sPostFieldValue = $_POST[$tmp_name][$matches[1]]; $sPostFieldValue = $_POST[$tmp_name][$matches[1]];
if (empty($_POST[$tmp_name][$matches[1]])) { if (empty($_POST[$tmp_name][$matches[1]])) {
$bEmptyPost = true; $bEmptyPost = true;
@ -414,10 +439,7 @@ class w3form
// längenbereich bei allen typen prüfen // längenbereich bei allen typen prüfen
if (!empty($field['minlength']) && strlen($sPostFieldValue) < $field['minlength']) if (!empty($field['minlength']) && strlen($sPostFieldValue) < $field['minlength'])
return false; return false;
if (!empty($field['maxlength']) && strlen($sPostFieldValue) > $field['maxlength']) return !(!empty($field['maxlength']) && strlen($sPostFieldValue) > $field['maxlength']);
return false;
return true;
} }
public function process(): void public function process(): void
@ -440,7 +462,7 @@ if (cRegistry::isBackendEditMode()) {
echo "<p>" . mi18n("Hier ist die Ausgabe einzugeben, die erscheint, wenn das Formular erfolgreich prozessiert worden ist:") . "</p>"; echo "<p>" . mi18n("Hier ist die Ausgabe einzugeben, die erscheint, wenn das Formular erfolgreich prozessiert worden ist:") . "</p>";
echo "CMS_HTML[101]"; echo "CMS_HTML[101]";
} else { } else {
$form = new w3form($captchaInstalled); $form = new w3form($captchaInstalled, $sess);
$form->addEmailAdress("CMS_VALUE[0]"); $form->addEmailAdress("CMS_VALUE[0]");
$form->setEmailSubject("CMS_VALUE[1]"); $form->setEmailSubject("CMS_VALUE[1]");
$form->setEmailFrom("CMS_VALUE[2]", "CMS_VALUE[3]"); $form->setEmailFrom("CMS_VALUE[2]", "CMS_VALUE[3]");
@ -450,4 +472,4 @@ if (cRegistry::isBackendEditMode()) {
$form->process(); $form->process();
} }
?> ?>