105 lines
3.4 KiB
PHP
105 lines
3.4 KiB
PHP
<?php
|
|
/**
|
|
* File: VerificationService.php
|
|
* Version: 1.0
|
|
* Path: /app/Services/VerificationService.php
|
|
* Purpose: Manages generation, storage, expiration, and removal of email verification codes.
|
|
* Project: Wizdom Networks Website
|
|
*/
|
|
|
|
namespace WizdomNetworks\WizeWeb\Services;
|
|
|
|
use WizdomNetworks\WizeWeb\Utilities\Database;
|
|
use WizdomNetworks\WizeWeb\Utilities\Logger;
|
|
use WizdomNetworks\WizeWeb\Utilities\ErrorHandler;
|
|
use DateTime;
|
|
use Exception;
|
|
|
|
class VerificationService
|
|
{
|
|
private const CODE_BYTES = 16;
|
|
private const EXPIRATION_INTERVAL = '+72 hours';
|
|
|
|
/**
|
|
* Generates a secure verification code.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function generateCode(): string
|
|
{
|
|
return bin2hex(random_bytes(self::CODE_BYTES));
|
|
}
|
|
|
|
/**
|
|
* Returns the expiration timestamp for a verification code.
|
|
*
|
|
* @return string MySQL-compatible datetime string
|
|
*/
|
|
public function getExpirationTime(): string
|
|
{
|
|
return (new DateTime(self::EXPIRATION_INTERVAL))->format('Y-m-d H:i:s');
|
|
}
|
|
|
|
/**
|
|
* Assigns a verification code to a contact or subscriber record.
|
|
*
|
|
* @param string $table Table name (e.g., 'subscribers', 'contact_messages')
|
|
* @param int $id Record ID
|
|
* @param string $code Verification code
|
|
* @param string $expiresAt Expiration timestamp
|
|
* @return bool True on success, false on failure
|
|
*/
|
|
public function assignCodeToRecord(string $table, int $id, string $code, string $expiresAt): bool
|
|
{
|
|
try {
|
|
$db = Database::getConnection();
|
|
$stmt = $db->prepare("UPDATE {$table} SET verification_code = ?, is_verified = 0, verification_expires_at = ? WHERE id = ?");
|
|
return $stmt->execute([$code, $expiresAt, $id]);
|
|
} catch (Exception $e) {
|
|
Logger::error("Failed to assign verification code to {$table} ID {$id}: " . $e->getMessage());
|
|
ErrorHandler::exception($e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Deletes expired verification codes from a table.
|
|
*
|
|
* @param string $table Table name (e.g., 'subscribers', 'contact_messages')
|
|
* @return int Number of rows deleted
|
|
*/
|
|
public function deleteExpiredCodes(string $table): int
|
|
{
|
|
try {
|
|
$db = Database::getConnection();
|
|
$stmt = $db->prepare("UPDATE {$table} SET verification_code = NULL WHERE verification_expires_at IS NOT NULL AND verification_expires_at < NOW()");
|
|
$stmt->execute();
|
|
return $stmt->rowCount();
|
|
} catch (Exception $e) {
|
|
Logger::error("Failed to clear expired codes in {$table}: " . $e->getMessage());
|
|
ErrorHandler::exception($e);
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Removes the verification code from a specific record.
|
|
*
|
|
* @param string $table Table name
|
|
* @param int $id Record ID
|
|
* @return bool
|
|
*/
|
|
public function clearCode(string $table, int $id): bool
|
|
{
|
|
try {
|
|
$db = Database::getConnection();
|
|
$stmt = $db->prepare("UPDATE {$table} SET verification_code = NULL WHERE id = ?");
|
|
return $stmt->execute([$id]);
|
|
} catch (Exception $e) {
|
|
Logger::error("Failed to clear verification code for {$table} ID {$id}: " . $e->getMessage());
|
|
ErrorHandler::exception($e);
|
|
return false;
|
|
}
|
|
}
|
|
}
|