2024-10-25 13:37:21 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Socialbox\Managers;
|
|
|
|
|
|
|
|
use DateTime;
|
|
|
|
use PDOException;
|
|
|
|
use Socialbox\Classes\Database;
|
2024-10-31 15:49:42 -04:00
|
|
|
use Socialbox\Classes\Logger;
|
2024-10-25 13:37:21 -04:00
|
|
|
use Socialbox\Classes\Utilities;
|
|
|
|
use Socialbox\Enums\Status\CaptchaStatus;
|
|
|
|
use Socialbox\Exceptions\DatabaseOperationException;
|
|
|
|
use Socialbox\Objects\Database\CaptchaRecord;
|
|
|
|
use Socialbox\Objects\Database\RegisteredPeerRecord;
|
|
|
|
|
|
|
|
class CaptchaManager
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Creates a new captcha for the given peer UUID.
|
|
|
|
*
|
|
|
|
* @param string|RegisteredPeerRecord $peer_uuid The UUID of the peer to create the captcha for.
|
|
|
|
* @return string The answer to the captcha.
|
|
|
|
* @throws DatabaseOperationException If the operation fails.
|
|
|
|
*/
|
2024-10-31 15:49:42 -04:00
|
|
|
public static function createCaptcha(string|RegisteredPeerRecord $peer_uuid): string
|
2024-10-25 13:37:21 -04:00
|
|
|
{
|
|
|
|
// If the peer_uuid is a RegisteredPeerRecord, get the UUID
|
|
|
|
if($peer_uuid instanceof RegisteredPeerRecord)
|
|
|
|
{
|
|
|
|
$peer_uuid = $peer_uuid->getUuid();
|
|
|
|
}
|
|
|
|
|
|
|
|
$answer = Utilities::randomString(6, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789');
|
2024-12-09 21:26:25 -05:00
|
|
|
$current_time = (new DateTime())->setTimestamp(time())->format('Y-m-d H:i:s');
|
2024-10-25 13:37:21 -04:00
|
|
|
|
|
|
|
if(!self::captchaExists($peer_uuid))
|
|
|
|
{
|
2024-10-31 15:49:42 -04:00
|
|
|
Logger::getLogger()->debug('Creating a new captcha record for peer ' . $peer_uuid);
|
2024-12-09 19:01:56 -05:00
|
|
|
$statement = Database::getConnection()->prepare("INSERT INTO captcha_images (peer_uuid, created, answer) VALUES (?, ?, ?)");
|
2024-10-25 13:37:21 -04:00
|
|
|
$statement->bindParam(1, $peer_uuid);
|
2024-12-09 19:01:56 -05:00
|
|
|
$statement->bindParam(2, $current_time);
|
|
|
|
$statement->bindParam(3, $answer);
|
2024-10-25 13:37:21 -04:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$statement->execute();
|
|
|
|
}
|
|
|
|
catch(PDOException $e)
|
|
|
|
{
|
|
|
|
throw new DatabaseOperationException('Failed to create a captcha in the database', $e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $answer;
|
|
|
|
}
|
|
|
|
|
2024-10-31 15:49:42 -04:00
|
|
|
Logger::getLogger()->debug('Updating an existing captcha record for peer ' . $peer_uuid);
|
2024-12-09 19:01:56 -05:00
|
|
|
$statement = Database::getConnection()->prepare("UPDATE captcha_images SET answer=?, status='UNSOLVED', created=? WHERE peer_uuid=?");
|
2024-10-25 13:37:21 -04:00
|
|
|
$statement->bindParam(1, $answer);
|
2024-12-09 19:01:56 -05:00
|
|
|
$statement->bindParam(2, $current_time);
|
|
|
|
$statement->bindParam(3, $peer_uuid);
|
2024-10-25 13:37:21 -04:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$statement->execute();
|
|
|
|
}
|
|
|
|
catch(PDOException $e)
|
|
|
|
{
|
|
|
|
throw new DatabaseOperationException('Failed to update a captcha in the database', $e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $answer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Answers a captcha for the given peer UUID.
|
|
|
|
*
|
|
|
|
* @param string|RegisteredPeerRecord $peer_uuid The UUID of the peer to answer the captcha for.
|
|
|
|
* @param string $answer The answer to the captcha.
|
|
|
|
* @return bool True if the answer is correct, false otherwise.
|
|
|
|
* @throws DatabaseOperationException If the operation fails.
|
|
|
|
*/
|
|
|
|
public static function answerCaptcha(string|RegisteredPeerRecord $peer_uuid, string $answer): bool
|
|
|
|
{
|
|
|
|
if($peer_uuid instanceof RegisteredPeerRecord)
|
|
|
|
{
|
|
|
|
$peer_uuid = $peer_uuid->getUuid();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return false if the captcha does not exist
|
|
|
|
if(!self::captchaExists($peer_uuid))
|
|
|
|
{
|
2024-10-31 15:49:42 -04:00
|
|
|
Logger::getLogger()->warning(sprintf("The requested captcha does not exist for the peer %s", $peer_uuid));
|
2024-10-25 13:37:21 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$captcha = self::getCaptcha($peer_uuid);
|
|
|
|
|
|
|
|
// Return false if the captcha has already been solved
|
|
|
|
if($captcha->getStatus() === CaptchaStatus::SOLVED)
|
|
|
|
{
|
2024-10-31 15:49:42 -04:00
|
|
|
Logger::getLogger()->warning(sprintf("The requested captcha has already been solved for the peer %s", $peer_uuid));
|
2024-10-25 13:37:21 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return false if the captcha is older than 5 minutes
|
2024-10-31 15:49:42 -04:00
|
|
|
if ($captcha->isExpired())
|
2024-10-25 13:37:21 -04:00
|
|
|
{
|
2024-10-31 15:49:42 -04:00
|
|
|
Logger::getLogger()->warning(sprintf("The requested captcha is older than 5 minutes for the peer %s", $peer_uuid));
|
2024-10-25 13:37:21 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the answer
|
|
|
|
if($captcha->getAnswer() !== $answer)
|
|
|
|
{
|
2024-10-31 15:49:42 -04:00
|
|
|
Logger::getLogger()->warning(sprintf("The answer to the requested captcha is incorrect for the peer %s", $peer_uuid));
|
2024-10-25 13:37:21 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-10-31 15:49:42 -04:00
|
|
|
Logger::getLogger()->debug(sprintf("The answer to the requested captcha is correct for the peer %s", $peer_uuid));
|
2024-10-25 13:37:21 -04:00
|
|
|
$statement = Database::getConnection()->prepare("UPDATE captcha_images SET status='SOLVED', answered=NOW() WHERE peer_uuid=?");
|
|
|
|
$statement->bindParam(1, $peer_uuid);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$statement->execute();
|
|
|
|
}
|
|
|
|
catch(PDOException $e)
|
|
|
|
{
|
|
|
|
throw new DatabaseOperationException('Failed to update a captcha in the database', $e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the captcha record for the given peer UUID.
|
|
|
|
*
|
|
|
|
* @param string|RegisteredPeerRecord $peer_uuid The UUID of the peer to retrieve the captcha for.
|
2024-12-14 00:43:19 -05:00
|
|
|
* @return CaptchaRecord|null The captcha record.
|
2024-10-25 13:37:21 -04:00
|
|
|
* @throws DatabaseOperationException If the operation fails.
|
|
|
|
*/
|
2024-12-14 00:43:19 -05:00
|
|
|
public static function getCaptcha(string|RegisteredPeerRecord $peer_uuid): ?CaptchaRecord
|
2024-10-25 13:37:21 -04:00
|
|
|
{
|
|
|
|
// If the peer_uuid is a RegisteredPeerRecord, get the UUID
|
|
|
|
if($peer_uuid instanceof RegisteredPeerRecord)
|
|
|
|
{
|
|
|
|
$peer_uuid = $peer_uuid->getUuid();
|
|
|
|
}
|
|
|
|
|
2024-10-31 15:49:42 -04:00
|
|
|
Logger::getLogger()->debug('Getting the captcha record for peer ' . $peer_uuid);
|
|
|
|
|
2024-10-25 13:37:21 -04:00
|
|
|
try
|
|
|
|
{
|
|
|
|
$statement = Database::getConnection()->prepare("SELECT * FROM captcha_images WHERE peer_uuid=? LIMIT 1");
|
|
|
|
$statement->bindParam(1, $peer_uuid);
|
|
|
|
$statement->execute();
|
|
|
|
$result = $statement->fetch();
|
|
|
|
}
|
|
|
|
catch(PDOException $e)
|
|
|
|
{
|
|
|
|
throw new DatabaseOperationException('Failed to get a captcha from the database', $e);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($result === false)
|
|
|
|
{
|
2024-12-14 00:43:19 -05:00
|
|
|
return null;
|
2024-10-25 13:37:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return CaptchaRecord::fromArray($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a captcha exists for the given peer UUID.
|
|
|
|
*
|
|
|
|
* @param string|RegisteredPeerRecord $peer_uuid The UUID of the peer to check for a captcha.
|
|
|
|
* @return bool True if a captcha exists, false otherwise.
|
|
|
|
* @throws DatabaseOperationException If the operation fails.
|
|
|
|
*/
|
2024-12-14 00:43:19 -05:00
|
|
|
public static function captchaExists(string|RegisteredPeerRecord $peer_uuid): bool
|
2024-10-25 13:37:21 -04:00
|
|
|
{
|
|
|
|
// If the peer_uuid is a RegisteredPeerRecord, get the UUID
|
|
|
|
if($peer_uuid instanceof RegisteredPeerRecord)
|
|
|
|
{
|
|
|
|
$peer_uuid = $peer_uuid->getUuid();
|
|
|
|
}
|
|
|
|
|
2024-10-31 15:49:42 -04:00
|
|
|
Logger::getLogger()->debug('Checking if a captcha exists for peer ' . $peer_uuid);
|
|
|
|
|
2024-10-25 13:37:21 -04:00
|
|
|
try
|
|
|
|
{
|
|
|
|
$statement = Database::getConnection()->prepare("SELECT COUNT(*) FROM captcha_images WHERE peer_uuid=?");
|
|
|
|
$statement->bindParam(1, $peer_uuid);
|
|
|
|
$statement->execute();
|
|
|
|
$result = $statement->fetchColumn();
|
|
|
|
}
|
|
|
|
catch(PDOException $e)
|
|
|
|
{
|
|
|
|
throw new DatabaseOperationException('Failed to check if a captcha exists in the database', $e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result > 0;
|
|
|
|
}
|
|
|
|
}
|