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
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
global $sess;
/**
* Module cl-contactform output
*
@ -21,11 +25,12 @@ if (is_readable($sFrontEndPath)) {
class w3form
{
public $emailAdresses;
private array $email = ['adresses' => '', '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="" 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" />';
$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 "<div id='captcha_container_1'>\n";
echo Securimage::getCaptchaHtml($options);
echo "\n</div>\n";
echo '<script src="securimage/securimage.js"></script>';
}
break;
case 'text':
echo "<input type=\"text\" $parameter value=\"$value\" $style />";
break;
case 'email':
case 'password':
echo "<input type=\"password\" $parameter value=\"$value\" $style />";
echo '<input type="'.$attribute['type'].'" '.$parameter.' value="'.$value.'" '.$style.' />';
break;
case 'textarea':
echo "<textarea name=\"{$attribute['name']}\" cols=\"";
@ -238,27 +268,22 @@ class w3form
break;
case 'select':
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($_POST["{$attribute['name']}"]) && $_POST["{$attribute['name']}"] == $attribute['optionvalue'][$i]) {
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 {
if (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 {
echo "<option value=\"{$attribute['optionvalue'][$i]}\">{$attribute['option'][$i]}</option>\n";
}
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 {
if (!empty($_POST["{$attribute['name']}"]) && $_POST["{$attribute['name']}"] == $attribute['option'][$i]) {
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 "<option>{$attribute['option'][$i]}</option>\n";
}
}
echo "</select>";
@ -271,12 +296,10 @@ class w3form
} else {
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 {
if (!empty($attribute['selected']) && $attribute['selected'] == 'true') {
echo "<input type=\"checkbox\" $parameter value=\"{$attribute['value']}\" checked=\"checked\"/>";
} else {
echo "<input type=\"checkbox\" $parameter value=\"{$attribute['value']}\"/>";
}
echo "<input type=\"checkbox\" $parameter value=\"{$attribute['value']}\"/>";
}
break;
case 'radio':
@ -286,12 +309,10 @@ class w3form
} else {
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 {
if (!empty($attribute['selected']) && $attribute['selected'] == 'true') {
echo "<input type=\"radio\" $parameter value=\"{$attribute['value']}\" checked=\"checked\"/>";
} else {
echo "<input type=\"radio\" $parameter value=\"{$attribute['value']}\"/>";
}
echo "<input type=\"radio\" $parameter value=\"{$attribute['value']}\"/>";
}
break;
}
@ -305,7 +326,7 @@ class w3form
switch ($field['type']) {
case 'captcha':
if ($this->captchaInstalled) {
$captcha = @$_POST['ct_captcha'];
$captcha = @$_POST['lets_check'];
$capId = @$_POST['captcha_id'];
$securimage = new Securimage();
if (!$securimage->check($captcha, $capId, true)) {
@ -326,7 +347,13 @@ class w3form
private function success(): void
{
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 {
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]');
preg_match('/\[(\d*)\]/', $field['name'], $matches);
$bEmptyPost = false;
$bIsPostArray = false;
$sPostFieldValue = $_POST[$tmp_name];
if (is_array($_POST[$tmp_name])) {
$bIsPostArray == true;
$sPostFieldValue = $_POST[$tmp_name][$matches[1]];
if (empty($_POST[$tmp_name][$matches[1]])) {
$bEmptyPost = true;
@ -414,10 +439,7 @@ class w3form
// längenbereich bei allen typen prüfen
if (!empty($field['minlength']) && strlen($sPostFieldValue) < $field['minlength'])
return false;
if (!empty($field['maxlength']) && strlen($sPostFieldValue) > $field['maxlength'])
return false;
return true;
return !(!empty($field['maxlength']) && strlen($sPostFieldValue) > $field['maxlength']);
}
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 "CMS_HTML[101]";
} else {
$form = new w3form($captchaInstalled);
$form = new w3form($captchaInstalled, $sess);
$form->addEmailAdress("CMS_VALUE[0]");
$form->setEmailSubject("CMS_VALUE[1]");
$form->setEmailFrom("CMS_VALUE[2]", "CMS_VALUE[3]");
@ -450,4 +472,4 @@ if (cRegistry::isBackendEditMode()) {
$form->process();
}
?>
?>