Refactor CaptchaManager to improve UUID validation and streamline variable usage
https://github.com/nosial/Socialbox-PHP/issues/74
This commit is contained in:
parent
938c558cdc
commit
4cdabf6603
1 changed files with 34 additions and 28 deletions
|
@ -3,10 +3,12 @@
|
||||||
namespace Socialbox\Managers;
|
namespace Socialbox\Managers;
|
||||||
|
|
||||||
use DateTime;
|
use DateTime;
|
||||||
|
use InvalidArgumentException;
|
||||||
use PDOException;
|
use PDOException;
|
||||||
use Socialbox\Classes\Database;
|
use Socialbox\Classes\Database;
|
||||||
use Socialbox\Classes\Logger;
|
use Socialbox\Classes\Logger;
|
||||||
use Socialbox\Classes\Utilities;
|
use Socialbox\Classes\Utilities;
|
||||||
|
use Socialbox\Classes\Validator;
|
||||||
use Socialbox\Enums\Status\CaptchaStatus;
|
use Socialbox\Enums\Status\CaptchaStatus;
|
||||||
use Socialbox\Exceptions\DatabaseOperationException;
|
use Socialbox\Exceptions\DatabaseOperationException;
|
||||||
use Socialbox\Objects\Database\CaptchaRecord;
|
use Socialbox\Objects\Database\CaptchaRecord;
|
||||||
|
@ -17,26 +19,30 @@
|
||||||
/**
|
/**
|
||||||
* Creates a new captcha for the given peer UUID.
|
* Creates a new captcha for the given peer UUID.
|
||||||
*
|
*
|
||||||
* @param string|PeerDatabaseRecord $peer_uuid The UUID of the peer to create the captcha for.
|
* @param string|PeerDatabaseRecord $peerUuid The UUID of the peer to create the captcha for.
|
||||||
* @return string The answer to the captcha.
|
* @return string The answer to the captcha.
|
||||||
* @throws DatabaseOperationException If the operation fails.
|
* @throws DatabaseOperationException If the operation fails.
|
||||||
*/
|
*/
|
||||||
public static function createCaptcha(string|PeerDatabaseRecord $peer_uuid): string
|
public static function createCaptcha(string|PeerDatabaseRecord $peerUuid): string
|
||||||
{
|
{
|
||||||
// If the peer_uuid is a RegisteredPeerRecord, get the UUID
|
// If the peer_uuid is a RegisteredPeerRecord, get the UUID
|
||||||
if($peer_uuid instanceof PeerDatabaseRecord)
|
if($peerUuid instanceof PeerDatabaseRecord)
|
||||||
{
|
{
|
||||||
$peer_uuid = $peer_uuid->getUuid();
|
$peerUuid = $peerUuid->getUuid();
|
||||||
|
}
|
||||||
|
elseif(!Validator::validateUuid($peerUuid))
|
||||||
|
{
|
||||||
|
throw new InvalidArgumentException('The given internal peer UUID is not a valid UUID V4');
|
||||||
}
|
}
|
||||||
|
|
||||||
$answer = Utilities::randomString(6, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789');
|
$answer = Utilities::randomString(6, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789');
|
||||||
$current_time = (new DateTime())->setTimestamp(time())->format('Y-m-d H:i:s');
|
$current_time = (new DateTime())->setTimestamp(time())->format('Y-m-d H:i:s');
|
||||||
|
|
||||||
if(!self::captchaExists($peer_uuid))
|
if(!self::captchaExists($peerUuid))
|
||||||
{
|
{
|
||||||
Logger::getLogger()->debug('Creating a new captcha record for peer ' . $peer_uuid);
|
Logger::getLogger()->debug('Creating a new captcha record for peer ' . $peerUuid);
|
||||||
$statement = Database::getConnection()->prepare("INSERT INTO captcha_images (peer_uuid, created, answer) VALUES (?, ?, ?)");
|
$statement = Database::getConnection()->prepare("INSERT INTO captcha_images (peer_uuid, created, answer) VALUES (?, ?, ?)");
|
||||||
$statement->bindParam(1, $peer_uuid);
|
$statement->bindParam(1, $peerUuid);
|
||||||
$statement->bindParam(2, $current_time);
|
$statement->bindParam(2, $current_time);
|
||||||
$statement->bindParam(3, $answer);
|
$statement->bindParam(3, $answer);
|
||||||
|
|
||||||
|
@ -52,11 +58,11 @@
|
||||||
return $answer;
|
return $answer;
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger::getLogger()->debug('Updating an existing captcha record for peer ' . $peer_uuid);
|
Logger::getLogger()->debug('Updating an existing captcha record for peer ' . $peerUuid);
|
||||||
$statement = Database::getConnection()->prepare("UPDATE captcha_images SET answer=?, status='UNSOLVED', created=? WHERE peer_uuid=?");
|
$statement = Database::getConnection()->prepare("UPDATE captcha_images SET answer=?, status='UNSOLVED', created=? WHERE peer_uuid=?");
|
||||||
$statement->bindParam(1, $answer);
|
$statement->bindParam(1, $answer);
|
||||||
$statement->bindParam(2, $current_time);
|
$statement->bindParam(2, $current_time);
|
||||||
$statement->bindParam(3, $peer_uuid);
|
$statement->bindParam(3, $peerUuid);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -73,25 +79,25 @@
|
||||||
/**
|
/**
|
||||||
* Answers a captcha for the given peer UUID.
|
* Answers a captcha for the given peer UUID.
|
||||||
*
|
*
|
||||||
* @param string|PeerDatabaseRecord $peer_uuid The UUID of the peer to answer the captcha for.
|
* @param string|PeerDatabaseRecord $peerUuid The UUID of the peer to answer the captcha for.
|
||||||
* @param string $answer The answer to the captcha.
|
* @param string $answer The answer to the captcha.
|
||||||
* @return bool True if the answer is correct, false otherwise.
|
* @return bool True if the answer is correct, false otherwise.
|
||||||
* @throws DatabaseOperationException If the operation fails.
|
* @throws DatabaseOperationException If the operation fails.
|
||||||
*/
|
*/
|
||||||
public static function answerCaptcha(string|PeerDatabaseRecord $peer_uuid, string $answer): bool
|
public static function answerCaptcha(string|PeerDatabaseRecord $peerUuid, string $answer): bool
|
||||||
{
|
{
|
||||||
if($peer_uuid instanceof PeerDatabaseRecord)
|
if($peerUuid instanceof PeerDatabaseRecord)
|
||||||
{
|
{
|
||||||
$peer_uuid = $peer_uuid->getUuid();
|
$peerUuid = $peerUuid->getUuid();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return false if the captcha does not exist
|
// Return false if the captcha does not exist
|
||||||
if(!self::captchaExists($peer_uuid))
|
if(!self::captchaExists($peerUuid))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$captcha = self::getCaptcha($peer_uuid);
|
$captcha = self::getCaptcha($peerUuid);
|
||||||
|
|
||||||
// Return false if the captcha has already been solved
|
// Return false if the captcha has already been solved
|
||||||
if($captcha->getStatus() === CaptchaStatus::SOLVED)
|
if($captcha->getStatus() === CaptchaStatus::SOLVED)
|
||||||
|
@ -112,7 +118,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
$statement = Database::getConnection()->prepare("UPDATE captcha_images SET status='SOLVED', answered=NOW() WHERE peer_uuid=?");
|
$statement = Database::getConnection()->prepare("UPDATE captcha_images SET status='SOLVED', answered=NOW() WHERE peer_uuid=?");
|
||||||
$statement->bindParam(1, $peer_uuid);
|
$statement->bindParam(1, $peerUuid);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -129,24 +135,24 @@
|
||||||
/**
|
/**
|
||||||
* Retrieves the captcha record for the given peer UUID.
|
* Retrieves the captcha record for the given peer UUID.
|
||||||
*
|
*
|
||||||
* @param string|PeerDatabaseRecord $peer_uuid The UUID of the peer to retrieve the captcha for.
|
* @param string|PeerDatabaseRecord $peerUuid The UUID of the peer to retrieve the captcha for.
|
||||||
* @return CaptchaRecord|null The captcha record.
|
* @return CaptchaRecord|null The captcha record.
|
||||||
* @throws DatabaseOperationException If the operation fails.
|
* @throws DatabaseOperationException If the operation fails.
|
||||||
*/
|
*/
|
||||||
public static function getCaptcha(string|PeerDatabaseRecord $peer_uuid): ?CaptchaRecord
|
public static function getCaptcha(string|PeerDatabaseRecord $peerUuid): ?CaptchaRecord
|
||||||
{
|
{
|
||||||
// If the peer_uuid is a RegisteredPeerRecord, get the UUID
|
// If the peer_uuid is a RegisteredPeerRecord, get the UUID
|
||||||
if($peer_uuid instanceof PeerDatabaseRecord)
|
if($peerUuid instanceof PeerDatabaseRecord)
|
||||||
{
|
{
|
||||||
$peer_uuid = $peer_uuid->getUuid();
|
$peerUuid = $peerUuid->getUuid();
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger::getLogger()->debug('Getting the captcha record for peer ' . $peer_uuid);
|
Logger::getLogger()->debug('Getting the captcha record for peer ' . $peerUuid);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
$statement = Database::getConnection()->prepare("SELECT * FROM captcha_images WHERE peer_uuid=? LIMIT 1");
|
$statement = Database::getConnection()->prepare("SELECT * FROM captcha_images WHERE peer_uuid=? LIMIT 1");
|
||||||
$statement->bindParam(1, $peer_uuid);
|
$statement->bindParam(1, $peerUuid);
|
||||||
$statement->execute();
|
$statement->execute();
|
||||||
$result = $statement->fetch();
|
$result = $statement->fetch();
|
||||||
}
|
}
|
||||||
|
@ -166,24 +172,24 @@
|
||||||
/**
|
/**
|
||||||
* Checks if a captcha exists for the given peer UUID.
|
* Checks if a captcha exists for the given peer UUID.
|
||||||
*
|
*
|
||||||
* @param string|PeerDatabaseRecord $peer_uuid The UUID of the peer to check for a captcha.
|
* @param string|PeerDatabaseRecord $peerUuid The UUID of the peer to check for a captcha.
|
||||||
* @return bool True if a captcha exists, false otherwise.
|
* @return bool True if a captcha exists, false otherwise.
|
||||||
* @throws DatabaseOperationException If the operation fails.
|
* @throws DatabaseOperationException If the operation fails.
|
||||||
*/
|
*/
|
||||||
public static function captchaExists(string|PeerDatabaseRecord $peer_uuid): bool
|
public static function captchaExists(string|PeerDatabaseRecord $peerUuid): bool
|
||||||
{
|
{
|
||||||
// If the peer_uuid is a RegisteredPeerRecord, get the UUID
|
// If the peer_uuid is a RegisteredPeerRecord, get the UUID
|
||||||
if($peer_uuid instanceof PeerDatabaseRecord)
|
if($peerUuid instanceof PeerDatabaseRecord)
|
||||||
{
|
{
|
||||||
$peer_uuid = $peer_uuid->getUuid();
|
$peerUuid = $peerUuid->getUuid();
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger::getLogger()->debug('Checking if a captcha exists for peer ' . $peer_uuid);
|
Logger::getLogger()->debug('Checking if a captcha exists for peer ' . $peerUuid);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
$statement = Database::getConnection()->prepare("SELECT COUNT(*) FROM captcha_images WHERE peer_uuid=?");
|
$statement = Database::getConnection()->prepare("SELECT COUNT(*) FROM captcha_images WHERE peer_uuid=?");
|
||||||
$statement->bindParam(1, $peer_uuid);
|
$statement->bindParam(1, $peerUuid);
|
||||||
$statement->execute();
|
$statement->execute();
|
||||||
$result = $statement->fetchColumn();
|
$result = $statement->fetchColumn();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue