ok… it’s a dirty solution… but it works =)
The Template-file in here is based on UIKit integrated in The Zero One Template
I just post this for others in need. i do not have the skills and time to maintain this as a plugin, nor is this universal (because of uikit dependence)
At first i made a solution without encryption like described above, (via iterating over used tags at first and third stage) but this was problematic in various cases… so i decided to go for a solution that is more compatible… for example in use via kirbytag()
Maybe this is a startingpoint for a new feature for the The Zero One Template @mrfreedom
index..php
<?php
Kirby::plugin('demlak/mailform', [
'snippets' => [
'mailformmodal' => __DIR__ . '/snippets/mailformmodal.php',
],
'templates' => [
'mailformparse' => __DIR__ . '/templates/mailformparse.php',
],
'routes' => function () {
return [
[
'pattern' => 'mailformparse',
'method' => 'GET|POST',
'action' => function () {
return Page::factory([
'slug' => 'mailformparse',
'template' => 'mailformparse',
'content' => [
// choose own
'decryption_iv' => '1234567891234567',
// choose own
'decryption_key' => 'mailform_by_demlak',
'ciphering' => 'AES-128-CTR',
]
]);
}
]
];
},
'tags' => [
'mailform' => [
'attr' => [
'class',
'label',
// image-tag currently not used in template
'image',
'title',
],
'html' => function($tag) {
// Store a string into the variable which need to be Encrypted
$simple_string = $tag->value();
// Store the cipher method
$ciphering = "AES-128-CTR";
// Use OpenSSl Encryption method
$iv_length = openssl_cipher_iv_length($ciphering);
$options = 0;
// Non-NULL Initialization Vector for encryption
$encryption_iv = '1234567891234567';
// Store the encryption key
$encryption_key = 'mailform_by_demlak';
// Use openssl_encrypt() function to encrypt the data
$encryption = openssl_encrypt($simple_string, $ciphering, $encryption_key, $options=0, $encryption_iv);
return snippet('mailformmodal', [
'class' => $tag->attr('class'),
'label' => $tag->attr('label'),
'image' => $tag->attr('image'),
'title' => $tag->attr('title'),
'targetid' => $encryption,
'randid' => Str::random(3)
], true
);
}
]
]
]);
snippets/mailformmodal.php
<a href="#modal-<?= $randid ?>" class="<?= $class ?>" uk-toggle><?= e(empty($label), "Kontakt", $label) ?></a>
<div id="modal-<?= $randid ?>" class="uk-flex-top" uk-modal>
<div class="uk-modal-dialog uk-modal-body uk-margin-auto-vertical">
<button class="uk-modal-close-default" type="button" uk-close></button>
<h3 class="uk-text-break">
<?php e(empty($title), $page->title()->html() . " - Kontakt", $title) ?>
</h3>
<hr />
<div class="uk-text-right">
<form id="my_form-<?= $randid ?>" onsubmit="submitForm<?= $randid ?>(); return false;">
<div class="tm-hon">
<label for="website">Website <abbr title="required">*</abbr></label>
<input type="website" id="website" name="website">
</div>
<p class="uk-inline uk-width-1-1">
<span class="uk-form-icon" uk-icon="icon: user"></span>
<input id="name<?= $randid ?>" placeholder="<?= $site->labelName()->html() ?>" required class="uk-input">
</p>
<p class="uk-inline uk-width-1-1">
<span class="uk-form-icon" uk-icon="icon: mail"></span>
<input id="email<?= $randid ?>" placeholder="<?= $site->labelEmail()->html() ?>" type="email" required class="uk-input">
</p>
<p class="uk-inline uk-width-1-1">
<textarea id="text<?= $randid ?>" placeholder="<?= $site->labelMessage()->html() ?>" rows="10" required class="uk-textarea"></textarea>
</p>
<p>
<span id="status_<?= $randid ?>"> </span>
<input id="mybtn<?= $randid ?>" type="submit" value="<?= $site->labelSubmit()->html() ?>" class="uk-button uk-button-default">
</p>
</form>
</div>
</div>
</div>
<script type="text/javascript">
function _(id){ return document.getElementById(id); }
function submitForm<?= $randid ?>(){
_("mybtn<?= $randid ?>").disabled = true;
_("status_<?= $randid ?>").innerHTML = 'please wait ...';
var formdata = new FormData();
formdata.append( "targetid", '<?= $targetid ?>');
formdata.append( "name", _("name<?= $randid ?>").value );
formdata.append( "email", _("email<?= $randid ?>").value );
formdata.append( "text", _("text<?= $randid ?>").value );
var ajax = new XMLHttpRequest();
ajax.open( "POST", "<?= $site->url() ?>/mailformparse" );
ajax.onreadystatechange = function() {
if(ajax.readyState == 4 && ajax.status == 200) {
if(ajax.responseText == "success"){
_("my_form-<?= $randid ?>").innerHTML = '<h2>Danke '+_("name<?= $randid ?>").value+', <br>deine Nachricht ist gesendet.</h2>';
} else {
_("status_<?= $randid ?>").innerHTML = ajax.responseText;
_("mybtn<?= $randid ?>").disabled = false;
}
}
}
ajax.send( formdata );
}
</script>
templates/mailformparse.php
<?php
$alert = null;
if( isset($_POST['name']) && isset($_POST['email']) && isset($_POST['text']) ){
$targetid = get('targetid');
$ciphering = $page->ciphering();
$decryption_key = $page->decryption_key();
$decryption_iv = $page->decryption_iv();
// Use openssl_decrypt() function to decrypt the data
$targetmail=openssl_decrypt($targetid, $ciphering, $decryption_key, $options=0, $decryption_iv);
// check the honeypot
if(empty(get('website')) === false) {
go($page->url());
exit;
}
$data = [
'name' => get('name'),
'email' => get('email'),
'text' => get('text')
];
$rules = [
'name' => ['required', 'min' => 3],
'email' => ['required', 'email'],
'text' => ['required', 'min' => 3, 'max' => 3000],
];
$messages = [
'name' => esc($site->labelAlertName()->or('Please enter a valid name')),
'email' => esc($site->labelAlertEmail()->or('Please enter a valid email address')),
'text' => esc($site->labelAlertMessage()->or('Please enter a text between 3 and 3000 characters'))
];
// some of the data is invalid
if($invalid = invalid($data, $rules, $messages)) {
$alert = $invalid;
echo '<div class="uk-text-left uk-alert-danger" uk-alert>';
foreach ($alert as $alertmessage) {
echo "<p>" . $alertmessage . "</p>";
}
echo "</div>";
// the data is fine, let's send the email
} else {
try {
$kirby->email([
'template' => 'email',
'from' => esc($site->email()),
'replyTo' => $data['email'],
'to' => esc($targetmail),
'subject' => '[evh-website] ' . esc($data['name']) . ' ' . esc($site->labelEmailSubject()->or('sent you a message from your contact form')),
'data' => [
'text' => esc($data['text']),
'sender' => esc($data['name'])
]
]);
} catch (Exception $error) {
echo "error";
}
// no exception occured, let's send a success message
if (empty($alert) === true) {
$data = [];
echo "success";
}
}
}
?>