minor updates

This commit is contained in:
essae 2025-05-13 01:20:16 -04:00
parent 761c41d3bb
commit b48a5f8e0c
2 changed files with 11 additions and 7 deletions

View File

@ -1,7 +1,7 @@
<?php <?php
/** /**
* File: ContactController.php * File: ContactController.php
* Version: 1.2 * Version: 1.5
* Path: /app/Controllers/ContactController.php * Path: /app/Controllers/ContactController.php
* Purpose: Handles contact form display and submission logic. * Purpose: Handles contact form display and submission logic.
* Project: Wizdom Networks Website * Project: Wizdom Networks Website
@ -14,6 +14,7 @@ use WizdomNetworks\WizeWeb\Utilities\Logger;
use WizdomNetworks\WizeWeb\Utilities\Validator; use WizdomNetworks\WizeWeb\Utilities\Validator;
use WizdomNetworks\WizeWeb\Utilities\Response; use WizdomNetworks\WizeWeb\Utilities\Response;
use WizdomNetworks\WizeWeb\Utilities\Sanitizer; use WizdomNetworks\WizeWeb\Utilities\Sanitizer;
use WizdomNetworks\WizeWeb\Utilities\Database;
use WizdomNetworks\WizeWeb\Models\ContactModel; use WizdomNetworks\WizeWeb\Models\ContactModel;
use Exception; use Exception;
@ -58,15 +59,18 @@ class ContactController
$message = Sanitizer::sanitizeString($data['message']); $message = Sanitizer::sanitizeString($data['message']);
// Validate email format // Validate email format
if (!Validator::isEmail($email)) { if (!Validator::IsEmail($email)) {
Response::badRequest('Invalid email address.'); Response::badRequest('Invalid email address.');
} }
$ip = $_SERVER['REMOTE_ADDR'] ?? 'unknown'; $ip = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
$userAgent = $_SERVER['HTTP_USER_AGENT'] ?? 'unknown'; $userAgent = $_SERVER['HTTP_USER_AGENT'] ?? 'unknown';
// Insert into database // Create DB connection and save to DB
$contact = new ContactModel(); $database = new Database();
$pdo = $database->getConnection();
$contact = new ContactModel($pdo);
$result = $contact->save([ $result = $contact->save([
'first_name' => $firstName, 'first_name' => $firstName,
'last_name' => $lastName, 'last_name' => $lastName,
@ -77,7 +81,7 @@ class ContactController
]); ]);
if (!$result) { if (!$result) {
Logger::logError("Failed to save contact form submission for email: $email"); Logger::error("Failed to save contact form submission for email: $email");
Response::serverError('An error occurred while submitting your message. Please try again later.'); Response::serverError('An error occurred while submitting your message. Please try again later.');
} }
@ -86,7 +90,7 @@ class ContactController
'message' => 'Your message has been successfully submitted. Thank you!' 'message' => 'Your message has been successfully submitted. Thank you!'
]); ]);
} catch (Exception $e) { } catch (Exception $e) {
Logger::logError("Exception during contact form submission: " . $e->getMessage()); Logger::error("Exception during contact form submission: " . $e->getMessage());
Response::serverError('A server error occurred. Please try again later.'); Response::serverError('A server error occurred. Please try again later.');
} }
} }

View File

@ -37,7 +37,7 @@ class Database
$dsn = sprintf('mysql:host=%s;dbname=%s;charset=utf8mb4', $_ENV['DB_HOST'], $_ENV['DB_NAME']); $dsn = sprintf('mysql:host=%s;dbname=%s;charset=utf8mb4', $_ENV['DB_HOST'], $_ENV['DB_NAME']);
try { try {
$this->connection = new PDO($dsn, $_ENV['DB_USER'], $_ENV['DB_PASSWORD']); $this->connection = new PDO($dsn, $_ENV['DB_USER'], $_ENV['DB_PASS']);
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Logger::info('Database connection established successfully.'); Logger::info('Database connection established successfully.');
} catch (PDOException $e) { } catch (PDOException $e) {