WizdomWeb/app/Models/SubmissionLogModel.php

69 lines
2.1 KiB
PHP

<?php
/**
* File: SubmissionLogModel.php
* Version: 1.0
* Path: /app/Models/SubmissionLogModel.php
* Purpose: Logs every contact form submission attempt for auditing and spam control.
* Project: Wizdom Networks Website
*/
namespace WizdomNetworks\WizeWeb\Models;
use PDO;
use WizdomNetworks\WizeWeb\Utilities\Logger;
use WizdomNetworks\WizeWeb\Utilities\ErrorHandler;
/**
* Class SubmissionLogModel
*
* Handles insertion of contact form submission logs into the database.
*/
class SubmissionLogModel
{
private PDO $db;
/**
* SubmissionLogModel constructor.
*
* @param PDO $db A valid database connection.
*/
public function __construct(PDO $db)
{
$this->db = $db;
}
/**
* Log a contact form submission attempt.
*
* @param array $data {
* @type string $email The submitted email address.
* @type string|null $phone The submitted phone number.
* @type string|null $ip_address The user's IP address.
* @type string|null $user_agent The user agent string.
* @type bool $was_saved True if saved to contact_messages.
* @type string $reason Classification reason (e.g. 'valid', 'blocked:honeypot').
* }
* @return bool True on success, false on failure.
*/
public function logAttempt(array $data): bool
{
try {
$sql = "INSERT INTO submission_logs (email, phone, ip_address, user_agent, was_saved, reason)
VALUES (:email, :phone, :ip, :agent, :saved, :reason)";
$stmt = $this->db->prepare($sql);
return $stmt->execute([
':email' => $data['email'],
':phone' => $data['phone'] ?? null,
':ip' => $data['ip_address'] ?? null,
':agent' => $data['user_agent'] ?? null,
':saved' => $data['was_saved'] ? 1 : 0,
':reason' => $data['reason']
]);
} catch (\Throwable $e) {
Logger::error("Failed to log submission attempt.");
ErrorHandler::exception($e);
return false;
}
}
}