Added captcha base and driver base

This commit is contained in:
Zi Xing 2022-01-13 21:07:01 -05:00
parent 9acbbe4f00
commit 9d937ed146
6 changed files with 259 additions and 0 deletions

View File

@ -0,0 +1,8 @@
<?php
namespace vCaptcha\Abstracts;
abstract class CaptchaType
{
const ImageTextScramble = 'IMAGE_TEXT_SCRAMBLE';
}

View File

@ -0,0 +1,12 @@
<?php
namespace vCaptcha\Abstracts;
abstract class CountryFilterMode
{
const Disabled = 'DISABLED';
const Whitelist = 'WHITELIST';
const Blacklist = 'BLACKLIST';
}

View File

@ -4,7 +4,12 @@
namespace vCaptcha\Managers;
use msqg\QueryBuilder;
use Symfony\Component\Uid\Uuid;
use vCaptcha\Objects\CaptchaInstance;
use vCaptcha\Objects\CaptchaInstance\FirewallOptions;
use vCaptcha\vCaptcha;
use ZiProto\ZiProto;
class CaptchaInstanceManager
{
@ -17,4 +22,26 @@
{
$this->vcaptcha = $vcaptcha;
}
public function createCaptcha(string $captcha_type, string $owner_id): CaptchaInstance
{
$CaptchaInstance = new CaptchaInstance();
$CaptchaInstance->ID = Uuid::v4()->toRfc4122();
$CaptchaInstance->CaptchaType = $captcha_type;
$CaptchaInstance->OwnerID = $owner_id;
$CaptchaInstance->Enabled = true;
$CaptchaInstance->FirewallOptions = new FirewallOptions();
$CaptchaInstance->CreatedTimestamp = time();
$CaptchaInstance->LastUpdatedTimestamp = time();
$Query = QUeryBuilder::insert_into('instances', [
'id' => $this->vcaptcha->getDatabase()->real_escape_string($CaptchaInstance->ID),
'captcha_type' => $this->vcaptcha->getDatabase()->real_escape_string($CaptchaInstance->CaptchaType),
'owner_id' => $this->vcaptcha->getDatabase()->real_escape_string($CaptchaInstance->OwnerID),
'enabled' => (int)$CaptchaInstance->Enabled,
'firewall_options' => $this->vcaptcha->getDatabase()->real_escape_string(ZiProto::encode($CaptchaInstance->FirewallOptions->toArray())),
'created_timestamp' => $this->vcaptcha->getDatabase()->real_escape_string($CaptchaInstance->CreatedTimestamp),
'last_updated_timestamp' => $this->vcaptcha->getDatabase()->real_escape_string($CaptchaInstance->LastUpdatedTimestamp)
]);
}
}

View File

@ -0,0 +1,123 @@
<?php
/** @noinspection PhpMissingFieldTypeInspection */
namespace vCaptcha\Objects;
use vCaptcha\Abstracts\CaptchaType;
use vCaptcha\Objects\CaptchaInstance\FirewallOptions;
class CaptchaInstance
{
/**
* The ID of the captcha instance
*
* @var string
*/
public $ID;
/**
* The ID of the owner of this captcha instance
*
* @var string
*/
public $OwnerID;
/**
* The secret key used for creating and validating
*
* @var string
*/
public $SecretKey;
/**
* Indicates if the captcha instance is enabled or not
*
* @var bool
*/
public $Enabled;
/**
* The captcha type that is used for this captcha
*
* @var string|CaptchaType
*/
public $CaptchaType;
/**
* Firewall options for this captcha instance
*
* @var FirewallOptions
*/
public $FirewallOptions;
/**
* The Unix Timestamp for when this captcha instance was last updated
*
* @var int
*/
public $LastUpdatedTimestamp;
/**
* The Unix Timestamp for when this captcha instance was created
*
* @var int
*/
public $CreatedTimestamp;
/**
* Returns an array representation of the object
*
* @return array
*/
public function toArray(): array
{
return [
'id' => $this->ID,
'owner_id' => $this->OwnerID,
'secret_key' => $this->SecretKey,
'enabled' => $this->Enabled,
'captcha_type' => $this->CaptchaType,
'firewall_options' => $this->FirewallOptions->toArray(),
'last_updated_timestamp' => $this->LastUpdatedTimestamp,
'created_timestamp' => $this->CreatedTimestamp
];
}
/**
* Constructs object from an array representation
*
* @param array $data
* @return CaptchaInstance
*/
public static function fromArray(array $data): CaptchaInstance
{
$CaptchaInstanceObject = new CaptchaInstance();
if(isset($data['id']))
$CaptchaInstanceObject->ID = $data['id'];
if(isset($data['owner_id']))
$CaptchaInstanceObject->OwnerID = $data['owner_id'];
if(isset($data['secret_key']))
$CaptchaInstanceObject->SecretKey = $data['secret_key'];
if(isset($data['enabled']))
$CaptchaInstanceObject->Enabled = $data['enabled'];
if(isset($data['captcha_type']))
$CaptchaInstanceObject->CaptchaType = $data['captcha_type'];
if(isset($data['firewall_options']))
$CaptchaInstanceObject->FirewallOptions = FirewallOptions::fromArray($data['firewall_options']);
if(isset($data['last_updated_timestamp']))
$CaptchaInstanceObject->LastUpdatedTimestamp = $data['last_updated_timestamp'];
if(isset($data['created_timestamp']))
$CaptchaInstanceObject->CreatedTimestamp = $data['created_timestamp'];
return $CaptchaInstanceObject;
}
}

View File

@ -0,0 +1,83 @@
<?php
/** @noinspection PhpMissingFieldTypeInspection */
namespace vCaptcha\Objects\CaptchaInstance;
use vCaptcha\Abstracts\CountryFilterMode;
class FirewallOptions
{
/**
* Indicates if tor connections should be blocked
*
* @var bool
*/
public $DisallowTor;
/**
* Indicates if abusive hosts should be blocked
*
* @var bool
*/
public $DisallowAbusiveHosts;
/**
*
* The list of country codes
*
* @var string[]
*/
public $CountryFilterList;
/**
* @var string|CountryFilterMode
*/
public $CountryFilterMode;
public function __construct()
{
$this->DisallowTor = false;
$this->DisallowAbusiveHosts = true;
$this->CountryFilterList = [];
$this->CountryFilterMode = CountryFilterMode::Disabled;
}
/**
* @return array
*/
public function toArray(): array
{
return [
'disallow_tor' => $this->DisallowTor,
'disallow_abusive_hosts' => $this->DisallowAbusiveHosts,
'country_filter_list' => $this->CountryFilterList,
'country_filter_mode' => $this->CountryFilterMode
];
}
/**
* Constructs object from an array representation of the object
*
* @param array $data
* @return FirewallOptions
*/
public static function fromArray(array $data): FirewallOptions
{
$FirewallOptionsObject = new FirewallOptions();
if(isset($data['disallow_tor']))
$FirewallOptionsObject->DisallowTor = (bool)$data['disallow_tor'];
if(isset($data['disallow_abusive_hosts']))
$FirewallOptionsObject->DisallowAbusiveHosts = (bool)$data['disallow_abusive_hosts'];
if(isset($data['country_filter_list']))
$FirewallOptionsObject->CountryFilterList = $data['country_filter_list'];
if(isset($data['country_filter_mode']))
$FirewallOptionsObject->CountryFilterMode = $data['country_filter_mode'];
return $FirewallOptionsObject;
}
}

View File

@ -25,6 +25,12 @@
"version": "latest",
"source": "default@github/intellivoid/acm2",
"required": true
},
{
"package": "com.symfony.uid",
"version": "latest",
"source": "symfony@composer/uid",
"required": true
}
],
"configuration": {