diff --git a/src/Services/Renderer.php b/src/Services/Renderer.php index dd96528..9ef3143 100644 --- a/src/Services/Renderer.php +++ b/src/Services/Renderer.php @@ -71,18 +71,44 @@ class Renderer } $projectExportDir = $this->exportsPath . DIRECTORY_SEPARATOR . $projectId; - $assetsDir = $projectExportDir . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'css'; + $assetsDir = $projectExportDir . DIRECTORY_SEPARATOR . 'assets'; + $cssDir = $assetsDir . DIRECTORY_SEPARATOR . 'css'; + $jsDir = $assetsDir . DIRECTORY_SEPARATOR . 'js'; // 1. Create directory structure - if (!is_dir($assetsDir)) { - mkdir($assetsDir, 0777, true); - } + if (!is_dir($cssDir)) mkdir($cssDir, 0777, true); + if (!is_dir($jsDir)) mkdir($jsDir, 0777, true); // 2. Copy static assets - $siteCssSource = $this->templatesPath . DIRECTORY_SEPARATOR . 'css' . DIRECTORY_SEPARATOR . 'site.css'; - copy($siteCssSource, $assetsDir . DIRECTORY_SEPARATOR . 'site.css'); + copy($this->templatesPath . '/css/site.css', $cssDir . '/site.css'); + copy($this->templatesPath . '/js/site.js', $jsDir . '/site.js'); - // 3. Prepare relative paths for assets + // 3. Handle Contact Form Logic + $formConfig = $projectData['wizard_data']['modules']['contact_form'] ?? []; + if (!empty($formConfig['enabled'])) { + // Copy handler + copy($this->templatesPath . '/emailer.php', $projectExportDir . '/ajax.php'); + + // Create config for handler + $siteConfig = [ + 'site_name' => $projectData['wizard_data']['identity']['business_name'], + 'form_mode' => $formConfig['mode'] ?? 'local', + 'smtp' => $formConfig['smtp'] ?? null, + 'local_password_hash' => $formConfig['local_viewer']['password_hash'] ?? null + ]; + file_put_contents($projectExportDir . '/config.json', json_encode($siteConfig, JSON_PRETTY_PRINT)); + + if ($siteConfig['form_mode'] === 'local') { + // Copy viewer + copy($this->templatesPath . '/form-viewer.php', $projectExportDir . '/form-viewer.php'); + // Create messages dir + $msgDir = $projectExportDir . '/messages'; + if (!is_dir($msgDir)) mkdir($msgDir, 0777, true); + file_put_contents($msgDir . '/.htaccess', "Deny from all"); + } + } + + // 4. Prepare relative paths for assets if (!empty($projectData['wizard_data']['assets']['logo'])) { $projectData['wizard_data']['assets']['logo'] = $this->makePathRelative($projectData['wizard_data']['assets']['logo'], $projectId); } diff --git a/src/Templates/base.php b/src/Templates/base.php index 674a7a3..3894c28 100644 --- a/src/Templates/base.php +++ b/src/Templates/base.php @@ -82,5 +82,6 @@ $assets = $project['wizard_data']['assets'] ?? []; + diff --git a/src/Templates/emailer.php b/src/Templates/emailer.php new file mode 100644 index 0000000..cfe307c --- /dev/null +++ b/src/Templates/emailer.php @@ -0,0 +1,82 @@ + $success, 'message' => $message]); + exit; +} + +if ($_SERVER['REQUEST_METHOD'] !== 'POST') { + sendResponse(false, 'Method not allowed.'); +} + +// Honeypot spam protection +if (!empty($_POST['website'])) { + sendResponse(true, 'Spam detected.'); // Fake success for bots +} + +$name = trim($_POST['name'] ?? ''); +$email = trim($_POST['email'] ?? ''); +$message = trim($_POST['message'] ?? ''); + +if (!$name || !$email || !$message) { + sendResponse(false, 'Prosím vyplňte všetky povinné polia.'); +} + +if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { + sendResponse(false, 'Neplatná emailová adresa.'); +} + +$mode = $config['form_mode'] ?? 'local'; + +if ($mode === 'local') { + // Save to messages directory + $msgDir = 'messages'; + if (!is_dir($msgDir)) { + mkdir($msgDir, 0777, true); + } + + $msgId = date('Ymd-His') . '-' . bin2hex(random_bytes(4)); + $msgData = [ + 'id' => $msgId, + 'timestamp' => date('c'), + 'sender_name' => $name, + 'sender_email' => $email, + 'message' => $message + ]; + + if (file_put_contents($msgDir . '/' . $msgId . '.json', json_encode($msgData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE))) { + sendResponse(true, 'Vaša správa bola úspešne uložená.'); + } else { + sendResponse(false, 'Nepodarilo sa uložiť správu.'); + } +} else { + // SMTP / Mail mode + $to = $config['smtp']['recipient'] ?? ''; + if (!$to) { + sendResponse(false, 'Cieľový email nie je nakonfigurovaný.'); + } + + $subject = "Nová správa z webu: " . ($config['site_name'] ?? 'WebWizard'); + $body = "Meno: $name\nEmail: $email\n\nSpráva:\n$message"; + $headers = "From: webwizard@{$_SERVER['HTTP_HOST']}\r\n" . + "Reply-To: $email\r\n" . + "X-Mailer: PHP/" . phpversion(); + + // Using standard mail() as fallback for MVP if no complex SMTP is provided + if (mail($to, $subject, $body, $headers)) { + sendResponse(true, 'Vaša správa bola úspešne odoslaná.'); + } else { + sendResponse(false, 'Nepodarilo sa odoslať email.'); + } +} diff --git a/src/Templates/form-viewer.php b/src/Templates/form-viewer.php new file mode 100644 index 0000000..2e2b8f8 --- /dev/null +++ b/src/Templates/form-viewer.php @@ -0,0 +1,95 @@ + + + + + + Prehliadač správ - WebWizard + + + + +
+ +
+

Prihlásenie

+
+
+ + +
+
+ + Odhlásiť sa +

Prijaté správy

+ + +

Zatiaľ ste neprijali žiadne správy.

+ +
+
+ + ID: +
+
Od: (<>)
+
+
+ + +
+ + + diff --git a/src/Templates/js/site.js b/src/Templates/js/site.js new file mode 100644 index 0000000..f03140f --- /dev/null +++ b/src/Templates/js/site.js @@ -0,0 +1,40 @@ +/** + * WebWizard Site Scripts + */ +document.addEventListener('DOMContentLoaded', () => { + const contactForm = document.getElementById('site-contact-form'); + + if (contactForm) { + contactForm.addEventListener('submit', async (e) => { + e.preventDefault(); + + const formData = new FormData(contactForm); + const submitBtn = contactForm.querySelector('button[type="submit"]'); + const originalBtnText = submitBtn.textContent; + + submitBtn.disabled = true; + submitBtn.textContent = 'Odosielam...'; + + try { + const response = await fetch(contactForm.action, { + method: 'POST', + body: formData + }); + + const result = await response.json(); + + alert(result.message); + + if (result.success) { + contactForm.reset(); + } + } catch (error) { + console.error('Form submission error:', error); + alert('Vyskytla sa chyba pri odosielaní formulára. Skúste to prosím neskôr.'); + } finally { + submitBtn.disabled = false; + submitBtn.textContent = originalBtnText; + } + }); + } +}); diff --git a/src/Templates/sections/contact.php b/src/Templates/sections/contact.php index 7becf23..30f0f0c 100644 --- a/src/Templates/sections/contact.php +++ b/src/Templates/sections/contact.php @@ -33,6 +33,9 @@ $formEnabled = $project['wizard_data']['modules']['contact_form']['enabled'] ??

Napíšte nám

+ + +