Poprawki bezpieczeństwa formularza i panelu ustawień

- wp_unslash dla pól formularza i opcji (koniec z \' w mailach)
- panel: wymagany nonce i manage_options, sanityzacja, esc_attr
- reCAPTCHA: oczekiwana akcja i próg wyniku 0.5
- From z domeny strony, adres nadawcy tylko w Reply-To
- honeypot bez die(), walidacja JS nie jest omijana
- komunikat o nieudanej wysyłce, zachowanie wpisanych pól
- nowy wygląd formularza (klasy ic-*) i szablon maila

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 23:44:46 +01:00
co-authored by Claude Fable 5.2
parent f6b707a6f2
commit 0f7eddd6a0
7 changed files with 506 additions and 268 deletions
+232 -183
View File
@@ -3,12 +3,12 @@
namespace inContact;
/**
* Klasa Menus
* Klasa Shortcode
*
* @link https://krzysztof-turek.com
*
* @package tylkofotografia.pl
* @version 0.2
* @version 0.3
*/
class Shortcode
{
@@ -29,29 +29,34 @@ class Shortcode
$em_name = null;
$em_mail = null;
$em_message = null;
$class = null;
$html = null;
$error = null;
$body = null;
$telefon = null;
$mailResult = 0;
$canSend = false;
$sendFailed = false;
$form_submitted = $_SERVER["REQUEST_METHOD"] === "POST";
if (!empty($_POST["website"])) {
die();
}
global $pluginDirUrl;
// Honeypot — bot dostaje pozorny sukces, mail nie jest wysyłany
$isBot = !empty($_POST["website"]);
if ($form_submitted) {
$emmsg = isset($_POST["emmsg"]) ? $_POST["emmsg"] : "";
// Walidacja pól
if (!empty($_POST["em_name"])) {
$em_name = $_POST["em_name"];
$em_name = sanitize_text_field(wp_unslash($_POST["em_name"]));
} else {
$error .=
esc_html__("Please enter your name", "in-contact") .
"<br />";
}
if (!empty($_POST["em_mail"])) {
$em_mail = $_POST["em_mail"];
if (!empty($_POST["em_mail"]) && is_email($_POST["em_mail"])) {
$em_mail = sanitize_email(wp_unslash($_POST["em_mail"]));
} else {
$error .=
esc_html__(
@@ -60,41 +65,47 @@ class Shortcode
) . "<br />";
}
if (!empty($_POST["em_message"])) {
$em_message = $_POST["em_message"];
$em_message = sanitize_textarea_field(
wp_unslash($_POST["em_message"]),
);
} else {
$error .=
esc_html__("Please enter your message", "in-contact") .
"<br />";
}
$targetEmail =
"To: Tylkofotografia <" .
get_option("incontact-target-mail") .
">";
$mailResult = 0;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .=
"Content-type: text/html; charset=" .
get_bloginfo("charset") .
"" .
"\r\n";
$headers .= "From: " . $em_name . " <" . $em_mail . ">" . "\r\n";
// Budowanie emaila
// From z domeny strony (SPF/DMARC), adres nadawcy tylko w Reply-To
$siteHost = (string) wp_parse_url(home_url(), PHP_URL_HOST);
if (strpos($siteHost, "www.") === 0) {
$siteHost = substr($siteHost, 4);
}
$headerChars = ["\"", "<", ">", "\r", "\n"];
$fromName = str_replace(
$headerChars,
"",
wp_specialchars_decode(get_bloginfo("name"), ENT_QUOTES),
);
$replyName = str_replace($headerChars, "", (string) $em_name);
$headers = [
"Content-Type: text/html; charset=UTF-8",
"From: " . $em_name . " <" . $em_mail . ">",
"Reply-To: " . $em_name . " <" . $em_mail . ">",
"From: " . $fromName . " <wordpress@" . $siteHost . ">",
"Reply-To: " . $replyName . " <" . $em_mail . ">",
];
$targetEmail = get_option("incontact-target-mail");
$emailTitle = __("New message from contact form", "in-contact");
$emailHeading = __("Contact details", "in-contact");
$emailNameLabel = __("Name and surname", "in-contact");
$emailEmailLabel = __("Email", "in-contact");
$emailMessageHeading = __("Message content", "in-contact");
$emailMsgHeading = __("Message content", "in-contact");
$emailReplyLabel = __("Reply to message", "in-contact");
$emailFooter = __(
"This email was generated automatically by the website contact form.",
"in-contact",
);
$emailRights = __("All rights reserved.", "in-contact");
$body =
'<!DOCTYPE html>
<html lang="pl">
@@ -104,70 +115,24 @@ class Shortcode
esc_html($emailTitle) .
'</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f5f5f5;
margin: 0;
padding: 0;
}
.container {
width: 90%;
max-width: 600px;
margin: 20px auto;
background-color: #ffffff;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.header {
background-color: #3a7bd5;
color: #ffffff;
padding: 20px;
text-align: center;
}
.content {
padding: 20px;
line-height: 1.6;
}
.content h2 {
margin-top: 0;
}
.footer {
background-color: #f1f1f1;
text-align: center;
padding: 10px;
font-size: 12px;
color: #777777;
}
.button {
display: inline-block;
padding: 10px 20px;
margin-top: 20px;
background-color: #3a7bd5;
color: #ffffff;
text-decoration: none;
border-radius: 5px;
}
.info {
background-color: #f9f9f9;
border-left: 4px solid #3a7bd5;
padding: 10px;
margin: 10px 0;
}
@media (max-width: 600px) {
.container {
width: 95%;
}
}
body { font-family: Arial, sans-serif; background-color: #f5f5f5; margin: 0; padding: 0; }
.container { width: 90%; max-width: 600px; margin: 20px auto; background-color: #ffffff; border-radius: 8px; overflow: hidden; box-shadow: 0 0 10px rgba(0,0,0,.1); }
.header { background-color: #1a1a2e; color: #ffffff; padding: 20px; text-align: center; }
.header h1 { margin: 0; font-size: 20px; }
.content { padding: 24px; line-height: 1.6; }
.content h2 { margin-top: 0; color: #1a1a2e; }
.info { background-color: #f9f9f9; border-left: 4px solid #e2b04a; padding: 12px 16px; margin: 16px 0; border-radius: 0 4px 4px 0; }
.info h3 { margin: 0 0 8px; color: #1a1a2e; font-size: 14px; text-transform: uppercase; letter-spacing: .05em; }
.button { display: inline-block; padding: 10px 20px; margin-top: 20px; background-color: #e2b04a; color: #1a1a2e; text-decoration: none; border-radius: 4px; font-weight: bold; }
.footer { background-color: #f1f1f1; text-align: center; padding: 12px; font-size: 12px; color: #777; }
@media (max-width: 600px) { .container { width: 95%; } }
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>' .
<div class="header"><h1>' .
esc_html($emailTitle) .
'</h1>
</div>
'</h1></div>
<div class="content">
<h2>' .
esc_html($emailHeading) .
@@ -178,15 +143,12 @@ class Shortcode
<p><strong>' .
esc_html($emailEmailLabel) .
':</strong> {{EMAIL}}</p>
<div class="info">
<h3>' .
esc_html($emailMessageHeading) .
':</h3>
esc_html($emailMsgHeading) .
'</h3>
<p>{{WIADOMOSC}}</p>
</div>
<a href="mailto:{{EMAIL}}" class="button">' .
esc_html($emailReplyLabel) .
'</a>
@@ -195,14 +157,16 @@ class Shortcode
' .
esc_html($emailFooter) .
'<br>
© {{ROK}} Twoja Firma. ' .
&copy; {{ROK}} ' .
esc_html(get_bloginfo("name")) .
". " .
esc_html($emailRights) .
'
</div>
</div>
</body>
</html>
';
</html>';
$body = str_replace(
[
"{{IMIE_NAZWISKO}}",
@@ -212,35 +176,44 @@ class Shortcode
"{{ROK}}",
],
[
$em_name,
$em_mail,
$telefon,
str_replace(["\r\n", "\r", "\n"], "<br />", $em_message),
esc_html($em_name),
esc_html($em_mail),
esc_html($telefon),
str_replace(
["\r\n", "\r", "\n"],
"<br />",
esc_html($em_message),
),
date("Y"),
],
$body,
);
$gRecaptchaResponse = isset($_POST["g-recaptcha-response"])
? $_POST["g-recaptcha-response"]
: "";
// Obsługa błędów walidacji
if (!empty($error)) {
$html .=
'<div class="alert alert-success" role="alert">' .
'<div class="ic-alert ic-alert--error">' .
$error .
"</div>";
} elseif ($emmsg == "true") {
$secret = get_option("incontact-secret-key");
$response = null;
if (get_option("incontact-site-key") != "") {
} elseif ($isBot) {
$mailResult = 1;
} elseif ($emmsg === "true") {
// reCAPTCHA
$gRecaptchaResponse = isset($_POST["g-recaptcha-response"])
? $_POST["g-recaptcha-response"]
: "";
$siteKey = get_option("incontact-site-key");
if ($siteKey != "") {
$secret = get_option("incontact-secret-key");
$reCaptcha = new \ReCaptcha\ReCaptcha($secret);
$resp = $reCaptcha
->setExpectedHostname($_SERVER["SERVER_NAME"])
->setExpectedAction("submit")
->setScoreThreshold(0.5)
->verify($gRecaptchaResponse, $_SERVER["REMOTE_ADDR"]);
if ($resp->isSuccess()) {
$canSend = true;
} else {
$errors = $resp->getErrorCodes();
}
} else {
$canSend = true;
@@ -254,98 +227,174 @@ class Shortcode
$headers,
);
}
$sendFailed = !$mailResult;
}
}
wp_enqueue_style("ContactCss", plugins_url("/css/cform.css", __FILE__));
if (get_option("incontact-site-key") != "") {
$html .=
'<script src="https://www.google.com/recaptcha/api.js"></script>
<script>
$(document).ready(function(){window.setTimeout(function(){$(".alert").fadeTo(500,0).slideUp(500,function(){$(this).remove()})},2e3)}),$(document).ready(function(){"use strict";window.addEventListener("load",function(){var t=document.getElementsByClassName("needs-validation");Array.prototype.filter.call(t,function(t){t.addEventListener("submit",function(e){!1===t.checkValidity()&&(e.preventDefault(),e.stopPropagation()),t.classList.add("was-validated")},!1)})},!1)})();
</script>' . "\n";
// Enqueue styles & reCAPTCHA
wp_enqueue_style(
"inContactCss",
$pluginDirUrl . "css/inContact.css?v=12",
[],
"0.3",
);
$siteKey = get_option("incontact-site-key");
if ($siteKey != "") {
wp_enqueue_script(
"recaptcha",
"https://www.google.com/recaptcha/api.js?render=" . $siteKey,
[],
null,
true,
);
wp_add_inline_script(
"recaptcha",
"
document.getElementById('ic-form').addEventListener('submit', function(e) {
if (e.defaultPrevented) return;
var token = document.getElementById('g-recaptcha-response').value;
if (!token) {
e.preventDefault();
grecaptcha.ready(function() {
grecaptcha.execute('" .
esc_js($siteKey) .
"', {action: 'submit'})
.then(function(token) {
document.getElementById('g-recaptcha-response').value = token;
document.getElementById('ic-form').submit();
});
});
}
});
",
);
}
$html .= '<div class="row justify-content-md-center">
<div class="col-12">';
// Budowanie HTML formularza
$html .= '<div class="ic-wrap">';
if ($mailResult == 1) {
$html .=
'<div class="alert alert-success" role="alert">' .
get_option("incontact-form-snd-ok") .
'<div class="ic-alert ic-alert--success">' .
esc_html(get_option("incontact-form-snd-ok")) .
"</div>";
} elseif ($sendFailed) {
$html .=
'<div class="ic-alert ic-alert--error">' .
esc_html__(
"The message could not be sent. Please try again.",
"in-contact",
) .
"</div>";
}
// Po nieudanej próbie pola zachowują wpisane wartości
$keep = $form_submitted && $mailResult != 1;
$valName = $keep ? esc_attr((string) $em_name) : "";
$valMail = $keep ? esc_attr((string) $em_mail) : "";
$valMessage = $keep ? esc_textarea((string) $em_message) : "";
$html .= '<form action="" method="post" id="ic-form" novalidate>';
$html .=
'<div class="jumbotron"><form action="" class="needs-validation" novalidate method="post" id="demo-form">
<input type="hidden" name="emmsg" value="true" />
<input type="text" id="website" name="website" />
<div class="jumbotron">
<div class="form-group">
<label for="name" class="col-2 control-label">' .
get_option("incontact-form-name") .
'</label>
<div class="col-12">
<input type="text" class="form-control" id="name" name="em_name" placeholder="' .
get_option("incontact-form-ph-name") .
'" value="" required>
<div class="invalid-feedback">
' .
'<input type="hidden" name="g-recaptcha-response" id="g-recaptcha-response">';
$html .= '<input type="hidden" name="emmsg" value="true" />';
// Honeypot — ukryte pole
$html .=
'<div class="ic-field ic-field--honey" aria-hidden="true">' .
'<input type="text" id="website" name="website" tabindex="-1" autocomplete="off" />' .
"</div>";
// Pole: Nazwa
$html .=
'<div class="ic-field">' .
'<label for="ic-name">' .
esc_html(get_option("incontact-form-name")) .
"</label>" .
'<input type="text" id="ic-name" name="em_name" value="' .
$valName .
'" placeholder="' .
esc_attr(get_option("incontact-form-ph-name")) .
'" required />' .
'<span class="ic-field__error">' .
esc_html__("Please enter your name!", "in-contact") .
'
</div>
</div>
</div>
<div class="form-group">
<label for="email" class="col-2 control-label">' .
get_option("incontact-form-email") .
'</label>
<div class="col-12">
<input type="email" class="form-control" id="email" name="em_mail" placeholder="' .
get_option("incontact-form-ph-email") .
'" value="" required>
<div class="invalid-feedback">
' .
"</span>" .
"</div>";
// Pole: Email
$html .=
'<div class="ic-field">' .
'<label for="ic-email">' .
esc_html(get_option("incontact-form-email")) .
"</label>" .
'<input type="email" id="ic-email" name="em_mail" value="' .
$valMail .
'" placeholder="' .
esc_attr(get_option("incontact-form-ph-email")) .
'" required />' .
'<span class="ic-field__error">' .
esc_html__("Please enter your email address!", "in-contact") .
'
</div>
</div>
</div>
<div class="form-group">
<p class="col-2 control-label">' .
get_option("incontact-form-message") .
'</p>
<div class="col-12">
<textarea class="form-control" rows="4" name="em_message" placeholder="' .
get_option("incontact-form-ph-message") .
'" required></textarea>
<div class="invalid-feedback">
' .
"</span>" .
"</div>";
// Pole: Wiadomość
$html .=
'<div class="ic-field">' .
'<label for="ic-message">' .
esc_html(get_option("incontact-form-message")) .
"</label>" .
'<textarea id="ic-message" name="em_message" rows="5" placeholder="' .
esc_attr(get_option("incontact-form-ph-message")) .
'" required>' .
$valMessage .
'</textarea>' .
'<span class="ic-field__error">' .
esc_html__("Please enter your message!", "in-contact") .
'
</div>
</div>
</div>
<div class="form-group">
<div class="col-4">
<div class="g-recaptcha" data-sitekey="' .
get_option("incontact-site-key") .
'"></div>
</div>
</div>
<div class="form-group">
<p></p>
<div class="col-4">
<button id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
<i class="fas fa-paper-plane"></i> ' .
"</span>" .
"</div>";
// Przycisk
$html .=
'<div class="ic-field ic-field--submit">' .
'<button type="submit" id="ic-submit">' .
'<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><line x1="22" y1="2" x2="11" y2="13"></line><polygon points="22 2 15 22 11 13 2 9 22 2"></polygon></svg>' .
" " .
esc_html__("Send", "in-contact") .
'
</button>
</div>
</div>
</div>
</form>
</div>
</div></div>';
"</button>" .
"</div>";
$html .= "</form>";
$html .= "</div>"; // .ic-wrap
// Inline JS — walidacja bez Bootstrap
$html .= "<script>
(function(){
var form = document.getElementById('ic-form');
if (!form) return;
form.addEventListener('submit', function(e) {
var valid = true;
form.querySelectorAll('[required]').forEach(function(field) {
var wrapper = field.closest('.ic-field');
if (!field.value.trim()) {
wrapper.classList.add('ic-field--invalid');
valid = false;
} else {
wrapper.classList.remove('ic-field--invalid');
}
});
if (!valid) e.preventDefault();
});
form.querySelectorAll('[required]').forEach(function(field) {
field.addEventListener('input', function() {
if (field.value.trim()) {
field.closest('.ic-field').classList.remove('ic-field--invalid');
}
});
});
})();
</script>";
return $html;
}